Merge trunk version 209848 into gupc branch.
[official-gcc.git] / gcc / c / gupcspec.c
blob11d30a8a3330aa03056cf4d16028ad83218ecff0
1 /* gupcspec.c: the UPC compiler driver program
2 Copyright (C) 2001-2014 Free Software Foundation, Inc.
3 Contributed by Gary Funck <gary@intrepid.com>
4 and Nenad Vukicevic <nenad@intrepid.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "gcc.h"
26 #include "opts.h"
28 #include "tm.h"
29 #include "intl.h"
31 #include <errno.h>
32 #include <stdio.h>
33 #include <string.h>
35 /* GUPC driver - derived from fortran/gfortranspec.c. */
37 /* The original argument list and related info is copied here. */
38 static unsigned int gupc_xargc;
39 static const struct cl_decoded_option *gupc_x_decoded_options;
40 static void append_arg (const struct cl_decoded_option *);
42 /* The new argument list will be built here. */
43 static unsigned int gupc_newargc;
44 static struct cl_decoded_option *gupc_new_decoded_options;
46 static const char *const standard_bindir_prefix = STANDARD_BINDIR_PREFIX;
47 static const char *const standard_exec_prefix = STANDARD_EXEC_PREFIX;
49 /* By default the linker is always invoked. */
50 static int invoke_linker = 1;
52 static int match_suffix (const char *s, const char *suffix);
54 #define END_ARGS ((char *) 0)
56 /* Return true if the strings S1 and S2 are either both NULL
57 * or both the same string. */
59 static bool
60 strings_same (const char *s1, const char *s2)
62 return s1 == s2 || (s1 != NULL && s2 != NULL && strcmp (s1, s2) == 0);
65 /* Return whether decoded option structures OPT1 and OPT2 are the
66 same. */
68 static bool
69 options_same (const struct cl_decoded_option *opt1,
70 const struct cl_decoded_option *opt2)
72 return (opt1->opt_index == opt2->opt_index
73 && strings_same (opt1->arg, opt2->arg)
74 && strings_same (opt1->orig_option_with_args_text,
75 opt2->orig_option_with_args_text)
76 && strings_same (opt1->canonical_option[0],
77 opt2->canonical_option[0])
78 && strings_same (opt1->canonical_option[1],
79 opt2->canonical_option[1])
80 && strings_same (opt1->canonical_option[2],
81 opt2->canonical_option[2])
82 && strings_same (opt1->canonical_option[3],
83 opt2->canonical_option[3])
84 && (opt1->canonical_option_num_elements
85 == opt2->canonical_option_num_elements)
86 && opt1->value == opt2->value && opt1->errors == opt2->errors);
89 /* Append another argument to the list being built. As long as it is
90 identical to the corresponding arg in the original list, just increment
91 the new arg count. Otherwise allocate a new list, etc. */
93 static void
94 append_arg (const struct cl_decoded_option *arg)
96 static unsigned int newargsize;
98 if (gupc_new_decoded_options == gupc_x_decoded_options
99 && gupc_newargc < gupc_xargc
100 && options_same (arg, &gupc_x_decoded_options[gupc_newargc]))
102 ++gupc_newargc;
103 return; /* Nothing new here. */
106 if (gupc_new_decoded_options == gupc_x_decoded_options)
107 { /* Make new arglist. */
108 unsigned int i;
110 newargsize = (gupc_xargc << 2) + 20; /* This should handle all. */
111 gupc_new_decoded_options =
112 XNEWVEC (struct cl_decoded_option, newargsize);
114 /* Copy what has been done so far. */
115 for (i = 0; i < gupc_newargc; ++i)
116 gupc_new_decoded_options[i] = gupc_x_decoded_options[i];
119 if (gupc_newargc == newargsize)
120 fatal_error ("overflowed output arg list for %qs",
121 arg->orig_option_with_args_text);
123 gupc_new_decoded_options[gupc_newargc++] = *arg;
126 /* Append an option described by OPT_INDEX, ARG and VALUE to the list
127 being built. */
128 static void
129 append_option (size_t opt_index, const char *arg, int value)
131 struct cl_decoded_option decoded;
133 generate_option (opt_index, arg, value, CL_DRIVER, &decoded);
134 append_arg (&decoded);
137 static int
138 match_suffix (const char *s, const char *suffix)
140 int slen = strlen (s);
141 int xlen = strlen (suffix);
142 const char *start = (xlen <= slen) ? s + slen - xlen : 0;
143 return start && !strncmp (start, suffix, xlen);
146 void
147 lang_specific_driver (struct cl_decoded_option **in_decoded_options,
148 unsigned int *in_decoded_options_count,
149 int *in_added_libraries ATTRIBUTE_UNUSED)
151 struct cl_decoded_option *decoded_options = *in_decoded_options;
152 unsigned int i;
153 int is_x_in_effect = 0;
154 int is_x_upc_in_effect = 0;
155 int verbose = 0;
156 int n_infiles = 0;
157 int n_outfiles = 0;
158 int pre_processed = 0;
160 gupc_xargc = *in_decoded_options_count;
161 gupc_x_decoded_options = decoded_options;
162 gupc_newargc = 0;
163 gupc_new_decoded_options = decoded_options;
165 /* First pass through the argument list. */
167 /* Check to see if any switches are asserted that inhibit linking
168 and record the presence of other switches that may require
169 special handling. */
170 for (i = 1; i < gupc_xargc; ++i)
172 if (decoded_options[i].errors & CL_ERR_MISSING_ARG)
173 continue;
175 switch (decoded_options[i].opt_index)
178 case OPT_SPECIAL_input_file:
179 ++n_infiles;
180 continue;
182 case OPT_nostdlib:
183 case OPT_nodefaultlibs:
184 case OPT_c:
185 case OPT_S:
186 case OPT_fsyntax_only:
187 case OPT_E:
188 /* These options disable linking entirely or linking of the
189 standard libraries. */
190 invoke_linker = 0;
191 break;
193 case OPT_fpreprocessed:
194 pre_processed = 1;
195 break;
197 case OPT_l:
198 ++n_infiles;
199 break;
201 case OPT_o:
202 ++n_outfiles;
203 break;
205 case OPT_v:
206 verbose = 1;
207 break;
209 case OPT__version:
210 /* Optional GUPC version string. Let GCC handle it for now. */
211 break;
213 case OPT__help:
214 /* Let gcc.c handle this, as it has a really
215 cool facility for handling --help and --verbose --help. */
216 return;
218 default:
219 break;
223 /* Create the new argument list. */
225 /* Start with the compiler itself. */
226 append_arg (&decoded_options[0]);
228 /* Always assert -fupc. */
229 append_option (OPT_fupc, NULL, 1);
231 /* If there are no input files, no need for the library. */
232 if (n_infiles == 0)
233 invoke_linker = 0;
235 /* Copy in the arguments as passed to 'upc' */
236 is_x_in_effect = 0;
237 is_x_upc_in_effect = 0;
238 for (i = 1; i < gupc_xargc; ++i)
240 /* Skip -fupc, we asserted it above. */
241 if (decoded_options[i].opt_index == OPT_fupc)
242 continue;
244 /* Check for "-x [c,upc,..]". */
245 if (decoded_options[i].opt_index == OPT_x)
247 /* Go to default if "none" found. */
248 if (!strcmp (decoded_options[i].arg, "none"))
250 is_x_in_effect = 0;
251 is_x_upc_in_effect = 0;
253 else
254 is_x_in_effect = 1;
257 /* By default, driver accepts C files as UPC files. Unless there
258 is "-x" option in affect. */
259 if (decoded_options[i].opt_index == OPT_SPECIAL_input_file)
261 const int is_c_file = match_suffix (decoded_options[i].arg, ".c")
262 || match_suffix (decoded_options[i].arg, ".h")
263 || (pre_processed && match_suffix (decoded_options[i].arg, ".i"));
264 if (is_c_file)
266 if ( !(is_x_in_effect || is_x_upc_in_effect))
268 append_option (OPT_x, "upc", 1);
269 is_x_upc_in_effect = 1;
271 append_arg (&decoded_options[i]);
272 continue;
274 else
276 if (is_x_upc_in_effect)
278 is_x_upc_in_effect = 0;
279 append_option (OPT_x, "none", 1);
283 append_arg (&decoded_options[i]);
286 if (verbose)
288 fprintf (stderr, "Driving:");
289 for (i = 0; i < gupc_newargc; i++)
290 fprintf (stderr, " %s",
291 gupc_new_decoded_options[i].orig_option_with_args_text);
292 fprintf (stderr, "\n");
295 *in_decoded_options_count = gupc_newargc;
296 *in_decoded_options = gupc_new_decoded_options;
299 /* Called before linking. Returns 0 on success and -1 on failure. */
301 lang_specific_pre_link (void)
303 return 0;
306 /* Number of extra output files that lang_specific_pre_link may generate. */
307 int lang_specific_extra_outfiles = 0; /* Not used for GUPC. */