- call new init code
[cacao.git] / src / cacaoh / cacaoh.c
1 /* 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 1910 2005-02-10 09:55:49Z twisti $
34
35 */
36
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "config.h"
43 #include "types.h"
44 #include "cacaoh/headers.h"
45 #include "mm/boehm.h"
46 #include "mm/memory.h"
47 #include "native/include/java_lang_Throwable.h"
48
49 #if defined(USE_THREADS)
50 # if defined(NATIVE_THREADS)
51 #  include "threads/native/threads.h"
52 # else
53 #  include "threads/green/threads.h"
54 # endif
55 #endif
56
57 #include "toolbox/logging.h"
58 #include "vm/exceptions.h"
59 #include "vm/global.h"
60 #include "vm/loader.h"
61 #include "vm/options.h"
62 #include "vm/statistics.h"
63 #include "vm/stringlocal.h"
64 #include "vm/tables.h"
65
66
67 /* define heap sizes **********************************************************/
68
69 #define HEAP_MAXSIZE      2 * 1024 * 1024;  /* default 2MB                    */
70 #define HEAP_STARTSIZE    100 * 1024;       /* default 100kB                  */
71
72
73 /* define cacaoh options ******************************************************/
74
75 #define OPT_HELP          2
76 #define OPT_VERSION       3
77 #define OPT_VERBOSE       4
78 #define OPT_DIRECTORY     5
79 #define OPT_CLASSPATH     6
80 #define OPT_BOOTCLASSPATH 7
81
82 opt_struct opts[] = {
83         { "help",             false, OPT_HELP          },
84         { "version",          false, OPT_VERSION       },
85         { "verbose",          false, OPT_VERBOSE       },
86         { "d",                true,  OPT_DIRECTORY     },
87         { "classpath",        true,  OPT_CLASSPATH     },
88         { "bootclasspath",    true,  OPT_BOOTCLASSPATH },
89         { NULL,               false, 0                 }
90 };
91
92
93 /* usage ***********************************************************************
94
95    Obviously prints usage information of cacaoh.
96
97 *******************************************************************************/
98
99 static void usage()
100 {
101         printf("Usage: cacaoh [options] <classes>\n"
102                    "\n"
103                    "Options:\n"
104                    "    -help                 Print this message\n"
105                    "    -classpath <path>     \n"
106                    "    -bootclasspath <path> \n"
107                    "    -d <dir>              Output directory\n"
108                    "    -version              Print version information\n"
109                    "    -verbose              Enable verbose output\n");
110
111         /* exit with error code */
112
113         exit(1);
114 }
115
116
117 static void version()
118 {
119         printf("cacaoh "VERSION"\n");
120         exit(0);
121 }
122
123
124 /* main ************************************************************************
125
126    Main program.
127    
128 *******************************************************************************/
129
130 int main(int argc, char **argv)
131 {
132         s4 i, a;
133         classinfo *c;
134         char *opt_directory;
135         void *dummy;
136
137         /********** internal (only used by main) *****************************/
138    
139         char *bootclasspath;
140         char *classpath;
141         char *cp;
142         s4    cplen;
143         u4    heapmaxsize;
144         u4    heapstartsize;
145
146         if (argc < 2)
147                 usage();
148
149
150         /* set the bootclasspath */
151
152         cp = getenv("BOOTCLASSPATH");
153         if (cp) {
154                 bootclasspath = MNEW(char, strlen(cp) + 1);
155                 strcpy(bootclasspath, cp);
156
157         } else {
158                 cplen = strlen(CACAO_INSTALL_PREFIX) + strlen(CACAO_RT_JAR_PATH);
159
160                 bootclasspath = MNEW(char, cplen + 1);
161                 strcpy(bootclasspath, CACAO_INSTALL_PREFIX);
162                 strcat(bootclasspath, CACAO_RT_JAR_PATH);
163         }
164
165
166         /* set the classpath */
167
168         cp = getenv("CLASSPATH");
169         if (cp) {
170                 classpath = MNEW(char, strlen(cp) + 1);
171                 strcat(classpath, cp);
172
173         } else {
174                 classpath = MNEW(char, 2);
175                 strcpy(classpath, ".");
176         }
177
178
179         /* initialize options with default values */
180
181         opt_verbose = false;
182         opt_directory = NULL;
183
184         heapmaxsize = HEAP_MAXSIZE;
185         heapstartsize = HEAP_STARTSIZE;
186
187         while ((i = get_opt(argc, argv, opts)) != OPT_DONE) {
188                 switch (i) {
189                 case OPT_IGNORE:
190                         break;
191
192                 case OPT_HELP:
193                         usage();
194                         break;
195
196                 case OPT_CLASSPATH:
197                         /* forget old classpath and set the argument as new classpath */
198                         MFREE(classpath, char, strlen(classpath));
199
200                         classpath = MNEW(char, strlen(opt_arg) + 1);
201                         strcpy(classpath, opt_arg);
202                         break;
203
204                 case OPT_BOOTCLASSPATH:
205                         /* Forget default bootclasspath and set the argument as new boot  */
206                         /* classpath.                                                     */
207                         MFREE(bootclasspath, char, strlen(bootclasspath));
208
209                         bootclasspath = MNEW(char, strlen(opt_arg) + 1);
210                         strcpy(bootclasspath, opt_arg);
211                         break;
212
213                 case OPT_DIRECTORY:
214                         opt_directory = MNEW(char, strlen(opt_arg) + 1);
215                         strcpy(opt_directory, opt_arg);
216                         break;
217
218                 case OPT_VERSION:
219                         version();
220                         break;
221
222                 case OPT_VERBOSE:
223                         opt_verbose = true;
224                         break;
225
226                 default:
227                         usage();
228                 }
229         }
230                         
231         /**************************** Program start **************************/
232
233         if (opt_verbose) {
234                 log_init(NULL);
235                 log_text("Java - header-generator started"); 
236         }
237         
238         /* initialize the garbage collector */
239
240         gc_init(heapmaxsize, heapstartsize);
241
242         tables_init();
243         
244         /* initialize the loader with bootclasspath and append classpath entries */
245
246         suck_init(bootclasspath);
247         suck_init(classpath);
248    
249 #if defined(USE_THREADS)
250 #if defined(NATIVE_THREADS)
251         initThreadsEarly();
252 #endif
253         initLocks();
254 #endif
255
256         /* initialize some cacao subsystems */
257
258         utf8_init();
259         class_init_foo();
260         loader_init((u1 *) &dummy);
261
262
263         /*********************** Load JAVA classes  **************************/
264         
265         nativemethod_chain = chain_new();
266         nativeclass_chain = chain_new();
267         
268         for (a = opt_ind; a < argc; a++) {
269                 cp = argv[a];
270
271                 /* convert classname */
272
273                 for (i = strlen(cp) - 1; i >= 0; i--) {
274                         switch (cp[i]) {
275                         case '.': cp[i] = '/';
276                                 break;
277                         case '_': cp[i] = '$';
278                         }
279                 }
280         
281                 c = class_new(utf_new_char(cp));
282
283                 /* exceptions are catched with new_exception call */
284
285                 if (!class_load(c))
286                         throw_cacao_exception_exit(string_java_lang_NoClassDefFoundError,
287                                                                            cp);
288
289                 if (!class_link(c))
290                         throw_cacao_exception_exit(string_java_lang_LinkageError,
291                                                                            cp);
292
293                 headerfile_generate(c, opt_directory);
294         }
295
296         /************************ Release all resources **********************/
297
298         loader_close();
299         tables_close();
300
301         if (opt_verbose) {
302                 log_text("Java - header-generator stopped");
303                 log_cputime();
304                 mem_usagelog(true);
305         }
306         
307         return 0;
308 }
309
310
311 /*
312  * These are local overrides for various environment variables in Emacs.
313  * Please do not remove this and leave it at the end of the file, where
314  * Emacs will automagically detect them.
315  * ---------------------------------------------------------------------
316  * Local variables:
317  * mode: c
318  * indent-tabs-mode: t
319  * c-basic-offset: 4
320  * tab-width: 4
321  * End:
322  */