1 /* Program to generate "main" a Java(TM) class containing a main method.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2007, 2008 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
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> */
29 #include "coretypes.h"
34 #include "java-tree.h"
37 static char * do_mangle_classname (const char *string
);
39 struct obstack name_obstack
;
40 struct obstack
*mangle_obstack
= &name_obstack
;
42 static void usage (const char *) ATTRIBUTE_NORETURN
;
45 usage (const char *name
)
47 fprintf (stderr
, _("Usage: %s [OPTIONS]... CLASSNAMEmain [OUTFILE]\n"),
53 main (int argc
, char **argv
)
57 const char *mangled_classname
;
60 char *prog_name
= argv
[0];
62 /* Unlock the stdio streams. */
63 unlock_std_streams ();
67 if (argc
> 1 && ! strcmp (argv
[1], "-findirect-dispatch"))
77 for (i
= 1; i
< argc
; ++i
)
79 if (! strncmp (argv
[i
], "-D", 2))
87 if (i
< argc
- 2 || i
== argc
)
93 /* gcj always appends `main' to classname. We need to strip this here. */
94 p
= strrchr (classname
, 'm');
95 if (p
== NULL
|| p
== classname
|| strcmp (p
, "main") != 0)
100 gcc_obstack_init (mangle_obstack
);
101 mangled_classname
= do_mangle_classname (classname
);
103 if (i
< argc
- 1 && strcmp (argv
[i
+ 1], "-") != 0)
105 const char *outfile
= argv
[i
+ 1];
106 stream
= fopen (outfile
, "w");
109 fprintf (stderr
, _("%s: Cannot open output file: %s\n"),
117 /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
118 option. Process them appropriately. */
119 fprintf (stream
, "extern const char **_Jv_Compiler_Properties;\n");
120 fprintf (stream
, "static const char *props[] =\n{\n");
121 for (i
= 1; i
< last_arg
; ++i
)
124 fprintf (stream
, " \"");
125 for (p
= &argv
[i
][2]; *p
; ++p
)
128 fprintf (stream
, "\\%o", *p
);
129 else if (*p
== '\\' || *p
== '"')
130 fprintf (stream
, "\\%c", *p
);
134 fprintf (stream
, "\",\n");
136 fprintf (stream
, " 0\n};\n\n");
138 fprintf (stream
, "int main (int argc, const char **argv)\n");
139 fprintf (stream
, "{\n");
140 fprintf (stream
, " _Jv_Compiler_Properties = props;\n");
142 fprintf (stream
, " JvRunMainName (\"%s\", argc, argv);\n", classname
);
145 fprintf (stream
, " extern char %s;\n", mangled_classname
);
146 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 (const char *string
)
165 obstack_grow (&name_obstack
, "_ZN", 3);
167 for (ptr
= string
; *ptr
; ptr
++ )
171 append_gpp_mangled_name (ptr
- count
, count
);
177 append_gpp_mangled_name (&ptr
[-count
], count
);
178 obstack_grow (mangle_obstack
, "6class$E", strlen ("6class$E"));
179 obstack_1grow (mangle_obstack
, '\0');
180 return XOBFINISH (mangle_obstack
, char *);