jvspec.c (jvgenmain_spec): Add %I to cc1 call.
[official-gcc.git] / gcc / java / jvspec.c
blobb66d14f522c50c0aacfb719f61bcd7ac771a1be5
1 /* Specific flags and argument handling of the front-end of the
2 GNU compiler for the Java(TM) language.
3 Copyright (C) 1996-2013 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "gcc.h"
30 #include "jcf.h"
31 #include "opts.h"
33 /* Name of spec file. */
34 #define SPEC_FILE "libgcj.spec"
36 /* This bit is set if we saw a `-xfoo' language specification. */
37 #define LANGSPEC (1<<1)
38 /* True if this arg is a .java input file name. */
39 #define JAVA_FILE_ARG (1<<3)
40 /* True if this arg is a .class input file name. */
41 #define CLASS_FILE_ARG (1<<4)
42 /* True if this arg is a .zip or .jar input file name. */
43 #define ZIP_FILE_ARG (1<<5)
44 /* True if this arg is @FILE - where FILE contains a list of filenames. */
45 #define INDIRECT_FILE_ARG (1<<6)
46 /* True if this arg is a resource file. */
47 #define RESOURCE_FILE_ARG (1<<7)
49 static char *find_spec_file (const char *);
50 static int verify_class_name (const char *);
52 static const char *main_class_name = NULL;
53 int lang_specific_extra_outfiles = 0;
55 /* True if we should add -shared-libgcc to the command-line. */
56 int shared_libgcc = 1;
58 static const char jvgenmain_spec[] =
59 "jvgenmain %{findirect-dispatch} %{D*} %b %m.i |\n\
60 cc1 %m.i %1 \
61 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*}\
62 %{g*} %{O*} %I \
63 %{v:-version} %{pg:-p} %{p}\
64 %<fbounds-check %<fno-bounds-check\
65 %<fassume-compiled* %<fno-assume-compiled*\
66 %<fcompile-resource* %<fassert %<fno-assert \
67 %<femit-class-file %<femit-class-files %<fencoding*\
68 %<fuse-boehm-gc %<fhash-synchronization %<fjni\
69 %<findirect-dispatch\
70 %<fno-store-check %<foutput-class-dir\
71 %<fclasspath* %<fbootclasspath*\
72 %<fextdirs*\
73 %<fuse-divide-subroutine %<fno-use-divide-subroutine\
74 %<fuse-atomic-builtins %<fno-use-atomic-builtins\
75 %<fcheck-references %<fno-check-references\
76 %<ffilelist-file %<fsaw-java-file %<fsource* %<ftarget*\
77 %{f*} -fdollars-in-identifiers\
78 %{aux-info*}\
79 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
80 %{S:%W{o*}%{!o*:-o %b.s}}\
81 %(invoke_as)";
83 /* Return full path name of spec file if it is in DIR, or NULL if
84 not. */
85 static char *
86 find_spec_file (const char *dir)
88 char *spec;
89 struct stat sb;
91 spec = XNEWVEC (char, strlen (dir) + sizeof (SPEC_FILE) + 4);
92 strcpy (spec, dir);
93 strcat (spec, "/");
94 strcat (spec, SPEC_FILE);
95 if (! stat (spec, &sb))
96 return spec;
97 free (spec);
98 return NULL;
101 #define JAVA_START_CHAR_P(c) (c < 128 && (ISIDST (c) || c == '$'))
102 #define JAVA_PART_CHAR_P(c) (c < 128 \
103 && (ISIDNUM (c) \
104 || c == '$' \
105 || (c >= 0x00 && c <= 0x08) \
106 || (c >= 0x0e && c <= 0x1b) \
107 || c == 0x7f))
109 /* Verify that NAME is a valid Java class name that might contain
110 `main'. Return 0 on failure. */
111 static int
112 verify_class_name (const char *name)
114 /* FIXME: what encoding do we use for command-line arguments? For
115 now we assume plain ASCII, which of course is wrong. */
116 while (*name)
118 int ch = *name++;
119 if (ch < 0 || ! JAVA_START_CHAR_P (ch))
120 return 0;
121 while (*name)
123 ch = *name++;
124 if (ch < 0)
125 return 0;
126 /* We found a break between class names. Next character
127 must be an identifier start again. */
128 if (ch == '.')
129 break;
130 if (! JAVA_PART_CHAR_P (ch))
131 return 0;
135 return 1;
138 void
139 lang_specific_driver (struct cl_decoded_option **in_decoded_options,
140 unsigned int *in_decoded_options_count,
141 int *in_added_libraries)
143 unsigned int i, j;
145 int saw_save_temps = 0;
147 /* This will be 0 if we encounter a situation where we should not
148 link in libgcj. */
149 int library = 1;
151 /* This will be 1 if multiple input files (.class and/or .java)
152 should be passed to a single jc1 invocation. */
153 int combine_inputs = 0;
155 /* Number of .java and .class source file arguments seen. */
156 int java_files_count = 0;
157 int class_files_count = 0;
158 /* Number of .zip or .jar file arguments seen. */
159 int zip_files_count = 0;
160 /* Number of '@FILES' arguments seen. */
161 int indirect_files_count = 0;
163 /* Name of file containing list of files to compile. */
164 char *filelist_filename = 0;
166 FILE *filelist_file = 0;
168 /* The number of arguments being added to what's in argv, other than
169 libraries. */
170 int added = 2;
172 /* The new argument list will be contained in this. */
173 struct cl_decoded_option *new_decoded_options;
175 /* Nonzero if we saw a `-xfoo' language specification on the
176 command line. Used to avoid adding our own -xc++ if the user
177 already gave a language for the file. */
178 int saw_speclang = 0;
180 /* Saw --resource, -C or -o options, respectively. */
181 int saw_resource = 0;
182 int saw_C = 0;
183 int saw_o = 0;
185 /* Saw some -O* or -g* option, respectively. */
186 int saw_O = 0;
187 int saw_g = 0;
189 /* Saw a `-D' option. */
190 int saw_D = 0;
192 /* An array used to flag each argument that needs a bit set for
193 LANGSPEC, MATHLIB, WITHLIBC, or GCLIB. */
194 int *args;
196 /* The total number of arguments with the new stuff. */
197 unsigned int argc;
199 /* The argument list. */
200 struct cl_decoded_option *decoded_options;
202 /* The number of libraries added in. */
203 int added_libraries;
205 /* The total number of arguments with the new stuff. */
206 unsigned int num_args = 1;
208 /* Nonzero if linking is supposed to happen. */
209 int will_link = 1;
211 /* Nonzero if we want to find the spec file. */
212 int want_spec_file = 1;
214 /* The argument we use to specify the spec file. */
215 char *spec_file = NULL;
217 /* If linking, nonzero if the BC-ABI is in use. */
218 int link_for_bc_abi = 0;
220 argc = *in_decoded_options_count;
221 decoded_options = *in_decoded_options;
222 added_libraries = *in_added_libraries;
224 args = XCNEWVEC (int, argc);
226 for (i = 1; i < argc; i++)
228 switch (decoded_options[i].opt_index)
230 case OPT_nostdlib:
231 case OPT_nodefaultlibs:
232 library = 0;
233 break;
235 case OPT_fmain_:
236 main_class_name = decoded_options[i].arg;
237 added--;
238 break;
240 case OPT__help:
241 want_spec_file = 0;
242 break;
244 case OPT_v:
245 if (argc == 2)
247 /* If they only gave us `-v', don't try to link
248 in libgcj. */
249 library = 0;
251 break;
253 case OPT_x:
254 saw_speclang = 1;
255 break;
257 case OPT_C:
258 saw_C = 1;
259 want_spec_file = 0;
260 if (library != 0)
261 added -= 2;
262 library = 0;
263 will_link = 0;
264 break;
266 case OPT_fcompile_resource_:
267 saw_resource = 1;
268 want_spec_file = 0;
269 if (library != 0)
270 --added;
271 library = 0;
272 will_link = 0;
273 break;
275 case OPT_D:
276 saw_D = 1;
277 break;
279 case OPT_g:
280 case OPT_gcoff:
281 case OPT_gdwarf_:
282 case OPT_ggdb:
283 case OPT_gstabs:
284 case OPT_gstabs_:
285 case OPT_gvms:
286 case OPT_gxcoff:
287 case OPT_gxcoff_:
288 saw_g = 1;
289 break;
291 case OPT_O:
292 case OPT_Os:
293 case OPT_Ofast:
294 saw_O = 1;
295 break;
297 case OPT_o:
298 saw_o = 1;
299 break;
301 case OPT_fclasspath_:
302 case OPT_fbootclasspath_:
303 case OPT_extdirs:
304 added -= 1;
305 break;
307 case OPT_c:
308 case OPT_S:
309 case OPT_E:
310 case OPT_M:
311 case OPT_MM:
312 /* Don't specify libraries if we won't link, since that would
313 cause a warning. */
314 library = 0;
315 added -= 2;
317 /* Remember this so we can confirm -fmain option. */
318 will_link = 0;
319 break;
321 case OPT_fsyntax_only:
322 library = 0;
323 will_link = 0;
324 continue;
326 case OPT_save_temps:
327 saw_save_temps = 1;
328 break;
330 case OPT_static_libgcc:
331 case OPT_static:
332 shared_libgcc = 0;
333 break;
335 case OPT_findirect_dispatch:
336 link_for_bc_abi = 1;
337 break;
339 case OPT_SPECIAL_input_file:
341 const char *arg = decoded_options[i].arg;
342 int len;
344 /* We don't do this anymore, since we don't get them with minus
345 signs on them. */
346 if (arg[0] == '\0' || arg[1] == '\0')
347 continue;
349 if (saw_speclang)
351 saw_speclang = 0;
352 continue;
355 if (saw_resource)
357 args[i] |= RESOURCE_FILE_ARG;
358 added += 2; /* for -xjava and -xnone */
361 if (arg[0] == '@')
363 args[i] |= INDIRECT_FILE_ARG;
364 indirect_files_count++;
365 added += 2; /* for -xjava and -xnone */
368 len = strlen (arg);
369 if (len > 5 && strcmp (arg + len - 5, ".java") == 0)
371 args[i] |= JAVA_FILE_ARG;
372 java_files_count++;
374 if (len > 6 && strcmp (arg + len - 6, ".class") == 0)
376 args[i] |= CLASS_FILE_ARG;
377 class_files_count++;
379 if (len > 4
380 && (strcmp (arg + len - 4, ".zip") == 0
381 || strcmp (arg + len - 4, ".jar") == 0))
383 args[i] |= ZIP_FILE_ARG;
384 zip_files_count++;
388 default:
389 /* Pass other options through. */
390 continue;
394 if (saw_D && ! main_class_name)
395 fatal_error ("can%'t specify %<-D%> without %<--main%>");
397 if (main_class_name && ! verify_class_name (main_class_name))
398 fatal_error ("%qs is not a valid class name", main_class_name);
400 num_args = argc + added;
401 if (saw_resource)
403 if (! saw_o)
404 fatal_error ("--resource requires -o");
406 if (saw_C)
408 num_args += 3;
409 if (class_files_count + zip_files_count > 0)
411 warning (0, "already-compiled .class files ignored with -C");
412 num_args -= class_files_count + zip_files_count;
413 class_files_count = 0;
414 zip_files_count = 0;
416 num_args += 2; /* For -o NONE. */
417 if (saw_o)
418 fatal_error ("cannot specify both -C and -o");
420 if ((saw_o && java_files_count + class_files_count + zip_files_count > 1)
421 || (saw_C && java_files_count > 1)
422 || (indirect_files_count > 0
423 && java_files_count + class_files_count + zip_files_count > 0))
424 combine_inputs = 1;
426 if (combine_inputs)
428 filelist_filename = make_temp_file ("jx");
429 if (filelist_filename == NULL)
430 fatal_error ("cannot create temporary file");
431 record_temp_file (filelist_filename, ! saw_save_temps, 0);
432 filelist_file = fopen (filelist_filename, "w");
433 if (filelist_file == NULL)
434 pfatal_with_name (filelist_filename);
435 num_args -= java_files_count + class_files_count + zip_files_count;
436 num_args += 3; /* for the combined arg "-xjava", and "-xnone" */
439 if (main_class_name)
441 lang_specific_extra_outfiles++;
443 if (saw_g + saw_O == 0)
444 num_args++;
445 num_args++;
446 /* An additional entry for the classpath. */
447 num_args++;
449 if (combine_inputs || indirect_files_count > 0)
450 num_args += 1; /* for "-ffilelist-file" */
451 if (combine_inputs && indirect_files_count > 0)
452 fatal_error ("using both @FILE with multiple files not implemented");
454 /* There's no point adding -shared-libgcc if we don't have a shared
455 libgcc. */
456 #ifndef ENABLE_SHARED_LIBGCC
457 shared_libgcc = 0;
458 #endif
460 if (java_files_count > 0)
461 ++num_args;
463 num_args += shared_libgcc;
465 num_args += link_for_bc_abi;
467 new_decoded_options = XNEWVEC (struct cl_decoded_option, num_args);
468 j = 0;
470 new_decoded_options[j++] = decoded_options[0];
472 if (combine_inputs || indirect_files_count > 0)
473 generate_option (OPT_ffilelist_file, NULL, 1, CL_DRIVER,
474 &new_decoded_options[j++]);
476 if (combine_inputs)
478 generate_option (OPT_x, "java", 1, CL_DRIVER,
479 &new_decoded_options[j++]);
480 generate_option_input_file (filelist_filename,
481 &new_decoded_options[j++]);
482 generate_option (OPT_x, "none", 1, CL_DRIVER,
483 &new_decoded_options[j++]);
486 if (java_files_count > 0)
487 generate_option (OPT_fsaw_java_file, NULL, 1, CL_DRIVER,
488 &new_decoded_options[j++]);
490 jcf_path_init ();
491 for (i = 1; i < argc; i++, j++)
493 new_decoded_options[j] = decoded_options[i];
495 if (decoded_options[i].errors & CL_ERR_MISSING_ARG)
496 continue;
498 if ((args[i] & RESOURCE_FILE_ARG) != 0)
500 generate_option (OPT_x, "java", 1, CL_DRIVER,
501 &new_decoded_options[j++]);
502 new_decoded_options[j++] = decoded_options[i];
503 generate_option (OPT_x, "none", 1, CL_DRIVER,
504 &new_decoded_options[j]);
507 switch (decoded_options[i].opt_index)
509 case OPT_I:
510 jcf_path_include_arg (decoded_options[i].arg);
511 --j;
512 continue;
514 case OPT_fclasspath_:
515 jcf_path_classpath_arg (decoded_options[i].arg);
516 --j;
517 continue;
519 case OPT_fbootclasspath_:
520 jcf_path_bootclasspath_arg (decoded_options[i].arg);
521 --j;
522 continue;
524 case OPT_extdirs:
525 jcf_path_extdirs_arg (decoded_options[i].arg);
526 --j;
527 continue;
529 case OPT_L:
530 if (spec_file == NULL)
531 spec_file = find_spec_file (decoded_options[i].arg);
532 break;
534 case OPT_fmain_:
535 if (! will_link)
536 fatal_error ("cannot specify %<main%> class when not linking");
537 --j;
538 continue;
541 if ((args[i] & INDIRECT_FILE_ARG) != 0)
543 generate_option (OPT_x, "java", 1, CL_DRIVER,
544 &new_decoded_options[j++]);
545 /* Drop '@'. */
546 generate_option_input_file (decoded_options[i].arg + 1,
547 &new_decoded_options[j++]);
548 generate_option (OPT_x, "none", 1, CL_DRIVER,
549 &new_decoded_options[j]);
552 if ((args[i] & (CLASS_FILE_ARG|ZIP_FILE_ARG)) && saw_C)
554 --j;
555 continue;
558 if (combine_inputs
559 && (args[i] & (CLASS_FILE_ARG|JAVA_FILE_ARG|ZIP_FILE_ARG)) != 0)
561 fputs (decoded_options[i].arg, filelist_file);
562 fputc ('\n', filelist_file);
563 --j;
564 continue;
568 /* Handle classpath setting. We specify the bootclasspath since
569 that requires the fewest changes to our existing code... */
570 jcf_path_seal (0);
571 generate_option (OPT_fbootclasspath_, jcf_path_compute (""), 1,
572 CL_DRIVER, &new_decoded_options[j++]);
574 if (combine_inputs)
576 if (fclose (filelist_file))
577 pfatal_with_name (filelist_filename);
580 /* If we saw no -O or -g option, default to -g1, for javac compatibility. */
581 if (saw_g + saw_O == 0)
582 generate_option (OPT_g, "1", 1, CL_DRIVER, &new_decoded_options[j++]);
584 /* Read the specs file corresponding to libgcj.
585 If we didn't find the spec file on the -L path, then we hope it
586 is somewhere in the standard install areas. */
587 if (want_spec_file)
588 generate_option (OPT_specs_, spec_file == NULL ? "libgcj.spec" : spec_file,
589 1, CL_DRIVER, &new_decoded_options[j++]);
591 if (saw_C)
593 generate_option (OPT_fsyntax_only, NULL, 1, CL_DRIVER,
594 &new_decoded_options[j++]);
595 generate_option (OPT_femit_class_files, NULL, 1, CL_DRIVER,
596 &new_decoded_options[j++]);
597 generate_option (OPT_S, NULL, 1, CL_DRIVER, &new_decoded_options[j++]);
598 generate_option (OPT_o, "NONE", 1, CL_DRIVER,
599 &new_decoded_options[j++]);
602 if (shared_libgcc)
603 generate_option (OPT_shared_libgcc, NULL, 1, CL_DRIVER,
604 &new_decoded_options[j++]);
606 if (link_for_bc_abi)
607 generate_option (OPT_s_bc_abi, NULL, 1, CL_DRIVER,
608 &new_decoded_options[j++]);
610 *in_decoded_options_count = j;
611 *in_decoded_options = new_decoded_options;
612 *in_added_libraries = added_libraries;
616 lang_specific_pre_link (void)
618 int err;
619 if (main_class_name == NULL)
620 return 0;
621 /* Append `main' to make the filename unique and allow
623 gcj --main=hello -save-temps hello.java
625 to work. jvgenmain needs to strip this `main' to arrive at the correct
626 class name. Append dummy `.c' that can be stripped by set_input so %b
627 is correct. */
628 set_input (concat (main_class_name, "main.c", NULL));
629 err = do_spec (jvgenmain_spec);
630 if (err == 0)
632 /* Shift the outfiles array so the generated main comes first.
633 This is important when linking against (non-shared) libraries,
634 since otherwise we risk (a) nothing getting linked or
635 (b) 'main' getting picked up from a library. */
636 int i = n_infiles;
637 const char *generated = outfiles[i];
638 while (--i >= 0)
639 outfiles[i + 1] = outfiles[i];
640 outfiles[0] = generated;
642 return err;