1 /* Specific flags and argument handling of the Fortran front-end.
2 Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file is copied more or less verbatim from g77. */
23 /* This file contains a filter for the main `gcc' driver, which is
24 replicated for the `gfortran' driver by adding this filter. The purpose
25 of this filter is to be basically identical to gcc (in that
26 it faithfully passes all of the original arguments to gcc) but,
27 unless explicitly overridden by the user in certain ways, ensure
28 that the needs of the language supported by this wrapper are met.
30 For GNU Fortran 95(gfortran), we do the following to the argument list
31 before passing it to `gcc':
33 1. Make sure `-lgfortran -lm' is at the end of the list.
35 2. Make sure each time `-lgfortran' or `-lm' is seen, it forms
36 part of the series `-lgfortran -lm'.
38 #1 and #2 are not done if `-nostdlib' or any option that disables
39 the linking phase is present, or if `-xfoo' is in effect. Note that
40 a lack of source files or -l options disables linking.
42 This program was originally made out of gcc/cp/g++spec.c, but the
43 way it builds the new argument list was rewritten so it is much
44 easier to maintain, improve the way it decides to add or not add
45 extra arguments, etc. And several improvements were made in the
46 handling of arguments, primarily to make it more consistent with
53 #include "coretypes.h"
58 #define MATH_LIBRARY "-lm"
62 #define FORTRAN_INIT "-lgfortranbegin"
65 #ifndef FORTRAN_LIBRARY
66 #define FORTRAN_LIBRARY "-lgfortran"
69 #ifdef HAVE_LD_STATIC_DYNAMIC
70 #define ADD_ARG_LIBGFORTRAN(arg) \
72 if (static_lib && !static_linking) \
73 append_arg ("-Wl,-Bstatic"); \
75 if (static_lib && !static_linking) \
76 append_arg ("-Wl,-Bdynamic"); \
79 #define ADD_ARG_LIBGFORTRAN(arg) append_arg (arg);
83 /* Options this driver needs to recognize, not just know how to
87 OPTION_b, /* Aka --prefix. */
88 OPTION_B, /* Aka --target. */
89 OPTION_c, /* Aka --compile. */
90 OPTION_E, /* Aka --preprocess. */
91 OPTION_help, /* --help. */
92 OPTION_i, /* -imacros, -include, -include-*. */
94 OPTION_L, /* Aka --library-directory. */
95 OPTION_nostdlib, /* Aka --no-standard-libraries, or
97 OPTION_o, /* Aka --output. */
98 OPTION_S, /* Aka --assemble. */
99 OPTION_static, /* -static. */
100 OPTION_static_libgfortran, /* -static-libgfortran. */
101 OPTION_syntax_only, /* -fsyntax-only. */
102 OPTION_v, /* Aka --verbose. */
103 OPTION_version, /* --version. */
104 OPTION_V, /* Aka --use-version. */
105 OPTION_x, /* Aka --language. */
106 OPTION_ /* Unrecognized or unimportant. */
110 /* The original argument list and related info is copied here. */
111 static int g77_xargc;
112 static const char *const *g77_xargv;
113 static void lookup_option (Option *, int *, const char **, const char *);
114 static void append_arg (const char *);
116 /* The new argument list will be built here. */
117 static int g77_newargc;
118 static const char **g77_newargv;
120 /* --- This comes from gcc.c (2.8.1) verbatim: */
122 /* This defines which switch letters take arguments. */
124 #ifndef SWITCH_TAKES_ARG
125 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
128 /* This defines which multi-letter switches take arguments. */
130 #ifndef WORD_SWITCH_TAKES_ARG
131 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
134 /* --- End of verbatim. */
136 /* Assumes text[0] == '-'. Returns number of argv items that belong to
137 (and follow) this one, an option id for options important to the
138 caller, and a pointer to the first char of the arg, if embedded (else
139 returns NULL, meaning no arg or it's the next argv).
141 Note that this also assumes gcc.c's pass converting long options
142 to short ones, where available, has already been run. */
145 lookup_option (Option *xopt, int *xskip, const char **xarg, const char *text)
147 Option opt = OPTION_;
149 const char *arg = NULL;
151 if ((skip = SWITCH_TAKES_ARG (text[1])))
152 skip -= (text[2] != '\0'); /* See gcc.c. */
155 opt = OPTION_B, skip = (text[2] == '\0'), arg = text + 2;
156 else if (text[1] == 'b')
157 opt = OPTION_b, skip = (text[2] == '\0'), arg = text + 2;
158 else if ((text[1] == 'c') && (text[2] == '\0'))
159 opt = OPTION_c, skip = 0;
160 else if ((text[1] == 'E') && (text[2] == '\0'))
161 opt = OPTION_E, skip = 0;
162 else if (text[1] == 'i')
163 opt = OPTION_i, skip = 0;
164 else if (text[1] == 'l')
166 else if (text[1] == 'L')
167 opt = OPTION_L, arg = text + 2;
168 else if (text[1] == 'o')
170 else if ((text[1] == 'S') && (text[2] == '\0'))
171 opt = OPTION_S, skip = 0;
172 else if (text[1] == 'V')
173 opt = OPTION_V, skip = (text[2] == '\0');
174 else if ((text[1] == 'v') && (text[2] == '\0'))
175 opt = OPTION_v, skip = 0;
176 else if (text[1] == 'x')
177 opt = OPTION_x, arg = text + 2;
178 else if (text[1] == 'J')
182 if ((skip = WORD_SWITCH_TAKES_ARG (text + 1)) != 0) /* See gcc.c. */
184 else if (!strcmp (text, "-fhelp")) /* Really --help!! */
186 else if (!strcmp (text, "-nostdlib")
187 || !strcmp (text, "-nodefaultlibs"))
188 opt = OPTION_nostdlib;
189 else if (!strcmp (text, "-fsyntax-only"))
190 opt = OPTION_syntax_only;
191 else if (!strcmp (text, "-static-libgfortran"))
192 opt = OPTION_static_libgfortran;
193 else if (!strcmp (text, "-dumpversion"))
194 opt = OPTION_version;
195 else if (!strcmp (text, "-fversion")) /* Really --version!! */
196 opt = OPTION_version;
197 else if (!strcmp (text, "-Xlinker") || !strcmp (text, "-specs"))
209 if ((arg != NULL) && (arg[0] == '\0'))
216 /* Append another argument to the list being built. As long as it is
217 identical to the corresponding arg in the original list, just increment
218 the new arg count. Otherwise allocate a new list, etc. */
221 append_arg (const char *arg)
223 static int newargsize;
226 fprintf (stderr, "`%s'\n", arg);
229 if (g77_newargv == g77_xargv
230 && g77_newargc < g77_xargc
231 && (arg == g77_xargv[g77_newargc]
232 || !strcmp (arg, g77_xargv[g77_newargc])))
235 return; /* Nothing new here. */
238 if (g77_newargv == g77_xargv)
239 { /* Make new arglist. */
242 newargsize = (g77_xargc << 2) + 20; /* This should handle all. */
243 g77_newargv = (const char **) xmalloc (newargsize * sizeof (char *));
245 /* Copy what has been done so far. */
246 for (i = 0; i < g77_newargc; ++i)
247 g77_newargv[i] = g77_xargv[i];
250 if (g77_newargc == newargsize)
251 fatal ("overflowed output arg list for '%s'", arg);
253 g77_newargv[g77_newargc++] = arg;
257 lang_specific_driver (int *in_argc, const char *const **in_argv,
258 int *in_added_libraries ATTRIBUTE_UNUSED)
261 const char *const *argv = *in_argv;
268 /* This will be NULL if we encounter a situation where we should not
270 const char *library = FORTRAN_LIBRARY;
272 /* 0 => -xnone in effect.
273 1 => -xfoo in effect. */
274 int saw_speclang = 0;
276 /* 0 => initial/reset state
277 1 => last arg was -l<library>
278 2 => last two args were -l<library> -lm. */
281 /* 0 => initial/reset state
282 1 => FORTRAN_INIT linked in */
285 /* By default, we throw on the math library if we have one. */
286 int need_math = (MATH_LIBRARY[0] != '\0');
288 /* Whether we should link a static libgfortran. */
291 /* Whether we need to link statically. */
292 int static_linking = 0;
294 /* The number of input and output files in the incoming arg list. */
299 fprintf (stderr, "Incoming:");
300 for (i = 0; i < argc; i++)
301 fprintf (stderr, " %s", argv[i]);
302 fprintf (stderr, "\n");
308 g77_newargv = CONST_CAST2 (const char **, const char *const *, argv);
310 /* First pass through arglist.
312 If -nostdlib or a "turn-off-linking" option is anywhere in the
313 command line, don't do any library-option processing (except
314 relating to -x). Also, if -v is specified, but no other options
315 that do anything special (allowing -V version, etc.), remember
316 to add special stuff to make gcc command actually invoke all
317 the different phases of the compilation process so all the version
320 Also, here is where all problems with missing arguments to options
321 are caught. If this loop is exited normally, it means all options
322 have the appropriate number of arguments as far as the rest of this
323 program is concerned. */
325 for (i = 1; i < argc; ++i)
327 if ((argv[i][0] == '+') && (argv[i][1] == 'e'))
332 if ((argv[i][0] != '-') || (argv[i][1] == '\0'))
338 lookup_option (&opt, &skip, NULL, argv[i]);
342 case OPTION_nostdlib:
345 case OPTION_syntax_only:
347 /* These options disable linking entirely or linking of the
348 standard libraries. */
352 case OPTION_static_libgfortran:
376 /* These options are useful in conjunction with -v to get
377 appropriate version info. */
381 printf ("GNU Fortran %s%s\n", pkgversion_string, version_string);
382 printf ("Copyright %s 2008 Free Software Foundation, Inc.\n\n",
384 printf (_("GNU Fortran comes with NO WARRANTY, to the extent permitted by law.\n\
385 You may redistribute copies of GNU Fortran\n\
386 under the terms of the GNU General Public License.\n\
387 For more information about these matters, see the file named COPYING\n\n"));
392 /* Let gcc.c handle this, as it has a really
393 cool facility for handling --help and --verbose --help. */
400 /* This is the one place we check for missing arguments in the
406 fatal ("argument to '%s' missing", argv[i]);
409 if ((n_outfiles != 0) && (n_infiles == 0))
410 fatal ("no input files; unwilling to write output files");
412 /* If there are no input files, no need for the library. */
416 /* Second pass through arglist, transforming arguments as appropriate. */
418 append_arg (argv[0]); /* Start with command name, of course. */
420 for (i = 1; i < argc; ++i)
422 if (argv[i][0] == '\0')
424 append_arg (argv[i]); /* Interesting. Just append as is. */
428 if ((argv[i][0] == '-') && (argv[i][1] == 'M'))
432 if (argv[i][2] == '\0')
434 p = XNEWVEC (char, strlen (argv[i + 1]) + 2);
437 strcpy (&p[2], argv[i + 1]);
442 p = XNEWVEC (char, strlen (argv[i]) + 1);
449 if ((argv[i][0] == '-') && (argv[i][1] != 'l'))
451 /* Not a filename or library. */
453 if (saw_library == 1 && need_math) /* -l<library>. */
454 append_arg (MATH_LIBRARY);
458 lookup_option (&opt, &skip, &arg, argv[i]);
460 if (argv[i][1] == '\0')
462 append_arg (argv[i]); /* "-" == Standard input. */
468 /* Track input language. */
476 saw_speclang = (strcmp (lang, "none") != 0);
479 append_arg (argv[i]);
481 for (; skip != 0; --skip)
482 append_arg (argv[++i]);
487 /* A filename/library, not an option. */
490 saw_library = 0; /* -xfoo currently active. */
492 { /* -lfoo or filename. */
493 if (strcmp (argv[i], MATH_LIBRARY) == 0)
495 if (saw_library == 1)
496 saw_library = 2; /* -l<library> -lm. */
501 append_arg (FORTRAN_INIT);
505 ADD_ARG_LIBGFORTRAN (FORTRAN_LIBRARY);
508 else if (strcmp (argv[i], FORTRAN_LIBRARY) == 0)
510 saw_library = 1; /* -l<library>. */
511 ADD_ARG_LIBGFORTRAN (argv[i]);
515 { /* Other library, or filename. */
516 if (saw_library == 1 && need_math)
517 append_arg (MATH_LIBRARY);
521 append_arg (argv[i]);
524 /* Append `-lg2c -lm' as necessary. */
527 { /* Doing a link and no -nostdlib. */
529 append_arg ("-xnone");
536 append_arg (FORTRAN_INIT);
539 ADD_ARG_LIBGFORTRAN (library);
544 append_arg (MATH_LIBRARY);
550 #ifdef ENABLE_SHARED_LIBGCC
555 for (i = 1; i < g77_newargc; i++)
556 if (g77_newargv[i][0] == '-')
557 if (strcmp (g77_newargv[i], "-static-libgcc") == 0
558 || strcmp (g77_newargv[i], "-static") == 0)
561 if (i == g77_newargc)
562 append_arg ("-shared-libgcc");
567 if (verbose && g77_newargv != g77_xargv)
569 fprintf (stderr, _("Driving:"));
570 for (i = 0; i < g77_newargc; i++)
571 fprintf (stderr, " %s", g77_newargv[i]);
572 fprintf (stderr, "\n");
575 *in_argc = g77_newargc;
576 *in_argv = g77_newargv;
580 /* Called before linking. Returns 0 on success and -1 on failure. */
582 lang_specific_pre_link (void) /* Not used for F77. */
587 /* Number of extra output files that lang_specific_pre_link may generate. */
588 int lang_specific_extra_outfiles = 0; /* Not used for F77. */