OSDN Git Service

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