1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
|
/* G77 preliminary semantic processing for the compiler driver.
Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
Contributed by Brendan Kehoe (brendan@cygnus.com), with significant
modifications for GNU Fortran by James Craig Burley (burley@gnu.ai.mit.edu).
This file is part of GNU Fortran.
GNU Fortran is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Fortran is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Fortran; see the file COPYING. If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA. */
/* This program is a wrapper to the main `gcc' driver. The generic
goal of this program is to be basically identical to gcc (in that
it faithfully passes all of the original arguments to gcc) but,
unless explicitly overridden by the user in certain ways, ensure
that the needs of the language supported by this wrapper are met.
For GNU Fortran (g77), we do the following to the argument list
before passing it to `gcc':
1. Put `-xf77' or `-xf77-cpp-input' before each list of foo.f or foo.F
source files and put `-xnone' after that list, if necessary.
This shouldn't normally be necessary, but it is done in case
gcc.c normally treats .f/.F files as, say, to be compiled by f2c.
2. Make sure `-lf2c -lm' is at the end of the list.
3. Make sure each time `-lf2c' or `-lm' is seen, it forms
part of the series `-lf2c -lm'.
#1 is not done if `-xfoo' is in effect (where foo is not "none").
#2 and #3 are not done if `-nostdlib' or any option that disables
the linking phase is present, or if `-xfoo' is in effect. Note that
-v by itself disables linking.
This program was originally made out of gcc/cp/g++.c, but the
way it builds the new argument list was rewritten so it is much
easier to maintain, improve the way it decides to add or not add
extra arguments, etc. And several improvements were made in the
handling of arguments, primarily to make it more consistent with
`gcc' itself. */
#ifndef LANGUAGE_F77
#define LANGUAGE_F77 1 /* Assume f77 language wanted. */
#endif
#if LANGUAGE_F77 != 1
#include <stdio.h>
int
main (argc, argv)
int argc;
char **argv;
{
fprintf (stderr, "\
g77: `f77' language not included in list of languages\n\
built with this installation of gcc.\n");
exit (1);
}
#else /* LANGUAGE_F77 == 1 */
#include "config.j"
#include "zzz.h"
#include <sys/types.h>
#include <errno.h>
#ifndef _WIN32
#include <sys/file.h> /* May get R_OK, etc. on some systems. */
#else
#include <process.h>
#endif
#ifdef __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#include <stdio.h>
#ifndef R_OK
#define R_OK 4
#define W_OK 2
#define X_OK 1
#endif
/* Defined to the name of the compiler; if using a cross compiler, the
Makefile should compile this file with the proper name
(e.g., "i386-aout-gcc"). */
#ifndef GCC_NAME
#define GCC_NAME "gcc"
#endif
/* On MSDOS, write temp files in current dir
because there's no place else we can expect to use. */
#ifdef __MSDOS__
#ifndef P_tmpdir
#define P_tmpdir "."
#endif
#ifndef R_OK
#define R_OK 4
#define W_OK 2
#define X_OK 1
#endif
#endif
/* Add prototype support. */
#ifndef PROTO
#if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
#define PROTO(ARGS) ARGS
#else
#define PROTO(ARGS) ()
#endif
#endif
#ifndef VPROTO
#ifdef __STDC__
#define PVPROTO(ARGS) ARGS
#define VPROTO(ARGS) ARGS
#define VA_START(va_list,var) va_start(va_list,var)
#else
#define PVPROTO(ARGS) ()
#define VPROTO(ARGS) (va_alist) va_dcl
#define VA_START(va_list,var) va_start(va_list)
#endif
#endif
/* Define a generic NULL if one hasn't already been defined. */
#ifndef NULL
#define NULL 0
#endif
/* Define O_RDONLY if the system hasn't defined it for us. */
#ifndef O_RDONLY
#define O_RDONLY 0
#endif
#ifndef GENERIC_PTR
#if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
#define GENERIC_PTR void *
#else
#define GENERIC_PTR char *
#endif
#endif
#ifndef NULL_PTR
#define NULL_PTR ((GENERIC_PTR)0)
#endif
#ifdef USG
#define vfork fork
#endif /* USG */
/* On MSDOS, write temp files in current dir
because there's no place else we can expect to use. */
#ifdef __MSDOS__
#ifndef P_tmpdir
#define P_tmpdir "."
#endif
#endif
/* By default there is no special suffix for executables. */
#ifndef EXECUTABLE_SUFFIX
#define EXECUTABLE_SUFFIX ""
#endif
/* By default, colon separates directories in a path. */
#ifndef PATH_SEPARATOR
#define PATH_SEPARATOR ':'
#endif
#ifndef DIR_SEPARATOR
#define DIR_SEPARATOR '/'
#endif
static char dir_separator_str[] = {DIR_SEPARATOR, 0};
extern char *getenv ();
#ifndef errno
extern int errno;
#endif
extern int sys_nerr;
#ifndef HAVE_STRERROR
#if defined(bsd4_4)
extern const char *const sys_errlist[];
#else
extern char *sys_errlist[];
#endif
#else
extern char *strerror();
#endif
/* Name with which this program was invoked. */
static char *programname;
/* argc, argv from main(). */
static int xargc;
static char **xargv;
/* The new argument list will be contained in these, though if identical
to the original list, these will be == xargc, xargv. */
static int newargc;
static char **newargv;
/* Options this driver needs to recognize, not just know how to
skip over. */
typedef enum
{
OPTION_b, /* Aka --prefix. */
OPTION_B, /* Aka --target. */
OPTION_c, /* Aka --compile. */
OPTION_driver, /* Wrapper-specific option. */
OPTION_E, /* Aka --preprocess. */
OPTION_help, /* --help. */
OPTION_i, /* -imacros, -include, -include-*. */
OPTION_M, /* Aka --dependencies. */
OPTION_MM, /* Aka --user-dependencies. */
OPTION_nostdlib, /* Aka --no-standard-libraries, or
-nodefaultlibs. */
OPTION_P, /* Aka --print-*-name. */
OPTION_S, /* Aka --assemble. */
OPTION_v, /* Aka --verbose. */
OPTION_version, /* --version. */
OPTION_V, /* Aka --use-version. */
OPTION_x, /* Aka --language. */
OPTION_ /* Unrecognized or unimportant. */
} Option;
/* THE FOLLOWING COMES STRAIGHT FROM prerelease gcc-2.8.0/gcc.c: */
/* This defines which switch letters take arguments. */
#define DEFAULT_SWITCH_TAKES_ARG(CHAR) \
((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
|| (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
|| (CHAR) == 'I' || (CHAR) == 'm' || (CHAR) == 'x' \
|| (CHAR) == 'L' || (CHAR) == 'A')
#ifndef SWITCH_TAKES_ARG
#define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
#endif
/* This defines which multi-letter switches take arguments. */
#define DEFAULT_WORD_SWITCH_TAKES_ARG(STR) \
(!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext") \
|| !strcmp (STR, "Tbss") || !strcmp (STR, "include") \
|| !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
|| !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
|| !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
|| !strcmp (STR, "isystem"))
#ifndef WORD_SWITCH_TAKES_ARG
#define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
#endif
/* This is the common prefix we use to make temp file names.
It is chosen once for each run of this program.
It is substituted into a spec by %g.
Thus, all temp file names contain this prefix.
In practice, all temp file names start with this prefix.
This prefix comes from the envvar TMPDIR if it is defined;
otherwise, from the P_tmpdir macro if that is defined;
otherwise, in /usr/tmp or /tmp. */
static char *temp_filename;
/* Length of the prefix. */
static int temp_filename_length;
/* END OF STUFF FROM gcc-2.7.0/gcc.c. */
char *
my_strerror(e)
int e;
{
#ifdef HAVE_STRERROR
return strerror(e);
#else
static char buffer[30];
if (!e)
return "";
if (e > 0 && e < sys_nerr)
return sys_errlist[e];
sprintf (buffer, "Unknown error %d", e);
return buffer;
#endif
}
#ifdef HAVE_VPRINTF
/* Output an error message and exit */
static void
fatal VPROTO((char *format, ...))
{
#ifndef __STDC__
char *format;
#endif
va_list ap;
VA_START (ap, format);
#ifndef __STDC__
format = va_arg (ap, char*);
#endif
fprintf (stderr, "%s: ", programname);
vfprintf (stderr, format, ap);
va_end (ap);
fprintf (stderr, "\n");
#if 0
/* XXX Not needed for g77 driver. */
delete_temp_files ();
#endif
exit (1);
}
static void
error VPROTO((char *format, ...))
{
#ifndef __STDC__
char *format;
#endif
va_list ap;
VA_START (ap, format);
#ifndef __STDC__
format = va_arg (ap, char*);
#endif
fprintf (stderr, "%s: ", programname);
vfprintf (stderr, format, ap);
va_end (ap);
fprintf (stderr, "\n");
}
#else /* not HAVE_VPRINTF */
static void
error (msg, arg1, arg2)
char *msg, *arg1, *arg2;
{
fprintf (stderr, "%s: ", programname);
fprintf (stderr, msg, arg1, arg2);
fprintf (stderr, "\n");
}
static void
fatal (msg, arg1, arg2)
char *msg, *arg1, *arg2;
{
error (msg, arg1, arg2);
#if 0
/* XXX Not needed for g77 driver. */
delete_temp_files ();
#endif
exit (1);
}
#endif /* not HAVE_VPRINTF */
/* More 'friendly' abort that prints the line and file.
config.h can #define abort fancy_abort if you like that sort of thing. */
void
fancy_abort ()
{
fatal ("Internal g77 abort.");
}
char *
xmalloc (size)
unsigned size;
{
register char *value = (char *) malloc (size);
if (value == 0)
fatal ("virtual memory exhausted");
return value;
}
static char *
concat (s1, s2)
char *s1, *s2;
{
int len1 = strlen (s1);
int len2 = strlen (s2);
char *result = xmalloc (len1 + len2 + 1);
strcpy (result, s1);
strcpy (result + len1, s2);
*(result + len1 + len2) = 0;
return result;
}
static char *
concat3 (s1, s2, s3)
char *s1, *s2, *s3;
{
return concat (concat (s1, s2), s3);
}
static char *
concat4 (s1, s2, s3, s4)
char *s1, *s2, *s3, *s4;
{
return concat (concat (s1, s2), concat (s3, s4));
}
static char *
concat6 (s1, s2, s3, s4, s5, s6)
char *s1, *s2, *s3, *s4, *s5, *s6;
{
return concat3 (concat (s1, s2), concat (s3, s4), concat (s5, s6));
}
static void
pfatal_with_name (name)
char *name;
{
char *s;
if (errno < sys_nerr)
s = concat3 ("%s: ", sys_errlist[errno], "");
else
s = "cannot open %s";
fatal (s, name);
}
/* Compute a string to use as the base of all temporary file names.
It is substituted for %g. */
static char *
choose_temp_base_try (try, base)
char *try;
char *base;
{
char *rv;
if (base)
rv = base;
else if (try == (char *)0)
rv = 0;
else if (access (try, R_OK | W_OK) != 0)
rv = 0;
else
rv = try;
return rv;
}
static void
choose_temp_base ()
{
char *base = 0;
int len;
base = choose_temp_base_try (getenv ("TMPDIR"), base);
base = choose_temp_base_try (getenv ("TMP"), base);
base = choose_temp_base_try (getenv ("TEMP"), base);
#ifdef P_tmpdir
base = choose_temp_base_try (P_tmpdir, base);
#endif
base = choose_temp_base_try (concat4 (dir_separator_str, "usr",
dir_separator_str, "tmp"),
base);
base = choose_temp_base_try (concat (dir_separator_str, "tmp"), base);
/* If all else fails, use the current directory! */
if (base == (char *)0)
base = concat (".", dir_separator_str);
len = strlen (base);
temp_filename = xmalloc (len + strlen (concat (dir_separator_str,
"gfXXXXXX")) + 1);
strcpy (temp_filename, base);
if (len > 0 && temp_filename[len-1] != '/'
&& temp_filename[len-1] != DIR_SEPARATOR)
temp_filename[len++] = DIR_SEPARATOR;
strcpy (temp_filename + len, "gfXXXXXX");
mktemp (temp_filename);
temp_filename_length = strlen (temp_filename);
if (temp_filename_length == 0)
abort ();
}
#ifdef __MSDOS__
static void
perror_exec (name)
char *name;
{
char *s;
if (errno < sys_nerr)
s = concat3 ("installation problem, cannot exec %s: ",
my_strerror( errno ), "");
else
s = "installation problem, cannot exec %s";
error (s, name);
}
/* This is almost exactly what's in gcc.c:pexecute for MSDOS. */
void
run_dos (program, argv)
char *program;
char *argv[];
{
char *scmd, *rf;
FILE *argfile;
int i;
choose_temp_base (); /* not in gcc.c */
scmd = (char *) malloc (strlen (program) + strlen (temp_filename) + 10);
rf = scmd + strlen (program) + 6;
sprintf (scmd, "%s.exe @%s.gp", program, temp_filename);
argfile = fopen (rf, "w");
if (argfile == 0)
pfatal_with_name (rf);
for (i=1; argv[i]; i++)
{
char *cp;
for (cp = argv[i]; *cp; cp++)
{
if (*cp == '"' || *cp == '\'' || *cp == '\\' || isspace (*cp))
fputc ('\\', argfile);
fputc (*cp, argfile);
}
fputc ('\n', argfile);
}
fclose (argfile);
i = system (scmd);
remove (rf);
if (i == -1)
perror_exec (program);
}
#endif /* __MSDOS__ */
/* This structure describes one mapping. */
struct option_map
{
/* The long option's name. */
char *name;
/* The equivalent short option. */
char *equivalent;
/* Argument info. A string of flag chars; NULL equals no options.
a => argument required.
o => argument optional.
j => join argument to equivalent, making one word.
* => require other text after NAME as an argument. */
char *arg_info;
};
/* This is the table of mappings. Mappings are tried sequentially
for each option encountered; the first one that matches, wins. */
struct option_map option_map[] =
{
{"--all-warnings", "-Wall", 0},
{"--ansi", "-ansi", 0},
{"--assemble", "-S", 0},
{"--assert", "-A", "a"},
{"--comments", "-C", 0},
{"--compile", "-c", 0},
{"--debug", "-g", "oj"},
{"--define-macro", "-D", "a"},
{"--dependencies", "-M", 0},
{"--driver", "", 0}, /* Wrapper-specific. */
{"--dump", "-d", "a"},
{"--dumpbase", "-dumpbase", "a"},
{"--entry", "-e", 0},
{"--extra-warnings", "-W", 0},
{"--for-assembler", "-Wa", "a"},
{"--for-linker", "-Xlinker", "a"},
{"--force-link", "-u", "a"},
{"--imacros", "-imacros", "a"},
{"--include", "-include", "a"},
{"--include-barrier", "-I-", 0},
{"--include-directory", "-I", "a"},
{"--include-directory-after", "-idirafter", "a"},
{"--include-prefix", "-iprefix", "a"},
{"--include-with-prefix", "-iwithprefix", "a"},
{"--include-with-prefix-before", "-iwithprefixbefore", "a"},
{"--include-with-prefix-after", "-iwithprefix", "a"},
{"--language", "-x", "a"},
{"--library-directory", "-L", "a"},
{"--machine", "-m", "aj"},
{"--machine-", "-m", "*j"},
{"--no-line-commands", "-P", 0},
{"--no-precompiled-includes", "-noprecomp", 0},
{"--no-standard-includes", "-nostdinc", 0},
{"--no-standard-libraries", "-nostdlib", 0},
{"--no-warnings", "-w", 0},
{"--optimize", "-O", "oj"},
{"--output", "-o", "a"},
{"--pedantic", "-pedantic", 0},
{"--pedantic-errors", "-pedantic-errors", 0},
{"--pipe", "-pipe", 0},
{"--prefix", "-B", "a"},
{"--preprocess", "-E", 0},
{"--print-file-name", "-print-file-name=", "aj"},
{"--print-libgcc-file-name", "-print-libgcc-file-name", 0},
{"--print-missing-file-dependencies", "-MG", 0},
{"--print-multi-lib", "-print-multi-lib", 0},
{"--print-multi-directory", "-print-multi-directory", 0},
{"--print-prog-name", "-print-prog-name=", "aj"},
{"--profile", "-p", 0},
{"--profile-blocks", "-a", 0},
{"--quiet", "-q", 0},
{"--save-temps", "-save-temps", 0},
{"--shared", "-shared", 0},
{"--silent", "-q", 0},
{"--static", "-static", 0},
{"--symbolic", "-symbolic", 0},
{"--target", "-b", "a"},
{"--trace-includes", "-H", 0},
{"--traditional", "-traditional", 0},
{"--traditional-cpp", "-traditional-cpp", 0},
{"--trigraphs", "-trigraphs", 0},
{"--undefine-macro", "-U", "a"},
{"--use-version", "-V", "a"},
{"--user-dependencies", "-MM", 0},
{"--verbose", "-v", 0},
{"--version", "-dumpversion", 0},
{"--warn-", "-W", "*j"},
{"--write-dependencies", "-MD", 0},
{"--write-user-dependencies", "-MMD", 0},
{"--", "-f", "*j"}
};
/* Compares --options that take one arg. */
static int
opteq (xskip, xarg, opt, name)
int *xskip;
char **xarg;
char *opt;
char *name;
{
int optlen;
int namelen;
int complen;
int i;
int cmp = strcmp (opt, name);
int skip = 1;
char *arg = NULL;
if (cmp == 0)
{
/* Easy, a straight match. */
*xskip = skip;
*xarg = arg;
return cmp;
}
optlen = strlen (opt);
for (i = 0; i < sizeof (option_map) / sizeof (option_map[0]); ++i)
{
char *arginfo;
int j;
arginfo = option_map[i].arg_info;
if (arginfo == NULL)
arginfo = "";
namelen = strlen (option_map[i].name);
complen = optlen > namelen ? namelen : optlen;
if (strncmp (opt, option_map[i].name, complen) == 0)
{
if (optlen < namelen)
{
for (j = i + 1;
j < sizeof (option_map) / sizeof (option_map[0]);
++j)
if ((strlen (option_map[j].name) >= optlen)
&& (strncmp (opt, option_map[j].name, optlen) == 0))
fatal ("Ambiguous abbreviation `%s'", opt);
}
if (optlen > namelen)
{
if (opt[namelen] == '=')
{
skip = 0;
arg = opt + namelen + 1;
}
else if (index (arginfo, '*') != 0)
;
else
continue;
}
else if (index (arginfo, '*') != 0)
fatal ("Incomplete `%s' option", option_map[i].name);
if (strcmp (name, option_map[i].name) != 0)
return 1; /* Not what is being looked for. */
*xskip = skip;
*xarg = arg;
return 0;
}
}
return 1;
}
/* Assumes text[0] == '-'. Returns number of argv items that belong to
(and follow) this one, an option id for options important to the
caller, and a pointer to the first char of the arg, if embedded (else
returns NULL, meaning no arg or it's the next argv). */
static void
lookup_option (xopt, xskip, xarg, text)
Option *xopt;
int *xskip;
char **xarg;
char *text;
{
Option opt = OPTION_;
int skip = -1;
char *arg = NULL;
if ((skip = SWITCH_TAKES_ARG (text[1])) > (text[2] != '\0'))
skip -= (text[2] != '\0'); /* Usually one of "DUoeTuImLA". */
else if (text[1] == 'B')
opt = OPTION_B, skip = (text[2] == '\0'), arg = text + 2;
else if (text[1] == 'b')
opt = OPTION_b, skip = (text[2] == '\0'), arg = text + 2;
else if ((text[1] == 'c') && (text[2] == '\0'))
opt = OPTION_c, skip = 0;
else if ((text[1] == 'E') && (text[2] == '\0'))
opt = OPTION_E, skip = 0;
else if (text[1] == 'i')
opt = OPTION_i, skip = 0;
else if ((text[1] == 'S') && (text[2] == '\0'))
opt = OPTION_S, skip = 0;
else if (text[1] == 'V')
opt = OPTION_V, skip = (text[2] == '\0');
else if ((text[1] == 'v') && (text[2] == '\0'))
opt = OPTION_v, skip = 0;
else if (text[1] == 'x')
opt = OPTION_x, skip = (text[2] == '\0'), arg = text + 2;
else
{
if ((skip = WORD_SWITCH_TAKES_ARG (text + 1)) != 0)
/* Usually one of "Tdata", "Ttext", "Tbss", "include",
"imacros", "aux-info", "idirafter", "iprefix",
"iwithprefix", "iwithprefixbefore", "isystem". */
;
else if (text[1] != '-')
skip = 0;
else if (strcmp (text, "--assemble") == 0)
opt = OPTION_S;
else if (strcmp (text, "--compile") == 0)
opt = OPTION_c;
else if (opteq (&skip, &arg, text, "--driver") == 0)
opt = OPTION_driver;
else if (strcmp (text, "--help") == 0)
opt = OPTION_help;
else if ((opteq (&skip, &arg, text, "--imacros") == 0)
|| (opteq (&skip, &arg, text, "--include") == 0)
|| (opteq (&skip, &arg, text, "--include-directory-after") == 0)
|| (opteq (&skip, &arg, text, "--include-prefix") == 0)
|| (opteq (&skip, &arg, text, "--include-with-prefix") == 0)
|| (opteq (&skip, &arg, text, "--include-with-prefix-before") == 0)
|| (opteq (&skip, &arg, text, "--include-with-prefix-after") == 0))
opt = OPTION_i;
else if (opteq (&skip, &arg, text, "--language") == 0)
opt = OPTION_x;
else if ((strcmp (text, "-M") == 0)
|| (strcmp (text, "--dependencies") == 0))
opt = OPTION_M;
else if ((strcmp (text, "-MM") == 0)
|| (strcmp (text, "--user-dependencies") == 0))
opt = OPTION_MM;
else if (opteq (&skip, &arg, text, "--prefix") == 0)
opt = OPTION_B;
else if (strcmp (text, "--preprocess") == 0)
opt = OPTION_E;
else if ((opteq (&skip, &arg, text, "--print-file-name") == 0)
|| (strcmp (text, "--print-libgcc-file-name") == 0)
|| (strcmp (text, "--print-multi-lib") == 0)
|| (strcmp (text, "--print-multi-directory") == 0)
|| (opteq (&skip, &arg, text, "--print-prog-name") == 0))
opt = OPTION_P;
else if ((strcmp (text, "-nostdlib") == 0)
|| (strcmp (text, "--no-standard-libraries") == 0)
|| (strcmp (text, "-nodefaultlibs") == 0))
opt = OPTION_nostdlib;
else if (opteq (&skip, &arg, text, "--use-version") == 0)
opt = OPTION_V;
else if (strcmp (text, "--verbose") == 0)
opt = OPTION_v;
else if (strcmp (text, "--version") == 0)
opt = OPTION_version;
else if (strcmp (text, "-Xlinker") == 0)
skip = 1;
else if ((opteq (&skip, &arg, text, "--assert") == 0)
|| (opteq (&skip, &arg, text, "--define-macro") == 0)
|| (opteq (&skip, &arg, text, "--dump") == 0)
|| (opteq (&skip, &arg, text, "--dumpbase") == 0)
|| (opteq (&skip, &arg, text, "--for-assembler") == 0)
|| (opteq (&skip, &arg, text, "--for-linker") == 0)
|| (opteq (&skip, &arg, text, "--force-link") == 0)
|| (opteq (&skip, &arg, text, "--library-directory") == 0)
|| (opteq (&skip, &arg, text, "--machine") == 0)
|| (opteq (&skip, &arg, text, "--output") == 0)
|| (opteq (&skip, &arg, text, "--target") == 0)
|| (opteq (&skip, &arg, text, "--undefine-macro") == 0))
;
else
skip = 0;
}
if (xopt != NULL)
*xopt = opt;
if (xskip != NULL)
*xskip = skip;
if (xarg != NULL)
*xarg = arg;
}
static void
append_arg (arg)
char *arg;
{
static int newargsize;
#if 0
fprintf (stderr, "`%s'\n", arg);
#endif
if ((newargv == xargv)
&& (arg == xargv[newargc]))
{
++newargc;
return; /* Nothing new here. */
}
if (newargv == xargv)
{ /* Make new arglist. */
int i;
newargsize = (xargc << 2) + 20;
newargv = (char **) malloc (newargsize * sizeof (char *));
/* Copy what has been done so far. */
for (i = 0; i < newargc; ++i)
newargv[i] = xargv[i];
}
if (newargc == newargsize)
fatal ("overflowed output arg list for `%s'", arg);
newargv[newargc++] = arg;
}
int
main (argc, argv)
int argc;
char **argv;
{
register int i = 0;
register char *p;
int verbose = 0;
Option opt;
int skip;
char *arg;
/* This will be NULL if we encounter a situation where we should not
link in libf2c. */
char *library = "-lf2c";
/* This will become 0 if anything other than -v and kin (like -V)
is seen, meaning the user is trying to accomplish something.
If it remains nonzero, the user wants version info, so add stuff to
the command line to make gcc invoke all the appropriate phases
to get all the version info. */
int add_version_magic = 1;
/* The name of the compiler we will want to run---by default, it
will be the definition of `GCC_NAME', e.g., `gcc'. */
char *gcc = GCC_NAME;
/* 0 => -xnone in effect on input/output
1 => -xfoo in effect on input/output
2 => -xnone in effect on input, -xf77 on output
3 => -xnone in effect on input, -xf77-cpp-input on output. */
int saw_speclang = 0;
/* 0 => initial/reset state
1 => last arg was -l<library>
2 => last two args were -l<library> -lm. */
int saw_library = 0;
/* Initialize for append_arg(). */
xargc = argc;
newargv = xargv = argv;
newargc = 0;
append_arg (argv[0]);
p = argv[0] + strlen (argv[0]);
while (p != argv[0] && p[-1] != '/')
--p;
programname = p;
if (argc == 1)
fatal ("No input files specified.\n");
#ifndef __MSDOS__
/* We do a little magic to find out where the main gcc executable
is. If they ran us as /usr/local/bin/g77, then we will look
for /usr/local/bin/gcc; similarly, if they just ran us as `g77',
we'll just look for `gcc'. */
if (p != argv[0])
{
*--p = '\0';
gcc = (char *) malloc ((strlen (argv[0]) + 1 + strlen (GCC_NAME) + 1)
* sizeof (char));
sprintf (gcc, "%s/%s", argv[0], GCC_NAME);
}
#endif
/* First pass through arglist.
If -nostdlib or a "turn-off-linking" option is anywhere in the
command line, don't do any library-option processing (except
relating to -x). Also, if -v is specified, but no other options
that do anything special (allowing -V version, etc.), remember
to add special stuff to make gcc command actually invoke all
the different phases of the compilation process so all the version
numbers can be seen.
Also, here is where all problems with missing arguments to options
are caught. If this loop is exited normally, it means all options
have the appropriate number of arguments as far as the rest of this
program is concerned. */
for (i = 1; i < argc; ++i)
{
if (argv[i][0] != '-')
{
add_version_magic = 0;
continue;
}
lookup_option (&opt, &skip, NULL, argv[i]);
switch (opt)
{
case OPTION_nostdlib:
case OPTION_c:
case OPTION_S:
case OPTION_E:
case OPTION_M:
case OPTION_MM:
/* These options disable linking entirely or linking of the
standard libraries. */
library = NULL;
add_version_magic = 0;
break;
case OPTION_v:
verbose = 1;
printf ("g77 version %s\n", ffezzz_version_string);
break;
case OPTION_b:
case OPTION_B:
case OPTION_driver:
case OPTION_i:
case OPTION_V:
/* These options are useful in conjunction with -v to get
appropriate version info. */
break;
case OPTION_version:
printf ("\
GNU Fortran %s\n\
Copyright (C) 1996 Free Software Foundation, Inc.\n\
For more version information on components of the GNU Fortran\n\
compilation system, especially useful when reporting bugs,\n\
type the command `g77 --verbose'.\n\
\n\
GNU Fortran comes with NO WARRANTY, to the extent permitted by law.\n\
You may redistribute copies of GNU Fortran\n\
under the terms of the GNU General Public License.\n\
For more information about these matters, see the file named COPYING\n\
or type the command `info -f g77 Copying'.\n\
", ffezzz_version_string);
exit (0);
break;
case OPTION_help:
printf ("\
Usage: g77 [OPTION]... FORTRAN-SOURCE...\n\
\n\
Compile and link Fortran source code to produce an executable program,\n\
which by default is named `a.out', and can be invoked with the UNIX\n\
command `./a.out'.\n\
\n\
Options:\n\
--debug include debugging information in executable.\n\
--driver=COMMAND specify preprocessor/compiler/linker driver\n\
to use instead of the default `gcc'.\n\
--help display this help and exit.\n\
--optimize[=LEVEL] take extra time and memory to make generated\n\
executable run faster. LEVEL is 0 for no\n\
optimization, 1 for normal optimization, and\n\
increases through 3 for more optimization.\n\
--output=PROGRAM name the executable PROGRAM instead of a.out;\n\
invoke with the command `./PROGRAM'.\n\
--version display version information and exit.\n\
\n\
Many other options exist to tailor the compilation process, specify\n\
the dialect of the Fortran source code, specify details of the\n\
code-generation methodology, and so on.\n\
\n\
For more information on g77 and gcc, type the commands `info -f g77'\n\
and `info -f gcc' to read the Info documentation on these commands.\n\
\n\
Report bugs to fortran@gnu.ai.mit.edu.\n");
exit (0);
break;
default:
add_version_magic = 0;
break;
}
/* This is the one place we check for missing arguments in the
program. */
if (i + skip < argc)
i += skip;
else
fatal ("argument to `%s' missing\n", argv[i]);
}
/* If only -v and related options (like -V), don't link the standard
libraries. */
if (add_version_magic)
library = NULL;
/* Second pass through arglist, transforming arguments as appropriate. */
for (i = 1; i < argc; ++i)
{
if (argv[i][0] == '\0')
append_arg (argv[i]); /* Interesting. Just append as is. */
else if ((argv[i][0] == '-') && (argv[i][1] != 'l'))
{
/* Not a filename or library. */
if (saw_library == 1) /* -l<library>. */
append_arg ("-lm");
saw_library = 0;
lookup_option (&opt, &skip, &arg, argv[i]);
if (argv[i][1] == '\0')
append_arg (argv[i]); /* "-" == Standard input. */
else if (opt == OPTION_x)
{
/* Track input language. */
char *lang;
append_arg (argv[i]);
if (arg == NULL)
lang = argv[i+1];
else
lang = arg;
saw_speclang = (strcmp (lang, "none") != 0);
}
else if (opt == OPTION_driver)
{
if (arg == NULL)
gcc = argv[i+1];
else
gcc = arg;
i += skip;
continue; /* Don't append args to new list. */
}
append_arg (argv[i]);
for (; skip != 0; --skip)
append_arg (argv[++i]);
}
else
{ /* A filename/library, not an option. */
int len;
int want_speclang;
/* Here, always append the arg _after_ other stuff, possibly. */
if (saw_speclang == 1)
saw_library = 0; /* -xfoo currently active. */
/* Put -xf77 and -xnone around list of filenames ending in
.F or .f, but don't include other filenames or libraries
in that list. */
else if ((argv[i][0] != '-') /* Not a library. */
&& (len = strlen (argv[i])) > 2
&& ((argv[i][len - 1] == 'F')
|| (argv[i][len - 1] == 'f'))
&& argv[i][len - 2] == '.')
{ /* filename.f or filename.F. */
if (saw_library == 1) /* -l<library>. */
append_arg ("-lm");
saw_library = 0;
want_speclang = (argv[i][len - 1] == 'F') + 2;
if (saw_speclang != want_speclang)
{
if (want_speclang == 2)
append_arg ("-xf77");
else
append_arg ("-xf77-cpp-input");
saw_speclang = want_speclang;
}
}
else
{ /* -lfoo or "alien" filename. */
if (saw_speclang)
append_arg ("-xnone");
saw_speclang = 0;
if (strcmp (argv[i], "-lm") == 0
|| strcmp (argv[i], "-lmath") == 0)
{
if (saw_library == 1)
saw_library = 2; /* -l<library> -lm. */
else if (library)
{
append_arg (library);
saw_library = 2; /* -l<library> -lm. */
}
}
else if ((library != NULL)
&& (strcmp (argv[i], library) == 0))
saw_library = 1; /* -l<library>. */
else
{ /* "Alien" library or filename. */
if (saw_library == 1)
append_arg ("-lm");
saw_library = 0;
}
}
append_arg (argv[i]);
}
}
/* Add -lf2c -lm as necessary. */
if (library)
{ /* Doing a link and no -nostdlib. */
if (saw_speclang)
append_arg ("-xnone");
switch (saw_library)
{
case 0:
append_arg (library);
case 1:
append_arg ("-lm");
default:
break;
}
}
else if (verbose && add_version_magic)
{
choose_temp_base ();
append_arg ("-fnull-version");
append_arg ("-o");
append_arg (temp_filename);
append_arg ("-xf77-cpp-input");
append_arg ("/dev/null");
append_arg ("-xnone");
if (library)
{
append_arg (library);
append_arg ("-lm");
}
}
append_arg (NULL);
--newargc; /* Don't count null arg at end. */
newargv[0] = gcc; /* This is safe even if newargv == xargv. */
if (verbose)
{
#if 0
if (newargv == xargv)
fprintf (stderr, "[Original:]");
#endif
for (i = 0; i < newargc; i++)
fprintf (stderr, " %s", newargv[i]);
fprintf (stderr, "\n");
}
#if !defined(OS2) && !defined (_WIN32)
#ifdef __MSDOS__
run_dos (gcc, newargv);
#else /* !__MSDOS__ */
if (execvp (gcc, newargv) < 0)
pfatal_with_name (gcc);
#endif /* __MSDOS__ */
#else /* OS2 or _WIN32 */
if (spawnvp (1, gcc, newargv) < 0)
pfatal_with_name (gcc);
#endif
return 0;
}
#endif /* LANGUAGE_F77 == 1 */
|