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 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC 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 2, or (at your option)
12 GNU CC 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 GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
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. */
30 /* Name of spec file. */
31 #define SPEC_FILE "libgcj.spec"
33 /* This bit is set if we saw a `-xfoo' language specification. */
34 #define LANGSPEC (1<<1)
35 /* True if this arg is a parameter to the previous option-taking arg. */
36 #define PARAM_ARG (1<<2)
37 /* True if this arg is a .java input file name. */
38 #define JAVA_FILE_ARG (1<<3)
39 /* True if this arg is a .class input file name. */
40 #define CLASS_FILE_ARG (1<<4)
41 /* True if this arg is a .zip or .jar input file name. */
42 #define ZIP_FILE_ARG (1<<5)
43 /* True if this arg is @FILE - where FILE contains a list of filenames. */
44 #define INDIRECT_FILE_ARG (1<<6)
45 /* True if this arg is a resource file. */
46 #define RESOURCE_FILE_ARG (1<<7)
48 static char *find_spec_file
PARAMS ((const char *));
49 static int verify_class_name
PARAMS ((const char *));
51 static const char *main_class_name
= NULL
;
52 int lang_specific_extra_outfiles
= 0;
54 /* True if we should add -shared-libgcc to the command-line. */
55 int shared_libgcc
= 1;
57 static const char jvgenmain_spec
[] =
58 "jvgenmain %{D*} %b %{!pipe:%u.i} |\n\
59 cc1 %{!pipe:%U.i} %1 \
60 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
62 %{v:-version} %{pg:-p} %{p}\
63 %{<fbounds-check} %{<fno-bounds-check}\
64 %{<fassume-compiled} %{<fno-assume-compiled}\
65 %{<fcompile-resource*} %{<fassert} %{<fno-assert} \
66 %{<femit-class-file} %{<femit-class-files} %{<fencoding*}\
67 %{<fuse-boehm-gc} %{<fhash-synchronization} %{<fjni}\
68 %{<findirect-dispatch} \
69 %{<fno-store-check} %{<foutput-class-dir}\
70 %{<fclasspath*} %{<fCLASSPATH*} %{<fbootclasspath*}\
72 %{<fuse-divide-subroutine} %{<fno-use-divide-subroutine}\
73 %{<fcheck-references} %{<fno-check-references}\
75 %{f*} -fdollars-in-identifiers\
77 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
78 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
79 %{!S:as %a %Y -o %d%w%u%O %{!pipe:%g.s} %A\n }";
81 /* Return full path name of spec file if it is in DIR, or NULL if
91 spec
= xmalloc (strlen (dir
) + sizeof (SPEC_FILE
)
92 + sizeof ("-specs=") + 4);
93 strcpy (spec
, "-specs=");
97 strcat (spec
, SPEC_FILE
);
98 if (! stat (spec
+ x
, &sb
))
104 /* FIXME: these should come from lex.h. */
105 #define JAVA_START_CHAR_P(c) (c < 128 && (ISIDST (c) || c == '$'))
106 #define JAVA_PART_CHAR_P(c) (c < 128 \
109 || (c >= 0x00 && c <= 0x08) \
110 || (c >= 0x0e && c <= 0x1b) \
113 /* Verify that NAME is a valid Java class name that might contain
114 `main'. Return 0 on failure. */
116 verify_class_name (name
)
119 /* FIXME: what encoding do we use for command-line arguments? For
120 now we assume plain ASCII, which of course is wrong. */
124 if (ch
< 0 || ! JAVA_START_CHAR_P (ch
))
131 /* We found a break between class names. Next character
132 must be an identifier start again. */
135 if (! JAVA_PART_CHAR_P (ch
))
144 lang_specific_driver (in_argc
, in_argv
, in_added_libraries
)
146 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 /* Index of last .java or .class argument. */
165 int last_input_index
;
167 /* Number of .java and .class source file arguments seen. */
168 int java_files_count
= 0;
169 int class_files_count
= 0;
170 /* Number of .zip or .jar file arguments seen. */
171 int zip_files_count
= 0;
172 /* Number of '@FILES' arguments seen. */
173 int indirect_files_count
= 0;
175 /* Name of file containing list of files to compile. */
176 char *filelist_filename
= 0;
178 FILE *filelist_file
= 0;
180 /* The number of arguments being added to what's in argv, other than
184 /* Used to track options that take arguments, so we don't go wrapping
185 those with -xc++/-xnone. */
186 const char *quote
= NULL
;
188 /* The new argument list will be contained in this. */
189 const char **arglist
;
191 /* Nonzero if we saw a `-xfoo' language specification on the
192 command line. Used to avoid adding our own -xc++ if the user
193 already gave a language for the file. */
194 int saw_speclang
= 0;
197 /* "-lm" or "-lmath" if it appears on the command line. */
198 const char *saw_math ATTRIBUTE_UNUSED
= 0;
200 /* "-lc" if it appears on the command line. */
201 const char *saw_libc ATTRIBUTE_UNUSED
= 0;
203 /* "-lgcjgc" if it appears on the command line. */
204 const char *saw_gc ATTRIBUTE_UNUSED
= 0;
206 /* Saw `-l' option for the thread library. */
207 const char *saw_threadlib ATTRIBUTE_UNUSED
= 0;
209 /* Saw `-lgcj' on command line. */
210 int saw_libgcj ATTRIBUTE_UNUSED
= 0;
213 /* Saw --resource, -C or -o options, respectively. */
214 int saw_resource
= 0;
218 /* Saw some -O* or -g* option, respectively. */
222 /* Saw a `-D' option. */
225 /* An array used to flag each argument that needs a bit set for
226 LANGSPEC, MATHLIB, WITHLIBC, or GCLIB. */
229 /* The total number of arguments with the new stuff. */
232 /* The argument list. */
233 const char *const *argv
;
235 /* The number of libraries added in. */
238 /* The total number of arguments with the new stuff. */
241 /* Nonzero if linking is supposed to happen. */
244 /* Nonzero if we want to find the spec file. */
245 int want_spec_file
= 1;
247 /* The argument we use to specify the spec file. */
248 char *spec_file
= NULL
;
252 added_libraries
= *in_added_libraries
;
254 args
= xcalloc (argc
, sizeof (int));
256 for (i
= 1; i
< argc
; i
++)
258 /* If the previous option took an argument, we swallow it here. */
262 args
[i
] |= PARAM_ARG
;
266 /* We don't do this anymore, since we don't get them with minus
268 if (argv
[i
][0] == '\0' || argv
[i
][1] == '\0')
271 if (argv
[i
][0] == '-')
273 if (library
!= 0 && (strcmp (argv
[i
], "-nostdlib") == 0
274 || strcmp (argv
[i
], "-nodefaultlibs") == 0))
278 else if (strncmp (argv
[i
], "-fmain=", 7) == 0)
280 main_class_name
= argv
[i
] + 7;
283 else if (strcmp (argv
[i
], "-fhelp") == 0)
285 else if (strcmp (argv
[i
], "-v") == 0)
287 saw_verbose_flag
= 1;
290 /* If they only gave us `-v', don't try to link
295 else if (strncmp (argv
[i
], "-x", 2) == 0)
297 else if (strcmp (argv
[i
], "-C") == 0)
306 else if (strncmp (argv
[i
], "-fcompile-resource=", 19) == 0)
315 else if (argv
[i
][1] == 'D')
317 else if (argv
[i
][1] == 'g')
319 else if (argv
[i
][1] == 'O')
321 else if ((argv
[i
][2] == '\0'
322 && (char *)strchr ("bBVDUoeTuIYmLiA", argv
[i
][1]) != NULL
)
323 || strcmp (argv
[i
], "-Tdata") == 0
324 || strcmp (argv
[i
], "-MT") == 0
325 || strcmp (argv
[i
], "-MF") == 0)
327 if (strcmp (argv
[i
], "-o") == 0)
331 else if (strcmp(argv
[i
], "-classpath") == 0
332 || strcmp(argv
[i
], "-bootclasspath") == 0
333 || strcmp(argv
[i
], "-CLASSPATH") == 0)
338 else if (library
!= 0
339 && ((argv
[i
][2] == '\0'
340 && (char *) strchr ("cSEM", argv
[i
][1]) != NULL
)
341 || strcmp (argv
[i
], "-MM") == 0))
343 /* Don't specify libraries if we won't link, since that would
348 /* Remember this so we can confirm -fmain option. */
351 else if (strcmp (argv
[i
], "-d") == 0)
353 /* `-d' option is for javac compatibility. */
357 else if (strcmp (argv
[i
], "-fsyntax-only") == 0
358 || strcmp (argv
[i
], "--syntax-only") == 0)
365 else if (strcmp (argv
[i
], "-save-temps") == 0)
367 else if (strcmp (argv
[i
], "-static-libgcc") == 0
368 || strcmp (argv
[i
], "-static") == 0)
371 /* Pass other options through. */
386 args
[i
] |= RESOURCE_FILE_ARG
;
387 last_input_index
= i
;
388 added
+= 2; /* for -xjava and -xnone */
391 if (argv
[i
][0] == '@')
393 args
[i
] |= INDIRECT_FILE_ARG
;
394 indirect_files_count
++;
395 added
+= 2; /* for -xjava and -xnone */
398 len
= strlen (argv
[i
]);
399 if (len
> 5 && strcmp (argv
[i
] + len
- 5, ".java") == 0)
401 args
[i
] |= JAVA_FILE_ARG
;
403 last_input_index
= i
;
405 if (len
> 6 && strcmp (argv
[i
] + len
- 6, ".class") == 0)
407 args
[i
] |= CLASS_FILE_ARG
;
409 last_input_index
= i
;
412 && (strcmp (argv
[i
] + len
- 4, ".zip") == 0
413 || strcmp (argv
[i
] + len
- 4, ".jar") == 0))
415 args
[i
] |= ZIP_FILE_ARG
;
417 last_input_index
= i
;
423 fatal ("argument to `%s' missing\n", quote
);
425 if (saw_D
&& ! main_class_name
)
426 fatal ("can't specify `-D' without `--main'\n");
428 if (main_class_name
&& ! verify_class_name (main_class_name
))
429 fatal ("`%s' is not a valid class name", main_class_name
);
431 num_args
= argc
+ added
;
435 fatal ("--resource requires -o");
440 if (class_files_count
+ zip_files_count
> 0)
442 error ("warning: already-compiled .class files ignored with -C");
443 num_args
-= class_files_count
+ zip_files_count
;
444 class_files_count
= 0;
447 num_args
+= 2; /* For -o NONE. */
449 fatal ("cannot specify both -C and -o");
451 if ((saw_o
&& java_files_count
+ class_files_count
+ zip_files_count
> 1)
452 || (saw_C
&& java_files_count
> 1)
453 || (indirect_files_count
> 0
454 && java_files_count
+ class_files_count
+ zip_files_count
> 0))
459 filelist_filename
= make_temp_file ("jx");
460 if (filelist_filename
== NULL
)
461 fatal ("cannot create temporary file");
462 record_temp_file (filelist_filename
, ! saw_save_temps
, 0);
463 filelist_file
= fopen (filelist_filename
, "w");
464 if (filelist_file
== NULL
)
465 pfatal_with_name (filelist_filename
);
466 num_args
-= java_files_count
+ class_files_count
+ zip_files_count
;
467 num_args
+= 2; /* for the combined arg and "-xjava" */
469 /* If we know we don't have to do anything, bail now. */
471 if (! added
&& ! library
&& main_class_name
== NULL
&& ! saw_C
)
480 lang_specific_extra_outfiles
++;
482 if (saw_g
+ saw_O
== 0)
486 if (combine_inputs
|| indirect_files_count
> 0)
487 num_args
+= 1; /* for "-ffilelist-file" */
488 if (combine_inputs
&& indirect_files_count
> 0)
489 fatal("using both @FILE with multiple files not implemented");
491 /* There's no point adding -shared-libgcc if we don't have a shared
493 #ifndef ENABLE_SHARED_LIBGCC
497 num_args
+= shared_libgcc
;
499 arglist
= xmalloc ((num_args
+ 1) * sizeof (char *));
502 for (i
= 0; i
< argc
; i
++, j
++)
504 arglist
[j
] = argv
[i
];
506 if ((args
[i
] & PARAM_ARG
) || i
== 0)
509 if ((args
[i
] & RESOURCE_FILE_ARG
) != 0)
511 arglist
[j
++] = "-xjava";
512 arglist
[j
++] = argv
[i
];
513 arglist
[j
] = "-xnone";
516 if (strcmp (argv
[i
], "-classpath") == 0
517 || strcmp (argv
[i
], "-bootclasspath") == 0
518 || strcmp (argv
[i
], "-CLASSPATH") == 0)
520 arglist
[j
] = concat ("-f", argv
[i
]+1, "=", argv
[i
+1], NULL
);
525 if (strcmp (argv
[i
], "-d") == 0)
527 arglist
[j
] = concat ("-foutput-class-dir=", argv
[i
+ 1], NULL
);
532 if (spec_file
== NULL
&& strncmp (argv
[i
], "-L", 2) == 0)
533 spec_file
= find_spec_file (argv
[i
] + 2);
535 if (strncmp (argv
[i
], "-fmain=", 7) == 0)
538 fatal ("cannot specify `main' class when not linking");
543 if ((args
[i
] & INDIRECT_FILE_ARG
) != 0)
545 arglist
[j
++] = "-xjava";
546 arglist
[j
++] = argv
[i
]+1; /* Drop '@'. */
547 arglist
[j
] = "-xnone";
550 if ((args
[i
] & (CLASS_FILE_ARG
|ZIP_FILE_ARG
)) && saw_C
)
557 && (args
[i
] & (CLASS_FILE_ARG
|JAVA_FILE_ARG
|ZIP_FILE_ARG
)) != 0)
559 fputs (argv
[i
], filelist_file
);
560 fputc ('\n', filelist_file
);
566 if (combine_inputs
|| indirect_files_count
> 0)
567 arglist
[j
++] = "-ffilelist-file";
571 if (fclose (filelist_file
))
572 pfatal_with_name (filelist_filename
);
573 arglist
[j
++] = "-xjava";
574 arglist
[j
++] = filelist_filename
;
577 /* If we saw no -O or -g option, default to -g1, for javac compatibility. */
578 if (saw_g
+ saw_O
== 0)
579 arglist
[j
++] = "-g1";
581 /* Read the specs file corresponding to libgcj.
582 If we didn't find the spec file on the -L path, then we hope it
583 is somewhere in the standard install areas. */
585 arglist
[j
++] = spec_file
== NULL
? "-specs=libgcj.spec" : spec_file
;
589 arglist
[j
++] = "-fsyntax-only";
590 arglist
[j
++] = "-femit-class-files";
593 arglist
[j
++] = "NONE";
597 arglist
[j
++] = "-shared-libgcc";
603 *in_added_libraries
= added_libraries
;
607 lang_specific_pre_link ()
610 if (main_class_name
== NULL
)
612 /* Append `main' to make the filename unique and allow
614 gcj --main=hello -save-temps hello.java
616 to work. jvgenmain needs to strip this `main' to arrive at the correct
617 class name. Append dummy `.c' that can be stripped by set_input so %b
619 set_input (concat (main_class_name
, "main.c", NULL
));
620 err
= do_spec (jvgenmain_spec
);
623 /* Shift the outfiles array so the generated main comes first.
624 This is important when linking against (non-shared) libraries,
625 since otherwise we risk (a) nothing getting linked or
626 (b) 'main' getting picked up from a library. */
628 const char *generated
= outfiles
[i
];
630 outfiles
[i
+ 1] = outfiles
[i
];
631 outfiles
[0] = generated
;
636 /* Table of language-specific spec functions. */
637 const struct spec_function lang_specific_spec_functions
[] =