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