Merge from mainline (163495:164578).
[official-gcc/graphite-test-results.git] / gcc / fortran / gfortranspec.c
blobb79f6a25706856c40105b581df7f90a8abccd4cd
1 /* Specific flags and argument handling of the Fortran front-end.
2 Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2007, 2008, 2009, 2010
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)
11 any later version.
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
47 `gcc' itself. */
49 #include "config.h"
50 #include "system.h"
51 #include "coretypes.h"
52 #include "gcc.h"
53 #include "opts.h"
55 #include "tm.h"
56 #include "intl.h"
58 #ifndef MATH_LIBRARY
59 #define MATH_LIBRARY "m"
60 #endif
62 #ifndef FORTRAN_LIBRARY
63 #define FORTRAN_LIBRARY "gfortran"
64 #endif
66 /* The original argument list and related info is copied here. */
67 static unsigned int g77_xargc;
68 static const struct cl_decoded_option *g77_x_decoded_options;
69 static void append_arg (const struct cl_decoded_option *);
71 /* The new argument list will be built here. */
72 static unsigned int g77_newargc;
73 static struct cl_decoded_option *g77_new_decoded_options;
75 /* Return whether strings S1 and S2 are both NULL or both the same
76 string. */
78 static bool
79 strings_same (const char *s1, const char *s2)
81 return s1 == s2 || (s1 != NULL && s2 != NULL && strcmp (s1, s2) == 0);
84 /* Return whether decoded option structures OPT1 and OPT2 are the
85 same. */
87 static bool
88 options_same (const struct cl_decoded_option *opt1,
89 const struct cl_decoded_option *opt2)
91 return (opt1->opt_index == opt2->opt_index
92 && strings_same (opt1->arg, opt2->arg)
93 && strings_same (opt1->orig_option_with_args_text,
94 opt2->orig_option_with_args_text)
95 && strings_same (opt1->canonical_option[0],
96 opt2->canonical_option[0])
97 && strings_same (opt1->canonical_option[1],
98 opt2->canonical_option[1])
99 && strings_same (opt1->canonical_option[2],
100 opt2->canonical_option[2])
101 && strings_same (opt1->canonical_option[3],
102 opt2->canonical_option[3])
103 && (opt1->canonical_option_num_elements
104 == opt2->canonical_option_num_elements)
105 && opt1->value == opt2->value
106 && opt1->errors == opt2->errors);
109 /* Append another argument to the list being built. As long as it is
110 identical to the corresponding arg in the original list, just increment
111 the new arg count. Otherwise allocate a new list, etc. */
113 static void
114 append_arg (const struct cl_decoded_option *arg)
116 static unsigned int newargsize;
118 #if 0
119 fprintf (stderr, "`%s'\n", arg);
120 #endif
122 if (g77_new_decoded_options == g77_x_decoded_options
123 && g77_newargc < g77_xargc
124 && options_same (arg, &g77_x_decoded_options[g77_newargc]))
126 ++g77_newargc;
127 return; /* Nothing new here. */
130 if (g77_new_decoded_options == g77_x_decoded_options)
131 { /* Make new arglist. */
132 unsigned int i;
134 newargsize = (g77_xargc << 2) + 20; /* This should handle all. */
135 g77_new_decoded_options = XNEWVEC (struct cl_decoded_option, newargsize);
137 /* Copy what has been done so far. */
138 for (i = 0; i < g77_newargc; ++i)
139 g77_new_decoded_options[i] = g77_x_decoded_options[i];
142 if (g77_newargc == newargsize)
143 fatal_error ("overflowed output arg list for %qs",
144 arg->orig_option_with_args_text);
146 g77_new_decoded_options[g77_newargc++] = *arg;
149 /* Append an option described by OPT_INDEX, ARG and VALUE to the list
150 being built. */
151 static void
152 append_option (size_t opt_index, const char *arg, int value)
154 struct cl_decoded_option decoded;
156 generate_option (opt_index, arg, value, CL_DRIVER, &decoded);
157 append_arg (&decoded);
160 /* Append a libgfortran argument to the list being built. If
161 FORCE_STATIC, ensure the library is linked statically. */
163 static void
164 add_arg_libgfortran (bool force_static ATTRIBUTE_UNUSED)
166 #ifdef HAVE_LD_STATIC_DYNAMIC
167 if (force_static)
168 append_option (OPT_Wl_, "-Bstatic", 1);
169 #endif
170 append_option (OPT_l, FORTRAN_LIBRARY, 1);
171 #ifdef HAVE_LD_STATIC_DYNAMIC
172 if (force_static)
173 append_option (OPT_Wl_, "-Bdynamic", 1);
174 #endif
177 void
178 lang_specific_driver (struct cl_decoded_option **in_decoded_options,
179 unsigned int *in_decoded_options_count,
180 int *in_added_libraries ATTRIBUTE_UNUSED)
182 unsigned int argc = *in_decoded_options_count;
183 struct cl_decoded_option *decoded_options = *in_decoded_options;
184 unsigned int i;
185 int verbose = 0;
187 /* This will be NULL if we encounter a situation where we should not
188 link in libf2c. */
189 const char *library = FORTRAN_LIBRARY;
191 /* 0 => -xnone in effect.
192 1 => -xfoo in effect. */
193 int saw_speclang = 0;
195 /* 0 => initial/reset state
196 1 => last arg was -l<library>
197 2 => last two args were -l<library> -lm. */
198 int saw_library = 0;
200 /* By default, we throw on the math library if we have one. */
201 int need_math = (MATH_LIBRARY[0] != '\0');
203 /* Whether we should link a static libgfortran. */
204 int static_lib = 0;
206 /* Whether we need to link statically. */
207 int static_linking = 0;
209 /* The number of input and output files in the incoming arg list. */
210 int n_infiles = 0;
211 int n_outfiles = 0;
213 #if 0
214 fprintf (stderr, "Incoming:");
215 for (i = 0; i < argc; i++)
216 fprintf (stderr, " %s", decoded_options[i].orig_option_with_args_text);
217 fprintf (stderr, "\n");
218 #endif
220 g77_xargc = argc;
221 g77_x_decoded_options = decoded_options;
222 g77_newargc = 0;
223 g77_new_decoded_options = decoded_options;
225 /* First pass through arglist.
227 If -nostdlib or a "turn-off-linking" option is anywhere in the
228 command line, don't do any library-option processing (except
229 relating to -x). */
231 for (i = 1; i < argc; ++i)
233 switch (decoded_options[i].opt_index)
235 case OPT_SPECIAL_input_file:
236 ++n_infiles;
237 continue;
239 case OPT_nostdlib:
240 case OPT_nodefaultlibs:
241 case OPT_c:
242 case OPT_S:
243 case OPT_fsyntax_only:
244 case OPT_E:
245 /* These options disable linking entirely or linking of the
246 standard libraries. */
247 library = 0;
248 break;
250 case OPT_static_libgfortran:
251 #ifdef HAVE_LD_STATIC_DYNAMIC
252 static_lib = 1;
253 #endif
254 break;
256 case OPT_static:
257 #ifdef HAVE_LD_STATIC_DYNAMIC
258 static_linking = 1;
259 #endif
260 break;
262 case OPT_l:
263 ++n_infiles;
264 break;
266 case OPT_o:
267 ++n_outfiles;
268 break;
270 case OPT_v:
271 verbose = 1;
272 break;
274 case OPT__version:
275 printf ("GNU Fortran %s%s\n", pkgversion_string, version_string);
276 printf ("Copyright %s 2010 Free Software Foundation, Inc.\n\n",
277 _("(C)"));
278 printf (_("GNU Fortran comes with NO WARRANTY, to the extent permitted by law.\n\
279 You may redistribute copies of GNU Fortran\n\
280 under the terms of the GNU General Public License.\n\
281 For more information about these matters, see the file named COPYING\n\n"));
282 exit (0);
283 break;
285 case OPT__help:
286 /* Let gcc.c handle this, as it has a really
287 cool facility for handling --help and --verbose --help. */
288 return;
290 default:
291 break;
295 if ((n_outfiles != 0) && (n_infiles == 0))
296 fatal_error ("no input files; unwilling to write output files");
298 /* If there are no input files, no need for the library. */
299 if (n_infiles == 0)
300 library = 0;
302 /* Second pass through arglist, transforming arguments as appropriate. */
304 append_arg (&decoded_options[0]); /* Start with command name, of course. */
306 for (i = 1; i < argc; ++i)
308 if (decoded_options[i].errors & CL_ERR_MISSING_ARG)
310 append_arg (&decoded_options[i]);
311 continue;
314 if (decoded_options[i].opt_index == OPT_SPECIAL_input_file
315 && decoded_options[i].arg[0] == '\0')
317 /* Interesting. Just append as is. */
318 append_arg (&decoded_options[i]);
319 continue;
322 if (decoded_options[i].opt_index != OPT_l
323 && (decoded_options[i].opt_index != OPT_SPECIAL_input_file
324 || strcmp (decoded_options[i].arg, "-") == 0))
326 /* Not a filename or library. */
328 if (saw_library == 1 && need_math) /* -l<library>. */
329 append_option (OPT_l, MATH_LIBRARY, 1);
331 saw_library = 0;
333 if (decoded_options[i].opt_index == OPT_SPECIAL_input_file)
335 append_arg (&decoded_options[i]); /* "-" == Standard input. */
336 continue;
339 if (decoded_options[i].opt_index == OPT_x)
341 /* Track input language. */
342 const char *lang = decoded_options[i].arg;
344 saw_speclang = (strcmp (lang, "none") != 0);
347 append_arg (&decoded_options[i]);
349 continue;
352 /* A filename/library, not an option. */
354 if (saw_speclang)
355 saw_library = 0; /* -xfoo currently active. */
356 else
357 { /* -lfoo or filename. */
358 if (decoded_options[i].opt_index == OPT_l
359 && strcmp (decoded_options[i].arg, MATH_LIBRARY) == 0)
361 if (saw_library == 1)
362 saw_library = 2; /* -l<library> -lm. */
363 else
364 add_arg_libgfortran (static_lib && !static_linking);
366 else if (decoded_options[i].opt_index == OPT_l
367 && strcmp (decoded_options[i].arg, FORTRAN_LIBRARY) == 0)
369 saw_library = 1; /* -l<library>. */
370 add_arg_libgfortran (static_lib && !static_linking);
371 continue;
373 else
374 { /* Other library, or filename. */
375 if (saw_library == 1 && need_math)
376 append_option (OPT_l, MATH_LIBRARY, 1);
377 saw_library = 0;
380 append_arg (&decoded_options[i]);
383 /* Append `-lgfortran -lm' as necessary. */
385 if (library)
386 { /* Doing a link and no -nostdlib. */
387 if (saw_speclang)
388 append_option (OPT_x, "none", 1);
390 switch (saw_library)
392 case 0:
393 add_arg_libgfortran (static_lib && !static_linking);
394 /* Fall through. */
396 case 1:
397 if (need_math)
398 append_option (OPT_l, MATH_LIBRARY, 1);
399 default:
400 break;
404 #ifdef ENABLE_SHARED_LIBGCC
405 if (library)
407 unsigned int i;
409 for (i = 1; i < g77_newargc; i++)
410 if (g77_new_decoded_options[i].opt_index == OPT_static_libgcc
411 || g77_new_decoded_options[i].opt_index == OPT_static)
412 break;
414 if (i == g77_newargc)
415 append_option (OPT_shared_libgcc, NULL, 1);
418 #endif
420 if (verbose && g77_new_decoded_options != g77_x_decoded_options)
422 fprintf (stderr, _("Driving:"));
423 for (i = 0; i < g77_newargc; i++)
424 fprintf (stderr, " %s",
425 g77_new_decoded_options[i].orig_option_with_args_text);
426 fprintf (stderr, "\n");
429 *in_decoded_options_count = g77_newargc;
430 *in_decoded_options = g77_new_decoded_options;
434 /* Called before linking. Returns 0 on success and -1 on failure. */
436 lang_specific_pre_link (void) /* Not used for F77. */
438 return 0;
441 /* Number of extra output files that lang_specific_pre_link may generate. */
442 int lang_specific_extra_outfiles = 0; /* Not used for F77. */