OSDN Git Service

2010-10-08 Robert Dewar <dewar@adacore.com>
[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 #include "opts.h"
33
34 /* Name of spec file.  */
35 #define SPEC_FILE "libgcj.spec"
36
37 /* This bit is set if we saw a `-xfoo' language specification.  */
38 #define LANGSPEC        (1<<1)
39 /* True if this arg is a .java input file name. */
40 #define JAVA_FILE_ARG   (1<<3)
41 /* True if this arg is a .class input file name. */
42 #define CLASS_FILE_ARG  (1<<4)
43 /* True if this arg is a .zip or .jar input file name. */
44 #define ZIP_FILE_ARG    (1<<5)
45 /* True if this arg is @FILE - where FILE contains a list of filenames. */
46 #define INDIRECT_FILE_ARG (1<<6)
47 /* True if this arg is a resource file.  */
48 #define RESOURCE_FILE_ARG (1<<7)
49
50 static char *find_spec_file (const char *);
51 static int verify_class_name (const char *);
52
53 static const char *main_class_name = NULL;
54 int lang_specific_extra_outfiles = 0;
55
56 /* True if we should add -shared-libgcc to the command-line.  */
57 int shared_libgcc = 1;
58
59 static const char jvgenmain_spec[] =
60   "jvgenmain %{findirect-dispatch} %{D*} %b %m.i |\n\
61    cc1 %m.i %1 \
62                    %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
63                    %{g*} %{O*} \
64                    %{v:-version} %{pg:-p} %{p}\
65                    %<fbounds-check %<fno-bounds-check\
66                    %<fassume-compiled* %<fno-assume-compiled*\
67                    %<fcompile-resource* %<fassert %<fno-assert \
68                    %<femit-class-file %<femit-class-files %<fencoding*\
69                    %<fuse-boehm-gc %<fhash-synchronization %<fjni\
70                    %<findirect-dispatch\
71                    %<fno-store-check %<foutput-class-dir\
72                    %<fclasspath* %<fbootclasspath*\
73                    %<fextdirs*\
74                    %<fuse-divide-subroutine %<fno-use-divide-subroutine\
75                    %<fuse-atomic-builtins %<fno-use-atomic-builtins\
76                    %<fcheck-references %<fno-check-references\
77                    %<ffilelist-file %<fsaw-java-file %<fsource* %<ftarget*\
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   struct stat sb;
91
92   spec = XNEWVEC (char, strlen (dir) + sizeof (SPEC_FILE) + 4);
93   strcpy (spec, dir);
94   strcat (spec, "/");
95   strcat (spec, SPEC_FILE);
96   if (! stat (spec, &sb))
97     return spec;
98   free (spec);
99   return NULL;
100 }
101
102 #define JAVA_START_CHAR_P(c) (c < 128 && (ISIDST (c) || c == '$'))
103 #define JAVA_PART_CHAR_P(c) (c < 128                                          \
104                              && (ISIDNUM (c)                                  \
105                                  || c == '$'                                  \
106                                  || (c >= 0x00 && c <= 0x08)                  \
107                                  || (c >= 0x0e && c <= 0x1b)                  \
108                                  || c == 0x7f))
109
110 /* Verify that NAME is a valid Java class name that might contain
111    `main'.  Return 0 on failure.  */
112 static int
113 verify_class_name (const char *name)
114 {
115   /* FIXME: what encoding do we use for command-line arguments?  For
116      now we assume plain ASCII, which of course is wrong.  */
117   while (*name)
118     {
119       int ch = *name++;
120       if (ch < 0 || ! JAVA_START_CHAR_P (ch))
121         return 0;
122       while (*name)
123         {
124           ch = *name++;
125           if (ch < 0)
126             return 0;
127           /* We found a break between class names.  Next character
128              must be an identifier start again.  */
129           if (ch == '.')
130             break;
131           if (! JAVA_PART_CHAR_P (ch))
132             return 0;
133         }
134     }
135
136   return 1;
137 }
138
139 void
140 lang_specific_driver (struct cl_decoded_option **in_decoded_options,
141                       unsigned int *in_decoded_options_count,
142                       int *in_added_libraries)
143 {
144   unsigned int i, j;
145
146   int saw_save_temps = 0;
147
148   /* This will be 0 if we encounter a situation where we should not
149      link in libgcj.  */
150   int library = 1;
151
152   /* This will be 1 if multiple input files (.class and/or .java)
153      should be passed to a single jc1 invocation. */
154   int combine_inputs = 0;
155
156   /* Number of .java and .class source file arguments seen. */
157   int java_files_count = 0;
158   int class_files_count = 0;
159   /* Number of .zip or .jar file arguments seen. */
160   int zip_files_count = 0;
161   /* Number of '@FILES' arguments seen. */
162   int indirect_files_count = 0;
163
164   /* Name of file containing list of files to compile. */
165   char *filelist_filename = 0;
166
167   FILE *filelist_file = 0;
168
169   /* The number of arguments being added to what's in argv, other than
170      libraries.  */
171   int added = 2;
172
173   /* The new argument list will be contained in this.  */
174   struct cl_decoded_option *new_decoded_options;
175
176   /* Nonzero if we saw a `-xfoo' language specification on the
177      command line.  Used to avoid adding our own -xc++ if the user
178      already gave a language for the file.  */
179   int saw_speclang = 0;
180
181   /* Saw --resource, -C or -o options, respectively. */
182   int saw_resource = 0;
183   int saw_C = 0;
184   int saw_o = 0;
185
186   /* Saw some -O* or -g* option, respectively. */
187   int saw_O = 0;
188   int saw_g = 0;
189
190   /* Saw a `-D' option.  */
191   int saw_D = 0;
192
193   /* An array used to flag each argument that needs a bit set for
194      LANGSPEC, MATHLIB, WITHLIBC, or GCLIB.  */
195   int *args;
196
197   /* The total number of arguments with the new stuff.  */
198   unsigned int argc;
199
200   /* The argument list.  */
201   struct cl_decoded_option *decoded_options;
202
203   /* The number of libraries added in.  */
204   int added_libraries;
205
206   /* The total number of arguments with the new stuff.  */
207   unsigned int num_args = 1;
208
209   /* Nonzero if linking is supposed to happen.  */
210   int will_link = 1;
211
212   /* Nonzero if we want to find the spec file.  */
213   int want_spec_file = 1;
214
215   /* The argument we use to specify the spec file.  */
216   char *spec_file = NULL;
217
218   /* If linking, nonzero if the BC-ABI is in use.  */
219   int link_for_bc_abi = 0;
220
221   argc = *in_decoded_options_count;
222   decoded_options = *in_decoded_options;
223   added_libraries = *in_added_libraries;
224
225   args = XCNEWVEC (int, argc);
226
227   for (i = 1; i < argc; i++)
228     {
229       switch (decoded_options[i].opt_index)
230         {
231         case OPT_nostdlib:
232         case OPT_nodefaultlibs:
233           library = 0;
234           break;
235
236         case OPT_fmain_:
237           main_class_name = decoded_options[i].arg;
238           added--;
239           break;
240
241         case OPT__help:
242           want_spec_file = 0;
243           break;
244
245         case OPT_v:
246           if (argc == 2)
247             {
248               /* If they only gave us `-v', don't try to link
249                  in libgcj.  */ 
250               library = 0;
251             }
252           break;
253
254         case OPT_x:
255           saw_speclang = 1;
256           break;
257
258         case OPT_C:
259           saw_C = 1;
260           want_spec_file = 0;
261           if (library != 0)
262             added -= 2;
263           library = 0;
264           will_link = 0;
265           break;
266
267         case OPT_fcompile_resource_:
268           saw_resource = 1;
269           want_spec_file = 0;
270           if (library != 0)
271             --added;
272           library = 0;
273           will_link = 0;
274           break;
275
276         case OPT_D:
277           saw_D = 1;
278           break;
279
280         case OPT_g:
281         case OPT_gcoff:
282         case OPT_gdwarf_:
283         case OPT_ggdb:
284         case OPT_gstabs:
285         case OPT_gstabs_:
286         case OPT_gvms:
287         case OPT_gxcoff:
288         case OPT_gxcoff_:
289           saw_g = 1;
290           break;
291
292         case OPT_O:
293         case OPT_Os:
294         case OPT_Ofast:
295           saw_O = 1;
296           break;
297
298         case OPT_o:
299           saw_o = 1;
300           break;
301
302         case OPT_fclasspath_:
303         case OPT_fbootclasspath_:
304         case OPT_extdirs:
305           added -= 1;
306           break;
307
308         case OPT_c:
309         case OPT_S:
310         case OPT_E:
311         case OPT_M:
312         case OPT_MM:
313           /* Don't specify libraries if we won't link, since that would
314              cause a warning.  */
315           library = 0;
316           added -= 2;
317
318           /* Remember this so we can confirm -fmain option.  */
319           will_link = 0;
320           break;
321
322         case OPT_fsyntax_only:
323           library = 0;
324           will_link = 0;
325           continue;
326
327         case OPT_save_temps:
328           saw_save_temps = 1;
329           break;
330
331         case OPT_static_libgcc:
332         case OPT_static:
333           shared_libgcc = 0;
334           break;
335
336         case OPT_findirect_dispatch:
337           link_for_bc_abi = 1;
338           break;
339
340         case OPT_SPECIAL_input_file:
341           {
342             const char *arg = decoded_options[i].arg;
343             int len;
344
345             /* We don't do this anymore, since we don't get them with minus
346                signs on them.  */
347             if (arg[0] == '\0' || arg[1] == '\0')
348               continue;
349
350             if (saw_speclang)
351               {
352                 saw_speclang = 0;
353                 continue;
354               }
355
356             if (saw_resource)
357               {
358                 args[i] |= RESOURCE_FILE_ARG;
359                 added += 2;  /* for -xjava and -xnone */
360               }
361
362             if (arg[0] == '@')
363               {
364                 args[i] |= INDIRECT_FILE_ARG;
365                 indirect_files_count++;
366                 added += 2;  /* for -xjava and -xnone */
367               }
368
369             len = strlen (arg);
370             if (len > 5 && strcmp (arg + len - 5, ".java") == 0)
371               {
372                 args[i] |= JAVA_FILE_ARG;
373                 java_files_count++;
374               }
375             if (len > 6 && strcmp (arg + len - 6, ".class") == 0)
376               {
377                 args[i] |= CLASS_FILE_ARG;
378                 class_files_count++;
379               }
380             if (len > 4
381                 && (strcmp (arg + len - 4, ".zip") == 0
382                     || strcmp (arg + len - 4, ".jar") == 0))
383               {
384                 args[i] |= ZIP_FILE_ARG;
385                 zip_files_count++;
386               }
387           }
388
389         default:
390           /* Pass other options through.  */
391           continue;
392         }
393     }
394
395   if (saw_D && ! main_class_name)
396     fatal_error ("can't specify %<-D%> without %<--main%>");
397
398   if (main_class_name && ! verify_class_name (main_class_name))
399     fatal_error ("%qs is not a valid class name", main_class_name);
400
401   num_args = argc + added;
402   if (saw_resource)
403     {
404       if (! saw_o)
405         fatal_error ("--resource requires -o");
406     }
407   if (saw_C)
408     {
409       num_args += 3;
410       if (class_files_count + zip_files_count > 0)
411         {
412           warning (0, "already-compiled .class files ignored with -C"); 
413           num_args -= class_files_count + zip_files_count;
414           class_files_count = 0;
415           zip_files_count = 0;
416         }
417       num_args += 2;  /* For -o NONE. */
418       if (saw_o)
419         fatal_error ("cannot specify both -C and -o");
420     }
421   if ((saw_o && java_files_count + class_files_count + zip_files_count > 1)
422       || (saw_C && java_files_count > 1)
423       || (indirect_files_count > 0
424           && java_files_count + class_files_count + zip_files_count > 0))
425     combine_inputs = 1;
426
427   if (combine_inputs)
428     {
429       filelist_filename = make_temp_file ("jx");
430       if (filelist_filename == NULL)
431         fatal_error ("cannot create temporary file");
432       record_temp_file (filelist_filename, ! saw_save_temps, 0);
433       filelist_file = fopen (filelist_filename, "w");
434       if (filelist_file == NULL)
435         pfatal_with_name (filelist_filename);
436       num_args -= java_files_count + class_files_count + zip_files_count;
437       num_args += 3;  /* for the combined arg "-xjava", and "-xnone" */
438     }
439
440   if (main_class_name)
441     {
442       lang_specific_extra_outfiles++;
443     }
444   if (saw_g + saw_O == 0)
445     num_args++;
446   num_args++;
447   /* An additional entry for the classpath.  */
448   num_args++;
449
450   if (combine_inputs || indirect_files_count > 0)
451     num_args += 1; /* for "-ffilelist-file" */
452   if (combine_inputs && indirect_files_count > 0)
453     fatal_error ("using both @FILE with multiple files not implemented");
454
455   /* There's no point adding -shared-libgcc if we don't have a shared
456      libgcc.  */
457 #ifndef ENABLE_SHARED_LIBGCC
458   shared_libgcc = 0;
459 #endif  
460   
461   if (java_files_count > 0)
462     ++num_args;
463
464   num_args += shared_libgcc;
465
466   num_args += link_for_bc_abi;
467
468   new_decoded_options = XNEWVEC (struct cl_decoded_option, num_args);
469   j = 0;
470
471   new_decoded_options[j++] = decoded_options[0];
472
473   if (combine_inputs || indirect_files_count > 0)
474     generate_option (OPT_ffilelist_file, NULL, 1, CL_DRIVER,
475                      &new_decoded_options[j++]);
476
477   if (combine_inputs)
478     {
479       generate_option (OPT_x, "java", 1, CL_DRIVER,
480                        &new_decoded_options[j++]);
481       generate_option_input_file (filelist_filename,
482                                   &new_decoded_options[j++]);
483       generate_option (OPT_x, "none", 1, CL_DRIVER,
484                        &new_decoded_options[j++]);
485     }
486
487   if (java_files_count > 0)
488     generate_option (OPT_fsaw_java_file, NULL, 1, CL_DRIVER,
489                      &new_decoded_options[j++]);
490
491   jcf_path_init ();
492   for (i = 1; i < argc; i++, j++)
493     {
494       new_decoded_options[j] = decoded_options[i];
495
496       if (decoded_options[i].errors & CL_ERR_MISSING_ARG)
497         continue;
498
499       if ((args[i] & RESOURCE_FILE_ARG) != 0)
500         {
501           generate_option (OPT_x, "java", 1, CL_DRIVER,
502                            &new_decoded_options[j++]);
503           new_decoded_options[j++] = decoded_options[i];
504           generate_option (OPT_x, "none", 1, CL_DRIVER,
505                            &new_decoded_options[j]);
506         }
507
508       switch (decoded_options[i].opt_index)
509         {
510         case OPT_I:
511           jcf_path_include_arg (decoded_options[i].arg);
512           --j;
513           continue;
514
515         case OPT_fclasspath_:
516           jcf_path_classpath_arg (decoded_options[i].arg);
517           --j;
518           continue;
519
520         case OPT_fbootclasspath_:
521           jcf_path_bootclasspath_arg (decoded_options[i].arg);
522           --j;
523           continue;
524
525         case OPT_extdirs:
526           jcf_path_extdirs_arg (decoded_options[i].arg);
527           --j;
528           continue;
529
530         case OPT_L:
531           if (spec_file == NULL)
532             spec_file = find_spec_file (decoded_options[i].arg);
533           break;
534
535         case OPT_fmain_:
536           if (! will_link)
537             fatal_error ("cannot specify %<main%> class when not linking");
538           --j;
539           continue;
540         }
541
542       if ((args[i] & INDIRECT_FILE_ARG) != 0)
543         {
544           generate_option (OPT_x, "java", 1, CL_DRIVER,
545                            &new_decoded_options[j++]);
546           /* Drop '@'.  */
547           generate_option_input_file (decoded_options[i].arg + 1,
548                                       &new_decoded_options[j++]);
549           generate_option (OPT_x, "none", 1, CL_DRIVER,
550                            &new_decoded_options[j]);
551         }
552
553       if ((args[i] & (CLASS_FILE_ARG|ZIP_FILE_ARG)) && saw_C)
554         {
555           --j;
556           continue;
557         }
558
559       if (combine_inputs
560           && (args[i] & (CLASS_FILE_ARG|JAVA_FILE_ARG|ZIP_FILE_ARG)) != 0)
561         {
562           fputs (decoded_options[i].arg, filelist_file);
563           fputc ('\n', filelist_file);
564           --j;
565           continue;
566         }
567   }
568
569   /* Handle classpath setting.  We specify the bootclasspath since
570      that requires the fewest changes to our existing code...  */
571   jcf_path_seal (0);
572   generate_option (OPT_fbootclasspath_, jcf_path_compute (""), 1,
573                    CL_DRIVER, &new_decoded_options[j++]);
574
575   if (combine_inputs)
576     {
577       if (fclose (filelist_file))
578         pfatal_with_name (filelist_filename);
579     }
580
581   /* If we saw no -O or -g option, default to -g1, for javac compatibility. */
582   if (saw_g + saw_O == 0)
583     generate_option (OPT_g, "1", 1, CL_DRIVER, &new_decoded_options[j++]);
584
585   /* Read the specs file corresponding to libgcj.
586      If we didn't find the spec file on the -L path, then we hope it
587      is somewhere in the standard install areas.  */
588   if (want_spec_file)
589     generate_option (OPT_specs_, spec_file == NULL ? "libgcj.spec" : spec_file,
590                      1, CL_DRIVER, &new_decoded_options[j++]);
591
592   if (saw_C)
593     {
594       generate_option (OPT_fsyntax_only, NULL, 1, CL_DRIVER,
595                        &new_decoded_options[j++]);
596       generate_option (OPT_femit_class_files, NULL, 1, CL_DRIVER,
597                        &new_decoded_options[j++]);
598       generate_option (OPT_S, NULL, 1, CL_DRIVER, &new_decoded_options[j++]);
599       generate_option (OPT_o, "NONE", 1, CL_DRIVER,
600                        &new_decoded_options[j++]);
601     }
602   
603   if (shared_libgcc)
604     generate_option (OPT_shared_libgcc, NULL, 1, CL_DRIVER,
605                      &new_decoded_options[j++]);
606
607   if (link_for_bc_abi)
608     generate_option (OPT_s_bc_abi, NULL, 1, CL_DRIVER,
609                      &new_decoded_options[j++]);
610
611   *in_decoded_options_count = j;
612   *in_decoded_options = new_decoded_options;
613   *in_added_libraries = added_libraries;
614 }
615
616 int
617 lang_specific_pre_link (void)
618 {
619   int err;
620   if (main_class_name == NULL)
621     return 0;
622   /* Append `main' to make the filename unique and allow
623
624         gcj --main=hello -save-temps hello.java
625
626      to work.  jvgenmain needs to strip this `main' to arrive at the correct
627      class name.  Append dummy `.c' that can be stripped by set_input so %b
628      is correct.  */ 
629   set_input (concat (main_class_name, "main.c", NULL));
630   err = do_spec (jvgenmain_spec);
631   if (err == 0)
632     {
633       /* Shift the outfiles array so the generated main comes first.
634          This is important when linking against (non-shared) libraries,
635          since otherwise we risk (a) nothing getting linked or
636          (b) 'main' getting picked up from a library. */
637       int i = n_infiles;
638       const char *generated = outfiles[i];
639       while (--i >= 0)
640         outfiles[i + 1] = outfiles[i];
641       outfiles[0] = generated;
642     }
643   return err;
644 }