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