2006-07-07 Andrew John Hughes <gnu_andrew@member.fsf.org>
[official-gcc.git] / libjava / gij.cc
blob933fe503df4bc9eeb3f38688ff98253c8ec9601c
1 /* Copyright (C) 1999-2006 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
9 #include <config.h>
11 #include <jvm.h>
12 #include <gcj/cni.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <unistd.h>
19 static void
20 help ()
22 printf ("Usage: gij [OPTION] ... CLASS [ARGS] ...\n");
23 printf (" to interpret Java bytecodes, or\n");
24 printf (" gij -jar [OPTION] ... JARFILE [ARGS] ...\n");
25 printf (" to execute a jar file\n\n");
26 printf (" --cp LIST set class path\n");
27 printf (" --classpath LIST set class path\n");
28 printf (" -DVAR=VAL define property VAR with value VAL\n");
29 printf (" -?, --help print this help, then exit\n");
30 printf (" -X print help on supported -X options, then exit\n");
31 printf (" --ms=NUMBER set initial heap size\n");
32 printf (" --mx=NUMBER set maximum heap size\n");
33 printf (" --verbose[:class] print information about class loading\n");
34 printf (" --showversion print version number, then keep going\n");
35 printf (" --version print version number, then exit\n");
36 printf ("\nOptions can be specified with `-' or `--'.\n");
37 printf ("\nSee http://gcc.gnu.org/java/ for information on reporting bugs\n");
38 exit (0);
41 static void
42 version ()
44 printf ("java version \"" JV_VERSION "\"\n");
45 printf ("gij (GNU libgcj) version %s\n\n", __VERSION__);
46 printf ("Copyright (C) 2006 Free Software Foundation, Inc.\n");
47 printf ("This is free software; see the source for copying conditions. There is NO\n");
48 printf ("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
51 static void
52 nonstandard_opts_help ()
54 printf (" -Xms<size> set initial heap size\n");
55 printf (" -Xmx<size> set maximum heap size\n");
56 printf (" -Xss<size> set thread stack size\n");
57 exit (0);
60 static void
61 add_option (JvVMInitArgs& vm_args, char const* option, void const* extra)
63 vm_args.options =
64 (JvVMOption*) JvRealloc (vm_args.options,
65 (vm_args.nOptions + 1) * sizeof (JvVMOption));
67 vm_args.options[vm_args.nOptions].optionString = const_cast<char*> (option);
68 vm_args.options[vm_args.nOptions].extraInfo = const_cast<void*> (extra);
69 ++vm_args.nOptions;
72 int
73 main (int argc, char const** argv)
75 // libjawt.so must be installed in GCJ's versioned directory and not
76 // the main library directory so that it doesn't override other
77 // libjawt.so implementations. Programs that use the AWT Native
78 // Interface contain a JNI library that links to libjawt.so. We do
79 // not want to require that users explicitly add GCJ's versioned
80 // directory to LD_LIBRARY_PATH when running such programs.
82 // Simply adding GCJ's versioned directory to the module load path
83 // does not solve this problem since libltdl searches its module
84 // load path only for object that it will dlopen; dependencies of
85 // these dynamically loaded objects are searched for in
86 // LD_LIBRARY_PATH.
88 // In addition, setting LD_LIBRARY_PATH from within the current
89 // process does not alter the dependency search path, since it is
90 // computed on startup. This behaviour makes sense since
91 // LD_LIBRARY_PATH is designed to allow users to override the path
92 // set by a program. This re-spawning trick makes it impossible to
93 // override, using LD_LIBRARY_PATH, the versioned directories
94 // searched by gij.
96 // Check if LD_LIBRARY_PATH is already prefixed with
97 // GCJ_VERSIONED_LIBDIR. If not, export LD_LIBRARY_PATH prefixed
98 // with GCJ_VERSIONED_LIBDIR and re-spawn gij.
99 char *libpath = getenv (LTDL_SHLIBPATH_VAR);
100 char *newpath = _Jv_PrependVersionedLibdir (libpath);
102 if (! libpath || strcmp (libpath, newpath))
104 setenv (LTDL_SHLIBPATH_VAR, newpath, 1);
105 JvFree (newpath);
107 int error_code = execvp (argv[0], (char* const*) argv);
109 fprintf (stderr, "error re-spawning gij with new "
110 LTDL_SHLIBPATH_VAR " value: %s\n", strerror (error_code));
112 return error_code;
114 JvFree (newpath);
116 JvVMInitArgs vm_args;
117 bool jar_mode = false;
119 vm_args.options = NULL;
120 vm_args.nOptions = 0;
121 vm_args.ignoreUnrecognized = true;
123 // Command-line options always override the CLASSPATH environment
124 // variable.
125 char *classpath = getenv("CLASSPATH");
127 if (classpath)
129 char* darg = (char*) JvMalloc (strlen (classpath)
130 + sizeof ("-Djava.class.path="));
131 sprintf (darg, "-Djava.class.path=%s", classpath);
132 add_option (vm_args, darg, NULL);
135 // Handle arguments to the java command. Store in vm_args arguments
136 // handled by the invocation API.
137 int i;
138 for (i = 1; i < argc; ++i)
140 char* arg = const_cast<char*> (argv[i]);
142 // A non-option stops processing.
143 if (arg[0] != '-')
144 break;
146 // A "--" stops processing.
147 if (! strcmp (arg, "--"))
149 ++i;
150 break;
153 // Allow both single or double hyphen for all options.
154 if (arg[1] == '-')
155 ++arg;
157 // Ignore JIT options
158 if (! strcmp (arg, "-client"))
159 continue;
160 else if (! strcmp (arg, "-server"))
161 continue;
162 else if (! strcmp (arg, "-hotspot"))
163 continue;
164 else if (! strcmp (arg, "-jrockit"))
165 continue;
166 // Ignore JVM Tool Interface options
167 else if (! strncmp (arg, "-agentlib:", sizeof ("-agentlib:") - 1))
168 continue;
169 else if (! strncmp (arg, "-agentpath:", sizeof ("-agentpath:") - 1))
170 continue;
171 else if (! strcmp (arg, "-classpath") || ! strcmp (arg, "-cp"))
173 if (i >= argc - 1)
175 no_arg:
176 fprintf (stderr, "gij: option requires an argument -- `%s'\n",
177 argv[i]);
178 fprintf (stderr, "Try `gij --help' for more information.\n");
179 exit (1);
182 // Sun seems to translate the -classpath option into
183 // -Djava.class.path because if both -classpath and
184 // -Djava.class.path are specified on the java command line,
185 // the last one always wins.
186 char* darg = (char*) JvMalloc (strlen (argv[++i])
187 + sizeof ("-Djava.class.path="));
188 sprintf (darg, "-Djava.class.path=%s", argv[i]);
189 add_option (vm_args, darg, NULL);
191 else if (! strcmp (arg, "-debug"))
193 char* xarg = strdup ("-Xdebug");
194 add_option (vm_args, xarg, NULL);
196 else if (! strncmp (arg, "-D", sizeof ("-D") - 1))
197 add_option (vm_args, arg, NULL);
198 // Ignore 32/64-bit JIT options
199 else if (! strcmp (arg, "-d32") || ! strcmp (arg, "-d64"))
200 continue;
201 else if (! strncmp (arg, "-enableassertions", sizeof ("-enableassertions") - 1)
202 || ! strncmp (arg, "-ea", sizeof ("-ea") - 1))
204 // FIXME: hook up assertion support
205 continue;
207 else if (! strncmp (arg, "-disableassertions", sizeof ("-disableassertions") - 1)
208 || ! strncmp (arg, "-da", sizeof ("-da") - 1))
210 // FIXME: hook up assertion support
211 continue;
213 else if (! strcmp (arg, "-enablesystemassertions")
214 || ! strcmp (arg, "-esa"))
216 // FIXME: hook up system assertion support
217 continue;
219 else if (! strcmp (arg, "-disablesystemassertions")
220 || ! strcmp (arg, "-dsa"))
222 // FIXME
223 continue;
225 else if (! strcmp (arg, "-jar"))
227 jar_mode = true;
228 continue;
230 // Ignore java.lang.instrument option
231 else if (! strncmp (arg, "-javaagent:", sizeof ("-javaagent:") - 1))
232 continue;
233 else if (! strcmp (arg, "-noclassgc"))
235 char* xarg = strdup ("-Xnoclassgc");
236 add_option (vm_args, xarg, NULL);
238 // -ms=n
239 else if (! strncmp (arg, "-ms=", sizeof ("-ms=") - 1))
241 arg[1] = 'X';
242 arg[2] = 'm';
243 arg[3] = 's';
244 add_option (vm_args, arg, NULL);
246 // -ms n
247 else if (! strcmp (arg, "-ms"))
249 if (i >= argc - 1)
250 goto no_arg;
252 char* xarg = (char*) JvMalloc (strlen (argv[++i])
253 + sizeof ("-Xms"));
254 sprintf (xarg, "-Xms%s", argv[i]);
255 add_option (vm_args, xarg, NULL);
257 // -msn
258 else if (! strncmp (arg, "-ms", sizeof ("-ms") - 1))
260 char* xarg = (char*) JvMalloc (strlen (arg) + sizeof ("X"));
261 sprintf (xarg, "-Xms%s", arg + sizeof ("-Xms") - 1);
262 add_option (vm_args, xarg, NULL);
264 // -mx=n
265 else if (! strncmp (arg, "-mx=", sizeof ("-mx=") - 1))
267 arg[1] = 'X';
268 arg[2] = 'm';
269 arg[3] = 'x';
270 add_option (vm_args, arg, NULL);
272 // -mx n
273 else if (! strcmp (arg, "-mx"))
275 if (i >= argc - 1)
276 goto no_arg;
278 char* xarg = (char*) JvMalloc (strlen (argv[++i])
279 + sizeof ("-Xmx"));
280 sprintf (xarg, "-Xmx%s", argv[i]);
281 add_option (vm_args, xarg, NULL);
283 // -mxn
284 else if (! strncmp (arg, "-mx", sizeof ("-mx") - 1))
286 char* xarg = (char*) JvMalloc (strlen (arg) + sizeof ("X"));
287 sprintf (xarg, "-Xmx%s", arg + sizeof ("-Xmx") - 1);
288 add_option (vm_args, xarg, NULL);
290 // -ss=n
291 else if (! strncmp (arg, "-ss=", sizeof ("-ss=") - 1))
293 arg[1] = 'X';
294 arg[2] = 's';
295 arg[3] = 's';
296 add_option (vm_args, arg, NULL);
298 // -ss n
299 else if (! strcmp (arg, "-ss"))
301 if (i >= argc - 1)
302 goto no_arg;
304 char* xarg = (char*) JvMalloc (strlen (argv[++i])
305 + sizeof ("-Xss"));
306 sprintf (xarg, "-Xss%s", argv[i]);
307 add_option (vm_args, xarg, NULL);
309 // -ssn
310 else if (! strncmp (arg, "-ss", sizeof ("-ss") - 1))
312 char* xarg = (char*) JvMalloc (strlen (arg) + sizeof ("X"));
313 sprintf (xarg, "-Xss%s", arg + sizeof ("-Xss") - 1);
314 add_option (vm_args, xarg, NULL);
316 // This handles all the option variants that begin with
317 // -verbose.
318 else if (! strncmp (arg, "-verbose", 8))
319 add_option (vm_args, arg, NULL);
320 else if (! strcmp (arg, "-version"))
322 version ();
323 exit (0);
325 else if (! strcmp (arg, "-fullversion"))
327 printf ("java full version \"gcj-" JV_VERSION "\"\n");
328 exit (0);
330 else if (! strcmp (arg, "-showversion"))
331 version ();
332 else if (! strcmp (arg, "-help") || ! strcmp (arg, "-?"))
333 help ();
334 else if (! strcmp (arg, "-X"))
335 nonstandard_opts_help ();
336 else if (! strncmp (arg, "-X", 2))
337 add_option (vm_args, arg, NULL);
338 // Obsolete options recognized for backwards-compatibility.
339 else if (! strcmp (arg, "-verify")
340 || ! strcmp (arg, "-verifyremote"))
341 continue;
342 else if (! strcmp (arg, "-noverify"))
344 gcj::verifyClasses = false;
346 else
348 fprintf (stderr, "gij: unrecognized option -- `%s'\n", argv[i]);
349 fprintf (stderr, "Try `gij --help' for more information.\n");
350 exit (1);
354 if (argc - i < 1)
356 fprintf (stderr, "Usage: gij [OPTION] ... CLASS [ARGS] ...\n");
357 fprintf (stderr, " to invoke CLASS.main, or\n");
358 fprintf (stderr, " gij -jar [OPTION] ... JARFILE [ARGS] ...\n");
359 fprintf (stderr, " to execute a jar file\n");
360 fprintf (stderr, "Try `gij --help' for more information.\n");
361 exit (1);
364 // -jar mode overrides all other modes of specifying class path:
365 // CLASSPATH, -Djava.class.path, -classpath and -cp.
366 if (jar_mode)
368 char* darg = (char*) JvMalloc (strlen (argv[i])
369 + sizeof ("-Djava.class.path="));
370 sprintf (darg, "-Djava.class.path=%s", argv[i]);
371 add_option (vm_args, darg, NULL);
374 _Jv_RunMain (&vm_args, NULL, argv[i], argc - i,
375 (char const**) (argv + i), jar_mode);