OSDN Git Service

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