Rewrite glibc version check
[platform/upstream/openblas.git] / f_check
1 #!/usr/bin/perl
2
3 $hostos   = `uname -s | sed -e s/\-.*//`;    chop($hostos);
4
5 #
6 # 1. Not specified
7 #   1.1 Automatically detect, then check compiler
8 #   1.2 If no fortran compiler is detected, gfortran is default with NOFORTRAN definition
9 # 2. Specified
10 #   2.1 If path is correct, check compiler
11 #   2.2 If path is not correct, but still valid compiler name, force setting
12 #     2.2.2 Path is not correct, invalid compiler name, then gfortran is default with NOFORTRAN definition
13 #
14
15 $makefile = shift(@ARGV);
16 $config   = shift(@ARGV);
17
18 $nofortran = 0;
19
20 $compiler = join(" ", @ARGV);
21 $compiler_bin = shift(@ARGV);
22
23 # f77 is too ambiguous
24 $compiler = "" if $compiler eq "f77";
25
26 @path = split(/:/, $ENV{"PATH"});
27
28 if ($compiler eq "") {
29
30     @lists = ("gfortran", "g95", "frt", "fort", "openf90", "openf95",
31               "sunf77", "sunf90", "sunf95",
32               "xlf95", "xlf90", "xlf",
33               "ppuf77", "ppuf95", "ppuf90", "ppuxlf",
34               "pathf90", "pathf95",
35               "pgf95", "pgf90", "pgf77",
36               "flang",
37               "ifort");
38
39 OUTER:
40     foreach $lists (@lists) {
41         foreach $path (@path) {
42             if (-x $path . "/" . $lists) {
43                 $compiler = $lists;
44                 $compiler_bin = $lists;
45                 last OUTER;
46             }
47         }
48     }
49
50 }
51
52 if ($compiler eq "") {
53
54     $nofortran = 1;
55     $compiler = "gfortran";
56     $vendor = GFORTRAN;
57     $bu       = "_";
58
59 } else {
60
61     $data = `which $compiler_bin > /dev/null 2> /dev/null`;
62     $vendor = "";
63
64     if (!$?) {
65
66         $data = `$compiler -O2 -S ftest.f > /dev/null 2>&1 && cat ftest.s && rm -f ftest.s`;
67
68         if ($data =~ /zhoge_/) {
69             $bu       = "_";
70         }
71
72         if ($data =~ /GNU/) {
73
74             $data =~ /(\d)\.(\d).(\d)/;
75             $major = $1;
76             $minor = $2;
77
78             if ($major >= 4) {
79                 $vendor = GFORTRAN;
80                 $openmp = "-fopenmp";
81             } else {
82                 if ($compiler =~ /flang/) {
83                     $vendor = FLANG;
84                     $openmp = "-fopenmp";
85                 } else {
86                     $vendor = G77;
87                     $openmp = "";
88                 }
89             }
90
91         }
92
93         if ($data =~ /g95/) {
94             $vendor = G95;
95             $openmp = "";
96         }
97
98         if ($data =~ /Intel/) {
99             $vendor = INTEL;
100             $openmp = "-fopenmp";
101         }
102
103         if ($data =~ /Sun Fortran/) {
104             $vendor = SUN;
105             $openmp = "-xopenmp=parallel";
106         }
107
108         if ($data =~ /PathScale/) {
109             $vendor = PATHSCALE;
110             $openmp = "-openmp";
111         }
112
113         if ($data =~ /Open64/) {
114             $vendor = OPEN64;
115             $openmp = "-mp";
116         }
117
118         if ($data =~ /PGF/) {
119             $vendor = PGI;
120             $openmp = "-mp";
121         }
122
123         if ($data =~ /IBM XL/) {
124             $vendor = IBM;
125             $openmp = "-openmp";
126         }
127
128         # for embeded underscore name, e.g. zho_ge, it may append 2 underscores.
129         $data = `$compiler -O2 -S ftest3.f > /dev/null 2>&1 && cat ftest3.s && rm -f ftest3.s`;
130         if ($data =~ / zho_ge__/) {
131             $need2bu       = 1;
132         }
133     }
134
135     if ($vendor eq "") {
136
137         if ($compiler =~ /g77/) {
138             $vendor = G77;
139             $bu       = "_";
140             $openmp = "";
141         }
142
143         if ($compiler =~ /g95/) {
144             $vendor = G95;
145             $bu       = "_";
146             $openmp = "";
147         }
148
149         if ($compiler =~ /gfortran/) {
150             $vendor = GFORTRAN;
151             $bu       = "_";
152             $openmp = "-fopenmp";
153         }
154
155         if ($compiler =~ /ifort/) {
156             $vendor = INTEL;
157             $bu       = "_";
158             $openmp = "-fopenmp";
159         }
160
161         if ($compiler =~ /pathf/) {
162             $vendor = PATHSCALE;
163             $bu       = "_";
164             $openmp = "-mp";
165         }
166
167         if ($compiler =~ /pgf/) {
168             $vendor = PGI;
169             $bu       = "_";
170             $openmp = "-mp";
171         }
172
173         if ($compiler =~ /ftn/) {
174             $vendor = PGI;
175             $bu       = "_";
176             $openmp = "-openmp";
177         }
178
179         if ($compiler =~ /frt/) {
180             $vendor = FUJITSU;
181             $bu       = "_";
182             $openmp = "-openmp";
183         }
184
185         if ($compiler =~ /sunf77|sunf90|sunf95/) {
186             $vendor = SUN;
187             $bu       = "_";
188             $openmp = "-xopenmp=parallel";
189         }
190
191         if ($compiler =~ /ppuf/) {
192             $vendor = IBM;
193             $openmp = "-openmp";
194         }
195
196         if ($compiler =~ /xlf/) {
197             $vendor = IBM;
198             $openmp = "-openmp";
199         }
200
201         if ($compiler =~ /open64/) {
202             $vendor = OPEN64;
203             $openmp = "-mp";
204         }
205
206         if ($compiler =~ /flang/) {
207             $vendor = FLANG;
208             $bu     = "_";
209             $openmp = "-fopenmp";
210         }
211
212         if ($vendor eq "") {
213             $nofortran = 1;
214             $compiler = "gfortran";
215             $vendor = GFORTRAN;
216             $bu       = "_";
217             $openmp = "";
218         }
219
220     }
221 }
222
223 $data = `which $compiler_bin > /dev/null 2> /dev/null`;
224
225 if (!$?) {
226
227     $binary = $ENV{"BINARY"};
228
229     $openmp = "" if $ENV{USE_OPENMP} != 1;
230
231     if ($binary == 32) {
232         $link = `$compiler $openmp -m32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
233         if ($?) {
234             $link = `$compiler $openmp -q32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
235         }
236         # for AIX
237         if ($?) {
238             $link = `$compiler $openmp -maix32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
239         }
240        #For gfortran MIPS
241         if ($?) {
242     $mips_data = `$compiler_bin -E -dM - < /dev/null`;
243     if ($mips_data =~ /_MIPS_ISA_MIPS64/) {
244         $link = `$compiler $openmp -mabi=n32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
245     } else {
246         $link = `$compiler $openmp -mabi=32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
247     }
248         }
249         $binary = "" if ($?);
250     }
251
252     if ($binary == 64) {
253         $link = `$compiler $openmp -m64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
254         if ($?) {
255             $link = `$compiler $openmp -q64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
256         }
257         # for AIX
258         if ($?) {
259             $link = `$compiler $openmp -maix64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
260         }
261        #For gfortran MIPS
262         if ($?) {
263             $link = `$compiler $openmp -mabi=64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
264         }
265         $binary = "" if ($?);
266     }
267
268     if ($binary eq "") {
269         $link = `$compiler $openmp -v ftest2.f 2>&1 && rm -f a.out a.exe`;
270     }
271 }
272
273 $linker_L = "";
274 $linker_l = "";
275 $linker_a = "";
276
277 if ($link ne "") {
278
279     $link =~ s/\-Y\sP\,/\-Y/g;
280
281     $link =~ s/\-rpath\s+/\-rpath\@/g;
282
283     $link =~ s/\-rpath-link\s+/\-rpath-link\@/g;
284
285     @flags = split(/[\s\,\n]/, $link);
286     # remove leading and trailing quotes from each flag.
287     @flags = map {s/^['"]|['"]$//g; $_} @flags;
288
289     foreach $flags (@flags) {
290         if (
291             ($flags =~ /^\-L/)
292             && ($flags !~ /^-LIST:/)
293             && ($flags !~ /^-LANG:/)
294             ) {
295             if ($vendor eq "PGI") {
296                 $flags =~ s/lib$/libso/;
297             }
298             $linker_L .= $flags . " ";
299         }
300
301         if ($flags =~ /^\-Y/) {
302             next if ($hostos eq 'SunOS');
303             $linker_L .= "-Wl,". $flags . " ";
304         }
305
306         if ($flags =~ /^\--exclude-libs/) {
307             $linker_L .= "-Wl,". $flags . " ";
308             $flags="";
309         }
310
311
312         if ($flags =~ /^\-rpath\@/) {
313             $flags =~ s/\@/\,/g;
314             if ($vendor eq "PGI") {
315                 $flags =~ s/lib$/libso/;
316             }
317             $linker_L .= "-Wl,". $flags . " " ;
318         }
319
320         if ($flags =~ /^\-rpath-link\@/) {
321             $flags =~ s/\@/\,/g;
322             if ($vendor eq "PGI") {
323                 $flags =~ s/lib$/libso/;
324             }
325             $linker_L .= "-Wl,". $flags . " " ;
326         }
327
328         if (
329             ($flags =~ /^\-l/)
330             && ($flags !~ /gfortranbegin/)
331             && ($flags !~ /frtbegin/)
332             && ($flags !~ /pathfstart/)
333             && ($flags !~ /numa/)
334             && ($flags !~ /crt[0-9]/)
335             && ($flags !~ /gcc/)
336             && ($flags !~ /user32/)
337             && ($flags !~ /kernel32/)
338             && ($flags !~ /advapi32/)
339             && ($flags !~ /shell32/)
340                 && ($flags !~ /^\-l$/)
341             ) {
342             $linker_l .= $flags . " ";
343         }
344
345         $linker_a .= $flags . " " if $flags =~ /\.a$/;
346     }
347
348 }
349
350 if ($vendor eq "INTEL"){
351     $linker_a .= "-lgfortran"
352 }
353
354 if ($vendor eq "FLANG"){
355     $linker_a .= "-lflang"
356 }
357
358 open(MAKEFILE, ">> $makefile") || die "Can't append $makefile";
359 open(CONFFILE, ">> $config"  ) || die "Can't append $config";
360
361 print MAKEFILE "F_COMPILER=$vendor\n";
362 print MAKEFILE "FC=$compiler\n";
363 print MAKEFILE "BU=$bu\n" if $bu ne "";
364 print MAKEFILE "NOFORTRAN=1\n" if $nofortran == 1;
365
366 print CONFFILE "#define BUNDERSCORE\t$bu\n" if $bu ne "";
367 print CONFFILE "#define NEEDBUNDERSCORE\t1\n" if $bu ne "";
368 print CONFFILE "#define NEED2UNDERSCORES\t1\n" if $need2bu ne "";
369
370 print MAKEFILE "NEED2UNDERSCORES=1\n" if $need2bu ne "";
371
372 if (($linker_l ne "") || ($linker_a ne "")) {
373     print MAKEFILE "FEXTRALIB=$linker_L $linker_l $linker_a\n";
374 }
375
376 close(MAKEFILE);
377 close(CONFFILE);