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