1 /* Specific flags and argument handling of the Fortran front-end.
2 Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21 /* This file is copied more or less verbatim from g77. */
22 /* This file contains a filter for the main `gcc' driver, which is
23 replicated for the `gfortran' driver by adding this filter. The purpose
24 of this filter is to be basically identical to gcc (in that
25 it faithfully passes all of the original arguments to gcc) but,
26 unless explicitly overridden by the user in certain ways, ensure
27 that the needs of the language supported by this wrapper are met.
29 For GNU Fortran 95(gfortran), we do the following to the argument list
30 before passing it to `gcc':
32 1. Make sure `-lgfortran -lm' is at the end of the list.
34 2. Make sure each time `-lgfortran' or `-lm' is seen, it forms
35 part of the series `-lgfortran -lm'.
37 #1 and #2 are not done if `-nostdlib' or any option that disables
38 the linking phase is present, or if `-xfoo' is in effect. Note that
39 a lack of source files or -l options disables linking.
41 This program was originally made out of gcc/cp/g++spec.c, but the
42 way it builds the new argument list was rewritten so it is much
43 easier to maintain, improve the way it decides to add or not add
44 extra arguments, etc. And several improvements were made in the
45 handling of arguments, primarily to make it more consistent with
52 #include "coretypes.h"
57 #define MATH_LIBRARY "-lm"
61 #define FORTRAN_INIT "-lgfortranbegin"
64 #ifndef FORTRAN_LIBRARY
65 #define FORTRAN_LIBRARY "-lgfortran"
68 /* Options this driver needs to recognize, not just know how to
72 OPTION_b
, /* Aka --prefix. */
73 OPTION_B
, /* Aka --target. */
74 OPTION_c
, /* Aka --compile. */
75 OPTION_E
, /* Aka --preprocess. */
76 OPTION_help
, /* --help. */
77 OPTION_i
, /* -imacros, -include, -include-*. */
79 OPTION_L
, /* Aka --library-directory. */
80 OPTION_nostdlib
, /* Aka --no-standard-libraries, or
82 OPTION_o
, /* Aka --output. */
83 OPTION_S
, /* Aka --assemble. */
84 OPTION_syntax_only
, /* -fsyntax-only. */
85 OPTION_v
, /* Aka --verbose. */
86 OPTION_version
, /* --version. */
87 OPTION_V
, /* Aka --use-version. */
88 OPTION_x
, /* Aka --language. */
89 OPTION_
/* Unrecognized or unimportant. */
93 /* The original argument list and related info is copied here. */
95 static const char *const *g77_xargv
;
96 static void lookup_option (Option
*, int *, const char **, const char *);
97 static void append_arg (const char *);
99 /* The new argument list will be built here. */
100 static int g77_newargc
;
101 static const char **g77_newargv
;
103 /* --- This comes from gcc.c (2.8.1) verbatim: */
105 /* This defines which switch letters take arguments. */
107 #ifndef SWITCH_TAKES_ARG
108 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
111 /* This defines which multi-letter switches take arguments. */
113 #ifndef WORD_SWITCH_TAKES_ARG
114 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
117 /* --- End of verbatim. */
119 /* Assumes text[0] == '-'. Returns number of argv items that belong to
120 (and follow) this one, an option id for options important to the
121 caller, and a pointer to the first char of the arg, if embedded (else
122 returns NULL, meaning no arg or it's the next argv).
124 Note that this also assumes gcc.c's pass converting long options
125 to short ones, where available, has already been run. */
128 lookup_option (Option
*xopt
, int *xskip
, const char **xarg
, const char *text
)
130 Option opt
= OPTION_
;
132 const char *arg
= NULL
;
134 if ((skip
= SWITCH_TAKES_ARG (text
[1])))
135 skip
-= (text
[2] != '\0'); /* See gcc.c. */
138 opt
= OPTION_B
, skip
= (text
[2] == '\0'), arg
= text
+ 2;
139 else if (text
[1] == 'b')
140 opt
= OPTION_b
, skip
= (text
[2] == '\0'), arg
= text
+ 2;
141 else if ((text
[1] == 'c') && (text
[2] == '\0'))
142 opt
= OPTION_c
, skip
= 0;
143 else if ((text
[1] == 'E') && (text
[2] == '\0'))
144 opt
= OPTION_E
, skip
= 0;
145 else if (text
[1] == 'i')
146 opt
= OPTION_i
, skip
= 0;
147 else if (text
[1] == 'l')
149 else if (text
[1] == 'L')
150 opt
= OPTION_L
, arg
= text
+ 2;
151 else if (text
[1] == 'o')
153 else if ((text
[1] == 'S') && (text
[2] == '\0'))
154 opt
= OPTION_S
, skip
= 0;
155 else if (text
[1] == 'V')
156 opt
= OPTION_V
, skip
= (text
[2] == '\0');
157 else if ((text
[1] == 'v') && (text
[2] == '\0'))
158 opt
= OPTION_v
, skip
= 0;
159 else if (text
[1] == 'x')
160 opt
= OPTION_x
, arg
= text
+ 2;
163 if ((skip
= WORD_SWITCH_TAKES_ARG (text
+ 1)) != 0) /* See gcc.c. */
165 else if (!strcmp (text
, "-fhelp")) /* Really --help!! */
167 else if (!strcmp (text
, "-nostdlib")
168 || !strcmp (text
, "-nodefaultlibs"))
169 opt
= OPTION_nostdlib
;
170 else if (!strcmp (text
, "-fsyntax-only"))
171 opt
= OPTION_syntax_only
;
172 else if (!strcmp (text
, "-dumpversion"))
173 opt
= OPTION_version
;
174 else if (!strcmp (text
, "-fversion")) /* Really --version!! */
175 opt
= OPTION_version
;
176 else if (!strcmp (text
, "-Xlinker") || !strcmp (text
, "-specs"))
188 if ((arg
!= NULL
) && (arg
[0] == '\0'))
195 /* Append another argument to the list being built. As long as it is
196 identical to the corresponding arg in the original list, just increment
197 the new arg count. Otherwise allocate a new list, etc. */
200 append_arg (const char *arg
)
202 static int newargsize
;
205 fprintf (stderr
, "`%s'\n", arg
);
208 if (g77_newargv
== g77_xargv
209 && g77_newargc
< g77_xargc
210 && (arg
== g77_xargv
[g77_newargc
]
211 || !strcmp (arg
, g77_xargv
[g77_newargc
])))
214 return; /* Nothing new here. */
217 if (g77_newargv
== g77_xargv
)
218 { /* Make new arglist. */
221 newargsize
= (g77_xargc
<< 2) + 20; /* This should handle all. */
222 g77_newargv
= (const char **) xmalloc (newargsize
* sizeof (char *));
224 /* Copy what has been done so far. */
225 for (i
= 0; i
< g77_newargc
; ++i
)
226 g77_newargv
[i
] = g77_xargv
[i
];
229 if (g77_newargc
== newargsize
)
230 fatal ("overflowed output arg list for '%s'", arg
);
232 g77_newargv
[g77_newargc
++] = arg
;
236 lang_specific_driver (int *in_argc
, const char *const **in_argv
,
237 int *in_added_libraries ATTRIBUTE_UNUSED
)
240 const char *const *argv
= *in_argv
;
247 /* This will be NULL if we encounter a situation where we should not
249 const char *library
= FORTRAN_LIBRARY
;
251 /* 0 => -xnone in effect.
252 1 => -xfoo in effect. */
253 int saw_speclang
= 0;
255 /* 0 => initial/reset state
256 1 => last arg was -l<library>
257 2 => last two args were -l<library> -lm. */
260 /* 0 => initial/reset state
261 1 => FORTRAN_INIT linked in */
264 /* By default, we throw on the math library if we have one. */
265 int need_math
= (MATH_LIBRARY
[0] != '\0');
267 /* The number of input and output files in the incoming arg list. */
272 fprintf (stderr
, "Incoming:");
273 for (i
= 0; i
< argc
; i
++)
274 fprintf (stderr
, " %s", argv
[i
]);
275 fprintf (stderr
, "\n");
281 g77_newargv
= (const char **) argv
;
283 /* First pass through arglist.
285 If -nostdlib or a "turn-off-linking" option is anywhere in the
286 command line, don't do any library-option processing (except
287 relating to -x). Also, if -v is specified, but no other options
288 that do anything special (allowing -V version, etc.), remember
289 to add special stuff to make gcc command actually invoke all
290 the different phases of the compilation process so all the version
293 Also, here is where all problems with missing arguments to options
294 are caught. If this loop is exited normally, it means all options
295 have the appropriate number of arguments as far as the rest of this
296 program is concerned. */
298 for (i
= 1; i
< argc
; ++i
)
300 if ((argv
[i
][0] == '+') && (argv
[i
][1] == 'e'))
305 if ((argv
[i
][0] != '-') || (argv
[i
][1] == '\0'))
311 lookup_option (&opt
, &skip
, NULL
, argv
[i
]);
315 case OPTION_nostdlib
:
318 case OPTION_syntax_only
:
320 /* These options disable linking entirely or linking of the
321 standard libraries. */
342 /* These options are useful in conjunction with -v to get
343 appropriate version info. */
347 printf ("GNU Fortran 95 (GCC) %s\n", version_string
);
348 printf ("Copyright %s 2006 Free Software Foundation, Inc.\n\n",
350 printf (_("GNU Fortran comes with NO WARRANTY, to the extent permitted by law.\n\
351 You may redistribute copies of GNU Fortran\n\
352 under the terms of the GNU General Public License.\n\
353 For more information about these matters, see the file named COPYING\n\n"));
358 /* Let gcc.c handle this, as it has a really
359 cool facility for handling --help and --verbose --help. */
366 /* This is the one place we check for missing arguments in the
372 fatal ("argument to '%s' missing", argv
[i
]);
375 if ((n_outfiles
!= 0) && (n_infiles
== 0))
376 fatal ("no input files; unwilling to write output files");
378 /* If there are no input files, no need for the library. */
382 /* Second pass through arglist, transforming arguments as appropriate. */
384 append_arg (argv
[0]); /* Start with command name, of course. */
386 for (i
= 1; i
< argc
; ++i
)
388 if (argv
[i
][0] == '\0')
390 append_arg (argv
[i
]); /* Interesting. Just append as is. */
394 if ((argv
[i
][0] == '-') && (argv
[i
][1] == 'M'))
398 if (argv
[i
][2] == '\0')
400 p
= XNEWVEC (char, strlen (argv
[i
+ 1]) + 2);
403 strcpy (&p
[2], argv
[i
+ 1]);
408 p
= XNEWVEC (char, strlen (argv
[i
]) + 1);
415 if ((argv
[i
][0] == '-') && (argv
[i
][1] != 'l'))
417 /* Not a filename or library. */
419 if (saw_library
== 1 && need_math
) /* -l<library>. */
420 append_arg (MATH_LIBRARY
);
424 lookup_option (&opt
, &skip
, &arg
, argv
[i
]);
426 if (argv
[i
][1] == '\0')
428 append_arg (argv
[i
]); /* "-" == Standard input. */
434 /* Track input language. */
442 saw_speclang
= (strcmp (lang
, "none") != 0);
445 append_arg (argv
[i
]);
447 for (; skip
!= 0; --skip
)
448 append_arg (argv
[++i
]);
453 /* A filename/library, not an option. */
456 saw_library
= 0; /* -xfoo currently active. */
458 { /* -lfoo or filename. */
459 if (strcmp (argv
[i
], MATH_LIBRARY
) == 0)
461 if (saw_library
== 1)
462 saw_library
= 2; /* -l<library> -lm. */
467 append_arg (FORTRAN_INIT
);
470 append_arg (FORTRAN_LIBRARY
);
473 else if (strcmp (argv
[i
], FORTRAN_LIBRARY
) == 0)
474 saw_library
= 1; /* -l<library>. */
476 { /* Other library, or filename. */
477 if (saw_library
== 1 && need_math
)
478 append_arg (MATH_LIBRARY
);
482 append_arg (argv
[i
]);
485 /* Append `-lg2c -lm' as necessary. */
488 { /* Doing a link and no -nostdlib. */
490 append_arg ("-xnone");
497 append_arg (FORTRAN_INIT
);
500 append_arg (library
);
503 append_arg (MATH_LIBRARY
);
509 #ifdef ENABLE_SHARED_LIBGCC
514 for (i
= 1; i
< g77_newargc
; i
++)
515 if (g77_newargv
[i
][0] == '-')
516 if (strcmp (g77_newargv
[i
], "-static-libgcc") == 0
517 || strcmp (g77_newargv
[i
], "-static") == 0)
520 if (i
== g77_newargc
)
521 append_arg ("-shared-libgcc");
526 if (verbose
&& g77_newargv
!= g77_xargv
)
528 fprintf (stderr
, _("Driving:"));
529 for (i
= 0; i
< g77_newargc
; i
++)
530 fprintf (stderr
, " %s", g77_newargv
[i
]);
531 fprintf (stderr
, "\n");
534 *in_argc
= g77_newargc
;
535 *in_argv
= g77_newargv
;
538 /* Called before linking. Returns 0 on success and -1 on failure. */
540 lang_specific_pre_link (void) /* Not used for F77. */
545 /* Number of extra output files that lang_specific_pre_link may generate. */
546 int lang_specific_extra_outfiles
= 0; /* Not used for F77. */