Merge trunk version 221103 into gupc branch.
[official-gcc.git] / gcc / c / gupcspec.c
blobef1d60443475c6171723024a29869a06e099b78a
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"
31 /* GUPC driver - derived from fortran/gfortranspec.c. */
33 /* The original argument list and related info is copied here. */
34 static unsigned int gupc_xargc;
35 static const struct cl_decoded_option *gupc_x_decoded_options;
36 static void append_arg (const struct cl_decoded_option *);
38 /* The new argument list will be built here. */
39 static unsigned int gupc_newargc;
40 static struct cl_decoded_option *gupc_new_decoded_options;
42 static const char *const standard_bindir_prefix = STANDARD_BINDIR_PREFIX;
43 static const char *const standard_exec_prefix = STANDARD_EXEC_PREFIX;
45 /* By default the linker is always invoked. */
46 static int invoke_linker = 1;
48 static int match_suffix (const char *s, const char *suffix);
50 #define END_ARGS ((char *) 0)
52 /* Return true if the strings S1 and S2 are either both NULL
53 * or both the same string. */
55 static bool
56 strings_same (const char *s1, const char *s2)
58 return s1 == s2 || (s1 != NULL && s2 != NULL && strcmp (s1, s2) == 0);
61 /* Return whether decoded option structures OPT1 and OPT2 are the
62 same. */
64 static bool
65 options_same (const struct cl_decoded_option *opt1,
66 const struct cl_decoded_option *opt2)
68 return (opt1->opt_index == opt2->opt_index
69 && strings_same (opt1->arg, opt2->arg)
70 && strings_same (opt1->orig_option_with_args_text,
71 opt2->orig_option_with_args_text)
72 && strings_same (opt1->canonical_option[0],
73 opt2->canonical_option[0])
74 && strings_same (opt1->canonical_option[1],
75 opt2->canonical_option[1])
76 && strings_same (opt1->canonical_option[2],
77 opt2->canonical_option[2])
78 && strings_same (opt1->canonical_option[3],
79 opt2->canonical_option[3])
80 && (opt1->canonical_option_num_elements
81 == opt2->canonical_option_num_elements)
82 && opt1->value == opt2->value && opt1->errors == opt2->errors);
85 /* Append another argument to the list being built. As long as it is
86 identical to the corresponding arg in the original list, just increment
87 the new arg count. Otherwise allocate a new list, etc. */
89 static void
90 append_arg (const struct cl_decoded_option *arg)
92 static unsigned int newargsize;
94 if (gupc_new_decoded_options == gupc_x_decoded_options
95 && gupc_newargc < gupc_xargc
96 && options_same (arg, &gupc_x_decoded_options[gupc_newargc]))
98 ++gupc_newargc;
99 return; /* Nothing new here. */
102 if (gupc_new_decoded_options == gupc_x_decoded_options)
103 { /* Make new arglist. */
104 unsigned int i;
106 newargsize = (gupc_xargc << 2) + 20; /* This should handle all. */
107 gupc_new_decoded_options =
108 XNEWVEC (struct cl_decoded_option, newargsize);
110 /* Copy what has been done so far. */
111 for (i = 0; i < gupc_newargc; ++i)
112 gupc_new_decoded_options[i] = gupc_x_decoded_options[i];
115 if (gupc_newargc == newargsize)
116 fatal_error (input_location, "overflowed output arg list for %qs",
117 arg->orig_option_with_args_text);
119 gupc_new_decoded_options[gupc_newargc++] = *arg;
122 /* Append an option described by OPT_INDEX, ARG and VALUE to the list
123 being built. */
124 static void
125 append_option (size_t opt_index, const char *arg, int value)
127 struct cl_decoded_option decoded;
129 generate_option (opt_index, arg, value, CL_DRIVER, &decoded);
130 append_arg (&decoded);
133 static int
134 match_suffix (const char *s, const char *suffix)
136 int slen = strlen (s);
137 int xlen = strlen (suffix);
138 const char *start = (xlen <= slen) ? s + slen - xlen : 0;
139 return start && !strncmp (start, suffix, xlen);
142 void
143 lang_specific_driver (struct cl_decoded_option **in_decoded_options,
144 unsigned int *in_decoded_options_count,
145 int *in_added_libraries ATTRIBUTE_UNUSED)
147 struct cl_decoded_option *decoded_options = *in_decoded_options;
148 unsigned int i;
149 int is_x_in_effect = 0;
150 int is_x_upc_in_effect = 0;
151 int verbose = 0;
152 int n_infiles = 0;
153 int n_outfiles = 0;
154 int pre_processed = 0;
156 gupc_xargc = *in_decoded_options_count;
157 gupc_x_decoded_options = decoded_options;
158 gupc_newargc = 0;
159 gupc_new_decoded_options = decoded_options;
161 /* First pass through the argument list. */
163 /* Check to see if any switches are asserted that inhibit linking
164 and record the presence of other switches that may require
165 special handling. */
166 for (i = 1; i < gupc_xargc; ++i)
168 if (decoded_options[i].errors & CL_ERR_MISSING_ARG)
169 continue;
171 switch (decoded_options[i].opt_index)
174 case OPT_SPECIAL_input_file:
175 ++n_infiles;
176 continue;
178 case OPT_nostdlib:
179 case OPT_nodefaultlibs:
180 case OPT_c:
181 case OPT_S:
182 case OPT_fsyntax_only:
183 case OPT_E:
184 /* These options disable linking entirely or linking of the
185 standard libraries. */
186 invoke_linker = 0;
187 break;
189 case OPT_fpreprocessed:
190 pre_processed = 1;
191 break;
193 case OPT_l:
194 ++n_infiles;
195 break;
197 case OPT_o:
198 ++n_outfiles;
199 break;
201 case OPT_v:
202 verbose = 1;
203 break;
205 case OPT__version:
206 /* Optional GUPC version string. Let GCC handle it for now. */
207 break;
209 case OPT__help:
210 /* Let gcc.c handle this, as it has a really
211 cool facility for handling --help and --verbose --help. */
212 return;
214 default:
215 break;
219 /* Create the new argument list. */
221 /* Start with the compiler itself. */
222 append_arg (&decoded_options[0]);
224 /* Always assert -fupc. */
225 append_option (OPT_fupc, NULL, 1);
227 /* If there are no input files, no need for the library. */
228 if (n_infiles == 0)
229 invoke_linker = 0;
231 /* Copy in the arguments as passed to 'upc' */
232 is_x_in_effect = 0;
233 is_x_upc_in_effect = 0;
234 for (i = 1; i < gupc_xargc; ++i)
236 /* Skip -fupc, we asserted it above. */
237 if (decoded_options[i].opt_index == OPT_fupc)
238 continue;
240 /* Check for "-x [c,upc,..]". */
241 if (decoded_options[i].opt_index == OPT_x)
243 /* Go to default if "none" found. */
244 if (!strcmp (decoded_options[i].arg, "none"))
246 is_x_in_effect = 0;
247 is_x_upc_in_effect = 0;
249 else
250 is_x_in_effect = 1;
253 /* By default, driver accepts C files as UPC files. Unless there
254 is "-x" option in affect. */
255 if (decoded_options[i].opt_index == OPT_SPECIAL_input_file)
257 const int is_c_file = match_suffix (decoded_options[i].arg, ".c")
258 || match_suffix (decoded_options[i].arg, ".h")
259 || (pre_processed && match_suffix (decoded_options[i].arg, ".i"));
260 if (is_c_file)
262 if ( !(is_x_in_effect || is_x_upc_in_effect))
264 append_option (OPT_x, "upc", 1);
265 is_x_upc_in_effect = 1;
267 append_arg (&decoded_options[i]);
268 continue;
270 else
272 if (is_x_upc_in_effect)
274 is_x_upc_in_effect = 0;
275 append_option (OPT_x, "none", 1);
279 append_arg (&decoded_options[i]);
282 if (verbose)
284 fprintf (stderr, "Driving:");
285 for (i = 0; i < gupc_newargc; i++)
286 fprintf (stderr, " %s",
287 gupc_new_decoded_options[i].orig_option_with_args_text);
288 fprintf (stderr, "\n");
291 *in_decoded_options_count = gupc_newargc;
292 *in_decoded_options = gupc_new_decoded_options;
295 /* Called before linking. Returns 0 on success and -1 on failure. */
297 lang_specific_pre_link (void)
299 return 0;
302 /* Number of extra output files that lang_specific_pre_link may generate. */
303 int lang_specific_extra_outfiles = 0; /* Not used for GUPC. */