OSDN Git Service

2006-10-09 Brooks Moses <bmoses@stanford.edu>
[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, 2004, 2005, 2006
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, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, 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 (const char *);
52 static int verify_class_name (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 %<fnew-verifier\
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 (const char *dir)
88 {
89   char *spec;
90   int x;
91   struct stat sb;
92
93   spec = XNEWVEC (char, 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 (const char *name)
119 {
120   /* FIXME: what encoding do we use for command-line arguments?  For
121      now we assume plain ASCII, which of course is wrong.  */
122   while (*name)
123     {
124       int ch = *name++;
125       if (ch < 0 || ! JAVA_START_CHAR_P (ch))
126         return 0;
127       while (*name)
128         {
129           ch = *name++;
130           if (ch < 0)
131             return 0;
132           /* We found a break between class names.  Next character
133              must be an identifier start again.  */
134           if (ch == '.')
135             break;
136           if (! JAVA_PART_CHAR_P (ch))
137             return 0;
138         }
139     }
140
141   return 1;
142 }
143
144 void
145 lang_specific_driver (int *in_argc, const char *const **in_argv,
146                       int *in_added_libraries)
147 {
148   int i, j;
149
150   /* If nonzero, the user gave us the `-v' flag.  */
151   int saw_verbose_flag = 0;
152
153   int saw_save_temps = 0;
154
155   /* This will be 0 if we encounter a situation where we should not
156      link in libgcj.  */
157   int library = 1;
158
159   /* This will be 1 if multiple input files (.class and/or .java)
160      should be passed to a single jc1 invocation. */
161   int combine_inputs = 0;
162
163   /* Number of .java and .class source file arguments seen. */
164   int java_files_count = 0;
165   int class_files_count = 0;
166   /* Number of .zip or .jar file arguments seen. */
167   int zip_files_count = 0;
168   /* Number of '@FILES' arguments seen. */
169   int indirect_files_count = 0;
170
171   /* Name of file containing list of files to compile. */
172   char *filelist_filename = 0;
173
174   FILE *filelist_file = 0;
175
176   /* The number of arguments being added to what's in argv, other than
177      libraries.  */
178   int added = 2;
179
180   /* Used to track options that take arguments, so we don't go wrapping
181      those with -xc++/-xnone.  */
182   const char *quote = NULL;
183
184   /* The new argument list will be contained in this.  */
185   const char **arglist;
186
187   /* Nonzero if we saw a `-xfoo' language specification on the
188      command line.  Used to avoid adding our own -xc++ if the user
189      already gave a language for the file.  */
190   int saw_speclang = 0;
191
192 #if 0
193   /* "-lm" or "-lmath" if it appears on the command line.  */
194   const char *saw_math ATTRIBUTE_UNUSED = 0;
195
196   /* "-lc" if it appears on the command line.  */
197   const char *saw_libc ATTRIBUTE_UNUSED = 0;
198
199   /* "-lgcjgc" if it appears on the command line.  */
200   const char *saw_gc ATTRIBUTE_UNUSED = 0;
201
202   /* Saw `-l' option for the thread library.  */
203   const char *saw_threadlib ATTRIBUTE_UNUSED = 0;
204
205   /* Saw `-lgcj' on command line.  */
206   int saw_libgcj ATTRIBUTE_UNUSED = 0;
207 #endif
208
209   /* Saw --resource, -C or -o options, respectively. */
210   int saw_resource = 0;
211   int saw_C = 0;
212   int saw_o = 0;
213
214   /* Saw some -O* or -g* option, respectively. */
215   int saw_O = 0;
216   int saw_g = 0;
217
218   /* Saw a `-D' option.  */
219   int saw_D = 0;
220
221   /* An array used to flag each argument that needs a bit set for
222      LANGSPEC, MATHLIB, WITHLIBC, or GCLIB.  */
223   int *args;
224
225   /* The total number of arguments with the new stuff.  */
226   int argc;
227
228   /* The argument list.  */
229   const char *const *argv;
230
231   /* The number of libraries added in.  */
232   int added_libraries;
233
234   /* The total number of arguments with the new stuff.  */
235   int num_args = 1;
236
237   /* Nonzero if linking is supposed to happen.  */
238   int will_link = 1;
239
240   /* Nonzero if we want to find the spec file.  */
241   int want_spec_file = 1;
242
243   /* The argument we use to specify the spec file.  */
244   char *spec_file = NULL;
245
246   /* If linking, nonzero if the BC-ABI is in use.  */
247   int link_for_bc_abi = 0;
248
249   argc = *in_argc;
250   argv = *in_argv;
251   added_libraries = *in_added_libraries;
252
253   args = XCNEWVEC (int, argc);
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                     && 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                    || strcmp (argv[i], "-encoding") == 0
334                    || strcmp (argv[i], "-extdirs") == 0)
335             {
336               quote = argv[i];
337               added -= 1;
338             }
339           else if (library != 0 
340                    && ((argv[i][2] == '\0'
341                         && strchr ("cSEM", argv[i][1]) != NULL)
342                        || strcmp (argv[i], "-MM") == 0))
343             {
344               /* Don't specify libraries if we won't link, since that would
345                  cause a warning.  */
346               library = 0;
347               added -= 2;
348
349               /* Remember this so we can confirm -fmain option.  */
350               will_link = 0;
351             }
352           else if (strcmp (argv[i], "-d") == 0)
353             {
354               /* `-d' option is for javac compatibility.  */
355               quote = argv[i];
356               added -= 1;
357             }
358           else if (strcmp (argv[i], "-fsyntax-only") == 0
359                    || strcmp (argv[i], "--syntax-only") == 0)
360             {
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 if (strcmp (argv[i], "-findirect-dispatch") == 0
371                    || strcmp (argv[i], "--indirect-dispatch") == 0)
372             {
373               link_for_bc_abi = 1;
374             }
375           else
376             /* Pass other options through.  */
377             continue;
378         }
379       else
380         {
381           int len; 
382
383           if (saw_speclang)
384             {
385               saw_speclang = 0;
386               continue;
387             }
388
389           if (saw_resource)
390             {
391               args[i] |= RESOURCE_FILE_ARG;
392               added += 2;  /* for -xjava and -xnone */
393             }
394
395           if (argv[i][0] == '@')
396             {
397               args[i] |= INDIRECT_FILE_ARG;
398               indirect_files_count++;
399               added += 2;  /* for -xjava and -xnone */
400             }
401
402           len = strlen (argv[i]);
403           if (len > 5 && strcmp (argv[i] + len - 5, ".java") == 0)
404             {
405               args[i] |= JAVA_FILE_ARG;
406               java_files_count++;
407             }
408           if (len > 6 && strcmp (argv[i] + len - 6, ".class") == 0)
409             {
410               args[i] |= CLASS_FILE_ARG;
411               class_files_count++;
412             }
413           if (len > 4
414               && (strcmp (argv[i] + len - 4, ".zip") == 0
415                   || strcmp (argv[i] + len - 4, ".jar") == 0))
416             {
417               args[i] |= ZIP_FILE_ARG;
418               zip_files_count++;
419             }
420         }
421     }
422
423   if (quote)
424     fatal ("argument to '%s' missing\n", quote);
425
426   if (saw_D && ! main_class_name)
427     fatal ("can't specify '-D' without '--main'\n");
428
429   if (main_class_name && ! verify_class_name (main_class_name))
430     fatal ("'%s' is not a valid class name", main_class_name);
431
432   num_args = argc + added;
433   if (saw_resource)
434     {
435       if (! saw_o)
436         fatal ("--resource requires -o");
437     }
438   if (saw_C)
439     {
440       num_args += 3;
441       if (class_files_count + zip_files_count > 0)
442         {
443           error ("warning: already-compiled .class files ignored with -C"); 
444           num_args -= class_files_count + zip_files_count;
445           class_files_count = 0;
446           zip_files_count = 0;
447         }
448       num_args += 2;  /* For -o NONE. */
449       if (saw_o)
450         fatal ("cannot specify both -C and -o");
451     }
452   if ((saw_o && java_files_count + class_files_count + zip_files_count > 1)
453       || (saw_C && java_files_count > 1)
454       || (indirect_files_count > 0
455           && java_files_count + class_files_count + zip_files_count > 0))
456     combine_inputs = 1;
457
458   if (combine_inputs)
459     {
460       filelist_filename = make_temp_file ("jx");
461       if (filelist_filename == NULL)
462         fatal ("cannot create temporary file");
463       record_temp_file (filelist_filename, ! saw_save_temps, 0);
464       filelist_file = fopen (filelist_filename, "w");
465       if (filelist_file == NULL)
466         pfatal_with_name (filelist_filename);
467       num_args -= java_files_count + class_files_count + zip_files_count;
468       num_args += 3;  /* for the combined arg "-xjava", and "-xnone" */
469     }
470   /* If we know we don't have to do anything, bail now.  */
471 #if 0
472   if (! added && ! library && main_class_name == NULL && ! saw_C)
473     {
474       free (args);
475       return;
476     }
477 #endif
478
479   if (main_class_name)
480     {
481       lang_specific_extra_outfiles++;
482     }
483   if (saw_g + saw_O == 0)
484     num_args++;
485   num_args++;
486
487   if (combine_inputs || indirect_files_count > 0)
488     num_args += 1; /* for "-ffilelist-file" */
489   if (combine_inputs && indirect_files_count > 0)
490     fatal("using both @FILE with multiple files not implemented");
491
492   /* There's no point adding -shared-libgcc if we don't have a shared
493      libgcc.  */
494 #ifndef ENABLE_SHARED_LIBGCC
495   shared_libgcc = 0;
496 #endif  
497   
498   num_args += shared_libgcc;
499
500   num_args += link_for_bc_abi;
501
502   arglist = XNEWVEC (const char *, num_args + 1);
503   j = 0;
504
505   arglist[j++] = argv[0];
506
507   if (combine_inputs || indirect_files_count > 0)
508     arglist[j++] = "-ffilelist-file";
509
510   if (combine_inputs)
511     {
512       arglist[j++] = "-xjava";
513       arglist[j++] = filelist_filename;
514       arglist[j++] = "-xnone";
515     }
516
517   for (i = 1; i < argc; i++, j++)
518     {
519       arglist[j] = argv[i];
520
521       if ((args[i] & PARAM_ARG))
522         continue;
523
524       if ((args[i] & RESOURCE_FILE_ARG) != 0)
525         {
526           arglist[j++] = "-xjava";
527           arglist[j++] = argv[i];
528           arglist[j] = "-xnone";
529         }
530
531       if (strcmp (argv[i], "-classpath") == 0
532           || strcmp (argv[i], "-bootclasspath") == 0
533           || strcmp (argv[i], "-CLASSPATH") == 0
534           || strcmp (argv[i], "-encoding") == 0
535           || strcmp (argv[i], "-extdirs") == 0)
536         {
537           arglist[j] = concat ("-f", argv[i]+1, "=", argv[i+1], NULL);
538           i++;
539           continue;
540         }
541
542       if (strcmp (argv[i], "-d") == 0)
543         {
544           arglist[j] = concat ("-foutput-class-dir=", argv[i + 1], NULL);
545           ++i;
546           continue;
547         }
548
549       if (spec_file == NULL && strncmp (argv[i], "-L", 2) == 0)
550         spec_file = find_spec_file (argv[i] + 2);
551
552       if (strncmp (argv[i], "-fmain=", 7) == 0)
553         {
554           if (! will_link)
555             fatal ("cannot specify 'main' class when not linking");
556           --j;
557           continue;
558         }
559
560       if ((args[i] & INDIRECT_FILE_ARG) != 0)
561         {
562           arglist[j++] = "-xjava";
563           arglist[j++] = argv[i]+1;  /* Drop '@'. */
564           arglist[j] = "-xnone";
565         }
566
567       if ((args[i] & (CLASS_FILE_ARG|ZIP_FILE_ARG)) && saw_C)
568         {
569           --j;
570           continue;
571         }
572
573       if (combine_inputs
574           && (args[i] & (CLASS_FILE_ARG|JAVA_FILE_ARG|ZIP_FILE_ARG)) != 0)
575         {
576           fputs (argv[i], filelist_file);
577           fputc ('\n', filelist_file);
578           --j;
579           continue;
580         }
581   }
582
583   if (combine_inputs)
584     {
585       if (fclose (filelist_file))
586         pfatal_with_name (filelist_filename);
587     }
588
589   /* If we saw no -O or -g option, default to -g1, for javac compatibility. */
590   if (saw_g + saw_O == 0)
591     arglist[j++] = "-g1";
592
593   /* Read the specs file corresponding to libgcj.
594      If we didn't find the spec file on the -L path, then we hope it
595      is somewhere in the standard install areas.  */
596   if (want_spec_file)
597     arglist[j++] = spec_file == NULL ? "-specs=libgcj.spec" : spec_file;
598
599   if (saw_C)
600     {
601       arglist[j++] = "-fsyntax-only";
602       arglist[j++] = "-femit-class-files";
603       arglist[j++] = "-S";
604       arglist[j++] = "-o";
605       arglist[j++] = "NONE";
606     }
607   
608   if (shared_libgcc)
609     arglist[j++] = "-shared-libgcc";
610
611   if (link_for_bc_abi)
612     arglist[j++] = "-s-bc-abi";
613
614   arglist[j] = NULL;
615
616   *in_argc = j;
617   *in_argv = arglist;
618   *in_added_libraries = added_libraries;
619 }
620
621 int
622 lang_specific_pre_link (void)
623 {
624   int err;
625   if (main_class_name == NULL)
626     return 0;
627   /* Append `main' to make the filename unique and allow
628
629         gcj --main=hello -save-temps hello.java
630
631      to work.  jvgenmain needs to strip this `main' to arrive at the correct
632      class name.  Append dummy `.c' that can be stripped by set_input so %b
633      is correct.  */ 
634   set_input (concat (main_class_name, "main.c", NULL));
635   err = do_spec (jvgenmain_spec);
636   if (err == 0)
637     {
638       /* Shift the outfiles array so the generated main comes first.
639          This is important when linking against (non-shared) libraries,
640          since otherwise we risk (a) nothing getting linked or
641          (b) 'main' getting picked up from a library. */
642       int i = n_infiles;
643       const char *generated = outfiles[i];
644       while (--i >= 0)
645         outfiles[i + 1] = outfiles[i];
646       outfiles[0] = generated;
647     }
648   return err;
649 }