OSDN Git Service

Initial revision
[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 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
28 #include "system.h"
29
30 #include "gansidecl.h"
31
32 #if defined (WITH_THREAD_posix) || defined (WITH_THREAD_pthreads)
33 #define THREAD_NAME "-lpthread"
34 #elif defined (WITH_THREAD_qt)
35 #define THREAD_NAME "-lqthreads"
36 #endif
37
38 /* This bit is set if we saw a `-xfoo' language specification.  */
39 #define LANGSPEC        (1<<1)
40 /* This bit is set if they did `-lm' or `-lmath'.  */
41 #define MATHLIB         (1<<2)
42 /* This bit is set if they did `-lc'.  */
43 #define WITHLIBC        (1<<3)
44 /* This bit is set if they did `-lgc'.  */
45 #define GCLIB           (1<<4)
46 /* This bit is set if they did `-lpthread' (or added some other thread
47    library).  */
48 #define THREADLIB       (1<<5)
49
50 #ifndef MATH_LIBRARY
51 #define MATH_LIBRARY "-lm"
52 #endif
53
54 extern char *xmalloc PROTO((size_t));
55 extern int do_spec              PROTO((char *));
56 extern char *input_filename;
57 extern size_t input_filename_length;
58
59 char *main_class_name = NULL;
60 int lang_specific_extra_outfiles = 0;
61
62 char jvgenmain_spec[] =
63   "jvgenmain %i %{!pipe:%g.i} |\n\
64    cc1 %{!pipe:%g.i} %1 \
65                    %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
66                    %{g*} %{O*} \
67                    %{v:-version} %{pg:-p} %{p} %{f*}\
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:%g.s}} |\n\
71               %{!S:as %a %Y -o %w%b%O %{!pipe:%g.s} %A\n }";
72
73 void
74 lang_specific_driver (fn, in_argc, in_argv, in_added_libraries)
75      void (*fn)();
76      int *in_argc;
77      char ***in_argv;
78      int *in_added_libraries;
79 {
80   int i, j;
81
82   /* If non-zero, the user gave us the `-v' flag.  */ 
83   int saw_verbose_flag = 0;
84
85   /* This will be 0 if we encounter a situation where we should not
86      link in libjava.  */
87   int library = 1;
88
89   /* The number of arguments being added to what's in argv, other than
90      libraries.  We use this to track the number of times we've inserted
91      -xc++/-xnone.  */
92   int added = 2;
93
94   /* Used to track options that take arguments, so we don't go wrapping
95      those with -xc++/-xnone.  */
96   char *quote = NULL;
97
98   /* The new argument list will be contained in this.  */
99   char **arglist;
100
101   /* Non-zero if we saw a `-xfoo' language specification on the
102      command line.  Used to avoid adding our own -xc++ if the user
103      already gave a language for the file.  */
104   int saw_speclang = 0;
105
106   /* "-lm" or "-lmath" if it appears on the command line.  */
107   char *saw_math = 0;
108
109   /* "-lc" if it appears on the command line.  */
110   char *saw_libc = 0;
111
112   /* "-lgc" if it appears on the command line.  */
113   char *saw_gc = 0;
114
115   /* Saw `-l' option for the thread library.  */
116   char *saw_threadlib = 0;
117
118   /* Saw `-ljava' on command line.  */
119   int saw_libjava = 0;
120
121   /* An array used to flag each argument that needs a bit set for
122      LANGSPEC, MATHLIB, WITHLIBC, or GCLIB.  */
123   int *args;
124
125   /* By default, we throw on the math library.  */
126   int need_math = 1;
127
128   /* By default, we throw in the thread library (if one is required).
129    */
130   int need_thread = 1;
131
132   /* The total number of arguments with the new stuff.  */
133   int argc;
134
135   /* The argument list.  */
136   char **argv;
137
138   /* The number of libraries added in.  */
139   int added_libraries;
140
141   /* The total number of arguments with the new stuff.  */
142   int num_args = 1;
143
144   argc = *in_argc;
145   argv = *in_argv;
146   added_libraries = *in_added_libraries;
147
148   args = (int *) xmalloc (argc * sizeof (int));
149   bzero ((char *) args, argc * sizeof (int));
150
151   for (i = 1; i < argc; i++)
152     {
153       /* If the previous option took an argument, we swallow it here.  */
154       if (quote)
155         {
156           quote = NULL;
157           continue;
158         }
159
160       /* We don't do this anymore, since we don't get them with minus
161          signs on them.  */
162       if (argv[i][0] == '\0' || argv[i][1] == '\0')
163         continue;
164
165       if (argv[i][0] == '-')
166         {
167           if (library != 0 && (strcmp (argv[i], "-nostdlib") == 0
168                                || strcmp (argv[i], "-nodefaultlibs") == 0))
169             {
170               library = 0;
171             }
172           else if (strcmp (argv[i], "-lm") == 0
173                    || strcmp (argv[i], "-lmath") == 0
174 #ifdef ALT_LIBM
175                    || strcmp (argv[i], ALT_LIBM) == 0
176 #endif
177                   )
178             {
179               args[i] |= MATHLIB;
180               need_math = 0;
181             }
182           else if (strncmp (argv[i], "-fmain=", 7) == 0)
183             main_class_name = argv[i] + 7;
184           else if (strcmp (argv[i], "-ljava") == 0)
185             saw_libjava = 1;
186           else if (strcmp (argv[i], "-lc") == 0)
187             args[i] |= WITHLIBC;
188           else if (strcmp (argv[i], "-lgc") == 0)
189             args[i] |= GCLIB;
190 #ifdef THREAD_NAME
191           else if (strcmp (argv[i], THREAD_NAME) == 0)
192             {
193               args[i] |= THREADLIB;
194               need_thread = 0;
195             }
196 #endif
197           else if (strcmp (argv[i], "-v") == 0)
198             {
199               saw_verbose_flag = 1;
200               if (argc == 2)
201                 {
202                   /* If they only gave us `-v', don't try to link
203                      in libjava.  */ 
204                   library = 0;
205                 }
206             }
207           else if (strncmp (argv[i], "-x", 2) == 0)
208             saw_speclang = 1;
209           else if (((argv[i][2] == '\0'
210                      && (char *)strchr ("bBVDUoeTuIYmLiA", argv[i][1]) != NULL)
211                     || strcmp (argv[i], "-Tdata") == 0))
212             quote = argv[i];
213           else if (library != 0 && ((argv[i][2] == '\0'
214                      && (char *) strchr ("cSEM", argv[i][1]) != NULL)
215                     || strcmp (argv[i], "-MM") == 0))
216             {
217               /* Don't specify libraries if we won't link, since that would
218                  cause a warning.  */
219               library = 0;
220               added -= 2;
221             }
222           else
223             /* Pass other options through.  */
224             continue;
225         }
226       else
227         {
228           int len; 
229
230           if (saw_speclang)
231             {
232               saw_speclang = 0;
233               continue;
234             }
235
236           /* If the filename ends in .c or .i, put options around it.
237              But not if a specified -x option is currently active.  */
238           len = strlen (argv[i]);
239           if (len > 2
240               && (argv[i][len - 1] == 'c' || argv[i][len - 1] == 'i')
241               && argv[i][len - 2] == '.')
242             {
243               args[i] |= LANGSPEC;
244               added += 2;
245             }
246         }
247     }
248
249   if (quote)
250     (*fn) ("argument to `%s' missing\n", quote);
251
252   /* If we know we don't have to do anything, bail now.  */
253   if (! added && ! library && main_class_name == NULL)
254     {
255       free (args);
256       return;
257     }
258
259   num_args = argc + added + need_math + need_thread;
260   if (main_class_name)
261     {
262       lang_specific_extra_outfiles++;
263     }
264   arglist = (char **) xmalloc (num_args * sizeof (char *));
265
266   /* NOTE: We start at 1 now, not 0.  */
267   for (i = 0, j = 0; i < argc; i++, j++)
268     {
269       arglist[j] = argv[i];
270
271       if (strncmp (argv[i], "-fmain=", 7) == 0)
272         {
273           --j;
274           continue;
275         }
276
277       /* Make sure -ljava is before the math library, since libjava
278          itself uses those math routines.  */
279       if (!saw_math && (args[i] & MATHLIB) && library)
280         {
281           --j;
282           saw_math = argv[i];
283         }
284
285       /* Likewise -ljava must come before -lc.  */
286       if (!saw_libc && (args[i] & WITHLIBC) && library)
287         {
288           --j;
289           saw_libc = argv[i];
290         }
291
292       /* And -ljava must come before -lgc.  */
293       if (!saw_gc && (args[i] & GCLIB) && library)
294         {
295           --j;
296           saw_gc = argv[i];
297         }
298
299       /* And -ljava must come before thread library.  */
300       if (!saw_threadlib && (args[i] & THREADLIB) && library)
301         {
302           --j;
303           saw_threadlib = argv[i];
304         }
305   }
306
307   /* Add `-ljava' if we haven't already done so.  */
308   if (library && ! saw_libjava)
309     {
310       arglist[j++] = "-ljava";
311       added_libraries++;
312     }
313
314   if (saw_math)
315     arglist[j++] = saw_math;
316   else if (library)
317     {
318       arglist[j++] = MATH_LIBRARY;
319       added_libraries++;
320     }
321
322   /* FIXME: we need a way to know when the GC library should be
323      added.  Then we can add it if the user hasn't already.  */
324   if (saw_gc)
325     arglist[j++] = saw_gc;
326
327   /* Thread library must come after GC library as well as after
328      -ljava.  */
329   if (saw_threadlib)
330     arglist[j++] = saw_threadlib;
331 #ifdef THREAD_NAME
332   else if (library)
333     {
334       arglist[j++] = THREAD_NAME;
335       added_libraries++;
336     }
337 #endif
338
339   if (saw_libc)
340     arglist[j++] = saw_libc;
341
342   arglist[j] = NULL;
343
344   *in_argc = j;
345   *in_argv = arglist;
346   *in_added_libraries = added_libraries;
347 }
348
349 int
350 lang_specific_pre_link ()
351 {
352   if (main_class_name == NULL)
353     return 0;
354   input_filename = main_class_name;
355   input_filename_length = strlen (main_class_name);
356   return do_spec (jvgenmain_spec);
357 }