Debian Codegraph @

wrappers/perl/ExtUtils/F77.pm

raw
1
2 package ExtUtils::F77;
3
4 use Config;
5
6 =head1 NAME
7
8 ExtUtils::F77 - Simple interface to F77 libs
9
10 =head1 DESCRIPTION
11
12 This module tries to figure out how to link C programs with
13 Fortran subroutines on your system. Basically one must add a list
14 of Fortran runtime libraries. The problem is their location
15 and name varies with each OS/compiler combination!
16
17 This module tries to implement a simple
18 'rule-of-thumb' database for various flavours of UNIX systems.
19 A simple self-documenting Perl database of knowledge/code
20 for figuring out how to link for various combinations of OS and
21 compiler is embedded in the modules Perl code. Please help
22 save the world by sending database entries for
23 your system to [email protected]
24
25 The library list which the module returns
26 can be explicitly overridden by setting the environment
27 variable F77LIBS, e.g.
28
29 % setenv F77LIBS "-lfoo -lbar"
30 % perl Makefile.PL
31 ...
32
33 =cut
34
35 $VERSION = "1.11_br";
36
37 # Database starts here. Basically we have a large hash specifying
38 # entries for each os/compiler combination. Entries can be code refs
39 # in which case they are executed and the returned value used. This
40 # allows us to be quite smart.
41
42 # Hash key convention is uppercase first letter of
43 # hash keys. First key is usually the name of the architecture as
44 # returned by Config (modulo ucfirst()).
45
46 %F77config=();
47
48 ### SunOS (use this as a template for new entries) ###
49
50 # Code to figure out and return link-string for this architecture
51 # Returns false if it can't find anything sensible.
52
53 $F77config{Sunos}{F77}{Link} = sub {
54 $dir = find_highest_SC("/usr/lang/SC*");
55 return "" unless $dir; # Failure
56 print "$Pkg: Found Fortran latest version lib dir $dir\n";
57 return "-L$dir -lF77 -lm";
58 };
59
60 # Whether symbols (subroutine names etc.) have trailing underscores
61 # (true/false)
62
63 $F77config{Sunos}{F77}{Trail_} = 1;
64
65 # Name of default compiler - corresponds to one of the above keys
66
67 $F77config{Sunos}{DEFAULT} = 'F77';
68
69 # Program to run to actually compile stuff
70
71 $F77config{Sunos}{F77}{Compiler} = 'f77';
72
73 # Associated compiler flags
74
75 $F77config{Sunos}{F77}{Cflags} = '-O';
76
77 # Associated compiler flags for shared libraries
78 ## BR Jun 30 1999 I have only added entries for the two systems for
79 ## which I actually know the answer -- g77 and irix
80
81 $F77config{Sunos}{F77}{Share} = '';
82
83 ############ Rest of database is here ############
84
85 ### Solaris ###
86
87 $F77config{Solaris}{F77}{Link} = sub {
88 $dir = find_highest_SC("/opt/SUNWspro/SC*/lib");
89 return "" unless $dir; # Failure
90 print "$Pkg: Found Fortran latest version lib dir $dir\n";
91 return "-L$dir -lF77 -lM77 -lsunmath -lm";
92 };
93 $F77config{Solaris}{F77}{Trail_} = 1;
94 $F77config{Solaris}{F77}{Compiler} = 'f77';
95 $F77config{Solaris}{F77}{Cflags} = '-O';
96 $F77config{Solaris}{F77}{Share} = '';
97 $F77config{Solaris}{DEFAULT} = 'F77';
98
99 ### Generic GNU-77 or F2C system ###
100
101 $F77config{Generic}{G77}{Link} = sub {
102 my @libs = ( 'f2c', 'g2c');
103 my ($dir, $lib, $test);
104 foreach $test (@libs) {
105 $dir = `g77 -print-file-name=lib$test.a`;
106 chomp $dir;
107 # Note that -print-file-name returns just the library name
108 # if it cant be found - make sure that we only accept the
109 # directory if it returns a proper path (or matches a /)
110 if (defined $dir && $dir ne "lib$test.a") {
111 $lib = $test; # Found an existing library
112 last;
113 }
114 }
115
116 if( defined $dir && defined $lib) {
117 $dir =~ s,/lib$lib.a$,,;
118 } else {
119 $dir = "/usr/local/lib";
120 $lib = "f2c";
121 }
122 return( "-L$dir -L/usr/lib -l$lib -lm" );
123 };
124 $F77config{Generic}{G77}{Trail_} = 1;
125 $F77config{Generic}{G77}{Compiler} = find_in_path('g77','f77','fort77');
126 $F77config{Generic}{G77}{Cflags} = '-O';
127 $F77config{Generic}{G77}{Share} = '-shared -fpic';
128 $F77config{Generic}{DEFAULT} = 'G77';
129 $F77config{Generic}{F2c} = $F77config{Generic}{G77};
130
131 ### cygwin ###
132 #"-lg2c -lm";
133 # needed this on my cygwin system to get things working properly
134 sub getcyglink {
135 return join ' ', map {my $lp = `g77 -print-file-name=lib$_.a`;
136 $lp =~ s|/[^/]+$||;
137 $lp =~ s|L([a-z,A-Z]):|L//$1|g;
138 "-L$lp -l$_"} qw/g2c m/;
139 }
140
141 $F77config{Cygwin}{G77}{Trail_} = 1;
142 $F77config{Cygwin}{G77}{Compiler} = 'g77';
143 $F77config{Cygwin}{G77}{Cflags} = '-O';
144 $F77config{Cygwin}{G77}{Link} = \&getcyglink;
145 $F77config{Cygwin}{DEFAULT} = 'G77';
146
147 ### Linux ###
148
149 $F77config{Linux}{G77} = $F77config{Generic}{G77};
150 $F77config{Linux}{F2c} = $F77config{Generic}{G77};
151 $F77config{Linux}{DEFAULT} = 'G77';
152
153 ### DEC OSF/1 ###
154
155 $F77config{Dec_osf}{F77}{Link} = "-L/usr/lib -lUfor -lfor -lFutil -lm -lots -lc";
156 $F77config{Dec_osf}{F77}{Trail_} = 1;
157 $F77config{Dec_osf}{F77}{Compiler} = 'f77';
158 $F77config{Dec_osf}{F77}{Cflags} = '-O';
159 $F77config{Dec_osf}{F77}{Share} = '';
160 $F77config{Dec_osf}{DEFAULT} = 'F77';
161
162 ### HP/UX ###
163
164 $F77config{Hpux}{F77}{Link} = "-L/usr/lib -lcl -lm";
165 $F77config{Hpux}{F77}{Trail_} = 0;
166 $F77config{Hpux}{F77}{Compiler} = 'f77';
167 $F77config{Hpux}{F77}{Cflags} = '-O +z';
168 $F77config{Hpux}{F77}{Share} = '';
169 $F77config{Hpux}{DEFAULT} = 'F77';
170
171 ### IRIX ###
172
173 if ($Config{'cc'} =~ /-n32/) # Modified by Allen Smith
174 {
175 if (($Config{'cc'} =~ /-mips4/) && (-r "/usr/lib32/mips4") && (-d _) && (-x _))
176 {
177 $F77config{Irix}{F77}{Cflags} = "-n32 -mips4";
178 if ((-r "/usr/lib32/mips4") && (-d _) && (-x _))
179 {
180 $F77config{Irix}{F77}{Link} = "-L/usr/lib32/mips4 -L/usr/lib32 -lftn -lm";
181 }
182 }
183 elsif (($Config{'cc'} =~ /-mips3/) && (-r "/usr/lib32/mips3") && (-d _) && (-x _))
184 {
185 $F77config{Irix}{F77}{Cflags} = "-n32 -mips3";
186 if ((-r "/usr/lib32/mips3") && (-d _) && (-x _))
187 {
188 $F77config{Irix}{F77}{Link} = "-L/usr/lib32/mips4 -L/usr/lib32 -lftn -lm";
189 }
190 }
191 else
192 {
193 $F77config{Irix}{F77}{Cflags} = "-n32";
194 $F77config{Irix}{F77}{Link} = "-L/usr/lib32 -lftn -lm";
195 }
196 }
197 else
198 {
199 $F77config{Irix}{F77}{Link} = "-L/usr/lib -lF77 -lI77 -lU77 -lisam -lm";
200 }
201 ## probably this depends on the switches above also...
202 $F77config{Irix}{F77}{Share} = '-KPIC -shared';
203 $F77config{Irix}{F77}{Trail_} = 1;
204 $F77config{Irix}{F77}{Compiler} = $Config{cc} =~ /-n32/ ?
205 'f77 -n32' : 'f77 -o32';
206 $F77config{Irix}{DEFAULT} = 'F77';
207
208 ### AIX ###
209
210 $F77config{Aix}{F77}{Link} = "-L/usr/lib -lxlf90 -lxlf -lc -lm";
211 $F77config{Aix}{F77}{Trail_} = 0;
212 $F77config{Aix}{DEFAULT} = 'F77';
213
214 ### FreeBSD ###
215
216 $F77config{Freebsd}{F77}{Trail_} = 1;
217 $F77config{Freebsd}{F77}{Link} = '-L/usr/lib -lf2c -lm';
218 $F77config{Freebsd}{DEFAULT} = 'F77';
219
220 ### VMS ###
221
222 $F77config{VMS}{Fortran}{Trail_} = 0;
223 $F77config{VMS}{Fortran}{Link} = '';
224 $F77config{VMS}{DEFAULT} = 'Fortran';
225 $F77config{VMS}{Fortran}{Compiler} = 'Fortran';
226
227 ############ End of database is here ############
228
229 =head1 SYNOPSIS
230
231 use ExtUtils::F77; # Automatic guess
232 use ExtUtils::F77 qw(sunos); # Specify system
233 use ExtUtils::F77 qw(linux g77); # Specify system and compiler
234 $fortranlibs = ExtUtils::F77->runtime;
235
236 =cut
237
238 # Package variables
239
240 $Runtime = "-LSNAFU -lwontwork";
241 $RuntimeOK = 0;
242 $Trail_ = 1;
243 $Pkg = "";
244 $Compiler = "";
245 $Cflags = "";
246 $Share = "";
247
248 sub get; # See below
249
250 # All the figuring out occurs during import - this is because
251 # a lot of the answers depend on a lot of the guesswork.
252
253 sub import {
254 $Pkg = shift;
255 my $system = ucfirst(shift); # Set package variables
256 my $compiler = ucfirst(shift);
257
258 # Guesses if system/compiler not specified.
259
260 $system = ucfirst $Config{'osname'} unless $system;
261 $system = 'Cygwin' if $system =~ /Cygwin/;
262 $compiler = get $F77config{$system}{DEFAULT} unless $compiler;
263
264 print "$Pkg: Using system=$system compiler=$compiler\n";
265
266 if (defined($ENV{F77LIBS})) {
267 print "Overriding Fortran libs from value of enviroment variable F77LIBS = $ENV{F77LIBS}\n";
268 $Runtime = $ENV{F77LIBS};
269 }
270 else {
271
272 # Try this combination
273
274 if (defined( $F77config{$system} )){
275 my $flibs = get ($F77config{$system}{$compiler}{Link});
276 if ($flibs ne "") {
277 $Runtime = $flibs . gcclibs();
278 $Runtime =~ s|L([a-z,A-Z]):|L//$1|g if $^O =~ /cygwin/i;
279 print "Runtime: $Runtime\n";
280 $ok = 1;
281 $ok = validate_libs($Runtime) if $flibs ne "";
282 }
283 }else {
284 $Runtime = $ok = "";
285 }
286
287 # If it doesn't work try Generic + GNU77
288
289 unless (defined($Runtime) && $ok) {
290 print <<"EOD";
291 $Pkg: Unable to guess and/or validate system/compiler configuration
292 $Pkg: Will try system=Generic Compiler=G77
293 EOD
294 $system = "Generic";
295 $compiler = "G77";
296 my $flibs = get ($F77config{$system}{$compiler}{Link});
297 $Runtime = $flibs. gcclibs();
298 $ok = validate_libs($Runtime) if $flibs ne "";
299 print "$Pkg: Well that didn't appear to validate. Well I will try it anyway.\n"
300 unless $Runtime && $ok;
301 }
302
303 $RuntimeOK = $ok;
304
305 } # Not overriding
306
307 # Now get the misc info for the methods.
308
309 if (defined( $F77config{$system}{$compiler}{Trail_} )){
310 $Trail_ = get $F77config{$system}{$compiler}{Trail_};
311 }
312 else{
313 print << "EOD";
314 $Pkg: There does not appear to be any configuration info about
315 $Pkg: names with trailing underscores for system $system. Will assume
316 $Pkg: F77 names have trailing underscores.
317 EOD
318 $Trail_ = 1;
319 }
320
321 if (defined( $F77config{$system}{$compiler}{Compiler} )) {
322 $Compiler = $F77config{$system}{$compiler}{Compiler};
323 } else {
324 print << "EOD";
325 $Pkg: There does not appear to be any configuration info about
326 $Pkg: the F77 compiler name. Will assume 'f77'.
327 EOD
328 $Compiler = 'f77';
329 }
330 print "$Pkg: Compiler: $Compiler\n";
331
332 if (defined( $F77config{$system}{$compiler}{Cflags} )) {
333 $Cflags = $F77config{$system}{$compiler}{Cflags};
334 } else {
335 print << "EOD";
336 $Pkg: There does not appear to be any configuration info about
337 $Pkg: the options for the F77 compiler. Will assume none
338 $Pkg: necessary.
339 EOD
340 $Cflags = '';
341 }
342
343 print "$Pkg: Cflags: $Cflags\n";
344
345 ## snippet added by BR Jun 30 1999
346 if (defined( $F77config{$system}{$compiler}{Share} )) {
347 $Share = $F77config{$system}{$compiler}{Share};
348 } else {
349 print << "EOD";
350 $Pkg: There does not appear to be any configuration info about
351 $Pkg: shared library options for the F77 compiler. Will assume
352 $Pkg: none necessary.
353 EOD
354 $Share = '';
355 }
356
357 print "$Pkg: Share: $Share\n";
358 ## end of snippet by BR
359
360 } # End of import ()
361
362 =head1 METHODS
363
364 The following methods are provided:
365
366 =over 4
367
368 =item * B<runtime>
369
370 Returns a list of F77 runtime libraries.
371
372 $fortranlibs = ExtUtils::F77->runtime;
373
374 =item * B<runtimeok>
375
376 Returns TRUE only if runtime libraries have been found successfully.
377
378 =item * B<trail_>
379
380 Returns true if F77 names have trailing underscores.
381
382 =item * B<compiler>
383
384 Returns command to execute the compiler (e.g. 'f77').
385
386 =item * B<cflags>
387
388 Returns compiler flags.
389
390 =item * B<share>
391
392 Returns compiler flags for building shared libraries.
393
394 =item * B<testcompiler>
395
396 Test to see if compiler actually works.
397
398 =back
399
400 More methods will probably be added in the future.
401
402 =cut
403
404 sub runtime { return $Runtime; }
405 sub runtimeok { return $RuntimeOK; }
406 sub trail_ { return $Trail_; }
407 sub compiler { return $Compiler; }
408 sub cflags { return $Cflags; }
409 sub share { return $Share; }
410
411 ### Minor internal utility routines ###
412
413 # Get hash entry, evaluating code references
414
415 sub get { ref($_[0]) eq "CODE" ? &{$_[0]} : $_[0] };
416
417 # Test if any files exist matching glob
418
419 sub any_exists {
420 my @glob = glob(shift);
421 return scalar(@glob);
422 }
423
424 # Find highest version number of SCN.N(.N) directories
425 # (Nasty SunOS/Solaris naming scheme for F77 libs]
426
427 sub find_highest_SC {
428 print "$Pkg: Scanning for $_[0]\n";
429 my @glob = glob(shift);
430 my %n=();
431 for (@glob) {
432 #print "Found $_\n";
433 if ( m|/SC(\d)\.(\d)/?.*$| ) {
434 $n{$_} = $1 *100 + $2 * 10;
435 }
436 if ( m|/SC(\d)\.(\d)\.(\d)/?.*$| ) {
437 $n{$_} = $1 *100 + $2 * 10 + $3;
438 }
439 }
440 my @sorted_dirs = sort {$n{$a} <=> $n{$b}} @glob;
441 return pop @sorted_dirs; # Highest N
442 }
443
444 # Validate a string of form "-Ldir -lfoo -lbar"
445
446 sub validate_libs {
447 print "$Pkg: Validating $_[0] ";
448 my @args = split(' ',shift());
449 my $pat;
450 my $ret = 1;
451
452 # Create list of directories to search (with common defaults)
453
454 my @path = ();
455 for (@args, "/usr/lib", "/lib") {
456 push @path, $1 if /^-L(.+)$/ && -d $1;
457 }
458
459 # Search directories
460
461 for (@args) {
462 next if /^-L/;
463 next if $_ eq "-lm"; # Ignore this common guy
464 if (/^-l(.+)$/) {
465 $pat = join(" ", map {$_."/lib".$1.".*"} @path); # Join dirs + file
466 #print "Checking for $pat\n";
467 unless (any_exists($pat)) {
468 print "\n$Pkg: Unable to find library $_" ;
469 $ret = 0;
470 }
471 }
472 }
473 print $ret ? "[ok]\n" : "\n";
474 return $ret;
475 }
476
477
478 sub testcompiler {
479
480 my $file = "/tmp/testf77$$";
481 my $ret;
482 open(OUT,">$file.f");
483 print OUT " print *, 'Hello World'\n";
484 print OUT " end\n";
485 close(OUT);
486 print "Compiling the test Fortran program...\n";
487 system "$Compiler $Cflags $file.f -o ${file}_exe";
488 print "Executing the test program...\n";
489 if (`${file}_exe` ne " Hello World\n") {
490 print "Test of Fortran Compiler FAILED. \n";
491 print "Do not know how to compile Fortran on your system\n";
492 $ret=0;
493 }
494 else{
495 print "Congratulations you seem to have a working f77!\n";
496 $ret=1;
497 }
498 unlink("${file}_exe"); unlink("$file.f"); unlink("$file.o") if -e "$file.o";
499 return $ret;
500 };
501
502 # Return gcc libs (e.g. -L/usr/local/lib/gcc-lib/sparc-sun-sunos4.1.3_U1/2.7.0 -lgcc)
503
504 sub gcclibs {
505 my $isgcc = $Config{'cc'} eq 'gcc';
506 if (!$isgcc && $^O ne 'VMS') {
507 print "Checking for gcc in disguise:\n";
508 $isgcc = 1 if $Config{gccversion};
509 my $string;
510 if ($isgcc) {
511 $string = "Compiler is gcc version $Config{gccversion}";
512 $string .= "\n" unless $string =~ /\n$/;
513 } else {
514 $string = "Not gcc\n";
515 }
516 print $string;
517 }
518 if ($isgcc) {
519 $gccdir = `gcc -print-libgcc-file-name`; chomp $gccdir;
520 $gccdir =~ s/\/libgcc.a//;
521 return " -L$gccdir -lgcc";
522 }else{
523 return "";
524 }
525 }
526
527 # Try and find a program in the users PATH
528
529 sub find_in_path {
530 my @names = @_;
531 my @path = split(":",$ENV{PATH});
532 my ($name,$dir);
533 for $name (@names) {
534 for $dir (@path) {
535 if (-x $dir."/$name") {
536 print "Found compiler $name\n";
537 return $name;
538 }
539 }
540 }
541 return '' if $^O eq 'VMS';
542 die "Unable to find a fortran compiler using names: ".join(" ",@names);
543 }
544
545
546 =head1 AUTHOR
547
548 Karl Glazebrook ([email protected]).
549
550 =cut
551
552
553 1; # Return true