oops - omitted from previous delta fixing UNIQUE_SECTION
[official-gcc.git] / gcc / f / g77spec.c
blob505bc12b0bbb32a0a4249f0dcf18026fb97fd490
1 /* Specific flags and argument handling of the Fortran front-end.
2 Copyright (C) 1997, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* This file contains a filter for the main `gcc' driver, which is
22 replicated for the `g77' driver by adding this filter. The purpose
23 of this filter is to be basically identical to gcc (in that
24 it faithfully passes all of the original arguments to gcc) but,
25 unless explicitly overridden by the user in certain ways, ensure
26 that the needs of the language supported by this wrapper are met.
28 For GNU Fortran (g77), we do the following to the argument list
29 before passing it to `gcc':
31 1. Make sure `-lg2c -lm' is at the end of the list.
33 2. Make sure each time `-lg2c' or `-lm' is seen, it forms
34 part of the series `-lg2c -lm'.
36 #1 and #2 are not done if `-nostdlib' or any option that disables
37 the linking phase is present, or if `-xfoo' is in effect. Note that
38 a lack of source files or -l options disables linking.
40 This program was originally made out of gcc/cp/g++spec.c, but the
41 way it builds the new argument list was rewritten so it is much
42 easier to maintain, improve the way it decides to add or not add
43 extra arguments, etc. And several improvements were made in the
44 handling of arguments, primarily to make it more consistent with
45 `gcc' itself. */
47 #include "config.h"
48 #include "system.h"
49 #include "gcc.h"
50 #include <f/version.h>
52 #ifndef MATH_LIBRARY
53 #define MATH_LIBRARY "-lm"
54 #endif
56 #ifndef FORTRAN_LIBRARY
57 #define FORTRAN_LIBRARY "-lg2c"
58 #endif
60 /* Options this driver needs to recognize, not just know how to
61 skip over. */
62 typedef enum
64 OPTION_b, /* Aka --prefix. */
65 OPTION_B, /* Aka --target. */
66 OPTION_c, /* Aka --compile. */
67 OPTION_driver, /* Wrapper-specific option. */
68 OPTION_E, /* Aka --preprocess. */
69 OPTION_help, /* --help. */
70 OPTION_i, /* -imacros, -include, -include-*. */
71 OPTION_l,
72 OPTION_L, /* Aka --library-directory. */
73 OPTION_M, /* Aka --dependencies. */
74 OPTION_MM, /* Aka --user-dependencies. */
75 OPTION_nostdlib, /* Aka --no-standard-libraries, or
76 -nodefaultlibs. */
77 OPTION_o, /* Aka --output. */
78 OPTION_S, /* Aka --assemble. */
79 OPTION_syntax_only, /* -fsyntax-only. */
80 OPTION_v, /* Aka --verbose. */
81 OPTION_version, /* --version. */
82 OPTION_V, /* Aka --use-version. */
83 OPTION_x, /* Aka --language. */
84 OPTION_ /* Unrecognized or unimportant. */
85 } Option;
87 /* The original argument list and related info is copied here. */
88 static int g77_xargc;
89 static const char **g77_xargv;
90 static void lookup_option PARAMS ((Option *, int *, const char **,
91 const char *));
92 static void append_arg PARAMS ((const char *));
94 /* The new argument list will be built here. */
95 static int g77_newargc;
96 static char **real_g77_newargv;
97 static const char **g77_newargv;
99 extern char *version_string;
101 /* --- This comes from gcc.c (2.8.1) verbatim: */
103 /* This defines which switch letters take arguments. */
105 #define DEFAULT_SWITCH_TAKES_ARG(CHAR) \
106 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
107 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
108 || (CHAR) == 'I' || (CHAR) == 'm' || (CHAR) == 'x' \
109 || (CHAR) == 'L' || (CHAR) == 'A')
111 #ifndef SWITCH_TAKES_ARG
112 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
113 #endif
115 /* This defines which multi-letter switches take arguments. */
117 #define DEFAULT_WORD_SWITCH_TAKES_ARG(STR) \
118 (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext") \
119 || !strcmp (STR, "Tbss") || !strcmp (STR, "include") \
120 || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
121 || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
122 || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
123 || !strcmp (STR, "isystem") || !strcmp (STR, "specs"))
125 #ifndef WORD_SWITCH_TAKES_ARG
126 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
127 #endif
129 /* --- End of verbatim. */
131 /* Assumes text[0] == '-'. Returns number of argv items that belong to
132 (and follow) this one, an option id for options important to the
133 caller, and a pointer to the first char of the arg, if embedded (else
134 returns NULL, meaning no arg or it's the next argv).
136 Note that this also assumes gcc.c's pass converting long options
137 to short ones, where available, has already been run. */
139 static void
140 lookup_option (xopt, xskip, xarg, text)
141 Option *xopt;
142 int *xskip;
143 const char **xarg;
144 const char *text;
146 Option opt = OPTION_;
147 int skip;
148 const char *arg = NULL;
150 if ((skip = SWITCH_TAKES_ARG (text[1])))
151 skip -= (text[2] != '\0'); /* See gcc.c. */
153 if (text[1] == 'B')
154 opt = OPTION_B, skip = (text[2] == '\0'), arg = text + 2;
155 else if (text[1] == 'b')
156 opt = OPTION_b, skip = (text[2] == '\0'), arg = text + 2;
157 else if ((text[1] == 'c') && (text[2] == '\0'))
158 opt = OPTION_c, skip = 0;
159 else if ((text[1] == 'E') && (text[2] == '\0'))
160 opt = OPTION_E, skip = 0;
161 else if (text[1] == 'i')
162 opt = OPTION_i, skip = 0;
163 else if (text[1] == 'l')
164 opt = OPTION_l;
165 else if (text[1] == 'L')
166 opt = OPTION_L, arg = text + 2;
167 else if (text[1] == 'o')
168 opt = OPTION_o;
169 else if ((text[1] == 'S') && (text[2] == '\0'))
170 opt = OPTION_S, skip = 0;
171 else if (text[1] == 'V')
172 opt = OPTION_V, skip = (text[2] == '\0');
173 else if ((text[1] == 'v') && (text[2] == '\0'))
174 opt = OPTION_v, skip = 0;
175 else if (text[1] == 'x')
176 opt = OPTION_x, arg = text + 2;
177 else
179 if ((skip = WORD_SWITCH_TAKES_ARG (text + 1)) != 0) /* See gcc.c. */
181 else if (! strncmp (text, "-fdriver", 8)) /* Really --driver!! */
182 opt = OPTION_driver; /* Never mind arg, this is unsupported. */
183 else if (! strcmp (text, "-fhelp")) /* Really --help!! */
184 opt = OPTION_help;
185 else if (! strcmp (text, "-M"))
186 opt = OPTION_M;
187 else if (! strcmp (text, "-MM"))
188 opt = OPTION_MM;
189 else if (! strcmp (text, "-nostdlib")
190 || ! strcmp (text, "-nodefaultlibs"))
191 opt = OPTION_nostdlib;
192 else if (! strcmp (text, "-fsyntax-only"))
193 opt = OPTION_syntax_only;
194 else if (! strcmp (text, "-dumpversion"))
195 opt = OPTION_version;
196 else if (! strcmp (text, "-Xlinker")
197 || ! strcmp (text, "-specs"))
198 skip = 1;
199 else
200 skip = 0;
203 if (xopt != NULL)
204 *xopt = opt;
205 if (xskip != NULL)
206 *xskip = skip;
207 if (xarg != NULL)
209 if ((arg != NULL)
210 && (arg[0] == '\0'))
211 *xarg = NULL;
212 else
213 *xarg = arg;
217 /* Append another argument to the list being built. As long as it is
218 identical to the corresponding arg in the original list, just increment
219 the new arg count. Otherwise allocate a new list, etc. */
221 static void
222 append_arg (arg)
223 const char *arg;
225 static int newargsize;
227 #if 0
228 fprintf (stderr, "`%s'\n", arg);
229 #endif
231 if (g77_newargv == g77_xargv
232 && g77_newargc < g77_xargc
233 && (arg == g77_xargv[g77_newargc]
234 || ! strcmp (arg, g77_xargv[g77_newargc])))
236 ++g77_newargc;
237 return; /* Nothing new here. */
240 if (g77_newargv == g77_xargv)
241 { /* Make new arglist. */
242 int i;
244 newargsize = (g77_xargc << 2) + 20; /* This should handle all. */
245 real_g77_newargv = (char **) xmalloc (newargsize * sizeof (char *));
246 g77_newargv = (const char **) real_g77_newargv;
248 /* Copy what has been done so far. */
249 for (i = 0; i < g77_newargc; ++i)
250 g77_newargv[i] = g77_xargv[i];
253 if (g77_newargc == newargsize)
254 fatal ("overflowed output arg list for `%s'", arg);
256 g77_newargv[g77_newargc++] = arg;
259 void
260 lang_specific_driver (in_argc, in_argv, in_added_libraries)
261 int *in_argc;
262 char ***in_argv;
263 int *in_added_libraries ATTRIBUTE_UNUSED;
265 int argc = *in_argc;
266 const char **argv = (const char **) *in_argv;
267 int i;
268 int verbose = 0;
269 Option opt;
270 int skip;
271 const char *arg;
273 /* This will be NULL if we encounter a situation where we should not
274 link in libf2c. */
275 const char *library = FORTRAN_LIBRARY;
277 /* This will become 0 if anything other than -v and kin (like -V)
278 is seen, meaning the user is trying to accomplish something.
279 If it remains nonzero, and the user wants version info, add stuff to
280 the command line to make gcc invoke all the appropriate phases
281 to get all the version info. */
282 int add_version_magic = 1;
284 /* 0 => -xnone in effect.
285 1 => -xfoo in effect. */
286 int saw_speclang = 0;
288 /* 0 => initial/reset state
289 1 => last arg was -l<library>
290 2 => last two args were -l<library> -lm. */
291 int saw_library = 0;
293 /* By default, we throw on the math library if we have one. */
294 int need_math = (MATH_LIBRARY[0] != '\0');
296 /* The number of input and output files in the incoming arg list. */
297 int n_infiles = 0;
298 int n_outfiles = 0;
300 #if 0
301 fprintf (stderr, "Incoming:");
302 for (i = 0; i < argc; i++)
303 fprintf (stderr, " %s", argv[i]);
304 fprintf (stderr, "\n");
305 #endif
307 real_g77_newargv = *in_argv;
309 g77_xargc = argc;
310 g77_xargv = argv;
311 g77_newargc = 0;
312 g77_newargv = argv;
314 /* First pass through arglist.
316 If -nostdlib or a "turn-off-linking" option is anywhere in the
317 command line, don't do any library-option processing (except
318 relating to -x). Also, if -v is specified, but no other options
319 that do anything special (allowing -V version, etc.), remember
320 to add special stuff to make gcc command actually invoke all
321 the different phases of the compilation process so all the version
322 numbers can be seen.
324 Also, here is where all problems with missing arguments to options
325 are caught. If this loop is exited normally, it means all options
326 have the appropriate number of arguments as far as the rest of this
327 program is concerned. */
329 for (i = 1; i < argc; ++i)
331 if ((argv[i][0] == '+') && (argv[i][1] == 'e'))
333 add_version_magic = 0;
334 continue;
337 if ((argv[i][0] != '-') || (argv[i][1] == '\0'))
339 ++n_infiles;
340 add_version_magic = 0;
341 continue;
344 lookup_option (&opt, &skip, NULL, argv[i]);
346 switch (opt)
348 case OPTION_nostdlib:
349 case OPTION_c:
350 case OPTION_S:
351 case OPTION_syntax_only:
352 case OPTION_E:
353 case OPTION_M:
354 case OPTION_MM:
355 /* These options disable linking entirely or linking of the
356 standard libraries. */
357 library = 0;
358 add_version_magic = 0;
359 break;
361 case OPTION_l:
362 ++n_infiles;
363 add_version_magic = 0;
364 break;
366 case OPTION_o:
367 ++n_outfiles;
368 add_version_magic = 0;
369 break;
371 case OPTION_v:
372 if (! verbose)
373 fprintf (stderr, "g77 version %s (from FSF-g77 version %s)\n",
374 version_string, ffe_version_string);
375 verbose = 1;
376 break;
378 case OPTION_b:
379 case OPTION_B:
380 case OPTION_L:
381 case OPTION_i:
382 case OPTION_V:
383 /* These options are useful in conjunction with -v to get
384 appropriate version info. */
385 break;
387 case OPTION_version:
388 printf ("\
389 GNU Fortran %s\n\
390 Copyright (C) 1997 Free Software Foundation, Inc.\n\
391 For more version information on components of the GNU Fortran\n\
392 compilation system, especially useful when reporting bugs,\n\
393 type the command `g77 --verbose'.\n\
395 GNU Fortran comes with NO WARRANTY, to the extent permitted by law.\n\
396 You may redistribute copies of GNU Fortran\n\
397 under the terms of the GNU General Public License.\n\
398 For more information about these matters, see the file named COPYING\n\
399 or type the command `info -f g77 Copying'.\n\
400 ", ffe_version_string);
401 exit (0);
402 break;
404 case OPTION_help:
405 /* Let gcc.c handle this, as the egcs version has a really
406 cool facility for handling --help and --verbose --help. */
407 return;
409 #if 0
410 printf ("\
411 Usage: g77 [OPTION]... FORTRAN-SOURCE...\n\
413 Compile and link Fortran source code to produce an executable program,\n\
414 which by default is named `a.out', and can be invoked with the UNIX\n\
415 command `./a.out'.\n\
417 Options:\n\
418 --debug include debugging information in executable.\n\
419 --help display this help and exit.\n\
420 --optimize[=LEVEL] take extra time and memory to make generated\n\
421 executable run faster. LEVEL is 0 for no\n\
422 optimization, 1 for normal optimization, and\n\
423 increases through 3 for more optimization.\n\
424 --output=PROGRAM name the executable PROGRAM instead of a.out;\n\
425 invoke with the command `./PROGRAM'.\n\
426 --version display version information and exit.\n\
428 Many other options exist to tailor the compilation process, specify\n\
429 the dialect of the Fortran source code, specify details of the\n\
430 code-generation methodology, and so on.\n\
432 For more information on g77 and gcc, type the commands `info -f g77'\n\
433 and `info -f gcc' to read the Info documentation.\n\
435 For bug reporting instructions, please see:\n\
436 <URL:http://www.gnu.org/software/gcc/faq.html#bugreport>.\n");
437 exit (0);
438 break;
439 #endif
441 case OPTION_driver:
442 fatal ("--driver no longer supported");
443 break;
445 default:
446 add_version_magic = 0;
447 break;
450 /* This is the one place we check for missing arguments in the
451 program. */
453 if (i + skip < argc)
454 i += skip;
455 else
456 fatal ("argument to `%s' missing", argv[i]);
459 if ((n_outfiles != 0) && (n_infiles == 0))
460 fatal ("No input files; unwilling to write output files");
462 /* Second pass through arglist, transforming arguments as appropriate. */
464 append_arg (argv[0]); /* Start with command name, of course. */
466 for (i = 1; i < argc; ++i)
468 if (argv[i][0] == '\0')
470 append_arg (argv[i]); /* Interesting. Just append as is. */
471 continue;
474 if ((argv[i][0] == '-') && (argv[i][1] != 'l'))
476 /* Not a filename or library. */
478 if (saw_library == 1 && need_math) /* -l<library>. */
479 append_arg (MATH_LIBRARY);
481 saw_library = 0;
483 lookup_option (&opt, &skip, &arg, argv[i]);
485 if (argv[i][1] == '\0')
487 append_arg (argv[i]); /* "-" == Standard input. */
488 continue;
491 if (opt == OPTION_x)
493 /* Track input language. */
494 const char *lang;
496 if (arg == NULL)
497 lang = argv[i+1];
498 else
499 lang = arg;
501 saw_speclang = (strcmp (lang, "none") != 0);
504 append_arg (argv[i]);
506 for (; skip != 0; --skip)
507 append_arg (argv[++i]);
509 continue;
512 /* A filename/library, not an option. */
514 if (saw_speclang)
515 saw_library = 0; /* -xfoo currently active. */
516 else
517 { /* -lfoo or filename. */
518 if (strcmp (argv[i], MATH_LIBRARY) == 0
519 #ifdef ALT_LIBM
520 || strcmp (argv[i], ALT_LIBM) == 0
521 #endif
524 if (saw_library == 1)
525 saw_library = 2; /* -l<library> -lm. */
526 else
527 append_arg (FORTRAN_LIBRARY);
529 else if (strcmp (argv[i], FORTRAN_LIBRARY) == 0)
530 saw_library = 1; /* -l<library>. */
531 else
532 { /* Other library, or filename. */
533 if (saw_library == 1 && need_math)
534 append_arg (MATH_LIBRARY);
535 saw_library = 0;
538 append_arg (argv[i]);
541 /* Append `-lg2c -lm' as necessary. */
543 if (! add_version_magic && library)
544 { /* Doing a link and no -nostdlib. */
545 if (saw_speclang)
546 append_arg ("-xnone");
548 switch (saw_library)
550 case 0:
551 append_arg (library);
552 case 1:
553 if (need_math)
554 append_arg (MATH_LIBRARY);
555 default:
556 break;
559 else if (add_version_magic && verbose)
561 append_arg ("-c");
562 append_arg ("-xf77-version");
563 append_arg ("/dev/null");
564 append_arg ("-xnone");
567 if (verbose
568 && g77_newargv != g77_xargv)
570 fprintf (stderr, "Driving:");
571 for (i = 0; i < g77_newargc; i++)
572 fprintf (stderr, " %s", g77_newargv[i]);
573 fprintf (stderr, "\n");
576 *in_argc = g77_newargc;
577 *in_argv = real_g77_newargv;
580 /* Called before linking. Returns 0 on success and -1 on failure. */
581 int lang_specific_pre_link () /* Not used for F77. */
583 return 0;
586 /* Number of extra output files that lang_specific_pre_link may generate. */
587 int lang_specific_extra_outfiles = 0; /* Not used for F77. */