This commit was manufactured by cvs2svn to create branch
[official-gcc.git] / gcc / f / g77spec.c
blob3dca7bc44838d114b2d1543f362196425b87565f
1 /* Specific flags and argument handling of the Fortran front-end.
2 Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* This file contains a filter for the main `gcc' driver, which is
23 replicated for the `g77' driver by adding this filter. The purpose
24 of this filter is to be basically identical to gcc (in that
25 it faithfully passes all of the original arguments to gcc) but,
26 unless explicitly overridden by the user in certain ways, ensure
27 that the needs of the language supported by this wrapper are met.
29 For GNU Fortran (g77), we do the following to the argument list
30 before passing it to `gcc':
32 1. Make sure `-lg2c -lm' is at the end of the list.
34 2. Make sure each time `-lg2c' or `-lm' is seen, it forms
35 part of the series `-lg2c -lm'.
37 #1 and #2 are not done if `-nostdlib' or any option that disables
38 the linking phase is present, or if `-xfoo' is in effect. Note that
39 a lack of source files or -l options disables linking.
41 This program was originally made out of gcc/cp/g++spec.c, but the
42 way it builds the new argument list was rewritten so it is much
43 easier to maintain, improve the way it decides to add or not add
44 extra arguments, etc. And several improvements were made in the
45 handling of arguments, primarily to make it more consistent with
46 `gcc' itself. */
48 #include "config.h"
49 #include "system.h"
50 #include "coretypes.h"
51 #include "tm.h"
52 #include "gcc.h"
53 #include "intl.h"
55 #ifndef MATH_LIBRARY
56 #define MATH_LIBRARY "-lm"
57 #endif
59 #ifndef FORTRAN_INIT
60 #define FORTRAN_INIT "-lfrtbegin"
61 #endif
63 #ifndef FORTRAN_LIBRARY
64 #define FORTRAN_LIBRARY "-lg2c"
65 #endif
67 /* Options this driver needs to recognize, not just know how to
68 skip over. */
69 typedef enum
71 OPTION_b, /* Aka --prefix. */
72 OPTION_B, /* Aka --target. */
73 OPTION_c, /* Aka --compile. */
74 OPTION_driver, /* Wrapper-specific option. */
75 OPTION_E, /* Aka --preprocess. */
76 OPTION_help, /* --help. */
77 OPTION_i, /* -imacros, -include, -include-*. */
78 OPTION_l,
79 OPTION_L, /* Aka --library-directory. */
80 OPTION_M, /* Aka --dependencies. */
81 OPTION_MM, /* Aka --user-dependencies. */
82 OPTION_nostdlib, /* Aka --no-standard-libraries, or
83 -nodefaultlibs. */
84 OPTION_o, /* Aka --output. */
85 OPTION_S, /* Aka --assemble. */
86 OPTION_syntax_only, /* -fsyntax-only. */
87 OPTION_v, /* Aka --verbose. */
88 OPTION_version, /* --version. */
89 OPTION_V, /* Aka --use-version. */
90 OPTION_x, /* Aka --language. */
91 OPTION_ /* Unrecognized or unimportant. */
92 } Option;
94 /* The original argument list and related info is copied here. */
95 static int g77_xargc;
96 static const char *const *g77_xargv;
97 static void lookup_option (Option *, int *, const char **, const char *);
98 static void append_arg (const char *);
100 /* The new argument list will be built here. */
101 static int g77_newargc;
102 static const char **g77_newargv;
104 #ifndef SWITCH_TAKES_ARG
105 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
106 #endif
108 #ifndef WORD_SWITCH_TAKES_ARG
109 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
110 #endif
112 /* Assumes text[0] == '-'. Returns number of argv items that belong to
113 (and follow) this one, an option id for options important to the
114 caller, and a pointer to the first char of the arg, if embedded (else
115 returns NULL, meaning no arg or it's the next argv).
117 Note that this also assumes gcc.c's pass converting long options
118 to short ones, where available, has already been run. */
120 static void
121 lookup_option (Option *xopt, int *xskip, const char **xarg, const char *text)
123 Option opt = OPTION_;
124 int skip;
125 const char *arg = NULL;
127 if ((skip = SWITCH_TAKES_ARG (text[1])))
128 skip -= (text[2] != '\0'); /* See gcc.c. */
130 if (text[1] == 'B')
131 opt = OPTION_B, skip = (text[2] == '\0'), arg = text + 2;
132 else if (text[1] == 'b')
133 opt = OPTION_b, skip = (text[2] == '\0'), arg = text + 2;
134 else if ((text[1] == 'c') && (text[2] == '\0'))
135 opt = OPTION_c, skip = 0;
136 else if ((text[1] == 'E') && (text[2] == '\0'))
137 opt = OPTION_E, skip = 0;
138 else if (text[1] == 'i')
139 opt = OPTION_i, skip = 0;
140 else if (text[1] == 'l')
141 opt = OPTION_l;
142 else if (text[1] == 'L')
143 opt = OPTION_L, arg = text + 2;
144 else if (text[1] == 'o')
145 opt = OPTION_o;
146 else if ((text[1] == 'S') && (text[2] == '\0'))
147 opt = OPTION_S, skip = 0;
148 else if (text[1] == 'V')
149 opt = OPTION_V, skip = (text[2] == '\0');
150 else if ((text[1] == 'v') && (text[2] == '\0'))
151 opt = OPTION_v, skip = 0;
152 else if (text[1] == 'x')
153 opt = OPTION_x, arg = text + 2;
154 else
156 if ((skip = WORD_SWITCH_TAKES_ARG (text + 1)) != 0) /* See gcc.c. */
158 else if (! strncmp (text, "-fdriver", 8)) /* Really --driver!! */
159 opt = OPTION_driver; /* Never mind arg, this is unsupported. */
160 else if (! strcmp (text, "-fhelp")) /* Really --help!! */
161 opt = OPTION_help;
162 else if (! strcmp (text, "-M"))
163 opt = OPTION_M;
164 else if (! strcmp (text, "-MM"))
165 opt = OPTION_MM;
166 else if (! strcmp (text, "-nostdlib")
167 || ! strcmp (text, "-nodefaultlibs"))
168 opt = OPTION_nostdlib;
169 else if (! strcmp (text, "-fsyntax-only"))
170 opt = OPTION_syntax_only;
171 else if (! strcmp (text, "-dumpversion"))
172 opt = OPTION_version;
173 else if (! strcmp (text, "-fversion")) /* Really --version!! */
174 opt = OPTION_version;
175 else if (! strcmp (text, "-Xlinker")
176 || ! strcmp (text, "-specs"))
177 skip = 1;
178 else
179 skip = 0;
182 if (xopt != NULL)
183 *xopt = opt;
184 if (xskip != NULL)
185 *xskip = skip;
186 if (xarg != NULL)
188 if ((arg != NULL)
189 && (arg[0] == '\0'))
190 *xarg = NULL;
191 else
192 *xarg = arg;
196 /* Append another argument to the list being built. As long as it is
197 identical to the corresponding arg in the original list, just increment
198 the new arg count. Otherwise allocate a new list, etc. */
200 static void
201 append_arg (const char *arg)
203 static int newargsize;
205 #if 0
206 fprintf (stderr, "`%s'\n", arg);
207 #endif
209 if (g77_newargv == g77_xargv
210 && g77_newargc < g77_xargc
211 && (arg == g77_xargv[g77_newargc]
212 || ! strcmp (arg, g77_xargv[g77_newargc])))
214 ++g77_newargc;
215 return; /* Nothing new here. */
218 if (g77_newargv == g77_xargv)
219 { /* Make new arglist. */
220 int i;
222 newargsize = (g77_xargc << 2) + 20; /* This should handle all. */
223 g77_newargv = xmalloc (newargsize * sizeof (char *));
225 /* Copy what has been done so far. */
226 for (i = 0; i < g77_newargc; ++i)
227 g77_newargv[i] = g77_xargv[i];
230 if (g77_newargc == newargsize)
231 fatal ("overflowed output arg list for `%s'", arg);
233 g77_newargv[g77_newargc++] = arg;
236 void
237 lang_specific_driver (int *in_argc, const char *const **in_argv,
238 int *in_added_libraries ATTRIBUTE_UNUSED)
240 int argc = *in_argc;
241 const char *const *argv = *in_argv;
242 int i;
243 int verbose = 0;
244 Option opt;
245 int skip;
246 const char *arg;
248 /* This will be NULL if we encounter a situation where we should not
249 link in libf2c. */
250 const char *library = FORTRAN_LIBRARY;
252 /* 0 => -xnone in effect.
253 1 => -xfoo in effect. */
254 int saw_speclang = 0;
256 /* 0 => initial/reset state
257 1 => last arg was -l<library>
258 2 => last two args were -l<library> -lm. */
259 int saw_library = 0;
261 /* 0 => initial/reset state
262 1 => FORTRAN_INIT linked in */
263 int use_init = 0;
264 /* By default, we throw on the math library if we have one. */
265 int need_math = (MATH_LIBRARY[0] != '\0');
267 /* The number of input and output files in the incoming arg list. */
268 int n_infiles = 0;
269 int n_outfiles = 0;
271 #if 0
272 fprintf (stderr, "Incoming:");
273 for (i = 0; i < argc; i++)
274 fprintf (stderr, " %s", argv[i]);
275 fprintf (stderr, "\n");
276 #endif
278 g77_xargc = argc;
279 g77_xargv = argv;
280 g77_newargc = 0;
281 g77_newargv = (const char **) argv;
283 /* First pass through arglist.
285 If -nostdlib or a "turn-off-linking" option is anywhere in the
286 command line, don't do any library-option processing (except
287 relating to -x). Also, if -v is specified, but no other options
288 that do anything special (allowing -V version, etc.), remember
289 to add special stuff to make gcc command actually invoke all
290 the different phases of the compilation process so all the version
291 numbers can be seen.
293 Also, here is where all problems with missing arguments to options
294 are caught. If this loop is exited normally, it means all options
295 have the appropriate number of arguments as far as the rest of this
296 program is concerned. */
298 for (i = 1; i < argc; ++i)
300 if ((argv[i][0] == '+') && (argv[i][1] == 'e'))
302 continue;
305 if ((argv[i][0] != '-') || (argv[i][1] == '\0'))
307 ++n_infiles;
308 continue;
311 lookup_option (&opt, &skip, NULL, argv[i]);
313 switch (opt)
315 case OPTION_nostdlib:
316 case OPTION_c:
317 case OPTION_S:
318 case OPTION_syntax_only:
319 case OPTION_E:
320 case OPTION_M:
321 case OPTION_MM:
322 /* These options disable linking entirely or linking of the
323 standard libraries. */
324 library = 0;
325 break;
327 case OPTION_l:
328 ++n_infiles;
329 break;
331 case OPTION_o:
332 ++n_outfiles;
333 break;
335 case OPTION_v:
336 verbose = 1;
337 break;
339 case OPTION_b:
340 case OPTION_B:
341 case OPTION_L:
342 case OPTION_i:
343 case OPTION_V:
344 /* These options are useful in conjunction with -v to get
345 appropriate version info. */
346 break;
348 case OPTION_version:
349 printf ("GNU Fortran (GCC) %s\n", version_string);
350 printf ("Copyright %s 2004 Free Software Foundation, Inc.\n",
351 _("(C)"));
352 printf ("\n");
353 printf (_("\
354 GNU Fortran comes with NO WARRANTY, to the extent permitted by law.\n\
355 You may redistribute copies of GNU Fortran\n\
356 under the terms of the GNU General Public License.\n\
357 For more information about these matters, see the file named COPYING\n\
358 or type the command `info -f g77 Copying'.\n\
359 "));
360 exit (0);
361 break;
363 case OPTION_help:
364 /* Let gcc.c handle this, as it has a really
365 cool facility for handling --help and --verbose --help. */
366 return;
368 case OPTION_driver:
369 fatal ("--driver no longer supported");
370 break;
372 default:
373 break;
376 /* This is the one place we check for missing arguments in the
377 program. */
379 if (i + skip < argc)
380 i += skip;
381 else
382 fatal ("argument to `%s' missing", argv[i]);
385 if ((n_outfiles != 0) && (n_infiles == 0))
386 fatal ("no input files; unwilling to write output files");
388 /* If there are no input files, no need for the library. */
389 if (n_infiles == 0)
390 library = 0;
392 /* Second pass through arglist, transforming arguments as appropriate. */
394 append_arg (argv[0]); /* Start with command name, of course. */
396 for (i = 1; i < argc; ++i)
398 if (argv[i][0] == '\0')
400 append_arg (argv[i]); /* Interesting. Just append as is. */
401 continue;
404 if ((argv[i][0] == '-') && (argv[i][1] != 'l'))
406 /* Not a filename or library. */
408 if (saw_library == 1 && need_math) /* -l<library>. */
409 append_arg (MATH_LIBRARY);
411 saw_library = 0;
413 lookup_option (&opt, &skip, &arg, argv[i]);
415 if (argv[i][1] == '\0')
417 append_arg (argv[i]); /* "-" == Standard input. */
418 continue;
421 if (opt == OPTION_x)
423 /* Track input language. */
424 const char *lang;
426 if (arg == NULL)
427 lang = argv[i+1];
428 else
429 lang = arg;
431 saw_speclang = (strcmp (lang, "none") != 0);
434 append_arg (argv[i]);
436 for (; skip != 0; --skip)
437 append_arg (argv[++i]);
439 continue;
442 /* A filename/library, not an option. */
444 if (saw_speclang)
445 saw_library = 0; /* -xfoo currently active. */
446 else
447 { /* -lfoo or filename. */
448 if (strcmp (argv[i], MATH_LIBRARY) == 0)
450 if (saw_library == 1)
451 saw_library = 2; /* -l<library> -lm. */
452 else
454 if (0 == use_init)
456 append_arg (FORTRAN_INIT);
457 use_init = 1;
459 append_arg (FORTRAN_LIBRARY);
462 else if (strcmp (argv[i], FORTRAN_LIBRARY) == 0)
463 saw_library = 1; /* -l<library>. */
464 else
465 { /* Other library, or filename. */
466 if (saw_library == 1 && need_math)
467 append_arg (MATH_LIBRARY);
468 saw_library = 0;
471 append_arg (argv[i]);
474 /* Append `-lg2c -lm' as necessary. */
476 if (library)
477 { /* Doing a link and no -nostdlib. */
478 if (saw_speclang)
479 append_arg ("-xnone");
481 switch (saw_library)
483 case 0:
484 if (0 == use_init)
486 append_arg (FORTRAN_INIT);
487 use_init = 1;
489 append_arg (library);
490 case 1:
491 if (need_math)
492 append_arg (MATH_LIBRARY);
493 default:
494 break;
498 #ifdef ENABLE_SHARED_LIBGCC
499 if (library)
501 int i;
503 for (i = 1; i < g77_newargc; i++)
504 if (g77_newargv[i][0] == '-')
505 if (strcmp (g77_newargv[i], "-static-libgcc") == 0
506 || strcmp (g77_newargv[i], "-static") == 0)
507 break;
509 if (i == g77_newargc)
510 append_arg ("-shared-libgcc");
513 #endif
515 if (verbose
516 && g77_newargv != g77_xargv)
518 fprintf (stderr, "Driving:");
519 for (i = 0; i < g77_newargc; i++)
520 fprintf (stderr, " %s", g77_newargv[i]);
521 fprintf (stderr, "\n");
524 *in_argc = g77_newargc;
525 *in_argv = g77_newargv;
528 /* Called before linking. Returns 0 on success and -1 on failure. */
529 int lang_specific_pre_link (void) /* Not used for F77. */
531 return 0;
534 /* Number of extra output files that lang_specific_pre_link may generate. */
535 int lang_specific_extra_outfiles = 0; /* Not used for F77. */
537 /* Table of language-specific spec functions. */
538 const struct spec_function lang_specific_spec_functions[] =
540 { 0, 0 }