* Removed all Id tags.
[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                         linkverbose = true;
251                         break;
252
253                 default:
254                         usage();
255                 }
256         }
257                         
258         /**************************** Program start **************************/
259
260         if (opt_verbose) {
261                 log_init(NULL);
262                 log_println("Java - header-generator started"); 
263         }
264
265         /* initialize the utf8 hashtable stuff: lock, often used utf8 strings
266            (must be done _after_ threads_preinit) */
267
268         if (!utf8_init())
269                 vm_abort("utf8_init failed\n");
270
271         /* initialize the classcache hashtable stuff: lock, hashtable
272            (must be done _after_ threads_preinit) */
273
274         if (!classcache_init())
275                 vm_abort("classcache_init failed\n");
276
277         /* initialize the loader with bootclasspath (must be done _after_
278            thread_preinit) */
279
280         if (!suck_init())
281                 vm_abort("suck_init failed\n");
282
283         suck_add(bootclasspath);
284
285         /* Also add the normal classpath, so the bootstrap class loader
286            can find the files. */
287
288         suck_add(classpath);
289
290         /* initialize the loader subsystems (must be done _after_
291        classcache_init) */
292
293         if (!loader_init())
294                 vm_abort("loader_init failed\n");
295
296
297         /* load Java classes ******************************************************/
298         
299         for (i = opt_index; i < vm_args->nOptions; i++) {
300                 cp = vm_args->options[i].optionString;
301
302                 /* convert classname */
303
304                 for (j = strlen(cp) - 1; j >= 0; j--) {
305                         switch (cp[j]) {
306                         case '.':
307                                 cp[j] = '/';
308                                 break;
309                         case '_':
310                                 cp[j] = '$';
311                                 break;
312                         }
313                 }
314         
315                 /* exceptions are catched with new_exception call */
316
317                 if (!(c = load_class_bootstrap(utf_new_char(cp))))
318                         vm_abort("java.lang.NoClassDefFoundError: %s\n", cp);
319
320                 if (!link_class(c))
321                         vm_abort("java.lang.LinkageError: %s\n", cp);
322
323                 headerfile_generate(c, opt_directory);
324         }
325
326         /************************ Release all resources **********************/
327
328         loader_close();
329
330         if (opt_verbose) {
331                 log_println("Java - header-generator stopped");
332 #if defined(ENABLE_STATISTICS)
333                 statistics_print_memory_usage();
334 #endif
335         }
336         
337         return 0;
338 }
339
340
341 /* cacaoh_options_prepare ******************************************************
342
343    Prepare the JavaVMInitArgs.
344
345 *******************************************************************************/
346
347 static JavaVMInitArgs *cacaoh_options_prepare(int argc, char **argv)
348 {
349         JavaVMInitArgs *vm_args;
350         s4              i;
351
352         vm_args = NEW(JavaVMInitArgs);
353
354         vm_args->nOptions = argc - 1;
355         vm_args->options  = MNEW(JavaVMOption, argc);
356
357         for (i = 1; i < argc; i++)
358                 vm_args->options[i - 1].optionString = argv[i];
359
360         return vm_args;
361 }
362
363
364 /*
365  * These are local overrides for various environment variables in Emacs.
366  * Please do not remove this and leave it at the end of the file, where
367  * Emacs will automagically detect them.
368  * ---------------------------------------------------------------------
369  * Local variables:
370  * mode: c
371  * indent-tabs-mode: t
372  * c-basic-offset: 4
373  * tab-width: 4
374  * End:
375  */