* STATISTICS: Renamed to ENABLE_STATISTICS.
[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 3957 2005-12-20 23:09:36Z 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 R. Grafl, A. Krall, C. Kruegel, C. Oates,\n");
136         printf("R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,\n");
137         printf("C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,\n");
138         printf("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         /* set the bootclasspath */
181
182         cp = getenv("BOOTCLASSPATH");
183         if (cp) {
184                 bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
185                 strcpy(bootclasspath, cp);
186
187         } else {
188                 cplen = strlen(CACAO_INSTALL_PREFIX) +
189                         strlen(CACAO_VM_ZIP_PATH) +
190                         strlen(":") +
191                         strlen(CLASSPATH_INSTALL_DIR) +
192                         strlen(CLASSPATH_GLIBJ_ZIP_PATH) +
193                         strlen("0");
194
195                 bootclasspath = MNEW(char, cplen);
196                 strcpy(bootclasspath, CACAO_INSTALL_PREFIX);
197                 strcat(bootclasspath, CACAO_VM_ZIP_PATH);
198                 strcat(bootclasspath, ":");
199                 strcat(bootclasspath, CLASSPATH_INSTALL_DIR);
200                 strcat(bootclasspath, CLASSPATH_GLIBJ_ZIP_PATH);
201         }
202
203
204         /* set the classpath */
205
206         cp = getenv("CLASSPATH");
207         if (cp) {
208                 classpath = MNEW(char, strlen(cp) + strlen("0"));
209                 strcat(classpath, cp);
210
211         } else {
212                 classpath = MNEW(char, strlen(".") + strlen("0"));
213                 strcpy(classpath, ".");
214         }
215
216
217         /* initialize options with default values */
218
219         opt_verbose = false;
220         opt_directory = NULL;
221
222         heapmaxsize = HEAP_MAXSIZE;
223         heapstartsize = HEAP_STARTSIZE;
224
225         while ((i = get_opt(argc, argv, opts)) != OPT_DONE) {
226                 switch (i) {
227                 case OPT_IGNORE:
228                         break;
229
230                 case OPT_HELP:
231                         usage();
232                         break;
233
234                 case OPT_CLASSPATH:
235                         /* forget old classpath and set the argument as new classpath */
236                         MFREE(classpath, char, strlen(classpath));
237
238                         classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
239                         strcpy(classpath, opt_arg);
240                         break;
241
242                 case OPT_BOOTCLASSPATH:
243                         /* Forget default bootclasspath and set the argument as new boot  */
244                         /* classpath.                                                     */
245                         MFREE(bootclasspath, char, strlen(bootclasspath));
246
247                         bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
248                         strcpy(bootclasspath, opt_arg);
249                         break;
250
251                 case OPT_DIRECTORY:
252                         opt_directory = MNEW(char, strlen(opt_arg) + strlen("0"));
253                         strcpy(opt_directory, opt_arg);
254                         break;
255
256                 case OPT_VERSION:
257                         version();
258                         break;
259
260                 case OPT_VERBOSE:
261                         opt_verbose = true;
262                         loadverbose = true;
263                         linkverbose = true;
264                         break;
265
266                 default:
267                         usage();
268                 }
269         }
270                         
271         /**************************** Program start **************************/
272
273         if (opt_verbose) {
274                 log_init(NULL);
275                 log_text("Java - header-generator started"); 
276         }
277         
278         /* initialize the garbage collector */
279
280         gc_init(heapmaxsize, heapstartsize);
281
282 #if defined(USE_THREADS)
283 #if defined(NATIVE_THREADS)
284         threads_preinit();
285 #endif
286         initLocks();
287 #endif
288
289         /* initialize the utf8 hashtable stuff: lock, often used utf8 strings
290            (must be done _after_ threads_preinit) */
291
292         if (!utf8_init())
293                 throw_main_exception_exit();
294
295         /* initialize the classcache hashtable stuff: lock, hashtable
296            (must be done _after_ threads_preinit) */
297
298         if (!classcache_init())
299                 throw_main_exception_exit();
300
301         /* initialize the loader with bootclasspath (must be done _after_
302            thread_preinit) */
303
304         suck_init();
305
306         suck_add(bootclasspath);
307
308         /* Also add the normal classpath, so the bootstrap class loader can find  */
309         /* the files.                                                             */
310
311         suck_add(classpath);
312
313         /* initialize the loader subsystems (must be done _after_
314        classcache_init) */
315
316         if (!loader_init((u1 *) &dummy))
317                 throw_main_exception_exit();
318
319
320         /*********************** Load JAVA classes  **************************/
321         
322         nativemethod_chain = chain_new();
323         nativeclass_chain = chain_new();
324         
325         for (a = opt_ind; a < argc; a++) {
326                 cp = argv[a];
327
328                 /* convert classname */
329
330                 for (i = strlen(cp) - 1; i >= 0; i--) {
331                         switch (cp[i]) {
332                         case '.': cp[i] = '/';
333                                 break;
334                         case '_': cp[i] = '$';
335                         }
336                 }
337         
338                 /* exceptions are catched with new_exception call */
339
340                 if (!(c = load_class_bootstrap(utf_new_char(cp))))
341                         throw_cacao_exception_exit(string_java_lang_NoClassDefFoundError,
342                                                                            cp);
343
344                 if (!link_class(c))
345                         throw_cacao_exception_exit(string_java_lang_LinkageError,
346                                                                            cp);
347
348                 headerfile_generate(c, opt_directory);
349         }
350
351         /************************ Release all resources **********************/
352
353         loader_close();
354
355         if (opt_verbose) {
356                 log_text("Java - header-generator stopped");
357 #if defined(ENABLE_STATISTICS)
358                 mem_usagelog(true);
359 #endif
360         }
361         
362         return 0;
363 }
364
365
366 /*
367  * These are local overrides for various environment variables in Emacs.
368  * Please do not remove this and leave it at the end of the file, where
369  * Emacs will automagically detect them.
370  * ---------------------------------------------------------------------
371  * Local variables:
372  * mode: c
373  * indent-tabs-mode: t
374  * c-basic-offset: 4
375  * tab-width: 4
376  * End:
377  */