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