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