1 /* Specific flags and argument handling of the front-end of the
2 GNU compiler for the Java(TM) language.
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2009, 2010, 2011 Free Software Foundation, Inc.
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)
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 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
28 #include "coretypes.h"
34 /* Name of spec file. */
35 #define SPEC_FILE "libgcj.spec"
37 /* This bit is set if we saw a `-xfoo' language specification. */
38 #define LANGSPEC (1<<1)
39 /* True if this arg is a .java input file name. */
40 #define JAVA_FILE_ARG (1<<3)
41 /* True if this arg is a .class input file name. */
42 #define CLASS_FILE_ARG (1<<4)
43 /* True if this arg is a .zip or .jar input file name. */
44 #define ZIP_FILE_ARG (1<<5)
45 /* True if this arg is @FILE - where FILE contains a list of filenames. */
46 #define INDIRECT_FILE_ARG (1<<6)
47 /* True if this arg is a resource file. */
48 #define RESOURCE_FILE_ARG (1<<7)
50 static char *find_spec_file (const char *);
51 static int verify_class_name (const char *);
53 static const char *main_class_name = NULL;
54 int lang_specific_extra_outfiles = 0;
56 /* True if we should add -shared-libgcc to the command-line. */
57 int shared_libgcc = 1;
59 static const char jvgenmain_spec[] =
60 "jvgenmain %{findirect-dispatch} %{D*} %b %m.i |\n\
62 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*}\
64 %{v:-version} %{pg:-p} %{p}\
65 %<fbounds-check %<fno-bounds-check\
66 %<fassume-compiled* %<fno-assume-compiled*\
67 %<fcompile-resource* %<fassert %<fno-assert \
68 %<femit-class-file %<femit-class-files %<fencoding*\
69 %<fuse-boehm-gc %<fhash-synchronization %<fjni\
71 %<fno-store-check %<foutput-class-dir\
72 %<fclasspath* %<fbootclasspath*\
74 %<fuse-divide-subroutine %<fno-use-divide-subroutine\
75 %<fuse-atomic-builtins %<fno-use-atomic-builtins\
76 %<fcheck-references %<fno-check-references\
77 %<ffilelist-file %<fsaw-java-file %<fsource* %<ftarget*\
78 %{f*} -fdollars-in-identifiers\
80 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
81 %{S:%W{o*}%{!o*:-o %b.s}}\
84 /* Return full path name of spec file if it is in DIR, or NULL if
87 find_spec_file (const char *dir)
92 spec = XNEWVEC (char, strlen (dir) + sizeof (SPEC_FILE) + 4);
95 strcat (spec, SPEC_FILE);
96 if (! stat (spec, &sb))
102 #define JAVA_START_CHAR_P(c) (c < 128 && (ISIDST (c) || c == '$'))
103 #define JAVA_PART_CHAR_P(c) (c < 128 \
106 || (c >= 0x00 && c <= 0x08) \
107 || (c >= 0x0e && c <= 0x1b) \
110 /* Verify that NAME is a valid Java class name that might contain
111 `main'. Return 0 on failure. */
113 verify_class_name (const char *name)
115 /* FIXME: what encoding do we use for command-line arguments? For
116 now we assume plain ASCII, which of course is wrong. */
120 if (ch < 0 || ! JAVA_START_CHAR_P (ch))
127 /* We found a break between class names. Next character
128 must be an identifier start again. */
131 if (! JAVA_PART_CHAR_P (ch))
140 lang_specific_driver (struct cl_decoded_option **in_decoded_options,
141 unsigned int *in_decoded_options_count,
142 int *in_added_libraries)
146 int saw_save_temps = 0;
148 /* This will be 0 if we encounter a situation where we should not
152 /* This will be 1 if multiple input files (.class and/or .java)
153 should be passed to a single jc1 invocation. */
154 int combine_inputs = 0;
156 /* Number of .java and .class source file arguments seen. */
157 int java_files_count = 0;
158 int class_files_count = 0;
159 /* Number of .zip or .jar file arguments seen. */
160 int zip_files_count = 0;
161 /* Number of '@FILES' arguments seen. */
162 int indirect_files_count = 0;
164 /* Name of file containing list of files to compile. */
165 char *filelist_filename = 0;
167 FILE *filelist_file = 0;
169 /* The number of arguments being added to what's in argv, other than
173 /* The new argument list will be contained in this. */
174 struct cl_decoded_option *new_decoded_options;
176 /* Nonzero if we saw a `-xfoo' language specification on the
177 command line. Used to avoid adding our own -xc++ if the user
178 already gave a language for the file. */
179 int saw_speclang = 0;
181 /* Saw --resource, -C or -o options, respectively. */
182 int saw_resource = 0;
186 /* Saw some -O* or -g* option, respectively. */
190 /* Saw a `-D' option. */
193 /* An array used to flag each argument that needs a bit set for
194 LANGSPEC, MATHLIB, WITHLIBC, or GCLIB. */
197 /* The total number of arguments with the new stuff. */
200 /* The argument list. */
201 struct cl_decoded_option *decoded_options;
203 /* The number of libraries added in. */
206 /* The total number of arguments with the new stuff. */
207 unsigned int num_args = 1;
209 /* Nonzero if linking is supposed to happen. */
212 /* Nonzero if we want to find the spec file. */
213 int want_spec_file = 1;
215 /* The argument we use to specify the spec file. */
216 char *spec_file = NULL;
218 /* If linking, nonzero if the BC-ABI is in use. */
219 int link_for_bc_abi = 0;
221 argc = *in_decoded_options_count;
222 decoded_options = *in_decoded_options;
223 added_libraries = *in_added_libraries;
225 args = XCNEWVEC (int, argc);
227 for (i = 1; i < argc; i++)
229 switch (decoded_options[i].opt_index)
232 case OPT_nodefaultlibs:
237 main_class_name = decoded_options[i].arg;
248 /* If they only gave us `-v', don't try to link
267 case OPT_fcompile_resource_:
302 case OPT_fclasspath_:
303 case OPT_fbootclasspath_:
313 /* Don't specify libraries if we won't link, since that would
318 /* Remember this so we can confirm -fmain option. */
322 case OPT_fsyntax_only:
331 case OPT_static_libgcc:
336 case OPT_findirect_dispatch:
340 case OPT_SPECIAL_input_file:
342 const char *arg = decoded_options[i].arg;
345 /* We don't do this anymore, since we don't get them with minus
347 if (arg[0] == '\0' || arg[1] == '\0')
358 args[i] |= RESOURCE_FILE_ARG;
359 added += 2; /* for -xjava and -xnone */
364 args[i] |= INDIRECT_FILE_ARG;
365 indirect_files_count++;
366 added += 2; /* for -xjava and -xnone */
370 if (len > 5 && strcmp (arg + len - 5, ".java") == 0)
372 args[i] |= JAVA_FILE_ARG;
375 if (len > 6 && strcmp (arg + len - 6, ".class") == 0)
377 args[i] |= CLASS_FILE_ARG;
381 && (strcmp (arg + len - 4, ".zip") == 0
382 || strcmp (arg + len - 4, ".jar") == 0))
384 args[i] |= ZIP_FILE_ARG;
390 /* Pass other options through. */
395 if (saw_D && ! main_class_name)
396 fatal_error ("can%'t specify %<-D%> without %<--main%>");
398 if (main_class_name && ! verify_class_name (main_class_name))
399 fatal_error ("%qs is not a valid class name", main_class_name);
401 num_args = argc + added;
405 fatal_error ("--resource requires -o");
410 if (class_files_count + zip_files_count > 0)
412 warning (0, "already-compiled .class files ignored with -C");
413 num_args -= class_files_count + zip_files_count;
414 class_files_count = 0;
417 num_args += 2; /* For -o NONE. */
419 fatal_error ("cannot specify both -C and -o");
421 if ((saw_o && java_files_count + class_files_count + zip_files_count > 1)
422 || (saw_C && java_files_count > 1)
423 || (indirect_files_count > 0
424 && java_files_count + class_files_count + zip_files_count > 0))
429 filelist_filename = make_temp_file ("jx");
430 if (filelist_filename == NULL)
431 fatal_error ("cannot create temporary file");
432 record_temp_file (filelist_filename, ! saw_save_temps, 0);
433 filelist_file = fopen (filelist_filename, "w");
434 if (filelist_file == NULL)
435 pfatal_with_name (filelist_filename);
436 num_args -= java_files_count + class_files_count + zip_files_count;
437 num_args += 3; /* for the combined arg "-xjava", and "-xnone" */
442 lang_specific_extra_outfiles++;
444 if (saw_g + saw_O == 0)
447 /* An additional entry for the classpath. */
450 if (combine_inputs || indirect_files_count > 0)
451 num_args += 1; /* for "-ffilelist-file" */
452 if (combine_inputs && indirect_files_count > 0)
453 fatal_error ("using both @FILE with multiple files not implemented");
455 /* There's no point adding -shared-libgcc if we don't have a shared
457 #ifndef ENABLE_SHARED_LIBGCC
461 if (java_files_count > 0)
464 num_args += shared_libgcc;
466 num_args += link_for_bc_abi;
468 new_decoded_options = XNEWVEC (struct cl_decoded_option, num_args);
471 new_decoded_options[j++] = decoded_options[0];
473 if (combine_inputs || indirect_files_count > 0)
474 generate_option (OPT_ffilelist_file, NULL, 1, CL_DRIVER,
475 &new_decoded_options[j++]);
479 generate_option (OPT_x, "java", 1, CL_DRIVER,
480 &new_decoded_options[j++]);
481 generate_option_input_file (filelist_filename,
482 &new_decoded_options[j++]);
483 generate_option (OPT_x, "none", 1, CL_DRIVER,
484 &new_decoded_options[j++]);
487 if (java_files_count > 0)
488 generate_option (OPT_fsaw_java_file, NULL, 1, CL_DRIVER,
489 &new_decoded_options[j++]);
492 for (i = 1; i < argc; i++, j++)
494 new_decoded_options[j] = decoded_options[i];
496 if (decoded_options[i].errors & CL_ERR_MISSING_ARG)
499 if ((args[i] & RESOURCE_FILE_ARG) != 0)
501 generate_option (OPT_x, "java", 1, CL_DRIVER,
502 &new_decoded_options[j++]);
503 new_decoded_options[j++] = decoded_options[i];
504 generate_option (OPT_x, "none", 1, CL_DRIVER,
505 &new_decoded_options[j]);
508 switch (decoded_options[i].opt_index)
511 jcf_path_include_arg (decoded_options[i].arg);
515 case OPT_fclasspath_:
516 jcf_path_classpath_arg (decoded_options[i].arg);
520 case OPT_fbootclasspath_:
521 jcf_path_bootclasspath_arg (decoded_options[i].arg);
526 jcf_path_extdirs_arg (decoded_options[i].arg);
531 if (spec_file == NULL)
532 spec_file = find_spec_file (decoded_options[i].arg);
537 fatal_error ("cannot specify %<main%> class when not linking");
542 if ((args[i] & INDIRECT_FILE_ARG) != 0)
544 generate_option (OPT_x, "java", 1, CL_DRIVER,
545 &new_decoded_options[j++]);
547 generate_option_input_file (decoded_options[i].arg + 1,
548 &new_decoded_options[j++]);
549 generate_option (OPT_x, "none", 1, CL_DRIVER,
550 &new_decoded_options[j]);
553 if ((args[i] & (CLASS_FILE_ARG|ZIP_FILE_ARG)) && saw_C)
560 && (args[i] & (CLASS_FILE_ARG|JAVA_FILE_ARG|ZIP_FILE_ARG)) != 0)
562 fputs (decoded_options[i].arg, filelist_file);
563 fputc ('\n', filelist_file);
569 /* Handle classpath setting. We specify the bootclasspath since
570 that requires the fewest changes to our existing code... */
572 generate_option (OPT_fbootclasspath_, jcf_path_compute (""), 1,
573 CL_DRIVER, &new_decoded_options[j++]);
577 if (fclose (filelist_file))
578 pfatal_with_name (filelist_filename);
581 /* If we saw no -O or -g option, default to -g1, for javac compatibility. */
582 if (saw_g + saw_O == 0)
583 generate_option (OPT_g, "1", 1, CL_DRIVER, &new_decoded_options[j++]);
585 /* Read the specs file corresponding to libgcj.
586 If we didn't find the spec file on the -L path, then we hope it
587 is somewhere in the standard install areas. */
589 generate_option (OPT_specs_, spec_file == NULL ? "libgcj.spec" : spec_file,
590 1, CL_DRIVER, &new_decoded_options[j++]);
594 generate_option (OPT_fsyntax_only, NULL, 1, CL_DRIVER,
595 &new_decoded_options[j++]);
596 generate_option (OPT_femit_class_files, NULL, 1, CL_DRIVER,
597 &new_decoded_options[j++]);
598 generate_option (OPT_S, NULL, 1, CL_DRIVER, &new_decoded_options[j++]);
599 generate_option (OPT_o, "NONE", 1, CL_DRIVER,
600 &new_decoded_options[j++]);
604 generate_option (OPT_shared_libgcc, NULL, 1, CL_DRIVER,
605 &new_decoded_options[j++]);
608 generate_option (OPT_s_bc_abi, NULL, 1, CL_DRIVER,
609 &new_decoded_options[j++]);
611 *in_decoded_options_count = j;
612 *in_decoded_options = new_decoded_options;
613 *in_added_libraries = added_libraries;
617 lang_specific_pre_link (void)
620 if (main_class_name == NULL)
622 /* Append `main' to make the filename unique and allow
624 gcj --main=hello -save-temps hello.java
626 to work. jvgenmain needs to strip this `main' to arrive at the correct
627 class name. Append dummy `.c' that can be stripped by set_input so %b
629 set_input (concat (main_class_name, "main.c", NULL));
630 err = do_spec (jvgenmain_spec);
633 /* Shift the outfiles array so the generated main comes first.
634 This is important when linking against (non-shared) libraries,
635 since otherwise we risk (a) nothing getting linked or
636 (b) 'main' getting picked up from a library. */
638 const char *generated = outfiles[i];
640 outfiles[i + 1] = outfiles[i];
641 outfiles[0] = generated;