PR target/4198
[official-gcc.git] / gcc / java / jvspec.c
blob479916b7d659fba75108028a731cff18b48b063c
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 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)
11 any later version.
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, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, 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. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "gcc.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 parameter to the previous option-taking arg. */
39 #define PARAM_ARG (1<<2)
40 /* True if this arg is a .java input file name. */
41 #define JAVA_FILE_ARG (1<<3)
42 /* True if this arg is a .class input file name. */
43 #define CLASS_FILE_ARG (1<<4)
44 /* True if this arg is a .zip or .jar input file name. */
45 #define ZIP_FILE_ARG (1<<5)
46 /* True if this arg is @FILE - where FILE contains a list of filenames. */
47 #define INDIRECT_FILE_ARG (1<<6)
48 /* True if this arg is a resource file. */
49 #define RESOURCE_FILE_ARG (1<<7)
51 static char *find_spec_file (const char *);
52 static int verify_class_name (const char *);
54 static const char *main_class_name = NULL;
55 int lang_specific_extra_outfiles = 0;
57 /* True if we should add -shared-libgcc to the command-line. */
58 int shared_libgcc = 1;
60 static const char jvgenmain_spec[] =
61 "jvgenmain %{D*} %b %m.i |\n\
62 cc1 %m.i %1 \
63 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
64 %{g*} %{O*} \
65 %{v:-version} %{pg:-p} %{p}\
66 %<fbounds-check %<fno-bounds-check\
67 %<fassume-compiled* %<fno-assume-compiled*\
68 %<fcompile-resource* %<fassert %<fno-assert \
69 %<femit-class-file %<femit-class-files %<fencoding*\
70 %<fuse-boehm-gc %<fhash-synchronization %<fjni\
71 %<findirect-dispatch %<fnew-verifier\
72 %<fno-store-check %<foutput-class-dir\
73 %<fclasspath* %<fCLASSPATH* %<fbootclasspath*\
74 %<fextdirs*\
75 %<fuse-divide-subroutine %<fno-use-divide-subroutine\
76 %<fcheck-references %<fno-check-references\
77 %<ffilelist-file\
78 %{f*} -fdollars-in-identifiers\
79 %{aux-info*}\
80 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
81 %{S:%W{o*}%{!o*:-o %b.s}}\
82 %(invoke_as)";
84 /* Return full path name of spec file if it is in DIR, or NULL if
85 not. */
86 static char *
87 find_spec_file (const char *dir)
89 char *spec;
90 int x;
91 struct stat sb;
93 spec = xmalloc (strlen (dir) + sizeof (SPEC_FILE)
94 + sizeof ("-specs=") + 4);
95 strcpy (spec, "-specs=");
96 x = strlen (spec);
97 strcat (spec, dir);
98 strcat (spec, "/");
99 strcat (spec, SPEC_FILE);
100 if (! stat (spec + x, &sb))
101 return spec;
102 free (spec);
103 return NULL;
106 /* FIXME: these should come from lex.h. */
107 #define JAVA_START_CHAR_P(c) (c < 128 && (ISIDST (c) || c == '$'))
108 #define JAVA_PART_CHAR_P(c) (c < 128 \
109 && (ISIDNUM (c) \
110 || c == '$' \
111 || (c >= 0x00 && c <= 0x08) \
112 || (c >= 0x0e && c <= 0x1b) \
113 || c == 0x7f))
115 /* Verify that NAME is a valid Java class name that might contain
116 `main'. Return 0 on failure. */
117 static int
118 verify_class_name (const char *name)
120 /* FIXME: what encoding do we use for command-line arguments? For
121 now we assume plain ASCII, which of course is wrong. */
122 while (*name)
124 int ch = *name++;
125 if (ch < 0 || ! JAVA_START_CHAR_P (ch))
126 return 0;
127 while (*name)
129 ch = *name++;
130 if (ch < 0)
131 return 0;
132 /* We found a break between class names. Next character
133 must be an identifier start again. */
134 if (ch == '.')
135 break;
136 if (! JAVA_PART_CHAR_P (ch))
137 return 0;
141 return 1;
144 void
145 lang_specific_driver (int *in_argc, const char *const **in_argv,
146 int *in_added_libraries)
148 int i, j;
150 /* If nonzero, the user gave us the `-v' flag. */
151 int saw_verbose_flag = 0;
153 int saw_save_temps = 0;
155 /* This will be 0 if we encounter a situation where we should not
156 link in libgcj. */
157 int library = 1;
159 /* This will be 1 if multiple input files (.class and/or .java)
160 should be passed to a single jc1 invocation. */
161 int combine_inputs = 0;
163 /* Number of .java and .class source file arguments seen. */
164 int java_files_count = 0;
165 int class_files_count = 0;
166 /* Number of .zip or .jar file arguments seen. */
167 int zip_files_count = 0;
168 /* Number of '@FILES' arguments seen. */
169 int indirect_files_count = 0;
171 /* Name of file containing list of files to compile. */
172 char *filelist_filename = 0;
174 FILE *filelist_file = 0;
176 /* The number of arguments being added to what's in argv, other than
177 libraries. */
178 int added = 2;
180 /* Used to track options that take arguments, so we don't go wrapping
181 those with -xc++/-xnone. */
182 const char *quote = NULL;
184 /* The new argument list will be contained in this. */
185 const char **arglist;
187 /* Nonzero if we saw a `-xfoo' language specification on the
188 command line. Used to avoid adding our own -xc++ if the user
189 already gave a language for the file. */
190 int saw_speclang = 0;
192 #if 0
193 /* "-lm" or "-lmath" if it appears on the command line. */
194 const char *saw_math ATTRIBUTE_UNUSED = 0;
196 /* "-lc" if it appears on the command line. */
197 const char *saw_libc ATTRIBUTE_UNUSED = 0;
199 /* "-lgcjgc" if it appears on the command line. */
200 const char *saw_gc ATTRIBUTE_UNUSED = 0;
202 /* Saw `-l' option for the thread library. */
203 const char *saw_threadlib ATTRIBUTE_UNUSED = 0;
205 /* Saw `-lgcj' on command line. */
206 int saw_libgcj ATTRIBUTE_UNUSED = 0;
207 #endif
209 /* Saw --resource, -C or -o options, respectively. */
210 int saw_resource = 0;
211 int saw_C = 0;
212 int saw_o = 0;
214 /* Saw some -O* or -g* option, respectively. */
215 int saw_O = 0;
216 int saw_g = 0;
218 /* Saw a `-D' option. */
219 int saw_D = 0;
221 /* An array used to flag each argument that needs a bit set for
222 LANGSPEC, MATHLIB, WITHLIBC, or GCLIB. */
223 int *args;
225 /* The total number of arguments with the new stuff. */
226 int argc;
228 /* The argument list. */
229 const char *const *argv;
231 /* The number of libraries added in. */
232 int added_libraries;
234 /* The total number of arguments with the new stuff. */
235 int num_args = 1;
237 /* Nonzero if linking is supposed to happen. */
238 int will_link = 1;
240 /* Nonzero if we want to find the spec file. */
241 int want_spec_file = 1;
243 /* The argument we use to specify the spec file. */
244 char *spec_file = NULL;
246 argc = *in_argc;
247 argv = *in_argv;
248 added_libraries = *in_added_libraries;
250 args = xcalloc (argc, sizeof (int));
252 for (i = 1; i < argc; i++)
254 /* If the previous option took an argument, we swallow it here. */
255 if (quote)
257 quote = NULL;
258 args[i] |= PARAM_ARG;
259 continue;
262 /* We don't do this anymore, since we don't get them with minus
263 signs on them. */
264 if (argv[i][0] == '\0' || argv[i][1] == '\0')
265 continue;
267 if (argv[i][0] == '-')
269 if (library != 0 && (strcmp (argv[i], "-nostdlib") == 0
270 || strcmp (argv[i], "-nodefaultlibs") == 0))
272 library = 0;
274 else if (strncmp (argv[i], "-fmain=", 7) == 0)
276 main_class_name = argv[i] + 7;
277 added--;
279 else if (strcmp (argv[i], "-fhelp") == 0)
280 want_spec_file = 0;
281 else if (strcmp (argv[i], "-v") == 0)
283 saw_verbose_flag = 1;
284 if (argc == 2)
286 /* If they only gave us `-v', don't try to link
287 in libgcj. */
288 library = 0;
291 else if (strncmp (argv[i], "-x", 2) == 0)
292 saw_speclang = 1;
293 else if (strcmp (argv[i], "-C") == 0)
295 saw_C = 1;
296 want_spec_file = 0;
297 if (library != 0)
298 added -= 2;
299 library = 0;
300 will_link = 0;
302 else if (strncmp (argv[i], "-fcompile-resource=", 19) == 0)
304 saw_resource = 1;
305 want_spec_file = 0;
306 if (library != 0)
307 --added;
308 library = 0;
309 will_link = 0;
311 else if (argv[i][1] == 'D')
312 saw_D = 1;
313 else if (argv[i][1] == 'g')
314 saw_g = 1;
315 else if (argv[i][1] == 'O')
316 saw_O = 1;
317 else if ((argv[i][2] == '\0'
318 && strchr ("bBVDUoeTuIYmLiA", argv[i][1]) != NULL)
319 || strcmp (argv[i], "-Tdata") == 0
320 || strcmp (argv[i], "-MT") == 0
321 || strcmp (argv[i], "-MF") == 0)
323 if (strcmp (argv[i], "-o") == 0)
324 saw_o = 1;
325 quote = argv[i];
327 else if (strcmp (argv[i], "-classpath") == 0
328 || strcmp (argv[i], "-bootclasspath") == 0
329 || strcmp (argv[i], "-CLASSPATH") == 0
330 || strcmp (argv[i], "-encoding") == 0
331 || strcmp (argv[i], "-extdirs") == 0)
333 quote = argv[i];
334 added -= 1;
336 else if (library != 0
337 && ((argv[i][2] == '\0'
338 && strchr ("cSEM", argv[i][1]) != NULL)
339 || strcmp (argv[i], "-MM") == 0))
341 /* Don't specify libraries if we won't link, since that would
342 cause a warning. */
343 library = 0;
344 added -= 2;
346 /* Remember this so we can confirm -fmain option. */
347 will_link = 0;
349 else if (strcmp (argv[i], "-d") == 0)
351 /* `-d' option is for javac compatibility. */
352 quote = argv[i];
353 added -= 1;
355 else if (strcmp (argv[i], "-fsyntax-only") == 0
356 || strcmp (argv[i], "--syntax-only") == 0)
358 want_spec_file = 0;
359 library = 0;
360 will_link = 0;
361 continue;
363 else if (strcmp (argv[i], "-save-temps") == 0)
364 saw_save_temps = 1;
365 else if (strcmp (argv[i], "-static-libgcc") == 0
366 || strcmp (argv[i], "-static") == 0)
367 shared_libgcc = 0;
368 else
369 /* Pass other options through. */
370 continue;
372 else
374 int len;
376 if (saw_speclang)
378 saw_speclang = 0;
379 continue;
382 if (saw_resource)
384 args[i] |= RESOURCE_FILE_ARG;
385 added += 2; /* for -xjava and -xnone */
388 if (argv[i][0] == '@')
390 args[i] |= INDIRECT_FILE_ARG;
391 indirect_files_count++;
392 added += 2; /* for -xjava and -xnone */
395 len = strlen (argv[i]);
396 if (len > 5 && strcmp (argv[i] + len - 5, ".java") == 0)
398 args[i] |= JAVA_FILE_ARG;
399 java_files_count++;
401 if (len > 6 && strcmp (argv[i] + len - 6, ".class") == 0)
403 args[i] |= CLASS_FILE_ARG;
404 class_files_count++;
406 if (len > 4
407 && (strcmp (argv[i] + len - 4, ".zip") == 0
408 || strcmp (argv[i] + len - 4, ".jar") == 0))
410 args[i] |= ZIP_FILE_ARG;
411 zip_files_count++;
416 if (quote)
417 fatal ("argument to '%s' missing\n", quote);
419 if (saw_D && ! main_class_name)
420 fatal ("can't specify '-D' without '--main'\n");
422 if (main_class_name && ! verify_class_name (main_class_name))
423 fatal ("'%s' is not a valid class name", main_class_name);
425 num_args = argc + added;
426 if (saw_resource)
428 if (! saw_o)
429 fatal ("--resource requires -o");
431 if (saw_C)
433 num_args += 3;
434 if (class_files_count + zip_files_count > 0)
436 error ("warning: already-compiled .class files ignored with -C");
437 num_args -= class_files_count + zip_files_count;
438 class_files_count = 0;
439 zip_files_count = 0;
441 num_args += 2; /* For -o NONE. */
442 if (saw_o)
443 fatal ("cannot specify both -C and -o");
445 if ((saw_o && java_files_count + class_files_count + zip_files_count > 1)
446 || (saw_C && java_files_count > 1)
447 || (indirect_files_count > 0
448 && java_files_count + class_files_count + zip_files_count > 0))
449 combine_inputs = 1;
451 if (combine_inputs)
453 filelist_filename = make_temp_file ("jx");
454 if (filelist_filename == NULL)
455 fatal ("cannot create temporary file");
456 record_temp_file (filelist_filename, ! saw_save_temps, 0);
457 filelist_file = fopen (filelist_filename, "w");
458 if (filelist_file == NULL)
459 pfatal_with_name (filelist_filename);
460 num_args -= java_files_count + class_files_count + zip_files_count;
461 num_args += 2; /* for the combined arg and "-xjava" */
463 /* If we know we don't have to do anything, bail now. */
464 #if 0
465 if (! added && ! library && main_class_name == NULL && ! saw_C)
467 free (args);
468 return;
470 #endif
472 if (main_class_name)
474 lang_specific_extra_outfiles++;
476 if (saw_g + saw_O == 0)
477 num_args++;
478 num_args++;
480 if (combine_inputs || indirect_files_count > 0)
481 num_args += 1; /* for "-ffilelist-file" */
482 if (combine_inputs && indirect_files_count > 0)
483 fatal("using both @FILE with multiple files not implemented");
485 /* There's no point adding -shared-libgcc if we don't have a shared
486 libgcc. */
487 #ifndef ENABLE_SHARED_LIBGCC
488 shared_libgcc = 0;
489 #endif
491 num_args += shared_libgcc;
493 arglist = xmalloc ((num_args + 1) * sizeof (char *));
494 j = 0;
496 for (i = 0; i < argc; i++, j++)
498 arglist[j] = argv[i];
500 if ((args[i] & PARAM_ARG) || i == 0)
501 continue;
503 if ((args[i] & RESOURCE_FILE_ARG) != 0)
505 arglist[j++] = "-xjava";
506 arglist[j++] = argv[i];
507 arglist[j] = "-xnone";
510 if (strcmp (argv[i], "-classpath") == 0
511 || strcmp (argv[i], "-bootclasspath") == 0
512 || strcmp (argv[i], "-CLASSPATH") == 0
513 || strcmp (argv[i], "-encoding") == 0
514 || strcmp (argv[i], "-extdirs") == 0)
516 arglist[j] = concat ("-f", argv[i]+1, "=", argv[i+1], NULL);
517 i++;
518 continue;
521 if (strcmp (argv[i], "-d") == 0)
523 arglist[j] = concat ("-foutput-class-dir=", argv[i + 1], NULL);
524 ++i;
525 continue;
528 if (spec_file == NULL && strncmp (argv[i], "-L", 2) == 0)
529 spec_file = find_spec_file (argv[i] + 2);
531 if (strncmp (argv[i], "-fmain=", 7) == 0)
533 if (! will_link)
534 fatal ("cannot specify 'main' class when not linking");
535 --j;
536 continue;
539 if ((args[i] & INDIRECT_FILE_ARG) != 0)
541 arglist[j++] = "-xjava";
542 arglist[j++] = argv[i]+1; /* Drop '@'. */
543 arglist[j] = "-xnone";
546 if ((args[i] & (CLASS_FILE_ARG|ZIP_FILE_ARG)) && saw_C)
548 --j;
549 continue;
552 if (combine_inputs
553 && (args[i] & (CLASS_FILE_ARG|JAVA_FILE_ARG|ZIP_FILE_ARG)) != 0)
555 fputs (argv[i], filelist_file);
556 fputc ('\n', filelist_file);
557 --j;
558 continue;
562 if (combine_inputs || indirect_files_count > 0)
563 arglist[j++] = "-ffilelist-file";
565 if (combine_inputs)
567 if (fclose (filelist_file))
568 pfatal_with_name (filelist_filename);
569 arglist[j++] = "-xjava";
570 arglist[j++] = filelist_filename;
573 /* If we saw no -O or -g option, default to -g1, for javac compatibility. */
574 if (saw_g + saw_O == 0)
575 arglist[j++] = "-g1";
577 /* Read the specs file corresponding to libgcj.
578 If we didn't find the spec file on the -L path, then we hope it
579 is somewhere in the standard install areas. */
580 if (want_spec_file)
581 arglist[j++] = spec_file == NULL ? "-specs=libgcj.spec" : spec_file;
583 if (saw_C)
585 arglist[j++] = "-fsyntax-only";
586 arglist[j++] = "-femit-class-files";
587 arglist[j++] = "-S";
588 arglist[j++] = "-o";
589 arglist[j++] = "NONE";
592 if (shared_libgcc)
593 arglist[j++] = "-shared-libgcc";
595 arglist[j] = NULL;
597 *in_argc = j;
598 *in_argv = arglist;
599 *in_added_libraries = added_libraries;
603 lang_specific_pre_link (void)
605 int err;
606 if (main_class_name == NULL)
607 return 0;
608 /* Append `main' to make the filename unique and allow
610 gcj --main=hello -save-temps hello.java
612 to work. jvgenmain needs to strip this `main' to arrive at the correct
613 class name. Append dummy `.c' that can be stripped by set_input so %b
614 is correct. */
615 set_input (concat (main_class_name, "main.c", NULL));
616 err = do_spec (jvgenmain_spec);
617 if (err == 0)
619 /* Shift the outfiles array so the generated main comes first.
620 This is important when linking against (non-shared) libraries,
621 since otherwise we risk (a) nothing getting linked or
622 (b) 'main' getting picked up from a library. */
623 int i = n_infiles;
624 const char *generated = outfiles[i];
625 while (--i >= 0)
626 outfiles[i + 1] = outfiles[i];
627 outfiles[0] = generated;
629 return err;
632 /* Table of language-specific spec functions. */
633 const struct spec_function lang_specific_spec_functions[] =
635 { 0, 0 }