2014-01-17 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / fortran / gfortranspec.c
bloba6296efbf4d7a6d9be5abf2ff9cf23c652d7e421
1 /* Specific flags and argument handling of the Fortran front-end.
2 Copyright (C) 1997-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
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 3, 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 GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file is copied more or less verbatim from g77. */
21 /* This file contains a filter for the main `gcc' driver, which is
22 replicated for the `gfortran' 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 95(gfortran), we do the following to the argument list
29 before passing it to `gcc':
31 1. Make sure `-lgfortran -lm' is at the end of the list.
33 2. Make sure each time `-lgfortran' or `-lm' is seen, it forms
34 part of the series `-lgfortran -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 "coretypes.h"
50 #include "gcc.h"
51 #include "opts.h"
53 #include "tm.h"
54 #include "intl.h"
56 #ifndef MATH_LIBRARY
57 #define MATH_LIBRARY "m"
58 #endif
60 #ifndef FORTRAN_LIBRARY
61 #define FORTRAN_LIBRARY "gfortran"
62 #endif
64 /* Name of the spec file. */
65 #define SPEC_FILE "libgfortran.spec"
67 /* The original argument list and related info is copied here. */
68 static unsigned int g77_xargc;
69 static const struct cl_decoded_option *g77_x_decoded_options;
70 static void append_arg (const struct cl_decoded_option *);
72 /* The new argument list will be built here. */
73 static unsigned int g77_newargc;
74 static struct cl_decoded_option *g77_new_decoded_options;
76 /* The path to the spec file. */
77 static char *spec_file = NULL;
79 /* This will be NULL if we encounter a situation where we should not
80 link in the fortran libraries. */
81 static const char *library = NULL;
84 /* Return full path name of spec file if it is in DIR, or NULL if
85 not. */
86 static char *
87 find_spec_file (const char *dir)
89 const char dirsep_string[] = { DIR_SEPARATOR, '\0' };
90 char *spec;
91 struct stat sb;
93 spec = XNEWVEC (char, strlen (dir) + sizeof (SPEC_FILE) + 4);
94 strcpy (spec, dir);
95 strcat (spec, dirsep_string);
96 strcat (spec, SPEC_FILE);
97 if (!stat (spec, &sb))
98 return spec;
99 free (spec);
100 return NULL;
104 /* Return whether strings S1 and S2 are both NULL or both the same
105 string. */
107 static bool
108 strings_same (const char *s1, const char *s2)
110 return s1 == s2 || (s1 != NULL && s2 != NULL && strcmp (s1, s2) == 0);
113 /* Return whether decoded option structures OPT1 and OPT2 are the
114 same. */
116 static bool
117 options_same (const struct cl_decoded_option *opt1,
118 const struct cl_decoded_option *opt2)
120 return (opt1->opt_index == opt2->opt_index
121 && strings_same (opt1->arg, opt2->arg)
122 && strings_same (opt1->orig_option_with_args_text,
123 opt2->orig_option_with_args_text)
124 && strings_same (opt1->canonical_option[0],
125 opt2->canonical_option[0])
126 && strings_same (opt1->canonical_option[1],
127 opt2->canonical_option[1])
128 && strings_same (opt1->canonical_option[2],
129 opt2->canonical_option[2])
130 && strings_same (opt1->canonical_option[3],
131 opt2->canonical_option[3])
132 && (opt1->canonical_option_num_elements
133 == opt2->canonical_option_num_elements)
134 && opt1->value == opt2->value
135 && opt1->errors == opt2->errors);
138 /* Append another argument to the list being built. As long as it is
139 identical to the corresponding arg in the original list, just increment
140 the new arg count. Otherwise allocate a new list, etc. */
142 static void
143 append_arg (const struct cl_decoded_option *arg)
145 static unsigned int newargsize;
147 if (g77_new_decoded_options == g77_x_decoded_options
148 && g77_newargc < g77_xargc
149 && options_same (arg, &g77_x_decoded_options[g77_newargc]))
151 ++g77_newargc;
152 return; /* Nothing new here. */
155 if (g77_new_decoded_options == g77_x_decoded_options)
156 { /* Make new arglist. */
157 unsigned int i;
159 newargsize = (g77_xargc << 2) + 20; /* This should handle all. */
160 g77_new_decoded_options = XNEWVEC (struct cl_decoded_option, newargsize);
162 /* Copy what has been done so far. */
163 for (i = 0; i < g77_newargc; ++i)
164 g77_new_decoded_options[i] = g77_x_decoded_options[i];
167 if (g77_newargc == newargsize)
168 fatal_error ("overflowed output arg list for %qs",
169 arg->orig_option_with_args_text);
171 g77_new_decoded_options[g77_newargc++] = *arg;
174 /* Append an option described by OPT_INDEX, ARG and VALUE to the list
175 being built. */
176 static void
177 append_option (size_t opt_index, const char *arg, int value)
179 struct cl_decoded_option decoded;
181 generate_option (opt_index, arg, value, CL_DRIVER, &decoded);
182 append_arg (&decoded);
185 /* Append a libgfortran argument to the list being built. If
186 FORCE_STATIC, ensure the library is linked statically. */
188 static void
189 add_arg_libgfortran (bool force_static ATTRIBUTE_UNUSED)
191 #ifdef HAVE_LD_STATIC_DYNAMIC
192 if (force_static)
193 append_option (OPT_Wl_, LD_STATIC_OPTION, 1);
194 #endif
195 append_option (OPT_l, FORTRAN_LIBRARY, 1);
196 #ifdef HAVE_LD_STATIC_DYNAMIC
197 if (force_static)
198 append_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1);
199 #endif
202 void
203 lang_specific_driver (struct cl_decoded_option **in_decoded_options,
204 unsigned int *in_decoded_options_count,
205 int *in_added_libraries ATTRIBUTE_UNUSED)
207 unsigned int argc = *in_decoded_options_count;
208 struct cl_decoded_option *decoded_options = *in_decoded_options;
209 unsigned int i;
210 int verbose = 0;
212 /* 0 => -xnone in effect.
213 1 => -xfoo in effect. */
214 int saw_speclang = 0;
216 /* 0 => initial/reset state
217 1 => last arg was -l<library>
218 2 => last two args were -l<library> -lm. */
219 int saw_library = 0;
221 /* By default, we throw on the math library if we have one. */
222 int need_math = (MATH_LIBRARY[0] != '\0');
224 /* Whether we should link a static libgfortran. */
225 int static_lib = 0;
227 /* Whether we need to link statically. */
228 int static_linking = 0;
230 /* The number of input and output files in the incoming arg list. */
231 int n_infiles = 0;
232 int n_outfiles = 0;
234 library = FORTRAN_LIBRARY;
236 #if 0
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");
241 #endif
243 g77_xargc = argc;
244 g77_x_decoded_options = decoded_options;
245 g77_newargc = 0;
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
252 relating to -x). */
254 for (i = 1; i < argc; ++i)
256 if (decoded_options[i].errors & CL_ERR_MISSING_ARG)
257 continue;
259 switch (decoded_options[i].opt_index)
261 case OPT_SPECIAL_input_file:
262 ++n_infiles;
263 continue;
265 case OPT_nostdlib:
266 case OPT_nodefaultlibs:
267 case OPT_c:
268 case OPT_S:
269 case OPT_fsyntax_only:
270 case OPT_E:
271 /* These options disable linking entirely or linking of the
272 standard libraries. */
273 library = 0;
274 break;
276 case OPT_static_libgfortran:
277 #ifdef HAVE_LD_STATIC_DYNAMIC
278 static_lib = 1;
279 #endif
280 break;
282 case OPT_static:
283 #ifdef HAVE_LD_STATIC_DYNAMIC
284 static_linking = 1;
285 #endif
286 break;
288 case OPT_l:
289 ++n_infiles;
290 break;
292 case OPT_o:
293 ++n_outfiles;
294 break;
296 case OPT_v:
297 verbose = 1;
298 break;
300 case OPT__version:
301 printf ("GNU Fortran %s%s\n", pkgversion_string, version_string);
302 printf ("Copyright %s 2014 Free Software Foundation, Inc.\n\n",
303 _("(C)"));
304 printf (_("GNU Fortran comes with NO WARRANTY, to the extent permitted by law.\n\
305 You may redistribute copies of GNU Fortran\n\
306 under the terms of the GNU General Public License.\n\
307 For more information about these matters, see the file named COPYING\n\n"));
308 exit (0);
309 break;
311 case OPT__help:
312 /* Let gcc.c handle this, as it has a really
313 cool facility for handling --help and --verbose --help. */
314 return;
316 case OPT_L:
317 if (!spec_file)
318 spec_file = find_spec_file (decoded_options[i].arg);
319 break;
322 default:
323 break;
327 if ((n_outfiles != 0) && (n_infiles == 0))
328 fatal_error ("no input files; unwilling to write output files");
330 /* If there are no input files, no need for the library. */
331 if (n_infiles == 0)
332 library = 0;
334 /* Second pass through arglist, transforming arguments as appropriate. */
336 append_arg (&decoded_options[0]); /* Start with command name, of course. */
338 for (i = 1; i < argc; ++i)
340 if (decoded_options[i].errors & CL_ERR_MISSING_ARG)
342 append_arg (&decoded_options[i]);
343 continue;
346 if (decoded_options[i].opt_index == OPT_SPECIAL_input_file
347 && decoded_options[i].arg[0] == '\0')
349 /* Interesting. Just append as is. */
350 append_arg (&decoded_options[i]);
351 continue;
354 if (decoded_options[i].opt_index != OPT_l
355 && (decoded_options[i].opt_index != OPT_SPECIAL_input_file
356 || strcmp (decoded_options[i].arg, "-") == 0))
358 /* Not a filename or library. */
360 if (saw_library == 1 && need_math) /* -l<library>. */
361 append_option (OPT_l, MATH_LIBRARY, 1);
363 saw_library = 0;
365 if (decoded_options[i].opt_index == OPT_SPECIAL_input_file)
367 append_arg (&decoded_options[i]); /* "-" == Standard input. */
368 continue;
371 if (decoded_options[i].opt_index == OPT_x)
373 /* Track input language. */
374 const char *lang = decoded_options[i].arg;
376 saw_speclang = (strcmp (lang, "none") != 0);
379 append_arg (&decoded_options[i]);
381 continue;
384 /* A filename/library, not an option. */
386 if (saw_speclang)
387 saw_library = 0; /* -xfoo currently active. */
388 else
389 { /* -lfoo or filename. */
390 if (decoded_options[i].opt_index == OPT_l
391 && strcmp (decoded_options[i].arg, MATH_LIBRARY) == 0)
393 if (saw_library == 1)
394 saw_library = 2; /* -l<library> -lm. */
395 else
396 add_arg_libgfortran (static_lib && !static_linking);
398 else if (decoded_options[i].opt_index == OPT_l
399 && strcmp (decoded_options[i].arg, FORTRAN_LIBRARY) == 0)
401 saw_library = 1; /* -l<library>. */
402 add_arg_libgfortran (static_lib && !static_linking);
403 continue;
405 else
406 { /* Other library, or filename. */
407 if (saw_library == 1 && need_math)
408 append_option (OPT_l, MATH_LIBRARY, 1);
409 saw_library = 0;
412 append_arg (&decoded_options[i]);
415 /* Append `-lgfortran -lm' as necessary. */
417 if (library)
418 { /* Doing a link and no -nostdlib. */
419 if (saw_speclang)
420 append_option (OPT_x, "none", 1);
422 switch (saw_library)
424 case 0:
425 add_arg_libgfortran (static_lib && !static_linking);
426 /* Fall through. */
428 case 1:
429 if (need_math)
430 append_option (OPT_l, MATH_LIBRARY, 1);
431 default:
432 break;
436 #ifdef ENABLE_SHARED_LIBGCC
437 if (library)
439 unsigned int i;
441 for (i = 1; i < g77_newargc; i++)
442 if (g77_new_decoded_options[i].opt_index == OPT_static_libgcc
443 || g77_new_decoded_options[i].opt_index == OPT_static)
444 break;
446 if (i == g77_newargc)
447 append_option (OPT_shared_libgcc, NULL, 1);
450 #endif
452 /* Read the specs file corresponding to libgfortran.
453 If we didn't find the spec file on the -L path, we load it
454 via lang_specific_pre_link. */
455 if (spec_file)
456 append_option (OPT_specs_, spec_file, 1);
458 if (verbose && g77_new_decoded_options != g77_x_decoded_options)
460 fprintf (stderr, _("Driving:"));
461 for (i = 0; i < g77_newargc; i++)
462 fprintf (stderr, " %s",
463 g77_new_decoded_options[i].orig_option_with_args_text);
464 fprintf (stderr, "\n");
467 *in_decoded_options_count = g77_newargc;
468 *in_decoded_options = g77_new_decoded_options;
472 /* Called before linking. Returns 0 on success and -1 on failure. */
474 lang_specific_pre_link (void)
476 free (spec_file);
477 if (spec_file == NULL && library)
478 do_spec ("%:include(libgfortran.spec)");
480 return 0;
483 /* Number of extra output files that lang_specific_pre_link may generate. */
484 int lang_specific_extra_outfiles = 0; /* Not used for F77. */