OSDN Git Service

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