2006-08-07 Andrew John Hughes <gnu_andrew@member.fsf.org>
[official-gcc.git] / gcc / java / jvspec.c
blob0643143c82ee7983d4947742c3a079e5d65e0073
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)
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, 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. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "gcc.h"
32 #include "jcf.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\
63 cc1 %m.i %1 \
64 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
65 %{g*} %{O*} \
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*\
75 %<fextdirs*\
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\
80 %{aux-info*}\
81 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
82 %{S:%W{o*}%{!o*:-o %b.s}}\
83 %(invoke_as)";
85 /* Return full path name of spec file if it is in DIR, or NULL if
86 not. */
87 static char *
88 find_spec_file (const char *dir)
90 char *spec;
91 int x;
92 struct stat sb;
94 spec = XNEWVEC (char, strlen (dir) + sizeof (SPEC_FILE)
95 + sizeof ("-specs=") + 4);
96 strcpy (spec, "-specs=");
97 x = strlen (spec);
98 strcat (spec, dir);
99 strcat (spec, "/");
100 strcat (spec, SPEC_FILE);
101 if (! stat (spec + x, &sb))
102 return spec;
103 free (spec);
104 return NULL;
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 \
110 && (ISIDNUM (c) \
111 || c == '$' \
112 || (c >= 0x00 && c <= 0x08) \
113 || (c >= 0x0e && c <= 0x1b) \
114 || c == 0x7f))
116 /* Verify that NAME is a valid Java class name that might contain
117 `main'. Return 0 on failure. */
118 static int
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. */
123 while (*name)
125 int ch = *name++;
126 if (ch < 0 || ! JAVA_START_CHAR_P (ch))
127 return 0;
128 while (*name)
130 ch = *name++;
131 if (ch < 0)
132 return 0;
133 /* We found a break between class names. Next character
134 must be an identifier start again. */
135 if (ch == '.')
136 break;
137 if (! JAVA_PART_CHAR_P (ch))
138 return 0;
142 return 1;
145 void
146 lang_specific_driver (int *in_argc, const char *const **in_argv,
147 int *in_added_libraries)
149 int i, j;
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
157 link in libgcj. */
158 int library = 1;
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
178 libraries. */
179 int added = 2;
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;
195 int saw_C = 0;
196 int saw_o = 0;
198 /* Saw some -O* or -g* option, respectively. */
199 int saw_O = 0;
200 int saw_g = 0;
202 /* Saw a `-D' option. */
203 int saw_D = 0;
205 /* An array used to flag each argument that needs a bit set for
206 LANGSPEC, MATHLIB, WITHLIBC, or GCLIB. */
207 int *args;
209 /* The total number of arguments with the new stuff. */
210 int argc;
212 /* The argument list. */
213 const char *const *argv;
215 /* The number of libraries added in. */
216 int added_libraries;
218 /* The total number of arguments having to do with classpath
219 setting. */
220 int classpath_args = 0;
222 /* The total number of arguments with the new stuff. */
223 int num_args = 1;
225 /* Nonzero if linking is supposed to happen. */
226 int will_link = 1;
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;
234 argc = *in_argc;
235 argv = *in_argv;
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. */
243 if (quote)
245 quote = NULL;
246 args[i] |= PARAM_ARG;
247 continue;
250 /* We don't do this anymore, since we don't get them with minus
251 signs on them. */
252 if (argv[i][0] == '\0' || argv[i][1] == '\0')
253 continue;
255 if (argv[i][0] == '-')
257 if (library != 0 && (strcmp (argv[i], "-nostdlib") == 0
258 || strcmp (argv[i], "-nodefaultlibs") == 0))
260 library = 0;
262 else if (strncmp (argv[i], "-fmain=", 7) == 0)
264 main_class_name = argv[i] + 7;
265 added--;
267 else if (strcmp (argv[i], "-fhelp") == 0)
268 want_spec_file = 0;
269 else if (strcmp (argv[i], "-v") == 0)
271 saw_verbose_flag = 1;
272 if (argc == 2)
274 /* If they only gave us `-v', don't try to link
275 in libgcj. */
276 library = 0;
279 else if (strncmp (argv[i], "-x", 2) == 0)
280 saw_speclang = 1;
281 else if (strcmp (argv[i], "-C") == 0)
283 saw_C = 1;
284 want_spec_file = 0;
285 if (library != 0)
286 added -= 2;
287 library = 0;
288 will_link = 0;
290 else if (strncmp (argv[i], "-fcompile-resource=", 19) == 0)
292 saw_resource = 1;
293 want_spec_file = 0;
294 if (library != 0)
295 --added;
296 library = 0;
297 will_link = 0;
299 else if (argv[i][1] == 'D')
300 saw_D = 1;
301 else if (argv[i][1] == 'g')
302 saw_g = 1;
303 else if (argv[i][1] == 'O')
304 saw_O = 1;
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)
312 saw_o = 1;
313 quote = argv[i];
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)
321 quote = argv[i];
322 added -= 1;
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
330 cause a warning. */
331 library = 0;
332 added -= 2;
334 /* Remember this so we can confirm -fmain option. */
335 will_link = 0;
337 else if (strcmp (argv[i], "-d") == 0)
339 /* `-d' option is for javac compatibility. */
340 quote = argv[i];
341 added -= 1;
343 else if (strcmp (argv[i], "-fsyntax-only") == 0
344 || strcmp (argv[i], "--syntax-only") == 0)
346 want_spec_file = 0;
347 library = 0;
348 will_link = 0;
349 continue;
351 else if (strcmp (argv[i], "-save-temps") == 0)
352 saw_save_temps = 1;
353 else if (strcmp (argv[i], "-static-libgcc") == 0
354 || strcmp (argv[i], "-static") == 0)
355 shared_libgcc = 0;
356 else
357 /* Pass other options through. */
358 continue;
360 else
362 int len;
364 if (saw_speclang)
366 saw_speclang = 0;
367 continue;
370 if (saw_resource)
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;
387 java_files_count++;
389 if (len > 6 && strcmp (argv[i] + len - 6, ".class") == 0)
391 args[i] |= CLASS_FILE_ARG;
392 class_files_count++;
394 if (len > 4
395 && (strcmp (argv[i] + len - 4, ".zip") == 0
396 || strcmp (argv[i] + len - 4, ".jar") == 0))
398 args[i] |= ZIP_FILE_ARG;
399 zip_files_count++;
404 if (quote)
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;
414 if (saw_resource)
416 if (! saw_o)
417 fatal ("--resource requires -o");
419 if (saw_C)
421 num_args += 3;
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;
427 zip_files_count = 0;
429 num_args += 2; /* For -o NONE. */
430 if (saw_o)
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))
437 combine_inputs = 1;
439 if (combine_inputs)
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" */
452 if (main_class_name)
454 lang_specific_extra_outfiles++;
456 if (saw_g + saw_O == 0)
457 num_args++;
458 num_args++;
459 /* An additional entry for the classpath. */
460 num_args++;
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
468 libgcc. */
469 #ifndef ENABLE_SHARED_LIBGCC
470 shared_libgcc = 0;
471 #endif
473 if (java_files_count > 0)
474 ++num_args;
476 num_args += shared_libgcc;
478 arglist = XNEWVEC (const char *, num_args + 1);
479 j = 0;
481 arglist[j++] = argv[0];
483 if (combine_inputs || indirect_files_count > 0)
484 arglist[j++] = "-ffilelist-file";
486 if (combine_inputs)
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";
496 jcf_path_init ();
497 for (i = 1; i < argc; i++, j++)
499 arglist[j] = argv[i];
501 if ((args[i] & PARAM_ARG))
502 continue;
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]);
514 --j;
515 continue;
517 if (! strcmp (argv[i], "-classpath")
518 || ! strcmp (argv[i], "-CLASSPATH"))
520 jcf_path_classpath_arg (argv[i + 1]);
521 ++i;
522 --j;
523 continue;
525 if (! strcmp (argv[i], "-bootclasspath"))
527 jcf_path_bootclasspath_arg (argv[i + 1]);
528 ++i;
529 --j;
530 continue;
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);
537 --j;
538 continue;
540 if (! strncmp (argv[i], "-fbootclasspath=", 16))
542 char *p = strchr (argv[i], '=');
543 jcf_path_bootclasspath_arg (p + 1);
544 --j;
545 continue;
547 if (! strcmp (argv[i], "-extdirs"))
549 jcf_path_extdirs_arg (argv[i + 1]);
550 ++i;
551 --j;
552 continue;
555 if (strcmp (argv[i], "-encoding") == 0)
557 arglist[j] = concat ("-f", argv[i]+1, "=", argv[i+1], NULL);
558 i++;
559 continue;
562 if (strcmp (argv[i], "-d") == 0)
564 arglist[j] = concat ("-foutput-class-dir=", argv[i + 1], NULL);
565 ++i;
566 continue;
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)
574 if (! will_link)
575 fatal ("cannot specify 'main' class when not linking");
576 --j;
577 continue;
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)
589 --j;
590 continue;
593 if (combine_inputs
594 && (args[i] & (CLASS_FILE_ARG|JAVA_FILE_ARG|ZIP_FILE_ARG)) != 0)
596 fputs (argv[i], filelist_file);
597 fputc ('\n', filelist_file);
598 --j;
599 continue;
603 /* Handle classpath setting. We specify the bootclasspath since
604 that requires the fewest changes to our existing code... */
605 jcf_path_seal (0);
606 arglist[j++] = jcf_path_compute ("-fbootclasspath=");
608 if (combine_inputs)
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. */
621 if (want_spec_file)
622 arglist[j++] = spec_file == NULL ? "-specs=libgcj.spec" : spec_file;
624 if (saw_C)
626 arglist[j++] = "-fsyntax-only";
627 arglist[j++] = "-femit-class-files";
628 arglist[j++] = "-S";
629 arglist[j++] = "-o";
630 arglist[j++] = "NONE";
633 if (shared_libgcc)
634 arglist[j++] = "-shared-libgcc";
636 arglist[j] = NULL;
638 *in_argc = j;
639 *in_argv = arglist;
640 *in_added_libraries = added_libraries;
644 lang_specific_pre_link (void)
646 int err;
647 if (main_class_name == NULL)
648 return 0;
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
655 is correct. */
656 set_input (concat (main_class_name, "main.c", NULL));
657 err = do_spec (jvgenmain_spec);
658 if (err == 0)
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. */
664 int i = n_infiles;
665 const char *generated = outfiles[i];
666 while (--i >= 0)
667 outfiles[i + 1] = outfiles[i];
668 outfiles[0] = generated;
670 return err;