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