xref: /openbsd-src/gnu/usr.bin/gcc/gcc/cp/g++spec.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /* Specific flags and argument handling of the C++ front-end.
2    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 
4 This file is part of GNU CC.
5 
6 GNU CC 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 2, or (at your option)
9 any later version.
10 
11 GNU CC 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 GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20 
21 #include "config.h"
22 #include "system.h"
23 #include "gcc.h"
24 
25 /* This bit is set if we saw a `-xfoo' language specification.  */
26 #define LANGSPEC	(1<<1)
27 /* This bit is set if they did `-lm' or `-lmath'.  */
28 #define MATHLIB		(1<<2)
29 /* This bit is set if they did `-lc'.  */
30 #define WITHLIBC	(1<<3)
31 
32 #ifndef MATH_LIBRARY
33 #define MATH_LIBRARY "-lm"
34 #endif
35 #ifndef MATH_LIBRARY_PROFILE
36 #define MATH_LIBRARY_PROFILE "-lm"
37 #endif
38 
39 #ifndef LIBSTDCXX
40 #define LIBSTDCXX "-lstdc++"
41 #endif
42 #ifndef LIBSTDCXX_PROFILE
43 #define LIBSTDCXX_PROFILE "-lstdc++"
44 #endif
45 #ifndef LIBSUPCXX
46 #define LIBSUPCXX "-lsupc++"
47 #endif
48 #ifndef LIBSUPCXX_PROFILE
49 #define LIBSUPCXX_PROFILE "-lsupc++"
50 #endif
51 
52 void
53 lang_specific_driver (in_argc, in_argv, in_added_libraries)
54      int *in_argc;
55      const char *const **in_argv;
56      int *in_added_libraries;
57 {
58   int i, j;
59 
60   /* If nonzero, the user gave us the `-p' or `-pg' flag.  */
61   int saw_profile_flag = 0;
62 
63   /* If nonzero, the user gave us the `-v' flag.  */
64   int saw_verbose_flag = 0;
65 
66   /* This will be 0 if we encounter a situation where we should not
67      link in libstdc++.  */
68   int library = 1;
69 
70   /* This will be 1 if we encounter a situation where we should link
71      libsupc++. */
72   int libsupcxx = 0;
73 
74   /* The number of arguments being added to what's in argv, other than
75      libraries.  We use this to track the number of times we've inserted
76      -xc++/-xnone.  */
77   int added = 2;
78 
79   /* Used to track options that take arguments, so we don't go wrapping
80      those with -xc++/-xnone.  */
81   const char *quote = NULL;
82 
83   /* The new argument list will be contained in this.  */
84   const char **arglist;
85 
86   /* Nonzero if we saw a `-xfoo' language specification on the
87      command line.  Used to avoid adding our own -xc++ if the user
88      already gave a language for the file.  */
89   int saw_speclang = 0;
90 
91   /* "-lm" or "-lmath" if it appears on the command line.  */
92   const char *saw_math = 0;
93 
94   /* "-lc" if it appears on the command line.  */
95   const char *saw_libc = 0;
96 
97   /* An array used to flag each argument that needs a bit set for
98      LANGSPEC, MATHLIB, 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   /* True if we should add -shared-libgcc to the command-line.  */
105   int shared_libgcc = 1;
106 
107   /* The total number of arguments with the new stuff.  */
108   int argc;
109 
110   /* The argument list.  */
111   const char *const *argv;
112 
113   /* The number of libraries added in.  */
114   int added_libraries;
115 
116   /* The total number of arguments with the new stuff.  */
117   int num_args = 1;
118 
119   argc = *in_argc;
120   argv = *in_argv;
121   added_libraries = *in_added_libraries;
122 
123   args = (int *) xcalloc (argc, sizeof (int));
124 
125   for (i = 1; i < argc; i++)
126     {
127       /* If the previous option took an argument, we swallow it here.  */
128       if (quote)
129 	{
130 	  quote = NULL;
131 	  continue;
132 	}
133 
134       /* We don't do this anymore, since we don't get them with minus
135 	 signs on them.  */
136       if (argv[i][0] == '\0' || argv[i][1] == '\0')
137 	continue;
138 
139       if (argv[i][0] == '-')
140 	{
141 	  if ((strcmp (argv[i], "-nostdlib") == 0
142 			       || strcmp (argv[i], "-nodefaultlibs") == 0))
143 	    {
144 	      library = 0;
145 	      libsupcxx = -1;
146 	    }
147 	  else if (strcmp (argv[i], "-shared") == 0)
148 	    {
149 	      library = 0;
150 	      if (libsupcxx == 0)
151 	        libsupcxx = 1;
152 	    }
153 	  else if (strcmp (argv[i], "-lm") == 0
154 		   || strcmp (argv[i], "-lmath") == 0
155 		   || strcmp (argv[i], MATH_LIBRARY) == 0
156 #ifdef ALT_LIBM
157 		   || strcmp (argv[i], ALT_LIBM) == 0
158 #endif
159 		  )
160 	    {
161 	      args[i] |= MATHLIB;
162 	      need_math = 0;
163 	    }
164 	  else if (strcmp (argv[i], "-lc") == 0)
165 	    args[i] |= WITHLIBC;
166 	  else if (strcmp (argv[i], "-lstdc++") == 0)
167 	    libsupcxx = -1;
168 	  else if (strcmp (argv[i], "-pg") == 0 || strcmp (argv[i], "-p") == 0)
169 	    saw_profile_flag++;
170 	  else if (strcmp (argv[i], "-v") == 0)
171 	    {
172 	      saw_verbose_flag = 1;
173 	      if (argc == 2)
174 		{
175 		  /* If they only gave us `-v', don't try to link
176 		     in libg++.  */
177 		  library = 0;
178 		}
179 	    }
180 	  else if (strncmp (argv[i], "-x", 2) == 0)
181 	    saw_speclang = 1;
182 	  else if (((argv[i][2] == '\0'
183 		     && (char *)strchr ("bBVDUoeTuIYmLiA", argv[i][1]) != NULL)
184 		    || strcmp (argv[i], "-Xlinker") == 0
185 		    || strcmp (argv[i], "-Tdata") == 0))
186 	    quote = argv[i];
187 	  else if (library != 0 && ((argv[i][2] == '\0'
188 		     && (char *) strchr ("cSEM", argv[i][1]) != NULL)
189 		    || strcmp (argv[i], "-MM") == 0
190 		    || strcmp (argv[i], "-fsyntax-only") == 0))
191 	    {
192 	      /* Don't specify libraries if we won't link, since that would
193 		 cause a warning.  */
194 	      library = 0;
195 	      added -= 2;
196 	    }
197 	  else if (strcmp (argv[i], "-static-libgcc") == 0
198 		   || strcmp (argv[i], "-static") == 0)
199 	    shared_libgcc = 0;
200 	  else if (DEFAULT_WORD_SWITCH_TAKES_ARG (&argv[i][1]))
201 	    i++;
202 	  else
203 	    /* Pass other options through.  */
204 	    continue;
205 	}
206       else
207 	{
208 	  int len;
209 
210 	  if (saw_speclang)
211 	    {
212 	      saw_speclang = 0;
213 	      continue;
214 	    }
215 
216 	  /* If the filename ends in .c or .i, put options around it.
217 	     But not if a specified -x option is currently active.  */
218 	  len = strlen (argv[i]);
219 	  if (len > 2
220 	      && (argv[i][len - 1] == 'c' || argv[i][len - 1] == 'i')
221 	      && argv[i][len - 2] == '.')
222 	    {
223 	      args[i] |= LANGSPEC;
224 	      added += 2;
225 	    }
226 	}
227     }
228 
229   if (quote)
230     fatal ("argument to `%s' missing\n", quote);
231 
232   /* If we know we don't have to do anything, bail now.  */
233   if (! added && ! library)
234     {
235       free (args);
236       return;
237     }
238 
239   /* There's no point adding -shared-libgcc if we don't have a shared
240      libgcc.  */
241 #ifndef ENABLE_SHARED_LIBGCC
242   shared_libgcc = 0;
243 #endif
244 
245   /* Make sure to have room for the trailing NULL argument.  */
246   num_args = argc + added + need_math + shared_libgcc + 1;
247   arglist = (const char **) xmalloc (num_args * sizeof (char *));
248 
249   i = 0;
250   j = 0;
251 
252   /* Copy the 0th argument, i.e., the name of the program itself.  */
253   arglist[i++] = argv[j++];
254 
255   /* NOTE: We start at 1 now, not 0.  */
256   while (i < argc)
257     {
258       arglist[j] = argv[i];
259 
260       /* Make sure -lstdc++ is before the math library, since libstdc++
261 	 itself uses those math routines.  */
262       if (!saw_math && (args[i] & MATHLIB) && library)
263 	{
264 	  --j;
265 	  saw_math = argv[i];
266 	}
267 
268       if (!saw_libc && (args[i] & WITHLIBC) && library)
269 	{
270 	  --j;
271 	  saw_libc = argv[i];
272 	}
273 
274       /* Wrap foo.c and foo.i files in a language specification to
275 	 force the gcc compiler driver to run cc1plus on them.  */
276       if (args[i] & LANGSPEC)
277 	{
278 	  int len = strlen (argv[i]);
279 	  if (argv[i][len - 1] == 'i')
280 	    arglist[j++] = "-xc++-cpp-output";
281 	  else
282 	    arglist[j++] = "-xc++";
283 	  arglist[j++] = argv[i];
284 	  arglist[j] = "-xnone";
285 	}
286 
287       i++;
288       j++;
289     }
290 
291   /* Add -lsupc++ for shared. */
292   if (libsupcxx == 1)
293     {
294       arglist[j++] = saw_profile_flag ? LIBSUPCXX_PROFILE : LIBSUPCXX;
295       added_libraries++;
296     }
297   /* Add `-lstdc++' if we haven't already done so.  */
298   if (library)
299     {
300       arglist[j++] = saw_profile_flag ? LIBSTDCXX_PROFILE : LIBSTDCXX;
301       added_libraries++;
302 #ifdef USE_LIBUNWIND_EXCEPTIONS
303 # ifndef LIBUNWIND
304 #  define LIBUNWIND "-lunwind"
305 # endif
306       arglist[j++] = LIBUNWIND;
307       added_libraries++;
308 #endif
309     }
310   if (saw_math)
311     arglist[j++] = saw_math;
312   else if (library && need_math)
313     {
314       arglist[j++] = saw_profile_flag ? MATH_LIBRARY_PROFILE : MATH_LIBRARY;
315       added_libraries++;
316     }
317   if (saw_libc)
318     arglist[j++] = saw_libc;
319   if (shared_libgcc)
320     arglist[j++] = "-shared-libgcc";
321 
322   arglist[j] = NULL;
323 
324   *in_argc = j;
325   *in_argv = arglist;
326   *in_added_libraries = added_libraries;
327 }
328 
329 /* Called before linking.  Returns 0 on success and -1 on failure.  */
330 int lang_specific_pre_link ()  /* Not used for C++.  */
331 {
332   return 0;
333 }
334 
335 /* Number of extra output files that lang_specific_pre_link may generate.  */
336 int lang_specific_extra_outfiles = 0;  /* Not used for C++.  */
337 
338 /* Table of language-specific spec functions.  */
339 const struct spec_function lang_specific_spec_functions[] =
340 {
341   { 0, 0 }
342 };
343