OSDN Git Service

1987138a6875bc9f5ab3ef260bc7556e0bb4b998
[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,
4    2005, 2006, 2007, 2009, 2010 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 3, 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 COPYING3.  If not see
20 <http://www.gnu.org/licenses/>. 
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 "coretypes.h"
29 #include "tm.h"
30 #include "gcc.h"
31 #include "jcf.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 %{findirect-dispatch} %{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                    %<fuse-atomic-builtins %<fno-use-atomic-builtins\
77                    %<fcheck-references %<fno-check-references\
78                    %<ffilelist-file %<fsaw-java-file %<fsource* %<ftarget*\
79                    %{f*} -fdollars-in-identifiers\
80                    %{aux-info*}\
81                    %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
82                    %{S:%W{o*}%{!o*:-o %b.s}}\
83    %(invoke_as)";
84
85 /* Return full path name of spec file if it is in DIR, or NULL if
86    not.  */
87 static char *
88 find_spec_file (const char *dir)
89 {
90   char *spec;
91   int x;
92   struct stat sb;
93
94   spec = XNEWVEC (char, 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 #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   int saw_save_temps = 0;
151
152   /* This will be 0 if we encounter a situation where we should not
153      link in libgcj.  */
154   int library = 1;
155
156   /* This will be 1 if multiple input files (.class and/or .java)
157      should be passed to a single jc1 invocation. */
158   int combine_inputs = 0;
159
160   /* Number of .java and .class source file arguments seen. */
161   int java_files_count = 0;
162   int class_files_count = 0;
163   /* Number of .zip or .jar file arguments seen. */
164   int zip_files_count = 0;
165   /* Number of '@FILES' arguments seen. */
166   int indirect_files_count = 0;
167
168   /* Name of file containing list of files to compile. */
169   char *filelist_filename = 0;
170
171   FILE *filelist_file = 0;
172
173   /* The number of arguments being added to what's in argv, other than
174      libraries.  */
175   int added = 2;
176
177   /* Used to track options that take arguments, so we don't go wrapping
178      those with -xc++/-xnone.  */
179   const char *quote = NULL;
180
181   /* The new argument list will be contained in this.  */
182   const char **arglist;
183
184   /* Nonzero if we saw a `-xfoo' language specification on the
185      command line.  Used to avoid adding our own -xc++ if the user
186      already gave a language for the file.  */
187   int saw_speclang = 0;
188
189   /* Saw --resource, -C or -o options, respectively. */
190   int saw_resource = 0;
191   int saw_C = 0;
192   int saw_o = 0;
193
194   /* Saw some -O* or -g* option, respectively. */
195   int saw_O = 0;
196   int saw_g = 0;
197
198   /* Saw a `-D' option.  */
199   int saw_D = 0;
200
201   /* An array used to flag each argument that needs a bit set for
202      LANGSPEC, MATHLIB, WITHLIBC, or GCLIB.  */
203   int *args;
204
205   /* The total number of arguments with the new stuff.  */
206   int argc;
207
208   /* The argument list.  */
209   const char *const *argv;
210
211   /* The number of libraries added in.  */
212   int added_libraries;
213
214   /* The total number of arguments with the new stuff.  */
215   int num_args = 1;
216
217   /* Nonzero if linking is supposed to happen.  */
218   int will_link = 1;
219
220   /* Nonzero if we want to find the spec file.  */
221   int want_spec_file = 1;
222
223   /* The argument we use to specify the spec file.  */
224   char *spec_file = NULL;
225
226   /* If linking, nonzero if the BC-ABI is in use.  */
227   int link_for_bc_abi = 0;
228
229   argc = *in_argc;
230   argv = *in_argv;
231   added_libraries = *in_added_libraries;
232
233   args = XCNEWVEC (int, argc);
234
235   for (i = 1; i < argc; i++)
236     {
237       /* If the previous option took an argument, we swallow it here.  */
238       if (quote)
239         {
240           quote = NULL;
241           args[i] |= PARAM_ARG;
242           continue;
243         }
244
245       /* We don't do this anymore, since we don't get them with minus
246          signs on them.  */
247       if (argv[i][0] == '\0' || argv[i][1] == '\0')
248         continue;
249
250       if (argv[i][0] == '-')
251         {
252           if (library != 0 && (strcmp (argv[i], "-nostdlib") == 0
253                                || strcmp (argv[i], "-nodefaultlibs") == 0))
254             {
255               library = 0;
256             }
257           else if (strncmp (argv[i], "-fmain=", 7) == 0)
258             {
259               main_class_name = argv[i] + 7;
260               added--;
261             }
262           else if (strcmp (argv[i], "-fhelp") == 0)
263             want_spec_file = 0;
264           else if (strcmp (argv[i], "-v") == 0)
265             {
266               if (argc == 2)
267                 {
268                   /* If they only gave us `-v', don't try to link
269                      in libgcj.  */ 
270                   library = 0;
271                 }
272             }
273           else if (strncmp (argv[i], "-x", 2) == 0)
274             saw_speclang = 1;
275           else if (strcmp (argv[i], "-C") == 0)
276             {
277               saw_C = 1;
278               want_spec_file = 0;
279               if (library != 0)
280                 added -= 2;
281               library = 0;
282               will_link = 0;
283             }
284           else if (strncmp (argv[i], "-fcompile-resource=", 19) == 0)
285             {
286               saw_resource = 1;
287               want_spec_file = 0;
288               if (library != 0)
289                 --added;
290               library = 0;
291               will_link = 0;
292             }
293           else if (argv[i][1] == 'D')
294             saw_D = 1;
295           else if (argv[i][1] == 'g')
296             saw_g = 1;
297           else if (argv[i][1] == 'O')
298             saw_O = 1;
299           else if ((argv[i][2] == '\0'
300                     && strchr ("bBVDUoeTuIYmLiAI", argv[i][1]) != NULL)
301                    || strcmp (argv[i], "-Tdata") == 0
302                    || strcmp (argv[i], "-MT") == 0
303                    || strcmp (argv[i], "-MF") == 0)
304             {
305               if (strcmp (argv[i], "-o") == 0)
306                 saw_o = 1;
307               quote = argv[i];
308             }
309           else if (strcmp (argv[i], "-classpath") == 0
310                    || strcmp (argv[i], "-bootclasspath") == 0
311                    || strcmp (argv[i], "-CLASSPATH") == 0
312                    || strcmp (argv[i], "-encoding") == 0
313                    || strcmp (argv[i], "-extdirs") == 0)
314             {
315               quote = argv[i];
316               added -= 1;
317             }
318           else if (library != 0 
319                    && ((argv[i][2] == '\0'
320                         && strchr ("cSEM", argv[i][1]) != NULL)
321                        || strcmp (argv[i], "-MM") == 0))
322             {
323               /* Don't specify libraries if we won't link, since that would
324                  cause a warning.  */
325               library = 0;
326               added -= 2;
327
328               /* Remember this so we can confirm -fmain option.  */
329               will_link = 0;
330             }
331           else if (strcmp (argv[i], "-d") == 0)
332             {
333               /* `-d' option is for javac compatibility.  */
334               quote = argv[i];
335               added -= 1;
336             }
337           else if (strcmp (argv[i], "-fsyntax-only") == 0
338                    || strcmp (argv[i], "--syntax-only") == 0)
339             {
340               library = 0;
341               will_link = 0;
342               continue;
343             }
344           else if (strcmp (argv[i], "-save-temps") == 0)
345             saw_save_temps = 1;
346           else if (strcmp (argv[i], "-static-libgcc") == 0
347                    || strcmp (argv[i], "-static") == 0)
348             shared_libgcc = 0;
349           else if (strcmp (argv[i], "-findirect-dispatch") == 0
350                    || strcmp (argv[i], "--indirect-dispatch") == 0)
351             {
352               link_for_bc_abi = 1;
353             }
354           else
355             /* Pass other options through.  */
356             continue;
357         }
358       else
359         {
360           int len; 
361
362           if (saw_speclang)
363             {
364               saw_speclang = 0;
365               continue;
366             }
367
368           if (saw_resource)
369             {
370               args[i] |= RESOURCE_FILE_ARG;
371               added += 2;  /* for -xjava and -xnone */
372             }
373
374           if (argv[i][0] == '@')
375             {
376               args[i] |= INDIRECT_FILE_ARG;
377               indirect_files_count++;
378               added += 2;  /* for -xjava and -xnone */
379             }
380
381           len = strlen (argv[i]);
382           if (len > 5 && strcmp (argv[i] + len - 5, ".java") == 0)
383             {
384               args[i] |= JAVA_FILE_ARG;
385               java_files_count++;
386             }
387           if (len > 6 && strcmp (argv[i] + len - 6, ".class") == 0)
388             {
389               args[i] |= CLASS_FILE_ARG;
390               class_files_count++;
391             }
392           if (len > 4
393               && (strcmp (argv[i] + len - 4, ".zip") == 0
394                   || strcmp (argv[i] + len - 4, ".jar") == 0))
395             {
396               args[i] |= ZIP_FILE_ARG;
397               zip_files_count++;
398             }
399         }
400     }
401
402   if (quote)
403     fatal_error ("argument to %qs missing", quote);
404
405   if (saw_D && ! main_class_name)
406     fatal_error ("can't specify %<-D%> without %<--main%>");
407
408   if (main_class_name && ! verify_class_name (main_class_name))
409     fatal_error ("%qs is not a valid class name", main_class_name);
410
411   num_args = argc + added;
412   if (saw_resource)
413     {
414       if (! saw_o)
415         fatal_error ("--resource requires -o");
416     }
417   if (saw_C)
418     {
419       num_args += 3;
420       if (class_files_count + zip_files_count > 0)
421         {
422           warning (0, "already-compiled .class files ignored with -C"); 
423           num_args -= class_files_count + zip_files_count;
424           class_files_count = 0;
425           zip_files_count = 0;
426         }
427       num_args += 2;  /* For -o NONE. */
428       if (saw_o)
429         fatal_error ("cannot specify both -C and -o");
430     }
431   if ((saw_o && java_files_count + class_files_count + zip_files_count > 1)
432       || (saw_C && java_files_count > 1)
433       || (indirect_files_count > 0
434           && java_files_count + class_files_count + zip_files_count > 0))
435     combine_inputs = 1;
436
437   if (combine_inputs)
438     {
439       filelist_filename = make_temp_file ("jx");
440       if (filelist_filename == NULL)
441         fatal_error ("cannot create temporary file");
442       record_temp_file (filelist_filename, ! saw_save_temps, 0);
443       filelist_file = fopen (filelist_filename, "w");
444       if (filelist_file == NULL)
445         pfatal_with_name (filelist_filename);
446       num_args -= java_files_count + class_files_count + zip_files_count;
447       num_args += 3;  /* for the combined arg "-xjava", and "-xnone" */
448     }
449
450   if (main_class_name)
451     {
452       lang_specific_extra_outfiles++;
453     }
454   if (saw_g + saw_O == 0)
455     num_args++;
456   num_args++;
457   /* An additional entry for the classpath.  */
458   num_args++;
459
460   if (combine_inputs || indirect_files_count > 0)
461     num_args += 1; /* for "-ffilelist-file" */
462   if (combine_inputs && indirect_files_count > 0)
463     fatal_error ("using both @FILE with multiple files not implemented");
464
465   /* There's no point adding -shared-libgcc if we don't have a shared
466      libgcc.  */
467 #ifndef ENABLE_SHARED_LIBGCC
468   shared_libgcc = 0;
469 #endif  
470   
471   if (java_files_count > 0)
472     ++num_args;
473
474   num_args += shared_libgcc;
475
476   num_args += link_for_bc_abi;
477
478   arglist = XNEWVEC (const char *, num_args + 1);
479   j = 0;
480
481   arglist[j++] = argv[0];
482
483   if (combine_inputs || indirect_files_count > 0)
484     arglist[j++] = "-ffilelist-file";
485
486   if (combine_inputs)
487     {
488       arglist[j++] = "-xjava";
489       arglist[j++] = filelist_filename;
490       arglist[j++] = "-xnone";
491     }
492
493   if (java_files_count > 0)
494     arglist[j++] = "-fsaw-java-file";
495
496   jcf_path_init ();
497   for (i = 1; i < argc; i++, j++)
498     {
499       arglist[j] = argv[i];
500
501       if ((args[i] & PARAM_ARG))
502         continue;
503
504       if ((args[i] & RESOURCE_FILE_ARG) != 0)
505         {
506           arglist[j++] = "-xjava";
507           arglist[j++] = argv[i];
508           arglist[j] = "-xnone";
509         }
510
511       if (argv[i][0] == '-' && argv[i][1] == 'I')
512         {
513           const char *arg;
514           if (argv[i][2] == '\0')
515             {
516               gcc_assert (i + 1 < argc && (args[i + 1] & PARAM_ARG) != 0);
517               arg = argv[i + 1];
518               /* Drop the argument.  */
519               ++i;
520             }
521           else
522             arg = &argv[i][2];
523           jcf_path_include_arg (arg);
524           --j;
525           continue;
526         }
527       if (! strcmp (argv[i], "-classpath")
528           || ! strcmp (argv[i], "-CLASSPATH"))
529         {
530           jcf_path_classpath_arg (argv[i + 1]);
531           ++i;
532           --j;
533           continue;
534         }
535       if (! strcmp (argv[i], "-bootclasspath"))
536         {
537           jcf_path_bootclasspath_arg (argv[i + 1]);
538           ++i;
539           --j;
540           continue;
541         }
542       if (! strncmp (argv[i], "-fCLASSPATH=", 12)
543           || ! strncmp (argv[i], "-fclasspath=", 12))
544         {
545           const char *p = strchr (argv[i], '=');
546           jcf_path_classpath_arg (p + 1);
547           --j;
548           continue;
549         }
550       if (! strncmp (argv[i], "-fbootclasspath=", 16))
551         {
552           const char *p = strchr (argv[i], '=');
553           jcf_path_bootclasspath_arg (p + 1);
554           --j;
555           continue;
556         }
557       if (! strcmp (argv[i], "-extdirs"))
558         {
559           jcf_path_extdirs_arg (argv[i + 1]);
560           ++i;
561           --j;
562           continue;
563         }
564
565       if (strcmp (argv[i], "-encoding") == 0)
566         {
567           arglist[j] = concat ("-f", argv[i]+1, "=", argv[i+1], NULL);
568           i++;
569           continue;
570         }
571
572       if (strcmp (argv[i], "-d") == 0)
573         {
574           arglist[j] = concat ("-foutput-class-dir=", argv[i + 1], NULL);
575           ++i;
576           continue;
577         }
578
579       if (spec_file == NULL && strncmp (argv[i], "-L", 2) == 0)
580         spec_file = find_spec_file (argv[i] + 2);
581
582       if (strncmp (argv[i], "-fmain=", 7) == 0)
583         {
584           if (! will_link)
585             fatal_error ("cannot specify %<main%> class when not linking");
586           --j;
587           continue;
588         }
589
590       if ((args[i] & INDIRECT_FILE_ARG) != 0)
591         {
592           arglist[j++] = "-xjava";
593           arglist[j++] = argv[i]+1;  /* Drop '@'. */
594           arglist[j] = "-xnone";
595         }
596
597       if ((args[i] & (CLASS_FILE_ARG|ZIP_FILE_ARG)) && saw_C)
598         {
599           --j;
600           continue;
601         }
602
603       if (combine_inputs
604           && (args[i] & (CLASS_FILE_ARG|JAVA_FILE_ARG|ZIP_FILE_ARG)) != 0)
605         {
606           fputs (argv[i], filelist_file);
607           fputc ('\n', filelist_file);
608           --j;
609           continue;
610         }
611   }
612
613   /* Handle classpath setting.  We specify the bootclasspath since
614      that requires the fewest changes to our existing code...  */
615   jcf_path_seal (0);
616   arglist[j++] = jcf_path_compute ("-fbootclasspath=");
617
618   if (combine_inputs)
619     {
620       if (fclose (filelist_file))
621         pfatal_with_name (filelist_filename);
622     }
623
624   /* If we saw no -O or -g option, default to -g1, for javac compatibility. */
625   if (saw_g + saw_O == 0)
626     arglist[j++] = "-g1";
627
628   /* Read the specs file corresponding to libgcj.
629      If we didn't find the spec file on the -L path, then we hope it
630      is somewhere in the standard install areas.  */
631   if (want_spec_file)
632     arglist[j++] = spec_file == NULL ? "-specs=libgcj.spec" : spec_file;
633
634   if (saw_C)
635     {
636       arglist[j++] = "-fsyntax-only";
637       arglist[j++] = "-femit-class-files";
638       arglist[j++] = "-S";
639       arglist[j++] = "-o";
640       arglist[j++] = "NONE";
641     }
642   
643   if (shared_libgcc)
644     arglist[j++] = "-shared-libgcc";
645
646   if (link_for_bc_abi)
647     arglist[j++] = "-s-bc-abi";
648
649   arglist[j] = NULL;
650
651   *in_argc = j;
652   *in_argv = arglist;
653   *in_added_libraries = added_libraries;
654 }
655
656 int
657 lang_specific_pre_link (void)
658 {
659   int err;
660   if (main_class_name == NULL)
661     return 0;
662   /* Append `main' to make the filename unique and allow
663
664         gcj --main=hello -save-temps hello.java
665
666      to work.  jvgenmain needs to strip this `main' to arrive at the correct
667      class name.  Append dummy `.c' that can be stripped by set_input so %b
668      is correct.  */ 
669   set_input (concat (main_class_name, "main.c", NULL));
670   err = do_spec (jvgenmain_spec);
671   if (err == 0)
672     {
673       /* Shift the outfiles array so the generated main comes first.
674          This is important when linking against (non-shared) libraries,
675          since otherwise we risk (a) nothing getting linked or
676          (b) 'main' getting picked up from a library. */
677       int i = n_infiles;
678       const char *generated = outfiles[i];
679       while (--i >= 0)
680         outfiles[i + 1] = outfiles[i];
681       outfiles[0] = generated;
682     }
683   return err;
684 }