Debian Codegraph @

attic/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.14";
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 print "Loaded ExtUtils::F77 version $VERSION\n";
47
48 %F77config=();
49
50 ### SunOS (use this as a template for new entries) ###
51
52 # Code to figure out and return link-string for this architecture
53 # Returns false if it can't find anything sensible.
54
55 $F77config{Sunos}{F77}{Link} = sub {
56 $dir = find_highest_SC("/usr/lang/SC*");
57 return "" unless $dir; # Failure
58 print "$Pkg: Found Fortran latest version lib dir $dir\n";
59 return "-L$dir -lF77 -lm";
60 };
61
62 # Whether symbols (subroutine names etc.) have trailing underscores
63 # (true/false)
64
65 $F77config{Sunos}{F77}{Trail_} = 1;
66
67 # Name of default compiler - corresponds to one of the above keys
68
69 $F77config{Sunos}{DEFAULT} = 'F77';
70
71 # Program to run to actually compile stuff
72
73 $F77config{Sunos}{F77}{Compiler} = 'f77';
74
75 # Associated compiler flags
76
77 $F77config{Sunos}{F77}{Cflags} = '-O';
78
79 ############ Rest of database is here ############
80
81 ### Solaris ###
82
83 $F77config{Solaris}{F77}{Link} = sub {
84 my $NSPATH;
85 my $dir;
86
87 #find the SUNWspro entry of nonstandard inst. in LD_LIBRARY_PATH
88 if ( defined $ENV{'LD_LIBRARY_PATH'} &&
89 $ENV{'LD_LIBRARY_PATH'} =~ m{([^:]*SUNWspro[^/]*)} )
90 {
91 $NSPATH = $1;
92 }
93
94 elsif ( defined $ENV{'PATH'} ) {
95
96 foreach ( split (/:/,$ENV{PATH}) ) {
97 if ( m{(.*SUNWspro[^/]*)} ) {
98 $NSPATH = $1;
99 last;
100 }
101 }
102 }
103
104
105 if (defined $NSPATH) {
106
107 print "$Pkg: Found F77 path:--->$NSPATH\n";
108
109 $dir = find_highest_SC("$NSPATH/WS*/lib") ||
110 find_highest_SC("$NSPATH/SC*/lib") ||
111 find_highest_SC("$NSPATH/sunwspro*/SUNWspro/SC*/lib");
112
113 # that failed. try $NSPATH/lib. use glob to
114 # match for libF77.*, as libF77.a isn't there anymore?
115 unless ( $dir )
116 {
117 print "$Pkg: Trying $NSPATH/lib\n";
118 $dir = "$NSPATH/lib" if glob("$NSPATH/lib/libF77*");
119 }
120
121 } else {
122 }
123 return "" unless $dir; # Failure
124 print "$Pkg: Found Fortran latest version lib dir $dir\n";
125 return "-L$dir -lF77 -lM77 -lsunmath -lm";
126 };
127
128
129 $F77config{Solaris}{F77}{Trail_} = 1;
130 $F77config{Solaris}{F77}{Compiler} = 'f77';
131 $F77config{Solaris}{F77}{Cflags} = '-O';
132 $F77config{Solaris}{DEFAULT} = 'F77';
133
134 ### Generic GNU-77 or F2C system ###
135
136 $F77config{Generic}{G77}{Link} = sub {
137 my @libs = ('g2c', 'f2c');
138 my ($dir, $lib, $test);
139 foreach $test (@libs) {
140 $dir = `g77 -print-file-name=lib$test.a`;
141 chomp $dir;
142 # Note that -print-file-name returns just the library name
143 # if it cant be found - make sure that we only accept the
144 # directory if it returns a proper path (or matches a /)
145 if (defined $dir && $dir ne "lib$test.a") {
146 $lib = $test; # Found an existing library
147 last;
148 }
149 }
150
151 if( defined $dir && defined $lib) {
152 $dir =~ s,/lib$lib.a$,,;
153 } else {
154 $dir = "/usr/local/lib";
155 $lib = "f2c";
156 }
157 return( "-L$dir -L/usr/lib -l$lib -lm" );
158 };
159 $F77config{Generic}{G77}{Trail_} = 1;
160 $F77config{Generic}{G77}{Compiler} = find_in_path('g77','f77','fort77');
161 $F77config{Generic}{G77}{Cflags} = '-O';
162 $F77config{Generic}{DEFAULT} = 'G77';
163 $F77config{Generic}{F2c} = $F77config{Generic}{G77};
164
165 ### cygwin ###
166 #"-lg2c -lm";
167 # needed this on my cygwin system to get things working properly
168 sub getcyglink {
169 return join ' ', map {my $lp = `g77 -print-file-name=lib$_.a`;
170 $lp =~ s|/[^/]+$||;
171 $lp =~ s|L([a-z,A-Z]):|L//$1|g;
172 "-L$lp -l$_"} qw/g2c m/;
173 }
174
175 $F77config{Cygwin}{G77}{Trail_} = 1;
176 $F77config{Cygwin}{G77}{Compiler} = 'g77';
177 $F77config{Cygwin}{G77}{Cflags} = '-O';
178 $F77config{Cygwin}{G77}{Link} = \&getcyglink;
179 $F77config{Cygwin}{DEFAULT} = 'G77';
180
181 ### Linux ###
182
183 $F77config{Linux}{G77} = $F77config{Generic}{G77};
184 $F77config{Linux}{F2c} = $F77config{Generic}{G77};
185 $F77config{Linux}{DEFAULT} = 'G77';
186
187 ### Darwin (Mac OSX) suitable for 10.2 ###
188
189 $F77config{Darwin}{Link} = "-flat_namespace -bundle -undefined suppress -L/usr/local/lib -framework Foundation -framework AppKit";
190 $F77config{Darwin}{G77} = $F77config{Generic}{G77};
191 $F77config{Darwin}{F2c} = $F77config{Generic}{G77};
192 $F77config{Darwin}{DEFAULT} = 'G77';
193
194 ### DEC OSF/1 ###
195
196 $F77config{Dec_osf}{F77}{Link} = "-L/usr/lib -lUfor -lfor -lFutil -lm -lots -lc";
197 $F77config{Dec_osf}{F77}{Trail_} = 1;
198 $F77config{Dec_osf}{F77}{Compiler} = 'f77';
199 $F77config{Dec_osf}{F77}{Cflags} = '-O';
200 $F77config{Dec_osf}{DEFAULT} = 'F77';
201
202 ### HP/UX ###
203
204 $F77config{Hpux}{F77}{Link} = "-L/usr/lib -lcl -lm";
205 $F77config{Hpux}{F77}{Trail_} = 0;
206 $F77config{Hpux}{F77}{Compiler} = 'f77';
207 $F77config{Hpux}{F77}{Cflags} = '-O';
208 $F77config{Hpux}{DEFAULT} = 'F77';
209
210 ### IRIX ###
211
212 # From: Ovidiu Toader <[email protected]>
213 # For an SGI running IRIX 6.4 or higher (probably lower than 6.4 also)
214 # there is a new abi, -64, which produces 64 bit executables. This is no
215 # longer an experimental feature and I am using it exclusively without any
216 # problem. The code below is what I use instead of original IRIX section
217 # in the ExtUtils::F77 package. It adds the -64 flag and it is supposed to
218 # provide the same functionality as the old code for a non -64 abi.
219
220 if (ucfirst($Config{'osname'}) eq "Irix")
221 {
222 my ($cflags,$mips,$default_abi,$abi,$mips_dir,$libs);
223 $cflags = $Config{cc};
224 ($mips) = ($cflags =~ /(-mips\d)/g);
225 $mips = "" if ! defined($mips);
226 $mips_dir = $mips;$mips_dir =~ s/-//g;
227 $default_abi = $Config{osvers} >= 6.4 ? "-n32" : "-o32";
228 GET_ABI:
229 {
230 # -32 seems to be synonymous for -o32 (CS)
231 $abi = "-o32",last GET_ABI if $cflags =~ /-o?32/;
232 $abi = "-n32",last GET_ABI if $cflags =~ /-n32/;
233 $abi = "-64",last GET_ABI if $cflags =~ /-64/;
234 $abi = $default_abi;
235 }
236 if ( $abi eq "-64" ){
237 $libs = ( (-r "/usr/lib64/$mips_dir") && (-d _) && (-x _) ) ?
238 "-L/usr/lib64/$mips_dir" : "";
239 $libs .= " -L/usr/lib64 -lftn -lm";
240 }
241 if ( $abi eq "-n32" ){
242 $libs = ( (-r "/usr/lib32/$mips_dir") && (-d _) && (-x _) ) ?
243 "-L/usr/lib32/$mips_dir" : "";
244 $libs .= " -L/usr/lib32 -lftn -lm";
245 }
246 if ( $abi eq "-o32" ){
247 $libs = "-L/usr/lib -lF77 -lI77 -lU77 -lisam -lm";
248 }
249 $F77config{Irix}{F77}{Cflags} = "$abi $mips";
250 $F77config{Irix}{F77}{Link} = "$libs";
251 $F77config{Irix}{F77}{Trail_} = 1;
252 $F77config{Irix}{F77}{Compiler} = "f77 $abi";
253 $F77config{Irix}{DEFAULT} = 'F77';
254 }
255
256 ### AIX ###
257
258 $F77config{Aix}{F77}{Link} = "-L/usr/lib -lxlf90 -lxlf -lc -lm";
259 $F77config{Aix}{F77}{Trail_} = 0;
260 $F77config{Aix}{DEFAULT} = 'F77';
261
262 ### FreeBSD ###
263
264 $F77config{Freebsd}{F77}{Trail_} = 1;
265 $F77config{Freebsd}{F77}{Link} = '-L/usr/lib -lf2c -lm';
266 $F77config{Freebsd}{DEFAULT} = 'F77';
267
268 ### VMS ###
269
270 $F77config{VMS}{Fortran}{Trail_} = 0;
271 $F77config{VMS}{Fortran}{Link} = ' '; # <---need this space!
272 $F77config{VMS}{DEFAULT} = 'Fortran';
273 $F77config{VMS}{Fortran}{Compiler} = 'Fortran';
274
275 ############ End of database is here ############
276
277 =head1 SYNOPSIS
278
279 use ExtUtils::F77; # Automatic guess
280 use ExtUtils::F77 qw(sunos); # Specify system
281 use ExtUtils::F77 qw(linux g77); # Specify system and compiler
282 $fortranlibs = ExtUtils::F77->runtime;
283
284 =cut
285
286 # Package variables
287
288 $Runtime = "-LSNAFU -lwontwork";
289 $RuntimeOK = 0;
290 $Trail_ = 1;
291 $Pkg = "";
292 $Compiler = "";
293 $Cflags = "";
294
295 sub get; # See below
296
297 # All the figuring out occurs during import - this is because
298 # a lot of the answers depend on a lot of the guesswork.
299
300 sub import {
301 $Pkg = shift;
302 my $system = ucfirst(shift); # Set package variables
303 my $compiler = ucfirst(shift);
304 $Runtime = "-LSNAFU -lwontwork";
305
306 # Guesses if system/compiler not specified.
307
308 $system = ucfirst $Config{'osname'} unless $system;
309 $system = 'Cygwin' if $system =~ /Cygwin/;
310 $compiler = get $F77config{$system}{DEFAULT} unless $compiler;
311
312 print "$Pkg: Using system=$system compiler=$compiler\n";
313
314 if (defined($ENV{F77LIBS})) {
315 print "Overriding Fortran libs from value of enviroment variable F77LIBS = $ENV{F77LIBS}\n";
316 $Runtime = $ENV{F77LIBS};
317 }
318 else {
319
320 # Try this combination
321
322 if (defined( $F77config{$system} )){
323 my $flibs = get ($F77config{$system}{$compiler}{Link});
324 if ($flibs ne "") {
325 $Runtime = $flibs;# . gcclibs();
326 # We don't want to append gcc libs if we are using
327 # non gnu compilers. Initially, explicitly check for
328 # use of sun compiler
329 $Runtime .= gcclibs($flibs) unless $flibs =~ /sunmath/;
330 $Runtime =~ s|L([a-z,A-Z]):|L//$1|g if $^O =~ /cygwin/i;
331 $Runtime = ' ' if $^O eq 'VMS'; # <-- need this space!
332 print "Runtime: $Runtime\n";
333 $ok = 1;
334 $ok = validate_libs($Runtime) if $flibs ne "";
335 }
336 }else {
337 $Runtime = $ok = "";
338 }
339
340 # If it doesn't work try Generic + GNU77
341
342 unless (("$Runtime" ne "-LSNAFU -lwontwork") && $ok) {
343 print <<"EOD";
344 $Pkg: Unable to guess and/or validate system/compiler configuration
345 $Pkg: Will try system=Generic Compiler=G77
346 EOD
347 $system = "Generic";
348 $compiler = "G77";
349 my $flibs = get ($F77config{$system}{$compiler}{Link});
350 $Runtime = $flibs . gcclibs($flibs);
351 $ok = validate_libs($Runtime) if $flibs ne "";
352 print "$Pkg: Well that didn't appear to validate. Well I will try it anyway.\n"
353 unless $Runtime && $ok;
354 }
355
356 $RuntimeOK = $ok;
357
358 } # Not overriding
359
360 # Now get the misc info for the methods.
361
362 if (defined( $F77config{$system}{$compiler}{Trail_} )){
363 $Trail_ = get $F77config{$system}{$compiler}{Trail_};
364 }
365 else{
366 print << "EOD";
367 $Pkg: There does not appear to be any configuration info about
368 $Pkg: names with trailing underscores for system $system. Will assume
369 $Pkg: F77 names have trailing underscores.
370 EOD
371 $Trail_ = 1;
372 }
373
374 if (defined( $F77config{$system}{$compiler}{Compiler} )) {
375 $Compiler = $F77config{$system}{$compiler}{Compiler};
376 } else {
377 print << "EOD";
378 $Pkg: There does not appear to be any configuration info about
379 $Pkg: the F77 compiler name. Will assume 'f77'.
380 EOD
381 $Compiler = 'f77';
382 }
383 print "$Pkg: Compiler: $Compiler\n";
384
385 if (defined( $F77config{$system}{$compiler}{Cflags} )) {
386 $Cflags = $F77config{$system}{$compiler}{Cflags};
387 } else {
388 print << "EOD";
389 $Pkg: There does not appear to be any configuration info about
390 $Pkg: the options for the F77 compiler. Will assume none
391 $Pkg: necessary.
392 EOD
393 $Cflags = '';
394 }
395
396 print "$Pkg: Cflags: $Cflags\n";
397
398 } # End of import ()
399
400 =head1 METHODS
401
402 The following methods are provided:
403
404 =over 4
405
406 =item * B<runtime>
407
408 Returns a list of F77 runtime libraries.
409
410 $fortranlibs = ExtUtils::F77->runtime;
411
412 =item * B<runtimeok>
413
414 Returns TRUE only if runtime libraries have been found successfully.
415
416 =item * B<trail_>
417
418 Returns true if F77 names have trailing underscores.
419
420 =item * B<compiler>
421
422 Returns command to execute the compiler (e.g. 'f77').
423
424 =item * B<cflags>
425
426 Returns compiler flags.
427
428 =item * B<testcompiler>
429
430 Test to see if compiler actually works.
431
432 =back
433
434 More methods will probably be added in the future.
435
436 =cut
437
438 sub runtime { return $Runtime; }
439 sub runtimeok { return $RuntimeOK; }
440 sub trail_ { return $Trail_; }
441 sub compiler { return $Compiler; }
442 sub cflags { return $Cflags; }
443
444 ### Minor internal utility routines ###
445
446 # Get hash entry, evaluating code references
447
448 sub get { ref($_[0]) eq "CODE" ? &{$_[0]} : $_[0] };
449
450 # Test if any files exist matching glob
451
452 sub any_exists {
453 my @glob = glob(shift);
454 return scalar(@glob);
455 }
456
457 # Find highest version number of SCN.N(.N) directories
458 # (Nasty SunOS/Solaris naming scheme for F77 libs]
459 sub find_highest_SC {
460 print "$Pkg: Scanning for $_[0]\n";
461 my @glob = glob(shift);
462 my %n=();
463 for (@glob) {
464 #print "Found $_\n";
465 if ( m|/SC(\d)\.(\d)/?.*$| ) {
466 $n{$_} = $1 *100 + $2 * 10;
467 }
468 if ( m|/SC(\d)\.(\d)\.(\d)/?.*$| ) {
469 $n{$_} = $1 *100 + $2 * 10 + $3;
470 }
471 }
472 my @sorted_dirs = grep {-f "$_/libF77.a"} sort {$n{$a} <=> $n{$b}} @glob;
473
474 return pop @sorted_dirs; # Highest N
475 }
476
477 # Validate a string of form "-Ldir -lfoo -lbar"
478
479 sub validate_libs {
480 print "$Pkg: Validating $_[0] ";
481 my @args = split(' ',shift());
482 my $pat;
483 my $ret = 1;
484
485 # Create list of directories to search (with common defaults)
486
487 my @path = ();
488 for (@args, "/usr/lib", "/lib") {
489 push @path, $1 if /^-L(.+)$/ && -d $1;
490 }
491
492 # Search directories
493
494 for (@args) {
495 next if /^-L/;
496 next if $_ eq "-lm"; # Ignore this common guy
497 if (/^-l(.+)$/) {
498 $pat = join(" ", map {$_."/lib".$1.".*"} @path); # Join dirs + file
499 #print "Checking for $pat\n";
500 unless (any_exists($pat)) {
501 print "\n$Pkg: Unable to find library $_" ;
502 $ret = 0;
503 }
504 }
505 }
506 print $ret ? "[ok]\n" : "\n";
507 return $ret;
508 }
509
510
511 sub testcompiler {
512
513 my $file = "/tmp/testf77$$";
514 my $ret;
515 open(OUT,">$file.f");
516 print OUT " print *, 'Hello World'\n";
517 print OUT " end\n";
518 close(OUT);
519 print "Compiling the test Fortran program...\n";
520 system "$Compiler $Cflags $file.f -o ${file}_exe";
521 print "Executing the test program...\n";
522 if (`${file}_exe` ne " Hello World\n") {
523 print "Test of Fortran Compiler FAILED. \n";
524 print "Do not know how to compile Fortran on your system\n";
525 $ret=0;
526 }
527 else{
528 print "Congratulations you seem to have a working f77!\n";
529 $ret=1;
530 }
531 unlink("${file}_exe"); unlink("$file.f"); unlink("$file.o") if -e "$file.o";
532 return $ret;
533 };
534
535 # Return gcc libs (e.g. -L/usr/local/lib/gcc-lib/sparc-sun-sunos4.1.3_U1/2.7.0 -lgcc)
536
537 sub gcclibs {
538 my $flibs = shift; # Fortran libs
539 my $isgcc = $Config{'cc'} eq 'gcc';
540 if (!$isgcc && $^O ne 'VMS') {
541 print "Checking for gcc in disguise:\n";
542 $isgcc = 1 if $Config{gccversion};
543 my $string;
544 if ($isgcc) {
545 $string = "Compiler is gcc version $Config{gccversion}";
546 $string .= "\n" unless $string =~ /\n$/;
547 } else {
548 $string = "Not gcc\n";
549 }
550 print $string;
551 }
552 if ($isgcc or ($flibs =~ /-lg2c/) or ($flibs =~ /-lf2c/) ) {
553 $gccdir = `gcc -print-libgcc-file-name`; chomp $gccdir;
554 $gccdir =~ s/\/libgcc.a//;
555 return " -L$gccdir -lgcc";
556 }else{
557 return "";
558 }
559 }
560
561 # Try and find a program in the users PATH
562
563 sub find_in_path {
564 my @names = @_;
565 my @path = split(":",$ENV{PATH});
566 my ($name,$dir);
567 for $name (@names) {
568 for $dir (@path) {
569 if (-x $dir."/$name") {
570 print "Found compiler $name\n";
571 return $name;
572 }
573 }
574 }
575 return '' if $^O eq 'VMS';
576 die "Unable to find a fortran compiler using names: ".join(" ",@names);
577 }
578
579
580 =head1 AUTHOR
581
582 Karl Glazebrook ([email protected]).
583
584 =cut
585
586
587 1; # Return true