OSDN Git Service

* Make-lang.in (java/jcf-write.o): Depend on $(TM_P_H).
[pf3gnuchains/gcc-fork.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, 2003
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. 
22
23 Java and all Java-based marks are trademarks or registered trademarks
24 of Sun Microsystems, Inc. in the United States and other countries.
25 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "gcc.h"
32
33 /* Name of spec file.  */
34 #define SPEC_FILE "libgcj.spec"
35
36 /* This bit is set if we saw a `-xfoo' language specification.  */
37 #define LANGSPEC        (1<<1)
38 /* True if this arg is a parameter to the previous option-taking arg. */
39 #define PARAM_ARG       (1<<2)
40 /* True if this arg is a .java input file name. */
41 #define JAVA_FILE_ARG   (1<<3)
42 /* True if this arg is a .class input file name. */
43 #define CLASS_FILE_ARG  (1<<4)
44 /* True if this arg is a .zip or .jar input file name. */
45 #define ZIP_FILE_ARG    (1<<5)
46 /* True if this arg is @FILE - where FILE contains a list of filenames. */
47 #define INDIRECT_FILE_ARG (1<<6)
48 /* True if this arg is a resource file.  */
49 #define RESOURCE_FILE_ARG (1<<7)
50
51 static char *find_spec_file     PARAMS ((const char *));
52 static int verify_class_name    PARAMS ((const char *));
53
54 static const char *main_class_name = NULL;
55 int lang_specific_extra_outfiles = 0;
56
57 /* True if we should add -shared-libgcc to the command-line.  */
58 int shared_libgcc = 1;
59
60 static const char jvgenmain_spec[] =
61   "jvgenmain %{D*} %b %m.i |\n\
62    cc1 %m.i %1 \
63                    %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
64                    %{g*} %{O*} \
65                    %{v:-version} %{pg:-p} %{p}\
66                    %<fbounds-check %<fno-bounds-check\
67                    %<fassume-compiled %<fno-assume-compiled\
68                    %<fcompile-resource* %<fassert %<fno-assert \
69                    %<femit-class-file %<femit-class-files %<fencoding*\
70                    %<fuse-boehm-gc %<fhash-synchronization %<fjni\
71                    %<findirect-dispatch \
72                    %<fno-store-check %<foutput-class-dir\
73                    %<fclasspath* %<fCLASSPATH* %<fbootclasspath*\
74                    %<fextdirs*\
75                    %<fuse-divide-subroutine %<fno-use-divide-subroutine\
76                    %<fcheck-references %<fno-check-references\
77                    %<ffilelist-file\
78                    %{f*} -fdollars-in-identifiers\
79                    %{aux-info*}\
80                    %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
81                    %{S:%W{o*}%{!o*:-o %b.s}}\
82    %(invoke_as)";
83
84 /* Return full path name of spec file if it is in DIR, or NULL if
85    not.  */
86 static char *
87 find_spec_file (dir)
88      const char *dir;
89 {
90   char *spec;
91   int x;
92   struct stat sb;
93
94   spec = xmalloc (strlen (dir) + sizeof (SPEC_FILE)
95                   + sizeof ("-specs=") + 4);
96   strcpy (spec, "-specs=");
97   x = strlen (spec);
98   strcat (spec, dir);
99   strcat (spec, "/");
100   strcat (spec, SPEC_FILE);
101   if (! stat (spec + x, &sb))
102     return spec;
103   free (spec);
104   return NULL;
105 }
106
107 /* FIXME: these should come from lex.h.  */
108 #define JAVA_START_CHAR_P(c) (c < 128 && (ISIDST (c) || c == '$'))
109 #define JAVA_PART_CHAR_P(c) (c < 128                                          \
110                              && (ISIDNUM (c)                                  \
111                                  || c == '$'                                  \
112                                  || (c >= 0x00 && c <= 0x08)                  \
113                                  || (c >= 0x0e && c <= 0x1b)                  \
114                                  || c == 0x7f))
115
116 /* Verify that NAME is a valid Java class name that might contain
117    `main'.  Return 0 on failure.  */
118 static int
119 verify_class_name (name)
120      const char *name;
121 {
122   /* FIXME: what encoding do we use for command-line arguments?  For
123      now we assume plain ASCII, which of course is wrong.  */
124   while (*name)
125     {
126       int ch = *name++;
127       if (ch < 0 || ! JAVA_START_CHAR_P (ch))
128         return 0;
129       while (*name)
130         {
131           ch = *name++;
132           if (ch < 0)
133             return 0;
134           /* We found a break between class names.  Next character
135              must be an identifier start again.  */
136           if (ch == '.')
137             break;
138           if (! JAVA_PART_CHAR_P (ch))
139             return 0;
140         }
141     }
142
143   return 1;
144 }
145
146 void
147 lang_specific_driver (in_argc, in_argv, in_added_libraries)
148      int *in_argc;
149      const char *const **in_argv;
150      int *in_added_libraries;
151 {
152   int i, j;
153
154   /* If nonzero, the user gave us the `-v' flag.  */
155   int saw_verbose_flag = 0;
156
157   int saw_save_temps = 0;
158
159   /* This will be 0 if we encounter a situation where we should not
160      link in libgcj.  */
161   int library = 1;
162
163   /* This will be 1 if multiple input files (.class and/or .java)
164      should be passed to a single jc1 invocation. */
165   int combine_inputs = 0;
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               added += 2;  /* for -xjava and -xnone */
388             }
389
390           if (argv[i][0] == '@')
391             {
392               args[i] |= INDIRECT_FILE_ARG;
393               indirect_files_count++;
394               added += 2;  /* for -xjava and -xnone */
395             }
396
397           len = strlen (argv[i]);
398           if (len > 5 && strcmp (argv[i] + len - 5, ".java") == 0)
399             {
400               args[i] |= JAVA_FILE_ARG;
401               java_files_count++;
402             }
403           if (len > 6 && strcmp (argv[i] + len - 6, ".class") == 0)
404             {
405               args[i] |= CLASS_FILE_ARG;
406               class_files_count++;
407             }
408           if (len > 4
409               && (strcmp (argv[i] + len - 4, ".zip") == 0
410                   || strcmp (argv[i] + len - 4, ".jar") == 0))
411             {
412               args[i] |= ZIP_FILE_ARG;
413               zip_files_count++;
414             }
415         }
416     }
417
418   if (quote)
419     fatal ("argument to `%s' missing\n", quote);
420
421   if (saw_D && ! main_class_name)
422     fatal ("can't specify `-D' without `--main'\n");
423
424   if (main_class_name && ! verify_class_name (main_class_name))
425     fatal ("`%s' is not a valid class name", main_class_name);
426
427   num_args = argc + added;
428   if (saw_resource)
429     {
430       if (! saw_o)
431         fatal ("--resource requires -o");
432     }
433   if (saw_C)
434     {
435       num_args += 3;
436       if (class_files_count + zip_files_count > 0)
437         {
438           error ("warning: already-compiled .class files ignored with -C"); 
439           num_args -= class_files_count + zip_files_count;
440           class_files_count = 0;
441           zip_files_count = 0;
442         }
443       num_args += 2;  /* For -o NONE. */
444       if (saw_o)
445         fatal ("cannot specify both -C and -o");
446     }
447   if ((saw_o && java_files_count + class_files_count + zip_files_count > 1)
448       || (saw_C && java_files_count > 1)
449       || (indirect_files_count > 0
450           && java_files_count + class_files_count + zip_files_count > 0))
451     combine_inputs = 1;
452
453   if (combine_inputs)
454     {
455       filelist_filename = make_temp_file ("jx");
456       if (filelist_filename == NULL)
457         fatal ("cannot create temporary file");
458       record_temp_file (filelist_filename, ! saw_save_temps, 0);
459       filelist_file = fopen (filelist_filename, "w");
460       if (filelist_file == NULL)
461         pfatal_with_name (filelist_filename);
462       num_args -= java_files_count + class_files_count + zip_files_count;
463       num_args += 2;  /* for the combined arg and "-xjava" */
464     }
465   /* If we know we don't have to do anything, bail now.  */
466 #if 0
467   if (! added && ! library && main_class_name == NULL && ! saw_C)
468     {
469       free (args);
470       return;
471     }
472 #endif
473
474   if (main_class_name)
475     {
476       lang_specific_extra_outfiles++;
477     }
478   if (saw_g + saw_O == 0)
479     num_args++;
480   num_args++;
481
482   if (combine_inputs || indirect_files_count > 0)
483     num_args += 1; /* for "-ffilelist-file" */
484   if (combine_inputs && indirect_files_count > 0)
485     fatal("using both @FILE with multiple files not implemented");
486
487   /* There's no point adding -shared-libgcc if we don't have a shared
488      libgcc.  */
489 #ifndef ENABLE_SHARED_LIBGCC
490   shared_libgcc = 0;
491 #endif  
492   
493   num_args += shared_libgcc;
494
495   arglist = xmalloc ((num_args + 1) * sizeof (char *));
496   j = 0;
497
498   for (i = 0; i < argc; i++, j++)
499     {
500       arglist[j] = argv[i];
501
502       if ((args[i] & PARAM_ARG) || i == 0)
503         continue;
504
505       if ((args[i] & RESOURCE_FILE_ARG) != 0)
506         {
507           arglist[j++] = "-xjava";
508           arglist[j++] = argv[i];
509           arglist[j] = "-xnone";
510         }
511
512       if (strcmp (argv[i], "-classpath") == 0
513           || strcmp (argv[i], "-bootclasspath") == 0
514           || strcmp (argv[i], "-CLASSPATH") == 0)
515         {
516           arglist[j] = concat ("-f", argv[i]+1, "=", argv[i+1], NULL);
517           i++;
518           continue;
519         }
520
521       if (strcmp (argv[i], "-d") == 0)
522         {
523           arglist[j] = concat ("-foutput-class-dir=", argv[i + 1], NULL);
524           ++i;
525           continue;
526         }
527
528       if (spec_file == NULL && strncmp (argv[i], "-L", 2) == 0)
529         spec_file = find_spec_file (argv[i] + 2);
530
531       if (strncmp (argv[i], "-fmain=", 7) == 0)
532         {
533           if (! will_link)
534             fatal ("cannot specify `main' class when not linking");
535           --j;
536           continue;
537         }
538
539       if ((args[i] & INDIRECT_FILE_ARG) != 0)
540         {
541           arglist[j++] = "-xjava";
542           arglist[j++] = argv[i]+1;  /* Drop '@'. */
543           arglist[j] = "-xnone";
544         }
545
546       if ((args[i] & (CLASS_FILE_ARG|ZIP_FILE_ARG)) && saw_C)
547         {
548           --j;
549           continue;
550         }
551
552       if (combine_inputs
553           && (args[i] & (CLASS_FILE_ARG|JAVA_FILE_ARG|ZIP_FILE_ARG)) != 0)
554         {
555           fputs (argv[i], filelist_file);
556           fputc ('\n', filelist_file);
557           --j;
558           continue;
559         }
560   }
561
562   if (combine_inputs || indirect_files_count > 0)
563     arglist[j++] = "-ffilelist-file";
564
565   if (combine_inputs)
566     {
567       if (fclose (filelist_file))
568         pfatal_with_name (filelist_filename);
569       arglist[j++] = "-xjava";
570       arglist[j++] = filelist_filename;
571     }
572
573   /* If we saw no -O or -g option, default to -g1, for javac compatibility. */
574   if (saw_g + saw_O == 0)
575     arglist[j++] = "-g1";
576
577   /* Read the specs file corresponding to libgcj.
578      If we didn't find the spec file on the -L path, then we hope it
579      is somewhere in the standard install areas.  */
580   if (want_spec_file)
581     arglist[j++] = spec_file == NULL ? "-specs=libgcj.spec" : spec_file;
582
583   if (saw_C)
584     {
585       arglist[j++] = "-fsyntax-only";
586       arglist[j++] = "-femit-class-files";
587       arglist[j++] = "-S";
588       arglist[j++] = "-o";
589       arglist[j++] = "NONE";
590     }
591   
592   if (shared_libgcc)
593     arglist[j++] = "-shared-libgcc";
594
595   arglist[j] = NULL;
596
597   *in_argc = j;
598   *in_argv = arglist;
599   *in_added_libraries = added_libraries;
600 }
601
602 int
603 lang_specific_pre_link ()
604 {
605   int err;
606   if (main_class_name == NULL)
607     return 0;
608   /* Append `main' to make the filename unique and allow
609
610         gcj --main=hello -save-temps hello.java
611
612      to work.  jvgenmain needs to strip this `main' to arrive at the correct
613      class name.  Append dummy `.c' that can be stripped by set_input so %b
614      is correct.  */ 
615   set_input (concat (main_class_name, "main.c", NULL));
616   err = do_spec (jvgenmain_spec);
617   if (err == 0)
618     {
619       /* Shift the outfiles array so the generated main comes first.
620          This is important when linking against (non-shared) libraries,
621          since otherwise we risk (a) nothing getting linked or
622          (b) 'main' getting picked up from a library. */
623       int i = n_infiles;
624       const char *generated = outfiles[i];
625       while (--i >= 0)
626         outfiles[i + 1] = outfiles[i];
627       outfiles[0] = generated;
628     }
629   return err;
630 }
631
632 /* Table of language-specific spec functions.  */ 
633 const struct spec_function lang_specific_spec_functions[] =
634 {
635   { 0, 0 }
636 };