* OPT_VERBOSE: added load/linkverbose
[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 2879 2005-06-30 09:58:38Z 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(void)
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 /* version *********************************************************************
118
119    Prints cacaoh version information.
120
121 *******************************************************************************/
122
123 static void version(void)
124 {
125         printf("cacaoh version "VERSION"\n");
126         printf("Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,\n");
127         printf("R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,\n");
128         printf("C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,\n");
129         printf("Institut f. Computersprachen - TU Wien\n\n");
130
131         printf("This program is free software; you can redistribute it and/or\n");
132         printf("modify it under the terms of the GNU General Public License as\n");
133         printf("published by the Free Software Foundation; either version 2, or (at\n");
134         printf("your option) any later version.\n\n");
135
136         printf("This program is distributed in the hope that it will be useful, but\n");
137         printf("WITHOUT ANY WARRANTY; without even the implied warranty of\n");
138         printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n");
139         printf("General Public License for more details.\n");
140
141         exit(0);
142 }
143
144
145 /* main ************************************************************************
146
147    Main program.
148    
149 *******************************************************************************/
150
151 int main(int argc, char **argv)
152 {
153         s4 i, a;
154         classinfo *c;
155         char *opt_directory;
156         void *dummy;
157
158         /********** internal (only used by main) *****************************/
159    
160         char *bootclasspath;
161         char *classpath;
162         char *cp;
163         s4    cplen;
164         u4    heapmaxsize;
165         u4    heapstartsize;
166
167         if (argc < 2)
168                 usage();
169
170
171         /* set the bootclasspath */
172
173         cp = getenv("BOOTCLASSPATH");
174         if (cp) {
175                 bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
176                 strcpy(bootclasspath, cp);
177
178         } else {
179                 cplen = strlen(CACAO_INSTALL_PREFIX) + strlen(CACAO_RT_JAR_PATH) +
180                         strlen("0");
181
182                 bootclasspath = MNEW(char, cplen);
183                 strcpy(bootclasspath, CACAO_INSTALL_PREFIX);
184                 strcat(bootclasspath, CACAO_RT_JAR_PATH);
185         }
186
187
188         /* set the classpath */
189
190         cp = getenv("CLASSPATH");
191         if (cp) {
192                 classpath = MNEW(char, strlen(cp) + strlen("0"));
193                 strcat(classpath, cp);
194
195         } else {
196                 classpath = MNEW(char, strlen(".") + strlen("0"));
197                 strcpy(classpath, ".");
198         }
199
200
201         /* initialize options with default values */
202
203         opt_verbose = false;
204         opt_directory = NULL;
205
206         heapmaxsize = HEAP_MAXSIZE;
207         heapstartsize = HEAP_STARTSIZE;
208
209         while ((i = get_opt(argc, argv, opts)) != OPT_DONE) {
210                 switch (i) {
211                 case OPT_IGNORE:
212                         break;
213
214                 case OPT_HELP:
215                         usage();
216                         break;
217
218                 case OPT_CLASSPATH:
219                         /* forget old classpath and set the argument as new classpath */
220                         MFREE(classpath, char, strlen(classpath));
221
222                         classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
223                         strcpy(classpath, opt_arg);
224                         break;
225
226                 case OPT_BOOTCLASSPATH:
227                         /* Forget default bootclasspath and set the argument as new boot  */
228                         /* classpath.                                                     */
229                         MFREE(bootclasspath, char, strlen(bootclasspath));
230
231                         bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
232                         strcpy(bootclasspath, opt_arg);
233                         break;
234
235                 case OPT_DIRECTORY:
236                         opt_directory = MNEW(char, strlen(opt_arg) + strlen("0"));
237                         strcpy(opt_directory, opt_arg);
238                         break;
239
240                 case OPT_VERSION:
241                         version();
242                         break;
243
244                 case OPT_VERBOSE:
245                         opt_verbose = true;
246                         loadverbose = true;
247                         linkverbose = true;
248                         break;
249
250                 default:
251                         usage();
252                 }
253         }
254                         
255         /**************************** Program start **************************/
256
257         if (opt_verbose) {
258                 log_init(NULL);
259                 log_text("Java - header-generator started"); 
260         }
261         
262         /* initialize the garbage collector */
263
264         gc_init(heapmaxsize, heapstartsize);
265
266         tables_init();
267         
268         /* initialize the loader with bootclasspath */
269
270         suck_init(bootclasspath);
271
272         /* Also add the normal classpath, so the bootstrap class loader can find  */
273         /* the files.                                                             */
274
275         suck_init(classpath);
276    
277 #if defined(USE_THREADS)
278 #if defined(NATIVE_THREADS)
279         initThreadsEarly();
280 #endif
281         initLocks();
282 #endif
283
284         /* initialize some cacao subsystems */
285
286         utf8_init();
287         loader_init((u1 *) &dummy);
288
289
290         /*********************** Load JAVA classes  **************************/
291         
292         nativemethod_chain = chain_new();
293         nativeclass_chain = chain_new();
294         
295         for (a = opt_ind; a < argc; a++) {
296                 cp = argv[a];
297
298                 /* convert classname */
299
300                 for (i = strlen(cp) - 1; i >= 0; i--) {
301                         switch (cp[i]) {
302                         case '.': cp[i] = '/';
303                                 break;
304                         case '_': cp[i] = '$';
305                         }
306                 }
307         
308                 /* exceptions are catched with new_exception call */
309
310                 if (!(c = load_class_bootstrap(utf_new_char(cp))))
311                         throw_cacao_exception_exit(string_java_lang_NoClassDefFoundError,
312                                                                            cp);
313
314                 if (!link_class(c))
315                         throw_cacao_exception_exit(string_java_lang_LinkageError,
316                                                                            cp);
317
318                 headerfile_generate(c, opt_directory);
319         }
320
321         /************************ Release all resources **********************/
322
323         loader_close();
324         tables_close();
325
326         if (opt_verbose) {
327                 log_text("Java - header-generator stopped");
328 #if defined(STATISTICS)
329                 log_cputime();
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  */