4 # use File::Temp qw(tempfile);
6 # Checking cross compile
7 $hostos = `uname -s | sed -e s/\-.*//`; chop($hostos);
8 $hostarch = `uname -m | sed -e s/i.86/x86/`;chop($hostarch);
9 $hostarch = `uname -p` if ($hostos eq "AIX");
10 $hostarch = "x86_64" if ($hostarch eq "amd64");
11 $hostarch = "arm" if ($hostarch =~ /^arm.*/);
12 $hostarch = "arm64" if ($hostarch eq "aarch64");
13 $hostarch = "power" if ($hostarch =~ /^(powerpc|ppc).*/);
14 $hostarch = "zarch" if ($hostarch eq "s390x");
16 #$tmpf = new File::Temp( UNLINK => 1 );
17 $binary = $ENV{"BINARY"};
19 $makefile = shift(@ARGV);
20 $config = shift(@ARGV);
22 $compiler_name = shift(@ARGV);
23 $flags = join(" ", @ARGV);
25 # First, we need to know the target OS and compiler name
27 $data = `$compiler_name $flags -E ctest.c`;
30 printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
36 eval "use File::Basename";
38 warn "could not load PERL module File::Basename, emulating its functionality";
39 my $dirnam = substr($compiler_name, 0, rindex($compiler_name, "/")-1 );
41 $cross_suffix .= $dirnam . "/";
43 my $basnam = substr($compiler_name, rindex($compiler_name,"/")+1, length($compiler_name)-rindex($compiler_name,"/")-1);
44 if ($basnam =~ /([^\s]*-)(.*)/) {
48 if (dirname($compiler_name) ne ".") {
49 $cross_suffix .= dirname($compiler_name) . "/";
52 if (basename($compiler_name) =~ /([^\s]*-)(.*)/) {
58 $compiler = LSB if ($data =~ /COMPILER_LSB/);
59 $compiler = CLANG if ($data =~ /COMPILER_CLANG/);
60 $compiler = PGI if ($data =~ /COMPILER_PGI/);
61 $compiler = PATHSCALE if ($data =~ /COMPILER_PATHSCALE/);
62 $compiler = INTEL if ($data =~ /COMPILER_INTEL/);
63 $compiler = OPEN64 if ($data =~ /COMPILER_OPEN64/);
64 $compiler = SUN if ($data =~ /COMPILER_SUN/);
65 $compiler = IBM if ($data =~ /COMPILER_IBM/);
66 $compiler = DEC if ($data =~ /COMPILER_DEC/);
67 $compiler = GCC if ($compiler eq "");
69 $os = Linux if ($data =~ /OS_LINUX/);
70 $os = FreeBSD if ($data =~ /OS_FREEBSD/);
71 $os = NetBSD if ($data =~ /OS_NETBSD/);
72 $os = OpenBSD if ($data =~ /OS_OPENBSD/);
73 $os = DragonFly if ($data =~ /OS_DRAGONFLY/);
74 $os = Darwin if ($data =~ /OS_DARWIN/);
75 $os = SunOS if ($data =~ /OS_SUNOS/);
76 $os = AIX if ($data =~ /OS_AIX/);
77 $os = osf if ($data =~ /OS_OSF/);
78 $os = WINNT if ($data =~ /OS_WINNT/);
79 $os = CYGWIN_NT if ($data =~ /OS_CYGWIN_NT/);
80 $os = Interix if ($data =~ /OS_INTERIX/);
81 $os = Android if ($data =~ /OS_ANDROID/);
82 $os = Haiku if ($data =~ /OS_HAIKU/);
84 $architecture = x86 if ($data =~ /ARCH_X86/);
85 $architecture = x86_64 if ($data =~ /ARCH_X86_64/);
86 $architecture = power if ($data =~ /ARCH_POWER/);
87 $architecture = mips if ($data =~ /ARCH_MIPS/);
88 $architecture = mips64 if ($data =~ /ARCH_MIPS64/);
89 $architecture = alpha if ($data =~ /ARCH_ALPHA/);
90 $architecture = sparc if ($data =~ /ARCH_SPARC/);
91 $architecture = ia64 if ($data =~ /ARCH_IA64/);
92 $architecture = arm if ($data =~ /ARCH_ARM/);
93 $architecture = arm64 if ($data =~ /ARCH_ARM64/);
94 $architecture = zarch if ($data =~ /ARCH_ZARCH/);
99 $compiler_name .= " -maix32" if ($binary eq "32");
100 $compiler_name .= " -maix64" if ($binary eq "64");
104 if ($architecture eq "mips") {
105 $compiler_name .= " -mabi=32";
109 if ($architecture eq "mips64") {
110 $compiler_name .= " -mabi=n32" if ($binary eq "32");
111 $compiler_name .= " -mabi=64" if ($binary eq "64");
115 if (($architecture eq "arm") || ($architecture eq "arm64")) {
119 if ($architecture eq "zarch") {
124 if ($architecture eq "alpha") {
129 if ($architecture eq "ia64") {
134 if (($architecture eq "x86") && ($os ne Darwin) && ($os ne SunOS)) {
139 if ($compiler eq "PGI") {
140 $compiler_name .= " -tp p7" if ($binary eq "32");
141 $compiler_name .= " -tp p7-64" if ($binary eq "64");
146 if ($compiler eq "IBM") {
147 $compiler_name .= " -q32" if ($binary eq "32");
148 $compiler_name .= " -q64" if ($binary eq "64");
149 $openmp = "-qsmp=omp";
153 if ($compiler eq "INTEL") {
157 if ($compiler eq "PATHSCALE") {
161 if ($compiler eq "OPEN64") {
165 if ($compiler eq "CLANG") {
166 $openmp = "-fopenmp";
169 if ($compiler eq "GCC" || $compiler eq "LSB") {
170 $openmp = "-fopenmp";
174 $compiler_name .= " -m32" if ($binary eq "32");
175 $compiler_name .= " -m64" if ($binary eq "64");
180 $data = `$compiler_name $flags -E ctest.c`;
183 printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
188 if (($architecture eq "mips") || ($architecture eq "mips64")) {
189 eval "use File::Temp qw(tempfile)";
191 warn "could not load PERL module File::Temp, so could not check MSA capatibility";
193 $tmpf = new File::Temp( SUFFIX => '.c' , UNLINK => 1 );
194 $code = '"addvi.b $w0, $w1, 1"';
195 $msa_flags = "-mmsa -mfp64 -msched-weight -mload-store-pairs";
196 print $tmpf "#include <msa.h>\n\n";
197 print $tmpf "void main(void){ __asm__ volatile($code); }\n";
199 $args = "$msa_flags -o $tmpf.o $tmpf";
200 my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
211 $architecture = x86 if ($data =~ /ARCH_X86/);
212 $architecture = x86_64 if ($data =~ /ARCH_X86_64/);
213 $architecture = power if ($data =~ /ARCH_POWER/);
214 $architecture = mips if ($data =~ /ARCH_MIPS/);
215 $architecture = mips64 if ($data =~ /ARCH_MIPS64/);
216 $architecture = alpha if ($data =~ /ARCH_ALPHA/);
217 $architecture = sparc if ($data =~ /ARCH_SPARC/);
218 $architecture = ia64 if ($data =~ /ARCH_IA64/);
219 $architecture = arm if ($data =~ /ARCH_ARM/);
220 $architecture = arm64 if ($data =~ /ARCH_ARM64/);
221 $architecture = zarch if ($data =~ /ARCH_ZARCH/);
224 $binformat = bin64 if ($data =~ /BINARY_64/);
227 if (($architecture eq "x86") || ($architecture eq "x86_64")) {
228 eval "use File::Temp qw(tempfile)";
230 warn "could not load PERL module File::Temp, so could not check compiler compatibility with AVX512";
233 # $tmpf = new File::Temp( UNLINK => 1 );
234 ($fh,$tmpf) = tempfile( SUFFIX => '.c' , UNLINK => 1 );
235 $code = '"vbroadcastss -4 * 4(%rsi), %zmm2"';
236 print $tmpf "#include <immintrin.h>\n\nint main(void){ __asm__ volatile($code); }\n";
237 $args = " -march=skylake-avx512 -c -o $tmpf.o $tmpf";
238 if ($compiler eq "PGI") {
239 $args = " -tp skylake -c -o $tmpf.o $tmpf";
241 my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
253 if ($data =~ /HAVE_C11/) {
254 eval "use File::Temp qw(tempfile)";
256 warn "could not load PERL module File::Temp, so could not check compiler compatibility with C11";
259 ($fh,$tmpf) = tempfile( SUFFIX => '.c' , UNLINK => 1 );
260 print $tmpf "#include <stdatomic.h>\nint main(void){}\n";
261 $args = " -c -o $tmpf.o $tmpf";
262 my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
274 $data = `$compiler_name $flags -S ctest1.c && grep globl ctest1.s | head -n 1 && rm -f ctest1.s`;
276 $data =~ /globl\s([_\.]*)(.*)/;
282 if ($architecture ne $hostarch) {
284 $cross = 0 if (($hostarch eq "x86_64") && ($architecture eq "x86"));
285 $cross = 0 if (($hostarch eq "mips64") && ($architecture eq "mips"));
288 $cross = 1 if ($os ne $hostos);
290 $openmp = "" if $ENV{USE_OPENMP} != 1;
297 $link = `$compiler_name $flags -c ctest2.c -o ctest2.o 2>&1 && $compiler_name $flags $openmp -v ctest2.o -o ctest2 2>&1 && rm -f ctest2.o ctest2 ctest2.exe`;
299 $link =~ s/\-Y\sP\,/\-Y/g;
301 @flags = split(/[\s\,\n]/, $link);
302 # remove leading and trailing quotes from each flag.
303 @flags = map {s/^['"]|['"]$//g; $_} @flags;
305 foreach $flags (@flags) {
308 && ($flags !~ /^-LIST:/)
309 && ($flags !~ /^-LANG:/)
311 $linker_L .= $flags . " "
314 if ($flags =~ /^\-Y/) {
315 $linker_L .= "-Wl,". $flags . " "
318 if ($flags =~ /^\--exclude-libs/) {
319 $linker_L .= "-Wl,". $flags . " ";
325 && ($flags !~ /gfortranbegin/)
326 && ($flags !~ /frtbegin/)
327 && ($flags !~ /pathfstart/)
328 && ($flags !~ /numa/)
329 && ($flags !~ /crt[0-9]/)
331 && ($flags !~ /user32/)
332 && ($flags !~ /kernel32/)
333 && ($flags !~ /advapi32/)
334 && ($flags !~ /shell32/)
336 && ($flags !~ /[0-9]+/)
338 $linker_l .= $flags . " "
341 $linker_a .= $flags . " " if $flags =~ /\.a$/;
346 open(MAKEFILE, "> $makefile") || die "Can't create $makefile";
347 open(CONFFILE, "> $config" ) || die "Can't create $config";
351 print MAKEFILE "OSNAME=$os\n";
352 print MAKEFILE "ARCH=$architecture\n";
353 print MAKEFILE "C_COMPILER=$compiler\n";
354 print MAKEFILE "BINARY32=\n" if $binformat ne bin32;
355 print MAKEFILE "BINARY64=\n" if $binformat ne bin64;
356 print MAKEFILE "BINARY32=1\n" if $binformat eq bin32;
357 print MAKEFILE "BINARY64=1\n" if $binformat eq bin64;
358 print MAKEFILE "FU=$need_fu\n" if $need_fu ne "";
359 print MAKEFILE "CROSS_SUFFIX=$cross_suffix\n" if $cross != 0 && $cross_suffix ne "";
360 print MAKEFILE "CROSS=1\n" if $cross != 0;
361 print MAKEFILE "CEXTRALIB=$linker_L $linker_l $linker_a\n";
362 print MAKEFILE "HAVE_MSA=1\n" if $have_msa eq 1;
363 print MAKEFILE "MSA_FLAGS=$msa_flags\n" if $have_msa eq 1;
364 print MAKEFILE "NO_AVX512=1\n" if $no_avx512 eq 1;
366 $os =~ tr/[a-z]/[A-Z]/;
367 $architecture =~ tr/[a-z]/[A-Z]/;
368 $compiler =~ tr/[a-z]/[A-Z]/;
370 print CONFFILE "#define OS_$os\t1\n";
371 print CONFFILE "#define ARCH_$architecture\t1\n";
372 print CONFFILE "#define C_$compiler\t1\n";
373 print CONFFILE "#define __32BIT__\t1\n" if $binformat eq bin32;
374 print CONFFILE "#define __64BIT__\t1\n" if $binformat eq bin64;
375 print CONFFILE "#define FUNDERSCORE\t$need_fu\n" if $need_fu ne "";
376 print CONFFILE "#define HAVE_MSA\t1\n" if $have_msa eq 1;
377 print CONFFILE "#define HAVE_C11\t1\n" if $c11_atomics eq 1;
380 if ($os eq "LINUX") {
382 # @pthread = split(/\s+/, `nm /lib/libpthread.so* | grep _pthread_create`);
384 # if ($pthread[2] ne "") {
385 # print CONFFILE "#define PTHREAD_CREATE_FUNC $pthread[2]\n";
387 print CONFFILE "#define PTHREAD_CREATE_FUNC pthread_create\n";
390 print CONFFILE "#define PTHREAD_CREATE_FUNC pthread_create\n";