diff options
author | Bruno Haible <bruno@clisp.org> | 2001-09-17 12:43:24 +0000 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2001-09-17 12:43:24 +0000 |
commit | 031366782b48aa75d1c430de00afc38d8967d68e (patch) | |
tree | e3659aa29ae2c4b40a2db90430bc4033af68d548 /lib/javacomp.c | |
parent | 5b6bacd697f1609131376e07d38ba6609c0c61dd (diff) | |
download | external_gettext-031366782b48aa75d1c430de00afc38d8967d68e.zip external_gettext-031366782b48aa75d1c430de00afc38d8967d68e.tar.gz external_gettext-031366782b48aa75d1c430de00afc38d8967d68e.tar.bz2 |
Routines for compiling and executing Java programs.
Diffstat (limited to 'lib/javacomp.c')
-rw-r--r-- | lib/javacomp.c | 420 |
1 files changed, 420 insertions, 0 deletions
diff --git a/lib/javacomp.c b/lib/javacomp.c new file mode 100644 index 0000000..6ed8714 --- /dev/null +++ b/lib/javacomp.c @@ -0,0 +1,420 @@ +/* Compile a Java program. + Copyright (C) 2001 Free Software Foundation, Inc. + Written by Bruno Haible <haible@clisp.cons.org>, 2001. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +/* Specification. */ +#include "javacomp.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "execute.h" +#include "setenv.h" +#include "sh-quote.h" +#include "system.h" +#include "error.h" +#include "libgettext.h" + +#define _(str) gettext (str) + + +/* CLASSPATH handling subroutines. */ +#include "classpath.c" + +/* Survey of Java compilers. + + A = does it work without CLASSPATH being set + C = option to set CLASSPATH, other than setting it in the environment + O = option for optimizing + g = option for debugging + T = test for presence + + Program from A C O g T + + $JAVAC unknown N n/a -O -g true + gcj -C GCC 3.0 Y --classpath=P -O -g gcj --version >/dev/null + javac JDK 1.1.8 Y -classpath P -O -g javac 2>/dev/null; test $? = 1 + javac JDK 1.3.0 Y -classpath P -O -g javac 2>/dev/null; test $? = 1 + jikes Jikes 1.14 N -classpath P -O -g jikes 2>/dev/null; test $? = 1 + + All compilers support the option "-d DIRECTORY" for the base directory + of the classes to be written. + + The CLASSPATH is a colon separated list of pathnames. (On Windows: a + semicolon separated list of pathnames.) + + We try the Java compilers in the following order: + 1. getenv ("JAVAC"), because the user must be able to override our + preferences, + 2. "gcj -C", because it is a completely free compiler, + 3. "javac", because it is a standard compiler, + 4. "jikes", comes last because it has some deviating interpretation + of the Java Language Specification and because it requires a + CLASSPATH environment variable. + + We unset the JAVA_HOME environment variable, because a wrong setting of + this variable can confuse the JDK's javac. + */ + +bool +compile_java_class (java_sources, java_sources_count, + classpaths, classpaths_count, + directory, optimize, debug, use_minimal_classpath, verbose) + const char * const *java_sources; + unsigned int java_sources_count; + const char * const *classpaths; + unsigned int classpaths_count; + const char *directory; + bool optimize; + bool debug; + bool use_minimal_classpath; + bool verbose; +{ + bool err = false; + char *old_JAVA_HOME; + + { + const char *javac = getenv ("JAVAC"); + if (javac != NULL && javac[0] != '\0') + { + /* Because $JAVAC may consist of a command and options, we use the + shell. Because $JAVAC has been set by the user, we leave all + all environment variables in place, including JAVA_HOME, and + we don't erase the user's CLASSPATH. */ + char *old_classpath; + unsigned int command_length; + char *command; + char *argv[4]; + int exitstatus; + unsigned int i; + char *p; + + /* Set CLASSPATH. */ + old_classpath = + set_classpath (classpaths, classpaths_count, false, + verbose); + + command_length = strlen (javac); + if (optimize) + command_length += 3; + if (debug) + command_length += 3; + if (directory != NULL) + command_length += 4 + shell_quote_length (directory); + for (i = 0; i < java_sources_count; i++) + command_length += 1 + shell_quote_length (java_sources[i]); + command_length += 1; + + command = (char *) alloca (command_length); + p = command; + /* Don't shell_quote $JAVAC, because it may consist of a command + and options. */ + memcpy (p, javac, strlen (javac)); + p += strlen (javac); + if (optimize) + { + memcpy (p, " -O", 3); + p += 3; + } + if (debug) + { + memcpy (p, " -g", 3); + p += 3; + } + if (directory != NULL) + { + memcpy (p, " -d ", 4); + p += 4; + p = shell_quote_copy (p, directory); + } + for (i = 0; i < java_sources_count; i++) + { + *p++ = ' '; + p = shell_quote_copy (p, java_sources[i]); + } + *p++ = '\0'; + /* Ensure command_length was correctly calculated. */ + if (p - command > command_length) + abort (); + + if (verbose) + printf ("%s\n", command); + + argv[0] = "/bin/sh"; + argv[1] = "-c"; + argv[2] = command; + argv[3] = NULL; + exitstatus = execute (javac, "/bin/sh", argv, false, false, false); + err = (exitstatus != 0); + + /* Reset CLASSPATH. */ + reset_classpath (old_classpath); + + goto done1; + } + } + + /* Unset the JAVA_HOME environment variable. */ + old_JAVA_HOME = getenv ("JAVA_HOME"); + if (old_JAVA_HOME != NULL) + { + old_JAVA_HOME = xstrdup (old_JAVA_HOME); + unsetenv ("JAVA_HOME"); + } + + { + static bool gcj_tested; + static bool gcj_present; + + if (!gcj_tested) + { + /* Test for presence of gcj: "gcj --version > /dev/null" */ + char *argv[3]; + int exitstatus; + + argv[0] = "gcj"; + argv[1] = "--version"; + argv[2] = NULL; + exitstatus = execute ("gcj", "gcj", argv, false, true, true); + gcj_present = (exitstatus == 0); + gcj_tested = true; + } + + if (gcj_present) + { + char *old_classpath; + unsigned int argc; + char **argv; + char **argp; + int exitstatus; + unsigned int i; + + /* Set CLASSPATH. We could also use the --CLASSPATH=... option + of gcj. Note that --classpath=... option is different: its + argument should also contain gcj's libgcj.jar, but we don't + know its location. */ + old_classpath = + set_classpath (classpaths, classpaths_count, use_minimal_classpath, + verbose); + + argc = + 2 + (optimize ? 1 : 0) + (debug ? 1 : 0) + (directory != NULL ? 2 : 0) + + java_sources_count; + argv = (char **) alloca ((argc + 1) * sizeof (char *)); + + argp = argv; + *argp++ = "gcj"; + *argp++ = "-C"; + if (optimize) + *argp++ = "-O"; + if (debug) + *argp++ = "-g"; + if (directory != NULL) + { + *argp++ = "-d"; + *argp++ = (char *) directory; + } + for (i = 0; i < java_sources_count; i++) + *argp++ = (char *) java_sources[i]; + *argp = NULL; + /* Ensure argv length was correctly calculated. */ + if (argp - argv != argc) + abort (); + + if (verbose) + { + char *command = shell_quote_argv (argv); + printf ("%s\n", command); + free (command); + } + + exitstatus = execute ("gcj", "gcj", argv, false, false, false); + err = (exitstatus != 0); + + /* Reset CLASSPATH. */ + reset_classpath (old_classpath); + + goto done2; + } + } + + { + static bool javac_tested; + static bool javac_present; + + if (!javac_tested) + { + /* Test for presence of javac: "javac 2> /dev/null ; test $? = 1" */ + char *argv[2]; + int exitstatus; + + argv[0] = "javac"; + argv[1] = NULL; + exitstatus = execute ("javac", "javac", argv, false, true, true); + javac_present = (exitstatus == 0 || exitstatus == 1); + javac_tested = true; + } + + if (javac_present) + { + char *old_classpath; + unsigned int argc; + char **argv; + char **argp; + int exitstatus; + unsigned int i; + + /* Set CLASSPATH. We don't use the "-classpath ..." option because + in JDK 1.1.x its argument should also contain the JDK's classes.zip, + but we don't know its location. (In JDK 1.3.0 it would work.) */ + old_classpath = + set_classpath (classpaths, classpaths_count, use_minimal_classpath, + verbose); + + argc = + 1 + (optimize ? 1 : 0) + (debug ? 1 : 0) + (directory != NULL ? 2 : 0) + + java_sources_count; + argv = (char **) alloca ((argc + 1) * sizeof (char *)); + + argp = argv; + *argp++ = "javac"; + if (optimize) + *argp++ = "-O"; + if (debug) + *argp++ = "-g"; + if (directory != NULL) + { + *argp++ = "-d"; + *argp++ = (char *) directory; + } + for (i = 0; i < java_sources_count; i++) + *argp++ = (char *) java_sources[i]; + *argp = NULL; + /* Ensure argv length was correctly calculated. */ + if (argp - argv != argc) + abort (); + + if (verbose) + { + char *command = shell_quote_argv (argv); + printf ("%s\n", command); + free (command); + } + + exitstatus = execute ("javac", "javac", argv, false, false, false); + err = (exitstatus != 0); + + /* Reset CLASSPATH. */ + reset_classpath (old_classpath); + + goto done2; + } + } + + { + static bool jikes_tested; + static bool jikes_present; + + if (!jikes_tested) + { + /* Test for presence of jikes: "jikes 2> /dev/null ; test $? = 1" */ + char *argv[2]; + int exitstatus; + + argv[0] = "jikes"; + argv[1] = NULL; + exitstatus = execute ("jikes", "jikes", argv, false, true, true); + jikes_present = (exitstatus == 0 || exitstatus == 1); + jikes_tested = true; + } + + if (jikes_present) + { + char *old_classpath; + unsigned int argc; + char **argv; + char **argp; + int exitstatus; + unsigned int i; + + /* Set CLASSPATH. We could also use the "-classpath ..." option. + Since jikes doesn't come with its own standard library, it + needs a classes.zip or rt.jar or libgcj.jar in the CLASSPATH. + To increase the chance of success, we reuse the current CLASSPATH + if the user has set it. */ + old_classpath = + set_classpath (classpaths, classpaths_count, false, + verbose); + + argc = + 1 + (optimize ? 1 : 0) + (debug ? 1 : 0) + (directory != NULL ? 2 : 0) + + java_sources_count; + argv = (char **) alloca ((argc + 1) * sizeof (char *)); + + argp = argv; + *argp++ = "jikes"; + if (optimize) + *argp++ = "-O"; + if (debug) + *argp++ = "-g"; + if (directory != NULL) + { + *argp++ = "-d"; + *argp++ = (char *) directory; + } + for (i = 0; i < java_sources_count; i++) + *argp++ = (char *) java_sources[i]; + *argp = NULL; + /* Ensure argv length was correctly calculated. */ + if (argp - argv != argc) + abort (); + + if (verbose) + { + char *command = shell_quote_argv (argv); + printf ("%s\n", command); + free (command); + } + + exitstatus = execute ("jikes", "jikes", argv, false, false, false); + err = (exitstatus != 0); + + /* Reset CLASSPATH. */ + reset_classpath (old_classpath); + + goto done2; + } + } + + error (0, 0, _("Java compiler not found, try installing gcj or set $JAVAC")); + err = true; + + done2: + if (old_JAVA_HOME != NULL) + { + setenv ("JAVA_HOME", old_JAVA_HOME, 1); + free (old_JAVA_HOME); + } + + done1: + return err; +} |