* Renamed vm/tables.h to vm/hashtable.h.
[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 3827 2005-12-01 19:38:20Z 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/hashtable.h"
63 #include "vm/loader.h"
64 #include "vm/options.h"
65 #include "vm/statistics.h"
66 #include "vm/stringlocal.h"
67 #include "vm/suck.h"
68
69
70 /* define heap sizes **********************************************************/
71
72 #define HEAP_MAXSIZE      2 * 1024 * 1024;  /* default 2MB                    */
73 #define HEAP_STARTSIZE    100 * 1024;       /* default 100kB                  */
74
75
76 /* define cacaoh options ******************************************************/
77
78 #define OPT_HELP          2
79 #define OPT_VERSION       3
80 #define OPT_VERBOSE       4
81 #define OPT_DIRECTORY     5
82 #define OPT_CLASSPATH     6
83 #define OPT_BOOTCLASSPATH 7
84
85 opt_struct opts[] = {
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 },
92         { NULL,               false, 0                 }
93 };
94
95
96 /* usage ***********************************************************************
97
98    Obviously prints usage information of cacaoh.
99
100 *******************************************************************************/
101
102 static void usage(void)
103 {
104         printf("Usage: cacaoh [options] <classes>\n"
105                    "\n"
106                    "Options:\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");
113
114         /* exit with error code */
115
116         exit(1);
117 }
118
119
120 /* version *********************************************************************
121
122    Prints cacaoh version information.
123
124 *******************************************************************************/
125
126 static void version(void)
127 {
128         printf("cacaoh version "VERSION"\n");
129         printf("Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,\n");
130         printf("R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,\n");
131         printf("C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,\n");
132         printf("Institut f. Computersprachen - TU Wien\n\n");
133
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");
138
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");
143
144         exit(0);
145 }
146
147
148 /* main ************************************************************************
149
150    Main program.
151    
152 *******************************************************************************/
153
154 int main(int argc, char **argv)
155 {
156         s4 i, a;
157         classinfo *c;
158         char *opt_directory;
159         void *dummy;
160
161         /********** internal (only used by main) *****************************/
162    
163         char *bootclasspath;
164         char *classpath;
165         char *cp;
166         s4    cplen;
167         u4    heapmaxsize;
168         u4    heapstartsize;
169
170         if (argc < 2)
171                 usage();
172
173
174         /* set the bootclasspath */
175
176         cp = getenv("BOOTCLASSPATH");
177         if (cp) {
178                 bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
179                 strcpy(bootclasspath, cp);
180
181         } else {
182                 cplen = strlen(CACAO_INSTALL_PREFIX) +
183                         strlen(CACAO_VM_ZIP_PATH) +
184                         strlen(":") +
185                         strlen(CLASSPATH_INSTALL_DIR) +
186                         strlen(CLASSPATH_GLIBJ_ZIP_PATH) +
187                         strlen("0");
188
189                 bootclasspath = MNEW(char, cplen);
190                 strcpy(bootclasspath, CACAO_INSTALL_PREFIX);
191                 strcat(bootclasspath, CACAO_VM_ZIP_PATH);
192                 strcat(bootclasspath, ":");
193                 strcat(bootclasspath, CLASSPATH_INSTALL_DIR);
194                 strcat(bootclasspath, CLASSPATH_GLIBJ_ZIP_PATH);
195         }
196
197
198         /* set the classpath */
199
200         cp = getenv("CLASSPATH");
201         if (cp) {
202                 classpath = MNEW(char, strlen(cp) + strlen("0"));
203                 strcat(classpath, cp);
204
205         } else {
206                 classpath = MNEW(char, strlen(".") + strlen("0"));
207                 strcpy(classpath, ".");
208         }
209
210
211         /* initialize options with default values */
212
213         opt_verbose = false;
214         opt_directory = NULL;
215
216         heapmaxsize = HEAP_MAXSIZE;
217         heapstartsize = HEAP_STARTSIZE;
218
219         while ((i = get_opt(argc, argv, opts)) != OPT_DONE) {
220                 switch (i) {
221                 case OPT_IGNORE:
222                         break;
223
224                 case OPT_HELP:
225                         usage();
226                         break;
227
228                 case OPT_CLASSPATH:
229                         /* forget old classpath and set the argument as new classpath */
230                         MFREE(classpath, char, strlen(classpath));
231
232                         classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
233                         strcpy(classpath, opt_arg);
234                         break;
235
236                 case OPT_BOOTCLASSPATH:
237                         /* Forget default bootclasspath and set the argument as new boot  */
238                         /* classpath.                                                     */
239                         MFREE(bootclasspath, char, strlen(bootclasspath));
240
241                         bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
242                         strcpy(bootclasspath, opt_arg);
243                         break;
244
245                 case OPT_DIRECTORY:
246                         opt_directory = MNEW(char, strlen(opt_arg) + strlen("0"));
247                         strcpy(opt_directory, opt_arg);
248                         break;
249
250                 case OPT_VERSION:
251                         version();
252                         break;
253
254                 case OPT_VERBOSE:
255                         opt_verbose = true;
256                         loadverbose = true;
257                         linkverbose = true;
258                         break;
259
260                 default:
261                         usage();
262                 }
263         }
264                         
265         /**************************** Program start **************************/
266
267         if (opt_verbose) {
268                 log_init(NULL);
269                 log_text("Java - header-generator started"); 
270         }
271         
272         /* initialize the garbage collector */
273
274         gc_init(heapmaxsize, heapstartsize);
275
276 #if defined(USE_THREADS)
277 #if defined(NATIVE_THREADS)
278         threads_preinit();
279 #endif
280         initLocks();
281 #endif
282
283         /* initialize the utf8 hashtable stuff: lock, often used utf8 strings
284            (must be done _after_ threads_preinit) */
285
286         if (!utf8_init())
287                 throw_main_exception_exit();
288
289         /* initialize the classcache hashtable stuff: lock, hashtable
290            (must be done _after_ threads_preinit) */
291
292         if (!classcache_init())
293                 throw_main_exception_exit();
294
295         /* initialize the loader with bootclasspath (must be done _after_
296            thread_preinit) */
297
298         suck_init(bootclasspath);
299
300         /* Also add the normal classpath, so the bootstrap class loader can find  */
301         /* the files.                                                             */
302
303         suck_init(classpath);
304
305         /* initialize the loader subsystems (must be done _after_ classcache_init) */
306
307         if (!loader_init((u1 *) &dummy))
308                 throw_main_exception_exit();
309
310
311         /*********************** Load JAVA classes  **************************/
312         
313         nativemethod_chain = chain_new();
314         nativeclass_chain = chain_new();
315         
316         for (a = opt_ind; a < argc; a++) {
317                 cp = argv[a];
318
319                 /* convert classname */
320
321                 for (i = strlen(cp) - 1; i >= 0; i--) {
322                         switch (cp[i]) {
323                         case '.': cp[i] = '/';
324                                 break;
325                         case '_': cp[i] = '$';
326                         }
327                 }
328         
329                 /* exceptions are catched with new_exception call */
330
331                 if (!(c = load_class_bootstrap(utf_new_char(cp))))
332                         throw_cacao_exception_exit(string_java_lang_NoClassDefFoundError,
333                                                                            cp);
334
335                 if (!link_class(c))
336                         throw_cacao_exception_exit(string_java_lang_LinkageError,
337                                                                            cp);
338
339                 headerfile_generate(c, opt_directory);
340         }
341
342         /************************ Release all resources **********************/
343
344         loader_close();
345
346         if (opt_verbose) {
347                 log_text("Java - header-generator stopped");
348 #if defined(STATISTICS)
349                 mem_usagelog(true);
350 #endif
351         }
352         
353         return 0;
354 }
355
356
357 /*
358  * These are local overrides for various environment variables in Emacs.
359  * Please do not remove this and leave it at the end of the file, where
360  * Emacs will automagically detect them.
361  * ---------------------------------------------------------------------
362  * Local variables:
363  * mode: c
364  * indent-tabs-mode: t
365  * c-basic-offset: 4
366  * tab-width: 4
367  * End:
368  */