Merge pull request #3633 from martin-frbg/perl_fallback
[platform/upstream/openblas.git] / f_check.pl
1 #!/usr/bin/env 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", "pgfortran", "nvfortran",
36               "flang", "egfortran",
37               "ifort", "nagfor");
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         if ($data eq "") {
68                 $data = `$compiler -O2 -S ftest.f > /dev/null 2>&1 && cat ftest.c && rm -f ftest.c`;
69         }
70         if ($data =~ /zhoge_/) {
71             $bu       = "_";
72         }
73
74         if ($data =~ /Fujitsu/) {
75
76             $vendor = FUJITSU;
77             $openmp = "-Kopenmp";
78
79         } elsif ($data =~ /GNU/ || $data =~ /GCC/ ) {
80
81             $data =~ s/\(+.*?\)+//g;
82             $data =~ /(\d+)\.(\d+).(\d+)/;
83             $major = $1;
84             $minor = $2;
85
86             if ($major >= 4) {
87                 $vendor = GFORTRAN;
88                 $openmp = "-fopenmp";
89             } else {
90                 if ($compiler =~ /flang/) {
91                     $vendor = FLANG;
92                     $openmp = "-fopenmp";
93             } elsif ($compiler =~ /pgf/ || $compiler =~ /nvf/) {
94                     $vendor = PGI;
95                     $openmp = "-mp";
96                 } else {
97                     $vendor = G77;
98                     $openmp = "";
99                 }
100             }
101
102         }
103
104         if ($data =~ /g95/) {
105             $vendor = G95;
106             $openmp = "";
107         }
108
109         if ($data =~ /Intel/) {
110             $vendor = INTEL;
111             $openmp = "-fopenmp";
112         }
113
114         if ($data =~ /Sun Fortran/) {
115             $vendor = SUN;
116             $openmp = "-xopenmp=parallel";
117         }
118
119         if ($data =~ /PathScale/) {
120             $vendor = PATHSCALE;
121             $openmp = "-openmp";
122         }
123
124         if ($data =~ /Open64/) {
125             $vendor = OPEN64;
126             $openmp = "-mp";
127         }
128
129         if ($data =~ /PGF/ || $data =~ /NVF/) {
130             $vendor = PGI;
131             $openmp = "-mp";
132         }
133
134         if ($data =~ /IBM XL/) {
135             $vendor = IBM;
136             $openmp = "-openmp";
137         }
138
139         if ($data =~ /NAG/) {
140             $vendor = NAG;
141             $openmp = "-openmp";
142         }
143
144         # for embedded underscore name, e.g. zho_ge, it may append 2 underscores.
145         $data = `$compiler -O2 -S ftest3.f > /dev/null 2>&1 && cat ftest3.s && rm -f ftest3.s`;
146         if ($data eq "") {
147                 $data = `$compiler -O2 -S ftest3.f > /dev/null 2>&1 && cat ftest3.c && rm -f ftest3.c`;
148         }
149         if ($data =~ / zho_ge__/) {
150             $need2bu       = 1;
151         }
152         if ($vendor =~ /G95/) {
153           if ($ENV{NO_LAPACKE} != 1) {
154                 $need2bu = "";
155           }
156         }
157     }
158
159     if ($vendor eq "") {
160
161         if ($compiler =~ /g77/) {
162             $vendor = G77;
163             $bu       = "_";
164             $openmp = "";
165         }
166
167         if ($compiler =~ /g95/) {
168             $vendor = G95;
169             $bu       = "_";
170             $openmp = "";
171         }
172
173         if ($compiler =~ /gfortran/) {
174             $vendor = GFORTRAN;
175             $bu       = "_";
176             $openmp = "-fopenmp";
177         }
178
179         if ($compiler =~ /ifort/) {
180             $vendor = INTEL;
181             $bu       = "_";
182             $openmp = "-fopenmp";
183         }
184
185         if ($compiler =~ /pathf/) {
186             $vendor = PATHSCALE;
187             $bu       = "_";
188             $openmp = "-mp";
189         }
190
191         if ($compiler =~ /pgf/ || $compiler =~ /nvf/) {
192             $vendor = PGI;
193             $bu       = "_";
194             $openmp = "-mp";
195         }
196
197         if ($compiler =~ /ftn/) {
198             $vendor = PGI;
199             $bu       = "_";
200             $openmp = "-openmp";
201         }
202
203         if ($compiler =~ /frt/) {
204             $vendor = FUJITSU;
205             $bu       = "_";
206             $openmp = "-openmp";
207         }
208
209         if ($compiler =~ /sunf77|sunf90|sunf95/) {
210             $vendor = SUN;
211             $bu       = "_";
212             $openmp = "-xopenmp=parallel";
213         }
214
215         if ($compiler =~ /ppuf/) {
216             $vendor = IBM;
217             $openmp = "-openmp";
218         }
219
220         if ($compiler =~ /xlf/) {
221             $vendor = IBM;
222             $openmp = "-openmp";
223         }
224
225         if ($compiler =~ /open64/) {
226             $vendor = OPEN64;
227             $openmp = "-mp";
228         }
229
230         if ($compiler =~ /flang/) {
231             $vendor = FLANG;
232             $bu     = "_";
233             $openmp = "-fopenmp";
234         }
235
236         if ($compiler =~ /nagfor/) {
237             $vendor = NAG;
238             $bu     = "_";
239             $openmp = "-openmp";
240         }
241
242         if ($vendor eq "") {
243             $nofortran = 1;
244             $compiler = "gfortran";
245             $vendor = GFORTRAN;
246             $bu       = "_";
247             $openmp = "";
248         }
249
250     }
251 }
252
253 $data = `which $compiler_bin > /dev/null 2> /dev/null`;
254
255 if (!$?) {
256
257     $binary = $ENV{"BINARY"};
258
259     $openmp = "" if $ENV{USE_OPENMP} != 1;
260
261     if ($binary == 32) {
262         $link = `$compiler $openmp -m32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
263         if ($?) {
264             $link = `$compiler $openmp -q32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
265         }
266         # for AIX
267         if ($?) {
268             $link = `$compiler $openmp -maix32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
269         }
270        #For gfortran MIPS
271         if ($?) {
272     $mips_data = `$compiler_bin -E -dM - < /dev/null`;
273     if ($mips_data =~ /_MIPS_ISA_MIPS64/) {
274         $link = `$compiler $openmp -mabi=n32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
275     } else {
276         $link = `$compiler $openmp -mabi=32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
277     }
278         }
279         $binary = "" if ($?);
280     }
281
282     if ($binary == 64) {
283         $link = `$compiler $openmp -m64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
284         if ($?) {
285             $link = `$compiler $openmp -q64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
286         }
287         # for AIX
288         if ($?) {
289             $link = `$compiler $openmp -maix64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
290         }
291        #For gfortran MIPS
292         if ($?) {
293             $link = `$compiler $openmp -mabi=64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
294         }
295        #For nagfor
296         if ($?) {
297             $link = `$compiler $openmp -dryrun ftest2.f 2>&1 && rm -f a.out a.exe`;
298         }
299         $binary = "" if ($?);
300     }
301     if ($binary eq "") {
302         $link = `$compiler $openmp -v ftest2.f 2>&1 && rm -f a.out a.exe`;
303     }
304 }
305
306 if ( $vendor eq "NAG") {
307             $link = `$compiler $openmp -dryrun ftest2.f 2>&1 && rm -f a.out a.exe`;
308     }
309 $linker_L = "";
310 $linker_l = "";
311 $linker_a = "";
312
313 if ($link ne "") {
314
315     $link =~ s/\-Y\sP\,/\-Y/g;
316     
317     $link =~ s/\-R\s*/\-rpath\%/g;
318
319     $link =~ s/\-rpath\s+/\-rpath\%/g;
320
321     $link =~ s/\-rpath-link\s+/\-rpath-link\%/g;
322
323     @flags = split(/[\s\,\n]/, $link);
324     # remove leading and trailing quotes from each flag.
325     @flags = map {s/^['"]|['"]$//g; $_} @flags;
326
327     foreach $flags (@flags) {
328         if (
329             ($flags =~ /^\-L/)
330             && ($flags !~ /^-LIST:/)
331             && ($flags !~ /^-LANG:/)
332             ) {
333             $linker_L .= $flags . " ";
334         }
335
336         if ($flags =~ /^\-Y/) {
337             next if ($hostos eq 'SunOS');
338             $linker_L .= "-Wl,". $flags . " ";
339         }
340
341         if ($flags =~ /^\--exclude-libs/) {
342             $linker_L .= "-Wl,". $flags . " ";
343             $flags="";
344         }
345
346
347         if ($flags =~ /^\-rpath\%/) {
348             $flags =~ s/\%/\,/g;
349             $linker_L .= "-Wl,". $flags . " " ;
350         }
351
352         if ($flags =~ /^\-rpath-link\%/) {
353             $flags =~ s/\%/\,/g;
354             $linker_L .= "-Wl,". $flags . " " ;
355         }
356         if ($flags =~ /-lgomp/ && $ENV{"CC"} =~ /clang/) {
357             $flags = "-lomp";
358         }
359
360         if (
361             ($flags =~ /^\-l/)
362             && ($flags !~ /ibrary/)
363             && ($flags !~ /gfortranbegin/)
364             && ($flags !~ /flangmain/)
365             && ($flags !~ /frtbegin/)
366             && ($flags !~ /pathfstart/)
367             && ($flags !~ /crt[0-9]/)
368             && ($flags !~ /gcc/)
369             && ($flags !~ /user32/)
370             && ($flags !~ /kernel32/)
371             && ($flags !~ /advapi32/)
372             && ($flags !~ /shell32/)
373             && ($flags !~ /omp/ || ($vendor !~ /PGI/ && $vendor !~ /FUJITSU/ && $flags =~ /omp/))
374             && ($flags !~ /[0-9]+/ || ($vendor == FUJITSU && $flags =~ /^-lfj90/))
375                 && ($flags !~ /^\-l$/)
376             ) {
377             $linker_l .= $flags . " ";
378         }
379
380         if ( $flags =~ /quickfit.o/ && $vendor == NAG) {
381             $linker_l .= $flags . " ";
382         }
383         if ( $flags =~ /safefit.o/ && $vendor == NAG) {
384             $linker_l .= $flags . " ";
385         }
386         if ( $flags =~ /thsafe.o/ && $vendor == NAG) {
387             $linker_l .= $flags . " ";
388         }
389
390         $linker_a .= $flags . " " if $flags =~ /\.a$/;
391     }
392
393 }
394
395 if ($vendor eq "FLANG"){
396     $linker_a .= "-lflang"
397 }
398
399 open(MAKEFILE, ">> $makefile") || die "Can't append $makefile";
400 open(CONFFILE, ">> $config"  ) || die "Can't append $config";
401
402 print MAKEFILE "F_COMPILER=$vendor\n";
403 print MAKEFILE "FC=$compiler\n";
404 print MAKEFILE "BU=$bu\n" if $bu ne "";
405 print MAKEFILE "NOFORTRAN=1\n" if $nofortran == 1;
406
407 print CONFFILE "#define BUNDERSCORE\t$bu\n" if $bu ne "";
408 print CONFFILE "#define NEEDBUNDERSCORE\t1\n" if $bu ne "";
409 print CONFFILE "#define NEED2UNDERSCORES\t1\n" if $need2bu ne "";
410
411 print MAKEFILE "NEED2UNDERSCORES=1\n" if $need2bu ne "";
412
413 if (($linker_l ne "") || ($linker_a ne "")) {
414     print MAKEFILE "FEXTRALIB=$linker_L $linker_l $linker_a\n";
415 }
416
417 close(MAKEFILE);
418 close(CONFFILE);