OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / f / g77spec.c
1 /* Specific flags and argument handling of the Fortran front-end.
2    Copyright (C) 1997, 1999, 2000 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
21 /* This file contains a filter for the main `gcc' driver, which is
22    replicated for the `g77' 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 (g77), we do the following to the argument list
29    before passing it to `gcc':
30
31    1.  Make sure `-lg2c -lm' is at the end of the list.
32
33    2.  Make sure each time `-lg2c' or `-lm' is seen, it forms
34        part of the series `-lg2c -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 #include <f/version.h>
51
52 #ifndef MATH_LIBRARY
53 #define MATH_LIBRARY "-lm"
54 #endif
55
56 #ifndef FORTRAN_LIBRARY
57 #define FORTRAN_LIBRARY "-lg2c"
58 #endif
59
60 /* Options this driver needs to recognize, not just know how to
61    skip over.  */
62 typedef enum
63 {
64   OPTION_b,                     /* Aka --prefix. */
65   OPTION_B,                     /* Aka --target. */
66   OPTION_c,                     /* Aka --compile. */
67   OPTION_driver,                /* Wrapper-specific option. */
68   OPTION_E,                     /* Aka --preprocess. */
69   OPTION_help,                  /* --help. */
70   OPTION_i,                     /* -imacros, -include, -include-*. */
71   OPTION_l,
72   OPTION_L,                     /* Aka --library-directory. */
73   OPTION_M,                     /* Aka --dependencies. */
74   OPTION_MM,                    /* Aka --user-dependencies. */
75   OPTION_nostdlib,              /* Aka --no-standard-libraries, or
76                                    -nodefaultlibs. */
77   OPTION_o,                     /* Aka --output. */
78   OPTION_S,                     /* Aka --assemble. */
79   OPTION_syntax_only,           /* -fsyntax-only. */
80   OPTION_v,                     /* Aka --verbose. */
81   OPTION_version,               /* --version. */
82   OPTION_V,                     /* Aka --use-version. */
83   OPTION_x,                     /* Aka --language. */
84   OPTION_                       /* Unrecognized or unimportant. */
85 } Option;
86
87 /* The original argument list and related info is copied here.  */
88 static int g77_xargc;
89 static const char *const *g77_xargv;
90 static void lookup_option PARAMS ((Option *, int *, const char **,
91                                    const char *));
92 static void append_arg PARAMS ((const char *));
93
94 /* The new argument list will be built here.  */
95 static int g77_newargc;
96 static const char **g77_newargv;
97
98 /* --- This comes from gcc.c (2.8.1) verbatim: */
99
100 /* This defines which switch letters take arguments.  */
101
102 #define DEFAULT_SWITCH_TAKES_ARG(CHAR)      \
103   ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
104    || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
105    || (CHAR) == 'I' || (CHAR) == 'm' || (CHAR) == 'x' \
106    || (CHAR) == 'L' || (CHAR) == 'A')
107
108 #ifndef SWITCH_TAKES_ARG
109 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
110 #endif
111
112 /* This defines which multi-letter switches take arguments.  */
113
114 #define DEFAULT_WORD_SWITCH_TAKES_ARG(STR)              \
115  (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext")      \
116   || !strcmp (STR, "Tbss") || !strcmp (STR, "include")  \
117   || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
118   || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
119   || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
120   || !strcmp (STR, "isystem") || !strcmp (STR, "specs"))
121
122 #ifndef WORD_SWITCH_TAKES_ARG
123 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
124 #endif
125
126 /* --- End of verbatim.  */
127
128 /* Assumes text[0] == '-'.  Returns number of argv items that belong to
129    (and follow) this one, an option id for options important to the
130    caller, and a pointer to the first char of the arg, if embedded (else
131    returns NULL, meaning no arg or it's the next argv).
132
133    Note that this also assumes gcc.c's pass converting long options
134    to short ones, where available, has already been run.  */
135
136 static void
137 lookup_option (xopt, xskip, xarg, text)
138      Option *xopt;
139      int *xskip;
140      const char **xarg;
141      const char *text;
142 {
143   Option opt = OPTION_;
144   int skip;
145   const char *arg = NULL;
146
147   if ((skip = SWITCH_TAKES_ARG (text[1])))
148     skip -= (text[2] != '\0');  /* See gcc.c. */
149
150   if (text[1] == 'B')
151     opt = OPTION_B, skip = (text[2] == '\0'), arg = text + 2;
152   else if (text[1] == 'b')
153     opt = OPTION_b, skip = (text[2] == '\0'), arg = text + 2;
154   else if ((text[1] == 'c') && (text[2] == '\0'))
155     opt = OPTION_c, skip = 0;
156   else if ((text[1] == 'E') && (text[2] == '\0'))
157     opt = OPTION_E, skip = 0;
158   else if (text[1] == 'i')
159     opt = OPTION_i, skip = 0;
160   else if (text[1] == 'l')
161     opt = OPTION_l;
162   else if (text[1] == 'L')
163     opt = OPTION_L, arg = text + 2;
164   else if (text[1] == 'o')
165     opt = OPTION_o;
166   else if ((text[1] == 'S') && (text[2] == '\0'))
167     opt = OPTION_S, skip = 0;
168   else if (text[1] == 'V')
169     opt = OPTION_V, skip = (text[2] == '\0');
170   else if ((text[1] == 'v') && (text[2] == '\0'))
171     opt = OPTION_v, skip = 0;
172   else if (text[1] == 'x')
173     opt = OPTION_x, arg = text + 2;
174   else
175     {
176       if ((skip = WORD_SWITCH_TAKES_ARG (text + 1)) != 0)  /* See gcc.c. */
177         ;
178       else if (! strncmp (text, "-fdriver", 8))  /* Really --driver!! */
179         opt = OPTION_driver;    /* Never mind arg, this is unsupported. */
180       else if (! strcmp (text, "-fhelp"))  /* Really --help!! */
181         opt = OPTION_help;
182       else if (! strcmp (text, "-M"))
183         opt = OPTION_M;
184       else if (! strcmp (text, "-MM"))
185         opt = OPTION_MM;
186       else if (! strcmp (text, "-nostdlib")
187                || ! strcmp (text, "-nodefaultlibs"))
188         opt = OPTION_nostdlib;
189       else if (! strcmp (text, "-fsyntax-only"))
190         opt = OPTION_syntax_only;
191       else if (! strcmp (text, "-dumpversion"))
192         opt = OPTION_version;
193       else if (! strcmp (text, "-Xlinker")
194                || ! strcmp (text, "-specs"))
195         skip = 1;
196       else
197         skip = 0;
198     }
199
200   if (xopt != NULL)
201     *xopt = opt;
202   if (xskip != NULL)
203     *xskip = skip;
204   if (xarg != NULL)
205     {
206       if ((arg != NULL)
207           && (arg[0] == '\0'))
208         *xarg = NULL;
209       else
210         *xarg = arg;
211     }
212 }
213
214 /* Append another argument to the list being built.  As long as it is
215    identical to the corresponding arg in the original list, just increment
216    the new arg count.  Otherwise allocate a new list, etc.  */
217
218 static void
219 append_arg (arg)
220      const char *arg;
221 {
222   static int newargsize;
223
224 #if 0
225   fprintf (stderr, "`%s'\n", arg);
226 #endif
227
228   if (g77_newargv == g77_xargv
229       && g77_newargc < g77_xargc
230       && (arg == g77_xargv[g77_newargc]
231           || ! strcmp (arg, g77_xargv[g77_newargc])))
232     {
233       ++g77_newargc;
234       return;                   /* Nothing new here. */
235     }
236
237   if (g77_newargv == g77_xargv)
238     {                           /* Make new arglist. */
239       int i;
240
241       newargsize = (g77_xargc << 2) + 20;       /* This should handle all. */
242       g77_newargv = (const char **) xmalloc (newargsize * sizeof (char *));
243
244       /* Copy what has been done so far.  */
245       for (i = 0; i < g77_newargc; ++i)
246         g77_newargv[i] = g77_xargv[i];
247     }
248
249   if (g77_newargc == newargsize)
250     fatal ("overflowed output arg list for `%s'", arg);
251
252   g77_newargv[g77_newargc++] = arg;
253 }
254
255 void
256 lang_specific_driver (in_argc, in_argv, in_added_libraries)
257      int *in_argc;
258      const char *const **in_argv;
259      int *in_added_libraries ATTRIBUTE_UNUSED;
260 {
261   int argc = *in_argc;
262   const char *const *argv = *in_argv;
263   int i;
264   int verbose = 0;
265   Option opt;
266   int skip;
267   const char *arg;
268
269   /* This will be NULL if we encounter a situation where we should not
270      link in libf2c.  */
271   const char *library = FORTRAN_LIBRARY;
272
273   /* This will become 0 if anything other than -v and kin (like -V)
274      is seen, meaning the user is trying to accomplish something.
275      If it remains nonzero, and the user wants version info, add stuff to
276      the command line to make gcc invoke all the appropriate phases
277      to get all the version info.  */
278   int add_version_magic = 1;
279
280   /* 0 => -xnone in effect.
281      1 => -xfoo in effect.  */
282   int saw_speclang = 0;
283
284   /* 0 => initial/reset state
285      1 => last arg was -l<library>
286      2 => last two args were -l<library> -lm.  */
287   int saw_library = 0;
288
289   /* By default, we throw on the math library if we have one.  */
290   int need_math = (MATH_LIBRARY[0] != '\0');
291
292   /* The number of input and output files in the incoming arg list.  */
293   int n_infiles = 0;
294   int n_outfiles = 0;
295
296 #if 0
297   fprintf (stderr, "Incoming:");
298   for (i = 0; i < argc; i++)
299     fprintf (stderr, " %s", argv[i]);
300   fprintf (stderr, "\n");
301 #endif
302
303   g77_xargc = argc;
304   g77_xargv = argv;
305   g77_newargc = 0;
306   g77_newargv = (const char **) argv;
307
308   /* First pass through arglist.
309
310      If -nostdlib or a "turn-off-linking" option is anywhere in the
311      command line, don't do any library-option processing (except
312      relating to -x).  Also, if -v is specified, but no other options
313      that do anything special (allowing -V version, etc.), remember
314      to add special stuff to make gcc command actually invoke all
315      the different phases of the compilation process so all the version
316      numbers can be seen.
317
318      Also, here is where all problems with missing arguments to options
319      are caught.  If this loop is exited normally, it means all options
320      have the appropriate number of arguments as far as the rest of this
321      program is concerned.  */
322
323   for (i = 1; i < argc; ++i)
324     {
325       if ((argv[i][0] == '+') && (argv[i][1] == 'e'))
326         {
327           add_version_magic = 0;
328           continue;
329         }
330
331       if ((argv[i][0] != '-') || (argv[i][1] == '\0'))
332         {
333           ++n_infiles;
334           add_version_magic = 0;
335           continue;
336         }
337
338       lookup_option (&opt, &skip, NULL, argv[i]);
339
340       switch (opt)
341         {
342         case OPTION_nostdlib:
343         case OPTION_c:
344         case OPTION_S:
345         case OPTION_syntax_only:
346         case OPTION_E:
347         case OPTION_M:
348         case OPTION_MM:
349           /* These options disable linking entirely or linking of the
350              standard libraries.  */
351           library = 0;
352           add_version_magic = 0;
353           break;
354
355         case OPTION_l:
356           ++n_infiles;
357           add_version_magic = 0;
358           break;
359
360         case OPTION_o:
361           ++n_outfiles;
362           add_version_magic = 0;
363           break;
364
365         case OPTION_v:
366           if (! verbose)
367             fprintf (stderr, "g77 version %s (Fortran Frontend version %s)\n",
368                      version_string, ffe_version_string);
369           verbose = 1;
370           break;
371
372         case OPTION_b:
373         case OPTION_B:
374         case OPTION_L:
375         case OPTION_i:
376         case OPTION_V:
377           /* These options are useful in conjunction with -v to get
378              appropriate version info.  */
379           break;
380
381         case OPTION_version:
382           printf ("\
383 GNU Fortran %s\n\
384 Copyright (C) 2001 Free Software Foundation, Inc.\n\
385 For more version information on components of the GNU Fortran\n\
386 compilation system, especially useful when reporting bugs,\n\
387 type the command `g77 --verbose'.\n\
388 \n\
389 GNU Fortran comes with NO WARRANTY, to the extent permitted by law.\n\
390 You may redistribute copies of GNU Fortran\n\
391 under the terms of the GNU General Public License.\n\
392 For more information about these matters, see the file named COPYING\n\
393 or type the command `info -f g77 Copying'.\n\
394 ", ffe_version_string);
395           exit (0);
396           break;
397
398         case OPTION_help:
399           /* Let gcc.c handle this, as it has a really
400              cool facility for handling --help and --verbose --help.  */
401           return;
402
403 #if 0
404           printf ("\
405 Usage: g77 [OPTION]... FORTRAN-SOURCE...\n\
406 \n\
407 Compile and link Fortran source code to produce an executable program,\n\
408 which by default is named `a.out', and can be invoked with the UNIX\n\
409 command `./a.out'.\n\
410 \n\
411 Options:\n\
412 --debug                include debugging information in executable.\n\
413 --help                 display this help and exit.\n\
414 --optimize[=LEVEL]     take extra time and memory to make generated\n\
415                          executable run faster.  LEVEL is 0 for no\n\
416                          optimization, 1 for normal optimization, and\n\
417                          increases through 3 for more optimization.\n\
418 --output=PROGRAM       name the executable PROGRAM instead of a.out;\n\
419                          invoke with the command `./PROGRAM'.\n\
420 --version              display version information and exit.\n\
421 \n\
422 Many other options exist to tailor the compilation process, specify\n\
423 the dialect of the Fortran source code, specify details of the\n\
424 code-generation methodology, and so on.\n\
425 \n\
426 For more information on g77 and gcc, type the commands `info -f g77'\n\
427 and `info -f gcc' to read the Info documentation.\n\
428 \n\
429 For bug reporting instructions, please see:\n\
430 %s.\n", GCCBUGURL);
431           exit (0);
432           break;
433 #endif
434
435         case OPTION_driver:
436           fatal ("--driver no longer supported");
437           break;
438
439         default:
440           add_version_magic = 0;
441           break;
442         }
443
444       /* This is the one place we check for missing arguments in the
445          program.  */
446
447       if (i + skip < argc)
448         i += skip;
449       else
450         fatal ("argument to `%s' missing", argv[i]);
451     }
452
453   if ((n_outfiles != 0) && (n_infiles == 0))
454     fatal ("No input files; unwilling to write output files");
455
456   /* Second pass through arglist, transforming arguments as appropriate.  */
457
458   append_arg (argv[0]); /* Start with command name, of course. */
459
460   for (i = 1; i < argc; ++i)
461     {
462       if (argv[i][0] == '\0')
463         {
464           append_arg (argv[i]); /* Interesting.  Just append as is. */
465           continue;
466         }
467
468       if ((argv[i][0] == '-') && (argv[i][1] != 'l'))
469         {
470           /* Not a filename or library. */
471
472          if (saw_library == 1 && need_math)    /* -l<library>. */
473             append_arg (MATH_LIBRARY);
474
475           saw_library = 0;
476
477           lookup_option (&opt, &skip, &arg, argv[i]);
478
479           if (argv[i][1] == '\0')
480             {
481               append_arg (argv[i]);     /* "-" == Standard input. */
482               continue;
483             }
484
485           if (opt == OPTION_x)
486             {
487               /* Track input language. */
488               const char *lang;
489
490               if (arg == NULL)
491                 lang = argv[i+1];
492               else
493                 lang = arg;
494
495               saw_speclang = (strcmp (lang, "none") != 0);
496             }
497
498           append_arg (argv[i]);
499
500           for (; skip != 0; --skip)
501             append_arg (argv[++i]);
502
503           continue;
504         }
505
506       /* A filename/library, not an option. */
507
508       if (saw_speclang)
509         saw_library = 0;        /* -xfoo currently active. */
510       else
511         {                       /* -lfoo or filename. */
512           if (strcmp (argv[i], MATH_LIBRARY) == 0
513 #ifdef ALT_LIBM
514               || strcmp (argv[i], ALT_LIBM) == 0
515 #endif
516               )
517             {
518               if (saw_library == 1)
519                 saw_library = 2;        /* -l<library> -lm. */
520               else
521                 append_arg (FORTRAN_LIBRARY);
522             }
523           else if (strcmp (argv[i], FORTRAN_LIBRARY) == 0)
524             saw_library = 1;    /* -l<library>. */
525           else
526             {           /* Other library, or filename. */
527              if (saw_library == 1 && need_math)
528                 append_arg (MATH_LIBRARY);
529               saw_library = 0;
530             }
531         }
532       append_arg (argv[i]);
533     }
534
535   /* Append `-lg2c -lm' as necessary.  */
536
537   if (! add_version_magic && library)
538     {                           /* Doing a link and no -nostdlib. */
539       if (saw_speclang)
540         append_arg ("-xnone");
541
542       switch (saw_library)
543         {
544         case 0:
545           append_arg (library);
546         case 1:
547          if (need_math)
548            append_arg (MATH_LIBRARY);
549         default:
550           break;
551         }
552     }
553   else if (add_version_magic && verbose)
554     {
555       append_arg ("-c");
556       append_arg ("-xf77-version");
557       append_arg ("/dev/null");
558       append_arg ("-xnone");
559     }
560
561   if (verbose
562       && g77_newargv != g77_xargv)
563     {
564       fprintf (stderr, "Driving:");
565       for (i = 0; i < g77_newargc; i++)
566         fprintf (stderr, " %s", g77_newargv[i]);
567       fprintf (stderr, "\n");
568     }
569
570   *in_argc = g77_newargc;
571   *in_argv = g77_newargv;
572 }
573
574 /* Called before linking.  Returns 0 on success and -1 on failure. */
575 int lang_specific_pre_link ()  /* Not used for F77. */
576 {
577   return 0;
578 }
579
580 /* Number of extra output files that lang_specific_pre_link may generate. */
581 int lang_specific_extra_outfiles = 0;  /* Not used for F77. */