]>
Commit | Line | Data |
---|---|---|
377029eb | 1 | /* Program to generate "main" a Java(TM) class containing a main method. |
d353bf18 | 2 | Copyright (C) 1998-2015 Free Software Foundation, Inc. |
377029eb | 3 | |
7d82ed5e | 4 | This file is part of GCC. |
377029eb | 5 | |
7d82ed5e | 6 | GCC is free software; you can redistribute it and/or modify |
377029eb | 7 | it under the terms of the GNU General Public License as published by |
e4b52719 | 8 | the Free Software Foundation; either version 3, or (at your option) |
377029eb | 9 | any later version. |
10 | ||
7d82ed5e | 11 | GCC is distributed in the hope that it will be useful, |
377029eb | 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. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
e4b52719 | 17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. | |
377029eb | 19 | |
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. */ | |
23 | ||
24 | /* Written by Per Bothner <bothner@cygnus.com> */ | |
25 | ||
377029eb | 26 | #include "config.h" |
014e6e0c | 27 | #include "system.h" |
805e22b2 | 28 | #include "coretypes.h" |
377029eb | 29 | #include "obstack.h" |
003019ba | 30 | #include "jcf.h" |
b20a8bb4 | 31 | #include "alias.h" |
003019ba | 32 | #include "tree.h" |
9ef16211 | 33 | #include "options.h" |
003019ba | 34 | #include "java-tree.h" |
44b49c15 | 35 | #include "intl.h" |
0b4f4daf | 36 | #include "diagnostic.h" |
377029eb | 37 | |
6852521a | 38 | static char * do_mangle_classname (const char *string); |
6599bf3a | 39 | |
bca8957b | 40 | struct obstack name_obstack; |
41 | struct obstack *mangle_obstack = &name_obstack; | |
377029eb | 42 | |
4e46d728 | 43 | static void usage (const char *) ATTRIBUTE_NORETURN; |
44 | ||
d25efa9c | 45 | static void |
46 | usage (const char *name) | |
47 | { | |
44b49c15 | 48 | fprintf (stderr, _("Usage: %s [OPTIONS]... CLASSNAMEmain [OUTFILE]\n"), |
49 | name); | |
d25efa9c | 50 | exit (1); |
51 | } | |
52 | ||
377029eb | 53 | int |
32d661fc | 54 | main (int argc, char **argv) |
377029eb | 55 | { |
32d661fc | 56 | char *classname, *p; |
377029eb | 57 | FILE *stream; |
68ddcc2a | 58 | const char *mangled_classname; |
d25efa9c | 59 | int i, last_arg; |
65bf3316 | 60 | int indirect = 0; |
61 | char *prog_name = argv[0]; | |
d25efa9c | 62 | |
0b4f4daf | 63 | p = argv[0] + strlen (argv[0]); |
64 | while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1])) | |
65 | --p; | |
66 | progname = p; | |
67 | ||
68 | xmalloc_set_program_name (progname); | |
69 | ||
4367c81f | 70 | /* Unlock the stdio streams. */ |
9c8f076b | 71 | unlock_std_streams (); |
4367c81f | 72 | |
44b49c15 | 73 | gcc_init_libintl (); |
74 | ||
0b4f4daf | 75 | diagnostic_initialize (global_dc, 0); |
76 | ||
65bf3316 | 77 | if (argc > 1 && ! strcmp (argv[1], "-findirect-dispatch")) |
78 | { | |
79 | indirect = 1; | |
80 | ++argv; | |
81 | --argc; | |
82 | } | |
83 | ||
d25efa9c | 84 | if (argc < 2) |
65bf3316 | 85 | usage (prog_name); |
377029eb | 86 | |
d25efa9c | 87 | for (i = 1; i < argc; ++i) |
377029eb | 88 | { |
d25efa9c | 89 | if (! strncmp (argv[i], "-D", 2)) |
90 | { | |
111fedf3 | 91 | /* Handled later. Check "-D XXX=YYY". */ |
92 | if (argv[i][2] == '\0') | |
93 | i++; | |
d25efa9c | 94 | } |
95 | else | |
96 | break; | |
377029eb | 97 | } |
98 | ||
d25efa9c | 99 | if (i < argc - 2 || i == argc) |
65bf3316 | 100 | usage (prog_name); |
d25efa9c | 101 | last_arg = i; |
102 | ||
103 | classname = argv[i]; | |
377029eb | 104 | |
32d661fc | 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) | |
65bf3316 | 108 | usage (prog_name); |
32d661fc | 109 | else |
110 | *p = '\0'; | |
111 | ||
bca8957b | 112 | gcc_obstack_init (mangle_obstack); |
6599bf3a | 113 | mangled_classname = do_mangle_classname (classname); |
377029eb | 114 | |
d25efa9c | 115 | if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0) |
377029eb | 116 | { |
d25efa9c | 117 | const char *outfile = argv[i + 1]; |
377029eb | 118 | stream = fopen (outfile, "w"); |
119 | if (stream == NULL) | |
120 | { | |
44b49c15 | 121 | fprintf (stderr, _("%s: Cannot open output file: %s\n"), |
65bf3316 | 122 | prog_name, outfile); |
d25efa9c | 123 | exit (1); |
377029eb | 124 | } |
125 | } | |
126 | else | |
127 | stream = stdout; | |
d25efa9c | 128 | |
129 | /* At this point every element of ARGV from 1 to LAST_ARG is a `-D' | |
130 | option. Process them appropriately. */ | |
cb4952eb | 131 | fprintf (stream, "extern const char **_Jv_Compiler_Properties;\n"); |
89f54759 | 132 | if (indirect) |
133 | fprintf (stream, "extern void JvRunMainName ();\n"); | |
134 | else | |
135 | fprintf (stream, "extern void JvRunMain ();\n"); | |
cb4952eb | 136 | fprintf (stream, "static const char *props[] =\n{\n"); |
d25efa9c | 137 | for (i = 1; i < last_arg; ++i) |
138 | { | |
139 | const char *p; | |
a17fab44 | 140 | |
141 | if (strcmp (argv[i], "-D") == 0) | |
142 | continue; | |
143 | ||
d25efa9c | 144 | fprintf (stream, " \""); |
a17fab44 | 145 | for (p = argv[i]; *p; ++p) |
d25efa9c | 146 | { |
93ce8ce4 | 147 | if (! ISPRINT (*p)) |
d25efa9c | 148 | fprintf (stream, "\\%o", *p); |
149 | else if (*p == '\\' || *p == '"') | |
150 | fprintf (stream, "\\%c", *p); | |
151 | else | |
152 | putc (*p, stream); | |
153 | } | |
154 | fprintf (stream, "\",\n"); | |
155 | } | |
156 | fprintf (stream, " 0\n};\n\n"); | |
157 | ||
377029eb | 158 | fprintf (stream, "int main (int argc, const char **argv)\n"); |
159 | fprintf (stream, "{\n"); | |
cb4952eb | 160 | fprintf (stream, " _Jv_Compiler_Properties = props;\n"); |
65bf3316 | 161 | if (indirect) |
162 | fprintf (stream, " JvRunMainName (\"%s\", argc, argv);\n", classname); | |
163 | else | |
164 | { | |
c1265840 | 165 | fprintf (stream, " extern char %s;\n", mangled_classname); |
166 | fprintf (stream, " JvRunMain (&%s, argc, argv);\n", mangled_classname); | |
65bf3316 | 167 | } |
377029eb | 168 | fprintf (stream, "}\n"); |
169 | if (stream != stdout && fclose (stream) != 0) | |
170 | { | |
44b49c15 | 171 | fprintf (stderr, _("%s: Failed to close output file %s\n"), |
65bf3316 | 172 | prog_name, argv[2]); |
d25efa9c | 173 | exit (1); |
377029eb | 174 | } |
175 | return 0; | |
176 | } | |
6599bf3a | 177 | |
178 | ||
179 | static char * | |
2883a3ed | 180 | do_mangle_classname (const char *string) |
6599bf3a | 181 | { |
bca8957b | 182 | const char *ptr; |
6599bf3a | 183 | int count = 0; |
184 | ||
6599bf3a | 185 | obstack_grow (&name_obstack, "_ZN", 3); |
186 | ||
bca8957b | 187 | for (ptr = string; *ptr; ptr++ ) |
6599bf3a | 188 | { |
2934c6b5 | 189 | if (*ptr == '.') |
6599bf3a | 190 | { |
2934c6b5 | 191 | append_gpp_mangled_name (ptr - count, count); |
bca8957b | 192 | count = 0; |
6599bf3a | 193 | } |
194 | else | |
195 | count++; | |
196 | } | |
bca8957b | 197 | append_gpp_mangled_name (&ptr [-count], count); |
c1265840 | 198 | obstack_grow (mangle_obstack, "6class$E", strlen ("6class$E")); |
7fd2eec7 | 199 | obstack_1grow (mangle_obstack, '\0'); |
25a1c410 | 200 | return XOBFINISH (mangle_obstack, char *); |
6599bf3a | 201 | } |