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