OSDN Git Service

cp/:
[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, 1999, 2000, 2001, 2002, 2003, 2004,
3    2007, 2008, 2009 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 3, 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 COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "gcc.h"
26
27 /* This bit is set if we saw a `-xfoo' language specification.  */
28 #define LANGSPEC        (1<<1)
29 /* This bit is set if they did `-lm' or `-lmath'.  */
30 #define MATHLIB         (1<<2)
31 /* This bit is set if they did `-lc'.  */
32 #define WITHLIBC        (1<<3)
33 /* Skip this option.  */
34 #define SKIPOPT         (1<<4)
35
36 #ifndef MATH_LIBRARY
37 #define MATH_LIBRARY "-lm"
38 #endif
39 #ifndef MATH_LIBRARY_PROFILE
40 #define MATH_LIBRARY_PROFILE MATH_LIBRARY
41 #endif
42
43 #ifndef LIBSTDCXX
44 #define LIBSTDCXX "-lstdc++"
45 #endif
46 #ifndef LIBSTDCXX_PROFILE
47 #define LIBSTDCXX_PROFILE LIBSTDCXX
48 #endif
49 #ifndef LIBSTDCXX_STATIC
50 #define LIBSTDCXX_STATIC NULL
51 #endif
52
53 void
54 lang_specific_driver (int *in_argc, const char *const **in_argv,
55                       int *in_added_libraries)
56 {
57   int i, j;
58
59   /* If nonzero, the user gave us the `-p' or `-pg' flag.  */
60   int saw_profile_flag = 0;
61
62   /* If nonzero, the user gave us the `-v' flag.  */
63   int saw_verbose_flag = 0;
64
65   /* What do with libstdc++:
66      -1 means we should not link in libstdc++
67      0  means we should link in libstdc++ if it is needed
68      1  means libstdc++ is needed and should be linked in.
69      2  means libstdc++ is needed and should be linked statically.  */
70   int library = 0;
71
72   /* The number of arguments being added to what's in argv, other than
73      libraries.  We use this to track the number of times we've inserted
74      -xc++/-xnone.  */
75   int added = 0;
76
77   /* Used to track options that take arguments, so we don't go wrapping
78      those with -xc++/-xnone.  */
79   const char *quote = NULL;
80
81   /* The new argument list will be contained in this.  */
82   const char **arglist;
83
84   /* Nonzero if we saw a `-xfoo' language specification on the
85      command line.  Used to avoid adding our own -xc++ if the user
86      already gave a language for the file.  */
87   int saw_speclang = 0;
88
89   /* "-lm" or "-lmath" if it appears on the command line.  */
90   const char *saw_math = 0;
91
92   /* "-lc" if it appears on the command line.  */
93   const char *saw_libc = 0;
94
95   /* An array used to flag each argument that needs a bit set for
96      LANGSPEC, MATHLIB, or WITHLIBC.  */
97   int *args;
98
99   /* By default, we throw on the math library if we have one.  */
100   int need_math = (MATH_LIBRARY[0] != '\0');
101
102   /* True if we saw -static.  */
103   int static_link = 0;
104
105   /* True if we should add -shared-libgcc to the command-line.  */
106   int shared_libgcc = 1;
107
108   /* The total number of arguments with the new stuff.  */
109   int argc;
110
111   /* The argument list.  */
112   const char *const *argv;
113
114   /* The number of libraries added in.  */
115   int added_libraries;
116
117   /* The total number of arguments with the new stuff.  */
118   int num_args = 1;
119
120   argc = *in_argc;
121   argv = *in_argv;
122   added_libraries = *in_added_libraries;
123
124   args = XCNEWVEC (int, argc);
125
126   for (i = 1; i < argc; i++)
127     {
128       /* If the previous option took an argument, we swallow it here.  */
129       if (quote)
130         {
131           quote = NULL;
132           continue;
133         }
134
135       /* We don't do this anymore, since we don't get them with minus
136          signs on them.  */
137       if (argv[i][0] == '\0' || argv[i][1] == '\0')
138         continue;
139
140       if (argv[i][0] == '-')
141         {
142           if (strcmp (argv[i], "-nostdlib") == 0
143               || strcmp (argv[i], "-nodefaultlibs") == 0)
144             {
145               library = -1;
146             }
147           else if (strcmp (argv[i], MATH_LIBRARY) == 0)
148             {
149               args[i] |= MATHLIB;
150               need_math = 0;
151             }
152           else if (strcmp (argv[i], "-lc") == 0)
153             args[i] |= WITHLIBC;
154           else if (strcmp (argv[i], "-pg") == 0 || strcmp (argv[i], "-p") == 0)
155             saw_profile_flag++;
156           else if (strcmp (argv[i], "-v") == 0)
157             saw_verbose_flag = 1;
158           else if (strncmp (argv[i], "-x", 2) == 0)
159             {
160               const char * arg;
161               if (argv[i][2] != '\0')
162                 arg = argv[i]+2;
163               else if ((argv[i+1]) != NULL)
164                 /* We need to swallow arg on next loop.  */
165                 quote = arg = argv[i+1];
166               else  /* Error condition, message will be printed later.  */
167                 arg = "";
168               if (library == 0
169                   && (strcmp (arg, "c++") == 0
170                       || strcmp (arg, "c++-cpp-output") == 0
171                       || strcmp (arg, "objective-c++") == 0
172                       || strcmp (arg, "objective-c++-cpp-output") == 0))
173                 library = 1;
174                 
175               saw_speclang = 1;
176             }
177           else if (strcmp (argv[i], "-ObjC++") == 0)
178             {
179               if (library == 0)
180                 library = 1;
181               saw_speclang = 1;
182             }
183           /* Arguments that go directly to the linker might be .o files,
184              or something, and so might cause libstdc++ to be needed.  */
185           else if (strcmp (argv[i], "-Xlinker") == 0)
186             {
187               quote = argv[i];
188               if (library == 0)
189                 library = 1;
190             }
191           else if (strncmp (argv[i], "-Wl,", 4) == 0)
192             library = (library == 0) ? 1 : library;
193           /* Unrecognized libraries (e.g. -lfoo) may require libstdc++.  */
194           else if (strncmp (argv[i], "-l", 2) == 0)
195             library = (library == 0) ? 1 : library;
196           else if (((argv[i][2] == '\0'
197                      && strchr ("bBVDUoeTuIYmLiA", argv[i][1]) != NULL)
198                     || strcmp (argv[i], "-Tdata") == 0))
199             quote = argv[i];
200           else if ((argv[i][2] == '\0'
201                     && strchr ("cSEM", argv[i][1]) != NULL)
202                    || strcmp (argv[i], "-MM") == 0
203                    || strcmp (argv[i], "-fsyntax-only") == 0)
204             {
205               /* Don't specify libraries if we won't link, since that would
206                  cause a warning.  */
207               library = -1;
208             }
209           else if (strcmp (argv[i], "-static") == 0)
210             static_link = 1;
211           else if (strcmp (argv[i], "-static-libgcc") == 0)
212             shared_libgcc = 0;
213           else if (strcmp (argv[i], "-static-libstdc++") == 0)
214             {
215               library = library >= 0 ? 2 : library;
216               args[i] |= SKIPOPT;
217             }
218           else if (DEFAULT_WORD_SWITCH_TAKES_ARG (&argv[i][1]))
219             i++;
220           else
221             /* Pass other options through.  */
222             continue;
223         }
224       else
225         {
226           int len;
227
228           if (saw_speclang)
229             {
230               saw_speclang = 0;
231               continue;
232             }
233
234           /* If the filename ends in .[chi], put options around it.
235              But not if a specified -x option is currently active.  */
236           len = strlen (argv[i]);
237           if (len > 2
238               && (argv[i][len - 1] == 'c'
239                   || argv[i][len - 1] == 'i'
240                   || argv[i][len - 1] == 'h')
241               && argv[i][len - 2] == '.')
242             {
243               args[i] |= LANGSPEC;
244               added += 2;
245             }
246
247           /* If we don't know that this is a header file, we might
248              need to be linking in the libraries.  */
249           if (library == 0)
250             {
251               if ((len <= 2 || strcmp (argv[i] + (len - 2), ".H") != 0)
252                   && (len <= 2 || strcmp (argv[i] + (len - 2), ".h") != 0)
253                   && (len <= 4 || strcmp (argv[i] + (len - 4), ".hpp") != 0)
254                   && (len <= 3 || strcmp (argv[i] + (len - 3), ".hp") != 0)
255                   && (len <= 4 || strcmp (argv[i] + (len - 4), ".hxx") != 0)
256                   && (len <= 4 || strcmp (argv[i] + (len - 4), ".h++") != 0)
257                   && (len <= 4 || strcmp (argv[i] + (len - 4), ".HPP") != 0)
258                   && (len <= 4 || strcmp (argv[i] + (len - 4), ".tcc") != 0)
259                   && (len <= 3 || strcmp (argv[i] + (len - 3), ".hh") != 0))
260                 library = 1;
261             }
262         }
263     }
264
265   if (quote)
266     fatal ("argument to '%s' missing\n", quote);
267
268   /* There's no point adding -shared-libgcc if we don't have a shared
269      libgcc.  */
270 #ifndef ENABLE_SHARED_LIBGCC
271   shared_libgcc = 0;
272 #endif
273
274   /* Make sure to have room for the trailing NULL argument.
275      Add one for shared_libgcc or extra static library.  */
276   num_args = argc + added + need_math + (library > 0) * 4 + 1;
277   arglist = XNEWVEC (const char *, num_args);
278
279   i = 0;
280   j = 0;
281
282   /* Copy the 0th argument, i.e., the name of the program itself.  */
283   arglist[i++] = argv[j++];
284
285   /* NOTE: We start at 1 now, not 0.  */
286   while (i < argc)
287     {
288       arglist[j] = argv[i];
289
290       /* Make sure -lstdc++ is before the math library, since libstdc++
291          itself uses those math routines.  */
292       if (!saw_math && (args[i] & MATHLIB) && library > 0)
293         {
294           --j;
295           saw_math = argv[i];
296         }
297
298       if (!saw_libc && (args[i] & WITHLIBC) && library > 0)
299         {
300           --j;
301           saw_libc = argv[i];
302         }
303
304       /* Wrap foo.[chi] files in a language specification to
305          force the gcc compiler driver to run cc1plus on them.  */
306       if (args[i] & LANGSPEC)
307         {
308           int len = strlen (argv[i]);
309           switch (argv[i][len - 1])
310             {
311             case 'c':
312               arglist[j++] = "-xc++";
313               break;
314             case 'i':
315               arglist[j++] = "-xc++-cpp-output";
316               break;
317             case 'h':
318               arglist[j++] = "-xc++-header";
319               break;
320             default:
321               gcc_unreachable ();
322             }
323           arglist[j++] = argv[i];
324           arglist[j] = "-xnone";
325         }
326
327       if ((args[i] & SKIPOPT) != 0)
328         --j;
329
330       i++;
331       j++;
332     }
333
334   /* Add `-lstdc++' if we haven't already done so.  */
335   if (library > 0)
336     {
337 #ifdef HAVE_LD_STATIC_DYNAMIC
338       if (library > 1 && !static_link)
339         {
340           arglist[j] = "-Wl,-Bstatic";
341           j++;
342         }
343 #endif
344       arglist[j] = saw_profile_flag ? LIBSTDCXX_PROFILE : LIBSTDCXX;
345       if (arglist[j][0] != '-' || arglist[j][1] == 'l')
346         added_libraries++;
347       j++;
348       /* Add target-dependent static library, if necessary.  */
349       if ((static_link || library > 1) && LIBSTDCXX_STATIC != NULL)
350         {
351           arglist[j] = LIBSTDCXX_STATIC;
352           if (arglist[j][0] != '-' || arglist[j][1] == 'l')
353             added_libraries++;
354           j++;
355         }
356 #ifdef HAVE_LD_STATIC_DYNAMIC
357       if (library > 1 && !static_link)
358         {
359           arglist[j] = "-Wl,-Bdynamic";
360           j++;
361         }
362 #endif
363     }
364   if (saw_math)
365     arglist[j++] = saw_math;
366   else if (library > 0 && need_math)
367     {
368       arglist[j] = saw_profile_flag ? MATH_LIBRARY_PROFILE : MATH_LIBRARY;
369       if (arglist[j][0] != '-' || arglist[j][1] == 'l')
370         added_libraries++;
371       j++;
372     }
373   if (saw_libc)
374     arglist[j++] = saw_libc;
375   if (shared_libgcc && !static_link)
376     arglist[j++] = "-shared-libgcc";
377
378   arglist[j] = NULL;
379
380   *in_argc = j;
381   *in_argv = arglist;
382   *in_added_libraries = added_libraries;
383 }
384
385 /* Called before linking.  Returns 0 on success and -1 on failure.  */
386 int lang_specific_pre_link (void)  /* Not used for C++.  */
387 {
388   return 0;
389 }
390
391 /* Number of extra output files that lang_specific_pre_link may generate.  */
392 int lang_specific_extra_outfiles = 0;  /* Not used for C++.  */