1 /* src/cacaoh/cacaoh.c - main for header generation (cacaoh)
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
8 This file is part of CACAO.
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.
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.
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
25 Contact: cacao@cacaojvm.org
27 Authors: Reinhard Grafl
32 $Id: cacaoh.c 6286 2007-01-10 10:03:38Z twisti $
45 #include "cacaoh/headers.h"
46 #include "mm/gc-common.h"
47 #include "mm/memory.h"
49 #if defined(ENABLE_THREADS)
50 # include "threads/native/threads.h"
53 #include "toolbox/logging.h"
54 #include "vm/classcache.h"
55 #include "vm/exceptions.h"
56 #include "vm/global.h"
57 #include "vm/hashtable.h"
58 #include "vm/loader.h"
59 #include "vm/options.h"
60 #include "vm/statistics.h"
61 #include "vm/stringlocal.h"
65 /* define heap sizes **********************************************************/
67 #define HEAP_MAXSIZE 2 * 1024 * 1024 /* default 2MB */
68 #define HEAP_STARTSIZE 100 * 1024 /* default 100kB */
71 /* define cacaoh options ******************************************************/
86 { "help", false, OPT_HELP },
87 { "version", false, OPT_VERSION },
88 { "verbose", false, OPT_VERBOSE },
89 { "d", true, OPT_DIRECTORY },
90 { "classpath", true, OPT_CLASSPATH },
91 { "bootclasspath", true, OPT_BOOTCLASSPATH },
96 /* usage ***********************************************************************
98 Obviously prints usage information of cacaoh.
100 *******************************************************************************/
102 static void usage(void)
104 printf("Usage: cacaoh [options] <classes>\n"
107 " -help Print this message\n"
108 " -classpath <path> \n"
109 " -bootclasspath <path> \n"
110 " -d <dir> Output directory\n"
111 " -version Print version information\n"
112 " -verbose Enable verbose output\n");
114 /* exit with error code */
120 /* version *********************************************************************
122 Prints cacaoh version information.
124 *******************************************************************************/
126 static void version(void)
128 printf("cacaoh version "VERSION"\n");
129 printf("Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,\n");
130 printf("C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,\n");
131 printf("E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,\n");
132 printf("J. Wenninger, Institut f. Computersprachen - TU Wien\n\n");
134 printf("This program is free software; you can redistribute it and/or\n");
135 printf("modify it under the terms of the GNU General Public License as\n");
136 printf("published by the Free Software Foundation; either version 2, or (at\n");
137 printf("your option) any later version.\n\n");
139 printf("This program is distributed in the hope that it will be useful, but\n");
140 printf("WITHOUT ANY WARRANTY; without even the implied warranty of\n");
141 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n");
142 printf("General Public License for more details.\n");
148 /* forward declarations *******************************************************/
150 static JavaVMInitArgs *cacaoh_options_prepare(int argc, char **argv);
151 static void cacaoh_abort(const char *text, ...);
154 /* main ************************************************************************
158 *******************************************************************************/
160 int main(int argc, char **argv)
162 JavaVMInitArgs *vm_args;
168 /********** internal (only used by main) *****************************/
180 /* set the bootclasspath */
182 cp = getenv("BOOTCLASSPATH");
185 bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
186 strcpy(bootclasspath, cp);
190 #if defined(WITH_CLASSPATH_GNU)
191 strlen(CACAO_VM_ZIP) +
194 strlen(CLASSPATH_CLASSES) +
197 bootclasspath = MNEW(char, cplen);
198 #if defined(WITH_CLASSPATH_GNU)
199 strcat(bootclasspath, CACAO_VM_ZIP);
200 strcat(bootclasspath, ":");
202 strcat(bootclasspath, CLASSPATH_CLASSES);
206 /* set the classpath */
208 cp = getenv("CLASSPATH");
211 classpath = MNEW(char, strlen(cp) + strlen("0"));
212 strcat(classpath, cp);
215 classpath = MNEW(char, strlen(".") + strlen("0"));
216 strcpy(classpath, ".");
220 /* initialize options with default values */
223 opt_directory = NULL;
225 heapmaxsize = HEAP_MAXSIZE;
226 heapstartsize = HEAP_STARTSIZE;
229 /* parse the options ******************************************************/
231 vm_args = cacaoh_options_prepare(argc, argv);
233 while ((opt = options_get(opts, vm_args)) != OPT_DONE) {
243 /* forget old classpath and set the argument as new classpath */
244 MFREE(classpath, char, strlen(classpath));
246 classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
247 strcpy(classpath, opt_arg);
250 case OPT_BOOTCLASSPATH:
251 /* Forget default bootclasspath and set the argument as
252 new boot classpath. */
253 MFREE(bootclasspath, char, strlen(bootclasspath));
255 bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
256 strcpy(bootclasspath, opt_arg);
260 opt_directory = MNEW(char, strlen(opt_arg) + strlen("0"));
261 strcpy(opt_directory, opt_arg);
279 /**************************** Program start **************************/
283 log_println("Java - header-generator started");
286 #if defined(ENABLE_THREADS)
287 /* pre-initialize some core thread stuff, like the stopworldlock,
288 thus this has to happen _before_ gc_init()!!! */
293 /* initialize the garbage collector */
295 gc_init(heapmaxsize, heapstartsize);
297 /* initialize the string hashtable stuff: lock (must be done
298 _after_ threads_preinit) */
301 cacaoh_abort("string_init failed\n");
303 /* initialize the utf8 hashtable stuff: lock, often used utf8 strings
304 (must be done _after_ threads_preinit) */
307 cacaoh_abort("utf8_init failed\n");
309 /* initialize the classcache hashtable stuff: lock, hashtable
310 (must be done _after_ threads_preinit) */
312 if (!classcache_init())
313 cacaoh_abort("classcache_init failed\n");
315 /* initialize the loader with bootclasspath (must be done _after_
319 cacaoh_abort("suck_init failed\n");
321 suck_add(bootclasspath);
323 /* Also add the normal classpath, so the bootstrap class loader
324 can find the files. */
328 /* initialize the loader subsystems (must be done _after_
332 cacaoh_abort("loader_init failed\n");
335 /* load Java classes ******************************************************/
337 for (i = opt_index; i < vm_args->nOptions; i++) {
338 cp = vm_args->options[i].optionString;
340 /* convert classname */
342 for (j = strlen(cp) - 1; j >= 0; j--) {
353 /* exceptions are catched with new_exception call */
355 if (!(c = load_class_bootstrap(utf_new_char(cp))))
356 cacaoh_abort("java.lang.NoClassDefFoundError: %s\n", cp);
359 cacaoh_abort("java.lang.LinkageError: %s\n", cp);
361 headerfile_generate(c, opt_directory);
364 /************************ Release all resources **********************/
369 log_println("Java - header-generator stopped");
370 #if defined(ENABLE_STATISTICS)
379 /* cacaoh_options_prepare ******************************************************
381 Prepare the JavaVMInitArgs.
383 *******************************************************************************/
385 static JavaVMInitArgs *cacaoh_options_prepare(int argc, char **argv)
387 JavaVMInitArgs *vm_args;
390 vm_args = NEW(JavaVMInitArgs);
392 vm_args->nOptions = argc - 1;
393 vm_args->options = MNEW(JavaVMOption, argc);
395 for (i = 1; i < argc; i++)
396 vm_args->options[i - 1].optionString = argv[i];
402 /* cacaoh_abort ****************************************************************
404 Prints an error message and aborts the VM.
406 *******************************************************************************/
408 static void cacaoh_abort(const char *text, ...)
412 /* print the log message */
417 log_vprint(text, ap);
422 /* now abort the VM */
429 * These are local overrides for various environment variables in Emacs.
430 * Please do not remove this and leave it at the end of the file, where
431 * Emacs will automagically detect them.
432 * ---------------------------------------------------------------------
435 * indent-tabs-mode: t