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/`;
9 $hostarch = `uname -p` if ($hostos eq "AIX" || $hostos eq "SunOS");
11 $hostarch = "x86_64" if ($hostarch eq "amd64");
12 $hostarch = "arm" if ($hostarch ne "arm64" && $hostarch =~ /^arm.*/);
13 $hostarch = "arm64" if ($hostarch eq "aarch64");
14 $hostarch = "power" if ($hostarch =~ /^(powerpc|ppc).*/);
15 $hostarch = "zarch" if ($hostarch eq "s390x");
17 #$tmpf = new File::Temp( UNLINK => 1 );
18 $binary = $ENV{"BINARY"};
20 $makefile = shift(@ARGV);
21 $config = shift(@ARGV);
23 $compiler_name = shift(@ARGV);
24 $flags = join(" ", @ARGV);
26 # First, we need to know the target OS and compiler name
28 $data = `$compiler_name $flags -E ctest.c`;
31 printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
37 eval "use File::Basename";
39 warn "could not load PERL module File::Basename, emulating its functionality";
40 my $dirnam = substr($compiler_name, 0, rindex($compiler_name, "/")-1 );
42 $cross_suffix .= $dirnam . "/";
44 my $basnam = substr($compiler_name, rindex($compiler_name,"/")+1, length($compiler_name)-rindex($compiler_name,"/")-1);
45 if ($basnam =~ /([^\s]*-)(.*)/) {
49 if (dirname($compiler_name) ne ".") {
50 $cross_suffix .= dirname($compiler_name) . "/";
53 if (basename($compiler_name) =~ /([^\s]*-)(.*)/) {
59 $compiler = LSB if ($data =~ /COMPILER_LSB/);
60 $compiler = CLANG if ($data =~ /COMPILER_CLANG/);
61 $compiler = PGI if ($data =~ /COMPILER_PGI/);
62 $compiler = PATHSCALE if ($data =~ /COMPILER_PATHSCALE/);
63 $compiler = INTEL if ($data =~ /COMPILER_INTEL/);
64 $compiler = OPEN64 if ($data =~ /COMPILER_OPEN64/);
65 $compiler = SUN if ($data =~ /COMPILER_SUN/);
66 $compiler = IBM if ($data =~ /COMPILER_IBM/);
67 $compiler = DEC if ($data =~ /COMPILER_DEC/);
68 $compiler = GCC if ($compiler eq "");
70 $os = Linux if ($data =~ /OS_LINUX/);
71 $os = FreeBSD if ($data =~ /OS_FREEBSD/);
72 $os = NetBSD if ($data =~ /OS_NETBSD/);
73 $os = OpenBSD if ($data =~ /OS_OPENBSD/);
74 $os = DragonFly if ($data =~ /OS_DRAGONFLY/);
75 $os = Darwin if ($data =~ /OS_DARWIN/);
76 $os = SunOS if ($data =~ /OS_SUNOS/);
77 $os = AIX if ($data =~ /OS_AIX/);
78 $os = osf if ($data =~ /OS_OSF/);
79 $os = WINNT if ($data =~ /OS_WINNT/);
80 $os = CYGWIN_NT if ($data =~ /OS_CYGWIN_NT/);
81 $os = Interix if ($data =~ /OS_INTERIX/);
82 $os = Android if ($data =~ /OS_ANDROID/);
83 $os = Haiku if ($data =~ /OS_HAIKU/);
85 $architecture = x86 if ($data =~ /ARCH_X86/);
86 $architecture = x86_64 if ($data =~ /ARCH_X86_64/);
87 $architecture = power if ($data =~ /ARCH_POWER/);
88 $architecture = mips if ($data =~ /ARCH_MIPS/);
89 $architecture = mips64 if ($data =~ /ARCH_MIPS64/);
90 $architecture = alpha if ($data =~ /ARCH_ALPHA/);
91 $architecture = sparc if ($data =~ /ARCH_SPARC/);
92 $architecture = ia64 if ($data =~ /ARCH_IA64/);
93 $architecture = arm if ($data =~ /ARCH_ARM/);
94 $architecture = arm64 if ($data =~ /ARCH_ARM64/);
95 $architecture = zarch if ($data =~ /ARCH_ZARCH/);
96 $architecture = riscv64 if ($data =~ /ARCH_RISCV64/);
101 $compiler_name .= " -maix32" if ($binary eq "32");
102 $compiler_name .= " -maix64" if ($binary eq "64");
106 if ($architecture eq "mips") {
107 $compiler_name .= " -mabi=32";
111 if ($architecture eq "mips64") {
112 $compiler_name .= " -mabi=n32" if ($binary eq "32");
113 $compiler_name .= " -mabi=64" if ($binary eq "64");
117 if (($architecture eq "arm") || ($architecture eq "arm64")) {
121 if ($architecture eq "zarch") {
126 if ($architecture eq "alpha") {
131 if ($architecture eq "ia64") {
136 if (($architecture eq "x86") && ($os ne Darwin) && ($os ne SunOS)) {
141 if ($architecture eq "riscv64") {
146 if ($compiler eq "PGI") {
147 $compiler_name .= " -tp p7" if ($binary eq "32");
148 $compiler_name .= " -tp p7-64" if ($binary eq "64");
153 if ($compiler eq "IBM") {
154 $compiler_name .= " -q32" if ($binary eq "32");
155 $compiler_name .= " -q64" if ($binary eq "64");
156 $openmp = "-qsmp=omp";
160 if ($compiler eq "INTEL") {
164 if ($compiler eq "PATHSCALE") {
168 if ($compiler eq "OPEN64") {
172 if ($compiler eq "CLANG") {
173 $openmp = "-fopenmp";
176 if ($compiler eq "GCC" || $compiler eq "LSB") {
177 $openmp = "-fopenmp";
181 $compiler_name .= " -m32" if ($binary eq "32");
182 $compiler_name .= " -m64" if ($binary eq "64");
187 $data = `$compiler_name $flags -E ctest.c`;
190 printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
195 if (($architecture eq "mips") || ($architecture eq "mips64")) {
196 eval "use File::Temp qw(tempfile)";
198 warn "could not load PERL module File::Temp, so could not check MSA capatibility";
200 $tmpf = new File::Temp( SUFFIX => '.c' , UNLINK => 1 );
201 $code = '"addvi.b $w0, $w1, 1"';
202 $msa_flags = "-mmsa -mfp64 -mload-store-pairs";
203 print $tmpf "#include <msa.h>\n\n";
204 print $tmpf "void main(void){ __asm__ volatile($code); }\n";
206 $args = "$msa_flags -o $tmpf.o $tmpf";
207 my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
218 $architecture = x86 if ($data =~ /ARCH_X86/);
219 $architecture = x86_64 if ($data =~ /ARCH_X86_64/);
220 $architecture = power if ($data =~ /ARCH_POWER/);
221 $architecture = mips if ($data =~ /ARCH_MIPS/);
222 $architecture = mips64 if ($data =~ /ARCH_MIPS64/);
223 $architecture = alpha if ($data =~ /ARCH_ALPHA/);
224 $architecture = sparc if ($data =~ /ARCH_SPARC/);
225 $architecture = ia64 if ($data =~ /ARCH_IA64/);
226 $architecture = arm if ($data =~ /ARCH_ARM/);
227 $architecture = arm64 if ($data =~ /ARCH_ARM64/);
228 $architecture = zarch if ($data =~ /ARCH_ZARCH/);
231 $binformat = bin64 if ($data =~ /BINARY_64/);
234 if (($architecture eq "x86") || ($architecture eq "x86_64")) {
235 eval "use File::Temp qw(tempfile)";
237 warn "could not load PERL module File::Temp, so could not check compiler compatibility with AVX512";
240 # $tmpf = new File::Temp( UNLINK => 1 );
241 ($fh,$tmpf) = tempfile( SUFFIX => '.c' , UNLINK => 1 );
242 $code = '"vbroadcastss -4 * 4(%rsi), %zmm2"';
243 print $tmpf "#include <immintrin.h>\n\nint main(void){ __asm__ volatile($code); }\n";
244 $args = " -march=skylake-avx512 -c -o $tmpf.o $tmpf";
245 if ($compiler eq "PGI") {
246 $args = " -tp skylake -c -o $tmpf.o $tmpf";
248 my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
260 if ($data =~ /HAVE_C11/) {
261 eval "use File::Temp qw(tempfile)";
263 warn "could not load PERL module File::Temp, so could not check compiler compatibility with C11";
266 ($fh,$tmpf) = tempfile( SUFFIX => '.c' , UNLINK => 1 );
267 print $tmpf "#include <stdatomic.h>\nint main(void){}\n";
268 $args = " -c -o $tmpf.o $tmpf";
269 my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
280 if ($compiler eq "GCC" &&( ($architecture eq "x86") || ($architecture eq "x86_64"))) {
283 $data = `$compiler_name -dumpversion`;
290 $data = `$compiler_name $flags -S ctest1.c && grep globl ctest1.s | head -n 1 && rm -f ctest1.s`;
292 $data =~ /globl\s([_\.]*)(.*)/;
298 if ($architecture ne $hostarch) {
300 $cross = 0 if (($hostarch eq "x86_64") && ($architecture eq "x86"));
301 $cross = 0 if (($hostarch eq "mips64") && ($architecture eq "mips"));
304 $cross = 1 if ($os ne $hostos);
306 $openmp = "" if $ENV{USE_OPENMP} != 1;
313 $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`;
315 $link =~ s/\-Y\sP\,/\-Y/g;
317 @flags = split(/[\s\,\n]/, $link);
318 # remove leading and trailing quotes from each flag.
319 @flags = map {s/^['"]|['"]$//g; $_} @flags;
321 foreach $flags (@flags) {
324 && ($flags !~ /^-LIST:/)
325 && ($flags !~ /^-LANG:/)
327 $linker_L .= $flags . " "
330 if ($flags =~ /^\-Y/) {
331 $linker_L .= "-Wl,". $flags . " "
334 if ($flags =~ /^\--exclude-libs/) {
335 $linker_L .= "-Wl,". $flags . " ";
341 && ($flags !~ /gfortranbegin/)
342 && ($flags !~ /frtbegin/)
343 && ($flags !~ /pathfstart/)
344 && ($flags !~ /numa/)
345 && ($flags !~ /crt[0-9]/)
347 && ($flags !~ /user32/)
348 && ($flags !~ /kernel32/)
349 && ($flags !~ /advapi32/)
350 && ($flags !~ /shell32/)
352 && ($flags !~ /[0-9]+/)
354 $linker_l .= $flags . " "
357 $linker_a .= $flags . " " if $flags =~ /\.a$/;
362 open(MAKEFILE, "> $makefile") || die "Can't create $makefile";
363 open(CONFFILE, "> $config" ) || die "Can't create $config";
367 print MAKEFILE "OSNAME=$os\n";
368 print MAKEFILE "ARCH=$architecture\n";
369 print MAKEFILE "C_COMPILER=$compiler\n";
370 print MAKEFILE "BINARY32=\n" if $binformat ne bin32;
371 print MAKEFILE "BINARY64=\n" if $binformat ne bin64;
372 print MAKEFILE "BINARY32=1\n" if $binformat eq bin32;
373 print MAKEFILE "BINARY64=1\n" if $binformat eq bin64;
374 print MAKEFILE "FU=$need_fu\n" if $need_fu ne "";
375 print MAKEFILE "CROSS_SUFFIX=$cross_suffix\n" if $cross != 0 && $cross_suffix ne "";
376 print MAKEFILE "CROSS=1\n" if $cross != 0;
377 print MAKEFILE "CEXTRALIB=$linker_L $linker_l $linker_a\n";
378 print MAKEFILE "HAVE_MSA=1\n" if $have_msa eq 1;
379 print MAKEFILE "MSA_FLAGS=$msa_flags\n" if $have_msa eq 1;
380 print MAKEFILE "NO_AVX512=1\n" if $no_avx512 eq 1;
381 print MAKEFILE "NO_AVX2=1\n" if $no_avx2 eq 1;
382 print MAKEFILE "OLDGCC=1\n" if $oldgcc eq 1;
384 $os =~ tr/[a-z]/[A-Z]/;
385 $architecture =~ tr/[a-z]/[A-Z]/;
386 $compiler =~ tr/[a-z]/[A-Z]/;
388 print CONFFILE "#define OS_$os\t1\n";
389 print CONFFILE "#define ARCH_$architecture\t1\n";
390 print CONFFILE "#define C_$compiler\t1\n";
391 print CONFFILE "#define __32BIT__\t1\n" if $binformat eq bin32;
392 print CONFFILE "#define __64BIT__\t1\n" if $binformat eq bin64;
393 print CONFFILE "#define FUNDERSCORE\t$need_fu\n" if $need_fu ne "";
394 print CONFFILE "#define HAVE_MSA\t1\n" if $have_msa eq 1;
395 print CONFFILE "#define HAVE_C11\t1\n" if $c11_atomics eq 1;
398 if ($os eq "LINUX") {
400 # @pthread = split(/\s+/, `nm /lib/libpthread.so* | grep _pthread_create`);
402 # if ($pthread[2] ne "") {
403 # print CONFFILE "#define PTHREAD_CREATE_FUNC $pthread[2]\n";
405 print CONFFILE "#define PTHREAD_CREATE_FUNC pthread_create\n";
408 print CONFFILE "#define PTHREAD_CREATE_FUNC pthread_create\n";