1/* Specific flags and argument handling of the C++ front-end.
2   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3   Free Software Foundation, Inc.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING.  If not, write to
19the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20Boston, MA 02110-1301, USA.  */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
26#include "gcc.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
35#ifndef MATH_LIBRARY
36#define MATH_LIBRARY "-lm"
37#endif
38#ifndef MATH_LIBRARY_PROFILE
39#define MATH_LIBRARY_PROFILE MATH_LIBRARY
40#endif
41
42#ifndef LIBSTDCXX
43#define LIBSTDCXX "-lstdc++"
44#endif
45#ifndef LIBSTDCXX_PROFILE
46#define LIBSTDCXX_PROFILE LIBSTDCXX
47#endif
48
49void
50lang_specific_driver (int *in_argc, const char *const **in_argv,
51		      int *in_added_libraries)
52{
53  int i, j;
54
55  /* If nonzero, the user gave us the `-p' or `-pg' flag.  */
56  int saw_profile_flag = 0;
57
58  /* If nonzero, the user gave us the `-v' flag.  */
59  int saw_verbose_flag = 0;
60
61  /* This is a tristate:
62     -1 means we should not link in libstdc++
63     0  means we should link in libstdc++ if it is needed
64     1  means libstdc++ is needed and should be linked in.  */
65  int library = 0;
66
67  /* The number of arguments being added to what's in argv, other than
68     libraries.  We use this to track the number of times we've inserted
69     -xc++/-xnone.  */
70  int added = 0;
71
72  /* Used to track options that take arguments, so we don't go wrapping
73     those with -xc++/-xnone.  */
74  const char *quote = NULL;
75
76  /* The new argument list will be contained in this.  */
77  const char **arglist;
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 char *saw_math = 0;
86
87  /* "-lc" if it appears on the command line.  */
88  const char *saw_libc = 0;
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 should add -shared-libgcc to the command-line.  */
98  int shared_libgcc = 1;
99
100  /* The total number of arguments with the new stuff.  */
101  int argc;
102
103  /* The argument list.  */
104  const char *const *argv;
105
106  /* The number of libraries added in.  */
107  int added_libraries;
108
109  /* The total number of arguments with the new stuff.  */
110  int num_args = 1;
111
112  argc = *in_argc;
113  argv = *in_argv;
114  added_libraries = *in_added_libraries;
115
116  args = XCNEWVEC (int, argc);
117
118  for (i = 1; i < argc; i++)
119    {
120      /* If the previous option took an argument, we swallow it here.  */
121      if (quote)
122	{
123	  quote = NULL;
124	  continue;
125	}
126
127      /* We don't do this anymore, since we don't get them with minus
128	 signs on them.  */
129      if (argv[i][0] == '\0' || argv[i][1] == '\0')
130	continue;
131
132      if (argv[i][0] == '-')
133	{
134	  if (strcmp (argv[i], "-nostdlib") == 0
135	      || strcmp (argv[i], "-nodefaultlibs") == 0)
136	    {
137	      library = -1;
138	    }
139	  else if (strcmp (argv[i], MATH_LIBRARY) == 0)
140	    {
141	      args[i] |= MATHLIB;
142	      need_math = 0;
143	    }
144	  else if (strcmp (argv[i], "-lc") == 0)
145	    args[i] |= WITHLIBC;
146	  else if (strcmp (argv[i], "-pg") == 0 || strcmp (argv[i], "-p") == 0)
147	    saw_profile_flag++;
148	  else if (strcmp (argv[i], "-v") == 0)
149	    saw_verbose_flag = 1;
150	  else if (strncmp (argv[i], "-x", 2) == 0)
151	    {
152	      const char * arg;
153	      if (argv[i][2] != '\0')
154		arg = argv[i]+2;
155	      else if ((argv[i+1]) != NULL)
156		/* We need to swallow arg on next loop.  */
157		quote = arg = argv[i+1];
158  	      else  /* Error condition, message will be printed later.  */
159		arg = "";
160	      if (library == 0
161		  && (strcmp (arg, "c++") == 0
162		      || strcmp (arg, "c++-cpp-output") == 0))
163		library = 1;
164
165	      saw_speclang = 1;
166	    }
167	  /* Arguments that go directly to the linker might be .o files,
168	     or something, and so might cause libstdc++ to be needed.  */
169	  else if (strcmp (argv[i], "-Xlinker") == 0)
170	    {
171	      quote = argv[i];
172	      if (library == 0)
173		library = 1;
174	    }
175	  else if (strncmp (argv[i], "-Wl,", 4) == 0)
176	    library = (library == 0) ? 1 : library;
177	  /* Unrecognized libraries (e.g. -lfoo) may require libstdc++.  */
178	  else if (strncmp (argv[i], "-l", 2) == 0)
179	    library = (library == 0) ? 1 : library;
180	  else if (((argv[i][2] == '\0'
181		     && strchr ("bBVDUoeTuIYmLiA", argv[i][1]) != NULL)
182		    || strcmp (argv[i], "-Tdata") == 0))
183	    quote = argv[i];
184	  else if ((argv[i][2] == '\0'
185		    && strchr ("cSEM", argv[i][1]) != NULL)
186		   || strcmp (argv[i], "-MM") == 0
187		   || strcmp (argv[i], "-fsyntax-only") == 0)
188	    {
189	      /* Don't specify libraries if we won't link, since that would
190		 cause a warning.  */
191	      library = -1;
192	    }
193	  else if (strcmp (argv[i], "-static-libgcc") == 0
194		   || strcmp (argv[i], "-static") == 0)
195	    shared_libgcc = 0;
196	  else if (DEFAULT_WORD_SWITCH_TAKES_ARG (&argv[i][1]))
197	    i++;
198	  else
199	    /* Pass other options through.  */
200	    continue;
201	}
202      else
203	{
204	  int len;
205
206	  if (saw_speclang)
207	    {
208	      saw_speclang = 0;
209	      continue;
210	    }
211
212	  /* If the filename ends in .[chi], put options around it.
213	     But not if a specified -x option is currently active.  */
214	  len = strlen (argv[i]);
215	  if (len > 2
216	      && (argv[i][len - 1] == 'c'
217		  || argv[i][len - 1] == 'i'
218		  || argv[i][len - 1] == 'h')
219	      && argv[i][len - 2] == '.')
220	    {
221	      args[i] |= LANGSPEC;
222	      added += 2;
223	    }
224
225	  /* If we don't know that this is a header file, we might
226	     need to be linking in the libraries.  */
227	  if (library == 0)
228	    {
229	      if ((len <= 2 || strcmp (argv[i] + (len - 2), ".H") != 0)
230		  && (len <= 2 || strcmp (argv[i] + (len - 2), ".h") != 0)
231		  && (len <= 3 || strcmp (argv[i] + (len - 3), ".hh") != 0))
232		library = 1;
233	    }
234	}
235    }
236
237  if (quote)
238    fatal ("argument to '%s' missing\n", quote);
239
240  /* If we know we don't have to do anything, bail now.  */
241  if (! added && library <= 0)
242    {
243      free (args);
244      return;
245    }
246
247  /* There's no point adding -shared-libgcc if we don't have a shared
248     libgcc.  */
249#ifndef ENABLE_SHARED_LIBGCC
250  shared_libgcc = 0;
251#endif
252
253  /* Make sure to have room for the trailing NULL argument.  */
254  num_args = argc + added + need_math + shared_libgcc + (library > 0) + 1;
255  arglist = XNEWVEC (const char *, num_args);
256
257  i = 0;
258  j = 0;
259
260  /* Copy the 0th argument, i.e., the name of the program itself.  */
261  arglist[i++] = argv[j++];
262
263  /* NOTE: We start at 1 now, not 0.  */
264  while (i < argc)
265    {
266      arglist[j] = argv[i];
267
268      /* Make sure -lstdc++ is before the math library, since libstdc++
269	 itself uses those math routines.  */
270      if (!saw_math && (args[i] & MATHLIB) && library > 0)
271	{
272	  --j;
273	  saw_math = argv[i];
274	}
275
276      if (!saw_libc && (args[i] & WITHLIBC) && library > 0)
277	{
278	  --j;
279	  saw_libc = argv[i];
280	}
281
282      /* Wrap foo.[chi] files in a language specification to
283	 force the gcc compiler driver to run cc1plus on them.  */
284      if (args[i] & LANGSPEC)
285	{
286	  int len = strlen (argv[i]);
287	  switch (argv[i][len - 1])
288	    {
289	    case 'c':
290	      arglist[j++] = "-xc++";
291	      break;
292	    case 'i':
293	      arglist[j++] = "-xc++-cpp-output";
294	      break;
295	    case 'h':
296	      arglist[j++] = "-xc++-header";
297	      break;
298	    default:
299	      gcc_unreachable ();
300	    }
301	  arglist[j++] = argv[i];
302	  arglist[j] = "-xnone";
303	}
304
305      i++;
306      j++;
307    }
308
309  /* Add `-lstdc++' if we haven't already done so.  */
310  if (library > 0)
311    {
312      arglist[j] = saw_profile_flag ? LIBSTDCXX_PROFILE : LIBSTDCXX;
313      if (arglist[j][0] != '-' || arglist[j][1] == 'l')
314	added_libraries++;
315      j++;
316    }
317  if (saw_math)
318    arglist[j++] = saw_math;
319  else if (library > 0 && need_math)
320    {
321      arglist[j] = saw_profile_flag ? MATH_LIBRARY_PROFILE : MATH_LIBRARY;
322      if (arglist[j][0] != '-' || arglist[j][1] == 'l')
323	added_libraries++;
324      j++;
325    }
326  if (saw_libc)
327    arglist[j++] = saw_libc;
328  if (shared_libgcc)
329    arglist[j++] = "-shared-libgcc";
330
331  arglist[j] = NULL;
332
333  *in_argc = j;
334  *in_argv = arglist;
335  *in_added_libraries = added_libraries;
336}
337
338/* Called before linking.  Returns 0 on success and -1 on failure.  */
339int lang_specific_pre_link (void)  /* Not used for C++.  */
340{
341  return 0;
342}
343
344/* Number of extra output files that lang_specific_pre_link may generate.  */
345int lang_specific_extra_outfiles = 0;  /* Not used for C++.  */
346