OSDN Git Service

JCB patch.
[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 #include "config.h"
22
23 #include <sys/types.h>
24 #include <stdio.h>
25
26 #include "gansidecl.h"
27
28 #ifdef HAVE_STDLIB_H
29 #include <stdlib.h>
30 #endif
31
32 #ifdef HAVE_STRING_H
33 #include <string.h>
34 #endif
35
36 /* This bit is set if we saw a `-xfoo' language specification.  */
37 #define LANGSPEC        (1<<1)
38 /* This bit is set if they did `-lm' or `-lmath'.  */
39 #define MATHLIB         (1<<2)
40 /* This bit is set if they did `-lc'.  */
41 #define WITHLIBC        (1<<3)
42
43 #ifndef MATH_LIBRARY
44 #define MATH_LIBRARY "-lm"
45 #endif
46
47 #ifndef FORTRAN_LIBRARY
48 #define FORTRAN_LIBRARY "-lf2c"
49 #endif
50
51 extern char *xmalloc PROTO((size_t));
52
53 void
54 lang_specific_driver (fn, in_argc, in_argv, in_added_libraries)
55      void (*fn)();
56      int *in_argc;
57      char ***in_argv;
58      int *in_added_libraries;
59 {
60   int i, j;
61
62   /* If non-zero, the user gave us the `-v' flag.  */ 
63   int saw_verbose_flag = 0;
64
65   /* This will be 0 if we encounter a situation where we should not
66      link in libstdf77.  */
67   int library = 1;
68
69   /* The number of arguments being added to what's in argv, other than
70      libraries.  We use this to track the number of times we've inserted
71      -xf77/-xnone.  */
72   int added = 2;
73
74   /* Used to track options that take arguments, so we don't go wrapping
75      those with -xf77/-xnone.  */
76   char *quote = NULL;
77
78   /* The new argument list will be contained in this.  */
79   char **arglist;
80
81   /* Non-zero if we saw a `-xfoo' language specification on the
82      command line.  Used to avoid adding our own -xf77 if the user
83      already gave a language for the file.  */
84   int saw_speclang = 0;
85
86   /* "-lm" or "-lmath" if it appears on the command line.  */
87   char *saw_math = 0;
88
89   /* "-lc" if it appears on the command line.  */
90   char *saw_libc = 0;
91
92   /* An array used to flag each argument that needs a bit set for
93      LANGSPEC, MATHLIB, or WITHLIBC.  */
94   int *args;
95
96   /* By default, we throw on the math library.  */
97   int need_math = 1;
98
99   /* The total number of arguments with the new stuff.  */
100   int argc;
101
102   /* The argument list.  */
103   char **argv;
104
105   /* The number of libraries added in.  */
106   int added_libraries;
107
108   /* The total number of arguments with the new stuff.  */
109   int num_args = 1;
110
111   argc = *in_argc;
112   argv = *in_argv;
113   added_libraries = *in_added_libraries;
114
115   args = (int *) xmalloc (argc * sizeof (int));
116   bzero ((char *) args, argc * sizeof (int));
117
118   for (i = 1; i < argc; i++)
119     {
120       /* If the previous option took an argument, we swallow it here.  */
121       if (quote)
122         {
123           quote = NULL;
124           continue;
125         }
126
127       /* We don't do this anymore, since we don't get them with minus
128          signs on them.  */
129       if (argv[i][0] == '\0' || argv[i][1] == '\0')
130         continue;
131
132       if (argv[i][0] == '-')
133         {
134           if (library != 0 && (strcmp (argv[i], "-nostdlib") == 0
135                                || strcmp (argv[i], "-nodefaultlibs") == 0))
136             {
137               library = 0;
138             }
139           else if (strcmp (argv[i], "-lm") == 0
140                    || strcmp (argv[i], "-lmath") == 0
141 #ifdef ALT_LIBM
142                    || strcmp (argv[i], ALT_LIBM) == 0
143 #endif
144                   )
145             {
146               args[i] |= MATHLIB;
147               need_math = 0;
148             }
149           else if (strcmp (argv[i], "-lc") == 0)
150             args[i] |= WITHLIBC;
151           else if (strcmp (argv[i], "-v") == 0)
152             {
153               saw_verbose_flag = 1;
154               if (argc == 2)
155                 {
156                   /* If they only gave us `-v', don't try to link in libf2c. */ 
157                   library = 0;
158                 }
159             }
160           else if (strncmp (argv[i], "-x", 2) == 0)
161             saw_speclang = 1;
162           else if (((argv[i][2] == '\0'
163                      && (char *)strchr ("bBVDUoeTuIYmLiA", argv[i][1]) != NULL)
164                     || strcmp (argv[i], "-Tdata") == 0))
165             quote = argv[i];
166           else if (library != 0 && ((argv[i][2] == '\0'
167                      && (char *) strchr ("cSEM", argv[i][1]) != NULL)
168                     || strcmp (argv[i], "-MM") == 0))
169             {
170               /* Don't specify libraries if we won't link, since that would
171                  cause a warning.  */
172               library = 0;
173               added -= 2;
174             }
175           else
176             /* Pass other options through.  */
177             continue;
178         }
179       else
180         {
181           int len; 
182
183           if (saw_speclang)
184             {
185               saw_speclang = 0;
186               continue;
187             }
188
189           /* If the filename ends in .c or .i, put options around it.
190              But not if a specified -x option is currently active.  */
191           len = strlen (argv[i]);
192           if (len > 2
193               && (argv[i][len - 1] == 'c' || argv[i][len - 1] == 'i')
194               && argv[i][len - 2] == '.')
195             {
196               args[i] |= LANGSPEC;
197               added += 2;
198             }
199         }
200     }
201
202   if (quote)
203     (*fn) ("argument to `%s' missing\n", quote);
204
205   /* If we know we don't have to do anything, bail now.  */
206   if (! added && ! library)
207     {
208       free (args);
209       return;
210     }
211
212   num_args = argc + added + need_math;
213   arglist = (char **) xmalloc (num_args * sizeof (char *));
214
215   /* NOTE: We start at 1 now, not 0.  */
216   for (i = 0, j = 0; i < argc; i++, j++)
217     {
218       arglist[j] = argv[i];
219
220       /* Make sure -lf2c is before the math library, since libf2c
221          itself uses those math routines.  */
222       if (!saw_math && (args[i] & MATHLIB) && library)
223         {
224           --j;
225           saw_math = argv[i];
226         }
227
228       if (!saw_libc && (args[i] & WITHLIBC) && library)
229         {
230           --j;
231           saw_libc = argv[i];
232         }
233
234       /* Wrap foo.c and foo.i files in a language specification to
235          force the gcc compiler driver to run cc1plus on them.  */
236       if (args[i] & LANGSPEC)
237         {
238           int len = strlen (argv[i]);
239           switch (argv[i][len - 1])
240             {
241             case 'F':
242               arglist[j++] = "-xf77-cpp-input";
243               break;
244             case 'r':
245               /* Don't do ratfor for ".for".  */
246               if (argv[i][len - 2] == '.')
247                 {
248                   arglist[j++] = "-xratfor";
249                   break;
250                 }
251             default:
252               arglist[j++] = "-xf77";
253               break;
254             }
255           arglist[j++] = argv[i];
256           arglist[j] = "-xnone";
257         }
258   }
259
260   /* Add `-lf2c' if we haven't already done so.  */
261   if (library)
262     {
263       arglist[j++] = FORTRAN_LIBRARY;
264       added_libraries++;
265     }
266   if (saw_math)
267     arglist[j++] = saw_math;
268   else if (library)
269     {
270       arglist[j++] = MATH_LIBRARY;
271       added_libraries++;
272     }
273   if (saw_libc)
274     arglist[j++] = saw_libc;
275
276   arglist[j] = NULL;
277
278   *in_argc = j;
279   *in_argv = arglist;
280   *in_added_libraries = added_libraries;
281 }