* configure.ac (--with-jre-layout): Added.
[cacao.git] / src / cacao / cacao.c
1 /* src/cacao/cacao.c - contains main() of cacao
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Reinhard Grafl
28
29    Changes: Andi Krall
30             Mark Probst
31             Philipp Tomsich
32             Christian Thalinger
33
34    $Id: cacao.c 5658 2006-10-04 10:10:01Z twisti $
35
36 */
37
38
39 #include "config.h"
40
41 #include <assert.h>
42
43 #if defined(ENABLE_LIBJVM)
44 # include <ltdl.h>
45 #endif
46
47 #if defined(WITH_JRE_LAYOUT)
48 # include <errno.h>
49 # include <libgen.h>
50 # include <unistd.h>
51 #endif
52
53 #include <stdlib.h>
54 #include <string.h>
55
56 #include "vm/types.h"
57
58 #include "native/jni.h"
59
60 #if defined(ENABLE_JVMTI)
61 # include "native/jvmti/jvmti.h"
62 # include "native/jvmti/cacaodbg.h"
63 #endif
64
65 #include "vm/vm.h"
66
67
68 /* forward declarations *******************************************************/
69
70 static JavaVMInitArgs *cacao_options_prepare(int argc, char **argv);
71
72
73 /* main ************************************************************************
74
75    The main program.
76    
77 *******************************************************************************/
78
79 int main(int argc, char **argv)
80 {
81         char           *path;
82
83 #if defined(ENABLE_LIBJVM)      
84         /* Variables for JNI_CreateJavaVM dlopen call. */
85         lt_dlhandle     libjvm_handle;
86         lt_ptr          libjvm_createvm;
87         lt_ptr          libjvm_vm_run;
88
89         s4 (*JNI_CreateJavaVM)(JavaVM **, void **, void *);
90         void (*vm_run)(JavaVM *, JavaVMInitArgs *);
91 #endif
92
93         JavaVM         *vm;                 /* denotes a Java VM                  */
94         JNIEnv         *env;
95         JavaVMInitArgs *vm_args;
96
97         /* prepare the options */
98
99         vm_args = cacao_options_prepare(argc, argv);
100         
101         /* load and initialize a Java VM, return a JNI interface pointer in env */
102
103 #if defined(ENABLE_LIBJVM)
104 # if defined(WITH_JRE_LAYOUT)
105         /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
106
107         path = malloc(sizeof(char) * 4096);
108
109         if (readlink("/proc/self/exe", path, 4095) == -1) {
110                 fprintf(stderr, "readlink failed: %s\n", strerror(errno));
111                 abort();
112         }
113
114         /* get the path of the current executable */
115
116         path = dirname(path);
117
118         if ((strlen(path) + strlen("/../lib/libjvm") + strlen("0")) > 4096) {
119                 fprintf(stderr, "libjvm name to long for buffer\n");
120                 abort();
121         }
122
123         /* concatinate the library name */
124
125         strcat(path, "/../lib/libjvm");
126 # else
127         path = CACAO_LIBDIR"/libjvm";
128 # endif
129
130         if (lt_dlinit()) {
131                 fprintf(stderr, "lt_dlinit failed: %s\n", lt_dlerror());
132                 abort();
133         }
134
135         if (!(libjvm_handle = lt_dlopenext(path))) {
136                 fprintf(stderr, "lt_dlopenext failed: %s\n", lt_dlerror());
137                 abort();
138         }
139
140         if (!(libjvm_createvm = lt_dlsym(libjvm_handle, "JNI_CreateJavaVM"))) {
141                 fprintf(stderr, "lt_dlsym failed: %s\n", lt_dlerror());
142                 abort();
143         }
144
145         JNI_CreateJavaVM =
146                 (s4 (*)(JavaVM **, void **, void *)) (ptrint) libjvm_createvm;
147 #endif
148
149         /* create the Java VM */
150
151         JNI_CreateJavaVM(&vm, (void *) &env, vm_args);
152
153 #if defined(ENABLE_JVMTI)
154         pthread_mutex_init(&dbgcomlock,NULL);
155         if (jvmti) jvmti_set_phase(JVMTI_PHASE_START);
156 #endif
157
158 #if defined(ENABLE_LIBJVM)
159         if (!(libjvm_vm_run = lt_dlsym(libjvm_handle, "vm_run"))) {
160                 fprintf(stderr, "lt_dlsym failed: %s\n", lt_dlerror());
161                 abort();
162         }
163
164         vm_run = (void (*)(JavaVM *, JavaVMInitArgs *)) (ptrint) libjvm_vm_run;
165 #endif
166
167         /* run the VM */
168
169         vm_run(vm, vm_args);
170
171         /* keep compiler happy */
172
173         return 0;
174 }
175
176
177 /* cacao_options_prepare *******************************************************
178
179    Prepare the JavaVMInitArgs.
180
181 *******************************************************************************/
182
183 static JavaVMInitArgs *cacao_options_prepare(int argc, char **argv)
184 {
185         JavaVMInitArgs *vm_args;
186         s4              i;
187
188         vm_args = malloc(sizeof(JavaVMInitArgs));
189
190         vm_args->version            = JNI_VERSION_1_2;
191         vm_args->nOptions           = argc - 1;
192         vm_args->options            = malloc(sizeof(JavaVMOption) * argc);
193         vm_args->ignoreUnrecognized = JNI_FALSE;
194
195         for (i = 1; i < argc; i++)
196                 vm_args->options[i - 1].optionString = argv[i];
197
198         return vm_args;
199 }
200
201
202 /*
203  * These are local overrides for various environment variables in Emacs.
204  * Please do not remove this and leave it at the end of the file, where
205  * Emacs will automagically detect them.
206  * ---------------------------------------------------------------------
207  * Local variables:
208  * mode: c
209  * indent-tabs-mode: t
210  * c-basic-offset: 4
211  * tab-width: 4
212  * End:
213  */