1 /* Program to generate "main" a Java(TM) class containing a main method.
2 Copyright (C) 1998-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC 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 3, or (at your option)
11 GCC 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 GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
20 Java and all Java-based marks are trademarks or registered trademarks
21 of Sun Microsystems, Inc. in the United States and other countries.
22 The Free Software Foundation is independent of Sun Microsystems, Inc. */
24 /* Written by Per Bothner <bothner@cygnus.com> */
28 #include "coretypes.h"
34 #include "java-tree.h"
36 #include "diagnostic.h"
38 static char * do_mangle_classname (const char *string
);
40 struct obstack name_obstack
;
41 struct obstack
*mangle_obstack
= &name_obstack
;
43 static void usage (const char *) ATTRIBUTE_NORETURN
;
46 usage (const char *name
)
48 fprintf (stderr
, _("Usage: %s [OPTIONS]... CLASSNAMEmain [OUTFILE]\n"),
54 main (int argc
, char **argv
)
58 const char *mangled_classname
;
61 char *prog_name
= argv
[0];
63 p
= argv
[0] + strlen (argv
[0]);
64 while (p
!= argv
[0] && !IS_DIR_SEPARATOR (p
[-1]))
68 xmalloc_set_program_name (progname
);
70 /* Unlock the stdio streams. */
71 unlock_std_streams ();
75 diagnostic_initialize (global_dc
, 0);
77 if (argc
> 1 && ! strcmp (argv
[1], "-findirect-dispatch"))
87 for (i
= 1; i
< argc
; ++i
)
89 if (! strncmp (argv
[i
], "-D", 2))
91 /* Handled later. Check "-D XXX=YYY". */
92 if (argv
[i
][2] == '\0')
99 if (i
< argc
- 2 || i
== argc
)
105 /* gcj always appends `main' to classname. We need to strip this here. */
106 p
= strrchr (classname
, 'm');
107 if (p
== NULL
|| p
== classname
|| strcmp (p
, "main") != 0)
112 gcc_obstack_init (mangle_obstack
);
113 mangled_classname
= do_mangle_classname (classname
);
115 if (i
< argc
- 1 && strcmp (argv
[i
+ 1], "-") != 0)
117 const char *outfile
= argv
[i
+ 1];
118 stream
= fopen (outfile
, "w");
121 fprintf (stderr
, _("%s: Cannot open output file: %s\n"),
129 /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
130 option. Process them appropriately. */
131 fprintf (stream
, "extern const char **_Jv_Compiler_Properties;\n");
133 fprintf (stream
, "extern void JvRunMainName ();\n");
135 fprintf (stream
, "extern void JvRunMain ();\n");
136 fprintf (stream
, "static const char *props[] =\n{\n");
137 for (i
= 1; i
< last_arg
; ++i
)
141 if (strcmp (argv
[i
], "-D") == 0)
144 fprintf (stream
, " \"");
145 for (p
= argv
[i
]; *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
, "int main (int argc, const char **argv)\n");
159 fprintf (stream
, "{\n");
160 fprintf (stream
, " _Jv_Compiler_Properties = props;\n");
162 fprintf (stream
, " JvRunMainName (\"%s\", argc, argv);\n", classname
);
165 fprintf (stream
, " extern char %s;\n", mangled_classname
);
166 fprintf (stream
, " JvRunMain (&%s, argc, argv);\n", mangled_classname
);
168 fprintf (stream
, "}\n");
169 if (stream
!= stdout
&& fclose (stream
) != 0)
171 fprintf (stderr
, _("%s: Failed to close output file %s\n"),
180 do_mangle_classname (const char *string
)
185 obstack_grow (&name_obstack
, "_ZN", 3);
187 for (ptr
= string
; *ptr
; ptr
++ )
191 append_gpp_mangled_name (ptr
- count
, count
);
197 append_gpp_mangled_name (&ptr
[-count
], count
);
198 obstack_grow (mangle_obstack
, "6class$E", strlen ("6class$E"));
199 obstack_1grow (mangle_obstack
, '\0');
200 return XOBFINISH (mangle_obstack
, char *);