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