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