Fix typo
[platform/upstream/openblas.git] / c_check
1 #!/usr/bin/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/`;chop($hostarch);
9 $hostarch = "x86_64" if ($hostarch eq "amd64");
10 $hostarch = "arm" if ($hostarch =~ /^arm.*/);
11 $hostarch = "arm64" if ($hostarch eq "aarch64");
12 $hostarch = "power" if ($hostarch =~ /^(powerpc|ppc).*/);
13 $hostarch = "zarch" if ($hostarch eq "s390x");
14
15 #$tmpf = new File::Temp( UNLINK => 1 );
16 $binary = $ENV{"BINARY"};
17
18 $makefile = shift(@ARGV);
19 $config   = shift(@ARGV);
20
21 $compiler_name = join(" ", @ARGV);
22
23 # First, we need to know the target OS and compiler name
24
25 $data = `$compiler_name -E ctest.c`;
26
27 if ($?) {
28     printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
29     die 1;
30 }
31
32 $cross_suffix = "";
33
34 eval "use File::Basename";
35 if ($@){ 
36     warn "could not load PERL module File::Basename, emulating its functionality";
37     my $dirnam = substr($compiler_name, 0, rindex($compiler_name, "/")-1 );
38     if ($dirnam ne ".") {
39         $cross_suffix .= $dirnam . "/";
40     }
41     my $basnam = substr($compiler_name, rindex($compiler_name,"/")+1, length($compiler_name)-rindex($compiler_name,"/")-1);
42         if ($basnam =~ /([^\s]*-)(.*)/) {
43         $cross_suffix .= $1;
44     }
45 } else {
46     if (dirname($compiler_name) ne ".") {
47         $cross_suffix .= dirname($compiler_name) . "/";
48     }
49
50     if (basename($compiler_name) =~ /([^\s]*-)(.*)/) {
51         $cross_suffix .= $1;
52     }
53 }
54
55 $compiler = "";
56 $compiler = LSB       if ($data =~ /COMPILER_LSB/);
57 $compiler = CLANG     if ($data =~ /COMPILER_CLANG/);
58 $compiler = PGI       if ($data =~ /COMPILER_PGI/);
59 $compiler = PATHSCALE if ($data =~ /COMPILER_PATHSCALE/);
60 $compiler = INTEL     if ($data =~ /COMPILER_INTEL/);
61 $compiler = OPEN64    if ($data =~ /COMPILER_OPEN64/);
62 $compiler = SUN       if ($data =~ /COMPILER_SUN/);
63 $compiler = IBM       if ($data =~ /COMPILER_IBM/);
64 $compiler = DEC       if ($data =~ /COMPILER_DEC/);
65 $compiler = GCC       if ($compiler eq "");
66
67 $os = Linux           if ($data =~ /OS_LINUX/);
68 $os = FreeBSD         if ($data =~ /OS_FREEBSD/);
69 $os = NetBSD          if ($data =~ /OS_NETBSD/);
70 $os = OpenBSD         if ($data =~ /OS_OPENBSD/);
71 $os = DragonFly       if ($data =~ /OS_DRAGONFLY/);
72 $os = Darwin          if ($data =~ /OS_DARWIN/);
73 $os = SunOS           if ($data =~ /OS_SUNOS/);
74 $os = AIX             if ($data =~ /OS_AIX/);
75 $os = osf             if ($data =~ /OS_OSF/);
76 $os = WINNT           if ($data =~ /OS_WINNT/);
77 $os = CYGWIN_NT       if ($data =~ /OS_CYGWIN_NT/);
78 $os = Interix         if ($data =~ /OS_INTERIX/);
79 $os = Android         if ($data =~ /OS_ANDROID/);
80 $os = Haiku           if ($data =~ /OS_HAIKU/);
81
82 $architecture = x86    if ($data =~ /ARCH_X86/);
83 $architecture = x86_64 if ($data =~ /ARCH_X86_64/);
84 $architecture = power  if ($data =~ /ARCH_POWER/);
85 $architecture = mips   if ($data =~ /ARCH_MIPS/);
86 $architecture = mips64 if ($data =~ /ARCH_MIPS64/);
87 $architecture = alpha  if ($data =~ /ARCH_ALPHA/);
88 $architecture = sparc  if ($data =~ /ARCH_SPARC/);
89 $architecture = ia64   if ($data =~ /ARCH_IA64/);
90 $architecture = arm    if ($data =~ /ARCH_ARM/);
91 $architecture = arm64  if ($data =~ /ARCH_ARM64/);
92 $architecture = zarch  if ($data =~ /ARCH_ZARCH/);
93
94 $defined = 0;
95
96 if ($os eq "AIX") {
97     $compiler_name .= " -maix32" if ($binary eq "32");
98     $compiler_name .= " -maix64" if ($binary eq "64");
99     $defined = 1;
100 }
101
102 if ($architecture eq "mips") {
103     $compiler_name .= " -mabi=32";
104     $defined = 1;
105 }
106
107 if ($architecture eq "mips64") {
108     $compiler_name .= " -mabi=n32" if ($binary eq "32");
109     $compiler_name .= " -mabi=64" if ($binary eq "64");
110     $defined = 1;
111 }
112
113 if (($architecture eq "arm") || ($architecture eq "arm64")) {
114     $defined = 1;
115 }
116
117 if ($architecture eq "zarch") {
118     $defined = 1;
119     $binary = 64;
120 }
121
122 if ($architecture eq "alpha") {
123     $defined = 1;
124     $binary = 64;
125 }
126
127 if ($architecture eq "ia64") {
128     $defined = 1;
129     $binary = 64;
130 }
131
132 if (($architecture eq "x86") && ($os ne Darwin) && ($os ne SunOS)) {
133     $defined = 1;
134     $binary =32;
135 }
136
137 if ($compiler eq "PGI") {
138     $compiler_name .= " -tp p7"    if ($binary eq "32");
139     $compiler_name .= " -tp p7-64" if ($binary eq "64");
140     $openmp = "-mp";
141     $defined = 1;
142 }
143
144 if ($compiler eq "IBM") {
145     $compiler_name .= " -q32"  if ($binary eq "32");
146     $compiler_name .= " -q64"  if ($binary eq "64");
147     $openmp = "-qsmp=omp";
148     $defined = 1;
149 }
150
151 if ($compiler eq "INTEL") {
152     $openmp = "-openmp";
153 }
154
155 if ($compiler eq "PATHSCALE") {
156     $openmp = "-mp";
157 }
158
159 if ($compiler eq "OPEN64") {
160     $openmp = "-mp";
161 }
162
163 if ($compiler eq "CLANG") {
164     $openmp = "-fopenmp";
165 }
166
167 if ($compiler eq "GCC" || $compiler eq "LSB") {
168     $openmp = "-fopenmp";
169 }
170
171 if ($defined == 0) {
172     $compiler_name .= " -m32" if ($binary eq "32");
173     $compiler_name .= " -m64" if ($binary eq "64");
174 }
175
176 # Do again
177
178 $data = `$compiler_name -E ctest.c`;
179
180 if ($?) {
181     printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
182     die 1;
183 }
184
185 $have_msa = 0;
186 if (($architecture eq "mips") || ($architecture eq "mips64")) {
187     eval "use File::Temp qw(tempfile)";
188     if ($@){ 
189         warn "could not load PERL module File::Temp, so could not check MSA capatibility";
190     } else {
191         $tmpf = new File::Temp( UNLINK => 1 );
192         $code = '"addvi.b $w0, $w1, 1"';
193         $msa_flags = "-mmsa -mfp64 -msched-weight -mload-store-pairs";
194         print $tmpf "#include <msa.h>\n\n";
195         print $tmpf "void main(void){ __asm__ volatile($code); }\n";
196
197         $args = "$msa_flags -o $tmpf.o -x c $tmpf";
198         my @cmd = ("$compiler_name $args");
199         system(@cmd) == 0;
200         if ($? != 0) {
201             $have_msa = 0;
202         } else {
203             $have_msa = 1;
204         }
205         unlink("$tmpf.o");
206     }
207 }
208
209 $architecture = x86    if ($data =~ /ARCH_X86/);
210 $architecture = x86_64 if ($data =~ /ARCH_X86_64/);
211 $architecture = power  if ($data =~ /ARCH_POWER/);
212 $architecture = mips   if ($data =~ /ARCH_MIPS/);
213 $architecture = mips64 if ($data =~ /ARCH_MIPS64/);
214 $architecture = alpha  if ($data =~ /ARCH_ALPHA/);
215 $architecture = sparc  if ($data =~ /ARCH_SPARC/);
216 $architecture = ia64   if ($data =~ /ARCH_IA64/);
217 $architecture = arm    if ($data =~ /ARCH_ARM/);
218 $architecture = arm64  if ($data =~ /ARCH_ARM64/);
219 $architecture = zarch  if ($data =~ /ARCH_ZARCH/);
220
221 $binformat    = bin32;
222 $binformat    = bin64  if ($data =~ /BINARY_64/);
223
224 $no_avx512= 0;
225 if (($architecture eq "x86") || ($architecture eq "x86_64")) {
226     eval "use File::Temp qw(tempfile)";
227     if ($@){ 
228         warn "could not load PERL module File::Temp, so could not check compiler compatibility with AVX512";
229         $no_avx512 = 0;
230     } else {
231 #       $tmpf = new File::Temp( UNLINK => 1 );
232         ($fh,$tmpf) = tempfile( UNLINK => 1 );
233         $code = '"vbroadcastss -4 * 4(%rsi), %zmm2"';
234         print $tmpf "#include <immintrin.h>\n\nint main(void){ __asm__ volatile($code); }\n";
235         $args = " -march=skylake-avx512 -c -o $tmpf.o -x c $tmpf";
236         my @cmd = ("$compiler_name $args >/dev/null 2>/dev/null");
237         system(@cmd) == 0;
238         if ($? != 0) {
239             $no_avx512 = 1;
240         } else {
241             $no_avx512 = 0;
242         }
243         unlink("tmpf.o");
244     }
245 }
246
247 $data = `$compiler_name -S ctest1.c && grep globl ctest1.s | head -n 1 && rm -f ctest1.s`;
248
249 $data =~ /globl\s([_\.]*)(.*)/;
250
251 $need_fu      = $1;
252
253 $cross = 0;
254
255 if ($architecture ne $hostarch) {
256     $cross = 1;
257     $cross = 0 if (($hostarch eq "x86_64") && ($architecture eq "x86"));
258     $cross = 0 if (($hostarch eq "mips64") && ($architecture eq "mips"));
259 }
260
261 $cross = 1 if ($os ne $hostos);
262
263 $openmp = "" if $ENV{USE_OPENMP} != 1;
264
265 $linker_L = "";
266 $linker_l = "";
267 $linker_a = "";
268
269 {
270     $link = `$compiler_name -c ctest2.c -o ctest2.o 2>&1 && $compiler_name $openmp -v ctest2.o -o ctest2 2>&1 && rm -f ctest2.o ctest2 ctest2.exe`;
271
272     $link =~ s/\-Y\sP\,/\-Y/g;
273
274     @flags = split(/[\s\,\n]/, $link);
275     # remove leading and trailing quotes from each flag.
276     @flags = map {s/^['"]|['"]$//g; $_} @flags;
277
278     foreach $flags (@flags) {
279         if (
280             ($flags =~ /^\-L/)
281             && ($flags !~ /^-LIST:/)
282             && ($flags !~ /^-LANG:/)
283             ) {
284             $linker_L .= $flags . " "
285             }
286
287         if ($flags =~ /^\-Y/) {
288             $linker_L .= "-Wl,". $flags . " "
289             }
290
291         if ($flags =~ /^\--exclude-libs/) {
292             $linker_L .= "-Wl,". $flags . " ";
293             $flags="";
294            }
295
296         if (
297             ($flags =~ /^\-l/)
298             && ($flags !~ /gfortranbegin/)
299             && ($flags !~ /frtbegin/)
300             && ($flags !~ /pathfstart/)
301             && ($flags !~ /numa/)
302             && ($flags !~ /crt[0-9]/)
303             && ($flags !~ /gcc/)
304             && ($flags !~ /user32/)
305             && ($flags !~ /kernel32/)
306             && ($flags !~ /advapi32/)
307             && ($flags !~ /shell32/)
308             ) {
309             $linker_l .= $flags . " "
310         }
311
312         $linker_a .= $flags . " " if $flags =~ /\.a$/;
313     }
314
315 }
316
317 open(MAKEFILE, "> $makefile") || die "Can't create $makefile";
318 open(CONFFILE, "> $config"  ) || die "Can't create $config";
319
320 # print $data, "\n";
321
322 print MAKEFILE "OSNAME=$os\n";
323 print MAKEFILE "ARCH=$architecture\n";
324 print MAKEFILE "C_COMPILER=$compiler\n";
325 print MAKEFILE "BINARY32=\n" if $binformat ne bin32;
326 print MAKEFILE "BINARY64=\n" if $binformat ne bin64;
327 print MAKEFILE "BINARY32=1\n" if $binformat eq bin32;
328 print MAKEFILE "BINARY64=1\n" if $binformat eq bin64;
329 print MAKEFILE "FU=$need_fu\n" if $need_fu ne "";
330 print MAKEFILE "CROSS_SUFFIX=$cross_suffix\n" if $cross != 0 && $cross_suffix ne "";
331 print MAKEFILE "CROSS=1\n" if $cross != 0;
332 print MAKEFILE "CEXTRALIB=$linker_L $linker_l $linker_a\n";
333 print MAKEFILE "HAVE_MSA=1\n" if $have_msa eq 1;
334 print MAKEFILE "MSA_FLAGS=$msa_flags\n" if $have_msa eq 1;
335 print MAKEFILE "NO_AVX512=1\n" if $no_avx512 eq 1;
336
337 $os           =~ tr/[a-z]/[A-Z]/;
338 $architecture =~ tr/[a-z]/[A-Z]/;
339 $compiler     =~ tr/[a-z]/[A-Z]/;
340
341 print CONFFILE "#define OS_$os\t1\n";
342 print CONFFILE "#define ARCH_$architecture\t1\n";
343 print CONFFILE "#define C_$compiler\t1\n";
344 print CONFFILE "#define __32BIT__\t1\n"  if $binformat eq bin32;
345 print CONFFILE "#define __64BIT__\t1\n"  if $binformat eq bin64;
346 print CONFFILE "#define FUNDERSCORE\t$need_fu\n" if $need_fu ne "";
347 print CONFFILE "#define HAVE_MSA\t1\n"  if $have_msa eq 1;
348
349 if ($os eq "LINUX") {
350
351 #    @pthread = split(/\s+/, `nm /lib/libpthread.so* | grep _pthread_create`);
352
353 #    if ($pthread[2] ne "") {
354 #       print CONFFILE "#define PTHREAD_CREATE_FUNC     $pthread[2]\n";
355 #    } else {
356         print CONFFILE "#define PTHREAD_CREATE_FUNC     pthread_create\n";
357 #    }
358 } else {
359     print CONFFILE "#define PTHREAD_CREATE_FUNC pthread_create\n";
360 }
361
362 close(MAKEFILE);
363 close(CONFFILE);