]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/java/jvspec.c
re PR java/2369 (--main should check the following symbol)
[thirdparty/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 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 "gcc.h"
29
30 /* Name of spec file. */
31 #define SPEC_FILE "libgcj.spec"
32
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)
47
48 static char *find_spec_file PARAMS ((const char *));
49 static int verify_class_name PARAMS ((const char *));
50
51 static const char *main_class_name = NULL;
52 int lang_specific_extra_outfiles = 0;
53
54 /* True if we should add -shared-libgcc to the command-line. */
55 int shared_libgcc = 1;
56
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 %{<fclasspath*} %{<fCLASSPATH*} %{<foutput-class-dir}\
70 %{<fuse-divide-subroutine} %{<fno-use-divide-subroutine}\
71 %{<fcheck-references} %{<fno-check-references}\
72 %{<ffilelist-file}\
73 %{f*} -fdollars-in-identifiers\
74 %{aux-info*}\
75 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
76 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
77 %{!S:as %a %Y -o %d%w%u%O %{!pipe:%g.s} %A\n }";
78
79 /* Return full path name of spec file if it is in DIR, or NULL if
80 not. */
81 static char *
82 find_spec_file (dir)
83 const char *dir;
84 {
85 char *spec;
86 int x;
87 struct stat sb;
88
89 spec = (char *) xmalloc (strlen (dir) + sizeof (SPEC_FILE)
90 + sizeof ("-specs=") + 4);
91 strcpy (spec, "-specs=");
92 x = strlen (spec);
93 strcat (spec, dir);
94 strcat (spec, "/");
95 strcat (spec, SPEC_FILE);
96 if (! stat (spec + x, &sb))
97 return spec;
98 free (spec);
99 return NULL;
100 }
101
102 /* FIXME: these should come from lex.h. */
103 #define JAVA_START_CHAR_P(c) (c < 128 && (ISIDST (c) || c == '$'))
104 #define JAVA_PART_CHAR_P(c) (c < 128 \
105 && (ISIDNUM (c) \
106 || c == '$' \
107 || (c >= 0x00 && c <= 0x08) \
108 || (c >= 0x0e && c <= 0x1b) \
109 || c == 0x7f))
110
111 /* Verify that NAME is a valid Java class name that might contain
112 `main'. Return 0 on failure. */
113 static int
114 verify_class_name (name)
115 const char *name;
116 {
117 /* FIXME: what encoding do we use for command-line arguments? For
118 now we assume plain ASCII, which of course is wrong. */
119 while (*name)
120 {
121 int ch = *name++;
122 if (ch < 0 || ! JAVA_START_CHAR_P (ch))
123 return 0;
124 while (*name)
125 {
126 ch = *name++;
127 if (ch < 0)
128 return 0;
129 /* We found a break between class names. Next character
130 must be an identifier start again. */
131 if (ch == '.')
132 break;
133 if (! JAVA_PART_CHAR_P (ch))
134 return 0;
135 }
136 }
137
138 return 1;
139 }
140
141 void
142 lang_specific_driver (in_argc, in_argv, in_added_libraries)
143 int *in_argc;
144 const char *const **in_argv;
145 int *in_added_libraries;
146 {
147 int i, j;
148
149 /* If non-zero, the user gave us the `-v' flag. */
150 int saw_verbose_flag = 0;
151
152 int saw_save_temps = 0;
153
154 /* This will be 0 if we encounter a situation where we should not
155 link in libgcj. */
156 int library = 1;
157
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;
161
162 /* Index of last .java or .class argument. */
163 int last_input_index;
164
165 /* Number of .java and .class source file arguments seen. */
166 int java_files_count = 0;
167 int class_files_count = 0;
168 /* Number of .zip or .jar file arguments seen. */
169 int zip_files_count = 0;
170 /* Number of '@FILES' arguments seen. */
171 int indirect_files_count = 0;
172
173 /* Name of file containing list of files to compile. */
174 char *filelist_filename = 0;
175
176 FILE *filelist_file = 0;
177
178 /* The number of arguments being added to what's in argv, other than
179 libraries. */
180 int added = 2;
181
182 /* Used to track options that take arguments, so we don't go wrapping
183 those with -xc++/-xnone. */
184 const char *quote = NULL;
185
186 /* The new argument list will be contained in this. */
187 const char **arglist;
188
189 /* Non-zero if we saw a `-xfoo' language specification on the
190 command line. Used to avoid adding our own -xc++ if the user
191 already gave a language for the file. */
192 int saw_speclang = 0;
193
194 #if 0
195 /* "-lm" or "-lmath" if it appears on the command line. */
196 const char *saw_math ATTRIBUTE_UNUSED = 0;
197
198 /* "-lc" if it appears on the command line. */
199 const char *saw_libc ATTRIBUTE_UNUSED = 0;
200
201 /* "-lgcjgc" if it appears on the command line. */
202 const char *saw_gc ATTRIBUTE_UNUSED = 0;
203
204 /* Saw `-l' option for the thread library. */
205 const char *saw_threadlib ATTRIBUTE_UNUSED = 0;
206
207 /* Saw `-lgcj' on command line. */
208 int saw_libgcj ATTRIBUTE_UNUSED = 0;
209 #endif
210
211 /* Saw -R, -C or -o options, respectively. */
212 int saw_R = 0;
213 int saw_C = 0;
214 int saw_o = 0;
215
216 /* Saw some -O* or -g* option, respectively. */
217 int saw_O = 0;
218 int saw_g = 0;
219
220 /* Saw a `-D' option. */
221 int saw_D = 0;
222
223 /* An array used to flag each argument that needs a bit set for
224 LANGSPEC, MATHLIB, WITHLIBC, or GCLIB. */
225 int *args;
226
227 /* The total number of arguments with the new stuff. */
228 int argc;
229
230 /* The argument list. */
231 const char *const *argv;
232
233 /* The number of libraries added in. */
234 int added_libraries;
235
236 /* The total number of arguments with the new stuff. */
237 int num_args = 1;
238
239 /* Non-zero if linking is supposed to happen. */
240 int will_link = 1;
241
242 /* Non-zero if we want to find the spec file. */
243 int want_spec_file = 1;
244
245 /* The argument we use to specify the spec file. */
246 char *spec_file = NULL;
247
248 argc = *in_argc;
249 argv = *in_argv;
250 added_libraries = *in_added_libraries;
251
252 args = (int *) xcalloc (argc, sizeof (int));
253
254 for (i = 1; i < argc; i++)
255 {
256 /* If the previous option took an argument, we swallow it here. */
257 if (quote)
258 {
259 quote = NULL;
260 args[i] |= PARAM_ARG;
261 continue;
262 }
263
264 /* We don't do this anymore, since we don't get them with minus
265 signs on them. */
266 if (argv[i][0] == '\0' || argv[i][1] == '\0')
267 continue;
268
269 if (argv[i][0] == '-')
270 {
271 if (library != 0 && (strcmp (argv[i], "-nostdlib") == 0
272 || strcmp (argv[i], "-nodefaultlibs") == 0))
273 {
274 library = 0;
275 }
276 else if (strncmp (argv[i], "-fmain=", 7) == 0)
277 {
278 main_class_name = argv[i] + 7;
279 added--;
280 }
281 else if (strcmp (argv[i], "-fhelp") == 0)
282 want_spec_file = 0;
283 else if (strcmp (argv[i], "-v") == 0)
284 {
285 saw_verbose_flag = 1;
286 if (argc == 2)
287 {
288 /* If they only gave us `-v', don't try to link
289 in libgcj. */
290 library = 0;
291 }
292 }
293 else if (strncmp (argv[i], "-x", 2) == 0)
294 saw_speclang = 1;
295 else if (strcmp (argv[i], "-C") == 0)
296 {
297 saw_C = 1;
298 want_spec_file = 0;
299 if (library != 0)
300 added -= 2;
301 library = 0;
302 will_link = 0;
303 }
304 else if (strcmp (argv[i], "-R") == 0)
305 {
306 saw_R = 1;
307 quote = argv[i];
308 want_spec_file = 0;
309 if (library != 0)
310 added -= 2;
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], "-CLASSPATH") == 0)
332 {
333 quote = argv[i];
334 added -= 1;
335 }
336 else if (library != 0
337 && ((argv[i][2] == '\0'
338 && (char *) strchr ("cSEM", argv[i][1]) != NULL)
339 || strcmp (argv[i], "-MM") == 0))
340 {
341 /* Don't specify libraries if we won't link, since that would
342 cause a warning. */
343 library = 0;
344 added -= 2;
345
346 /* Remember this so we can confirm -fmain option. */
347 will_link = 0;
348 }
349 else if (strcmp (argv[i], "-d") == 0)
350 {
351 /* `-d' option is for javac compatibility. */
352 quote = argv[i];
353 added -= 1;
354 }
355 else if (strcmp (argv[i], "-fsyntax-only") == 0
356 || strcmp (argv[i], "--syntax-only") == 0)
357 {
358 want_spec_file = 0;
359 library = 0;
360 will_link = 0;
361 continue;
362 }
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;
371 }
372 else
373 {
374 int len;
375
376 if (saw_speclang)
377 {
378 saw_speclang = 0;
379 continue;
380 }
381
382 if (saw_R)
383 {
384 args[i] |= RESOURCE_FILE_ARG;
385 last_input_index = i;
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 last_input_index = i;
402 }
403 if (len > 6 && strcmp (argv[i] + len - 6, ".class") == 0)
404 {
405 args[i] |= CLASS_FILE_ARG;
406 class_files_count++;
407 last_input_index = i;
408 }
409 if (len > 4
410 && (strcmp (argv[i] + len - 4, ".zip") == 0
411 || strcmp (argv[i] + len - 4, ".jar") == 0))
412 {
413 args[i] |= ZIP_FILE_ARG;
414 zip_files_count++;
415 last_input_index = i;
416 }
417 }
418 }
419
420 if (quote)
421 fatal ("argument to `%s' missing\n", quote);
422
423 if (saw_D && ! main_class_name)
424 fatal ("can't specify `-D' without `--main'\n");
425
426 if (main_class_name && ! verify_class_name (main_class_name))
427 fatal ("`%s' is not a valid class name", main_class_name);
428
429 num_args = argc + added;
430 if (saw_R)
431 {
432 if (! saw_o)
433 fatal ("-R requires -o");
434 }
435 if (saw_C)
436 {
437 num_args += 3;
438 if (class_files_count + zip_files_count > 0)
439 {
440 error ("warning: already-compiled .class files ignored with -C");
441 num_args -= class_files_count + zip_files_count;
442 class_files_count = 0;
443 zip_files_count = 0;
444 }
445 num_args += 2; /* For -o NONE. */
446 if (saw_o)
447 fatal ("cannot specify both -C and -o");
448 }
449 if ((saw_o && java_files_count + class_files_count + zip_files_count > 1)
450 || (saw_C && java_files_count > 1)
451 || (indirect_files_count > 0
452 && java_files_count + class_files_count + zip_files_count > 0))
453 combine_inputs = 1;
454
455 if (combine_inputs)
456 {
457 filelist_filename = make_temp_file ("jx");
458 if (filelist_filename == NULL)
459 fatal ("cannot create temporary file");
460 record_temp_file (filelist_filename, ! saw_save_temps, 0);
461 filelist_file = fopen (filelist_filename, "w");
462 if (filelist_file == NULL)
463 pfatal_with_name (filelist_filename);
464 num_args -= java_files_count + class_files_count + zip_files_count;
465 num_args += 2; /* for the combined arg and "-xjava" */
466 }
467 /* If we know we don't have to do anything, bail now. */
468 #if 0
469 if (! added && ! library && main_class_name == NULL && ! saw_C)
470 {
471 free (args);
472 return;
473 }
474 #endif
475
476 if (main_class_name)
477 {
478 lang_specific_extra_outfiles++;
479 }
480 if (saw_g + saw_O == 0)
481 num_args++;
482 num_args++;
483
484 if (combine_inputs || indirect_files_count > 0)
485 num_args += 1; /* for "-ffilelist-file" */
486 if (combine_inputs && indirect_files_count > 0)
487 fatal("using both @FILE with multiple files not implemented");
488
489 /* There's no point adding -shared-libgcc if we don't have a shared
490 libgcc. */
491 #ifndef ENABLE_SHARED_LIBGCC
492 shared_libgcc = 0;
493 #endif
494
495 num_args += shared_libgcc;
496
497 arglist = (const char **) xmalloc ((num_args + 1) * sizeof (char *));
498 j = 0;
499
500 for (i = 0; i < argc; i++, j++)
501 {
502 arglist[j] = argv[i];
503
504 if ((args[i] & PARAM_ARG) || i == 0)
505 continue;
506
507 if ((args[i] & RESOURCE_FILE_ARG) != 0)
508 {
509 arglist[j++] = "-xjava";
510 arglist[j++] = argv[i];
511 arglist[j] = "-xnone";
512 }
513
514 if (strcmp (argv[i], "-R") == 0)
515 {
516 arglist[j] = concat ("-fcompile-resource=",
517 *argv[i+1] == '/' ? "" : "/",
518 argv[i+1], NULL);
519 i++;
520 continue;
521 }
522
523 if (strcmp (argv[i], "-classpath") == 0
524 || strcmp (argv[i], "-CLASSPATH") == 0)
525 {
526 arglist[j] = concat ("-f", argv[i]+1, "=", argv[i+1], NULL);
527 i++;
528 continue;
529 }
530
531 if (strcmp (argv[i], "-d") == 0)
532 {
533 arglist[j] = concat ("-foutput-class-dir=", argv[i + 1], NULL);
534 ++i;
535 continue;
536 }
537
538 if (spec_file == NULL && strncmp (argv[i], "-L", 2) == 0)
539 spec_file = find_spec_file (argv[i] + 2);
540
541 if (strncmp (argv[i], "-fmain=", 7) == 0)
542 {
543 if (! will_link)
544 fatal ("cannot specify `main' class when not linking");
545 --j;
546 continue;
547 }
548
549 if ((args[i] & INDIRECT_FILE_ARG) != 0)
550 {
551 arglist[j++] = "-xjava";
552 arglist[j++] = argv[i]+1; /* Drop '@'. */
553 arglist[j] = "-xnone";
554 }
555
556 if ((args[i] & (CLASS_FILE_ARG|ZIP_FILE_ARG)) && saw_C)
557 {
558 --j;
559 continue;
560 }
561
562 if (combine_inputs
563 && (args[i] & (CLASS_FILE_ARG|JAVA_FILE_ARG|ZIP_FILE_ARG)) != 0)
564 {
565 fputs (argv[i], filelist_file);
566 fputc ('\n', filelist_file);
567 --j;
568 continue;
569 }
570 }
571
572 if (combine_inputs || indirect_files_count > 0)
573 arglist[j++] = "-ffilelist-file";
574
575 if (combine_inputs)
576 {
577 if (fclose (filelist_file))
578 pfatal_with_name (filelist_filename);
579 arglist[j++] = "-xjava";
580 arglist[j++] = filelist_filename;
581 }
582
583 /* If we saw no -O or -g option, default to -g1, for javac compatibility. */
584 if (saw_g + saw_O == 0)
585 arglist[j++] = "-g1";
586
587 /* Read the specs file corresponding to libgcj.
588 If we didn't find the spec file on the -L path, then we hope it
589 is somewhere in the standard install areas. */
590 if (want_spec_file)
591 arglist[j++] = spec_file == NULL ? "-specs=libgcj.spec" : spec_file;
592
593 if (saw_C)
594 {
595 arglist[j++] = "-fsyntax-only";
596 arglist[j++] = "-femit-class-files";
597 arglist[j++] = "-S";
598 arglist[j++] = "-o";
599 arglist[j++] = "NONE";
600 }
601
602 if (shared_libgcc)
603 arglist[j++] = "-shared-libgcc";
604
605 arglist[j] = NULL;
606
607 *in_argc = j;
608 *in_argv = arglist;
609 *in_added_libraries = added_libraries;
610 }
611
612 int
613 lang_specific_pre_link ()
614 {
615 int err;
616 if (main_class_name == NULL)
617 return 0;
618 /* Append `main' to make the filename unique and allow
619
620 gcj --main=hello -save-temps hello.java
621
622 to work. jvgenmain needs to strip this `main' to arrive at the correct
623 class name. Append dummy `.c' that can be stripped by set_input so %b
624 is correct. */
625 set_input (concat (main_class_name, "main.c", NULL));
626 err = do_spec (jvgenmain_spec);
627 if (err == 0)
628 {
629 /* Shift the outfiles array so the generated main comes first.
630 This is important when linking against (non-shared) libraries,
631 since otherwise we risk (a) nothing getting linked or
632 (b) 'main' getting picked up from a library. */
633 int i = n_infiles;
634 const char *generated = outfiles[i];
635 while (--i >= 0)
636 outfiles[i + 1] = outfiles[i];
637 outfiles[0] = generated;
638 }
639 return err;
640 }