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