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 = e2k if ($data =~ /ARCH_E2K/);
88 $architecture = power if ($data =~ /ARCH_POWER/);
89 $architecture = mips if ($data =~ /ARCH_MIPS/);
90 $architecture = mips64 if ($data =~ /ARCH_MIPS64/);
91 $architecture = alpha if ($data =~ /ARCH_ALPHA/);
92 $architecture = sparc if ($data =~ /ARCH_SPARC/);
93 $architecture = ia64 if ($data =~ /ARCH_IA64/);
94 $architecture = arm if ($data =~ /ARCH_ARM/);
95 $architecture = arm64 if ($data =~ /ARCH_ARM64/);
96 $architecture = zarch if ($data =~ /ARCH_ZARCH/);
97 $architecture = riscv64 if ($data =~ /ARCH_RISCV64/);
98 $architecture = loongarch64 if ($data =~ /ARCH_LOONGARCH64/);
103 $compiler_name .= " -maix32" if ($binary eq "32");
104 $compiler_name .= " -maix64" if ($binary eq "64");
108 if ($architecture eq "mips") {
109 $compiler_name .= " -mabi=32";
113 if ($architecture eq "mips64") {
114 $compiler_name .= " -mabi=n32" if ($binary eq "32");
115 $compiler_name .= " -mabi=64" if ($binary eq "64");
119 if (($architecture eq "arm") || ($architecture eq "arm64")) {
123 if ($architecture eq "zarch") {
128 if ($architecture eq "e2k") {
133 if ($architecture eq "alpha") {
138 if ($architecture eq "ia64") {
143 if (($architecture eq "x86") && ($os ne Darwin) && ($os ne SunOS)) {
148 if ($architecture eq "riscv64") {
153 if ($architecture eq "loongarch64") {
158 if ($compiler eq "PGI") {
159 $compiler_name .= " -tp p7" if ($binary eq "32");
160 $compiler_name .= " -tp p7-64" if ($binary eq "64");
165 if ($compiler eq "IBM") {
166 $compiler_name .= " -q32" if ($binary eq "32");
167 $compiler_name .= " -q64" if ($binary eq "64");
168 $openmp = "-qsmp=omp";
172 if ($compiler eq "INTEL") {
176 if ($compiler eq "PATHSCALE") {
180 if ($compiler eq "OPEN64") {
184 if ($compiler eq "CLANG") {
185 $openmp = "-fopenmp";
188 if ($compiler eq "GCC" || $compiler eq "LSB") {
189 $openmp = "-fopenmp";
193 $compiler_name .= " -m32" if ($binary eq "32");
194 $compiler_name .= " -m64" if ($binary eq "64");
199 $data = `$compiler_name $flags -E ctest.c`;
202 printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
207 if (($architecture eq "mips") || ($architecture eq "mips64")) {
208 eval "use File::Temp qw(tempfile)";
210 warn "could not load PERL module File::Temp, so could not check MSA capatibility";
212 $tmpf = new File::Temp( SUFFIX => '.c' , UNLINK => 1 );
213 $code = '"addvi.b $w0, $w1, 1"';
214 $msa_flags = "-mmsa -mfp64 -mload-store-pairs";
215 print $tmpf "#include <msa.h>\n\n";
216 print $tmpf "void main(void){ __asm__ volatile($code); }\n";
218 $args = "$msa_flags -o $tmpf.o $tmpf";
219 my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
230 $architecture = x86 if ($data =~ /ARCH_X86/);
231 $architecture = x86_64 if ($data =~ /ARCH_X86_64/);
232 $architecture = e2k if ($data =~ /ARCH_E2K/);
233 $architecture = power if ($data =~ /ARCH_POWER/);
234 $architecture = mips if ($data =~ /ARCH_MIPS/);
235 $architecture = mips64 if ($data =~ /ARCH_MIPS64/);
236 $architecture = alpha if ($data =~ /ARCH_ALPHA/);
237 $architecture = sparc if ($data =~ /ARCH_SPARC/);
238 $architecture = ia64 if ($data =~ /ARCH_IA64/);
239 $architecture = arm if ($data =~ /ARCH_ARM/);
240 $architecture = arm64 if ($data =~ /ARCH_ARM64/);
241 $architecture = zarch if ($data =~ /ARCH_ZARCH/);
242 $architecture = loongarch64 if ($data =~ /ARCH_LOONGARCH64/);
245 $binformat = bin64 if ($data =~ /BINARY_64/);
248 if (($architecture eq "x86") || ($architecture eq "x86_64")) {
249 eval "use File::Temp qw(tempfile)";
251 warn "could not load PERL module File::Temp, so could not check compiler compatibility with AVX512";
254 # $tmpf = new File::Temp( UNLINK => 1 );
255 ($fh,$tmpf) = tempfile( SUFFIX => '.c' , UNLINK => 1 );
256 $code = '"vbroadcastss -4 * 4(%rsi), %zmm2"';
257 print $fh "#include <immintrin.h>\n\nint main(void){ __asm__ volatile($code); }\n";
258 $args = " -march=skylake-avx512 -c -o $tmpf.o $tmpf";
259 if ($compiler eq "PGI") {
260 $args = " -tp skylake -c -o $tmpf.o $tmpf";
262 my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
274 if (($architecture eq "riscv64")) {
275 eval "use File::Temp qw(tempfile)";
277 warn "could not load PERL module File::Temp, so could not check compiler compatibility with the RISCV vector extension";
280 # $tmpf = new File::Temp( UNLINK => 1 );
281 ($fh,$tmpf) = tempfile( SUFFIX => '.c' , UNLINK => 1 );
282 $code = '"vsetvli zero, zero, e8, m1\n"';
283 print $fh "int main(void){ __asm__ volatile($code); }\n";
284 $args = " -march=rv64gv -c -o $tmpf.o $tmpf";
285 my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
297 if ($data =~ /HAVE_C11/) {
298 eval "use File::Temp qw(tempfile)";
300 warn "could not load PERL module File::Temp, so could not check compiler compatibility with C11";
303 ($fh,$tmpf) = tempfile( SUFFIX => '.c' , UNLINK => 1 );
304 print $fh "#include <stdatomic.h>\nint main(void){}\n";
305 $args = " -c -o $tmpf.o $tmpf";
306 my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
317 if ($compiler eq "GCC" &&( ($architecture eq "x86") || ($architecture eq "x86_64"))) {
320 $data = `$compiler_name -dumpversion`;
327 $data = `$compiler_name $flags -S ctest1.c && grep globl ctest1.s | head -n 1 && rm -f ctest1.s`;
329 $data =~ /globl\s([_\.]*)(.*)/;
335 if ($architecture ne $hostarch) {
337 $cross = 0 if (($hostarch eq "x86_64") && ($architecture eq "x86"));
338 $cross = 0 if (($hostarch eq "mips64") && ($architecture eq "mips"));
341 $cross = 1 if ($os ne $hostos);
342 $cross = 0 if (($os eq "Android") && ($hostos eq "Linux") && ($ENV{TERMUX_APP_PID} != ""));
344 $openmp = "" if $ENV{USE_OPENMP} != 1;
351 $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`;
353 $link =~ s/\-Y\sP\,/\-Y/g;
355 @flags = split(/[\s\,\n]/, $link);
356 # remove leading and trailing quotes from each flag.
357 @flags = map {s/^['"]|['"]$//g; $_} @flags;
359 foreach $flags (@flags) {
362 && ($flags !~ /^-LIST:/)
363 && ($flags !~ /^-LANG:/)
365 $linker_L .= $flags . " "
368 if ($flags =~ /^\-Y/) {
369 $linker_L .= "-Wl,". $flags . " "
372 if ($flags =~ /^\--exclude-libs/) {
373 $linker_L .= "-Wl,". $flags . " ";
379 && ($flags !~ /gfortranbegin/)
380 && ($flags !~ /frtbegin/)
381 && ($flags !~ /pathfstart/)
382 && ($flags !~ /numa/)
383 && ($flags !~ /crt[0-9]/)
385 && ($flags !~ /user32/)
386 && ($flags !~ /kernel32/)
387 && ($flags !~ /advapi32/)
388 && ($flags !~ /shell32/)
390 && ($flags !~ /[0-9]+/)
392 $linker_l .= $flags . " "
395 $linker_a .= $flags . " " if $flags =~ /\.a$/;
400 open(MAKEFILE, "> $makefile") || die "Can't create $makefile";
401 open(CONFFILE, "> $config" ) || die "Can't create $config";
405 print MAKEFILE "OSNAME=$os\n";
406 print MAKEFILE "ARCH=$architecture\n";
407 print MAKEFILE "C_COMPILER=$compiler\n";
408 print MAKEFILE "BINARY32=\n" if $binformat ne bin32;
409 print MAKEFILE "BINARY64=\n" if $binformat ne bin64;
410 print MAKEFILE "BINARY32=1\n" if $binformat eq bin32;
411 print MAKEFILE "BINARY64=1\n" if $binformat eq bin64;
412 print MAKEFILE "FU=$need_fu\n" if $need_fu ne "";
413 print MAKEFILE "CROSS_SUFFIX=$cross_suffix\n" if $cross != 0 && $cross_suffix ne "";
414 print MAKEFILE "CROSS=1\n" if $cross != 0;
415 print MAKEFILE "CEXTRALIB=$linker_L $linker_l $linker_a\n";
416 print MAKEFILE "HAVE_MSA=1\n" if $have_msa eq 1;
417 print MAKEFILE "MSA_FLAGS=$msa_flags\n" if $have_msa eq 1;
418 print MAKEFILE "NO_RV64GV=1\n" if $no_rv64gv eq 1;
419 print MAKEFILE "NO_AVX512=1\n" if $no_avx512 eq 1;
420 print MAKEFILE "NO_AVX2=1\n" if $no_avx2 eq 1;
421 print MAKEFILE "OLDGCC=1\n" if $oldgcc eq 1;
423 $os =~ tr/[a-z]/[A-Z]/;
424 $architecture =~ tr/[a-z]/[A-Z]/;
425 $compiler =~ tr/[a-z]/[A-Z]/;
427 print CONFFILE "#define OS_$os\t1\n";
428 print CONFFILE "#define ARCH_$architecture\t1\n";
429 print CONFFILE "#define C_$compiler\t1\n";
430 print CONFFILE "#define __32BIT__\t1\n" if $binformat eq bin32;
431 print CONFFILE "#define __64BIT__\t1\n" if $binformat eq bin64;
432 print CONFFILE "#define FUNDERSCORE\t$need_fu\n" if $need_fu ne "";
433 print CONFFILE "#define HAVE_MSA\t1\n" if $have_msa eq 1;
434 print CONFFILE "#define HAVE_C11\t1\n" if $c11_atomics eq 1;
437 if ($os eq "LINUX") {
439 # @pthread = split(/\s+/, `nm /lib/libpthread.so* | grep _pthread_create`);
441 # if ($pthread[2] ne "") {
442 # print CONFFILE "#define PTHREAD_CREATE_FUNC $pthread[2]\n";
444 print CONFFILE "#define PTHREAD_CREATE_FUNC pthread_create\n";
447 print CONFFILE "#define PTHREAD_CREATE_FUNC pthread_create\n";