Merge pull request #1286 from martin-frbg/baytrail
[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).(.)/;
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 = "-openmp";
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 = "-openmp";
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 gfortran MIPS
237         if ($?) {
238     $mips_data = `$compiler_bin -E -dM - < /dev/null`;
239     if ($mips_data =~ /_MIPS_ISA_MIPS64/) {
240         $link = `$compiler $openmp -mabi=n32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
241     } else {
242         $link = `$compiler $openmp -mabi=32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
243     }
244         }
245         $binary = "" if ($?);
246     }
247
248     if ($binary == 64) {
249         $link = `$compiler $openmp -m64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
250         if ($?) {
251             $link = `$compiler $openmp -q64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
252         }
253        #For gfortran MIPS
254         if ($?) {
255             $link = `$compiler $openmp -mabi=64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
256         }
257         $binary = "" if ($?);
258     }
259
260     if ($binary eq "") {
261         $link = `$compiler $openmp -v ftest2.f 2>&1 && rm -f a.out a.exe`;
262     }
263 }
264
265 $linker_L = "";
266 $linker_l = "";
267 $linker_a = "";
268
269 if ($link ne "") {
270
271     $link =~ s/\-Y\sP\,/\-Y/g;
272
273     $link =~ s/\-rpath\s+/\-rpath\@/g;
274
275     $link =~ s/\-rpath-link\s+/\-rpath-link\@/g;
276
277     @flags = split(/[\s\,\n]/, $link);
278     # remove leading and trailing quotes from each flag.
279     @flags = map {s/^['"]|['"]$//g; $_} @flags;
280
281     foreach $flags (@flags) {
282         if (
283             ($flags =~ /^\-L/)
284             && ($flags !~ /^-LIST:/)
285             && ($flags !~ /^-LANG:/)
286             ) {
287             if ($vendor eq "PGI") {
288                 $flags =~ s/lib$/libso/;
289             }
290             $linker_L .= $flags . " ";
291         }
292
293         if ($flags =~ /^\-Y/) {
294             next if ($hostos eq 'SunOS');
295             $linker_L .= "-Wl,". $flags . " ";
296         }
297
298         if ($flags =~ /^\--exclude-libs/) {
299             $linker_L .= "-Wl,". $flags . " ";
300             $flags="";
301         }
302
303
304         if ($flags =~ /^\-rpath\@/) {
305             $flags =~ s/\@/\,/g;
306             if ($vendor eq "PGI") {
307                 $flags =~ s/lib$/libso/;
308             }
309             $linker_L .= "-Wl,". $flags . " " ;
310         }
311
312         if ($flags =~ /^\-rpath-link\@/) {
313             $flags =~ s/\@/\,/g;
314             if ($vendor eq "PGI") {
315                 $flags =~ s/lib$/libso/;
316             }
317             $linker_L .= "-Wl,". $flags . " " ;
318         }
319
320         if (
321             ($flags =~ /^\-l/)
322             && ($flags !~ /gfortranbegin/)
323             && ($flags !~ /frtbegin/)
324             && ($flags !~ /pathfstart/)
325             && ($flags !~ /numa/)
326             && ($flags !~ /crt[0-9]/)
327             && ($flags !~ /gcc/)
328             && ($flags !~ /user32/)
329             && ($flags !~ /kernel32/)
330             && ($flags !~ /advapi32/)
331             && ($flags !~ /shell32/)
332                 && ($flags !~ /^\-l$/)
333             ) {
334             $linker_l .= $flags . " ";
335         }
336
337         $linker_a .= $flags . " " if $flags =~ /\.a$/;
338     }
339
340 }
341
342 if ($vendor eq "INTEL"){
343     $linker_a .= "-lgfortran"
344 }
345
346 if ($vendor eq "FLANG"){
347     $linker_a .= "-lflang"
348 }
349
350 open(MAKEFILE, ">> $makefile") || die "Can't append $makefile";
351 open(CONFFILE, ">> $config"  ) || die "Can't append $config";
352
353 print MAKEFILE "F_COMPILER=$vendor\n";
354 print MAKEFILE "FC=$compiler\n";
355 print MAKEFILE "BU=$bu\n" if $bu ne "";
356 print MAKEFILE "NOFORTRAN=1\n" if $nofortran == 1;
357
358 print CONFFILE "#define BUNDERSCORE\t$bu\n" if $bu ne "";
359 print CONFFILE "#define NEEDBUNDERSCORE\t1\n" if $bu ne "";
360 print CONFFILE "#define NEED2UNDERSCORES\t1\n" if $need2bu ne "";
361
362 print MAKEFILE "NEED2UNDERSCORES=1\n" if $need2bu ne "";
363
364 if (($linker_l ne "") || ($linker_a ne "")) {
365     print MAKEFILE "FEXTRALIB=$linker_L $linker_l $linker_a\n";
366 }
367
368 close(MAKEFILE);
369 close(CONFFILE);