1 /* Program to generate "main" a Java(TM) class containing a main method.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC 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)
12 GCC 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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
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. */
26 /* Written by Per Bothner <bothner@cygnus.com> */
30 #include "coretypes.h"
35 #include "java-tree.h"
37 static char * do_mangle_classname PARAMS ((const char *string));
39 struct obstack name_obstack;
40 struct obstack *mangle_obstack = &name_obstack;
43 gcc_obstack_init (obstack)
44 struct obstack *obstack;
46 /* Let particular systems override the size of a chunk. */
47 #ifndef OBSTACK_CHUNK_SIZE
48 #define OBSTACK_CHUNK_SIZE 0
50 /* Let them override the alloc and free routines too. */
51 #ifndef OBSTACK_CHUNK_ALLOC
52 #define OBSTACK_CHUNK_ALLOC xmalloc
54 #ifndef OBSTACK_CHUNK_FREE
55 #define OBSTACK_CHUNK_FREE free
57 _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
58 (void *(*) PARAMS ((long))) OBSTACK_CHUNK_ALLOC,
59 (void (*) PARAMS ((void *))) OBSTACK_CHUNK_FREE);
62 static void usage (const char *) ATTRIBUTE_NORETURN;
65 usage (const char *name)
67 fprintf (stderr, "Usage: %s [OPTIONS]... CLASSNAMEmain [OUTFILE]\n", name);
72 main (int argc, char **argv)
76 const char *mangled_classname;
82 for (i = 1; i < argc; ++i)
84 if (! strncmp (argv[i], "-D", 2))
92 if (i < argc - 2 || i == argc)
98 /* gcj always appends `main' to classname. We need to strip this here. */
99 p = strrchr (classname, 'm');
100 if (p == NULL || p == classname || strcmp (p, "main") != 0)
105 gcc_obstack_init (mangle_obstack);
106 mangled_classname = do_mangle_classname (classname);
108 if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0)
110 const char *outfile = argv[i + 1];
111 stream = fopen (outfile, "w");
114 fprintf (stderr, "%s: Cannot open output file: %s\n",
122 /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
123 option. Process them appropriately. */
124 fprintf (stream, "extern const char **_Jv_Compiler_Properties;\n");
125 fprintf (stream, "static const char *props[] =\n{\n");
126 for (i = 1; i < last_arg; ++i)
129 fprintf (stream, " \"");
130 for (p = &argv[i][2]; *p; ++p)
133 fprintf (stream, "\\%o", *p);
134 else if (*p == '\\' || *p == '"')
135 fprintf (stream, "\\%c", *p);
139 fprintf (stream, "\",\n");
141 fprintf (stream, " 0\n};\n\n");
143 fprintf (stream, "extern int %s;\n", mangled_classname);
144 fprintf (stream, "int main (int argc, const char **argv)\n");
145 fprintf (stream, "{\n");
146 fprintf (stream, " _Jv_Compiler_Properties = props;\n");
147 fprintf (stream, " JvRunMain (&%s, argc, argv);\n", mangled_classname);
148 fprintf (stream, "}\n");
149 if (stream != stdout && fclose (stream) != 0)
151 fprintf (stderr, "%s: Failed to close output file %s\n",
160 do_mangle_classname (string)
166 obstack_grow (&name_obstack, "_ZN", 3);
168 for (ptr = string; *ptr; ptr++ )
172 append_gpp_mangled_name (&ptr [-count], count);
178 append_gpp_mangled_name (&ptr [-count], count);
179 obstack_grow (mangle_obstack, "6class$E", 8);
180 obstack_1grow (mangle_obstack, '\0');
181 return obstack_finish (mangle_obstack);