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, 2011, 2012
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 static char *spec_file
= NULL
;
81 /* This will be NULL if we encounter a situation where we should not
82 link in the fortran libraries. */
83 static const char *library
= NULL
;
86 /* Return full path name of spec file if it is in DIR, or NULL if
89 find_spec_file (const char *dir
)
91 const char dirsep_string
[] = { DIR_SEPARATOR
, '\0' };
95 spec
= XNEWVEC (char, strlen (dir
) + sizeof (SPEC_FILE
) + 4);
97 strcat (spec
, dirsep_string
);
98 strcat (spec
, SPEC_FILE
);
99 if (!stat (spec
, &sb
))
106 /* Return whether strings S1 and S2 are both NULL or both the same
110 strings_same (const char *s1
, const char *s2
)
112 return s1
== s2
|| (s1
!= NULL
&& s2
!= NULL
&& strcmp (s1
, s2
) == 0);
115 /* Return whether decoded option structures OPT1 and OPT2 are the
119 options_same (const struct cl_decoded_option
*opt1
,
120 const struct cl_decoded_option
*opt2
)
122 return (opt1
->opt_index
== opt2
->opt_index
123 && strings_same (opt1
->arg
, opt2
->arg
)
124 && strings_same (opt1
->orig_option_with_args_text
,
125 opt2
->orig_option_with_args_text
)
126 && strings_same (opt1
->canonical_option
[0],
127 opt2
->canonical_option
[0])
128 && strings_same (opt1
->canonical_option
[1],
129 opt2
->canonical_option
[1])
130 && strings_same (opt1
->canonical_option
[2],
131 opt2
->canonical_option
[2])
132 && strings_same (opt1
->canonical_option
[3],
133 opt2
->canonical_option
[3])
134 && (opt1
->canonical_option_num_elements
135 == opt2
->canonical_option_num_elements
)
136 && opt1
->value
== opt2
->value
137 && opt1
->errors
== opt2
->errors
);
140 /* Append another argument to the list being built. As long as it is
141 identical to the corresponding arg in the original list, just increment
142 the new arg count. Otherwise allocate a new list, etc. */
145 append_arg (const struct cl_decoded_option
*arg
)
147 static unsigned int newargsize
;
149 if (g77_new_decoded_options
== g77_x_decoded_options
150 && g77_newargc
< g77_xargc
151 && options_same (arg
, &g77_x_decoded_options
[g77_newargc
]))
154 return; /* Nothing new here. */
157 if (g77_new_decoded_options
== g77_x_decoded_options
)
158 { /* Make new arglist. */
161 newargsize
= (g77_xargc
<< 2) + 20; /* This should handle all. */
162 g77_new_decoded_options
= XNEWVEC (struct cl_decoded_option
, newargsize
);
164 /* Copy what has been done so far. */
165 for (i
= 0; i
< g77_newargc
; ++i
)
166 g77_new_decoded_options
[i
] = g77_x_decoded_options
[i
];
169 if (g77_newargc
== newargsize
)
170 fatal_error ("overflowed output arg list for %qs",
171 arg
->orig_option_with_args_text
);
173 g77_new_decoded_options
[g77_newargc
++] = *arg
;
176 /* Append an option described by OPT_INDEX, ARG and VALUE to the list
179 append_option (size_t opt_index
, const char *arg
, int value
)
181 struct cl_decoded_option decoded
;
183 generate_option (opt_index
, arg
, value
, CL_DRIVER
, &decoded
);
184 append_arg (&decoded
);
187 /* Append a libgfortran argument to the list being built. If
188 FORCE_STATIC, ensure the library is linked statically. */
191 add_arg_libgfortran (bool force_static ATTRIBUTE_UNUSED
)
193 #ifdef HAVE_LD_STATIC_DYNAMIC
195 append_option (OPT_Wl_
, LD_STATIC_OPTION
, 1);
197 append_option (OPT_l
, FORTRAN_LIBRARY
, 1);
198 #ifdef HAVE_LD_STATIC_DYNAMIC
200 append_option (OPT_Wl_
, LD_DYNAMIC_OPTION
, 1);
205 lang_specific_driver (struct cl_decoded_option
**in_decoded_options
,
206 unsigned int *in_decoded_options_count
,
207 int *in_added_libraries ATTRIBUTE_UNUSED
)
209 unsigned int argc
= *in_decoded_options_count
;
210 struct cl_decoded_option
*decoded_options
= *in_decoded_options
;
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. */
236 library
= FORTRAN_LIBRARY
;
239 fprintf (stderr
, "Incoming:");
240 for (i
= 0; i
< argc
; i
++)
241 fprintf (stderr
, " %s", decoded_options
[i
].orig_option_with_args_text
);
242 fprintf (stderr
, "\n");
246 g77_x_decoded_options
= decoded_options
;
248 g77_new_decoded_options
= decoded_options
;
250 /* First pass through arglist.
252 If -nostdlib or a "turn-off-linking" option is anywhere in the
253 command line, don't do any library-option processing (except
256 for (i
= 1; i
< argc
; ++i
)
258 if (decoded_options
[i
].errors
& CL_ERR_MISSING_ARG
)
261 switch (decoded_options
[i
].opt_index
)
263 case OPT_SPECIAL_input_file
:
268 case OPT_nodefaultlibs
:
271 case OPT_fsyntax_only
:
273 /* These options disable linking entirely or linking of the
274 standard libraries. */
278 case OPT_static_libgfortran
:
279 #ifdef HAVE_LD_STATIC_DYNAMIC
285 #ifdef HAVE_LD_STATIC_DYNAMIC
303 printf ("GNU Fortran %s%s\n", pkgversion_string
, version_string
);
304 printf ("Copyright %s 2012 Free Software Foundation, Inc.\n\n",
306 printf (_("GNU Fortran comes with NO WARRANTY, to the extent permitted by law.\n\
307 You may redistribute copies of GNU Fortran\n\
308 under the terms of the GNU General Public License.\n\
309 For more information about these matters, see the file named COPYING\n\n"));
314 /* Let gcc.c handle this, as it has a really
315 cool facility for handling --help and --verbose --help. */
320 spec_file
= find_spec_file (decoded_options
[i
].arg
);
329 if ((n_outfiles
!= 0) && (n_infiles
== 0))
330 fatal_error ("no input files; unwilling to write output files");
332 /* If there are no input files, no need for the library. */
336 /* Second pass through arglist, transforming arguments as appropriate. */
338 append_arg (&decoded_options
[0]); /* Start with command name, of course. */
340 for (i
= 1; i
< argc
; ++i
)
342 if (decoded_options
[i
].errors
& CL_ERR_MISSING_ARG
)
344 append_arg (&decoded_options
[i
]);
348 if (decoded_options
[i
].opt_index
== OPT_SPECIAL_input_file
349 && decoded_options
[i
].arg
[0] == '\0')
351 /* Interesting. Just append as is. */
352 append_arg (&decoded_options
[i
]);
356 if (decoded_options
[i
].opt_index
!= OPT_l
357 && (decoded_options
[i
].opt_index
!= OPT_SPECIAL_input_file
358 || strcmp (decoded_options
[i
].arg
, "-") == 0))
360 /* Not a filename or library. */
362 if (saw_library
== 1 && need_math
) /* -l<library>. */
363 append_option (OPT_l
, MATH_LIBRARY
, 1);
367 if (decoded_options
[i
].opt_index
== OPT_SPECIAL_input_file
)
369 append_arg (&decoded_options
[i
]); /* "-" == Standard input. */
373 if (decoded_options
[i
].opt_index
== OPT_x
)
375 /* Track input language. */
376 const char *lang
= decoded_options
[i
].arg
;
378 saw_speclang
= (strcmp (lang
, "none") != 0);
381 append_arg (&decoded_options
[i
]);
386 /* A filename/library, not an option. */
389 saw_library
= 0; /* -xfoo currently active. */
391 { /* -lfoo or filename. */
392 if (decoded_options
[i
].opt_index
== OPT_l
393 && strcmp (decoded_options
[i
].arg
, MATH_LIBRARY
) == 0)
395 if (saw_library
== 1)
396 saw_library
= 2; /* -l<library> -lm. */
398 add_arg_libgfortran (static_lib
&& !static_linking
);
400 else if (decoded_options
[i
].opt_index
== OPT_l
401 && strcmp (decoded_options
[i
].arg
, FORTRAN_LIBRARY
) == 0)
403 saw_library
= 1; /* -l<library>. */
404 add_arg_libgfortran (static_lib
&& !static_linking
);
408 { /* Other library, or filename. */
409 if (saw_library
== 1 && need_math
)
410 append_option (OPT_l
, MATH_LIBRARY
, 1);
414 append_arg (&decoded_options
[i
]);
417 /* Append `-lgfortran -lm' as necessary. */
420 { /* Doing a link and no -nostdlib. */
422 append_option (OPT_x
, "none", 1);
427 add_arg_libgfortran (static_lib
&& !static_linking
);
432 append_option (OPT_l
, MATH_LIBRARY
, 1);
438 #ifdef ENABLE_SHARED_LIBGCC
443 for (i
= 1; i
< g77_newargc
; i
++)
444 if (g77_new_decoded_options
[i
].opt_index
== OPT_static_libgcc
445 || g77_new_decoded_options
[i
].opt_index
== OPT_static
)
448 if (i
== g77_newargc
)
449 append_option (OPT_shared_libgcc
, NULL
, 1);
454 /* Read the specs file corresponding to libgfortran.
455 If we didn't find the spec file on the -L path, we load it
456 via lang_specific_pre_link. */
458 append_option (OPT_specs_
, spec_file
, 1);
460 if (verbose
&& g77_new_decoded_options
!= g77_x_decoded_options
)
462 fprintf (stderr
, _("Driving:"));
463 for (i
= 0; i
< g77_newargc
; i
++)
464 fprintf (stderr
, " %s",
465 g77_new_decoded_options
[i
].orig_option_with_args_text
);
466 fprintf (stderr
, "\n");
469 *in_decoded_options_count
= g77_newargc
;
470 *in_decoded_options
= g77_new_decoded_options
;
474 /* Called before linking. Returns 0 on success and -1 on failure. */
476 lang_specific_pre_link (void)
479 if (spec_file
== NULL
&& library
)
480 do_spec ("%:include(libgfortran.spec)");
485 /* Number of extra output files that lang_specific_pre_link may generate. */
486 int lang_specific_extra_outfiles
= 0; /* Not used for F77. */