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.
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)
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. */
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 *));
50 static const char *main_class_name = NULL;
51 int lang_specific_extra_outfiles = 0;
53 /* True if we should add -shared-libgcc to the command-line. */
54 int shared_libgcc = 1;
56 const char jvgenmain_spec[] =
57 "jvgenmain %{D*} %i %{!pipe:%umain.i} |\n\
58 cc1 %{!pipe:%Umain.i} %1 \
59 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
61 %{v:-version} %{pg:-p} %{p}\
62 %{<fbounds-check} %{<fno-bounds-check}\
63 %{<fassume-compiled} %{<fno-assume-compiled}\
64 %{<fcompile-resource*}\
65 %{<femit-class-file} %{<femit-class-files} %{<fencoding*}\
66 %{<fuse-boehm-gc} %{<fhash-synchronization} %{<fjni}\
67 %{<fclasspath*} %{<fCLASSPATH*} %{<foutput-class-dir}\
68 %{<fuse-divide-subroutine} %{<fno-use-divide-subroutine}\
69 %{<fcheck-references} %{<fno-check-references}\
71 %{f*} -fdollars-in-identifiers\
73 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
74 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%Umain.s}} |\n\
75 %{!S:as %a %Y -o %d%w%umain%O %{!pipe:%Umain.s} %A\n }";
77 /* Return full path name of spec file if it is in DIR, or NULL if
87 spec = (char *) xmalloc (strlen (dir) + sizeof (SPEC_FILE)
88 + sizeof ("-specs=") + 4);
89 strcpy (spec, "-specs=");
93 strcat (spec, SPEC_FILE);
94 if (! stat (spec + x, &sb))
101 lang_specific_driver (in_argc, in_argv, in_added_libraries)
103 const char *const **in_argv;
104 int *in_added_libraries;
108 /* If non-zero, the user gave us the `-v' flag. */
109 int saw_verbose_flag = 0;
111 int saw_save_temps = 0;
113 /* This will be 0 if we encounter a situation where we should not
117 /* This will be 1 if multiple input files (.class and/or .java)
118 should be passed to a single jc1 invocation. */
119 int combine_inputs = 0;
121 /* Index of last .java or .class argument. */
122 int last_input_index;
124 /* Number of .java and .class source file arguments seen. */
125 int java_files_count = 0;
126 int class_files_count = 0;
127 /* Number of .zip or .jar file arguments seen. */
128 int zip_files_count = 0;
129 /* Number of '@FILES' arguments seen. */
130 int indirect_files_count = 0;
132 /* Name of file containing list of files to compile. */
133 char *filelist_filename = 0;
135 FILE *filelist_file = 0;
137 /* The number of arguments being added to what's in argv, other than
141 /* Used to track options that take arguments, so we don't go wrapping
142 those with -xc++/-xnone. */
143 const char *quote = NULL;
145 /* The new argument list will be contained in this. */
146 const char **arglist;
148 /* Non-zero if we saw a `-xfoo' language specification on the
149 command line. Used to avoid adding our own -xc++ if the user
150 already gave a language for the file. */
151 int saw_speclang = 0;
154 /* "-lm" or "-lmath" if it appears on the command line. */
155 const char *saw_math ATTRIBUTE_UNUSED = 0;
157 /* "-lc" if it appears on the command line. */
158 const char *saw_libc ATTRIBUTE_UNUSED = 0;
160 /* "-lgcjgc" if it appears on the command line. */
161 const char *saw_gc ATTRIBUTE_UNUSED = 0;
163 /* Saw `-l' option for the thread library. */
164 const char *saw_threadlib ATTRIBUTE_UNUSED = 0;
166 /* Saw `-lgcj' on command line. */
167 int saw_libgcj ATTRIBUTE_UNUSED = 0;
170 /* Saw -R, -C or -o options, respectively. */
175 /* Saw some -O* or -g* option, respectively. */
179 /* Saw a `-D' option. */
182 /* An array used to flag each argument that needs a bit set for
183 LANGSPEC, MATHLIB, WITHLIBC, or GCLIB. */
186 /* The total number of arguments with the new stuff. */
189 /* The argument list. */
190 const char *const *argv;
192 /* The number of libraries added in. */
195 /* The total number of arguments with the new stuff. */
198 /* Non-zero if linking is supposed to happen. */
201 /* Non-zero if we want to find the spec file. */
202 int want_spec_file = 1;
204 /* The argument we use to specify the spec file. */
205 char *spec_file = NULL;
209 added_libraries = *in_added_libraries;
211 args = (int *) xcalloc (argc, sizeof (int));
213 for (i = 1; i < argc; i++)
215 /* If the previous option took an argument, we swallow it here. */
219 args[i] |= PARAM_ARG;
223 /* We don't do this anymore, since we don't get them with minus
225 if (argv[i][0] == '\0' || argv[i][1] == '\0')
228 if (argv[i][0] == '-')
230 if (library != 0 && (strcmp (argv[i], "-nostdlib") == 0
231 || strcmp (argv[i], "-nodefaultlibs") == 0))
235 else if (strncmp (argv[i], "-fmain=", 7) == 0)
237 main_class_name = argv[i] + 7;
240 else if (strcmp (argv[i], "-fhelp") == 0)
242 else if (strcmp (argv[i], "-v") == 0)
244 saw_verbose_flag = 1;
247 /* If they only gave us `-v', don't try to link
252 else if (strncmp (argv[i], "-x", 2) == 0)
254 else if (strcmp (argv[i], "-C") == 0)
263 else if (strcmp (argv[i], "-R") == 0)
273 else if (argv[i][1] == 'D')
275 else if (argv[i][1] == 'g')
277 else if (argv[i][1] == 'O')
279 else if ((argv[i][2] == '\0'
280 && (char *)strchr ("bBVDUoeTuIYmLiA", argv[i][1]) != NULL)
281 || strcmp (argv[i], "-Tdata") == 0
282 || strcmp (argv[i], "-MT") == 0
283 || strcmp (argv[i], "-MF") == 0)
285 if (strcmp (argv[i], "-o") == 0)
289 else if (strcmp(argv[i], "-classpath") == 0
290 || strcmp(argv[i], "-CLASSPATH") == 0)
295 else if (library != 0
296 && ((argv[i][2] == '\0'
297 && (char *) strchr ("cSEM", argv[i][1]) != NULL)
298 || strcmp (argv[i], "-MM") == 0))
300 /* Don't specify libraries if we won't link, since that would
305 /* Remember this so we can confirm -fmain option. */
308 else if (strcmp (argv[i], "-d") == 0)
310 /* `-d' option is for javac compatibility. */
314 else if (strcmp (argv[i], "-fsyntax-only") == 0
315 || strcmp (argv[i], "--syntax-only") == 0)
322 else if (strcmp (argv[i], "-save-temps") == 0)
324 else if (strcmp (argv[i], "-static-libgcc") == 0
325 || strcmp (argv[i], "-static") == 0)
328 /* Pass other options through. */
343 args[i] |= RESOURCE_FILE_ARG;
344 last_input_index = i;
345 added += 2; /* for -xjava and -xnone */
348 if (argv[i][0] == '@')
350 args[i] |= INDIRECT_FILE_ARG;
351 indirect_files_count++;
352 added += 2; /* for -xjava and -xnone */
355 len = strlen (argv[i]);
356 if (len > 5 && strcmp (argv[i] + len - 5, ".java") == 0)
358 args[i] |= JAVA_FILE_ARG;
360 last_input_index = i;
362 if (len > 6 && strcmp (argv[i] + len - 6, ".class") == 0)
364 args[i] |= CLASS_FILE_ARG;
366 last_input_index = i;
369 && (strcmp (argv[i] + len - 4, ".zip") == 0
370 || strcmp (argv[i] + len - 4, ".jar") == 0))
372 args[i] |= ZIP_FILE_ARG;
374 last_input_index = i;
380 fatal ("argument to `%s' missing\n", quote);
382 if (saw_D && ! main_class_name)
383 fatal ("can't specify `-D' without `--main'\n");
385 num_args = argc + added;
389 fatal ("-R requires -o");
394 if (class_files_count + zip_files_count > 0)
396 error ("Warning: already-compiled .class files ignored with -C");
397 num_args -= class_files_count + zip_files_count;
398 class_files_count = 0;
401 num_args += 2; /* For -o NONE. */
403 fatal ("cannot specify both -C and -o");
405 if ((saw_o && java_files_count + class_files_count + zip_files_count > 1)
406 || (saw_C && java_files_count > 1)
407 || (indirect_files_count > 0
408 && java_files_count + class_files_count + zip_files_count > 0))
413 filelist_filename = make_temp_file ("jx");
414 if (filelist_filename == NULL)
415 fatal ("cannot create temporary file");
416 record_temp_file (filelist_filename, ! saw_save_temps, 0);
417 filelist_file = fopen (filelist_filename, "w");
418 if (filelist_file == NULL)
419 pfatal_with_name (filelist_filename);
420 num_args -= java_files_count + class_files_count + zip_files_count;
421 num_args += 2; /* for the combined arg and "-xjava" */
423 /* If we know we don't have to do anything, bail now. */
425 if (! added && ! library && main_class_name == NULL && ! saw_C)
434 lang_specific_extra_outfiles++;
436 if (saw_g + saw_O == 0)
440 if (combine_inputs || indirect_files_count > 0)
441 num_args += 1; /* for "-ffilelist-file" */
442 if (combine_inputs && indirect_files_count > 0)
443 fatal("using both @FILE with multiple files not implemented");
445 /* There's no point adding -shared-libgcc if we don't have a shared
447 #ifndef ENABLE_SHARED_LIBGCC
451 num_args += shared_libgcc;
453 arglist = (const char **) xmalloc ((num_args + 1) * sizeof (char *));
456 for (i = 0; i < argc; i++, j++)
458 arglist[j] = argv[i];
460 if ((args[i] & PARAM_ARG) || i == 0)
463 if ((args[i] & RESOURCE_FILE_ARG) != 0)
465 arglist[j++] = "-xjava";
466 arglist[j++] = argv[i];
467 arglist[j] = "-xnone";
470 if (strcmp (argv[i], "-R") == 0)
472 arglist[j] = concat ("-fcompile-resource=",
473 *argv[i+1] == '/' ? "" : "/",
479 if (strcmp (argv[i], "-classpath") == 0
480 || strcmp (argv[i], "-CLASSPATH") == 0)
482 arglist[j] = concat ("-f", argv[i]+1, "=", argv[i+1], NULL);
487 if (strcmp (argv[i], "-d") == 0)
489 arglist[j] = concat ("-foutput-class-dir=", argv[i + 1], NULL);
494 if (spec_file == NULL && strncmp (argv[i], "-L", 2) == 0)
495 spec_file = find_spec_file (argv[i] + 2);
497 if (strncmp (argv[i], "-fmain=", 7) == 0)
500 fatal ("cannot specify `main' class when not linking");
505 if ((args[i] & INDIRECT_FILE_ARG) != 0)
507 arglist[j++] = "-xjava";
508 arglist[j++] = argv[i]+1; /* Drop '@'. */
509 arglist[j] = "-xnone";
512 if ((args[i] & (CLASS_FILE_ARG|ZIP_FILE_ARG)) && saw_C)
519 && (args[i] & (CLASS_FILE_ARG|JAVA_FILE_ARG|ZIP_FILE_ARG)) != 0)
521 fputs (argv[i], filelist_file);
522 fputc ('\n', filelist_file);
528 if (combine_inputs || indirect_files_count > 0)
529 arglist[j++] = "-ffilelist-file";
533 if (fclose (filelist_file))
534 pfatal_with_name (filelist_filename);
535 arglist[j++] = "-xjava";
536 arglist[j++] = filelist_filename;
539 /* If we saw no -O or -g option, default to -g1, for javac compatibility. */
540 if (saw_g + saw_O == 0)
541 arglist[j++] = "-g1";
543 /* Read the specs file corresponding to libgcj.
544 If we didn't find the spec file on the -L path, then we hope it
545 is somewhere in the standard install areas. */
547 arglist[j++] = spec_file == NULL ? "-specs=libgcj.spec" : spec_file;
551 arglist[j++] = "-fsyntax-only";
552 arglist[j++] = "-femit-class-files";
555 arglist[j++] = "NONE";
559 arglist[j++] = "-shared-libgcc";
565 *in_added_libraries = added_libraries;
569 lang_specific_pre_link ()
572 if (main_class_name == NULL)
574 input_filename = main_class_name;
575 input_filename_length = strlen (main_class_name);
576 err = do_spec (jvgenmain_spec);
579 /* Shift the outfiles array so the generated main comes first.
580 This is important when linking against (non-shared) libraries,
581 since otherwise we risk (a) nothing getting linked or
582 (b) 'main' getting picked up from a library. */
584 const char *generated = outfiles[i];
586 outfiles[i + 1] = outfiles[i];
587 outfiles[0] = generated;