Debian Codegraph @

fort77.in

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