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 /* 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
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
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. */
114 append_arg (const struct cl_decoded_option
*arg
)
116 static unsigned int newargsize
;
119 fprintf (stderr
, "`%s'\n", arg
);
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
]))
127 return; /* Nothing new here. */
130 if (g77_new_decoded_options
== g77_x_decoded_options
)
131 { /* Make new arglist. */
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
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. */
164 add_arg_libgfortran (bool force_static ATTRIBUTE_UNUSED
)
166 #ifdef HAVE_LD_STATIC_DYNAMIC
168 append_option (OPT_Wl_
, "-Bstatic", 1);
170 append_option (OPT_l
, FORTRAN_LIBRARY
, 1);
171 #ifdef HAVE_LD_STATIC_DYNAMIC
173 append_option (OPT_Wl_
, "-Bdynamic", 1);
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
;
187 /* This will be NULL if we encounter a situation where we should not
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. */
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. */
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. */
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");
221 g77_x_decoded_options
= decoded_options
;
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
231 for (i
= 1; i
< argc
; ++i
)
233 switch (decoded_options
[i
].opt_index
)
235 case OPT_SPECIAL_input_file
:
240 case OPT_nodefaultlibs
:
243 case OPT_fsyntax_only
:
245 /* These options disable linking entirely or linking of the
246 standard libraries. */
250 case OPT_static_libgfortran
:
251 #ifdef HAVE_LD_STATIC_DYNAMIC
257 #ifdef HAVE_LD_STATIC_DYNAMIC
275 printf ("GNU Fortran %s%s\n", pkgversion_string
, version_string
);
276 printf ("Copyright %s 2010 Free Software Foundation, Inc.\n\n",
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"));
286 /* Let gcc.c handle this, as it has a really
287 cool facility for handling --help and --verbose --help. */
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. */
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
]);
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
]);
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);
333 if (decoded_options
[i
].opt_index
== OPT_SPECIAL_input_file
)
335 append_arg (&decoded_options
[i
]); /* "-" == Standard input. */
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
]);
352 /* A filename/library, not an option. */
355 saw_library
= 0; /* -xfoo currently active. */
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. */
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
);
374 { /* Other library, or filename. */
375 if (saw_library
== 1 && need_math
)
376 append_option (OPT_l
, MATH_LIBRARY
, 1);
380 append_arg (&decoded_options
[i
]);
383 /* Append `-lgfortran -lm' as necessary. */
386 { /* Doing a link and no -nostdlib. */
388 append_option (OPT_x
, "none", 1);
393 add_arg_libgfortran (static_lib
&& !static_linking
);
398 append_option (OPT_l
, MATH_LIBRARY
, 1);
404 #ifdef ENABLE_SHARED_LIBGCC
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
)
414 if (i
== g77_newargc
)
415 append_option (OPT_shared_libgcc
, NULL
, 1);
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. */
441 /* Number of extra output files that lang_specific_pre_link may generate. */
442 int lang_specific_extra_outfiles
= 0; /* Not used for F77. */