1 /* Specific flags and argument handling of the front-end of the
2 GNU compiler for the Java(TM) language.
3 Copyright (C) 1996-2014 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)
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. */
27 #include "coretypes.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\
61 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*}\
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\
70 %<fno-store-check %<foutput-class-dir\
71 %<fclasspath* %<fbootclasspath*\
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\
79 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
80 %{S:%W{o*}%{!o*:-o %b.s}}\
83 /* Return full path name of spec file if it is in DIR, or NULL if
86 find_spec_file (const char *dir
)
91 spec
= XNEWVEC (char, strlen (dir
) + sizeof (SPEC_FILE
) + 4);
94 strcat (spec
, SPEC_FILE
);
95 if (! stat (spec
, &sb
))
101 #define JAVA_START_CHAR_P(c) (c < 128 && (ISIDST (c) || c == '$'))
102 #define JAVA_PART_CHAR_P(c) (c < 128 \
105 || (c >= 0x00 && c <= 0x08) \
106 || (c >= 0x0e && c <= 0x1b) \
109 /* Verify that NAME is a valid Java class name that might contain
110 `main'. Return 0 on failure. */
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. */
119 if (ch
< 0 || ! JAVA_START_CHAR_P (ch
))
126 /* We found a break between class names. Next character
127 must be an identifier start again. */
130 if (! JAVA_PART_CHAR_P (ch
))
139 lang_specific_driver (struct cl_decoded_option
**in_decoded_options
,
140 unsigned int *in_decoded_options_count
,
141 int *in_added_libraries
)
145 int saw_save_temps
= 0;
147 /* This will be 0 if we encounter a situation where we should not
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
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;
185 /* Saw some -O* or -g* option, respectively. */
189 /* Saw a `-D' option. */
192 /* An array used to flag each argument that needs a bit set for
193 LANGSPEC, MATHLIB, WITHLIBC, or GCLIB. */
196 /* The total number of arguments with the new stuff. */
199 /* The argument list. */
200 struct cl_decoded_option
*decoded_options
;
202 /* The number of libraries added in. */
205 /* The total number of arguments with the new stuff. */
206 unsigned int num_args
= 1;
208 /* Nonzero if linking is supposed to happen. */
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
)
231 case OPT_nodefaultlibs
:
236 main_class_name
= decoded_options
[i
].arg
;
247 /* If they only gave us `-v', don't try to link
266 case OPT_fcompile_resource_
:
301 case OPT_fclasspath_
:
302 case OPT_fbootclasspath_
:
312 /* Don't specify libraries if we won't link, since that would
317 /* Remember this so we can confirm -fmain option. */
321 case OPT_fsyntax_only
:
330 case OPT_static_libgcc
:
335 case OPT_findirect_dispatch
:
339 case OPT_SPECIAL_input_file
:
341 const char *arg
= decoded_options
[i
].arg
;
344 /* We don't do this anymore, since we don't get them with minus
346 if (arg
[0] == '\0' || arg
[1] == '\0')
357 args
[i
] |= RESOURCE_FILE_ARG
;
358 added
+= 2; /* for -xjava and -xnone */
363 args
[i
] |= INDIRECT_FILE_ARG
;
364 indirect_files_count
++;
365 added
+= 2; /* for -xjava and -xnone */
369 if (len
> 5 && strcmp (arg
+ len
- 5, ".java") == 0)
371 args
[i
] |= JAVA_FILE_ARG
;
374 if (len
> 6 && strcmp (arg
+ len
- 6, ".class") == 0)
376 args
[i
] |= CLASS_FILE_ARG
;
380 && (strcmp (arg
+ len
- 4, ".zip") == 0
381 || strcmp (arg
+ len
- 4, ".jar") == 0))
383 args
[i
] |= ZIP_FILE_ARG
;
389 /* Pass other options through. */
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
;
404 fatal_error ("--resource requires -o");
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;
416 num_args
+= 2; /* For -o NONE. */
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))
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" */
441 lang_specific_extra_outfiles
++;
443 if (saw_g
+ saw_O
== 0)
446 /* An additional entry for the classpath. */
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
456 #ifndef ENABLE_SHARED_LIBGCC
460 if (java_files_count
> 0)
463 num_args
+= shared_libgcc
;
465 num_args
+= link_for_bc_abi
;
467 new_decoded_options
= XNEWVEC (struct cl_decoded_option
, num_args
);
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
++]);
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
++]);
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
)
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
)
510 jcf_path_include_arg (decoded_options
[i
].arg
);
514 case OPT_fclasspath_
:
515 jcf_path_classpath_arg (decoded_options
[i
].arg
);
519 case OPT_fbootclasspath_
:
520 jcf_path_bootclasspath_arg (decoded_options
[i
].arg
);
525 jcf_path_extdirs_arg (decoded_options
[i
].arg
);
530 if (spec_file
== NULL
)
531 spec_file
= find_spec_file (decoded_options
[i
].arg
);
536 fatal_error ("cannot specify %<main%> class when not linking");
541 if ((args
[i
] & INDIRECT_FILE_ARG
) != 0)
543 generate_option (OPT_x
, "java", 1, CL_DRIVER
,
544 &new_decoded_options
[j
++]);
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
)
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
);
568 /* Handle classpath setting. We specify the bootclasspath since
569 that requires the fewest changes to our existing code... */
571 generate_option (OPT_fbootclasspath_
, jcf_path_compute (""), 1,
572 CL_DRIVER
, &new_decoded_options
[j
++]);
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. */
588 generate_option (OPT_specs_
, spec_file
== NULL
? "libgcj.spec" : spec_file
,
589 1, CL_DRIVER
, &new_decoded_options
[j
++]);
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
++]);
603 generate_option (OPT_shared_libgcc
, NULL
, 1, CL_DRIVER
,
604 &new_decoded_options
[j
++]);
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)
619 if (main_class_name
== NULL
)
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
628 set_input (concat (main_class_name
, "main.c", NULL
));
629 err
= do_spec (jvgenmain_spec
);
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. */
637 const char *generated
= outfiles
[i
];
639 outfiles
[i
+ 1] = outfiles
[i
];
640 outfiles
[0] = generated
;