9a3e24240c16471bc126db854ece2189a9aefead
[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 5013 2006-06-06 11:22:56Z 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 /* main ************************************************************************
151
152    Main program.
153    
154 *******************************************************************************/
155
156 int main(int argc, char **argv)
157 {
158         JavaVMInitArgs *vm_args;
159         s4 i, j;
160         s4 opt;
161         classinfo *c;
162         char *opt_directory;
163
164         /********** internal (only used by main) *****************************/
165    
166         char *bootclasspath;
167         char *classpath;
168         char *cp;
169         s4    cplen;
170         u4    heapmaxsize;
171         u4    heapstartsize;
172
173         if (argc < 2)
174                 usage();
175
176
177 #if defined(DISABLE_GC)
178         nogc_init(HEAP_MAXSIZE, HEAP_STARTSIZE);
179 #endif
180
181         /* set the bootclasspath */
182
183         cp = getenv("BOOTCLASSPATH");
184
185         if (cp) {
186                 bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
187                 strcpy(bootclasspath, cp);
188
189         } else {
190                 cplen = strlen(CACAO_VM_ZIP) +
191                         strlen(":") +
192                         strlen(CLASSPATH_GLIBJ_ZIP) +
193                         strlen("0");
194
195                 bootclasspath = MNEW(char, cplen);
196                 strcat(bootclasspath, CACAO_VM_ZIP);
197                 strcat(bootclasspath, ":");
198                 strcat(bootclasspath, CLASSPATH_GLIBJ_ZIP);
199         }
200
201
202         /* set the classpath */
203
204         cp = getenv("CLASSPATH");
205
206         if (cp) {
207                 classpath = MNEW(char, strlen(cp) + strlen("0"));
208                 strcat(classpath, cp);
209
210         } else {
211                 classpath = MNEW(char, strlen(".") + strlen("0"));
212                 strcpy(classpath, ".");
213         }
214
215
216         /* initialize options with default values */
217
218         opt_verbose = false;
219         opt_directory = NULL;
220
221         heapmaxsize = HEAP_MAXSIZE;
222         heapstartsize = HEAP_STARTSIZE;
223
224
225         /* parse the options ******************************************************/
226
227         vm_args = options_prepare(argc, argv);
228
229         while ((opt = options_get(opts, vm_args)) != OPT_DONE) {
230                 switch (opt) {
231                 case OPT_IGNORE:
232                         break;
233
234                 case OPT_HELP:
235                         usage();
236                         break;
237
238                 case OPT_CLASSPATH:
239                         /* forget old classpath and set the argument as new classpath */
240                         MFREE(classpath, char, strlen(classpath));
241
242                         classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
243                         strcpy(classpath, opt_arg);
244                         break;
245
246                 case OPT_BOOTCLASSPATH:
247                         /* Forget default bootclasspath and set the argument as
248                            new boot classpath. */
249                         MFREE(bootclasspath, char, strlen(bootclasspath));
250
251                         bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
252                         strcpy(bootclasspath, opt_arg);
253                         break;
254
255                 case OPT_DIRECTORY:
256                         opt_directory = MNEW(char, strlen(opt_arg) + strlen("0"));
257                         strcpy(opt_directory, opt_arg);
258                         break;
259
260                 case OPT_VERSION:
261                         version();
262                         break;
263
264                 case OPT_VERBOSE:
265                         opt_verbose = true;
266                         loadverbose = true;
267                         linkverbose = true;
268                         break;
269
270                 default:
271                         usage();
272                 }
273         }
274                         
275         /**************************** Program start **************************/
276
277         if (opt_verbose) {
278                 log_init(NULL);
279                 log_text("Java - header-generator started"); 
280         }
281         
282         /* initialize the garbage collector */
283
284         gc_init(heapmaxsize, heapstartsize);
285
286 #if defined(ENABLE_THREADS)
287         threads_preinit();
288 #endif
289
290         /* initialize the string hashtable stuff: lock (must be done
291            _after_ threads_preinit) */
292
293         if (!string_init())
294                 throw_main_exception_exit();
295
296         /* initialize the utf8 hashtable stuff: lock, often used utf8 strings
297            (must be done _after_ threads_preinit) */
298
299         if (!utf8_init())
300                 throw_main_exception_exit();
301
302         /* initialize the classcache hashtable stuff: lock, hashtable
303            (must be done _after_ threads_preinit) */
304
305         if (!classcache_init())
306                 throw_main_exception_exit();
307
308         /* initialize the loader with bootclasspath (must be done _after_
309            thread_preinit) */
310
311         if (!suck_init())
312                 throw_main_exception_exit();
313
314         suck_add(bootclasspath);
315
316         /* Also add the normal classpath, so the bootstrap class loader
317            can find the files. */
318
319         suck_add(classpath);
320
321         /* initialize the loader subsystems (must be done _after_
322        classcache_init) */
323
324         if (!loader_init())
325                 throw_main_exception_exit();
326
327
328         /* load Java classes ******************************************************/
329         
330         for (i = opt_index; i < vm_args->nOptions; i++) {
331                 cp = vm_args->options[i].optionString;
332
333                 /* convert classname */
334
335                 for (j = strlen(cp) - 1; j >= 0; j--) {
336                         switch (cp[j]) {
337                         case '.':
338                                 cp[j] = '/';
339                                 break;
340                         case '_':
341                                 cp[j] = '$';
342                                 break;
343                         }
344                 }
345         
346                 /* exceptions are catched with new_exception call */
347
348                 if (!(c = load_class_bootstrap(utf_new_char(cp))))
349                         throw_cacao_exception_exit(string_java_lang_NoClassDefFoundError,
350                                                                            cp);
351
352                 if (!link_class(c))
353                         throw_cacao_exception_exit(string_java_lang_LinkageError,
354                                                                            cp);
355
356                 headerfile_generate(c, opt_directory);
357         }
358
359         /************************ Release all resources **********************/
360
361         loader_close();
362
363         if (opt_verbose) {
364                 log_text("Java - header-generator stopped");
365 #if defined(ENABLE_STATISTICS)
366                 mem_usagelog(true);
367 #endif
368         }
369         
370         return 0;
371 }
372
373
374 /*
375  * These are local overrides for various environment variables in Emacs.
376  * Please do not remove this and leave it at the end of the file, where
377  * Emacs will automagically detect them.
378  * ---------------------------------------------------------------------
379  * Local variables:
380  * mode: c
381  * indent-tabs-mode: t
382  * c-basic-offset: 4
383  * tab-width: 4
384  * End:
385  */