- removed 2nd jni_init()
[cacao.git] / cacaoh.c
1 /* cacaoh.c - main for header generation (cacaoh)
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5    M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6    P. Tomsich, J. Wenninger
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 1534 2004-11-18 10:38:05Z 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 "global.h"
45 #include "headers.h"
46 #include "loader.h"
47 #include "options.h"
48 #include "tables.h"
49 #include "mm/boehm.h"
50 #include "threads/thread.h"
51 #include "toolbox/logging.h"
52 #include "toolbox/memory.h"
53
54
55 /* define cacaoh options ******************************************************/
56
57 #define OPT_HELP      2
58 #define OPT_VERSION   3
59 #define OPT_VERBOSE   4
60 #define OPT_DIRECTORY 5
61
62 opt_struct opts[] = {
63         { "help",             false, OPT_HELP      },
64         { "version",          false, OPT_VERSION   },
65         { "verbose",          false, OPT_VERBOSE   },
66         { "d",                true,  OPT_DIRECTORY },
67         { NULL,               false, 0 }
68 };
69
70
71 /* usage ***********************************************************************
72
73    Obviously prints usage information of cacaoh.
74
75 *******************************************************************************/
76
77 static void usage()
78 {
79         printf("Usage: cacaoh [options] <classes>\n"
80                    "\n"
81                    "Options:\n"
82                    "        -help           Print this message\n"
83                    "        -version        Print version information\n"
84                    "        -verbose        Enable verbose output\n"
85                    "        -d <dir>        Output directory\n");
86
87         /* exit with error code */
88
89         exit(1);
90 }
91
92
93 static void version()
94 {
95         printf("cacaoh "VERSION"\n");
96         exit(0);
97 }
98
99
100 /* main ************************************************************************
101
102    Main program.
103    
104 *******************************************************************************/
105
106 int main(int argc, char **argv)
107 {
108         s4 i, a;
109         char *cp;
110         classinfo *c;
111         bool opt_verbose;
112         char *opt_directory;
113
114         /********** internal (only used by main) *****************************/
115    
116         char classpath[500] = "";
117         u4 heapmaxsize = 2 * 1024 * 1024;
118         u4 heapstartsize = 100 * 1024;
119
120
121         /************ Collect some info from the environment *****************/
122
123         if (argc < 2)
124                 usage();
125
126         cp = getenv("CLASSPATH");
127         if (cp) {
128                 strcpy(classpath + strlen(classpath), ":");
129                 strcpy(classpath + strlen(classpath), cp);
130         }
131
132         /* initialize options with default values */
133
134         opt_verbose = false;
135         opt_directory = NULL;
136
137         while ((i = get_opt(argc, argv, opts)) != OPT_DONE) {
138                 switch (i) {
139                 case OPT_IGNORE:
140                         break;
141
142                 case OPT_HELP:
143                         usage();
144                         break;
145
146                 case OPT_VERSION:
147                         version();
148                         break;
149
150                 case OPT_VERBOSE:
151                         opt_verbose = true;
152                         break;
153
154                 case OPT_DIRECTORY:
155                         opt_directory = MNEW(char, strlen(opt_arg));
156                         strcpy(opt_directory, opt_arg);
157                         break;
158
159                 default:
160                         usage();
161                 }
162         }
163                         
164         /**************************** Program start **************************/
165
166         if (opt_verbose) {
167                 log_init(NULL);
168                 log_text("Java - header-generator started"); 
169         }
170         
171         /* initialize the garbage collector */
172         gc_init(heapmaxsize, heapstartsize);
173
174         tables_init();
175
176         suck_init(classpath);
177    
178 #if defined(USE_THREADS)
179 #if defined(NATIVE_THREADS)
180         initThreadsEarly();
181 #endif
182         initLocks();
183 #endif
184
185         loader_init();
186
187
188         /*********************** Load JAVA classes  **************************/
189         
190         nativemethod_chain = chain_new();
191         nativeclass_chain = chain_new();
192         
193         for (a = opt_ind; a < argc; a++) {
194                 cp = argv[a];
195
196                 /* convert classname */
197                 for (i = strlen(cp) - 1; i >= 0; i--) {
198                         switch (cp[i]) {
199                         case '.': cp[i]='/';
200                                 break;
201                         case '_': cp[i]='$';    
202                         }
203                 }
204         
205                 c = class_new(utf_new_char(cp));
206
207                 /* exceptions are catched with new_exception call */
208                 class_load(c);
209                 class_link(c);
210
211                 headerfile_generate(c, opt_directory);
212         }
213
214         /************************ Release all resources **********************/
215
216         loader_close();
217         tables_close(literalstring_free);
218
219         if (opt_verbose) {
220                 log_text("Java - header-generator stopped");
221                 log_cputime();
222                 mem_usagelog(1);
223         }
224         
225         return 0;
226 }
227
228
229 /*
230  * These are local overrides for various environment variables in Emacs.
231  * Please do not remove this and leave it at the end of the file, where
232  * Emacs will automagically detect them.
233  * ---------------------------------------------------------------------
234  * Local variables:
235  * mode: c
236  * indent-tabs-mode: t
237  * c-basic-offset: 4
238  * tab-width: 4
239  * End:
240  */