g++spec.c revision 90075
169626Sru/* Specific flags and argument handling of the C++ front-end.
2104862Sru   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
369626Sru
4114402SruThis file is part of GNU CC.
5104862Sru
669626SruGNU CC is free software; you can redistribute it and/or modify
769626Sruit under the terms of the GNU General Public License as published by
8114402Sruthe Free Software Foundation; either version 2, or (at your option)
969626Sruany later version.
10104862Sru
1169626SruGNU CC is distributed in the hope that it will be useful,
1269626Srubut WITHOUT ANY WARRANTY; without even the implied warranty of
1369626SruMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1469626SruGNU General Public License for more details.
15104862Sru
1669626SruYou should have received a copy of the GNU General Public License
1769626Srualong with GNU CC; see the file COPYING.  If not, write to
1869626Sruthe Free Software Foundation, 59 Temple Place - Suite 330,
1969626SruBoston, MA 02111-1307, USA.  */
2069626Sru
2169626Sru#include "config.h"
2269626Sru#include "system.h"
2369626Sru#include "gcc.h"
2469626Sru
2569626Sru/* This bit is set if we saw a `-xfoo' language specification.  */
26104862Sru#define LANGSPEC	(1<<1)
27104862Sru/* This bit is set if they did `-lm' or `-lmath'.  */
2869626Sru#define MATHLIB		(1<<2)
2975584Sru/* This bit is set if they did `-lc'.  */
3069626Sru#define WITHLIBC	(1<<3)
3169626Sru
3269626Sru#ifndef MATH_LIBRARY
3369626Sru#define MATH_LIBRARY "-lm"
3469626Sru#endif
35104862Sru
36104862Sru#ifndef LIBSTDCXX
37104862Sru#define LIBSTDCXX "-lstdc++"
38104862Sru#endif
39104862Sru
40104862Sruvoid
41104862Srulang_specific_driver (in_argc, in_argv, in_added_libraries)
42104862Sru     int *in_argc;
43104862Sru     const char *const **in_argv;
44104862Sru     int *in_added_libraries;
45104862Sru{
46104862Sru  int i, j;
47104862Sru
48104862Sru  /* If non-zero, the user gave us the `-v' flag.  */
49104862Sru  int saw_verbose_flag = 0;
50104862Sru
51104862Sru  /* This will be 0 if we encounter a situation where we should not
52104862Sru     link in libstdc++.  */
53104862Sru  int library = 1;
54104862Sru
55104862Sru  /* The number of arguments being added to what's in argv, other than
56104862Sru     libraries.  We use this to track the number of times we've inserted
57104862Sru     -xc++/-xnone.  */
58104862Sru  int added = 2;
59104862Sru
60104862Sru  /* Used to track options that take arguments, so we don't go wrapping
61104862Sru     those with -xc++/-xnone.  */
62104862Sru  const char *quote = NULL;
63104862Sru
6469626Sru  /* The new argument list will be contained in this.  */
6569626Sru  const char **arglist;
66104862Sru
67104862Sru  /* Non-zero if we saw a `-xfoo' language specification on the
68104862Sru     command line.  Used to avoid adding our own -xc++ if the user
69104862Sru     already gave a language for the file.  */
70104862Sru  int saw_speclang = 0;
71104862Sru
7269626Sru  /* "-lm" or "-lmath" if it appears on the command line.  */
7369626Sru  const char *saw_math = 0;
74104862Sru
75104862Sru  /* "-lc" if it appears on the command line.  */
76104862Sru  const char *saw_libc = 0;
77104862Sru
78104862Sru  /* An array used to flag each argument that needs a bit set for
79104862Sru     LANGSPEC, MATHLIB, or WITHLIBC.  */
80104862Sru  int *args;
81104862Sru
82104862Sru  /* By default, we throw on the math library if we have one.  */
8369626Sru  int need_math = (MATH_LIBRARY[0] != '\0');
84104862Sru
85104862Sru  /* True if we should add -shared-libgcc to the command-line.  */
8669626Sru  int shared_libgcc = 1;
8769626Sru
88104862Sru  /* The total number of arguments with the new stuff.  */
89104862Sru  int argc;
9069626Sru
91104862Sru  /* The argument list.  */
92104862Sru  const char *const *argv;
9369626Sru
9469626Sru  /* The number of libraries added in.  */
95104862Sru  int added_libraries;
96104862Sru
97104862Sru  /* The total number of arguments with the new stuff.  */
98104862Sru  int num_args = 1;
99104862Sru
100104862Sru  argc = *in_argc;
101104862Sru  argv = *in_argv;
102104862Sru  added_libraries = *in_added_libraries;
103104862Sru
104104862Sru  args = (int *) xcalloc (argc, sizeof (int));
105104862Sru
106104862Sru  for (i = 1; i < argc; i++)
10769626Sru    {
10869626Sru      /* If the previous option took an argument, we swallow it here.  */
109104862Sru      if (quote)
110104862Sru	{
111104862Sru	  quote = NULL;
112104862Sru	  continue;
113104862Sru	}
114104862Sru
115104862Sru      /* We don't do this anymore, since we don't get them with minus
116104862Sru	 signs on them.  */
117104862Sru      if (argv[i][0] == '\0' || argv[i][1] == '\0')
118104862Sru	continue;
119104862Sru
120104862Sru      if (argv[i][0] == '-')
121104862Sru	{
122104862Sru	  if (library != 0 && (strcmp (argv[i], "-nostdlib") == 0
12369626Sru			       || strcmp (argv[i], "-nodefaultlibs") == 0))
12469626Sru	    {
125104862Sru	      library = 0;
126104862Sru	    }
127104862Sru	  else if (strcmp (argv[i], "-lm") == 0
128104862Sru		   || strcmp (argv[i], "-lmath") == 0
12975584Sru		   || strcmp (argv[i], MATH_LIBRARY) == 0
13075584Sru#ifdef ALT_LIBM
131104862Sru		   || strcmp (argv[i], ALT_LIBM) == 0
132104862Sru#endif
133104862Sru		  )
134104862Sru	    {
135104862Sru	      args[i] |= MATHLIB;
136104862Sru	      need_math = 0;
137104862Sru	    }
138104862Sru	  else if (strcmp (argv[i], "-lc") == 0)
139104862Sru	    args[i] |= WITHLIBC;
140104862Sru	  else if (strcmp (argv[i], "-v") == 0)
141104862Sru	    {
142104862Sru	      saw_verbose_flag = 1;
143104862Sru	      if (argc == 2)
14469626Sru		{
14569626Sru		  /* If they only gave us `-v', don't try to link
14669626Sru		     in libg++.  */
147104862Sru		  library = 0;
14869626Sru		}
14969626Sru	    }
150104862Sru	  else if (strncmp (argv[i], "-x", 2) == 0)
151104862Sru	    saw_speclang = 1;
152104862Sru	  else if (((argv[i][2] == '\0'
15369626Sru		     && (char *)strchr ("bBVDUoeTuIYmLiA", argv[i][1]) != NULL)
15469626Sru		    || strcmp (argv[i], "-Xlinker") == 0
15569626Sru		    || strcmp (argv[i], "-Tdata") == 0))
156104862Sru	    quote = argv[i];
15769626Sru	  else if (library != 0 && ((argv[i][2] == '\0'
15869626Sru		     && (char *) strchr ("cSEM", argv[i][1]) != NULL)
15969626Sru		    || strcmp (argv[i], "-MM") == 0
16069626Sru		    || strcmp (argv[i], "-fsyntax-only") == 0))
16169626Sru	    {
162104862Sru	      /* Don't specify libraries if we won't link, since that would
16369626Sru		 cause a warning.  */
16469626Sru	      library = 0;
165104862Sru	      added -= 2;
166104862Sru	    }
167104862Sru	  else if (strcmp (argv[i], "-static-libgcc") == 0
168104862Sru		   || strcmp (argv[i], "-static") == 0)
169104862Sru	    shared_libgcc = 0;
170104862Sru	  else
171104862Sru	    /* Pass other options through.  */
172104862Sru	    continue;
173104862Sru	}
174104862Sru      else
17569626Sru	{
176104862Sru	  int len;
177104862Sru
178104862Sru	  if (saw_speclang)
179104862Sru	    {
180104862Sru	      saw_speclang = 0;
181104862Sru	      continue;
182104862Sru	    }
18369626Sru
184104862Sru	  /* If the filename ends in .c or .i, put options around it.
185104862Sru	     But not if a specified -x option is currently active.  */
186104862Sru	  len = strlen (argv[i]);
187104862Sru	  if (len > 2
18869626Sru	      && (argv[i][len - 1] == 'c' || argv[i][len - 1] == 'i')
189104862Sru	      && argv[i][len - 2] == '.')
190104862Sru	    {
191104862Sru	      args[i] |= LANGSPEC;
192104862Sru	      added += 2;
193104862Sru	    }
194104862Sru	}
195104862Sru    }
196104862Sru
197104862Sru  if (quote)
198104862Sru    fatal ("argument to `%s' missing\n", quote);
199104862Sru
200104862Sru  /* If we know we don't have to do anything, bail now.  */
201104862Sru  if (! added && ! library)
202104862Sru    {
203104862Sru      free (args);
204104862Sru      return;
205104862Sru    }
206104862Sru
207104862Sru  /* There's no point adding -shared-libgcc if we don't have a shared
208104862Sru     libgcc.  */
209104862Sru#ifndef ENABLE_SHARED_LIBGCC
210104862Sru  shared_libgcc = 0;
211104862Sru#endif
212104862Sru
213104862Sru  /* Make sure to have room for the trailing NULL argument.  */
214104862Sru  num_args = argc + added + need_math + shared_libgcc + 1;
215104862Sru  arglist = (const char **) xmalloc (num_args * sizeof (char *));
216104862Sru
21769626Sru  i = 0;
218104862Sru  j = 0;
219104862Sru
220104862Sru  /* Copy the 0th argument, i.e., the name of the program itself.  */
221104862Sru  arglist[i++] = argv[j++];
222104862Sru
223104862Sru  /* NOTE: We start at 1 now, not 0.  */
224104862Sru  while (i < argc)
225104862Sru    {
226104862Sru      arglist[j] = argv[i];
227104862Sru
228104862Sru      /* Make sure -lstdc++ is before the math library, since libstdc++
229104862Sru	 itself uses those math routines.  */
230104862Sru      if (!saw_math && (args[i] & MATHLIB) && library)
231104862Sru	{
232104862Sru	  --j;
233104862Sru	  saw_math = argv[i];
234104862Sru	}
235104862Sru
236104862Sru      if (!saw_libc && (args[i] & WITHLIBC) && library)
237104862Sru	{
238104862Sru	  --j;
23969626Sru	  saw_libc = argv[i];
240104862Sru	}
24169626Sru
242104862Sru      /* Wrap foo.c and foo.i files in a language specification to
243104862Sru	 force the gcc compiler driver to run cc1plus on them.  */
24469626Sru      if (args[i] & LANGSPEC)
245104862Sru	{
246104862Sru	  int len = strlen (argv[i]);
247104862Sru	  if (argv[i][len - 1] == 'i')
248104862Sru	    arglist[j++] = "-xc++-cpp-output";
249104862Sru	  else
250104862Sru	    arglist[j++] = "-xc++";
251104862Sru	  arglist[j++] = argv[i];
252104862Sru	  arglist[j] = "-xnone";
253104862Sru	}
254104862Sru
255104862Sru      i++;
256104862Sru      j++;
257104862Sru    }
258104862Sru
25969626Sru  /* Add `-lstdc++' if we haven't already done so.  */
260104862Sru  if (library)
261104862Sru    {
262104862Sru      arglist[j++] = LIBSTDCXX;
263104862Sru      added_libraries++;
264104862Sru    }
265104862Sru  if (saw_math)
266104862Sru    arglist[j++] = saw_math;
267104862Sru  else if (library && need_math)
268104862Sru    {
269104862Sru      arglist[j++] = MATH_LIBRARY;
270104862Sru      added_libraries++;
271104862Sru    }
272114402Sru  if (saw_libc)
273104862Sru    arglist[j++] = saw_libc;
274104862Sru  if (shared_libgcc)
275104862Sru    arglist[j++] = "-shared-libgcc";
276104862Sru
277104862Sru  arglist[j] = NULL;
278104862Sru
279104862Sru  *in_argc = j;
280104862Sru  *in_argv = arglist;
281104862Sru  *in_added_libraries = added_libraries;
282104862Sru}
283104862Sru
284104862Sru/* Called before linking.  Returns 0 on success and -1 on failure. */
285104862Sruint lang_specific_pre_link ()  /* Not used for C++. */
286104862Sru{
287104862Sru  return 0;
288104862Sru}
289104862Sru
290104862Sru/* Number of extra output files that lang_specific_pre_link may generate. */
291104862Sruint lang_specific_extra_outfiles = 0;  /* Not used for C++. */
292104862Sru