2006-06-06 Paolo Carlini <pcarlini@suse.de>
[official-gcc.git] / libjava / gij.cc
blob817378f8bca1a6a5ea79e79ff95cbc9f19ddca98
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 char *buffer = (char *) JvMalloc (strlen (LTDL_SHLIBPATH_VAR)
105 + strlen (newpath) + 2);
106 strcpy (buffer, LTDL_SHLIBPATH_VAR);
107 strcat (buffer, "=");
108 strcat (buffer, newpath);
109 putenv (buffer);
110 JvFree (newpath);
112 int error_code = execvp (argv[0], (char* const*) argv);
114 fprintf (stderr, "error re-spawning gij with new "
115 LTDL_SHLIBPATH_VAR " value: %s\n", strerror (error_code));
117 return error_code;
119 JvFree (newpath);
121 JvVMInitArgs vm_args;
122 bool jar_mode = false;
124 vm_args.options = NULL;
125 vm_args.nOptions = 0;
126 vm_args.ignoreUnrecognized = true;
128 // Command-line options always override the CLASSPATH environment
129 // variable.
130 char *classpath = getenv("CLASSPATH");
132 if (classpath)
134 char* darg = (char*) JvMalloc (strlen (classpath)
135 + sizeof ("-Djava.class.path="));
136 sprintf (darg, "-Djava.class.path=%s", classpath);
137 add_option (vm_args, darg, NULL);
140 // Handle arguments to the java command. Store in vm_args arguments
141 // handled by the invocation API.
142 int i;
143 for (i = 1; i < argc; ++i)
145 char* arg = const_cast<char*> (argv[i]);
147 // A non-option stops processing.
148 if (arg[0] != '-')
149 break;
151 // A "--" stops processing.
152 if (! strcmp (arg, "--"))
154 ++i;
155 break;
158 // Allow both single or double hyphen for all options.
159 if (arg[1] == '-')
160 ++arg;
162 // Ignore JIT options
163 if (! strcmp (arg, "-client"))
164 continue;
165 else if (! strcmp (arg, "-server"))
166 continue;
167 else if (! strcmp (arg, "-hotspot"))
168 continue;
169 else if (! strcmp (arg, "-jrockit"))
170 continue;
171 // Ignore JVM Tool Interface options
172 else if (! strncmp (arg, "-agentlib:", sizeof ("-agentlib:") - 1))
173 continue;
174 else if (! strncmp (arg, "-agentpath:", sizeof ("-agentpath:") - 1))
175 continue;
176 else if (! strcmp (arg, "-classpath") || ! strcmp (arg, "-cp"))
178 if (i >= argc - 1)
180 no_arg:
181 fprintf (stderr, "gij: option requires an argument -- `%s'\n",
182 argv[i]);
183 fprintf (stderr, "Try `gij --help' for more information.\n");
184 exit (1);
187 // Sun seems to translate the -classpath option into
188 // -Djava.class.path because if both -classpath and
189 // -Djava.class.path are specified on the java command line,
190 // the last one always wins.
191 char* darg = (char*) JvMalloc (strlen (argv[++i])
192 + sizeof ("-Djava.class.path="));
193 sprintf (darg, "-Djava.class.path=%s", argv[i]);
194 add_option (vm_args, darg, NULL);
196 else if (! strcmp (arg, "-debug"))
198 char* xarg = strdup ("-Xdebug");
199 add_option (vm_args, xarg, NULL);
201 else if (! strncmp (arg, "-D", sizeof ("-D") - 1))
202 add_option (vm_args, arg, NULL);
203 // Ignore 32/64-bit JIT options
204 else if (! strcmp (arg, "-d32") || ! strcmp (arg, "-d64"))
205 continue;
206 else if (! strncmp (arg, "-enableassertions", sizeof ("-enableassertions") - 1)
207 || ! strncmp (arg, "-ea", sizeof ("-ea") - 1))
209 // FIXME: hook up assertion support
210 continue;
212 else if (! strncmp (arg, "-disableassertions", sizeof ("-disableassertions") - 1)
213 || ! strncmp (arg, "-da", sizeof ("-da") - 1))
215 // FIXME: hook up assertion support
216 continue;
218 else if (! strcmp (arg, "-enablesystemassertions")
219 || ! strcmp (arg, "-esa"))
221 // FIXME: hook up system assertion support
222 continue;
224 else if (! strcmp (arg, "-disablesystemassertions")
225 || ! strcmp (arg, "-dsa"))
227 // FIXME
228 continue;
230 else if (! strcmp (arg, "-jar"))
232 jar_mode = true;
233 continue;
235 // Ignore java.lang.instrument option
236 else if (! strncmp (arg, "-javaagent:", sizeof ("-javaagent:") - 1))
237 continue;
238 else if (! strcmp (arg, "-noclassgc"))
240 char* xarg = strdup ("-Xnoclassgc");
241 add_option (vm_args, xarg, NULL);
243 // -ms=n
244 else if (! strncmp (arg, "-ms=", sizeof ("-ms=") - 1))
246 arg[1] = 'X';
247 arg[2] = 'm';
248 arg[3] = 's';
249 add_option (vm_args, arg, NULL);
251 // -ms n
252 else if (! strcmp (arg, "-ms"))
254 if (i >= argc - 1)
255 goto no_arg;
257 char* xarg = (char*) JvMalloc (strlen (argv[++i])
258 + sizeof ("-Xms"));
259 sprintf (xarg, "-Xms%s", argv[i]);
260 add_option (vm_args, xarg, NULL);
262 // -msn
263 else if (! strncmp (arg, "-ms", sizeof ("-ms") - 1))
265 char* xarg = (char*) JvMalloc (strlen (arg) + sizeof ("X"));
266 sprintf (xarg, "-Xms%s", arg + sizeof ("-Xms") - 1);
267 add_option (vm_args, xarg, NULL);
269 // -mx=n
270 else if (! strncmp (arg, "-mx=", sizeof ("-mx=") - 1))
272 arg[1] = 'X';
273 arg[2] = 'm';
274 arg[3] = 'x';
275 add_option (vm_args, arg, NULL);
277 // -mx n
278 else if (! strcmp (arg, "-mx"))
280 if (i >= argc - 1)
281 goto no_arg;
283 char* xarg = (char*) JvMalloc (strlen (argv[++i])
284 + sizeof ("-Xmx"));
285 sprintf (xarg, "-Xmx%s", argv[i]);
286 add_option (vm_args, xarg, NULL);
288 // -mxn
289 else if (! strncmp (arg, "-mx", sizeof ("-mx") - 1))
291 char* xarg = (char*) JvMalloc (strlen (arg) + sizeof ("X"));
292 sprintf (xarg, "-Xmx%s", arg + sizeof ("-Xmx") - 1);
293 add_option (vm_args, xarg, NULL);
295 // -ss=n
296 else if (! strncmp (arg, "-ss=", sizeof ("-ss=") - 1))
298 arg[1] = 'X';
299 arg[2] = 's';
300 arg[3] = 's';
301 add_option (vm_args, arg, NULL);
303 // -ss n
304 else if (! strcmp (arg, "-ss"))
306 if (i >= argc - 1)
307 goto no_arg;
309 char* xarg = (char*) JvMalloc (strlen (argv[++i])
310 + sizeof ("-Xss"));
311 sprintf (xarg, "-Xss%s", argv[i]);
312 add_option (vm_args, xarg, NULL);
314 // -ssn
315 else if (! strncmp (arg, "-ss", sizeof ("-ss") - 1))
317 char* xarg = (char*) JvMalloc (strlen (arg) + sizeof ("X"));
318 sprintf (xarg, "-Xss%s", arg + sizeof ("-Xss") - 1);
319 add_option (vm_args, xarg, NULL);
321 // This handles all the option variants that begin with
322 // -verbose.
323 else if (! strncmp (arg, "-verbose", 8))
324 add_option (vm_args, arg, NULL);
325 else if (! strcmp (arg, "-version"))
327 version ();
328 exit (0);
330 else if (! strcmp (arg, "-fullversion"))
332 printf ("java full version \"gcj-" JV_VERSION "\"\n");
333 exit (0);
335 else if (! strcmp (arg, "-showversion"))
336 version ();
337 else if (! strcmp (arg, "-help") || ! strcmp (arg, "-?"))
338 help ();
339 else if (! strcmp (arg, "-X"))
340 nonstandard_opts_help ();
341 else if (! strncmp (arg, "-X", 2))
342 add_option (vm_args, arg, NULL);
343 // Obsolete options recognized for backwards-compatibility.
344 else if (! strcmp (arg, "-verify")
345 || ! strcmp (arg, "-verifyremote"))
346 continue;
347 else if (! strcmp (arg, "-noverify"))
349 gcj::verifyClasses = false;
351 else
353 fprintf (stderr, "gij: unrecognized option -- `%s'\n", argv[i]);
354 fprintf (stderr, "Try `gij --help' for more information.\n");
355 exit (1);
359 if (argc - i < 1)
361 fprintf (stderr, "Usage: gij [OPTION] ... CLASS [ARGS] ...\n");
362 fprintf (stderr, " to invoke CLASS.main, or\n");
363 fprintf (stderr, " gij -jar [OPTION] ... JARFILE [ARGS] ...\n");
364 fprintf (stderr, " to execute a jar file\n");
365 fprintf (stderr, "Try `gij --help' for more information.\n");
366 exit (1);
369 // -jar mode overrides all other modes of specifying class path:
370 // CLASSPATH, -Djava.class.path, -classpath and -cp.
371 if (jar_mode)
373 char* darg = (char*) JvMalloc (strlen (argv[i])
374 + sizeof ("-Djava.class.path="));
375 sprintf (darg, "-Djava.class.path=%s", argv[i]);
376 add_option (vm_args, darg, NULL);
379 _Jv_RunMain (&vm_args, NULL, argv[i], argc - i,
380 (char const**) (argv + i), jar_mode);