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
51 #include "coretypes.h"
59 #define MATH_LIBRARY "m"
62 #ifndef FORTRAN_LIBRARY
63 #define FORTRAN_LIBRARY "gfortran"
66 /* Name of the spec file. */
67 #define SPEC_FILE "libgfortran.spec"
69 /* The original argument list and related info is copied here. */
70 static unsigned int g77_xargc;
71 static const struct cl_decoded_option *g77_x_decoded_options;
72 static void append_arg (const struct cl_decoded_option *);
74 /* The new argument list will be built here. */
75 static unsigned int g77_newargc;
76 static struct cl_decoded_option *g77_new_decoded_options;
78 /* The path to the spec file. */
79 char *spec_file = NULL;
82 /* Return full path name of spec file if it is in DIR, or NULL if
85 find_spec_file (const char *dir)
87 const char dirsep_string[] = { DIR_SEPARATOR, '\0' };
91 spec = XNEWVEC (char, strlen (dir) + sizeof (SPEC_FILE) + 4);
93 strcat (spec, dirsep_string);
94 strcat (spec, SPEC_FILE);
95 if (!stat (spec, &sb))
102 /* Return whether strings S1 and S2 are both NULL or both the same
106 strings_same (const char *s1, const char *s2)
108 return s1 == s2 || (s1 != NULL && s2 != NULL && strcmp (s1, s2) == 0);
111 /* Return whether decoded option structures OPT1 and OPT2 are the
115 options_same (const struct cl_decoded_option *opt1,
116 const struct cl_decoded_option *opt2)
118 return (opt1->opt_index == opt2->opt_index
119 && strings_same (opt1->arg, opt2->arg)
120 && strings_same (opt1->orig_option_with_args_text,
121 opt2->orig_option_with_args_text)
122 && strings_same (opt1->canonical_option[0],
123 opt2->canonical_option[0])
124 && strings_same (opt1->canonical_option[1],
125 opt2->canonical_option[1])
126 && strings_same (opt1->canonical_option[2],
127 opt2->canonical_option[2])
128 && strings_same (opt1->canonical_option[3],
129 opt2->canonical_option[3])
130 && (opt1->canonical_option_num_elements
131 == opt2->canonical_option_num_elements)
132 && opt1->value == opt2->value
133 && opt1->errors == opt2->errors);
136 /* Append another argument to the list being built. As long as it is
137 identical to the corresponding arg in the original list, just increment
138 the new arg count. Otherwise allocate a new list, etc. */
141 append_arg (const struct cl_decoded_option *arg)
143 static unsigned int newargsize;
145 if (g77_new_decoded_options == g77_x_decoded_options
146 && g77_newargc < g77_xargc
147 && options_same (arg, &g77_x_decoded_options[g77_newargc]))
150 return; /* Nothing new here. */
153 if (g77_new_decoded_options == g77_x_decoded_options)
154 { /* Make new arglist. */
157 newargsize = (g77_xargc << 2) + 20; /* This should handle all. */
158 g77_new_decoded_options = XNEWVEC (struct cl_decoded_option, newargsize);
160 /* Copy what has been done so far. */
161 for (i = 0; i < g77_newargc; ++i)
162 g77_new_decoded_options[i] = g77_x_decoded_options[i];
165 if (g77_newargc == newargsize)
166 fatal_error ("overflowed output arg list for %qs",
167 arg->orig_option_with_args_text);
169 g77_new_decoded_options[g77_newargc++] = *arg;
172 /* Append an option described by OPT_INDEX, ARG and VALUE to the list
175 append_option (size_t opt_index, const char *arg, int value)
177 struct cl_decoded_option decoded;
179 generate_option (opt_index, arg, value, CL_DRIVER, &decoded);
180 append_arg (&decoded);
183 /* Append a libgfortran argument to the list being built. If
184 FORCE_STATIC, ensure the library is linked statically. */
187 add_arg_libgfortran (bool force_static ATTRIBUTE_UNUSED)
189 #ifdef HAVE_LD_STATIC_DYNAMIC
191 append_option (OPT_Wl_, "-Bstatic", 1);
193 append_option (OPT_l, FORTRAN_LIBRARY, 1);
194 #ifdef HAVE_LD_STATIC_DYNAMIC
196 append_option (OPT_Wl_, "-Bdynamic", 1);
201 lang_specific_driver (struct cl_decoded_option **in_decoded_options,
202 unsigned int *in_decoded_options_count,
203 int *in_added_libraries ATTRIBUTE_UNUSED)
205 unsigned int argc = *in_decoded_options_count;
206 struct cl_decoded_option *decoded_options = *in_decoded_options;
210 /* This will be NULL if we encounter a situation where we should not
212 const char *library = FORTRAN_LIBRARY;
214 /* 0 => -xnone in effect.
215 1 => -xfoo in effect. */
216 int saw_speclang = 0;
218 /* 0 => initial/reset state
219 1 => last arg was -l<library>
220 2 => last two args were -l<library> -lm. */
223 /* By default, we throw on the math library if we have one. */
224 int need_math = (MATH_LIBRARY[0] != '\0');
226 /* Whether we should link a static libgfortran. */
229 /* Whether we need to link statically. */
230 int static_linking = 0;
232 /* The number of input and output files in the incoming arg list. */
237 fprintf (stderr, "Incoming:");
238 for (i = 0; i < argc; i++)
239 fprintf (stderr, " %s", decoded_options[i].orig_option_with_args_text);
240 fprintf (stderr, "\n");
244 g77_x_decoded_options = decoded_options;
246 g77_new_decoded_options = decoded_options;
248 /* First pass through arglist.
250 If -nostdlib or a "turn-off-linking" option is anywhere in the
251 command line, don't do any library-option processing (except
254 for (i = 1; i < argc; ++i)
256 switch (decoded_options[i].opt_index)
258 case OPT_SPECIAL_input_file:
263 case OPT_nodefaultlibs:
266 case OPT_fsyntax_only:
268 /* These options disable linking entirely or linking of the
269 standard libraries. */
273 case OPT_static_libgfortran:
274 #ifdef HAVE_LD_STATIC_DYNAMIC
280 #ifdef HAVE_LD_STATIC_DYNAMIC
298 printf ("GNU Fortran %s%s\n", pkgversion_string, version_string);
299 printf ("Copyright %s 2010 Free Software Foundation, Inc.\n\n",
301 printf (_("GNU Fortran comes with NO WARRANTY, to the extent permitted by law.\n\
302 You may redistribute copies of GNU Fortran\n\
303 under the terms of the GNU General Public License.\n\
304 For more information about these matters, see the file named COPYING\n\n"));
309 /* Let gcc.c handle this, as it has a really
310 cool facility for handling --help and --verbose --help. */
315 spec_file = find_spec_file (decoded_options[i].arg);
324 if ((n_outfiles != 0) && (n_infiles == 0))
325 fatal_error ("no input files; unwilling to write output files");
327 /* If there are no input files, no need for the library. */
331 /* Second pass through arglist, transforming arguments as appropriate. */
333 append_arg (&decoded_options[0]); /* Start with command name, of course. */
335 for (i = 1; i < argc; ++i)
337 if (decoded_options[i].errors & CL_ERR_MISSING_ARG)
339 append_arg (&decoded_options[i]);
343 if (decoded_options[i].opt_index == OPT_SPECIAL_input_file
344 && decoded_options[i].arg[0] == '\0')
346 /* Interesting. Just append as is. */
347 append_arg (&decoded_options[i]);
351 if (decoded_options[i].opt_index != OPT_l
352 && (decoded_options[i].opt_index != OPT_SPECIAL_input_file
353 || strcmp (decoded_options[i].arg, "-") == 0))
355 /* Not a filename or library. */
357 if (saw_library == 1 && need_math) /* -l<library>. */
358 append_option (OPT_l, MATH_LIBRARY, 1);
362 if (decoded_options[i].opt_index == OPT_SPECIAL_input_file)
364 append_arg (&decoded_options[i]); /* "-" == Standard input. */
368 if (decoded_options[i].opt_index == OPT_x)
370 /* Track input language. */
371 const char *lang = decoded_options[i].arg;
373 saw_speclang = (strcmp (lang, "none") != 0);
376 append_arg (&decoded_options[i]);
381 /* A filename/library, not an option. */
384 saw_library = 0; /* -xfoo currently active. */
386 { /* -lfoo or filename. */
387 if (decoded_options[i].opt_index == OPT_l
388 && strcmp (decoded_options[i].arg, MATH_LIBRARY) == 0)
390 if (saw_library == 1)
391 saw_library = 2; /* -l<library> -lm. */
393 add_arg_libgfortran (static_lib && !static_linking);
395 else if (decoded_options[i].opt_index == OPT_l
396 && strcmp (decoded_options[i].arg, FORTRAN_LIBRARY) == 0)
398 saw_library = 1; /* -l<library>. */
399 add_arg_libgfortran (static_lib && !static_linking);
403 { /* Other library, or filename. */
404 if (saw_library == 1 && need_math)
405 append_option (OPT_l, MATH_LIBRARY, 1);
409 append_arg (&decoded_options[i]);
412 /* Append `-lgfortran -lm' as necessary. */
415 { /* Doing a link and no -nostdlib. */
417 append_option (OPT_x, "none", 1);
422 add_arg_libgfortran (static_lib && !static_linking);
427 append_option (OPT_l, MATH_LIBRARY, 1);
433 #ifdef ENABLE_SHARED_LIBGCC
438 for (i = 1; i < g77_newargc; i++)
439 if (g77_new_decoded_options[i].opt_index == OPT_static_libgcc
440 || g77_new_decoded_options[i].opt_index == OPT_static)
443 if (i == g77_newargc)
444 append_option (OPT_shared_libgcc, NULL, 1);
449 /* Read the specs file corresponding to libgfortran.
450 If we didn't find the spec file on the -L path, we load it
451 via lang_specific_pre_link. */
453 append_option (OPT_specs_, spec_file, 1);
455 if (verbose && g77_new_decoded_options != g77_x_decoded_options)
457 fprintf (stderr, _("Driving:"));
458 for (i = 0; i < g77_newargc; i++)
459 fprintf (stderr, " %s",
460 g77_new_decoded_options[i].orig_option_with_args_text);
461 fprintf (stderr, "\n");
464 *in_decoded_options_count = g77_newargc;
465 *in_decoded_options = g77_new_decoded_options;
469 /* Called before linking. Returns 0 on success and -1 on failure. */
471 lang_specific_pre_link (void)
476 do_spec ("%:include(libgfortran.spec)");
481 /* Number of extra output files that lang_specific_pre_link may generate. */
482 int lang_specific_extra_outfiles = 0; /* Not used for F77. */