* src/vmcore/linker.c (build_display): Removed superfluous recursion; return
[cacao.git] / src / cacaoh / cacaoh.c
1 /* src/cacaoh/cacaoh.c - main for header generation (cacaoh)
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "vm/types.h"
33
34 #include "cacaoh/headers.h"
35
36 #include "mm/gc-common.h"
37 #include "mm/memory.h"
38
39 #include "toolbox/hashtable.h"
40 #include "toolbox/logging.h"
41
42 #include "vm/exceptions.h"
43 #include "vm/global.h"
44 #include "vm/stringlocal.h"
45 #include "vm/vm.h"
46
47 #include "vmcore/classcache.h"
48 #include "vmcore/loader.h"
49 #include "vmcore/options.h"
50 #include "vmcore/statistics.h"
51 #include "vmcore/suck.h"
52
53
54 /* define cacaoh options ******************************************************/
55
56 enum {
57         OPT_HELP,
58         OPT_VERSION,
59         OPT_VERBOSE,
60         OPT_DIRECTORY,
61         OPT_CLASSPATH,
62         OPT_BOOTCLASSPATH,
63
64         DUMMY
65 };
66
67
68 opt_struct opts[] = {
69         { "help",             false, OPT_HELP          },
70         { "version",          false, OPT_VERSION       },
71         { "verbose",          false, OPT_VERBOSE       },
72         { "d",                true,  OPT_DIRECTORY     },
73         { "classpath",        true,  OPT_CLASSPATH     },
74         { "bootclasspath",    true,  OPT_BOOTCLASSPATH },
75         { NULL,               false, 0                 }
76 };
77
78
79 /* usage ***********************************************************************
80
81    Obviously prints usage information of cacaoh.
82
83 *******************************************************************************/
84
85 void usage(void)
86 {
87         printf("Usage: cacaoh [options] <classes>\n"
88                    "\n"
89                    "Options:\n"
90                    "    -help                 Print this message\n"
91                    "    -classpath <path>     \n"
92                    "    -bootclasspath <path> \n"
93                    "    -d <dir>              Output directory\n"
94                    "    -version              Print version information\n"
95                    "    -verbose              Enable verbose output\n");
96
97         /* exit with error code */
98
99         exit(1);
100 }
101
102
103 /* version *********************************************************************
104
105    Prints cacaoh version information.
106
107 *******************************************************************************/
108
109 static void version(void)
110 {
111         printf("cacaoh version "VERSION"\n");
112         printf("Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,\n");
113         printf("C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,\n");
114         printf("E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,\n");
115         printf("J. Wenninger, Institut f. Computersprachen - TU Wien\n\n");
116
117         printf("This program is free software; you can redistribute it and/or\n");
118         printf("modify it under the terms of the GNU General Public License as\n");
119         printf("published by the Free Software Foundation; either version 2, or (at\n");
120         printf("your option) any later version.\n\n");
121
122         printf("This program is distributed in the hope that it will be useful, but\n");
123         printf("WITHOUT ANY WARRANTY; without even the implied warranty of\n");
124         printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n");
125         printf("General Public License for more details.\n");
126
127         exit(0);
128 }
129
130
131 /* forward declarations *******************************************************/
132
133 static JavaVMInitArgs *cacaoh_options_prepare(int argc, char **argv);
134
135
136 /* main ************************************************************************
137
138    Main program.
139    
140 *******************************************************************************/
141
142 int main(int argc, char **argv)
143 {
144         JavaVMInitArgs *vm_args;
145         s4 i, j;
146         s4 opt;
147         classinfo *c;
148         char *opt_directory;
149
150         /********** internal (only used by main) *****************************/
151    
152         char *bootclasspath;
153         char *classpath;
154         char *cp;
155         s4    cplen;
156
157         if (argc < 2)
158                 usage();
159
160         /* set the bootclasspath */
161
162         cp = getenv("BOOTCLASSPATH");
163
164         if (cp) {
165                 bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
166                 strcpy(bootclasspath, cp);
167         }
168         else {
169                 cplen =
170 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
171                         strlen(CACAO_VM_ZIP) +
172                         strlen(":") +
173 #endif
174                         strlen(JAVA_RUNTIME_LIBRARY_CLASSES) +
175                         strlen("0");
176
177                 bootclasspath = MNEW(char, cplen);
178 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
179                 strcat(bootclasspath, CACAO_VM_ZIP);
180                 strcat(bootclasspath, ":");
181 #endif
182                 strcat(bootclasspath, JAVA_RUNTIME_LIBRARY_CLASSES);
183         }
184
185
186         /* set the classpath */
187
188         cp = getenv("CLASSPATH");
189
190         if (cp != NULL) {
191                 classpath = MNEW(char, strlen(cp) + strlen("0"));
192                 strcat(classpath, cp);
193         }
194         else {
195                 classpath = MNEW(char, strlen(".") + strlen("0"));
196                 strcpy(classpath, ".");
197         }
198
199
200         /* initialize options with default values */
201
202         opt_verbose = false;
203         opt_directory = NULL;
204
205
206         /* parse the options ******************************************************/
207
208         vm_args = cacaoh_options_prepare(argc, argv);
209
210         while ((opt = options_get(opts, vm_args)) != OPT_DONE) {
211                 switch (opt) {
212                 case OPT_IGNORE:
213                         break;
214
215                 case OPT_HELP:
216                         usage();
217                         break;
218
219                 case OPT_CLASSPATH:
220                         /* forget old classpath and set the argument as new classpath */
221                         MFREE(classpath, char, strlen(classpath));
222
223                         classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
224                         strcpy(classpath, opt_arg);
225                         break;
226
227                 case OPT_BOOTCLASSPATH:
228                         /* Forget default bootclasspath and set the argument as
229                            new boot classpath. */
230                         MFREE(bootclasspath, char, strlen(bootclasspath));
231
232                         bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
233                         strcpy(bootclasspath, opt_arg);
234                         break;
235
236                 case OPT_DIRECTORY:
237                         opt_directory = MNEW(char, strlen(opt_arg) + strlen("0"));
238                         strcpy(opt_directory, opt_arg);
239                         break;
240
241                 case OPT_VERSION:
242                         version();
243                         break;
244
245                 case OPT_VERBOSE:
246                         opt_verbose = true;
247                         loadverbose = true;
248                         break;
249
250                 default:
251                         usage();
252                 }
253         }
254                         
255         /**************************** Program start **************************/
256
257         if (opt_verbose) {
258                 log_init(NULL);
259                 log_println("Java - header-generator started"); 
260         }
261
262         utf8_init();
263
264         /* initialize the classcache hashtable stuff: lock, hashtable
265            (must be done _after_ threads_preinit) */
266
267         if (!classcache_init())
268                 vm_abort("classcache_init failed\n");
269
270         /* initialize the loader with bootclasspath (must be done _after_
271            thread_preinit) */
272
273         if (!suck_init())
274                 vm_abort("suck_init failed\n");
275
276         suck_add(bootclasspath);
277
278         /* Also add the normal classpath, so the bootstrap class loader
279            can find the files. */
280
281         suck_add(classpath);
282
283         /* AFTER: classcache_init */
284
285         loader_preinit();
286         loader_init();
287
288
289         /* load Java classes ******************************************************/
290         
291         for (i = opt_index; i < vm_args->nOptions; i++) {
292                 cp = vm_args->options[i].optionString;
293
294                 /* convert classname */
295
296                 for (j = strlen(cp) - 1; j >= 0; j--) {
297                         switch (cp[j]) {
298                         case '.':
299                                 cp[j] = '/';
300                                 break;
301                         case '_':
302                                 cp[j] = '$';
303                                 break;
304                         }
305                 }
306         
307                 /* exceptions are catched with new_exception call */
308
309                 if (!(c = load_class_bootstrap(utf_new_char(cp))))
310                         vm_abort("java.lang.NoClassDefFoundError: %s\n", cp);
311
312                 if (!link_class(c))
313                         vm_abort("java.lang.LinkageError: %s\n", cp);
314
315                 headerfile_generate(c, opt_directory);
316         }
317
318         /************************ Release all resources **********************/
319
320         loader_close();
321
322         if (opt_verbose) {
323                 log_println("Java - header-generator stopped");
324 #if defined(ENABLE_STATISTICS)
325                 statistics_print_memory_usage();
326 #endif
327         }
328         
329         return 0;
330 }
331
332
333 /* cacaoh_options_prepare ******************************************************
334
335    Prepare the JavaVMInitArgs.
336
337 *******************************************************************************/
338
339 static JavaVMInitArgs *cacaoh_options_prepare(int argc, char **argv)
340 {
341         JavaVMInitArgs *vm_args;
342         s4              i;
343
344         vm_args = NEW(JavaVMInitArgs);
345
346         vm_args->nOptions = argc - 1;
347         vm_args->options  = MNEW(JavaVMOption, argc);
348
349         for (i = 1; i < argc; i++)
350                 vm_args->options[i - 1].optionString = argv[i];
351
352         return vm_args;
353 }
354
355
356 /*
357  * These are local overrides for various environment variables in Emacs.
358  * Please do not remove this and leave it at the end of the file, where
359  * Emacs will automagically detect them.
360  * ---------------------------------------------------------------------
361  * Local variables:
362  * mode: c
363  * indent-tabs-mode: t
364  * c-basic-offset: 4
365  * tab-width: 4
366  * End:
367  */