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