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