| 1 |
#! /usr/bin/perl -w |
| 2 |
# fort77 (compiler driver) script for f2c |
| 3 |
# For use with gcc under Linux |
| 4 |
# This code is in the public domain; use at your own risk. |
| 5 |
# Parse options |
| 6 |
|
| 7 |
$version = "1.14a"; |
| 8 |
$nnflag = '-Nn802'; |
| 9 |
$tmpdir = $ENV{'TMPDIR'} || '/tmp'; |
| 10 |
$cpp = 0; |
| 11 |
$fast_math = 1; |
| 12 |
$debug = 0; |
| 13 |
$debugcmd = ""; |
| 14 |
push(@includes, "@INC@"); |
| 15 |
# Loop over all options; pull all options from @ARGV and put all |
| 16 |
# arguments into @argv. This is needed because, apparently, UNIX |
| 17 |
# compilers acceppt options anywhere on the command line. |
| 18 |
|
| 19 |
while ($_ = $ARGV[0]) { |
| 20 |
shift; |
| 21 |
|
| 22 |
if (!/^-/) { |
| 23 |
if (/\.P$/) { |
| 24 |
push(@pfiles, $_); |
| 25 |
} |
| 26 |
else { |
| 27 |
push(@argv, $_); |
| 28 |
} |
| 29 |
next; |
| 30 |
} |
| 31 |
# First, the f2c options. |
| 32 |
|
| 33 |
if (/^-[CUuaEhRrz]$/ || /^-I[24]$/ || /^-onetrip$/ || /^-![clPR]$/ || |
| 34 |
/^-ext$/ || /^-!bs$/ || /^-W[1-9][0-9]*$/ || /^-w8$/ || /^-w66$/ || |
| 35 |
/^-r8$/ || /^-N[^n][0-9]+$/) { |
| 36 |
push (@fopts, $_); |
| 37 |
} |
| 38 |
elsif (/^-Nn[0-9]+$/) { |
| 39 |
$nnflag = $_; |
| 40 |
} |
| 41 |
|
| 42 |
# Prototype flags for f2c |
| 43 |
|
| 44 |
elsif (/^-Ps?/) { |
| 45 |
$extract_prototypes ++; |
| 46 |
push (@fopts, $_); |
| 47 |
} |
| 48 |
|
| 49 |
# Does somebody want to run the preprocessor? |
| 50 |
|
| 51 |
elsif (/^-cpp$/) { |
| 52 |
$cpp++; |
| 53 |
} |
| 54 |
|
| 55 |
# These are common to both f2c and gcc |
| 56 |
elsif (/^-w$/) { |
| 57 |
push(@fopts, $_); |
| 58 |
push(@copts, $_); |
| 59 |
} |
| 60 |
|
| 61 |
# This is for the linker, too... |
| 62 |
elsif (/^-g$/) { |
| 63 |
push(@fopts, $_); |
| 64 |
push(@copts, $_); |
| 65 |
push(@lopts, $_); |
| 66 |
$debug ++; |
| 67 |
} |
| 68 |
|
| 69 |
# Special options for the different subprocesses: f for f2c step, |
| 70 |
# p for (separate) preprocessing, c for C compiler, l for linker. |
| 71 |
# a is also passed to the C compiler. |
| 72 |
|
| 73 |
elsif (/^-Wf,/) { |
| 74 |
push(@fopts, &parsewx($_)); |
| 75 |
} |
| 76 |
elsif (/-Wp,/) { |
| 77 |
push(@cppopts, &parsewx($_)); |
| 78 |
} |
| 79 |
elsif (/-W[ca],/) { |
| 80 |
push(@copts, &parsewx($_)); |
| 81 |
} |
| 82 |
elsif (/-Wl,/) { |
| 83 |
push(@lopts,&parsewx($_)); |
| 84 |
} |
| 85 |
|
| 86 |
# gcc only options |
| 87 |
|
| 88 |
# too many -f and -W options to list them all... |
| 89 |
|
| 90 |
# First, let's see wether somebody wants to adhere to the C standard |
| 91 |
# in Fortran. |
| 92 |
|
| 93 |
elsif (/^-fnofast-math$/) { |
| 94 |
$fast_math = 0; |
| 95 |
} |
| 96 |
elsif (/^-m64$/) { |
| 97 |
push(@copts, $_); |
| 98 |
push(@lopts, $_); |
| 99 |
} |
| 100 |
elsif (/^-m32$/) { |
| 101 |
push(@copts, $_); |
| 102 |
push(@lopts, $_); |
| 103 |
} |
| 104 |
|
| 105 |
# The '-f' option to f2c... |
| 106 |
|
| 107 |
elsif (/^-f$/) { |
| 108 |
push(@fopts, $_); |
| 109 |
} |
| 110 |
elsif (/^-[fWUAm]/ || /^-[Ex]$/ || /^-pipe$/ ) { |
| 111 |
push(@copts, $_); |
| 112 |
} |
| 113 |
|
| 114 |
# Includes and outputs... |
| 115 |
|
| 116 |
elsif (/^-I$/) { |
| 117 |
(@ARGV > 0) || die "$0: Missing argument to \"$_\"\n"; |
| 118 |
push(@includes, "-I".shift); |
| 119 |
} |
| 120 |
elsif (/^-I./) { |
| 121 |
push(@includes, $_); |
| 122 |
} |
| 123 |
elsif (/^-o$/) { |
| 124 |
(@ARGV > 0) || die "$0: Missing argument to \"$_\"\n"; |
| 125 |
$output = shift; |
| 126 |
} |
| 127 |
elsif (/^-o(.*)/) { |
| 128 |
$output = $1; |
| 129 |
} |
| 130 |
|
| 131 |
# Optimization |
| 132 |
elsif (/^-O/) { |
| 133 |
push(@copts, $_); |
| 134 |
push(@lopts, $_); |
| 135 |
$optimize ++; |
| 136 |
} |
| 137 |
|
| 138 |
# Options for both C compiler and linker |
| 139 |
|
| 140 |
elsif (/^-[Og]/ || /^-p$/ || /^-pg$/) { |
| 141 |
push(@copts, $_); |
| 142 |
push(@lopts, $_); |
| 143 |
} |
| 144 |
elsif (/^-[bV]$/ ) { |
| 145 |
(@ARGV > 0) || die "$0 : Missing argument to \"$_\"\n"; |
| 146 |
$arg = shift; |
| 147 |
push(@copts, $_, $arg); |
| 148 |
push(@lopts, $_, $arg); |
| 149 |
} |
| 150 |
elsif (/^-[bV]./ ) { |
| 151 |
push(@copts, $_); |
| 152 |
push(@lopts, $_); |
| 153 |
} |
| 154 |
|
| 155 |
# Linker only options |
| 156 |
|
| 157 |
elsif (/^-[lL]$/) { |
| 158 |
push(@lopts, $_); |
| 159 |
(@ARGV > 0) || die "$0: Missing argument to \"$_\"\n"; |
| 160 |
$_ = shift; |
| 161 |
push(@lopts, $_); |
| 162 |
} |
| 163 |
elsif (/^-[lL]./ || /^-nostartfiles$/ || /^-static$/ || /^-shared$/ || |
| 164 |
/^-symbolic$/) { |
| 165 |
push(@lopts, $_); |
| 166 |
} |
| 167 |
elsif (/^-[cS]$/) { |
| 168 |
$compile_only = $_; |
| 169 |
} |
| 170 |
elsif (/^-D/) { |
| 171 |
push(@cppopts, $_); |
| 172 |
} |
| 173 |
# Are we verbose? |
| 174 |
|
| 175 |
elsif (/^-v$/) { |
| 176 |
$verbose ++; |
| 177 |
} |
| 178 |
|
| 179 |
# Does somebody want to keep the C files around? |
| 180 |
|
| 181 |
elsif (/^-k$/) { |
| 182 |
$keep_c ++; |
| 183 |
} |
| 184 |
|
| 185 |
else { |
| 186 |
die "$0: Illegal option: $_\n"; |
| 187 |
} |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
push(@fopts,$nnflag); |
| 192 |
push(@copts,'-ffast-math') if $optimize && $fast_math; |
| 193 |
push(@cppopts,@includes); |
| 194 |
push(@fopts,@includes,"-I."); |
| 195 |
push(@fopts, @pfiles); |
| 196 |
|
| 197 |
if ($verbose) { |
| 198 |
print STDERR "$0: fort77 Version $version\n"; |
| 199 |
if ($verbose > 1) { |
| 200 |
push(@copts,"-v"); |
| 201 |
push(@lopts,"-v"); |
| 202 |
push(@cppopts,"-v"); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
@ARGV = @argv; |
| 208 |
|
| 209 |
if ($compile_only && $output && (@ARGV>1)) { |
| 210 |
warn "$0: Warning: $compile_only and -o with mutiple files, ignoring -o\n"; |
| 211 |
$output = ""; |
| 212 |
} |
| 213 |
|
| 214 |
die "$0: No input files specified\n" unless @ARGV; |
| 215 |
|
| 216 |
while ($_ = $ARGV[0]) { |
| 217 |
shift; |
| 218 |
$ffile = ""; |
| 219 |
$cfile = ""; |
| 220 |
$lfile = ""; |
| 221 |
$basefile = ""; |
| 222 |
|
| 223 |
if (/\.[fF]$/) { |
| 224 |
$ffile = $_; |
| 225 |
$basefile = $ffile; |
| 226 |
} |
| 227 |
elsif (/\.[cCisSm]$/ || /\.cc$/ || /\.cxx$/) { |
| 228 |
$cfile = $_; |
| 229 |
$basefile = $_; |
| 230 |
} |
| 231 |
else { |
| 232 |
push(@lfiles, $_); |
| 233 |
} |
| 234 |
if ($ffile) { |
| 235 |
&check_file_read($ffile); |
| 236 |
if ($keep_c) { |
| 237 |
$cfile = ($ffile =~ /([^\/]*\.).$/)[0] . "c"; |
| 238 |
} |
| 239 |
else { |
| 240 |
$seq ++; |
| 241 |
$cfile = "$tmpdir/fort77-$$-$seq.c"; |
| 242 |
} |
| 243 |
if ($debug) { |
| 244 |
$debugcmd = ' | /usr/bin/perl -p -e \'s/^(#line.*)""/$1"' |
| 245 |
. $ffile . '"/\' ' |
| 246 |
} |
| 247 |
if ($cpp || ($ffile =~ /\.F$/)) { |
| 248 |
# Backslashes at the end of comment lines confuse cpp... |
| 249 |
$pipe = "| /lib/cpp -traditional " . |
| 250 |
join(' ',@cppopts) . " | @f2c@ " . |
| 251 |
join(' ',@fopts) . $debugcmd . " > $cfile"; |
| 252 |
print STDERR "$0: Running \"$pipe\"" if $verbose; |
| 253 |
open(F2C,$pipe); |
| 254 |
|
| 255 |
open (FFILE, "$ffile") || die ("$0: Cannot open $ffile: $_\n"); |
| 256 |
while (<FFILE>) { |
| 257 |
s/([cC*].*)\\$/$1/; |
| 258 |
print F2C $_; |
| 259 |
} |
| 260 |
close(FFILE); |
| 261 |
close(F2C); |
| 262 |
$retcode = $? / 256; |
| 263 |
|
| 264 |
} |
| 265 |
else { |
| 266 |
$retcode = &mysystem("@f2c@ ". |
| 267 |
join (" ",@fopts). " < ". $ffile . |
| 268 |
$debugcmd . " > $cfile")/256; |
| 269 |
} |
| 270 |
if ($retcode && !$keep_c) { |
| 271 |
print STDERR "$0: unlinking $cfile\n" if $verbose; |
| 272 |
unlink $cfile; |
| 273 |
die "$0: aborting compilation\n"; |
| 274 |
} |
| 275 |
|
| 276 |
# Separate the prototypes out from the C files. |
| 277 |
|
| 278 |
if ($extract_prototypes) { |
| 279 |
$pfile = ($basefile =~ /([^\/]*\.).$/)[0] . "P"; |
| 280 |
open(CFILE, "$cfile") || die ("$0: Cannot open $cfile\n"); |
| 281 |
# *wdh* while (($line = <CFILE>) && |
| 282 |
while (defined($line = <CFILE>) && |
| 283 |
($line !~ '#ifdef P_R_O_T_O_T_Y_P_E_S\n')) { |
| 284 |
print $line; |
| 285 |
} |
| 286 |
if ($_) { |
| 287 |
open(PFILE, ">$pfile") || die ("$0: Cannot open $pfile\n"); |
| 288 |
# *wdh* while (($line = <CFILE>) && ($line !~ '#endif')) { |
| 289 |
while (defined($line = <CFILE>) && ($line !~ '#endif')) { |
| 290 |
print PFILE $line; |
| 291 |
} |
| 292 |
close(PFILE); |
| 293 |
} |
| 294 |
close(CFILE); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
# C compilation step. |
| 299 |
|
| 300 |
if ($cfile) { |
| 301 |
# *wdh* @command = ("cc",@cppopts,@copts); |
| 302 |
@command = ("@CC@",@cppopts,@copts); |
| 303 |
if ($compile_only && $output) { |
| 304 |
push(@command,'-o',$output,$compile_only); |
| 305 |
} |
| 306 |
elsif ((!$compile_only) || ($compile_only eq '-c')) { |
| 307 |
$lfile = ($basefile =~ /([^\/]*\.).$/)[0] . "o"; |
| 308 |
push(@command, '-c', '-o', $lfile); |
| 309 |
} |
| 310 |
elsif ($compile_only eq '-S') { |
| 311 |
$sfile = ($basefile =~ /([^\/]*\.).$/)[0] . "s"; |
| 312 |
push(@command, '-S', '-o', $sfile); |
| 313 |
} |
| 314 |
|
| 315 |
push(@command,$cfile); |
| 316 |
$retcode = &mysystem(@command)/256; |
| 317 |
|
| 318 |
if ($retcode) { |
| 319 |
die "$0: aborting compilation\n"; |
| 320 |
} |
| 321 |
if ($ffile && !$keep_c) { |
| 322 |
print STDERR "$0: unlinking $cfile\n" if $verbose; |
| 323 |
unlink $cfile; |
| 324 |
} |
| 325 |
if ($lfile) { |
| 326 |
push (@gener_lfiles, $lfile); push(@lfiles, $lfile); |
| 327 |
$lfile = ""; |
| 328 |
} |
| 329 |
} |
| 330 |
push (@lfiles, $lfile) if $lfile; |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
exit if $compile_only; |
| 335 |
|
| 336 |
push (@output, "-o", $output) if $output; |
| 337 |
|
| 338 |
$retcode = &mysystem("@CC@", @output, @lfiles, @lopts, "@LLIBDIR@","-lf2c", "-lm" ); |
| 339 |
if (@gener_lfiles) { |
| 340 |
print STDERR "$0: unlinking ",join(',',@gener_lfiles),"\n" if $verbose; |
| 341 |
unlink (@gener_lfiles); |
| 342 |
} |
| 343 |
exit $retcode; |
| 344 |
|
| 345 |
# Basically a system call, except that we want to be verbose if |
| 346 |
# necessary. |
| 347 |
|
| 348 |
sub mysystem |
| 349 |
{ |
| 350 |
local (@args) = @_; |
| 351 |
if (@args == 1) { |
| 352 |
print STDERR "$0: Running \"$args[0]\"\n" if $verbose; |
| 353 |
system($args[0]); |
| 354 |
} |
| 355 |
else { |
| 356 |
print STDERR "$0: Running \"",join(' ',@args),"\"\n" if $verbose; |
| 357 |
system(@args); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
sub parsewx |
| 362 |
{ |
| 363 |
local ($str) = @_; |
| 364 |
local(@tmp) = split(/,/,$str); |
| 365 |
shift(@tmp); |
| 366 |
return @tmp; |
| 367 |
} |
| 368 |
|
| 369 |
sub check_file_read |
| 370 |
{ |
| 371 |
local ($name) = @_; |
| 372 |
open (TESTFILE,"$name") || die "Cannot open $name: $!\n"; |
| 373 |
close(TESTFILE); |
| 374 |
} |