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