Merge pull request #3633 from martin-frbg/perl_fallback
[platform/upstream/openblas.git] / c_check.pl
1 #!/usr/bin/env perl
2
3 #use File::Basename;
4 # use File::Temp qw(tempfile);
5
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");
10 chop($hostarch);
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");
16
17 #$tmpf = new File::Temp( UNLINK => 1 );
18 $binary = $ENV{"BINARY"};
19
20 $makefile = shift(@ARGV);
21 $config   = shift(@ARGV);
22
23 $compiler_name = shift(@ARGV);
24 $flags = join(" ", @ARGV);
25
26 # First, we need to know the target OS and compiler name
27
28 $data = `$compiler_name $flags -E ctest.c`;
29
30 if ($?) {
31     printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
32     die 1;
33 }
34
35 $cross_suffix = "";
36
37 eval "use File::Basename";
38 if ($@){ 
39     warn "could not load PERL module File::Basename, emulating its functionality";
40     my $dirnam = substr($compiler_name, 0, rindex($compiler_name, "/")-1 );
41     if ($dirnam ne ".") {
42         $cross_suffix .= $dirnam . "/";
43     }
44     my $basnam = substr($compiler_name, rindex($compiler_name,"/")+1, length($compiler_name)-rindex($compiler_name,"/")-1);
45         if ($basnam =~ /([^\s]*-)(.*)/) {
46         $cross_suffix .= $1;
47     }
48 } else {
49     if (dirname($compiler_name) ne ".") {
50         $cross_suffix .= dirname($compiler_name) . "/";
51     }
52
53     if (basename($compiler_name) =~ /([^\s]*-)(.*)/) {
54         $cross_suffix .= $1;
55     }
56 }
57
58 $compiler = "";
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 "");
69
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/);
84
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/);
99
100 $defined = 0;
101
102 if ($os eq "AIX") {
103     $compiler_name .= " -maix32" if ($binary eq "32");
104     $compiler_name .= " -maix64" if ($binary eq "64");
105     $defined = 1;
106 }
107
108 if ($architecture eq "mips") {
109     $compiler_name .= " -mabi=32";
110     $defined = 1;
111 }
112
113 if ($architecture eq "mips64") {
114     $compiler_name .= " -mabi=n32" if ($binary eq "32");
115     $compiler_name .= " -mabi=64" if ($binary eq "64");
116     $defined = 1;
117 }
118
119 if (($architecture eq "arm") || ($architecture eq "arm64")) {
120     $defined = 1;
121 }
122
123 if ($architecture eq "zarch") {
124     $defined = 1;
125     $binary = 64;
126 }
127
128 if ($architecture eq "e2k") {
129     $defined = 1;
130     $binary = 64;
131 }
132
133 if ($architecture eq "alpha") {
134     $defined = 1;
135     $binary = 64;
136 }
137
138 if ($architecture eq "ia64") {
139     $defined = 1;
140     $binary = 64;
141 }
142
143 if (($architecture eq "x86") && ($os ne Darwin) && ($os ne SunOS)) {
144     $defined = 1;
145     $binary =32;
146 }
147
148 if ($architecture eq "riscv64") {
149     $defined = 1;
150     $binary = 64;
151 }
152
153 if ($architecture eq "loongarch64") {
154     $defined = 1;
155     $binary = 64;
156 }
157
158 if ($compiler eq "PGI") {
159     $compiler_name .= " -tp p7"    if ($binary eq "32");
160     $compiler_name .= " -tp p7-64" if ($binary eq "64");
161     $openmp = "-mp";
162     $defined = 1;
163 }
164
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";
169     $defined = 1;
170 }
171
172 if ($compiler eq "INTEL") {
173     $openmp = "-openmp";
174 }
175
176 if ($compiler eq "PATHSCALE") {
177     $openmp = "-mp";
178 }
179
180 if ($compiler eq "OPEN64") {
181     $openmp = "-mp";
182 }
183
184 if ($compiler eq "CLANG") {
185     $openmp = "-fopenmp";
186 }
187
188 if ($compiler eq "GCC" || $compiler eq "LSB") {
189     $openmp = "-fopenmp";
190 }
191
192 if ($defined == 0) {
193     $compiler_name .= " -m32" if ($binary eq "32");
194     $compiler_name .= " -m64" if ($binary eq "64");
195 }
196
197 # Do again
198
199 $data = `$compiler_name $flags -E ctest.c`;
200
201 if ($?) {
202     printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
203     die 1;
204 }
205
206 $have_msa = 0;
207 if (($architecture eq "mips") || ($architecture eq "mips64")) {
208     eval "use File::Temp qw(tempfile)";
209     if ($@){ 
210         warn "could not load PERL module File::Temp, so could not check MSA capatibility";
211     } else {
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";
217
218         $args = "$msa_flags -o $tmpf.o $tmpf";
219         my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
220         system(@cmd) == 0;
221         if ($? != 0) {
222             $have_msa = 0;
223         } else {
224             $have_msa = 1;
225         }
226         unlink("$tmpf.o");
227     }
228 }
229
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/);
243
244 $binformat    = bin32;
245 $binformat    = bin64  if ($data =~ /BINARY_64/);
246
247 $no_avx512= 0;
248 if (($architecture eq "x86") || ($architecture eq "x86_64")) {
249     eval "use File::Temp qw(tempfile)";
250     if ($@){ 
251         warn "could not load PERL module File::Temp, so could not check compiler compatibility with AVX512";
252         $no_avx512 = 0;
253     } else {
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";
261         }
262         my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
263         system(@cmd) == 0;
264         if ($? != 0) {
265             $no_avx512 = 1;
266         } else {
267             $no_avx512 = 0;
268         }
269         unlink("$tmpf.o");
270     }
271 }
272
273 $no_rv64gv= 0;
274 if (($architecture eq "riscv64")) {
275     eval "use File::Temp qw(tempfile)";
276     if ($@){ 
277         warn "could not load PERL module File::Temp, so could not check compiler compatibility with the RISCV vector extension";
278         $no_rv64gv = 0;
279     } else {
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");
286         system(@cmd) == 0;
287         if ($? != 0) {
288             $no_rv64gv = 1;
289         } else {
290             $no_rv64gv = 0;
291         }
292         unlink("$tmpf.o");
293     }
294 }
295
296 $c11_atomics = 0;
297 if ($data =~ /HAVE_C11/) {
298     eval "use File::Temp qw(tempfile)";
299     if ($@){ 
300        warn "could not load PERL module File::Temp, so could not check compiler compatibility with C11";
301        $c11_atomics = 0;
302     } else {
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");
307        system(@cmd) == 0;
308        if ($? != 0) {
309            $c11_atomics = 0;
310        } else {
311            $c11_atomics = 1;
312        }
313        unlink("$tmpf.o");
314     }
315 }
316
317 if ($compiler eq "GCC" &&( ($architecture eq "x86") || ($architecture eq "x86_64"))) {
318         $no_avx2 = 0;
319         $oldgcc = 0;
320         $data = `$compiler_name -dumpversion`;
321         if ($data <= 4.6) {
322                 $no_avx2 = 1;
323                 $oldgcc = 1;
324         }
325 }
326
327 $data = `$compiler_name $flags -S ctest1.c && grep globl ctest1.s | head -n 1 && rm -f ctest1.s`;
328
329 $data =~ /globl\s([_\.]*)(.*)/;
330
331 $need_fu      = $1;
332
333 $cross = 0;
334
335 if ($architecture ne $hostarch) {
336     $cross = 1;
337     $cross = 0 if (($hostarch eq "x86_64") && ($architecture eq "x86"));
338     $cross = 0 if (($hostarch eq "mips64") && ($architecture eq "mips"));
339 }
340
341 $cross = 1 if ($os ne $hostos);
342 $cross = 0 if (($os eq "Android") && ($hostos eq "Linux") && ($ENV{TERMUX_APP_PID} != ""));
343
344 $openmp = "" if $ENV{USE_OPENMP} != 1;
345
346 $linker_L = "";
347 $linker_l = "";
348 $linker_a = "";
349
350 {
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`;
352
353     $link =~ s/\-Y\sP\,/\-Y/g;
354
355     @flags = split(/[\s\,\n]/, $link);
356     # remove leading and trailing quotes from each flag.
357     @flags = map {s/^['"]|['"]$//g; $_} @flags;
358
359     foreach $flags (@flags) {
360         if (
361             ($flags =~ /^\-L/)
362             && ($flags !~ /^-LIST:/)
363             && ($flags !~ /^-LANG:/)
364             ) {
365             $linker_L .= $flags . " "
366             }
367
368         if ($flags =~ /^\-Y/) {
369             $linker_L .= "-Wl,". $flags . " "
370             }
371
372         if ($flags =~ /^\--exclude-libs/) {
373             $linker_L .= "-Wl,". $flags . " ";
374             $flags="";
375            }
376
377         if (
378             ($flags =~ /^\-l/)
379             && ($flags !~ /gfortranbegin/)
380             && ($flags !~ /frtbegin/)
381             && ($flags !~ /pathfstart/)
382             && ($flags !~ /numa/)
383             && ($flags !~ /crt[0-9]/)
384             && ($flags !~ /gcc/)
385             && ($flags !~ /user32/)
386             && ($flags !~ /kernel32/)
387             && ($flags !~ /advapi32/)
388             && ($flags !~ /shell32/)
389             && ($flags !~ /omp/)
390             && ($flags !~ /[0-9]+/)
391             ) {
392             $linker_l .= $flags . " "
393         }
394
395         $linker_a .= $flags . " " if $flags =~ /\.a$/;
396     }
397
398 }
399
400 open(MAKEFILE, "> $makefile") || die "Can't create $makefile";
401 open(CONFFILE, "> $config"  ) || die "Can't create $config";
402
403 # print $data, "\n";
404
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;
422
423 $os           =~ tr/[a-z]/[A-Z]/;
424 $architecture =~ tr/[a-z]/[A-Z]/;
425 $compiler     =~ tr/[a-z]/[A-Z]/;
426
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;
435
436
437 if ($os eq "LINUX") {
438
439 #    @pthread = split(/\s+/, `nm /lib/libpthread.so* | grep _pthread_create`);
440
441 #    if ($pthread[2] ne "") {
442 #       print CONFFILE "#define PTHREAD_CREATE_FUNC     $pthread[2]\n";
443 #    } else {
444         print CONFFILE "#define PTHREAD_CREATE_FUNC     pthread_create\n";
445 #    }
446 } else {
447     print CONFFILE "#define PTHREAD_CREATE_FUNC pthread_create\n";
448 }
449
450 close(MAKEFILE);
451 close(CONFFILE);