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