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