OSDN Git Service

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