2002-04-02 David S. Miller <davem@redhat.com>
[official-gcc.git] / gcc / java / jvspec.c
blob58aa1c6fc789cc928ce61c6bb962569a9d616499
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)
10 any later version.
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. */
26 #include "config.h"
27 #include "system.h"
28 #include "gcc.h"
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*}\
61 %{g*} %{O*} \
62 %{v:-version} %{pg:-p} %{p}\
63 %{<fbounds-check} %{<fno-bounds-check}\
64 %{<fassume-compiled} %{<fno-assume-compiled}\
65 %{<fcompile-resource*}\
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*}\
71 %{<fuse-divide-subroutine} %{<fno-use-divide-subroutine}\
72 %{<fcheck-references} %{<fno-check-references}\
73 %{<ffilelist-file}\
74 %{f*} -fdollars-in-identifiers\
75 %{aux-info*}\
76 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
77 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
78 %{!S:as %a %Y -o %d%w%u%O %{!pipe:%g.s} %A\n }";
80 /* Return full path name of spec file if it is in DIR, or NULL if
81 not. */
82 static char *
83 find_spec_file (dir)
84 const char *dir;
86 char *spec;
87 int x;
88 struct stat sb;
90 spec = (char *) xmalloc (strlen (dir) + sizeof (SPEC_FILE)
91 + sizeof ("-specs=") + 4);
92 strcpy (spec, "-specs=");
93 x = strlen (spec);
94 strcat (spec, dir);
95 strcat (spec, "/");
96 strcat (spec, SPEC_FILE);
97 if (! stat (spec + x, &sb))
98 return spec;
99 free (spec);
100 return NULL;
103 /* FIXME: these should come from lex.h. */
104 #define JAVA_START_CHAR_P(c) (c < 128 && (ISIDST (c) || c == '$'))
105 #define JAVA_PART_CHAR_P(c) (c < 128 \
106 && (ISIDNUM (c) \
107 || c == '$' \
108 || (c >= 0x00 && c <= 0x08) \
109 || (c >= 0x0e && c <= 0x1b) \
110 || c == 0x7f))
112 /* Verify that NAME is a valid Java class name that might contain
113 `main'. Return 0 on failure. */
114 static int
115 verify_class_name (name)
116 const char *name;
118 /* FIXME: what encoding do we use for command-line arguments? For
119 now we assume plain ASCII, which of course is wrong. */
120 while (*name)
122 int ch = *name++;
123 if (ch < 0 || ! JAVA_START_CHAR_P (ch))
124 return 0;
125 while (*name)
127 ch = *name++;
128 if (ch < 0)
129 return 0;
130 /* We found a break between class names. Next character
131 must be an identifier start again. */
132 if (ch == '.')
133 break;
134 if (! JAVA_PART_CHAR_P (ch))
135 return 0;
139 return 1;
142 void
143 lang_specific_driver (in_argc, in_argv, in_added_libraries)
144 int *in_argc;
145 const char *const **in_argv;
146 int *in_added_libraries;
148 int i, j;
150 /* If non-zero, 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 /* Index of last .java or .class argument. */
164 int last_input_index;
166 /* Number of .java and .class source file arguments seen. */
167 int java_files_count = 0;
168 int class_files_count = 0;
169 /* Number of .zip or .jar file arguments seen. */
170 int zip_files_count = 0;
171 /* Number of '@FILES' arguments seen. */
172 int indirect_files_count = 0;
174 /* Name of file containing list of files to compile. */
175 char *filelist_filename = 0;
177 FILE *filelist_file = 0;
179 /* The number of arguments being added to what's in argv, other than
180 libraries. */
181 int added = 2;
183 /* Used to track options that take arguments, so we don't go wrapping
184 those with -xc++/-xnone. */
185 const char *quote = NULL;
187 /* The new argument list will be contained in this. */
188 const char **arglist;
190 /* Non-zero if we saw a `-xfoo' language specification on the
191 command line. Used to avoid adding our own -xc++ if the user
192 already gave a language for the file. */
193 int saw_speclang = 0;
195 #if 0
196 /* "-lm" or "-lmath" if it appears on the command line. */
197 const char *saw_math ATTRIBUTE_UNUSED = 0;
199 /* "-lc" if it appears on the command line. */
200 const char *saw_libc ATTRIBUTE_UNUSED = 0;
202 /* "-lgcjgc" if it appears on the command line. */
203 const char *saw_gc ATTRIBUTE_UNUSED = 0;
205 /* Saw `-l' option for the thread library. */
206 const char *saw_threadlib ATTRIBUTE_UNUSED = 0;
208 /* Saw `-lgcj' on command line. */
209 int saw_libgcj ATTRIBUTE_UNUSED = 0;
210 #endif
212 /* Saw -R, -C or -o options, respectively. */
213 int saw_R = 0;
214 int saw_C = 0;
215 int saw_o = 0;
217 /* Saw some -O* or -g* option, respectively. */
218 int saw_O = 0;
219 int saw_g = 0;
221 /* Saw a `-D' option. */
222 int saw_D = 0;
224 /* An array used to flag each argument that needs a bit set for
225 LANGSPEC, MATHLIB, WITHLIBC, or GCLIB. */
226 int *args;
228 /* The total number of arguments with the new stuff. */
229 int argc;
231 /* The argument list. */
232 const char *const *argv;
234 /* The number of libraries added in. */
235 int added_libraries;
237 /* The total number of arguments with the new stuff. */
238 int num_args = 1;
240 /* Non-zero if linking is supposed to happen. */
241 int will_link = 1;
243 /* Non-zero if we want to find the spec file. */
244 int want_spec_file = 1;
246 /* The argument we use to specify the spec file. */
247 char *spec_file = NULL;
249 argc = *in_argc;
250 argv = *in_argv;
251 added_libraries = *in_added_libraries;
253 args = (int *) xcalloc (argc, sizeof (int));
255 for (i = 1; i < argc; i++)
257 /* If the previous option took an argument, we swallow it here. */
258 if (quote)
260 quote = NULL;
261 args[i] |= PARAM_ARG;
262 continue;
265 /* We don't do this anymore, since we don't get them with minus
266 signs on them. */
267 if (argv[i][0] == '\0' || argv[i][1] == '\0')
268 continue;
270 if (argv[i][0] == '-')
272 if (library != 0 && (strcmp (argv[i], "-nostdlib") == 0
273 || strcmp (argv[i], "-nodefaultlibs") == 0))
275 library = 0;
277 else if (strncmp (argv[i], "-fmain=", 7) == 0)
279 main_class_name = argv[i] + 7;
280 added--;
282 else if (strcmp (argv[i], "-fhelp") == 0)
283 want_spec_file = 0;
284 else if (strcmp (argv[i], "-v") == 0)
286 saw_verbose_flag = 1;
287 if (argc == 2)
289 /* If they only gave us `-v', don't try to link
290 in libgcj. */
291 library = 0;
294 else if (strncmp (argv[i], "-x", 2) == 0)
295 saw_speclang = 1;
296 else if (strcmp (argv[i], "-C") == 0)
298 saw_C = 1;
299 want_spec_file = 0;
300 if (library != 0)
301 added -= 2;
302 library = 0;
303 will_link = 0;
305 else if (strcmp (argv[i], "-R") == 0)
307 saw_R = 1;
308 quote = argv[i];
309 want_spec_file = 0;
310 if (library != 0)
311 added -= 2;
312 library = 0;
313 will_link = 0;
315 else if (argv[i][1] == 'D')
316 saw_D = 1;
317 else if (argv[i][1] == 'g')
318 saw_g = 1;
319 else if (argv[i][1] == 'O')
320 saw_O = 1;
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)
328 saw_o = 1;
329 quote = argv[i];
331 else if (strcmp(argv[i], "-classpath") == 0
332 || strcmp(argv[i], "-bootclasspath") == 0
333 || strcmp(argv[i], "-CLASSPATH") == 0)
335 quote = argv[i];
336 added -= 1;
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
344 cause a warning. */
345 library = 0;
346 added -= 2;
348 /* Remember this so we can confirm -fmain option. */
349 will_link = 0;
351 else if (strcmp (argv[i], "-d") == 0)
353 /* `-d' option is for javac compatibility. */
354 quote = argv[i];
355 added -= 1;
357 else if (strcmp (argv[i], "-fsyntax-only") == 0
358 || strcmp (argv[i], "--syntax-only") == 0)
360 want_spec_file = 0;
361 library = 0;
362 will_link = 0;
363 continue;
365 else if (strcmp (argv[i], "-save-temps") == 0)
366 saw_save_temps = 1;
367 else if (strcmp (argv[i], "-static-libgcc") == 0
368 || strcmp (argv[i], "-static") == 0)
369 shared_libgcc = 0;
370 else
371 /* Pass other options through. */
372 continue;
374 else
376 int len;
378 if (saw_speclang)
380 saw_speclang = 0;
381 continue;
384 if (saw_R)
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;
402 java_files_count++;
403 last_input_index = i;
405 if (len > 6 && strcmp (argv[i] + len - 6, ".class") == 0)
407 args[i] |= CLASS_FILE_ARG;
408 class_files_count++;
409 last_input_index = i;
411 if (len > 4
412 && (strcmp (argv[i] + len - 4, ".zip") == 0
413 || strcmp (argv[i] + len - 4, ".jar") == 0))
415 args[i] |= ZIP_FILE_ARG;
416 zip_files_count++;
417 last_input_index = i;
422 if (quote)
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;
432 if (saw_R)
434 if (! saw_o)
435 fatal ("-R requires -o");
437 if (saw_C)
439 num_args += 3;
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;
445 zip_files_count = 0;
447 num_args += 2; /* For -o NONE. */
448 if (saw_o)
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))
455 combine_inputs = 1;
457 if (combine_inputs)
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. */
470 #if 0
471 if (! added && ! library && main_class_name == NULL && ! saw_C)
473 free (args);
474 return;
476 #endif
478 if (main_class_name)
480 lang_specific_extra_outfiles++;
482 if (saw_g + saw_O == 0)
483 num_args++;
484 num_args++;
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
492 libgcc. */
493 #ifndef ENABLE_SHARED_LIBGCC
494 shared_libgcc = 0;
495 #endif
497 num_args += shared_libgcc;
499 arglist = (const char **) xmalloc ((num_args + 1) * sizeof (char *));
500 j = 0;
502 for (i = 0; i < argc; i++, j++)
504 arglist[j] = argv[i];
506 if ((args[i] & PARAM_ARG) || i == 0)
507 continue;
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], "-R") == 0)
518 arglist[j] = concat ("-fcompile-resource=",
519 *argv[i+1] == '/' ? "" : "/",
520 argv[i+1], NULL);
521 i++;
522 continue;
525 if (strcmp (argv[i], "-classpath") == 0
526 || strcmp (argv[i], "-bootclasspath") == 0
527 || strcmp (argv[i], "-CLASSPATH") == 0)
529 arglist[j] = concat ("-f", argv[i]+1, "=", argv[i+1], NULL);
530 i++;
531 continue;
534 if (strcmp (argv[i], "-d") == 0)
536 arglist[j] = concat ("-foutput-class-dir=", argv[i + 1], NULL);
537 ++i;
538 continue;
541 if (spec_file == NULL && strncmp (argv[i], "-L", 2) == 0)
542 spec_file = find_spec_file (argv[i] + 2);
544 if (strncmp (argv[i], "-fmain=", 7) == 0)
546 if (! will_link)
547 fatal ("cannot specify `main' class when not linking");
548 --j;
549 continue;
552 if ((args[i] & INDIRECT_FILE_ARG) != 0)
554 arglist[j++] = "-xjava";
555 arglist[j++] = argv[i]+1; /* Drop '@'. */
556 arglist[j] = "-xnone";
559 if ((args[i] & (CLASS_FILE_ARG|ZIP_FILE_ARG)) && saw_C)
561 --j;
562 continue;
565 if (combine_inputs
566 && (args[i] & (CLASS_FILE_ARG|JAVA_FILE_ARG|ZIP_FILE_ARG)) != 0)
568 fputs (argv[i], filelist_file);
569 fputc ('\n', filelist_file);
570 --j;
571 continue;
575 if (combine_inputs || indirect_files_count > 0)
576 arglist[j++] = "-ffilelist-file";
578 if (combine_inputs)
580 if (fclose (filelist_file))
581 pfatal_with_name (filelist_filename);
582 arglist[j++] = "-xjava";
583 arglist[j++] = filelist_filename;
586 /* If we saw no -O or -g option, default to -g1, for javac compatibility. */
587 if (saw_g + saw_O == 0)
588 arglist[j++] = "-g1";
590 /* Read the specs file corresponding to libgcj.
591 If we didn't find the spec file on the -L path, then we hope it
592 is somewhere in the standard install areas. */
593 if (want_spec_file)
594 arglist[j++] = spec_file == NULL ? "-specs=libgcj.spec" : spec_file;
596 if (saw_C)
598 arglist[j++] = "-fsyntax-only";
599 arglist[j++] = "-femit-class-files";
600 arglist[j++] = "-S";
601 arglist[j++] = "-o";
602 arglist[j++] = "NONE";
605 if (shared_libgcc)
606 arglist[j++] = "-shared-libgcc";
608 arglist[j] = NULL;
610 *in_argc = j;
611 *in_argv = arglist;
612 *in_added_libraries = added_libraries;
616 lang_specific_pre_link ()
618 int err;
619 if (main_class_name == NULL)
620 return 0;
621 /* Append `main' to make the filename unique and allow
623 gcj --main=hello -save-temps hello.java
625 to work. jvgenmain needs to strip this `main' to arrive at the correct
626 class name. Append dummy `.c' that can be stripped by set_input so %b
627 is correct. */
628 set_input (concat (main_class_name, "main.c", NULL));
629 err = do_spec (jvgenmain_spec);
630 if (err == 0)
632 /* Shift the outfiles array so the generated main comes first.
633 This is important when linking against (non-shared) libraries,
634 since otherwise we risk (a) nothing getting linked or
635 (b) 'main' getting picked up from a library. */
636 int i = n_infiles;
637 const char *generated = outfiles[i];
638 while (--i >= 0)
639 outfiles[i + 1] = outfiles[i];
640 outfiles[0] = generated;
642 return err;