Add D30V options
[official-gcc.git] / gcc / f / g77spec.c
blob532154b9187278ef496726e3eaea0491e28afa17
1 /* Specific flags and argument handling of the Fortran front-end.
2 Copyright (C) 1997, 1999, 2000 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 /* --- This comes from gcc.c (2.8.1) verbatim: */
101 /* This defines which switch letters take arguments. */
103 #define DEFAULT_SWITCH_TAKES_ARG(CHAR) \
104 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
105 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
106 || (CHAR) == 'I' || (CHAR) == 'm' || (CHAR) == 'x' \
107 || (CHAR) == 'L' || (CHAR) == 'A')
109 #ifndef SWITCH_TAKES_ARG
110 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
111 #endif
113 /* This defines which multi-letter switches take arguments. */
115 #define DEFAULT_WORD_SWITCH_TAKES_ARG(STR) \
116 (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext") \
117 || !strcmp (STR, "Tbss") || !strcmp (STR, "include") \
118 || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
119 || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
120 || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
121 || !strcmp (STR, "isystem") || !strcmp (STR, "specs"))
123 #ifndef WORD_SWITCH_TAKES_ARG
124 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
125 #endif
127 /* --- End of verbatim. */
129 /* Assumes text[0] == '-'. Returns number of argv items that belong to
130 (and follow) this one, an option id for options important to the
131 caller, and a pointer to the first char of the arg, if embedded (else
132 returns NULL, meaning no arg or it's the next argv).
134 Note that this also assumes gcc.c's pass converting long options
135 to short ones, where available, has already been run. */
137 static void
138 lookup_option (xopt, xskip, xarg, text)
139 Option *xopt;
140 int *xskip;
141 const char **xarg;
142 const char *text;
144 Option opt = OPTION_;
145 int skip;
146 const char *arg = NULL;
148 if ((skip = SWITCH_TAKES_ARG (text[1])))
149 skip -= (text[2] != '\0'); /* See gcc.c. */
151 if (text[1] == 'B')
152 opt = OPTION_B, skip = (text[2] == '\0'), arg = text + 2;
153 else if (text[1] == 'b')
154 opt = OPTION_b, skip = (text[2] == '\0'), arg = text + 2;
155 else if ((text[1] == 'c') && (text[2] == '\0'))
156 opt = OPTION_c, skip = 0;
157 else if ((text[1] == 'E') && (text[2] == '\0'))
158 opt = OPTION_E, skip = 0;
159 else if (text[1] == 'i')
160 opt = OPTION_i, skip = 0;
161 else if (text[1] == 'l')
162 opt = OPTION_l;
163 else if (text[1] == 'L')
164 opt = OPTION_L, arg = text + 2;
165 else if (text[1] == 'o')
166 opt = OPTION_o;
167 else if ((text[1] == 'S') && (text[2] == '\0'))
168 opt = OPTION_S, skip = 0;
169 else if (text[1] == 'V')
170 opt = OPTION_V, skip = (text[2] == '\0');
171 else if ((text[1] == 'v') && (text[2] == '\0'))
172 opt = OPTION_v, skip = 0;
173 else if (text[1] == 'x')
174 opt = OPTION_x, arg = text + 2;
175 else
177 if ((skip = WORD_SWITCH_TAKES_ARG (text + 1)) != 0) /* See gcc.c. */
179 else if (! strncmp (text, "-fdriver", 8)) /* Really --driver!! */
180 opt = OPTION_driver; /* Never mind arg, this is unsupported. */
181 else if (! strcmp (text, "-fhelp")) /* Really --help!! */
182 opt = OPTION_help;
183 else if (! strcmp (text, "-M"))
184 opt = OPTION_M;
185 else if (! strcmp (text, "-MM"))
186 opt = OPTION_MM;
187 else if (! strcmp (text, "-nostdlib")
188 || ! strcmp (text, "-nodefaultlibs"))
189 opt = OPTION_nostdlib;
190 else if (! strcmp (text, "-fsyntax-only"))
191 opt = OPTION_syntax_only;
192 else if (! strcmp (text, "-dumpversion"))
193 opt = OPTION_version;
194 else if (! strcmp (text, "-Xlinker")
195 || ! strcmp (text, "-specs"))
196 skip = 1;
197 else
198 skip = 0;
201 if (xopt != NULL)
202 *xopt = opt;
203 if (xskip != NULL)
204 *xskip = skip;
205 if (xarg != NULL)
207 if ((arg != NULL)
208 && (arg[0] == '\0'))
209 *xarg = NULL;
210 else
211 *xarg = arg;
215 /* Append another argument to the list being built. As long as it is
216 identical to the corresponding arg in the original list, just increment
217 the new arg count. Otherwise allocate a new list, etc. */
219 static void
220 append_arg (arg)
221 const char *arg;
223 static int newargsize;
225 #if 0
226 fprintf (stderr, "`%s'\n", arg);
227 #endif
229 if (g77_newargv == g77_xargv
230 && g77_newargc < g77_xargc
231 && (arg == g77_xargv[g77_newargc]
232 || ! strcmp (arg, g77_xargv[g77_newargc])))
234 ++g77_newargc;
235 return; /* Nothing new here. */
238 if (g77_newargv == g77_xargv)
239 { /* Make new arglist. */
240 int i;
242 newargsize = (g77_xargc << 2) + 20; /* This should handle all. */
243 real_g77_newargv = (char **) xmalloc (newargsize * sizeof (char *));
244 g77_newargv = (const char **) real_g77_newargv;
246 /* Copy what has been done so far. */
247 for (i = 0; i < g77_newargc; ++i)
248 g77_newargv[i] = g77_xargv[i];
251 if (g77_newargc == newargsize)
252 fatal ("overflowed output arg list for `%s'", arg);
254 g77_newargv[g77_newargc++] = arg;
257 void
258 lang_specific_driver (in_argc, in_argv, in_added_libraries)
259 int *in_argc;
260 char ***in_argv;
261 int *in_added_libraries ATTRIBUTE_UNUSED;
263 int argc = *in_argc;
264 const char **argv = (const char **) *in_argv;
265 int i;
266 int verbose = 0;
267 Option opt;
268 int skip;
269 const char *arg;
271 /* This will be NULL if we encounter a situation where we should not
272 link in libf2c. */
273 const char *library = FORTRAN_LIBRARY;
275 /* This will become 0 if anything other than -v and kin (like -V)
276 is seen, meaning the user is trying to accomplish something.
277 If it remains nonzero, and the user wants version info, add stuff to
278 the command line to make gcc invoke all the appropriate phases
279 to get all the version info. */
280 int add_version_magic = 1;
282 /* 0 => -xnone in effect.
283 1 => -xfoo in effect. */
284 int saw_speclang = 0;
286 /* 0 => initial/reset state
287 1 => last arg was -l<library>
288 2 => last two args were -l<library> -lm. */
289 int saw_library = 0;
291 /* By default, we throw on the math library if we have one. */
292 int need_math = (MATH_LIBRARY[0] != '\0');
294 /* The number of input and output files in the incoming arg list. */
295 int n_infiles = 0;
296 int n_outfiles = 0;
298 #if 0
299 fprintf (stderr, "Incoming:");
300 for (i = 0; i < argc; i++)
301 fprintf (stderr, " %s", argv[i]);
302 fprintf (stderr, "\n");
303 #endif
305 real_g77_newargv = *in_argv;
307 g77_xargc = argc;
308 g77_xargv = argv;
309 g77_newargc = 0;
310 g77_newargv = argv;
312 /* First pass through arglist.
314 If -nostdlib or a "turn-off-linking" option is anywhere in the
315 command line, don't do any library-option processing (except
316 relating to -x). Also, if -v is specified, but no other options
317 that do anything special (allowing -V version, etc.), remember
318 to add special stuff to make gcc command actually invoke all
319 the different phases of the compilation process so all the version
320 numbers can be seen.
322 Also, here is where all problems with missing arguments to options
323 are caught. If this loop is exited normally, it means all options
324 have the appropriate number of arguments as far as the rest of this
325 program is concerned. */
327 for (i = 1; i < argc; ++i)
329 if ((argv[i][0] == '+') && (argv[i][1] == 'e'))
331 add_version_magic = 0;
332 continue;
335 if ((argv[i][0] != '-') || (argv[i][1] == '\0'))
337 ++n_infiles;
338 add_version_magic = 0;
339 continue;
342 lookup_option (&opt, &skip, NULL, argv[i]);
344 switch (opt)
346 case OPTION_nostdlib:
347 case OPTION_c:
348 case OPTION_S:
349 case OPTION_syntax_only:
350 case OPTION_E:
351 case OPTION_M:
352 case OPTION_MM:
353 /* These options disable linking entirely or linking of the
354 standard libraries. */
355 library = 0;
356 add_version_magic = 0;
357 break;
359 case OPTION_l:
360 ++n_infiles;
361 add_version_magic = 0;
362 break;
364 case OPTION_o:
365 ++n_outfiles;
366 add_version_magic = 0;
367 break;
369 case OPTION_v:
370 if (! verbose)
371 fprintf (stderr, "g77 version %s (from FSF-g77 version %s)\n",
372 version_string, ffe_version_string);
373 verbose = 1;
374 break;
376 case OPTION_b:
377 case OPTION_B:
378 case OPTION_L:
379 case OPTION_i:
380 case OPTION_V:
381 /* These options are useful in conjunction with -v to get
382 appropriate version info. */
383 break;
385 case OPTION_version:
386 printf ("\
387 GNU Fortran %s\n\
388 Copyright (C) 1997 Free Software Foundation, Inc.\n\
389 For more version information on components of the GNU Fortran\n\
390 compilation system, especially useful when reporting bugs,\n\
391 type the command `g77 --verbose'.\n\
393 GNU Fortran comes with NO WARRANTY, to the extent permitted by law.\n\
394 You may redistribute copies of GNU Fortran\n\
395 under the terms of the GNU General Public License.\n\
396 For more information about these matters, see the file named COPYING\n\
397 or type the command `info -f g77 Copying'.\n\
398 ", ffe_version_string);
399 exit (0);
400 break;
402 case OPTION_help:
403 /* Let gcc.c handle this, as the egcs version has a really
404 cool facility for handling --help and --verbose --help. */
405 return;
407 #if 0
408 printf ("\
409 Usage: g77 [OPTION]... FORTRAN-SOURCE...\n\
411 Compile and link Fortran source code to produce an executable program,\n\
412 which by default is named `a.out', and can be invoked with the UNIX\n\
413 command `./a.out'.\n\
415 Options:\n\
416 --debug include debugging information in executable.\n\
417 --help display this help and exit.\n\
418 --optimize[=LEVEL] take extra time and memory to make generated\n\
419 executable run faster. LEVEL is 0 for no\n\
420 optimization, 1 for normal optimization, and\n\
421 increases through 3 for more optimization.\n\
422 --output=PROGRAM name the executable PROGRAM instead of a.out;\n\
423 invoke with the command `./PROGRAM'.\n\
424 --version display version information and exit.\n\
426 Many other options exist to tailor the compilation process, specify\n\
427 the dialect of the Fortran source code, specify details of the\n\
428 code-generation methodology, and so on.\n\
430 For more information on g77 and gcc, type the commands `info -f g77'\n\
431 and `info -f gcc' to read the Info documentation.\n\
433 For bug reporting instructions, please see:\n\
434 %s.\n", GCCBUGURL);
435 exit (0);
436 break;
437 #endif
439 case OPTION_driver:
440 fatal ("--driver no longer supported");
441 break;
443 default:
444 add_version_magic = 0;
445 break;
448 /* This is the one place we check for missing arguments in the
449 program. */
451 if (i + skip < argc)
452 i += skip;
453 else
454 fatal ("argument to `%s' missing", argv[i]);
457 if ((n_outfiles != 0) && (n_infiles == 0))
458 fatal ("No input files; unwilling to write output files");
460 /* Second pass through arglist, transforming arguments as appropriate. */
462 append_arg (argv[0]); /* Start with command name, of course. */
464 for (i = 1; i < argc; ++i)
466 if (argv[i][0] == '\0')
468 append_arg (argv[i]); /* Interesting. Just append as is. */
469 continue;
472 if ((argv[i][0] == '-') && (argv[i][1] != 'l'))
474 /* Not a filename or library. */
476 if (saw_library == 1 && need_math) /* -l<library>. */
477 append_arg (MATH_LIBRARY);
479 saw_library = 0;
481 lookup_option (&opt, &skip, &arg, argv[i]);
483 if (argv[i][1] == '\0')
485 append_arg (argv[i]); /* "-" == Standard input. */
486 continue;
489 if (opt == OPTION_x)
491 /* Track input language. */
492 const char *lang;
494 if (arg == NULL)
495 lang = argv[i+1];
496 else
497 lang = arg;
499 saw_speclang = (strcmp (lang, "none") != 0);
502 append_arg (argv[i]);
504 for (; skip != 0; --skip)
505 append_arg (argv[++i]);
507 continue;
510 /* A filename/library, not an option. */
512 if (saw_speclang)
513 saw_library = 0; /* -xfoo currently active. */
514 else
515 { /* -lfoo or filename. */
516 if (strcmp (argv[i], MATH_LIBRARY) == 0
517 #ifdef ALT_LIBM
518 || strcmp (argv[i], ALT_LIBM) == 0
519 #endif
522 if (saw_library == 1)
523 saw_library = 2; /* -l<library> -lm. */
524 else
525 append_arg (FORTRAN_LIBRARY);
527 else if (strcmp (argv[i], FORTRAN_LIBRARY) == 0)
528 saw_library = 1; /* -l<library>. */
529 else
530 { /* Other library, or filename. */
531 if (saw_library == 1 && need_math)
532 append_arg (MATH_LIBRARY);
533 saw_library = 0;
536 append_arg (argv[i]);
539 /* Append `-lg2c -lm' as necessary. */
541 if (! add_version_magic && library)
542 { /* Doing a link and no -nostdlib. */
543 if (saw_speclang)
544 append_arg ("-xnone");
546 switch (saw_library)
548 case 0:
549 append_arg (library);
550 case 1:
551 if (need_math)
552 append_arg (MATH_LIBRARY);
553 default:
554 break;
557 else if (add_version_magic && verbose)
559 append_arg ("-c");
560 append_arg ("-xf77-version");
561 append_arg ("/dev/null");
562 append_arg ("-xnone");
565 if (verbose
566 && g77_newargv != g77_xargv)
568 fprintf (stderr, "Driving:");
569 for (i = 0; i < g77_newargc; i++)
570 fprintf (stderr, " %s", g77_newargv[i]);
571 fprintf (stderr, "\n");
574 *in_argc = g77_newargc;
575 *in_argv = real_g77_newargv;
578 /* Called before linking. Returns 0 on success and -1 on failure. */
579 int lang_specific_pre_link () /* Not used for F77. */
581 return 0;
584 /* Number of extra output files that lang_specific_pre_link may generate. */
585 int lang_specific_extra_outfiles = 0; /* Not used for F77. */