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, 2005, 2006
4 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 2, 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 COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.
23 Java and all Java-based marks are trademarks or registered trademarks
24 of Sun Microsystems, Inc. in the United States and other countries.
25 The Free Software Foundation is independent of Sun Microsystems, Inc. */
29 #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 parameter to the previous option-taking arg. */
40 #define PARAM_ARG (1<<2)
41 /* True if this arg is a .java input file name. */
42 #define JAVA_FILE_ARG (1<<3)
43 /* True if this arg is a .class input file name. */
44 #define CLASS_FILE_ARG (1<<4)
45 /* True if this arg is a .zip or .jar input file name. */
46 #define ZIP_FILE_ARG (1<<5)
47 /* True if this arg is @FILE - where FILE contains a list of filenames. */
48 #define INDIRECT_FILE_ARG (1<<6)
49 /* True if this arg is a resource file. */
50 #define RESOURCE_FILE_ARG (1<<7)
52 static char *find_spec_file (const char *);
53 static int verify_class_name (const char *);
55 static const char *main_class_name
= NULL
;
56 int lang_specific_extra_outfiles
= 0;
58 /* True if we should add -shared-libgcc to the command-line. */
59 int shared_libgcc
= 1;
61 static const char jvgenmain_spec
[] =
62 "jvgenmain %{D*} %b %m.i |\n\
64 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
66 %{v:-version} %{pg:-p} %{p}\
67 %<fbounds-check %<fno-bounds-check\
68 %<fassume-compiled* %<fno-assume-compiled*\
69 %<fcompile-resource* %<fassert %<fno-assert \
70 %<femit-class-file %<femit-class-files %<fencoding*\
71 %<fuse-boehm-gc %<fhash-synchronization %<fjni\
72 %<findirect-dispatch %<fnew-verifier\
73 %<fno-store-check %<foutput-class-dir\
74 %<fclasspath* %<fCLASSPATH* %<fbootclasspath*\
76 %<fuse-divide-subroutine %<fno-use-divide-subroutine\
77 %<fcheck-references %<fno-check-references\
78 %<ffilelist-file %<fsaw-java-file %<fsource* %<ftarget*\
79 %{f*} -fdollars-in-identifiers\
81 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
82 %{S:%W{o*}%{!o*:-o %b.s}}\
85 /* Return full path name of spec file if it is in DIR, or NULL if
88 find_spec_file (const char *dir
)
94 spec
= XNEWVEC (char, strlen (dir
) + sizeof (SPEC_FILE
)
95 + sizeof ("-specs=") + 4);
96 strcpy (spec
, "-specs=");
100 strcat (spec
, SPEC_FILE
);
101 if (! stat (spec
+ x
, &sb
))
107 /* FIXME: these should come from lex.h. */
108 #define JAVA_START_CHAR_P(c) (c < 128 && (ISIDST (c) || c == '$'))
109 #define JAVA_PART_CHAR_P(c) (c < 128 \
112 || (c >= 0x00 && c <= 0x08) \
113 || (c >= 0x0e && c <= 0x1b) \
116 /* Verify that NAME is a valid Java class name that might contain
117 `main'. Return 0 on failure. */
119 verify_class_name (const char *name
)
121 /* FIXME: what encoding do we use for command-line arguments? For
122 now we assume plain ASCII, which of course is wrong. */
126 if (ch
< 0 || ! JAVA_START_CHAR_P (ch
))
133 /* We found a break between class names. Next character
134 must be an identifier start again. */
137 if (! JAVA_PART_CHAR_P (ch
))
146 lang_specific_driver (int *in_argc
, const char *const **in_argv
,
147 int *in_added_libraries
)
151 /* If nonzero, the user gave us the `-v' flag. */
152 int saw_verbose_flag
= 0;
154 int saw_save_temps
= 0;
156 /* This will be 0 if we encounter a situation where we should not
160 /* This will be 1 if multiple input files (.class and/or .java)
161 should be passed to a single jc1 invocation. */
162 int combine_inputs
= 0;
164 /* Number of .java and .class source file arguments seen. */
165 int java_files_count
= 0;
166 int class_files_count
= 0;
167 /* Number of .zip or .jar file arguments seen. */
168 int zip_files_count
= 0;
169 /* Number of '@FILES' arguments seen. */
170 int indirect_files_count
= 0;
172 /* Name of file containing list of files to compile. */
173 char *filelist_filename
= 0;
175 FILE *filelist_file
= 0;
177 /* The number of arguments being added to what's in argv, other than
181 /* Used to track options that take arguments, so we don't go wrapping
182 those with -xc++/-xnone. */
183 const char *quote
= NULL
;
185 /* The new argument list will be contained in this. */
186 const char **arglist
;
188 /* Nonzero if we saw a `-xfoo' language specification on the
189 command line. Used to avoid adding our own -xc++ if the user
190 already gave a language for the file. */
191 int saw_speclang
= 0;
193 /* Saw --resource, -C or -o options, respectively. */
194 int saw_resource
= 0;
198 /* Saw some -O* or -g* option, respectively. */
202 /* Saw a `-D' option. */
205 /* An array used to flag each argument that needs a bit set for
206 LANGSPEC, MATHLIB, WITHLIBC, or GCLIB. */
209 /* The total number of arguments with the new stuff. */
212 /* The argument list. */
213 const char *const *argv
;
215 /* The number of libraries added in. */
218 /* The total number of arguments having to do with classpath
220 int classpath_args
= 0;
222 /* The total number of arguments with the new stuff. */
225 /* Nonzero if linking is supposed to happen. */
228 /* Nonzero if we want to find the spec file. */
229 int want_spec_file
= 1;
231 /* The argument we use to specify the spec file. */
232 char *spec_file
= NULL
;
236 added_libraries
= *in_added_libraries
;
238 args
= XCNEWVEC (int, argc
);
240 for (i
= 1; i
< argc
; i
++)
242 /* If the previous option took an argument, we swallow it here. */
246 args
[i
] |= PARAM_ARG
;
250 /* We don't do this anymore, since we don't get them with minus
252 if (argv
[i
][0] == '\0' || argv
[i
][1] == '\0')
255 if (argv
[i
][0] == '-')
257 if (library
!= 0 && (strcmp (argv
[i
], "-nostdlib") == 0
258 || strcmp (argv
[i
], "-nodefaultlibs") == 0))
262 else if (strncmp (argv
[i
], "-fmain=", 7) == 0)
264 main_class_name
= argv
[i
] + 7;
267 else if (strcmp (argv
[i
], "-fhelp") == 0)
269 else if (strcmp (argv
[i
], "-v") == 0)
271 saw_verbose_flag
= 1;
274 /* If they only gave us `-v', don't try to link
279 else if (strncmp (argv
[i
], "-x", 2) == 0)
281 else if (strcmp (argv
[i
], "-C") == 0)
290 else if (strncmp (argv
[i
], "-fcompile-resource=", 19) == 0)
299 else if (argv
[i
][1] == 'D')
301 else if (argv
[i
][1] == 'g')
303 else if (argv
[i
][1] == 'O')
305 else if ((argv
[i
][2] == '\0'
306 && strchr ("bBVDUoeTuIYmLiA", argv
[i
][1]) != NULL
)
307 || strcmp (argv
[i
], "-Tdata") == 0
308 || strcmp (argv
[i
], "-MT") == 0
309 || strcmp (argv
[i
], "-MF") == 0)
311 if (strcmp (argv
[i
], "-o") == 0)
315 else if (strcmp (argv
[i
], "-classpath") == 0
316 || strcmp (argv
[i
], "-bootclasspath") == 0
317 || strcmp (argv
[i
], "-CLASSPATH") == 0
318 || strcmp (argv
[i
], "-encoding") == 0
319 || strcmp (argv
[i
], "-extdirs") == 0)
324 else if (library
!= 0
325 && ((argv
[i
][2] == '\0'
326 && strchr ("cSEM", argv
[i
][1]) != NULL
)
327 || strcmp (argv
[i
], "-MM") == 0))
329 /* Don't specify libraries if we won't link, since that would
334 /* Remember this so we can confirm -fmain option. */
337 else if (strcmp (argv
[i
], "-d") == 0)
339 /* `-d' option is for javac compatibility. */
343 else if (strcmp (argv
[i
], "-fsyntax-only") == 0
344 || strcmp (argv
[i
], "--syntax-only") == 0)
351 else if (strcmp (argv
[i
], "-save-temps") == 0)
353 else if (strcmp (argv
[i
], "-static-libgcc") == 0
354 || strcmp (argv
[i
], "-static") == 0)
357 /* Pass other options through. */
372 args
[i
] |= RESOURCE_FILE_ARG
;
373 added
+= 2; /* for -xjava and -xnone */
376 if (argv
[i
][0] == '@')
378 args
[i
] |= INDIRECT_FILE_ARG
;
379 indirect_files_count
++;
380 added
+= 2; /* for -xjava and -xnone */
383 len
= strlen (argv
[i
]);
384 if (len
> 5 && strcmp (argv
[i
] + len
- 5, ".java") == 0)
386 args
[i
] |= JAVA_FILE_ARG
;
389 if (len
> 6 && strcmp (argv
[i
] + len
- 6, ".class") == 0)
391 args
[i
] |= CLASS_FILE_ARG
;
395 && (strcmp (argv
[i
] + len
- 4, ".zip") == 0
396 || strcmp (argv
[i
] + len
- 4, ".jar") == 0))
398 args
[i
] |= ZIP_FILE_ARG
;
405 fatal ("argument to '%s' missing\n", quote
);
407 if (saw_D
&& ! main_class_name
)
408 fatal ("can't specify '-D' without '--main'\n");
410 if (main_class_name
&& ! verify_class_name (main_class_name
))
411 fatal ("'%s' is not a valid class name", main_class_name
);
413 num_args
= argc
+ added
;
417 fatal ("--resource requires -o");
422 if (class_files_count
+ zip_files_count
> 0)
424 error ("warning: already-compiled .class files ignored with -C");
425 num_args
-= class_files_count
+ zip_files_count
;
426 class_files_count
= 0;
429 num_args
+= 2; /* For -o NONE. */
431 fatal ("cannot specify both -C and -o");
433 if ((saw_o
&& java_files_count
+ class_files_count
+ zip_files_count
> 1)
434 || (saw_C
&& java_files_count
> 1)
435 || (indirect_files_count
> 0
436 && java_files_count
+ class_files_count
+ zip_files_count
> 0))
441 filelist_filename
= make_temp_file ("jx");
442 if (filelist_filename
== NULL
)
443 fatal ("cannot create temporary file");
444 record_temp_file (filelist_filename
, ! saw_save_temps
, 0);
445 filelist_file
= fopen (filelist_filename
, "w");
446 if (filelist_file
== NULL
)
447 pfatal_with_name (filelist_filename
);
448 num_args
-= java_files_count
+ class_files_count
+ zip_files_count
;
449 num_args
+= 3; /* for the combined arg "-xjava", and "-xnone" */
454 lang_specific_extra_outfiles
++;
456 if (saw_g
+ saw_O
== 0)
459 /* An additional entry for the classpath. */
462 if (combine_inputs
|| indirect_files_count
> 0)
463 num_args
+= 1; /* for "-ffilelist-file" */
464 if (combine_inputs
&& indirect_files_count
> 0)
465 fatal("using both @FILE with multiple files not implemented");
467 /* There's no point adding -shared-libgcc if we don't have a shared
469 #ifndef ENABLE_SHARED_LIBGCC
473 if (java_files_count
> 0)
476 num_args
+= shared_libgcc
;
478 arglist
= XNEWVEC (const char *, num_args
+ 1);
481 arglist
[j
++] = argv
[0];
483 if (combine_inputs
|| indirect_files_count
> 0)
484 arglist
[j
++] = "-ffilelist-file";
488 arglist
[j
++] = "-xjava";
489 arglist
[j
++] = filelist_filename
;
490 arglist
[j
++] = "-xnone";
493 if (java_files_count
> 0)
494 arglist
[j
++] = "-fsaw-java-file";
497 for (i
= 1; i
< argc
; i
++, j
++)
499 arglist
[j
] = argv
[i
];
501 if ((args
[i
] & PARAM_ARG
))
504 if ((args
[i
] & RESOURCE_FILE_ARG
) != 0)
506 arglist
[j
++] = "-xjava";
507 arglist
[j
++] = argv
[i
];
508 arglist
[j
] = "-xnone";
511 if (argv
[i
][1] == 'I')
513 jcf_path_include_arg (&argv
[i
][2]);
517 if (! strcmp (argv
[i
], "-classpath")
518 || ! strcmp (argv
[i
], "-CLASSPATH"))
520 jcf_path_classpath_arg (argv
[i
+ 1]);
525 if (! strcmp (argv
[i
], "-bootclasspath"))
527 jcf_path_bootclasspath_arg (argv
[i
+ 1]);
532 if (! strncmp (argv
[i
], "-fCLASSPATH=", 12)
533 || ! strncmp (argv
[i
], "-fclasspath=", 12))
535 char *p
= strchr (argv
[i
], '=');
536 jcf_path_classpath_arg (p
+ 1);
540 if (! strncmp (argv
[i
], "-fbootclasspath=", 16))
542 char *p
= strchr (argv
[i
], '=');
543 jcf_path_bootclasspath_arg (p
+ 1);
547 if (! strcmp (argv
[i
], "-extdirs"))
549 jcf_path_extdirs_arg (argv
[i
+ 1]);
555 if (strcmp (argv
[i
], "-encoding") == 0)
557 arglist
[j
] = concat ("-f", argv
[i
]+1, "=", argv
[i
+1], NULL
);
562 if (strcmp (argv
[i
], "-d") == 0)
564 arglist
[j
] = concat ("-foutput-class-dir=", argv
[i
+ 1], NULL
);
569 if (spec_file
== NULL
&& strncmp (argv
[i
], "-L", 2) == 0)
570 spec_file
= find_spec_file (argv
[i
] + 2);
572 if (strncmp (argv
[i
], "-fmain=", 7) == 0)
575 fatal ("cannot specify 'main' class when not linking");
580 if ((args
[i
] & INDIRECT_FILE_ARG
) != 0)
582 arglist
[j
++] = "-xjava";
583 arglist
[j
++] = argv
[i
]+1; /* Drop '@'. */
584 arglist
[j
] = "-xnone";
587 if ((args
[i
] & (CLASS_FILE_ARG
|ZIP_FILE_ARG
)) && saw_C
)
594 && (args
[i
] & (CLASS_FILE_ARG
|JAVA_FILE_ARG
|ZIP_FILE_ARG
)) != 0)
596 fputs (argv
[i
], filelist_file
);
597 fputc ('\n', filelist_file
);
603 /* Handle classpath setting. We specify the bootclasspath since
604 that requires the fewest changes to our existing code... */
606 arglist
[j
++] = jcf_path_compute ("-fbootclasspath=");
610 if (fclose (filelist_file
))
611 pfatal_with_name (filelist_filename
);
614 /* If we saw no -O or -g option, default to -g1, for javac compatibility. */
615 if (saw_g
+ saw_O
== 0)
616 arglist
[j
++] = "-g1";
618 /* Read the specs file corresponding to libgcj.
619 If we didn't find the spec file on the -L path, then we hope it
620 is somewhere in the standard install areas. */
622 arglist
[j
++] = spec_file
== NULL
? "-specs=libgcj.spec" : spec_file
;
626 arglist
[j
++] = "-fsyntax-only";
627 arglist
[j
++] = "-femit-class-files";
630 arglist
[j
++] = "NONE";
634 arglist
[j
++] = "-shared-libgcc";
640 *in_added_libraries
= added_libraries
;
644 lang_specific_pre_link (void)
647 if (main_class_name
== NULL
)
649 /* Append `main' to make the filename unique and allow
651 gcj --main=hello -save-temps hello.java
653 to work. jvgenmain needs to strip this `main' to arrive at the correct
654 class name. Append dummy `.c' that can be stripped by set_input so %b
656 set_input (concat (main_class_name
, "main.c", NULL
));
657 err
= do_spec (jvgenmain_spec
);
660 /* Shift the outfiles array so the generated main comes first.
661 This is important when linking against (non-shared) libraries,
662 since otherwise we risk (a) nothing getting linked or
663 (b) 'main' getting picked up from a library. */
665 const char *generated
= outfiles
[i
];
667 outfiles
[i
+ 1] = outfiles
[i
];
668 outfiles
[0] = generated
;