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