1 /* Specific flags and argument handling of the front-end of the
2 GNU compiler for the Java(TM) language.
3 Copyright (C) 1996-2015 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 (input_location
, "can%'t specify %<-D%> without %<--main%>");
397 if (main_class_name
&& ! verify_class_name (main_class_name
))
398 fatal_error (input_location
,
399 "%qs is not a valid class name", main_class_name
);
401 num_args
= argc
+ added
;
405 fatal_error (input_location
, "--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 (input_location
, "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 (input_location
, "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 (input_location
,
454 "using both @FILE with multiple files not implemented");
456 /* There's no point adding -shared-libgcc if we don't have a shared
458 #ifndef ENABLE_SHARED_LIBGCC
462 if (java_files_count
> 0)
465 num_args
+= shared_libgcc
;
467 num_args
+= link_for_bc_abi
;
469 new_decoded_options
= XNEWVEC (struct cl_decoded_option
, num_args
);
472 new_decoded_options
[j
++] = decoded_options
[0];
474 if (combine_inputs
|| indirect_files_count
> 0)
475 generate_option (OPT_ffilelist_file
, NULL
, 1, CL_DRIVER
,
476 &new_decoded_options
[j
++]);
480 generate_option (OPT_x
, "java", 1, CL_DRIVER
,
481 &new_decoded_options
[j
++]);
482 generate_option_input_file (filelist_filename
,
483 &new_decoded_options
[j
++]);
484 generate_option (OPT_x
, "none", 1, CL_DRIVER
,
485 &new_decoded_options
[j
++]);
488 if (java_files_count
> 0)
489 generate_option (OPT_fsaw_java_file
, NULL
, 1, CL_DRIVER
,
490 &new_decoded_options
[j
++]);
493 for (i
= 1; i
< argc
; i
++, j
++)
495 new_decoded_options
[j
] = decoded_options
[i
];
497 if (decoded_options
[i
].errors
& CL_ERR_MISSING_ARG
)
500 if ((args
[i
] & RESOURCE_FILE_ARG
) != 0)
502 generate_option (OPT_x
, "java", 1, CL_DRIVER
,
503 &new_decoded_options
[j
++]);
504 new_decoded_options
[j
++] = decoded_options
[i
];
505 generate_option (OPT_x
, "none", 1, CL_DRIVER
,
506 &new_decoded_options
[j
]);
509 switch (decoded_options
[i
].opt_index
)
512 jcf_path_include_arg (decoded_options
[i
].arg
);
516 case OPT_fclasspath_
:
517 jcf_path_classpath_arg (decoded_options
[i
].arg
);
521 case OPT_fbootclasspath_
:
522 jcf_path_bootclasspath_arg (decoded_options
[i
].arg
);
527 jcf_path_extdirs_arg (decoded_options
[i
].arg
);
532 if (spec_file
== NULL
)
533 spec_file
= find_spec_file (decoded_options
[i
].arg
);
538 fatal_error (input_location
,
539 "cannot specify %<main%> class when not linking");
544 if ((args
[i
] & INDIRECT_FILE_ARG
) != 0)
546 generate_option (OPT_x
, "java", 1, CL_DRIVER
,
547 &new_decoded_options
[j
++]);
549 generate_option_input_file (decoded_options
[i
].arg
+ 1,
550 &new_decoded_options
[j
++]);
551 generate_option (OPT_x
, "none", 1, CL_DRIVER
,
552 &new_decoded_options
[j
]);
555 if ((args
[i
] & (CLASS_FILE_ARG
|ZIP_FILE_ARG
)) && saw_C
)
562 && (args
[i
] & (CLASS_FILE_ARG
|JAVA_FILE_ARG
|ZIP_FILE_ARG
)) != 0)
564 fputs (decoded_options
[i
].arg
, filelist_file
);
565 fputc ('\n', filelist_file
);
571 /* Handle classpath setting. We specify the bootclasspath since
572 that requires the fewest changes to our existing code... */
574 generate_option (OPT_fbootclasspath_
, jcf_path_compute (""), 1,
575 CL_DRIVER
, &new_decoded_options
[j
++]);
579 if (fclose (filelist_file
))
580 pfatal_with_name (filelist_filename
);
583 /* If we saw no -O or -g option, default to -g1, for javac compatibility. */
584 if (saw_g
+ saw_O
== 0)
585 generate_option (OPT_g
, "1", 1, CL_DRIVER
, &new_decoded_options
[j
++]);
587 /* Read the specs file corresponding to libgcj.
588 If we didn't find the spec file on the -L path, then we hope it
589 is somewhere in the standard install areas. */
591 generate_option (OPT_specs_
, spec_file
== NULL
? "libgcj.spec" : spec_file
,
592 1, CL_DRIVER
, &new_decoded_options
[j
++]);
596 generate_option (OPT_fsyntax_only
, NULL
, 1, CL_DRIVER
,
597 &new_decoded_options
[j
++]);
598 generate_option (OPT_femit_class_files
, NULL
, 1, CL_DRIVER
,
599 &new_decoded_options
[j
++]);
600 generate_option (OPT_S
, NULL
, 1, CL_DRIVER
, &new_decoded_options
[j
++]);
601 generate_option (OPT_o
, "NONE", 1, CL_DRIVER
,
602 &new_decoded_options
[j
++]);
606 generate_option (OPT_shared_libgcc
, NULL
, 1, CL_DRIVER
,
607 &new_decoded_options
[j
++]);
610 generate_option (OPT_s_bc_abi
, NULL
, 1, CL_DRIVER
,
611 &new_decoded_options
[j
++]);
613 *in_decoded_options_count
= j
;
614 *in_decoded_options
= new_decoded_options
;
615 *in_added_libraries
= added_libraries
;
619 lang_specific_pre_link (void)
622 if (main_class_name
== NULL
)
624 /* Append `main' to make the filename unique and allow
626 gcj --main=hello -save-temps hello.java
628 to work. jvgenmain needs to strip this `main' to arrive at the correct
629 class name. Append dummy `.c' that can be stripped by set_input so %b
631 set_input (concat (main_class_name
, "main.c", NULL
));
632 err
= do_spec (jvgenmain_spec
);
635 /* Shift the outfiles array so the generated main comes first.
636 This is important when linking against (non-shared) libraries,
637 since otherwise we risk (a) nothing getting linked or
638 (b) 'main' getting picked up from a library. */
640 const char *generated
= outfiles
[i
];
642 outfiles
[i
+ 1] = outfiles
[i
];
643 outfiles
[0] = generated
;