OSDN Git Service

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