Patch: `-D' option for gcj

Tom Tromey tromey@cygnus.com
Sun Oct 31 23:33:00 GMT 1999


I heard this didn't get through the first time, so I'm resending.

I'm committing the appended patch.  It adds a `-D' option to gcj.
Usage is like C: -DVAR[=VALUE].  These options are compiled into the
resulting executable and used to populate the system properties at
runtime.

1999-10-14  Tom Tromey  <tromey@cygnus.com>

	* jvgenmain.c (usage): New function.
	(main): Use it.  Also, handle `-D' options.
	* jvspec.c (lang_specific_driver): Recognize -D.
	(jvgenmain_spec): Added `%{D*}' to jvgenmain invocation.

Tom

Index: jvspec.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/java/jvspec.c,v
retrieving revision 1.26
diff -u -r1.26 jvspec.c
--- jvspec.c	1999/10/14 20:55:21	1.26
+++ jvspec.c	1999/10/15 16:41:01
@@ -50,7 +50,7 @@
 #define COMBINE_INPUTS 0
 
 const char jvgenmain_spec[] =
-  "jvgenmain %i %{!pipe:%umain.i} |\n\
+  "jvgenmain %{D*} %i %{!pipe:%umain.i} |\n\
    cc1 %{!pipe:%Umain.i} %1 \
 		   %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
 		   %{g*} %{O*} \
@@ -163,6 +163,9 @@
   int saw_O = 0;
   int saw_g = 0;
 
+  /* Saw a `-D' option.  */
+  int saw_D = 0;
+
   /* An array used to flag each argument that needs a bit set for
      LANGSPEC, MATHLIB, WITHLIBC, or GCLIB.  */
   int *args;
@@ -247,6 +250,8 @@
 	      library = 0;
 	      will_link = 0;
 	    }
+	  else if (argv[i][1] == 'D')
+	    saw_D = 1;
 	  else if (argv[i][1] == 'g')
 	    saw_g = 1;
 	  else if (argv[i][1] == 'O')
@@ -330,6 +335,9 @@
 
   if (quote)
     fatal ("argument to `%s' missing\n", quote);
+
+  if (saw_D && ! main_class_name)
+    fatal ("can't specify `-D' without `--main'\n");
 
   num_args = argc + added;
   if (saw_C)
Index: jvgenmain.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/java/jvgenmain.c,v
retrieving revision 1.11
diff -u -r1.11 jvgenmain.c
--- jvgenmain.c	1999/09/16 15:45:11	1.11
+++ jvgenmain.c	1999/10/15 16:41:01
@@ -1,5 +1,5 @@
 /* Program to generate "main" a Java(TM) class containing a main method.
-   Copyright (C) 1998 Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999 Free Software Foundation, Inc.
 
 This file is part of GNU CC.
 
@@ -79,39 +79,79 @@
 		  (void (*) PROTO((void *))) OBSTACK_CHUNK_FREE);
 }
 
+static void
+usage (const char *name)
+{
+  fprintf (stderr, "Usage: %s [OPTIONS]... CLASSNAME [OUTFILE]\n", name);
+  exit (1);
+}
+
 int
 main (int argc, const char **argv)
 {
   const char *classname;
   FILE *stream;
   const char *mangled_classname;
+  int i, last_arg;
+
+  if (argc < 2)
+    usage (argv[0]);
 
-  if (argc < 2 || argc > 3)
+  for (i = 1; i < argc; ++i)
     {
-      fprintf (stderr, "Usage: %s CLASSNAME [OUTFILE]\n", argv[0]);
-      exit(-1);
+      if (! strncmp (argv[i], "-D", 2))
+	{
+	  /* Handled later.  */
+	}
+      else
+	break;
     }
+
+  if (i < argc - 2 || i == argc)
+    usage (argv[0]);
+  last_arg = i;
 
-  classname = argv[1];
+  classname = argv[i];
 
   gcc_obstack_init (&name_obstack);
   append_gpp_mangled_classtype (&name_obstack, classname);
   obstack_1grow (&name_obstack, '\0');
   mangled_classname = obstack_finish (&name_obstack);
 
-  if (argc > 2 && strcmp (argv[2], "-") != 0)
+  if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0)
     {
-      const char *outfile = argv[2];
+      const char *outfile = argv[i + 1];
       stream = fopen (outfile, "w");
       if (stream == NULL)
 	{
 	  fprintf (stderr, "%s: Cannot open output file: %s\n",
 		   argv[0], outfile);
-	  exit (-1);
+	  exit (1);
 	}
     }
   else
     stream = stdout;
+
+  /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
+     option.  Process them appropriately.  */
+  fprintf (stream, "const char *_Jv_Compiler_Properties[] =\n{\n");
+  for (i = 1; i < last_arg; ++i)
+    {
+      const char *p;
+      fprintf (stream, "  \"");
+      for (p = &argv[i][2]; *p; ++p)
+	{
+	  if (! isascii (*p))
+	    fprintf (stream, "\\%o", *p);
+	  else if (*p == '\\' || *p == '"')
+	    fprintf (stream, "\\%c", *p);
+	  else
+	    putc (*p, stream);
+	}
+      fprintf (stream, "\",\n");
+    }
+  fprintf (stream, "  0\n};\n\n");
+
   fprintf (stream, "extern struct Class %s%s;\n",
 	   class_mangling_prefix, mangled_classname);
   fprintf (stream, "int main (int argc, const char **argv)\n");
@@ -123,7 +163,7 @@
     {
       fprintf (stderr, "%s: Failed to close output file %s\n",
 	       argv[0], argv[2]);
-      exit (-1);
+      exit (1);
     }
   return 0;
 }



More information about the Gcc-patches mailing list