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