OSDN Git Service

2001-02-07 Andrew Haley <aph@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
46 static char *find_spec_file     PARAMS ((const char *));
47
48 static const char *main_class_name = NULL;
49 int lang_specific_extra_outfiles = 0;
50
51 /* True if we should add -shared-libgcc to the command-line.  */
52 int shared_libgcc = 1;
53
54 const char jvgenmain_spec[] =
55   "jvgenmain %{D*} %i %{!pipe:%umain.i} |\n\
56    cc1 %{!pipe:%Umain.i} %1 \
57                    %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
58                    %{g*} %{O*} \
59                    %{v:-version} %{pg:-p} %{p}\
60                    %{<fbounds-check} %{<fno-bounds-check}\
61                    %{<fassume-compiled} %{<fno-assume-compiled}\
62                    %{<femit-class-file} %{<femit-class-files} %{<fencoding*}\
63                    %{<fuse-boehm-gc} %{<fhash-synchronization} %{<fjni}\
64                    %{<fclasspath*} %{<fCLASSPATH*} %{<foutput-class-dir}\
65                    %{<fuse-divide-subroutine} %{<fno-use-divide-subroutine}\
66                    %{<fcheck-references} %{<fno-check-references}\
67                    %{f*} -fdollars-in-identifiers\
68                    %{aux-info*}\
69                    %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
70                    %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%Umain.s}} |\n\
71               %{!S:as %a %Y -o %d%w%umain%O %{!pipe:%Umain.s} %A\n }";
72
73 /* Return full path name of spec file if it is in DIR, or NULL if
74    not.  */
75 static char *
76 find_spec_file (dir)
77      const char *dir;
78 {
79   char *spec;
80   int x;
81   struct stat sb;
82
83   spec = (char *) xmalloc (strlen (dir) + sizeof (SPEC_FILE)
84                            + sizeof ("-specs=") + 4);
85   strcpy (spec, "-specs=");
86   x = strlen (spec);
87   strcat (spec, dir);
88   strcat (spec, "/");
89   strcat (spec, SPEC_FILE);
90   if (! stat (spec + x, &sb))
91     return spec;
92   free (spec);
93   return NULL;
94 }
95
96 void
97 lang_specific_driver (in_argc, in_argv, in_added_libraries)
98      int *in_argc;
99      const char *const **in_argv;
100      int *in_added_libraries;
101 {
102   int i, j;
103
104   /* If non-zero, the user gave us the `-v' flag.  */ 
105   int saw_verbose_flag = 0;
106
107   int saw_save_temps = 0;
108
109   /* This will be 0 if we encounter a situation where we should not
110      link in libgcj.  */
111   int library = 1;
112
113   /* This will be 1 if multiple input files (.class and/or .java)
114      should be passed to a single jc1 invocation. */
115   int combine_inputs = 0;
116
117   /* Index of last .java or .class argument. */
118   int last_input_index;
119
120   /* Number of .java and .class source file arguments seen. */
121   int java_files_count = 0;
122   int class_files_count = 0;
123   /* Number of .zip or .jar file arguments seen. */
124   int zip_files_count = 0;
125   /* Number of '@FILES' arguments seen. */
126   int indirect_files_count = 0;
127
128   /* Name of file containing list of files to compile. */
129   char *filelist_filename;
130
131   FILE *filelist_file;
132
133   /* The number of arguments being added to what's in argv, other than
134      libraries.  */
135   int added = 2;
136
137   /* Used to track options that take arguments, so we don't go wrapping
138      those with -xc++/-xnone.  */
139   const char *quote = NULL;
140
141   /* The new argument list will be contained in this.  */
142   const char **arglist;
143
144   /* Non-zero if we saw a `-xfoo' language specification on the
145      command line.  Used to avoid adding our own -xc++ if the user
146      already gave a language for the file.  */
147   int saw_speclang = 0;
148
149 #if 0
150   /* "-lm" or "-lmath" if it appears on the command line.  */
151   const char *saw_math ATTRIBUTE_UNUSED = 0;
152
153   /* "-lc" if it appears on the command line.  */
154   const char *saw_libc ATTRIBUTE_UNUSED = 0;
155
156   /* "-lgcjgc" if it appears on the command line.  */
157   const char *saw_gc ATTRIBUTE_UNUSED = 0;
158
159   /* Saw `-l' option for the thread library.  */
160   const char *saw_threadlib ATTRIBUTE_UNUSED = 0;
161
162   /* Saw `-lgcj' on command line.  */
163   int saw_libgcj ATTRIBUTE_UNUSED = 0;
164 #endif
165
166   /* Saw -C or -o option, respectively. */
167   int saw_C = 0;
168   int saw_o = 0;
169
170   /* Saw some -O* or -g* option, respectively. */
171   int saw_O = 0;
172   int saw_g = 0;
173
174   /* Saw a `-D' option.  */
175   int saw_D = 0;
176
177   /* An array used to flag each argument that needs a bit set for
178      LANGSPEC, MATHLIB, WITHLIBC, or GCLIB.  */
179   int *args;
180
181   /* The total number of arguments with the new stuff.  */
182   int argc;
183
184   /* The argument list.  */
185   const char *const *argv;
186
187   /* The number of libraries added in.  */
188   int added_libraries;
189
190   /* The total number of arguments with the new stuff.  */
191   int num_args = 1;
192
193   /* Non-zero if linking is supposed to happen.  */
194   int will_link = 1;
195
196   /* Non-zero if we want to find the spec file.  */
197   int want_spec_file = 1;
198
199   /* The argument we use to specify the spec file.  */
200   char *spec_file = NULL;
201
202   argc = *in_argc;
203   argv = *in_argv;
204   added_libraries = *in_added_libraries;
205
206   args = (int *) xcalloc (argc, sizeof (int));
207
208   for (i = 1; i < argc; i++)
209     {
210       /* If the previous option took an argument, we swallow it here.  */
211       if (quote)
212         {
213           quote = NULL;
214           args[i] |= PARAM_ARG;
215           continue;
216         }
217
218       /* We don't do this anymore, since we don't get them with minus
219          signs on them.  */
220       if (argv[i][0] == '\0' || argv[i][1] == '\0')
221         continue;
222
223       if (argv[i][0] == '-')
224         {
225           if (library != 0 && (strcmp (argv[i], "-nostdlib") == 0
226                                || strcmp (argv[i], "-nodefaultlibs") == 0))
227             {
228               library = 0;
229             }
230           else if (strncmp (argv[i], "-fmain=", 7) == 0)
231             {
232               main_class_name = argv[i] + 7;
233               added--;
234             }
235           else if (strcmp (argv[i], "-fhelp") == 0)
236             want_spec_file = 0;
237           else if (strcmp (argv[i], "-v") == 0)
238             {
239               saw_verbose_flag = 1;
240               if (argc == 2)
241                 {
242                   /* If they only gave us `-v', don't try to link
243                      in libgcj.  */ 
244                   library = 0;
245                 }
246             }
247           else if (strncmp (argv[i], "-x", 2) == 0)
248             saw_speclang = 1;
249           else if (strcmp (argv[i], "-C") == 0)
250             {
251               saw_C = 1;
252               want_spec_file = 0;
253               if (library != 0)
254                 added -= 2;
255               library = 0;
256               will_link = 0;
257             }
258           else if (argv[i][1] == 'D')
259             saw_D = 1;
260           else if (argv[i][1] == 'g')
261             saw_g = 1;
262           else if (argv[i][1] == 'O')
263             saw_O = 1;
264           else if ((argv[i][2] == '\0'
265                     && (char *)strchr ("bBVDUoeTuIYmLiA", argv[i][1]) != NULL)
266                    || strcmp (argv[i], "-Tdata") == 0
267                    || strcmp (argv[i], "-MT") == 0
268                    || strcmp (argv[i], "-MF") == 0)
269             {
270               if (strcmp (argv[i], "-o") == 0)
271                 saw_o = 1;
272               quote = argv[i];
273             }
274           else if (strcmp(argv[i], "-classpath") == 0
275                    || strcmp(argv[i], "-CLASSPATH") == 0)
276             {
277               quote = argv[i];
278               added -= 1;
279             }
280           else if (library != 0 
281                    && ((argv[i][2] == '\0'
282                         && (char *) strchr ("cSEM", argv[i][1]) != NULL)
283                        || strcmp (argv[i], "-MM") == 0))
284             {
285               /* Don't specify libraries if we won't link, since that would
286                  cause a warning.  */
287               library = 0;
288               added -= 2;
289
290               /* Remember this so we can confirm -fmain option.  */
291               will_link = 0;
292             }
293           else if (strcmp (argv[i], "-d") == 0)
294             {
295               /* `-d' option is for javac compatibility.  */
296               quote = argv[i];
297               added -= 1;
298             }
299           else if (strcmp (argv[i], "-fsyntax-only") == 0
300                    || strcmp (argv[i], "--syntax-only") == 0)
301             {
302               want_spec_file = 0;
303               library = 0;
304               will_link = 0;
305               continue;
306             }
307           else if (strcmp (argv[i], "-save-temps") == 0)
308             saw_save_temps = 1;
309           else if (strcmp (argv[i], "-static-libgcc") == 0
310                    || strcmp (argv[i], "-static") == 0)
311             shared_libgcc = 0;
312           else
313             /* Pass other options through.  */
314             continue;
315         }
316       else
317         {
318           int len; 
319
320           if (saw_speclang)
321             {
322               saw_speclang = 0;
323               continue;
324             }
325
326           if (argv[i][0] == '@')
327             {
328               args[i] |= INDIRECT_FILE_ARG;
329               indirect_files_count++;
330               added += 2;  /* for -xjava and -xnone */
331             }
332
333           len = strlen (argv[i]);
334           if (len > 5 && strcmp (argv[i] + len - 5, ".java") == 0)
335             {
336               args[i] |= JAVA_FILE_ARG;
337               java_files_count++;
338               last_input_index = i;
339             }
340           if (len > 6 && strcmp (argv[i] + len - 6, ".class") == 0)
341             {
342               args[i] |= CLASS_FILE_ARG;
343               class_files_count++;
344               last_input_index = i;
345             }
346           if (len > 4
347               && (strcmp (argv[i] + len - 4, ".zip") == 0
348                   || strcmp (argv[i] + len - 4, ".jar") == 0))
349             {
350               args[i] |= ZIP_FILE_ARG;
351               zip_files_count++;
352               last_input_index = i;
353             }
354         }
355     }
356
357   if (quote)
358     fatal ("argument to `%s' missing\n", quote);
359
360   if (saw_D && ! main_class_name)
361     fatal ("can't specify `-D' without `--main'\n");
362
363   num_args = argc + added;
364   if (saw_C)
365     {
366       num_args += 3;
367       if (class_files_count + zip_files_count > 0)
368         {
369           error ("Warning: already-compiled .class files ignored with -C"); 
370           num_args -= class_files_count + zip_files_count;
371           class_files_count = 0;
372           zip_files_count = 0;
373         }
374       num_args += 2;  /* For -o NONE. */
375       if (saw_o)
376         fatal ("cannot specify both -C and -o");
377     }
378   if ((saw_o && java_files_count + class_files_count + zip_files_count > 1)
379       || (saw_C && java_files_count > 1)
380       || (indirect_files_count > 0
381           && java_files_count + class_files_count + zip_files_count > 0))
382     combine_inputs = 1;
383
384   if (combine_inputs)
385     {
386       filelist_filename = make_temp_file ("jx");
387       if (filelist_filename == NULL)
388         fatal ("cannot create temporary file");
389       record_temp_file (filelist_filename, ! saw_save_temps, 0);
390       filelist_file = fopen (filelist_filename, "w");
391       if (filelist_file == NULL)
392         pfatal_with_name (filelist_filename);
393       num_args -= java_files_count + class_files_count + zip_files_count;
394       num_args += 2;  /* for the combined arg and "-xjava" */
395     }
396   /* If we know we don't have to do anything, bail now.  */
397 #if 0
398   if (! added && ! library && main_class_name == NULL && ! saw_C)
399     {
400       free (args);
401       return;
402     }
403 #endif
404
405   if (main_class_name)
406     {
407       lang_specific_extra_outfiles++;
408     }
409   if (saw_g + saw_O == 0)
410     num_args++;
411   num_args++;
412
413   if (combine_inputs || indirect_files_count > 0)
414     num_args += 1; /* for "-ffilelist-file" */
415   if (combine_inputs && indirect_files_count > 0)
416     fatal("using both @FILE with multiple files not implemented");
417
418   /* There's no point adding -shared-libgcc if we don't have a shared
419      libgcc.  */
420 #ifndef ENABLE_SHARED_LIBGCC
421   shared_libgcc = 0;
422 #endif  
423   
424   num_args += shared_libgcc;
425
426   arglist = (const char **) xmalloc ((num_args + 1) * sizeof (char *));
427   j = 0;
428
429   for (i = 0; i < argc; i++, j++)
430     {
431       arglist[j] = argv[i];
432
433       if ((args[i] & PARAM_ARG) || i == 0)
434         continue;
435
436       if (strcmp (argv[i], "-classpath") == 0
437           || strcmp (argv[i], "-CLASSPATH") == 0)
438         {
439           char* patharg
440             = (char*) xmalloc (strlen (argv[i]) + strlen (argv[i+1]) + 3);
441           sprintf (patharg, "-f%s=%s", argv[i]+1, argv[i+1]);
442           arglist[j] = patharg;
443           i++;
444           continue;
445         }
446
447       if (strcmp (argv[i], "-d") == 0)
448         {
449           char *patharg = (char *) xmalloc (sizeof ("-foutput-class-dir=")
450                                             + strlen (argv[i + 1]) + 1);
451           sprintf (patharg, "-foutput-class-dir=%s", argv[i + 1]);
452           arglist[j] = patharg;
453           ++i;
454           continue;
455         }
456
457       if (spec_file == NULL && strncmp (argv[i], "-L", 2) == 0)
458         spec_file = find_spec_file (argv[i] + 2);
459
460       if (strncmp (argv[i], "-fmain=", 7) == 0)
461         {
462           if (! will_link)
463             fatal ("cannot specify `main' class when not linking");
464           --j;
465           continue;
466         }
467
468       if ((args[i] & INDIRECT_FILE_ARG) != 0)
469         {
470           arglist[j++] = "-xjava";
471           arglist[j++] = argv[i]+1;  /* Drop '@'. */
472           arglist[j] = "-xnone";
473         }
474
475       if ((args[i] & (CLASS_FILE_ARG|ZIP_FILE_ARG)) && saw_C)
476         {
477           --j;
478           continue;
479         }
480
481       if (combine_inputs
482           && (args[i] & (CLASS_FILE_ARG|JAVA_FILE_ARG|ZIP_FILE_ARG)) != 0)
483         {
484           fputs (argv[i], filelist_file);
485           fputc ('\n', filelist_file);
486           --j;
487           continue;
488         }
489   }
490
491   if (combine_inputs || indirect_files_count > 0)
492     arglist[j++] = "-ffilelist-file";
493
494   if (combine_inputs)
495     {
496       if (fclose (filelist_file))
497         pfatal_with_name (filelist_filename);
498       arglist[j++] = "-xjava";
499       arglist[j++] = filelist_filename;
500     }
501
502   /* If we saw no -O or -g option, default to -g1, for javac compatibility. */
503   if (saw_g + saw_O == 0)
504     arglist[j++] = "-g1";
505
506   /* Read the specs file corresponding to libgcj.
507      If we didn't find the spec file on the -L path, then we hope it
508      is somewhere in the standard install areas.  */
509   if (want_spec_file)
510     arglist[j++] = spec_file == NULL ? "-specs=libgcj.spec" : spec_file;
511
512   if (saw_C)
513     {
514       arglist[j++] = "-fsyntax-only";
515       arglist[j++] = "-femit-class-files";
516       arglist[j++] = "-S";
517       arglist[j++] = "-o";
518       arglist[j++] = "NONE";
519     }
520   
521   if (shared_libgcc)
522     arglist[j++] = "-shared-libgcc";
523
524   arglist[j] = NULL;
525
526   *in_argc = j;
527   *in_argv = arglist;
528   *in_added_libraries = added_libraries;
529 }
530
531 int
532 lang_specific_pre_link ()
533 {
534   if (main_class_name == NULL)
535     return 0;
536   input_filename = main_class_name;
537   input_filename_length = strlen (main_class_name);
538   return do_spec (jvgenmain_spec);
539 }