OSDN Git Service

Merge tree-ssa-20020619-branch into mainline.
[pf3gnuchains/gcc-fork.git] / gcc / fortran / gfortranspec.c
1 /* Specific flags and argument handling of the Fortran front-end.
2    Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
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 2, or (at your option)
9 any later version.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
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.
27
28    For GNU Fortran 95(gfortran), we do the following to the argument list
29    before passing it to `gcc':
30
31    1.  Make sure `-lgfortran -lm' is at the end of the list.
32
33    2.  Make sure each time `-lgfortran' or `-lm' is seen, it forms
34        part of the series `-lgfortran -lm'.
35
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.
39
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.  */
46
47 #include "config.h"
48 #include "system.h"
49 #include "gcc.h"
50
51 #include "coretypes.h"
52 #include "tm.h"
53
54 #ifndef MATH_LIBRARY
55 #define MATH_LIBRARY "-lm"
56 #endif
57
58 #ifndef FORTRAN_INIT
59 #define FORTRAN_INIT "-lgfortranbegin"
60 #endif
61
62 #ifndef FORTRAN_LIBRARY
63 #define FORTRAN_LIBRARY "-lgfortran"
64 #endif
65
66 /* Options this driver needs to recognize, not just know how to
67    skip over.  */
68 typedef enum
69 {
70   OPTION_b,                     /* Aka --prefix. */
71   OPTION_B,                     /* Aka --target. */
72   OPTION_c,                     /* Aka --compile. */
73   OPTION_E,                     /* Aka --preprocess. */
74   OPTION_help,                  /* --help. */
75   OPTION_i,                     /* -imacros, -include, -include-*. */
76   OPTION_l,
77   OPTION_L,                     /* Aka --library-directory. */
78   OPTION_nostdlib,              /* Aka --no-standard-libraries, or
79                                    -nodefaultlibs. */
80   OPTION_o,                     /* Aka --output. */
81   OPTION_S,                     /* Aka --assemble. */
82   OPTION_syntax_only,           /* -fsyntax-only. */
83   OPTION_v,                     /* Aka --verbose. */
84   OPTION_version,               /* --version. */
85   OPTION_V,                     /* Aka --use-version. */
86   OPTION_x,                     /* Aka --language. */
87   OPTION_                       /* Unrecognized or unimportant. */
88 }
89 Option;
90
91 /* The original argument list and related info is copied here.  */
92 static int g77_xargc;
93 static const char *const *g77_xargv;
94 static void lookup_option (Option *, int *, const char **, const char *);
95 static void append_arg (const char *);
96
97 /* The new argument list will be built here.  */
98 static int g77_newargc;
99 static const char **g77_newargv;
100
101 const struct spec_function lang_specific_spec_functions[] = {{0,0}};
102
103 /* --- This comes from gcc.c (2.8.1) verbatim: */
104
105 /* This defines which switch letters take arguments.  */
106
107 #ifndef SWITCH_TAKES_ARG
108 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
109 #endif
110
111 /* This defines which multi-letter switches take arguments.  */
112
113 #ifndef WORD_SWITCH_TAKES_ARG
114 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
115 #endif
116
117 /* --- End of verbatim.  */
118
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).
123
124    Note that this also assumes gcc.c's pass converting long options
125    to short ones, where available, has already been run.  */
126
127 static void
128 lookup_option (Option *xopt, int *xskip, const char **xarg, const char *text)
129 {
130   Option opt = OPTION_;
131   int skip;
132   const char *arg = NULL;
133
134   if ((skip = SWITCH_TAKES_ARG (text[1])))
135     skip -= (text[2] != '\0');  /* See gcc.c. */
136
137   if (text[1] == 'B')
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')
148     opt = OPTION_l;
149   else if (text[1] == 'L')
150     opt = OPTION_L, arg = text + 2;
151   else if (text[1] == 'o')
152     opt = OPTION_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;
161   else
162     {
163       if ((skip = WORD_SWITCH_TAKES_ARG (text + 1)) != 0)       /* See gcc.c. */
164         ;
165       else if (!strcmp (text, "-fhelp"))        /* Really --help!! */
166         opt = OPTION_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"))
177         skip = 1;
178       else
179         skip = 0;
180     }
181
182   if (xopt != NULL)
183     *xopt = opt;
184   if (xskip != NULL)
185     *xskip = skip;
186   if (xarg != NULL)
187     {
188       if ((arg != NULL) && (arg[0] == '\0'))
189         *xarg = NULL;
190       else
191         *xarg = arg;
192     }
193 }
194
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.  */
198
199 static void
200 append_arg (const char *arg)
201 {
202   static int newargsize;
203
204 #if 0
205   fprintf (stderr, "`%s'\n", arg);
206 #endif
207
208   if (g77_newargv == g77_xargv
209       && g77_newargc < g77_xargc
210       && (arg == g77_xargv[g77_newargc]
211           || !strcmp (arg, g77_xargv[g77_newargc])))
212     {
213       ++g77_newargc;
214       return;                   /* Nothing new here. */
215     }
216
217   if (g77_newargv == g77_xargv)
218     {                           /* Make new arglist. */
219       int i;
220
221       newargsize = (g77_xargc << 2) + 20;       /* This should handle all. */
222       g77_newargv = (const char **) xmalloc (newargsize * sizeof (char *));
223
224       /* Copy what has been done so far.  */
225       for (i = 0; i < g77_newargc; ++i)
226         g77_newargv[i] = g77_xargv[i];
227     }
228
229   if (g77_newargc == newargsize)
230     fatal ("overflowed output arg list for `%s'", arg);
231
232   g77_newargv[g77_newargc++] = arg;
233 }
234
235 void
236 lang_specific_driver (int *in_argc, const char *const **in_argv,
237                       int *in_added_libraries ATTRIBUTE_UNUSED)
238 {
239   int argc = *in_argc;
240   const char *const *argv = *in_argv;
241   int i;
242   int verbose = 0;
243   Option opt;
244   int skip;
245   const char *arg;
246
247   /* This will be NULL if we encounter a situation where we should not
248      link in libf2c.  */
249   const char *library = FORTRAN_LIBRARY;
250
251   /* 0 => -xnone in effect.
252      1 => -xfoo in effect.  */
253   int saw_speclang = 0;
254
255   /* 0 => initial/reset state
256      1 => last arg was -l<library>
257      2 => last two args were -l<library> -lm.  */
258   int saw_library = 0;
259
260   /* 0 => initial/reset state
261      1 => FORTRAN_INIT linked in */
262   int use_init = 0;
263
264   /* By default, we throw on the math library if we have one.  */
265   int need_math = (MATH_LIBRARY[0] != '\0');
266
267   /* The number of input and output files in the incoming arg list.  */
268   int n_infiles = 0;
269   int n_outfiles = 0;
270
271 #if 0
272   fprintf (stderr, "Incoming:");
273   for (i = 0; i < argc; i++)
274     fprintf (stderr, " %s", argv[i]);
275   fprintf (stderr, "\n");
276 #endif
277
278   g77_xargc = argc;
279   g77_xargv = argv;
280   g77_newargc = 0;
281   g77_newargv = (const char **) argv;
282
283   /* First pass through arglist.
284
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
291      numbers can be seen.
292
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.  */
297
298   for (i = 1; i < argc; ++i)
299     {
300       if ((argv[i][0] == '+') && (argv[i][1] == 'e'))
301         {
302           continue;
303         }
304
305       if ((argv[i][0] != '-') || (argv[i][1] == '\0'))
306         {
307           ++n_infiles;
308           continue;
309         }
310
311       lookup_option (&opt, &skip, NULL, argv[i]);
312
313       switch (opt)
314         {
315         case OPTION_nostdlib:
316         case OPTION_c:
317         case OPTION_S:
318         case OPTION_syntax_only:
319         case OPTION_E:
320           /* These options disable linking entirely or linking of the
321              standard libraries.  */
322           library = 0;
323           break;
324
325         case OPTION_l:
326           ++n_infiles;
327           break;
328
329         case OPTION_o:
330           ++n_outfiles;
331           break;
332
333         case OPTION_v:
334           verbose = 1;
335           break;
336
337         case OPTION_b:
338         case OPTION_B:
339         case OPTION_L:
340         case OPTION_i:
341         case OPTION_V:
342           /* These options are useful in conjunction with -v to get
343              appropriate version info.  */
344           break;
345
346         case OPTION_version:
347           printf ("\
348 GNU Fortran 95 (GCC %s)\n\
349 Copyright (C) 2003 Free Software Foundation, Inc.\n\
350 \n\
351 GNU Fortran comes with NO WARRANTY, to the extent permitted by law.\n\
352 You may redistribute copies of GNU Fortran\n\
353 under the terms of the GNU General Public License.\n\
354 For more information about these matters, see the file named COPYING\n\
355 ", version_string);
356           exit (0);
357           break;
358
359         case OPTION_help:
360           /* Let gcc.c handle this, as it has a really
361              cool facility for handling --help and --verbose --help.  */
362           return;
363
364         default:
365           break;
366         }
367
368       /* This is the one place we check for missing arguments in the
369          program.  */
370
371       if (i + skip < argc)
372         i += skip;
373       else
374         fatal ("argument to `%s' missing", argv[i]);
375     }
376
377   if ((n_outfiles != 0) && (n_infiles == 0))
378     fatal ("no input files; unwilling to write output files");
379
380   /* If there are no input files, no need for the library.  */
381   if (n_infiles == 0)
382     library = 0;
383
384   /* Second pass through arglist, transforming arguments as appropriate.  */
385
386   append_arg (argv[0]);         /* Start with command name, of course. */
387
388   for (i = 1; i < argc; ++i)
389     {
390       if (argv[i][0] == '\0')
391         {
392           append_arg (argv[i]); /* Interesting.  Just append as is. */
393           continue;
394         }
395
396       if ((argv[i][0] == '-') && (argv[i][1] == 'M'))
397         {
398           char *p;
399
400           if (argv[i][2] == '\0')
401             {
402               p = xmalloc (strlen (argv[i + 1]) + 2);
403               p[0] = '-';
404               p[1] = 'J';
405               strcpy (&p[2], argv[i + 1]);
406               i++;
407             }
408           else
409             {
410               p = xmalloc (strlen (argv[i]) + 1);
411               strcpy (p, argv[i]);
412             }
413           append_arg (p);
414           continue;
415         }
416
417       if ((argv[i][0] == '-') && (argv[i][1] != 'l'))
418         {
419           /* Not a filename or library. */
420
421           if (saw_library == 1 && need_math)    /* -l<library>. */
422             append_arg (MATH_LIBRARY);
423
424           saw_library = 0;
425
426           lookup_option (&opt, &skip, &arg, argv[i]);
427
428           if (argv[i][1] == '\0')
429             {
430               append_arg (argv[i]);     /* "-" == Standard input. */
431               continue;
432             }
433
434           if (opt == OPTION_x)
435             {
436               /* Track input language. */
437               const char *lang;
438
439               if (arg == NULL)
440                 lang = argv[i + 1];
441               else
442                 lang = arg;
443
444               saw_speclang = (strcmp (lang, "none") != 0);
445             }
446
447           append_arg (argv[i]);
448
449           for (; skip != 0; --skip)
450             append_arg (argv[++i]);
451
452           continue;
453         }
454
455       /* A filename/library, not an option. */
456
457       if (saw_speclang)
458         saw_library = 0;        /* -xfoo currently active. */
459       else
460         {                       /* -lfoo or filename. */
461           if (strcmp (argv[i], MATH_LIBRARY) == 0)
462             {
463               if (saw_library == 1)
464                 saw_library = 2;        /* -l<library> -lm. */
465               else
466                 {
467                   if (0 == use_init)
468                     {
469                       append_arg (FORTRAN_INIT);
470                       use_init = 1;
471                     }
472                   append_arg (FORTRAN_LIBRARY);
473                 }
474             }
475           else if (strcmp (argv[i], FORTRAN_LIBRARY) == 0)
476             saw_library = 1;    /* -l<library>. */
477           else
478             {                   /* Other library, or filename. */
479               if (saw_library == 1 && need_math)
480                 append_arg (MATH_LIBRARY);
481               saw_library = 0;
482             }
483         }
484       append_arg (argv[i]);
485     }
486
487   /* Append `-lg2c -lm' as necessary.  */
488
489   if (library)
490     {                           /* Doing a link and no -nostdlib. */
491       if (saw_speclang)
492         append_arg ("-xnone");
493
494       switch (saw_library)
495         {
496         case 0:
497           if (0 == use_init)
498             {
499               append_arg (FORTRAN_INIT);
500               use_init = 1;
501             }
502           append_arg (library);
503         case 1:
504           if (need_math)
505             append_arg (MATH_LIBRARY);
506         default:
507           break;
508         }
509     }
510
511 #ifdef ENABLE_SHARED_LIBGCC
512   if (library)
513     {
514       int i;
515
516       for (i = 1; i < g77_newargc; i++)
517         if (g77_newargv[i][0] == '-')
518           if (strcmp (g77_newargv[i], "-static-libgcc") == 0
519               || strcmp (g77_newargv[i], "-static") == 0)
520             break;
521
522       if (i == g77_newargc)
523         append_arg ("-shared-libgcc");
524     }
525
526 #endif
527
528   if (verbose && g77_newargv != g77_xargv)
529     {
530       fprintf (stderr, "Driving:");
531       for (i = 0; i < g77_newargc; i++)
532         fprintf (stderr, " %s", g77_newargv[i]);
533       fprintf (stderr, "\n");
534     }
535
536   *in_argc = g77_newargc;
537   *in_argv = g77_newargv;
538 }
539
540 /* Called before linking.  Returns 0 on success and -1 on failure. */
541 int
542 lang_specific_pre_link (void)   /* Not used for F77. */
543 {
544   return 0;
545 }
546
547 /* Number of extra output files that lang_specific_pre_link may generate. */
548 int lang_specific_extra_outfiles = 0;   /* Not used for F77. */