aee48c894159d4d3309ec31c1337a3eba31edeb3
[gcc.git] / gcc / java / jvspec.c
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.
4
5 This file is part of GNU CC.
6
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.
11
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.
16
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.
21
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. */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "gcc.h"
31
32 /* Name of spec file. */
33 #define SPEC_FILE "libgcj.spec"
34
35 /* This bit is set if we saw a `-xfoo' language specification. */
36 #define LANGSPEC (1<<1)
37 /* True if this arg is a parameter to the previous option-taking arg. */
38 #define PARAM_ARG (1<<2)
39 /* True if this arg is a .java input file name. */
40 #define JAVA_FILE_ARG (1<<3)
41 /* True if this arg is a .class input file name. */
42 #define CLASS_FILE_ARG (1<<4)
43 /* True if this arg is a .zip or .jar input file name. */
44 #define ZIP_FILE_ARG (1<<5)
45 /* True if this arg is @FILE - where FILE contains a list of filenames. */
46 #define INDIRECT_FILE_ARG (1<<6)
47 /* True if this arg is a resource file. */
48 #define RESOURCE_FILE_ARG (1<<7)
49
50 static char *find_spec_file PARAMS ((const char *));
51 static int verify_class_name PARAMS ((const char *));
52
53 static const char *main_class_name = NULL;
54 int lang_specific_extra_outfiles = 0;
55
56 /* True if we should add -shared-libgcc to the command-line. */
57 int shared_libgcc = 1;
58
59 static const char jvgenmain_spec[] =
60 "jvgenmain %{D*} %b %m.i |\n\
61 cc1 %m.i %1 \
62 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
63 %{g*} %{O*} \
64 %{v:-version} %{pg:-p} %{p}\
65 %<fbounds-check %<fno-bounds-check\
66 %<fassume-compiled %<fno-assume-compiled\
67 %<fcompile-resource* %<fassert %<fno-assert \
68 %<femit-class-file %<femit-class-files %<fencoding*\
69 %<fuse-boehm-gc %<fhash-synchronization %<fjni\
70 %<findirect-dispatch \
71 %<fno-store-check %<foutput-class-dir\
72 %<fclasspath* %<fCLASSPATH* %<fbootclasspath*\
73 %<fextdirs*\
74 %<fuse-divide-subroutine %<fno-use-divide-subroutine\
75 %<fcheck-references %<fno-check-references\
76 %<ffilelist-file\
77 %{f*} -fdollars-in-identifiers\
78 %{aux-info*}\
79 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
80 %{S:%W{o*}%{!o*:-o %b.s}}\
81 %(invoke_as)";
82
83 /* Return full path name of spec file if it is in DIR, or NULL if
84 not. */
85 static char *
86 find_spec_file (dir)
87 const char *dir;
88 {
89 char *spec;
90 int x;
91 struct stat sb;
92
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;
104 }
105
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))
114
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 (name)
119 const char *name;
120 {
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)
124 {
125 int ch = *name++;
126 if (ch < 0 || ! JAVA_START_CHAR_P (ch))
127 return 0;
128 while (*name)
129 {
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;
139 }
140 }
141
142 return 1;
143 }
144
145 void
146 lang_specific_driver (in_argc, in_argv, in_added_libraries)
147 int *in_argc;
148 const char *const **in_argv;
149 int *in_added_libraries;
150 {
151 int i, j;
152
153 /* If nonzero, the user gave us the `-v' flag. */
154 int saw_verbose_flag = 0;
155
156 int saw_save_temps = 0;
157
158 /* This will be 0 if we encounter a situation where we should not
159 link in libgcj. */
160 int library = 1;
161
162 /* This will be 1 if multiple input files (.class and/or .java)
163 should be passed to a single jc1 invocation. */
164 int combine_inputs = 0;
165
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;
173
174 /* Name of file containing list of files to compile. */
175 char *filelist_filename = 0;
176
177 FILE *filelist_file = 0;
178
179 /* The number of arguments being added to what's in argv, other than
180 libraries. */
181 int added = 2;
182
183 /* Used to track options that take arguments, so we don't go wrapping
184 those with -xc++/-xnone. */
185 const char *quote = NULL;
186
187 /* The new argument list will be contained in this. */
188 const char **arglist;
189
190 /* Nonzero 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;
194
195 #if 0
196 /* "-lm" or "-lmath" if it appears on the command line. */
197 const char *saw_math ATTRIBUTE_UNUSED = 0;
198
199 /* "-lc" if it appears on the command line. */
200 const char *saw_libc ATTRIBUTE_UNUSED = 0;
201
202 /* "-lgcjgc" if it appears on the command line. */
203 const char *saw_gc ATTRIBUTE_UNUSED = 0;
204
205 /* Saw `-l' option for the thread library. */
206 const char *saw_threadlib ATTRIBUTE_UNUSED = 0;
207
208 /* Saw `-lgcj' on command line. */
209 int saw_libgcj ATTRIBUTE_UNUSED = 0;
210 #endif
211
212 /* Saw --resource, -C or -o options, respectively. */
213 int saw_resource = 0;
214 int saw_C = 0;
215 int saw_o = 0;
216
217 /* Saw some -O* or -g* option, respectively. */
218 int saw_O = 0;
219 int saw_g = 0;
220
221 /* Saw a `-D' option. */
222 int saw_D = 0;
223
224 /* An array used to flag each argument that needs a bit set for
225 LANGSPEC, MATHLIB, WITHLIBC, or GCLIB. */
226 int *args;
227
228 /* The total number of arguments with the new stuff. */
229 int argc;
230
231 /* The argument list. */
232 const char *const *argv;
233
234 /* The number of libraries added in. */
235 int added_libraries;
236
237 /* The total number of arguments with the new stuff. */
238 int num_args = 1;
239
240 /* Nonzero if linking is supposed to happen. */
241 int will_link = 1;
242
243 /* Nonzero if we want to find the spec file. */
244 int want_spec_file = 1;
245
246 /* The argument we use to specify the spec file. */
247 char *spec_file = NULL;
248
249 argc = *in_argc;
250 argv = *in_argv;
251 added_libraries = *in_added_libraries;
252
253 args = xcalloc (argc, sizeof (int));
254
255 for (i = 1; i < argc; i++)
256 {
257 /* If the previous option took an argument, we swallow it here. */
258 if (quote)
259 {
260 quote = NULL;
261 args[i] |= PARAM_ARG;
262 continue;
263 }
264
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;
269
270 if (argv[i][0] == '-')
271 {
272 if (library != 0 && (strcmp (argv[i], "-nostdlib") == 0
273 || strcmp (argv[i], "-nodefaultlibs") == 0))
274 {
275 library = 0;
276 }
277 else if (strncmp (argv[i], "-fmain=", 7) == 0)
278 {
279 main_class_name = argv[i] + 7;
280 added--;
281 }
282 else if (strcmp (argv[i], "-fhelp") == 0)
283 want_spec_file = 0;
284 else if (strcmp (argv[i], "-v") == 0)
285 {
286 saw_verbose_flag = 1;
287 if (argc == 2)
288 {
289 /* If they only gave us `-v', don't try to link
290 in libgcj. */
291 library = 0;
292 }
293 }
294 else if (strncmp (argv[i], "-x", 2) == 0)
295 saw_speclang = 1;
296 else if (strcmp (argv[i], "-C") == 0)
297 {
298 saw_C = 1;
299 want_spec_file = 0;
300 if (library != 0)
301 added -= 2;
302 library = 0;
303 will_link = 0;
304 }
305 else if (strncmp (argv[i], "-fcompile-resource=", 19) == 0)
306 {
307 saw_resource = 1;
308 want_spec_file = 0;
309 if (library != 0)
310 --added;
311 library = 0;
312 will_link = 0;
313 }
314 else if (argv[i][1] == 'D')
315 saw_D = 1;
316 else if (argv[i][1] == 'g')
317 saw_g = 1;
318 else if (argv[i][1] == 'O')
319 saw_O = 1;
320 else if ((argv[i][2] == '\0'
321 && (char *)strchr ("bBVDUoeTuIYmLiA", argv[i][1]) != NULL)
322 || strcmp (argv[i], "-Tdata") == 0
323 || strcmp (argv[i], "-MT") == 0
324 || strcmp (argv[i], "-MF") == 0)
325 {
326 if (strcmp (argv[i], "-o") == 0)
327 saw_o = 1;
328 quote = argv[i];
329 }
330 else if (strcmp(argv[i], "-classpath") == 0
331 || strcmp(argv[i], "-bootclasspath") == 0
332 || strcmp(argv[i], "-CLASSPATH") == 0)
333 {
334 quote = argv[i];
335 added -= 1;
336 }
337 else if (library != 0
338 && ((argv[i][2] == '\0'
339 && (char *) strchr ("cSEM", argv[i][1]) != NULL)
340 || strcmp (argv[i], "-MM") == 0))
341 {
342 /* Don't specify libraries if we won't link, since that would
343 cause a warning. */
344 library = 0;
345 added -= 2;
346
347 /* Remember this so we can confirm -fmain option. */
348 will_link = 0;
349 }
350 else if (strcmp (argv[i], "-d") == 0)
351 {
352 /* `-d' option is for javac compatibility. */
353 quote = argv[i];
354 added -= 1;
355 }
356 else if (strcmp (argv[i], "-fsyntax-only") == 0
357 || strcmp (argv[i], "--syntax-only") == 0)
358 {
359 want_spec_file = 0;
360 library = 0;
361 will_link = 0;
362 continue;
363 }
364 else if (strcmp (argv[i], "-save-temps") == 0)
365 saw_save_temps = 1;
366 else if (strcmp (argv[i], "-static-libgcc") == 0
367 || strcmp (argv[i], "-static") == 0)
368 shared_libgcc = 0;
369 else
370 /* Pass other options through. */
371 continue;
372 }
373 else
374 {
375 int len;
376
377 if (saw_speclang)
378 {
379 saw_speclang = 0;
380 continue;
381 }
382
383 if (saw_resource)
384 {
385 args[i] |= RESOURCE_FILE_ARG;
386 added += 2; /* for -xjava and -xnone */
387 }
388
389 if (argv[i][0] == '@')
390 {
391 args[i] |= INDIRECT_FILE_ARG;
392 indirect_files_count++;
393 added += 2; /* for -xjava and -xnone */
394 }
395
396 len = strlen (argv[i]);
397 if (len > 5 && strcmp (argv[i] + len - 5, ".java") == 0)
398 {
399 args[i] |= JAVA_FILE_ARG;
400 java_files_count++;
401 }
402 if (len > 6 && strcmp (argv[i] + len - 6, ".class") == 0)
403 {
404 args[i] |= CLASS_FILE_ARG;
405 class_files_count++;
406 }
407 if (len > 4
408 && (strcmp (argv[i] + len - 4, ".zip") == 0
409 || strcmp (argv[i] + len - 4, ".jar") == 0))
410 {
411 args[i] |= ZIP_FILE_ARG;
412 zip_files_count++;
413 }
414 }
415 }
416
417 if (quote)
418 fatal ("argument to `%s' missing\n", quote);
419
420 if (saw_D && ! main_class_name)
421 fatal ("can't specify `-D' without `--main'\n");
422
423 if (main_class_name && ! verify_class_name (main_class_name))
424 fatal ("`%s' is not a valid class name", main_class_name);
425
426 num_args = argc + added;
427 if (saw_resource)
428 {
429 if (! saw_o)
430 fatal ("--resource requires -o");
431 }
432 if (saw_C)
433 {
434 num_args += 3;
435 if (class_files_count + zip_files_count > 0)
436 {
437 error ("warning: already-compiled .class files ignored with -C");
438 num_args -= class_files_count + zip_files_count;
439 class_files_count = 0;
440 zip_files_count = 0;
441 }
442 num_args += 2; /* For -o NONE. */
443 if (saw_o)
444 fatal ("cannot specify both -C and -o");
445 }
446 if ((saw_o && java_files_count + class_files_count + zip_files_count > 1)
447 || (saw_C && java_files_count > 1)
448 || (indirect_files_count > 0
449 && java_files_count + class_files_count + zip_files_count > 0))
450 combine_inputs = 1;
451
452 if (combine_inputs)
453 {
454 filelist_filename = make_temp_file ("jx");
455 if (filelist_filename == NULL)
456 fatal ("cannot create temporary file");
457 record_temp_file (filelist_filename, ! saw_save_temps, 0);
458 filelist_file = fopen (filelist_filename, "w");
459 if (filelist_file == NULL)
460 pfatal_with_name (filelist_filename);
461 num_args -= java_files_count + class_files_count + zip_files_count;
462 num_args += 2; /* for the combined arg and "-xjava" */
463 }
464 /* If we know we don't have to do anything, bail now. */
465 #if 0
466 if (! added && ! library && main_class_name == NULL && ! saw_C)
467 {
468 free (args);
469 return;
470 }
471 #endif
472
473 if (main_class_name)
474 {
475 lang_specific_extra_outfiles++;
476 }
477 if (saw_g + saw_O == 0)
478 num_args++;
479 num_args++;
480
481 if (combine_inputs || indirect_files_count > 0)
482 num_args += 1; /* for "-ffilelist-file" */
483 if (combine_inputs && indirect_files_count > 0)
484 fatal("using both @FILE with multiple files not implemented");
485
486 /* There's no point adding -shared-libgcc if we don't have a shared
487 libgcc. */
488 #ifndef ENABLE_SHARED_LIBGCC
489 shared_libgcc = 0;
490 #endif
491
492 num_args += shared_libgcc;
493
494 arglist = xmalloc ((num_args + 1) * sizeof (char *));
495 j = 0;
496
497 for (i = 0; i < argc; i++, j++)
498 {
499 arglist[j] = argv[i];
500
501 if ((args[i] & PARAM_ARG) || i == 0)
502 continue;
503
504 if ((args[i] & RESOURCE_FILE_ARG) != 0)
505 {
506 arglist[j++] = "-xjava";
507 arglist[j++] = argv[i];
508 arglist[j] = "-xnone";
509 }
510
511 if (strcmp (argv[i], "-classpath") == 0
512 || strcmp (argv[i], "-bootclasspath") == 0
513 || strcmp (argv[i], "-CLASSPATH") == 0)
514 {
515 arglist[j] = concat ("-f", argv[i]+1, "=", argv[i+1], NULL);
516 i++;
517 continue;
518 }
519
520 if (strcmp (argv[i], "-d") == 0)
521 {
522 arglist[j] = concat ("-foutput-class-dir=", argv[i + 1], NULL);
523 ++i;
524 continue;
525 }
526
527 if (spec_file == NULL && strncmp (argv[i], "-L", 2) == 0)
528 spec_file = find_spec_file (argv[i] + 2);
529
530 if (strncmp (argv[i], "-fmain=", 7) == 0)
531 {
532 if (! will_link)
533 fatal ("cannot specify `main' class when not linking");
534 --j;
535 continue;
536 }
537
538 if ((args[i] & INDIRECT_FILE_ARG) != 0)
539 {
540 arglist[j++] = "-xjava";
541 arglist[j++] = argv[i]+1; /* Drop '@'. */
542 arglist[j] = "-xnone";
543 }
544
545 if ((args[i] & (CLASS_FILE_ARG|ZIP_FILE_ARG)) && saw_C)
546 {
547 --j;
548 continue;
549 }
550
551 if (combine_inputs
552 && (args[i] & (CLASS_FILE_ARG|JAVA_FILE_ARG|ZIP_FILE_ARG)) != 0)
553 {
554 fputs (argv[i], filelist_file);
555 fputc ('\n', filelist_file);
556 --j;
557 continue;
558 }
559 }
560
561 if (combine_inputs || indirect_files_count > 0)
562 arglist[j++] = "-ffilelist-file";
563
564 if (combine_inputs)
565 {
566 if (fclose (filelist_file))
567 pfatal_with_name (filelist_filename);
568 arglist[j++] = "-xjava";
569 arglist[j++] = filelist_filename;
570 }
571
572 /* If we saw no -O or -g option, default to -g1, for javac compatibility. */
573 if (saw_g + saw_O == 0)
574 arglist[j++] = "-g1";
575
576 /* Read the specs file corresponding to libgcj.
577 If we didn't find the spec file on the -L path, then we hope it
578 is somewhere in the standard install areas. */
579 if (want_spec_file)
580 arglist[j++] = spec_file == NULL ? "-specs=libgcj.spec" : spec_file;
581
582 if (saw_C)
583 {
584 arglist[j++] = "-fsyntax-only";
585 arglist[j++] = "-femit-class-files";
586 arglist[j++] = "-S";
587 arglist[j++] = "-o";
588 arglist[j++] = "NONE";
589 }
590
591 if (shared_libgcc)
592 arglist[j++] = "-shared-libgcc";
593
594 arglist[j] = NULL;
595
596 *in_argc = j;
597 *in_argv = arglist;
598 *in_added_libraries = added_libraries;
599 }
600
601 int
602 lang_specific_pre_link ()
603 {
604 int err;
605 if (main_class_name == NULL)
606 return 0;
607 /* Append `main' to make the filename unique and allow
608
609 gcj --main=hello -save-temps hello.java
610
611 to work. jvgenmain needs to strip this `main' to arrive at the correct
612 class name. Append dummy `.c' that can be stripped by set_input so %b
613 is correct. */
614 set_input (concat (main_class_name, "main.c", NULL));
615 err = do_spec (jvgenmain_spec);
616 if (err == 0)
617 {
618 /* Shift the outfiles array so the generated main comes first.
619 This is important when linking against (non-shared) libraries,
620 since otherwise we risk (a) nothing getting linked or
621 (b) 'main' getting picked up from a library. */
622 int i = n_infiles;
623 const char *generated = outfiles[i];
624 while (--i >= 0)
625 outfiles[i + 1] = outfiles[i];
626 outfiles[0] = generated;
627 }
628 return err;
629 }
630
631 /* Table of language-specific spec functions. */
632 const struct spec_function lang_specific_spec_functions[] =
633 {
634 { 0, 0 }
635 };