2008-05-20 Kai Tietz <kai.tietz@onevision.com>
[official-gcc.git] / gcc / java / jvspec.c
blob24f2e166a6408d0bc21d2d8a9cf9f5fc650a130f
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 2005, 2006, 2007 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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>.
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. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "gcc.h"
31 #include "jcf.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 %{findirect-dispatch} %{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 %<fsaw-java-file %<fsource* %<ftarget*\
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 = XNEWVEC (char, 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 #define JAVA_START_CHAR_P(c) (c < 128 && (ISIDST (c) || c == '$'))
107 #define JAVA_PART_CHAR_P(c) (c < 128 \
108 && (ISIDNUM (c) \
109 || c == '$' \
110 || (c >= 0x00 && c <= 0x08) \
111 || (c >= 0x0e && c <= 0x1b) \
112 || c == 0x7f))
114 /* Verify that NAME is a valid Java class name that might contain
115 `main'. Return 0 on failure. */
116 static int
117 verify_class_name (const char *name)
119 /* FIXME: what encoding do we use for command-line arguments? For
120 now we assume plain ASCII, which of course is wrong. */
121 while (*name)
123 int ch = *name++;
124 if (ch < 0 || ! JAVA_START_CHAR_P (ch))
125 return 0;
126 while (*name)
128 ch = *name++;
129 if (ch < 0)
130 return 0;
131 /* We found a break between class names. Next character
132 must be an identifier start again. */
133 if (ch == '.')
134 break;
135 if (! JAVA_PART_CHAR_P (ch))
136 return 0;
140 return 1;
143 void
144 lang_specific_driver (int *in_argc, const char *const **in_argv,
145 int *in_added_libraries)
147 int i, j;
149 /* If nonzero, the user gave us the `-v' flag. */
150 int saw_verbose_flag = 0;
152 int saw_save_temps = 0;
154 /* This will be 0 if we encounter a situation where we should not
155 link in libgcj. */
156 int library = 1;
158 /* This will be 1 if multiple input files (.class and/or .java)
159 should be passed to a single jc1 invocation. */
160 int combine_inputs = 0;
162 /* Number of .java and .class source file arguments seen. */
163 int java_files_count = 0;
164 int class_files_count = 0;
165 /* Number of .zip or .jar file arguments seen. */
166 int zip_files_count = 0;
167 /* Number of '@FILES' arguments seen. */
168 int indirect_files_count = 0;
170 /* Name of file containing list of files to compile. */
171 char *filelist_filename = 0;
173 FILE *filelist_file = 0;
175 /* The number of arguments being added to what's in argv, other than
176 libraries. */
177 int added = 2;
179 /* Used to track options that take arguments, so we don't go wrapping
180 those with -xc++/-xnone. */
181 const char *quote = NULL;
183 /* The new argument list will be contained in this. */
184 const char **arglist;
186 /* Nonzero if we saw a `-xfoo' language specification on the
187 command line. Used to avoid adding our own -xc++ if the user
188 already gave a language for the file. */
189 int saw_speclang = 0;
191 /* Saw --resource, -C or -o options, respectively. */
192 int saw_resource = 0;
193 int saw_C = 0;
194 int saw_o = 0;
196 /* Saw some -O* or -g* option, respectively. */
197 int saw_O = 0;
198 int saw_g = 0;
200 /* Saw a `-D' option. */
201 int saw_D = 0;
203 /* An array used to flag each argument that needs a bit set for
204 LANGSPEC, MATHLIB, WITHLIBC, or GCLIB. */
205 int *args;
207 /* The total number of arguments with the new stuff. */
208 int argc;
210 /* The argument list. */
211 const char *const *argv;
213 /* The number of libraries added in. */
214 int added_libraries;
216 /* The total number of arguments with the new stuff. */
217 int num_args = 1;
219 /* Nonzero if linking is supposed to happen. */
220 int will_link = 1;
222 /* Nonzero if we want to find the spec file. */
223 int want_spec_file = 1;
225 /* The argument we use to specify the spec file. */
226 char *spec_file = NULL;
228 /* If linking, nonzero if the BC-ABI is in use. */
229 int link_for_bc_abi = 0;
231 argc = *in_argc;
232 argv = *in_argv;
233 added_libraries = *in_added_libraries;
235 args = XCNEWVEC (int, argc);
237 for (i = 1; i < argc; i++)
239 /* If the previous option took an argument, we swallow it here. */
240 if (quote)
242 quote = NULL;
243 args[i] |= PARAM_ARG;
244 continue;
247 /* We don't do this anymore, since we don't get them with minus
248 signs on them. */
249 if (argv[i][0] == '\0' || argv[i][1] == '\0')
250 continue;
252 if (argv[i][0] == '-')
254 if (library != 0 && (strcmp (argv[i], "-nostdlib") == 0
255 || strcmp (argv[i], "-nodefaultlibs") == 0))
257 library = 0;
259 else if (strncmp (argv[i], "-fmain=", 7) == 0)
261 main_class_name = argv[i] + 7;
262 added--;
264 else if (strcmp (argv[i], "-fhelp") == 0)
265 want_spec_file = 0;
266 else if (strcmp (argv[i], "-v") == 0)
268 saw_verbose_flag = 1;
269 if (argc == 2)
271 /* If they only gave us `-v', don't try to link
272 in libgcj. */
273 library = 0;
276 else if (strncmp (argv[i], "-x", 2) == 0)
277 saw_speclang = 1;
278 else if (strcmp (argv[i], "-C") == 0)
280 saw_C = 1;
281 want_spec_file = 0;
282 if (library != 0)
283 added -= 2;
284 library = 0;
285 will_link = 0;
287 else if (strncmp (argv[i], "-fcompile-resource=", 19) == 0)
289 saw_resource = 1;
290 want_spec_file = 0;
291 if (library != 0)
292 --added;
293 library = 0;
294 will_link = 0;
296 else if (argv[i][1] == 'D')
297 saw_D = 1;
298 else if (argv[i][1] == 'g')
299 saw_g = 1;
300 else if (argv[i][1] == 'O')
301 saw_O = 1;
302 else if ((argv[i][2] == '\0'
303 && strchr ("bBVDUoeTuIYmLiAI", argv[i][1]) != NULL)
304 || strcmp (argv[i], "-Tdata") == 0
305 || strcmp (argv[i], "-MT") == 0
306 || strcmp (argv[i], "-MF") == 0)
308 if (strcmp (argv[i], "-o") == 0)
309 saw_o = 1;
310 quote = argv[i];
312 else if (strcmp (argv[i], "-classpath") == 0
313 || strcmp (argv[i], "-bootclasspath") == 0
314 || strcmp (argv[i], "-CLASSPATH") == 0
315 || strcmp (argv[i], "-encoding") == 0
316 || strcmp (argv[i], "-extdirs") == 0)
318 quote = argv[i];
319 added -= 1;
321 else if (library != 0
322 && ((argv[i][2] == '\0'
323 && strchr ("cSEM", argv[i][1]) != NULL)
324 || strcmp (argv[i], "-MM") == 0))
326 /* Don't specify libraries if we won't link, since that would
327 cause a warning. */
328 library = 0;
329 added -= 2;
331 /* Remember this so we can confirm -fmain option. */
332 will_link = 0;
334 else if (strcmp (argv[i], "-d") == 0)
336 /* `-d' option is for javac compatibility. */
337 quote = argv[i];
338 added -= 1;
340 else if (strcmp (argv[i], "-fsyntax-only") == 0
341 || strcmp (argv[i], "--syntax-only") == 0)
343 library = 0;
344 will_link = 0;
345 continue;
347 else if (strcmp (argv[i], "-save-temps") == 0)
348 saw_save_temps = 1;
349 else if (strcmp (argv[i], "-static-libgcc") == 0
350 || strcmp (argv[i], "-static") == 0)
351 shared_libgcc = 0;
352 else if (strcmp (argv[i], "-findirect-dispatch") == 0
353 || strcmp (argv[i], "--indirect-dispatch") == 0)
355 link_for_bc_abi = 1;
357 else
358 /* Pass other options through. */
359 continue;
361 else
363 int len;
365 if (saw_speclang)
367 saw_speclang = 0;
368 continue;
371 if (saw_resource)
373 args[i] |= RESOURCE_FILE_ARG;
374 added += 2; /* for -xjava and -xnone */
377 if (argv[i][0] == '@')
379 args[i] |= INDIRECT_FILE_ARG;
380 indirect_files_count++;
381 added += 2; /* for -xjava and -xnone */
384 len = strlen (argv[i]);
385 if (len > 5 && strcmp (argv[i] + len - 5, ".java") == 0)
387 args[i] |= JAVA_FILE_ARG;
388 java_files_count++;
390 if (len > 6 && strcmp (argv[i] + len - 6, ".class") == 0)
392 args[i] |= CLASS_FILE_ARG;
393 class_files_count++;
395 if (len > 4
396 && (strcmp (argv[i] + len - 4, ".zip") == 0
397 || strcmp (argv[i] + len - 4, ".jar") == 0))
399 args[i] |= ZIP_FILE_ARG;
400 zip_files_count++;
405 if (quote)
406 fatal ("argument to '%s' missing\n", quote);
408 if (saw_D && ! main_class_name)
409 fatal ("can't specify '-D' without '--main'\n");
411 if (main_class_name && ! verify_class_name (main_class_name))
412 fatal ("'%s' is not a valid class name", main_class_name);
414 num_args = argc + added;
415 if (saw_resource)
417 if (! saw_o)
418 fatal ("--resource requires -o");
420 if (saw_C)
422 num_args += 3;
423 if (class_files_count + zip_files_count > 0)
425 error ("warning: already-compiled .class files ignored with -C");
426 num_args -= class_files_count + zip_files_count;
427 class_files_count = 0;
428 zip_files_count = 0;
430 num_args += 2; /* For -o NONE. */
431 if (saw_o)
432 fatal ("cannot specify both -C and -o");
434 if ((saw_o && java_files_count + class_files_count + zip_files_count > 1)
435 || (saw_C && java_files_count > 1)
436 || (indirect_files_count > 0
437 && java_files_count + class_files_count + zip_files_count > 0))
438 combine_inputs = 1;
440 if (combine_inputs)
442 filelist_filename = make_temp_file ("jx");
443 if (filelist_filename == NULL)
444 fatal ("cannot create temporary file");
445 record_temp_file (filelist_filename, ! saw_save_temps, 0);
446 filelist_file = fopen (filelist_filename, "w");
447 if (filelist_file == NULL)
448 pfatal_with_name (filelist_filename);
449 num_args -= java_files_count + class_files_count + zip_files_count;
450 num_args += 3; /* for the combined arg "-xjava", and "-xnone" */
453 if (main_class_name)
455 lang_specific_extra_outfiles++;
457 if (saw_g + saw_O == 0)
458 num_args++;
459 num_args++;
460 /* An additional entry for the classpath. */
461 num_args++;
463 if (combine_inputs || indirect_files_count > 0)
464 num_args += 1; /* for "-ffilelist-file" */
465 if (combine_inputs && indirect_files_count > 0)
466 fatal("using both @FILE with multiple files not implemented");
468 /* There's no point adding -shared-libgcc if we don't have a shared
469 libgcc. */
470 #ifndef ENABLE_SHARED_LIBGCC
471 shared_libgcc = 0;
472 #endif
474 if (java_files_count > 0)
475 ++num_args;
477 num_args += shared_libgcc;
479 num_args += link_for_bc_abi;
481 arglist = XNEWVEC (const char *, num_args + 1);
482 j = 0;
484 arglist[j++] = argv[0];
486 if (combine_inputs || indirect_files_count > 0)
487 arglist[j++] = "-ffilelist-file";
489 if (combine_inputs)
491 arglist[j++] = "-xjava";
492 arglist[j++] = filelist_filename;
493 arglist[j++] = "-xnone";
496 if (java_files_count > 0)
497 arglist[j++] = "-fsaw-java-file";
499 jcf_path_init ();
500 for (i = 1; i < argc; i++, j++)
502 arglist[j] = argv[i];
504 if ((args[i] & PARAM_ARG))
505 continue;
507 if ((args[i] & RESOURCE_FILE_ARG) != 0)
509 arglist[j++] = "-xjava";
510 arglist[j++] = argv[i];
511 arglist[j] = "-xnone";
514 if (argv[i][0] == '-' && argv[i][1] == 'I')
516 const char *arg;
517 if (argv[i][2] == '\0')
519 gcc_assert (i + 1 < argc && (args[i + 1] & PARAM_ARG) != 0);
520 arg = argv[i + 1];
521 /* Drop the argument. */
522 ++i;
524 else
525 arg = &argv[i][2];
526 jcf_path_include_arg (arg);
527 --j;
528 continue;
530 if (! strcmp (argv[i], "-classpath")
531 || ! strcmp (argv[i], "-CLASSPATH"))
533 jcf_path_classpath_arg (argv[i + 1]);
534 ++i;
535 --j;
536 continue;
538 if (! strcmp (argv[i], "-bootclasspath"))
540 jcf_path_bootclasspath_arg (argv[i + 1]);
541 ++i;
542 --j;
543 continue;
545 if (! strncmp (argv[i], "-fCLASSPATH=", 12)
546 || ! strncmp (argv[i], "-fclasspath=", 12))
548 char *p = strchr (argv[i], '=');
549 jcf_path_classpath_arg (p + 1);
550 --j;
551 continue;
553 if (! strncmp (argv[i], "-fbootclasspath=", 16))
555 char *p = strchr (argv[i], '=');
556 jcf_path_bootclasspath_arg (p + 1);
557 --j;
558 continue;
560 if (! strcmp (argv[i], "-extdirs"))
562 jcf_path_extdirs_arg (argv[i + 1]);
563 ++i;
564 --j;
565 continue;
568 if (strcmp (argv[i], "-encoding") == 0)
570 arglist[j] = concat ("-f", argv[i]+1, "=", argv[i+1], NULL);
571 i++;
572 continue;
575 if (strcmp (argv[i], "-d") == 0)
577 arglist[j] = concat ("-foutput-class-dir=", argv[i + 1], NULL);
578 ++i;
579 continue;
582 if (spec_file == NULL && strncmp (argv[i], "-L", 2) == 0)
583 spec_file = find_spec_file (argv[i] + 2);
585 if (strncmp (argv[i], "-fmain=", 7) == 0)
587 if (! will_link)
588 fatal ("cannot specify 'main' class when not linking");
589 --j;
590 continue;
593 if ((args[i] & INDIRECT_FILE_ARG) != 0)
595 arglist[j++] = "-xjava";
596 arglist[j++] = argv[i]+1; /* Drop '@'. */
597 arglist[j] = "-xnone";
600 if ((args[i] & (CLASS_FILE_ARG|ZIP_FILE_ARG)) && saw_C)
602 --j;
603 continue;
606 if (combine_inputs
607 && (args[i] & (CLASS_FILE_ARG|JAVA_FILE_ARG|ZIP_FILE_ARG)) != 0)
609 fputs (argv[i], filelist_file);
610 fputc ('\n', filelist_file);
611 --j;
612 continue;
616 /* Handle classpath setting. We specify the bootclasspath since
617 that requires the fewest changes to our existing code... */
618 jcf_path_seal (0);
619 arglist[j++] = jcf_path_compute ("-fbootclasspath=");
621 if (combine_inputs)
623 if (fclose (filelist_file))
624 pfatal_with_name (filelist_filename);
627 /* If we saw no -O or -g option, default to -g1, for javac compatibility. */
628 if (saw_g + saw_O == 0)
629 arglist[j++] = "-g1";
631 /* Read the specs file corresponding to libgcj.
632 If we didn't find the spec file on the -L path, then we hope it
633 is somewhere in the standard install areas. */
634 if (want_spec_file)
635 arglist[j++] = spec_file == NULL ? "-specs=libgcj.spec" : spec_file;
637 if (saw_C)
639 arglist[j++] = "-fsyntax-only";
640 arglist[j++] = "-femit-class-files";
641 arglist[j++] = "-S";
642 arglist[j++] = "-o";
643 arglist[j++] = "NONE";
646 if (shared_libgcc)
647 arglist[j++] = "-shared-libgcc";
649 if (link_for_bc_abi)
650 arglist[j++] = "-s-bc-abi";
652 arglist[j] = NULL;
654 *in_argc = j;
655 *in_argv = arglist;
656 *in_added_libraries = added_libraries;
660 lang_specific_pre_link (void)
662 int err;
663 if (main_class_name == NULL)
664 return 0;
665 /* Append `main' to make the filename unique and allow
667 gcj --main=hello -save-temps hello.java
669 to work. jvgenmain needs to strip this `main' to arrive at the correct
670 class name. Append dummy `.c' that can be stripped by set_input so %b
671 is correct. */
672 set_input (concat (main_class_name, "main.c", NULL));
673 err = do_spec (jvgenmain_spec);
674 if (err == 0)
676 /* Shift the outfiles array so the generated main comes first.
677 This is important when linking against (non-shared) libraries,
678 since otherwise we risk (a) nothing getting linked or
679 (b) 'main' getting picked up from a library. */
680 int i = n_infiles;
681 const char *generated = outfiles[i];
682 while (--i >= 0)
683 outfiles[i + 1] = outfiles[i];
684 outfiles[0] = generated;
686 return err;