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