1/* Specific flags and argument handling of the front-end of the
2   GNU compiler for the Java(TM) language.
3   Copyright (C) 1996-2015 Free Software Foundation, Inc.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3.  If not see
19<http://www.gnu.org/licenses/>.
20
21Java and all Java-based marks are trademarks or registered trademarks
22of Sun Microsystems, Inc. in the United States and other countries.
23The Free Software Foundation is independent of Sun Microsystems, Inc.  */
24
25#include "config.h"
26#include "system.h"
27#include "coretypes.h"
28#include "tm.h"
29#include "gcc.h"
30#include "jcf.h"
31#include "opts.h"
32
33/* Name of spec file.  */
34#define SPEC_FILE "libgcj.spec"
35
36/* This bit is set if we saw a `-xfoo' language specification.  */
37#define LANGSPEC	(1<<1)
38/* True if this arg is a .java input file name. */
39#define JAVA_FILE_ARG	(1<<3)
40/* True if this arg is a .class input file name. */
41#define CLASS_FILE_ARG	(1<<4)
42/* True if this arg is a .zip or .jar input file name. */
43#define ZIP_FILE_ARG	(1<<5)
44/* True if this arg is @FILE - where FILE contains a list of filenames. */
45#define INDIRECT_FILE_ARG (1<<6)
46/* True if this arg is a resource file.  */
47#define RESOURCE_FILE_ARG (1<<7)
48
49static char *find_spec_file (const char *);
50static int verify_class_name (const char *);
51
52static const char *main_class_name = NULL;
53int lang_specific_extra_outfiles = 0;
54
55/* True if we should add -shared-libgcc to the command-line.  */
56int shared_libgcc = 1;
57
58static const char jvgenmain_spec[] =
59  "jvgenmain %{findirect-dispatch} %{D*} %b %m.i |\n\
60   cc1 %m.i %1 \
61		   %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*}\
62		   %{g*} %{O*} %I \
63		   %{v:-version} %{pg:-p} %{p}\
64		   %<fbounds-check %<fno-bounds-check\
65		   %<fassume-compiled* %<fno-assume-compiled*\
66		   %<fcompile-resource* %<fassert %<fno-assert \
67		   %<femit-class-file %<femit-class-files %<fencoding*\
68		   %<fuse-boehm-gc %<fhash-synchronization %<fjni\
69		   %<findirect-dispatch\
70		   %<fno-store-check %<foutput-class-dir\
71		   %<fclasspath* %<fbootclasspath*\
72		   %<fextdirs*\
73		   %<fuse-divide-subroutine %<fno-use-divide-subroutine\
74		   %<fuse-atomic-builtins %<fno-use-atomic-builtins\
75		   %<fcheck-references %<fno-check-references\
76		   %<ffilelist-file %<fsaw-java-file %<fsource* %<ftarget*\
77		   %{f*} -fdollars-in-identifiers\
78		   %{aux-info*}\
79		   %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
80		   %{S:%W{o*}%{!o*:-o %b.s}}\
81   %(invoke_as)";
82
83/* Return full path name of spec file if it is in DIR, or NULL if
84   not.  */
85static char *
86find_spec_file (const char *dir)
87{
88  char *spec;
89  struct stat sb;
90
91  spec = XNEWVEC (char, strlen (dir) + sizeof (SPEC_FILE) + 4);
92  strcpy (spec, dir);
93  strcat (spec, "/");
94  strcat (spec, SPEC_FILE);
95  if (! stat (spec, &sb))
96    return spec;
97  free (spec);
98  return NULL;
99}
100
101#define JAVA_START_CHAR_P(c) (c < 128 && (ISIDST (c) || c == '$'))
102#define JAVA_PART_CHAR_P(c) (c < 128					      \
103			     && (ISIDNUM (c)				      \
104				 || c == '$'				      \
105				 || (c >= 0x00 && c <= 0x08)		      \
106				 || (c >= 0x0e && c <= 0x1b)		      \
107				 || c == 0x7f))
108
109/* Verify that NAME is a valid Java class name that might contain
110   `main'.  Return 0 on failure.  */
111static int
112verify_class_name (const char *name)
113{
114  /* FIXME: what encoding do we use for command-line arguments?  For
115     now we assume plain ASCII, which of course is wrong.  */
116  while (*name)
117    {
118      int ch = *name++;
119      if (ch < 0 || ! JAVA_START_CHAR_P (ch))
120	return 0;
121      while (*name)
122	{
123	  ch = *name++;
124	  if (ch < 0)
125	    return 0;
126	  /* We found a break between class names.  Next character
127	     must be an identifier start again.  */
128	  if (ch == '.')
129	    break;
130	  if (! JAVA_PART_CHAR_P (ch))
131	    return 0;
132	}
133    }
134
135  return 1;
136}
137
138void
139lang_specific_driver (struct cl_decoded_option **in_decoded_options,
140		      unsigned int *in_decoded_options_count,
141		      int *in_added_libraries)
142{
143  unsigned int i, j;
144
145  int saw_save_temps = 0;
146
147  /* This will be 0 if we encounter a situation where we should not
148     link in libgcj.  */
149  int library = 1;
150
151  /* This will be 1 if multiple input files (.class and/or .java)
152     should be passed to a single jc1 invocation. */
153  int combine_inputs = 0;
154
155  /* Number of .java and .class source file arguments seen. */
156  int java_files_count = 0;
157  int class_files_count = 0;
158  /* Number of .zip or .jar file arguments seen. */
159  int zip_files_count = 0;
160  /* Number of '@FILES' arguments seen. */
161  int indirect_files_count = 0;
162
163  /* Name of file containing list of files to compile. */
164  char *filelist_filename = 0;
165
166  FILE *filelist_file = 0;
167
168  /* The number of arguments being added to what's in argv, other than
169     libraries.  */
170  int added = 2;
171
172  /* The new argument list will be contained in this.  */
173  struct cl_decoded_option *new_decoded_options;
174
175  /* Nonzero if we saw a `-xfoo' language specification on the
176     command line.  Used to avoid adding our own -xc++ if the user
177     already gave a language for the file.  */
178  int saw_speclang = 0;
179
180  /* Saw --resource, -C or -o options, respectively. */
181  int saw_resource = 0;
182  int saw_C = 0;
183  int saw_o = 0;
184
185  /* Saw some -O* or -g* option, respectively. */
186  int saw_O = 0;
187  int saw_g = 0;
188
189  /* Saw a `-D' option.  */
190  int saw_D = 0;
191
192  /* An array used to flag each argument that needs a bit set for
193     LANGSPEC, MATHLIB, WITHLIBC, or GCLIB.  */
194  int *args;
195
196  /* The total number of arguments with the new stuff.  */
197  unsigned int argc;
198
199  /* The argument list.  */
200  struct cl_decoded_option *decoded_options;
201
202  /* The number of libraries added in.  */
203  int added_libraries;
204
205  /* The total number of arguments with the new stuff.  */
206  unsigned int num_args = 1;
207
208  /* Nonzero if linking is supposed to happen.  */
209  int will_link = 1;
210
211  /* Nonzero if we want to find the spec file.  */
212  int want_spec_file = 1;
213
214  /* The argument we use to specify the spec file.  */
215  char *spec_file = NULL;
216
217  /* If linking, nonzero if the BC-ABI is in use.  */
218  int link_for_bc_abi = 0;
219
220  argc = *in_decoded_options_count;
221  decoded_options = *in_decoded_options;
222  added_libraries = *in_added_libraries;
223
224  args = XCNEWVEC (int, argc);
225
226  for (i = 1; i < argc; i++)
227    {
228      switch (decoded_options[i].opt_index)
229	{
230	case OPT_nostdlib:
231	case OPT_nodefaultlibs:
232	  library = 0;
233	  break;
234
235	case OPT_fmain_:
236	  main_class_name = decoded_options[i].arg;
237	  added--;
238	  break;
239
240	case OPT__help:
241	  want_spec_file = 0;
242	  break;
243
244	case OPT_v:
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	  break;
252
253	case OPT_x:
254	  saw_speclang = 1;
255	  break;
256
257	case OPT_C:
258	  saw_C = 1;
259	  want_spec_file = 0;
260	  if (library != 0)
261	    added -= 2;
262	  library = 0;
263	  will_link = 0;
264	  break;
265
266	case OPT_fcompile_resource_:
267	  saw_resource = 1;
268	  want_spec_file = 0;
269	  if (library != 0)
270	    --added;
271	  library = 0;
272	  will_link = 0;
273	  break;
274
275	case OPT_D:
276	  saw_D = 1;
277	  break;
278
279	case OPT_g:
280	case OPT_gcoff:
281	case OPT_gdwarf_:
282	case OPT_ggdb:
283	case OPT_gstabs:
284	case OPT_gstabs_:
285	case OPT_gvms:
286	case OPT_gxcoff:
287	case OPT_gxcoff_:
288	  saw_g = 1;
289	  break;
290
291	case OPT_O:
292	case OPT_Os:
293	case OPT_Ofast:
294	  saw_O = 1;
295	  break;
296
297	case OPT_o:
298	  saw_o = 1;
299	  break;
300
301	case OPT_fclasspath_:
302	case OPT_fbootclasspath_:
303	case OPT_extdirs:
304	  added -= 1;
305	  break;
306
307	case OPT_c:
308	case OPT_S:
309	case OPT_E:
310	case OPT_M:
311	case OPT_MM:
312	  /* Don't specify libraries if we won't link, since that would
313	     cause a warning.  */
314	  library = 0;
315	  added -= 2;
316
317	  /* Remember this so we can confirm -fmain option.  */
318	  will_link = 0;
319	  break;
320
321	case OPT_fsyntax_only:
322	  library = 0;
323	  will_link = 0;
324	  continue;
325
326	case OPT_save_temps:
327	  saw_save_temps = 1;
328	  break;
329
330	case OPT_static_libgcc:
331	case OPT_static:
332	  shared_libgcc = 0;
333	  break;
334
335	case OPT_findirect_dispatch:
336	  link_for_bc_abi = 1;
337	  break;
338
339	case OPT_SPECIAL_input_file:
340	  {
341	    const char *arg = decoded_options[i].arg;
342	    int len;
343
344	    /* We don't do this anymore, since we don't get them with minus
345	       signs on them.  */
346	    if (arg[0] == '\0' || arg[1] == '\0')
347	      continue;
348
349	    if (saw_speclang)
350	      {
351		saw_speclang = 0;
352		continue;
353	      }
354
355	    if (saw_resource)
356	      {
357		args[i] |= RESOURCE_FILE_ARG;
358		added += 2;  /* for -xjava and -xnone */
359	      }
360
361	    if (arg[0] == '@')
362	      {
363		args[i] |= INDIRECT_FILE_ARG;
364		indirect_files_count++;
365		added += 2;  /* for -xjava and -xnone */
366	      }
367
368	    len = strlen (arg);
369	    if (len > 5 && strcmp (arg + len - 5, ".java") == 0)
370	      {
371		args[i] |= JAVA_FILE_ARG;
372		java_files_count++;
373	      }
374	    if (len > 6 && strcmp (arg + len - 6, ".class") == 0)
375	      {
376		args[i] |= CLASS_FILE_ARG;
377		class_files_count++;
378	      }
379	    if (len > 4
380		&& (strcmp (arg + len - 4, ".zip") == 0
381		    || strcmp (arg + len - 4, ".jar") == 0))
382	      {
383		args[i] |= ZIP_FILE_ARG;
384		zip_files_count++;
385	      }
386	  }
387
388	default:
389	  /* Pass other options through.  */
390	  continue;
391	}
392    }
393
394  if (saw_D && ! main_class_name)
395    fatal_error (input_location, "can%'t specify %<-D%> without %<--main%>");
396
397  if (main_class_name && ! verify_class_name (main_class_name))
398    fatal_error (input_location,
399		 "%qs is not a valid class name", main_class_name);
400
401  num_args = argc + added;
402  if (saw_resource)
403    {
404      if (! saw_o)
405	fatal_error (input_location, "--resource requires -o");
406    }
407  if (saw_C)
408    {
409      num_args += 3;
410      if (class_files_count + zip_files_count > 0)
411	{
412	  warning (0, "already-compiled .class files ignored with -C");
413	  num_args -= class_files_count + zip_files_count;
414	  class_files_count = 0;
415	  zip_files_count = 0;
416	}
417      num_args += 2;  /* For -o NONE. */
418      if (saw_o)
419	fatal_error (input_location, "cannot specify both -C and -o");
420    }
421  if ((saw_o && java_files_count + class_files_count + zip_files_count > 1)
422      || (saw_C && java_files_count > 1)
423      || (indirect_files_count > 0
424	  && java_files_count + class_files_count + zip_files_count > 0))
425    combine_inputs = 1;
426
427  if (combine_inputs)
428    {
429      filelist_filename = make_temp_file ("jx");
430      if (filelist_filename == NULL)
431	fatal_error (input_location, "cannot create temporary file");
432      record_temp_file (filelist_filename, ! saw_save_temps, 0);
433      filelist_file = fopen (filelist_filename, "w");
434      if (filelist_file == NULL)
435	pfatal_with_name (filelist_filename);
436      num_args -= java_files_count + class_files_count + zip_files_count;
437      num_args += 3;  /* for the combined arg "-xjava", and "-xnone" */
438    }
439
440  if (main_class_name)
441    {
442      lang_specific_extra_outfiles++;
443    }
444  if (saw_g + saw_O == 0)
445    num_args++;
446  num_args++;
447  /* An additional entry for the classpath.  */
448  num_args++;
449
450  if (combine_inputs || indirect_files_count > 0)
451    num_args += 1; /* for "-ffilelist-file" */
452  if (combine_inputs && indirect_files_count > 0)
453    fatal_error (input_location,
454		 "using both @FILE with multiple files not implemented");
455
456  /* There's no point adding -shared-libgcc if we don't have a shared
457     libgcc.  */
458#ifndef ENABLE_SHARED_LIBGCC
459  shared_libgcc = 0;
460#endif
461
462  if (java_files_count > 0)
463    ++num_args;
464
465  num_args += shared_libgcc;
466
467  num_args += link_for_bc_abi;
468
469  new_decoded_options = XNEWVEC (struct cl_decoded_option, num_args);
470  j = 0;
471
472  new_decoded_options[j++] = decoded_options[0];
473
474  if (combine_inputs || indirect_files_count > 0)
475    generate_option (OPT_ffilelist_file, NULL, 1, CL_DRIVER,
476		     &new_decoded_options[j++]);
477
478  if (combine_inputs)
479    {
480      generate_option (OPT_x, "java", 1, CL_DRIVER,
481		       &new_decoded_options[j++]);
482      generate_option_input_file (filelist_filename,
483				  &new_decoded_options[j++]);
484      generate_option (OPT_x, "none", 1, CL_DRIVER,
485		       &new_decoded_options[j++]);
486    }
487
488  if (java_files_count > 0)
489    generate_option (OPT_fsaw_java_file, NULL, 1, CL_DRIVER,
490		     &new_decoded_options[j++]);
491
492  jcf_path_init ();
493  for (i = 1; i < argc; i++, j++)
494    {
495      new_decoded_options[j] = decoded_options[i];
496
497      if (decoded_options[i].errors & CL_ERR_MISSING_ARG)
498	continue;
499
500      if ((args[i] & RESOURCE_FILE_ARG) != 0)
501	{
502	  generate_option (OPT_x, "java", 1, CL_DRIVER,
503			   &new_decoded_options[j++]);
504	  new_decoded_options[j++] = decoded_options[i];
505	  generate_option (OPT_x, "none", 1, CL_DRIVER,
506			   &new_decoded_options[j]);
507	}
508
509      switch (decoded_options[i].opt_index)
510	{
511	case OPT_I:
512	  jcf_path_include_arg (decoded_options[i].arg);
513	  --j;
514	  continue;
515
516	case OPT_fclasspath_:
517	  jcf_path_classpath_arg (decoded_options[i].arg);
518	  --j;
519	  continue;
520
521	case OPT_fbootclasspath_:
522	  jcf_path_bootclasspath_arg (decoded_options[i].arg);
523	  --j;
524	  continue;
525
526	case OPT_extdirs:
527	  jcf_path_extdirs_arg (decoded_options[i].arg);
528	  --j;
529	  continue;
530
531	case OPT_L:
532	  if (spec_file == NULL)
533	    spec_file = find_spec_file (decoded_options[i].arg);
534	  break;
535
536	case OPT_fmain_:
537	  if (! will_link)
538	    fatal_error (input_location,
539			 "cannot specify %<main%> class when not linking");
540	  --j;
541	  continue;
542	}
543
544      if ((args[i] & INDIRECT_FILE_ARG) != 0)
545	{
546	  generate_option (OPT_x, "java", 1, CL_DRIVER,
547			   &new_decoded_options[j++]);
548	  /* Drop '@'.  */
549	  generate_option_input_file (decoded_options[i].arg + 1,
550				      &new_decoded_options[j++]);
551	  generate_option (OPT_x, "none", 1, CL_DRIVER,
552			   &new_decoded_options[j]);
553	}
554
555      if ((args[i] & (CLASS_FILE_ARG|ZIP_FILE_ARG)) && saw_C)
556	{
557	  --j;
558	  continue;
559	}
560
561      if (combine_inputs
562	  && (args[i] & (CLASS_FILE_ARG|JAVA_FILE_ARG|ZIP_FILE_ARG)) != 0)
563	{
564	  fputs (decoded_options[i].arg, filelist_file);
565	  fputc ('\n', filelist_file);
566	  --j;
567	  continue;
568	}
569  }
570
571  /* Handle classpath setting.  We specify the bootclasspath since
572     that requires the fewest changes to our existing code...  */
573  jcf_path_seal (0);
574  generate_option (OPT_fbootclasspath_, jcf_path_compute (""), 1,
575		   CL_DRIVER, &new_decoded_options[j++]);
576
577  if (combine_inputs)
578    {
579      if (fclose (filelist_file))
580	pfatal_with_name (filelist_filename);
581    }
582
583  /* If we saw no -O or -g option, default to -g1, for javac compatibility. */
584  if (saw_g + saw_O == 0)
585    generate_option (OPT_g, "1", 1, CL_DRIVER, &new_decoded_options[j++]);
586
587  /* Read the specs file corresponding to libgcj.
588     If we didn't find the spec file on the -L path, then we hope it
589     is somewhere in the standard install areas.  */
590  if (want_spec_file)
591    generate_option (OPT_specs_, spec_file == NULL ? "libgcj.spec" : spec_file,
592		     1, CL_DRIVER, &new_decoded_options[j++]);
593
594  if (saw_C)
595    {
596      generate_option (OPT_fsyntax_only, NULL, 1, CL_DRIVER,
597		       &new_decoded_options[j++]);
598      generate_option (OPT_femit_class_files, NULL, 1, CL_DRIVER,
599		       &new_decoded_options[j++]);
600      generate_option (OPT_S, NULL, 1, CL_DRIVER, &new_decoded_options[j++]);
601      generate_option (OPT_o, "NONE", 1, CL_DRIVER,
602		       &new_decoded_options[j++]);
603    }
604
605  if (shared_libgcc)
606    generate_option (OPT_shared_libgcc, NULL, 1, CL_DRIVER,
607		     &new_decoded_options[j++]);
608
609  if (link_for_bc_abi)
610    generate_option (OPT_s_bc_abi, NULL, 1, CL_DRIVER,
611		     &new_decoded_options[j++]);
612
613  *in_decoded_options_count = j;
614  *in_decoded_options = new_decoded_options;
615  *in_added_libraries = added_libraries;
616}
617
618int
619lang_specific_pre_link (void)
620{
621  int err;
622  if (main_class_name == NULL)
623    return 0;
624  /* Append `main' to make the filename unique and allow
625
626	gcj --main=hello -save-temps hello.java
627
628     to work.  jvgenmain needs to strip this `main' to arrive at the correct
629     class name.  Append dummy `.c' that can be stripped by set_input so %b
630     is correct.  */
631  set_input (concat (main_class_name, "main.c", NULL));
632  err = do_spec (jvgenmain_spec);
633  if (err == 0)
634    {
635      /* Shift the outfiles array so the generated main comes first.
636	 This is important when linking against (non-shared) libraries,
637	 since otherwise we risk (a) nothing getting linked or
638	 (b) 'main' getting picked up from a library. */
639      int i = n_infiles;
640      const char *generated = outfiles[i];
641      while (--i >= 0)
642	outfiles[i + 1] = outfiles[i];
643      outfiles[0] = generated;
644    }
645  return err;
646}
647