* src/threads/native/threads.c: The Big Thread Cleanup. No functional changes,
[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 4908 2006-05-12 16:49:50Z edwin $
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         JavaVMInitArgs *vm_args;
163         s4 i, j;
164         s4 opt;
165         classinfo *c;
166         char *opt_directory;
167         void *dummy;
168
169         /********** internal (only used by main) *****************************/
170    
171         char *bootclasspath;
172         char *classpath;
173         char *cp;
174         s4    cplen;
175         u4    heapmaxsize;
176         u4    heapstartsize;
177
178         if (argc < 2)
179                 usage();
180
181
182 #if defined(DISABLE_GC)
183         nogc_init(HEAP_MAXSIZE, HEAP_STARTSIZE);
184 #endif
185
186         /* set the bootclasspath */
187
188         cp = getenv("BOOTCLASSPATH");
189
190         if (cp) {
191                 bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
192                 strcpy(bootclasspath, cp);
193
194         } else {
195                 cplen = strlen(CACAO_VM_ZIP_PATH) +
196                         strlen(":") +
197                         strlen(CLASSPATH_GLIBJ_ZIP_PATH) +
198                         strlen("0");
199
200                 bootclasspath = MNEW(char, cplen);
201                 strcat(bootclasspath, CACAO_VM_ZIP_PATH);
202                 strcat(bootclasspath, ":");
203                 strcat(bootclasspath, CLASSPATH_GLIBJ_ZIP_PATH);
204         }
205
206
207         /* set the classpath */
208
209         cp = getenv("CLASSPATH");
210
211         if (cp) {
212                 classpath = MNEW(char, strlen(cp) + strlen("0"));
213                 strcat(classpath, cp);
214
215         } else {
216                 classpath = MNEW(char, strlen(".") + strlen("0"));
217                 strcpy(classpath, ".");
218         }
219
220
221         /* initialize options with default values */
222
223         opt_verbose = false;
224         opt_directory = NULL;
225
226         heapmaxsize = HEAP_MAXSIZE;
227         heapstartsize = HEAP_STARTSIZE;
228
229
230         /* parse the options ******************************************************/
231
232         vm_args = options_prepare(argc, argv);
233
234         while ((opt = options_get(opts, vm_args)) != OPT_DONE) {
235                 switch (opt) {
236                 case OPT_IGNORE:
237                         break;
238
239                 case OPT_HELP:
240                         usage();
241                         break;
242
243                 case OPT_CLASSPATH:
244                         /* forget old classpath and set the argument as new classpath */
245                         MFREE(classpath, char, strlen(classpath));
246
247                         classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
248                         strcpy(classpath, opt_arg);
249                         break;
250
251                 case OPT_BOOTCLASSPATH:
252                         /* Forget default bootclasspath and set the argument as
253                            new boot classpath. */
254                         MFREE(bootclasspath, char, strlen(bootclasspath));
255
256                         bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
257                         strcpy(bootclasspath, opt_arg);
258                         break;
259
260                 case OPT_DIRECTORY:
261                         opt_directory = MNEW(char, strlen(opt_arg) + strlen("0"));
262                         strcpy(opt_directory, opt_arg);
263                         break;
264
265                 case OPT_VERSION:
266                         version();
267                         break;
268
269                 case OPT_VERBOSE:
270                         opt_verbose = true;
271                         loadverbose = true;
272                         linkverbose = true;
273                         break;
274
275                 default:
276                         usage();
277                 }
278         }
279                         
280         /**************************** Program start **************************/
281
282         if (opt_verbose) {
283                 log_init(NULL);
284                 log_text("Java - header-generator started"); 
285         }
286         
287         /* initialize the garbage collector */
288
289         gc_init(heapmaxsize, heapstartsize);
290
291 #if defined(USE_THREADS)
292 #if defined(NATIVE_THREADS)
293         threads_preinit();
294 #endif
295         lock_init();
296 #endif
297
298         /* initialize the string hashtable stuff: lock (must be done
299            _after_ threads_preinit) */
300
301         if (!string_init())
302                 throw_main_exception_exit();
303
304         /* initialize the utf8 hashtable stuff: lock, often used utf8 strings
305            (must be done _after_ threads_preinit) */
306
307         if (!utf8_init())
308                 throw_main_exception_exit();
309
310         /* initialize the classcache hashtable stuff: lock, hashtable
311            (must be done _after_ threads_preinit) */
312
313         if (!classcache_init())
314                 throw_main_exception_exit();
315
316         /* initialize the loader with bootclasspath (must be done _after_
317            thread_preinit) */
318
319         if (!suck_init())
320                 throw_main_exception_exit();
321
322         suck_add(bootclasspath);
323
324         /* Also add the normal classpath, so the bootstrap class loader
325            can find the files. */
326
327         suck_add(classpath);
328
329         /* initialize the loader subsystems (must be done _after_
330        classcache_init) */
331
332         if (!loader_init((u1 *) &dummy))
333                 throw_main_exception_exit();
334
335
336         /* load Java classes ******************************************************/
337         
338         for (i = opt_index; i < vm_args->nOptions; i++) {
339                 cp = vm_args->options[i].optionString;
340
341                 /* convert classname */
342
343                 for (j = strlen(cp) - 1; j >= 0; j--) {
344                         switch (cp[j]) {
345                         case '.':
346                                 cp[j] = '/';
347                                 break;
348                         case '_':
349                                 cp[j] = '$';
350                                 break;
351                         }
352                 }
353         
354                 /* exceptions are catched with new_exception call */
355
356                 if (!(c = load_class_bootstrap(utf_new_char(cp))))
357                         throw_cacao_exception_exit(string_java_lang_NoClassDefFoundError,
358                                                                            cp);
359
360                 if (!link_class(c))
361                         throw_cacao_exception_exit(string_java_lang_LinkageError,
362                                                                            cp);
363
364                 headerfile_generate(c, opt_directory);
365         }
366
367         /************************ Release all resources **********************/
368
369         loader_close();
370
371         if (opt_verbose) {
372                 log_text("Java - header-generator stopped");
373 #if defined(ENABLE_STATISTICS)
374                 mem_usagelog(true);
375 #endif
376         }
377         
378         return 0;
379 }
380
381
382 /*
383  * These are local overrides for various environment variables in Emacs.
384  * Please do not remove this and leave it at the end of the file, where
385  * Emacs will automagically detect them.
386  * ---------------------------------------------------------------------
387  * Local variables:
388  * mode: c
389  * indent-tabs-mode: t
390  * c-basic-offset: 4
391  * tab-width: 4
392  * End:
393  */