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