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