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