1 /* Program to generate "main" a Java(TM) class containing a main method.
2 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Written by Per Bothner <bothner@cygnus.com> */
30 #include "gansidecl.h"
33 #include "java-tree.h"
35 const char main_method_prefix[] = "main__";
36 const char main_method_suffix[] = "Pt6JArray1ZPQ34java4lang6String";
37 const char class_mangling_suffix[] = ".class$";
39 struct obstack name_obstack;
41 extern void error PARAMS ((const char *, ...))
45 error VPARAMS ((const char *msgid, ...))
47 #ifndef ANSI_PROTOTYPES
54 #ifndef ANSI_PROTOTYPES
55 msgid = va_arg (ap, const char *);
58 vfprintf (stderr, msgid, ap);
63 gcc_obstack_init (obstack)
64 struct obstack *obstack;
66 /* Let particular systems override the size of a chunk. */
67 #ifndef OBSTACK_CHUNK_SIZE
68 #define OBSTACK_CHUNK_SIZE 0
70 /* Let them override the alloc and free routines too. */
71 #ifndef OBSTACK_CHUNK_ALLOC
72 #define OBSTACK_CHUNK_ALLOC xmalloc
74 #ifndef OBSTACK_CHUNK_FREE
75 #define OBSTACK_CHUNK_FREE free
77 _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
78 (void *(*) PARAMS ((long))) OBSTACK_CHUNK_ALLOC,
79 (void (*) PARAMS ((void *))) OBSTACK_CHUNK_FREE);
82 static void usage (const char *) ATTRIBUTE_NORETURN;
85 usage (const char *name)
87 fprintf (stderr, "Usage: %s [OPTIONS]... CLASSNAME [OUTFILE]\n", name);
92 main (int argc, const char **argv)
94 const char *classname;
96 const char *mangled_classname;
102 for (i = 1; i < argc; ++i)
104 if (! strncmp (argv[i], "-D", 2))
112 if (i < argc - 2 || i == argc)
118 gcc_obstack_init (&name_obstack);
119 append_gpp_mangled_classtype (&name_obstack, classname);
120 obstack_1grow (&name_obstack, '\0');
121 mangled_classname = obstack_finish (&name_obstack);
123 if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0)
125 const char *outfile = argv[i + 1];
126 stream = fopen (outfile, "w");
129 fprintf (stderr, "%s: Cannot open output file: %s\n",
137 /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
138 option. Process them appropriately. */
139 fprintf (stream, "extern const char **_Jv_Compiler_Properties;\n");
140 fprintf (stream, "static const char *props[] =\n{\n");
141 for (i = 1; i < last_arg; ++i)
144 fprintf (stream, " \"");
145 for (p = &argv[i][2]; *p; ++p)
148 fprintf (stream, "\\%o", *p);
149 else if (*p == '\\' || *p == '"')
150 fprintf (stream, "\\%c", *p);
154 fprintf (stream, "\",\n");
156 fprintf (stream, " 0\n};\n\n");
158 fprintf (stream, "extern int class __attribute__ ((alias (\"_%s%s\")));\n",
159 mangled_classname, class_mangling_suffix);
160 fprintf (stream, "int main (int argc, const char **argv)\n");
161 fprintf (stream, "{\n");
162 fprintf (stream, " _Jv_Compiler_Properties = props;\n");
163 fprintf (stream, " JvRunMain (&class, argc, argv);\n");
164 fprintf (stream, "}\n");
165 if (stream != stdout && fclose (stream) != 0)
167 fprintf (stderr, "%s: Failed to close output file %s\n",