OSDN Git Service

2001-09-06 Alexandre Petit-Bianco <apbianco@redhat.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 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
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)
10 any later version.
11
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.
16
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. 
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 "gcc.h"
29
30 /* Name of spec file.  */
31 #define SPEC_FILE "libgcj.spec"
32
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)
47
48 static char *find_spec_file     PARAMS ((const char *));
49
50 static const char *main_class_name = NULL;
51 int lang_specific_extra_outfiles = 0;
52
53 /* True if we should add -shared-libgcc to the command-line.  */
54 int shared_libgcc = 1;
55
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*}\
60                    %{g*} %{O*} \
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}\
70                    %{<ffilelist-file}\
71                    %{f*} -fdollars-in-identifiers\
72                    %{aux-info*}\
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 }";
76
77 /* Return full path name of spec file if it is in DIR, or NULL if
78    not.  */
79 static char *
80 find_spec_file (dir)
81      const char *dir;
82 {
83   char *spec;
84   int x;
85   struct stat sb;
86
87   spec = (char *) xmalloc (strlen (dir) + sizeof (SPEC_FILE)
88                            + sizeof ("-specs=") + 4);
89   strcpy (spec, "-specs=");
90   x = strlen (spec);
91   strcat (spec, dir);
92   strcat (spec, "/");
93   strcat (spec, SPEC_FILE);
94   if (! stat (spec + x, &sb))
95     return spec;
96   free (spec);
97   return NULL;
98 }
99
100 void
101 lang_specific_driver (in_argc, in_argv, in_added_libraries)
102      int *in_argc;
103      const char *const **in_argv;
104      int *in_added_libraries;
105 {
106   int i, j;
107
108   /* If non-zero, the user gave us the `-v' flag.  */ 
109   int saw_verbose_flag = 0;
110
111   int saw_save_temps = 0;
112
113   /* This will be 0 if we encounter a situation where we should not
114      link in libgcj.  */
115   int library = 1;
116
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;
120
121   /* Index of last .java or .class argument. */
122   int last_input_index;
123
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;
131
132   /* Name of file containing list of files to compile. */
133   char *filelist_filename = 0;
134
135   FILE *filelist_file = 0;
136
137   /* The number of arguments being added to what's in argv, other than
138      libraries.  */
139   int added = 2;
140
141   /* Used to track options that take arguments, so we don't go wrapping
142      those with -xc++/-xnone.  */
143   const char *quote = NULL;
144
145   /* The new argument list will be contained in this.  */
146   const char **arglist;
147
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;
152
153 #if 0
154   /* "-lm" or "-lmath" if it appears on the command line.  */
155   const char *saw_math ATTRIBUTE_UNUSED = 0;
156
157   /* "-lc" if it appears on the command line.  */
158   const char *saw_libc ATTRIBUTE_UNUSED = 0;
159
160   /* "-lgcjgc" if it appears on the command line.  */
161   const char *saw_gc ATTRIBUTE_UNUSED = 0;
162
163   /* Saw `-l' option for the thread library.  */
164   const char *saw_threadlib ATTRIBUTE_UNUSED = 0;
165
166   /* Saw `-lgcj' on command line.  */
167   int saw_libgcj ATTRIBUTE_UNUSED = 0;
168 #endif
169
170   /* Saw -R, -C or -o options, respectively. */
171   int saw_R = 0;
172   int saw_C = 0;
173   int saw_o = 0;
174
175   /* Saw some -O* or -g* option, respectively. */
176   int saw_O = 0;
177   int saw_g = 0;
178
179   /* Saw a `-D' option.  */
180   int saw_D = 0;
181
182   /* An array used to flag each argument that needs a bit set for
183      LANGSPEC, MATHLIB, WITHLIBC, or GCLIB.  */
184   int *args;
185
186   /* The total number of arguments with the new stuff.  */
187   int argc;
188
189   /* The argument list.  */
190   const char *const *argv;
191
192   /* The number of libraries added in.  */
193   int added_libraries;
194
195   /* The total number of arguments with the new stuff.  */
196   int num_args = 1;
197
198   /* Non-zero if linking is supposed to happen.  */
199   int will_link = 1;
200
201   /* Non-zero if we want to find the spec file.  */
202   int want_spec_file = 1;
203
204   /* The argument we use to specify the spec file.  */
205   char *spec_file = NULL;
206
207   argc = *in_argc;
208   argv = *in_argv;
209   added_libraries = *in_added_libraries;
210
211   args = (int *) xcalloc (argc, sizeof (int));
212
213   for (i = 1; i < argc; i++)
214     {
215       /* If the previous option took an argument, we swallow it here.  */
216       if (quote)
217         {
218           quote = NULL;
219           args[i] |= PARAM_ARG;
220           continue;
221         }
222
223       /* We don't do this anymore, since we don't get them with minus
224          signs on them.  */
225       if (argv[i][0] == '\0' || argv[i][1] == '\0')
226         continue;
227
228       if (argv[i][0] == '-')
229         {
230           if (library != 0 && (strcmp (argv[i], "-nostdlib") == 0
231                                || strcmp (argv[i], "-nodefaultlibs") == 0))
232             {
233               library = 0;
234             }
235           else if (strncmp (argv[i], "-fmain=", 7) == 0)
236             {
237               main_class_name = argv[i] + 7;
238               added--;
239             }
240           else if (strcmp (argv[i], "-fhelp") == 0)
241             want_spec_file = 0;
242           else if (strcmp (argv[i], "-v") == 0)
243             {
244               saw_verbose_flag = 1;
245               if (argc == 2)
246                 {
247                   /* If they only gave us `-v', don't try to link
248                      in libgcj.  */ 
249                   library = 0;
250                 }
251             }
252           else if (strncmp (argv[i], "-x", 2) == 0)
253             saw_speclang = 1;
254           else if (strcmp (argv[i], "-C") == 0)
255             {
256               saw_C = 1;
257               want_spec_file = 0;
258               if (library != 0)
259                 added -= 2;
260               library = 0;
261               will_link = 0;
262             }
263           else if (strcmp (argv[i], "-R") == 0)
264             {
265               saw_R = 1;
266               quote = argv[i];
267               want_spec_file = 0;
268               if (library != 0)
269                 added -= 2;
270               library = 0;
271               will_link = 0;
272             }
273           else if (argv[i][1] == 'D')
274             saw_D = 1;
275           else if (argv[i][1] == 'g')
276             saw_g = 1;
277           else if (argv[i][1] == 'O')
278             saw_O = 1;
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)
284             {
285               if (strcmp (argv[i], "-o") == 0)
286                 saw_o = 1;
287               quote = argv[i];
288             }
289           else if (strcmp(argv[i], "-classpath") == 0
290                    || strcmp(argv[i], "-CLASSPATH") == 0)
291             {
292               quote = argv[i];
293               added -= 1;
294             }
295           else if (library != 0 
296                    && ((argv[i][2] == '\0'
297                         && (char *) strchr ("cSEM", argv[i][1]) != NULL)
298                        || strcmp (argv[i], "-MM") == 0))
299             {
300               /* Don't specify libraries if we won't link, since that would
301                  cause a warning.  */
302               library = 0;
303               added -= 2;
304
305               /* Remember this so we can confirm -fmain option.  */
306               will_link = 0;
307             }
308           else if (strcmp (argv[i], "-d") == 0)
309             {
310               /* `-d' option is for javac compatibility.  */
311               quote = argv[i];
312               added -= 1;
313             }
314           else if (strcmp (argv[i], "-fsyntax-only") == 0
315                    || strcmp (argv[i], "--syntax-only") == 0)
316             {
317               want_spec_file = 0;
318               library = 0;
319               will_link = 0;
320               continue;
321             }
322           else if (strcmp (argv[i], "-save-temps") == 0)
323             saw_save_temps = 1;
324           else if (strcmp (argv[i], "-static-libgcc") == 0
325                    || strcmp (argv[i], "-static") == 0)
326             shared_libgcc = 0;
327           else
328             /* Pass other options through.  */
329             continue;
330         }
331       else
332         {
333           int len; 
334
335           if (saw_speclang)
336             {
337               saw_speclang = 0;
338               continue;
339             }
340
341           if (saw_R)
342             {
343               args[i] |= RESOURCE_FILE_ARG;
344               last_input_index = i;
345               added += 2;  /* for -xjava and -xnone */
346             }
347
348           if (argv[i][0] == '@')
349             {
350               args[i] |= INDIRECT_FILE_ARG;
351               indirect_files_count++;
352               added += 2;  /* for -xjava and -xnone */
353             }
354
355           len = strlen (argv[i]);
356           if (len > 5 && strcmp (argv[i] + len - 5, ".java") == 0)
357             {
358               args[i] |= JAVA_FILE_ARG;
359               java_files_count++;
360               last_input_index = i;
361             }
362           if (len > 6 && strcmp (argv[i] + len - 6, ".class") == 0)
363             {
364               args[i] |= CLASS_FILE_ARG;
365               class_files_count++;
366               last_input_index = i;
367             }
368           if (len > 4
369               && (strcmp (argv[i] + len - 4, ".zip") == 0
370                   || strcmp (argv[i] + len - 4, ".jar") == 0))
371             {
372               args[i] |= ZIP_FILE_ARG;
373               zip_files_count++;
374               last_input_index = i;
375             }
376         }
377     }
378
379   if (quote)
380     fatal ("argument to `%s' missing\n", quote);
381
382   if (saw_D && ! main_class_name)
383     fatal ("can't specify `-D' without `--main'\n");
384
385   num_args = argc + added;
386   if (saw_R)
387     {
388       if (! saw_o)
389         fatal ("-R requires -o");
390     }
391   if (saw_C)
392     {
393       num_args += 3;
394       if (class_files_count + zip_files_count > 0)
395         {
396           error ("Warning: already-compiled .class files ignored with -C"); 
397           num_args -= class_files_count + zip_files_count;
398           class_files_count = 0;
399           zip_files_count = 0;
400         }
401       num_args += 2;  /* For -o NONE. */
402       if (saw_o)
403         fatal ("cannot specify both -C and -o");
404     }
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))
409     combine_inputs = 1;
410
411   if (combine_inputs)
412     {
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" */
422     }
423   /* If we know we don't have to do anything, bail now.  */
424 #if 0
425   if (! added && ! library && main_class_name == NULL && ! saw_C)
426     {
427       free (args);
428       return;
429     }
430 #endif
431
432   if (main_class_name)
433     {
434       lang_specific_extra_outfiles++;
435     }
436   if (saw_g + saw_O == 0)
437     num_args++;
438   num_args++;
439
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");
444
445   /* There's no point adding -shared-libgcc if we don't have a shared
446      libgcc.  */
447 #ifndef ENABLE_SHARED_LIBGCC
448   shared_libgcc = 0;
449 #endif  
450   
451   num_args += shared_libgcc;
452
453   arglist = (const char **) xmalloc ((num_args + 1) * sizeof (char *));
454   j = 0;
455
456   for (i = 0; i < argc; i++, j++)
457     {
458       arglist[j] = argv[i];
459
460       if ((args[i] & PARAM_ARG) || i == 0)
461         continue;
462
463       if ((args[i] & RESOURCE_FILE_ARG) != 0)
464         {
465           arglist[j++] = "-xjava";
466           arglist[j++] = argv[i];
467           arglist[j] = "-xnone";
468         }
469
470       if (strcmp (argv[i], "-R") == 0)
471         {
472           arglist[j] = concat ("-fcompile-resource=",
473                                *argv[i+1] == '/' ? "" : "/",
474                                argv[i+1], NULL);
475           i++;
476           continue;
477         }
478
479       if (strcmp (argv[i], "-classpath") == 0
480           || strcmp (argv[i], "-CLASSPATH") == 0)
481         {
482           arglist[j] = concat ("-f", argv[i]+1, "=", argv[i+1], NULL);
483           i++;
484           continue;
485         }
486
487       if (strcmp (argv[i], "-d") == 0)
488         {
489           arglist[j] = concat ("-foutput-class-dir=", argv[i + 1], NULL);
490           ++i;
491           continue;
492         }
493
494       if (spec_file == NULL && strncmp (argv[i], "-L", 2) == 0)
495         spec_file = find_spec_file (argv[i] + 2);
496
497       if (strncmp (argv[i], "-fmain=", 7) == 0)
498         {
499           if (! will_link)
500             fatal ("cannot specify `main' class when not linking");
501           --j;
502           continue;
503         }
504
505       if ((args[i] & INDIRECT_FILE_ARG) != 0)
506         {
507           arglist[j++] = "-xjava";
508           arglist[j++] = argv[i]+1;  /* Drop '@'. */
509           arglist[j] = "-xnone";
510         }
511
512       if ((args[i] & (CLASS_FILE_ARG|ZIP_FILE_ARG)) && saw_C)
513         {
514           --j;
515           continue;
516         }
517
518       if (combine_inputs
519           && (args[i] & (CLASS_FILE_ARG|JAVA_FILE_ARG|ZIP_FILE_ARG)) != 0)
520         {
521           fputs (argv[i], filelist_file);
522           fputc ('\n', filelist_file);
523           --j;
524           continue;
525         }
526   }
527
528   if (combine_inputs || indirect_files_count > 0)
529     arglist[j++] = "-ffilelist-file";
530
531   if (combine_inputs)
532     {
533       if (fclose (filelist_file))
534         pfatal_with_name (filelist_filename);
535       arglist[j++] = "-xjava";
536       arglist[j++] = filelist_filename;
537     }
538
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";
542
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.  */
546   if (want_spec_file)
547     arglist[j++] = spec_file == NULL ? "-specs=libgcj.spec" : spec_file;
548
549   if (saw_C)
550     {
551       arglist[j++] = "-fsyntax-only";
552       arglist[j++] = "-femit-class-files";
553       arglist[j++] = "-S";
554       arglist[j++] = "-o";
555       arglist[j++] = "NONE";
556     }
557   
558   if (shared_libgcc)
559     arglist[j++] = "-shared-libgcc";
560
561   arglist[j] = NULL;
562
563   *in_argc = j;
564   *in_argv = arglist;
565   *in_added_libraries = added_libraries;
566 }
567
568 int
569 lang_specific_pre_link ()
570 {
571   int err;
572   if (main_class_name == NULL)
573     return 0;
574   input_filename = main_class_name;
575   input_filename_length = strlen (main_class_name);
576   err = do_spec (jvgenmain_spec);
577   if (err == 0)
578     {
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. */
583       int i = n_infiles;
584       const char *generated = outfiles[i];
585       while (--i >= 0)
586         outfiles[i + 1] = outfiles[i];
587       outfiles[0] = generated;
588     }
589   return err;
590 }