OSDN Git Service

compiler: add error to list of built-in types.
[pf3gnuchains/gcc-fork.git] / gcc / go / gospec.c
1 /* gospec.c -- Specific flags and argument handling of the gcc Go front end.
2    Copyright (C) 2009, 2010, 2011 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 it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 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 `-lpthread'.  */
32 #define THREADLIB       (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 #define THREAD_LIBRARY "pthread"
46 #define THREAD_LIBRARY_PROFILE THREAD_LIBRARY
47
48 #define LIBGO "go"
49 #define LIBGO_PROFILE LIBGO
50 #define LIBGOBEGIN "gobegin"
51
52 void
53 lang_specific_driver (struct cl_decoded_option **in_decoded_options,
54                       unsigned int *in_decoded_options_count,
55                       int *in_added_libraries)
56 {
57   unsigned int i, j;
58
59   /* If true, the user gave us the `-p' or `-pg' flag.  */
60   bool saw_profile_flag = false;
61
62   /* This is a tristate:
63      -1 means we should not link in libgo
64      0  means we should link in libgo if it is needed
65      1  means libgo is needed and should be linked in.
66      2  means libgo is needed and should be linked statically.  */
67   int library = 0;
68
69   /* The new argument list will be contained in this.  */
70   struct cl_decoded_option *new_decoded_options;
71
72   /* "-lm" or "-lmath" if it appears on the command line.  */
73   const struct cl_decoded_option *saw_math = 0;
74
75   /* "-lpthread" if it appears on the command line.  */
76   const struct cl_decoded_option *saw_thread = 0;
77
78   /* "-lc" if it appears on the command line.  */
79   const struct cl_decoded_option *saw_libc = 0;
80
81   /* An array used to flag each argument that needs a bit set for
82      LANGSPEC, MATHLIB, or WITHLIBC.  */
83   int *args;
84
85   /* Whether we need the thread library.  */
86   int need_thread = 0;
87
88   /* By default, we throw on the math library if we have one.  */
89   int need_math = (MATH_LIBRARY[0] != '\0');
90
91   /* True if we saw -static.  */
92   int static_link = 0;
93
94   /* True if we should add -shared-libgcc to the command-line.  */
95   int shared_libgcc = 1;
96
97   /* The total number of arguments with the new stuff.  */
98   unsigned int argc;
99
100   /* The argument list.  */
101   struct cl_decoded_option *decoded_options;
102
103   /* The number of libraries added in.  */
104   int added_libraries;
105
106   /* The total number of arguments with the new stuff.  */
107   int num_args = 1;
108
109   /* Whether the -o option was used.  */
110   bool saw_opt_o = false;
111
112   /* The first input file with an extension of .go.  */
113   const char *first_go_file = NULL;  
114
115   argc = *in_decoded_options_count;
116   decoded_options = *in_decoded_options;
117   added_libraries = *in_added_libraries;
118
119   args = XCNEWVEC (int, argc);
120
121   for (i = 1; i < argc; i++)
122     {
123       const char *arg = decoded_options[i].arg;
124
125       switch (decoded_options[i].opt_index)
126         {
127         case OPT_nostdlib:
128         case OPT_nodefaultlibs:
129           library = -1;
130           break;
131
132         case OPT_l:
133           if (strcmp (arg, MATH_LIBRARY) == 0)
134             {
135               args[i] |= MATHLIB;
136               need_math = 0;
137             }
138           else if (strcmp (arg, THREAD_LIBRARY) == 0)
139             args[i] |= THREADLIB;
140           else if (strcmp (arg, "c") == 0)
141             args[i] |= WITHLIBC;
142           else
143             /* Unrecognized libraries (e.g. -lfoo) may require libgo.  */
144             library = (library == 0) ? 1 : library;
145           break;
146
147         case OPT_pg:
148         case OPT_p:
149           saw_profile_flag = true;
150           break;
151
152         case OPT_x:
153           if (library == 0 && strcmp (arg, "go") == 0)
154             library = 1;
155           break;
156
157         case OPT_Xlinker:
158         case OPT_Wl_:
159           /* Arguments that go directly to the linker might be .o files,
160              or something, and so might cause libgo to be needed.  */
161           if (library == 0)
162             library = 1;
163           break;
164
165         case OPT_c:
166         case OPT_S:
167         case OPT_E:
168         case OPT_M:
169         case OPT_MM:
170         case OPT_fsyntax_only:
171           /* Don't specify libraries if we won't link, since that would
172              cause a warning.  */
173           library = -1;
174           break;
175
176         case OPT_o:
177           saw_opt_o = true;
178           break;
179
180         case OPT_static:
181           static_link = 1;
182           break;
183
184         case OPT_static_libgcc:
185           shared_libgcc = 0;
186           break;
187
188         case OPT_static_libgo:
189           library = library >= 0 ? 2 : library;
190           args[i] |= SKIPOPT;
191           break;
192
193         case OPT_SPECIAL_input_file:
194           if (library == 0)
195             library = 1;
196
197           if (first_go_file == NULL)
198             {
199               int len;
200
201               len = strlen (arg);
202               if (len > 3 && strcmp (arg + len - 3, ".go") == 0)
203                 first_go_file = arg;
204             }
205
206           break;
207         }
208     }
209
210   /* There's no point adding -shared-libgcc if we don't have a shared
211      libgcc.  */
212 #ifndef ENABLE_SHARED_LIBGCC
213   shared_libgcc = 0;
214 #endif
215
216   /* Make sure to have room for the trailing NULL argument.  */
217   num_args = argc + need_math + shared_libgcc + (library > 0) * 5 + 5;
218   new_decoded_options = XNEWVEC (struct cl_decoded_option, num_args);
219
220   i = 0;
221   j = 0;
222
223   /* Copy the 0th argument, i.e., the name of the program itself.  */
224   new_decoded_options[j++] = decoded_options[i++];
225
226   /* If we are linking, pass -fsplit-stack if it is supported.  */
227 #ifdef TARGET_CAN_SPLIT_STACK
228   if (library >= 0)
229     {
230       generate_option (OPT_fsplit_stack, NULL, 1, CL_DRIVER,
231                        &new_decoded_options[j]);
232       j++;
233     }
234 #endif
235
236   /* NOTE: We start at 1 now, not 0.  */
237   while (i < argc)
238     {
239       new_decoded_options[j] = decoded_options[i];
240
241       /* Make sure -lgo is before the math library, since libgo itself
242          uses those math routines.  */
243       if (!saw_math && (args[i] & MATHLIB) && library > 0)
244         {
245           --j;
246           saw_math = &decoded_options[i];
247         }
248
249       if (!saw_thread && (args[i] & THREADLIB) && library > 0)
250         {
251           --j;
252           saw_thread = &decoded_options[i];
253         }
254
255       if (!saw_libc && (args[i] & WITHLIBC) && library > 0)
256         {
257           --j;
258           saw_libc = &decoded_options[i];
259         }
260
261       if ((args[i] & SKIPOPT) != 0)
262         --j;
263
264       i++;
265       j++;
266     }
267
268   /* If we are not linking, add a -o option.  This is because we need
269      the driver to pass all .go files to go1.  Without a -o option the
270      driver will invoke go1 separately for each input file.  */
271   if (library < 0 && first_go_file != NULL && !saw_opt_o)
272     {
273       const char *base;
274       int baselen;
275       int alen;
276       char *out;
277
278       base = lbasename (first_go_file);
279       baselen = strlen (base) - 3;
280       alen = baselen + 3;
281       out = XNEWVEC (char, alen);
282       memcpy (out, base, baselen);
283       /* The driver will convert .o to some other suffix if
284          appropriate.  */
285       out[baselen] = '.';
286       out[baselen + 1] = 'o';
287       out[baselen + 2] = '\0';
288       generate_option (OPT_o, out, 1, CL_DRIVER,
289                        &new_decoded_options[j]);
290       j++;
291     }
292
293   /* Add `-lgo' if we haven't already done so.  */
294   if (library > 0)
295     {
296       generate_option (OPT_l, LIBGOBEGIN, 1, CL_DRIVER,
297                        &new_decoded_options[j]);
298       added_libraries++;
299       j++;
300
301 #ifdef HAVE_LD_STATIC_DYNAMIC
302       if (library > 1 && !static_link)
303         {
304           generate_option (OPT_Wl_, LD_STATIC_OPTION, 1, CL_DRIVER,
305                            &new_decoded_options[j]);
306           j++;
307         }
308 #endif
309
310       generate_option (OPT_l, saw_profile_flag ? LIBGO_PROFILE : LIBGO, 1,
311                        CL_DRIVER, &new_decoded_options[j]);
312       added_libraries++;
313       j++;
314
315 #ifdef HAVE_LD_STATIC_DYNAMIC
316       if (library > 1 && !static_link)
317         {
318           generate_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1, CL_DRIVER,
319                            &new_decoded_options[j]);
320           j++;
321         }
322 #endif
323
324       /* When linking libgo statically we also need to link with the
325          pthread library.  */
326       if (library > 1 || static_link)
327         need_thread = 1;
328     }
329
330   if (saw_thread)
331     new_decoded_options[j++] = *saw_thread;
332   else if (library > 0 && need_thread)
333     {
334       generate_option (OPT_l,
335                        (saw_profile_flag
336                         ? THREAD_LIBRARY_PROFILE
337                         : THREAD_LIBRARY),
338                        1, CL_DRIVER, &new_decoded_options[j]);
339       added_libraries++;
340       j++;
341     }
342
343   if (saw_math)
344     new_decoded_options[j++] = *saw_math;
345   else if (library > 0 && need_math)
346     {
347       generate_option (OPT_l,
348                        saw_profile_flag ? MATH_LIBRARY_PROFILE : MATH_LIBRARY,
349                        1, CL_DRIVER, &new_decoded_options[j]);
350       added_libraries++;
351       j++;
352     }
353
354   if (saw_libc)
355     new_decoded_options[j++] = *saw_libc;
356   if (shared_libgcc && !static_link)
357     generate_option (OPT_shared_libgcc, NULL, 1, CL_DRIVER,
358                      &new_decoded_options[j++]);
359
360   *in_decoded_options_count = j;
361   *in_decoded_options = new_decoded_options;
362   *in_added_libraries = added_libraries;
363 }
364
365 /* Called before linking.  Returns 0 on success and -1 on failure.  */
366 int lang_specific_pre_link (void)  /* Not used for Go.  */
367 {
368   return 0;
369 }
370
371 /* Number of extra output files that lang_specific_pre_link may generate.  */
372 int lang_specific_extra_outfiles = 0;  /* Not used for Go.  */