1 /* Compile a Java program.
2 Copyright (C) 2001-2003 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
33 #include "wait-process.h"
34 #include "classpath.h"
37 #include "safe-read.h"
43 #define _(str) gettext (str)
46 /* Survey of Java compilers.
48 A = does it work without CLASSPATH being set
49 C = option to set CLASSPATH, other than setting it in the environment
50 O = option for optimizing
51 g = option for debugging
54 Program from A C O g T
56 $JAVAC unknown N n/a -O -g true
57 gcj -C GCC 3.2 Y --classpath=P -O -g gcj --version | sed -e 's,^[^0-9]*,,' -e 1q | sed -e '/^3\.[01]/d' | grep '^[3-9]' >/dev/null
58 javac JDK 1.1.8 Y -classpath P -O -g javac 2>/dev/null; test $? = 1
59 javac JDK 1.3.0 Y -classpath P -O -g javac 2>/dev/null; test $? -le 2
60 jikes Jikes 1.14 N -classpath P -O -g jikes 2>/dev/null; test $? = 1
62 All compilers support the option "-d DIRECTORY" for the base directory
63 of the classes to be written.
65 The CLASSPATH is a colon separated list of pathnames. (On Windows: a
66 semicolon separated list of pathnames.)
68 We try the Java compilers in the following order:
69 1. getenv ("JAVAC"), because the user must be able to override our
71 2. "gcj -C", because it is a completely free compiler,
72 3. "javac", because it is a standard compiler,
73 4. "jikes", comes last because it has some deviating interpretation
74 of the Java Language Specification and because it requires a
75 CLASSPATH environment variable.
77 We unset the JAVA_HOME environment variable, because a wrong setting of
78 this variable can confuse the JDK's javac.
82 compile_java_class (const char * const *java_sources,
83 unsigned int java_sources_count,
84 const char * const *classpaths,
85 unsigned int classpaths_count,
86 const char *directory,
87 bool optimize, bool debug,
88 bool use_minimal_classpath,
95 const char *javac = getenv ("JAVAC");
96 if (javac != NULL && javac[0] != '\0')
98 /* Because $JAVAC may consist of a command and options, we use the
99 shell. Because $JAVAC has been set by the user, we leave all
100 all environment variables in place, including JAVA_HOME, and
101 we don't erase the user's CLASSPATH. */
103 unsigned int command_length;
112 set_classpath (classpaths, classpaths_count, false,
115 command_length = strlen (javac);
120 if (directory != NULL)
121 command_length += 4 + shell_quote_length (directory);
122 for (i = 0; i < java_sources_count; i++)
123 command_length += 1 + shell_quote_length (java_sources[i]);
126 command = (char *) xallocsa (command_length);
128 /* Don't shell_quote $JAVAC, because it may consist of a command
130 memcpy (p, javac, strlen (javac));
134 memcpy (p, " -O", 3);
139 memcpy (p, " -g", 3);
142 if (directory != NULL)
144 memcpy (p, " -d ", 4);
146 p = shell_quote_copy (p, directory);
148 for (i = 0; i < java_sources_count; i++)
151 p = shell_quote_copy (p, java_sources[i]);
154 /* Ensure command_length was correctly calculated. */
155 if (p - command > command_length)
159 printf ("%s\n", command);
165 exitstatus = execute (javac, "/bin/sh", argv, false, false, false,
167 err = (exitstatus != 0);
171 /* Reset CLASSPATH. */
172 reset_classpath (old_classpath);
178 /* Unset the JAVA_HOME environment variable. */
179 old_JAVA_HOME = getenv ("JAVA_HOME");
180 if (old_JAVA_HOME != NULL)
182 old_JAVA_HOME = xstrdup (old_JAVA_HOME);
183 unsetenv ("JAVA_HOME");
187 static bool gcj_tested;
188 static bool gcj_present;
192 /* Test for presence of gcj:
193 "gcj --version 2> /dev/null | \
194 sed -e 's,^[^0-9]*,,' -e 1q | \
195 sed -e '/^3\.[01]/d' | grep '^[3-9]' > /dev/null" */
202 argv[1] = "--version";
204 child = create_pipe_in ("gcj", "gcj", argv, DEV_NULL, true, true,
209 /* Read the subprocess output, drop all lines except the first,
210 drop all characters before the first digit, and test whether
211 the remaining string starts with a digit >= 3, but not with
216 while (safe_read (fd[0], &c[count], 1) > 0)
218 if (c[count] == '\n')
222 if (!(c[0] >= '0' && c[0] <= '9'))
224 gcj_present = (c[0] >= '3');
229 if (c[0] == '3' && c[1] == '.'
230 && (c[2] == '0' || c[2] == '1'))
235 while (safe_read (fd[0], &c[0], 1) > 0)
240 /* Remove zombie process from process list, and retrieve exit
243 wait_subprocess (child, "gcj", false, true, true, false);
259 /* Set CLASSPATH. We could also use the --CLASSPATH=... option
260 of gcj. Note that --classpath=... option is different: its
261 argument should also contain gcj's libgcj.jar, but we don't
262 know its location. */
264 set_classpath (classpaths, classpaths_count, use_minimal_classpath,
268 2 + (optimize ? 1 : 0) + (debug ? 1 : 0) + (directory != NULL ? 2 : 0)
269 + java_sources_count;
270 argv = (char **) xallocsa ((argc + 1) * sizeof (char *));
279 if (directory != NULL)
282 *argp++ = (char *) directory;
284 for (i = 0; i < java_sources_count; i++)
285 *argp++ = (char *) java_sources[i];
287 /* Ensure argv length was correctly calculated. */
288 if (argp - argv != argc)
293 char *command = shell_quote_argv (argv);
294 printf ("%s\n", command);
298 exitstatus = execute ("gcj", "gcj", argv, false, false, false, false,
300 err = (exitstatus != 0);
304 /* Reset CLASSPATH. */
305 reset_classpath (old_classpath);
312 static bool javac_tested;
313 static bool javac_present;
317 /* Test for presence of javac: "javac 2> /dev/null ; test $? -le 2" */
323 exitstatus = execute ("javac", "javac", argv, false, false, true, true,
325 javac_present = (exitstatus == 0 || exitstatus == 1 || exitstatus == 2);
338 /* Set CLASSPATH. We don't use the "-classpath ..." option because
339 in JDK 1.1.x its argument should also contain the JDK's classes.zip,
340 but we don't know its location. (In JDK 1.3.0 it would work.) */
342 set_classpath (classpaths, classpaths_count, use_minimal_classpath,
346 1 + (optimize ? 1 : 0) + (debug ? 1 : 0) + (directory != NULL ? 2 : 0)
347 + java_sources_count;
348 argv = (char **) xallocsa ((argc + 1) * sizeof (char *));
356 if (directory != NULL)
359 *argp++ = (char *) directory;
361 for (i = 0; i < java_sources_count; i++)
362 *argp++ = (char *) java_sources[i];
364 /* Ensure argv length was correctly calculated. */
365 if (argp - argv != argc)
370 char *command = shell_quote_argv (argv);
371 printf ("%s\n", command);
375 exitstatus = execute ("javac", "javac", argv, false, false, false,
377 err = (exitstatus != 0);
381 /* Reset CLASSPATH. */
382 reset_classpath (old_classpath);
389 static bool jikes_tested;
390 static bool jikes_present;
394 /* Test for presence of jikes: "jikes 2> /dev/null ; test $? = 1" */
400 exitstatus = execute ("jikes", "jikes", argv, false, false, true, true,
402 jikes_present = (exitstatus == 0 || exitstatus == 1);
415 /* Set CLASSPATH. We could also use the "-classpath ..." option.
416 Since jikes doesn't come with its own standard library, it
417 needs a classes.zip or rt.jar or libgcj.jar in the CLASSPATH.
418 To increase the chance of success, we reuse the current CLASSPATH
419 if the user has set it. */
421 set_classpath (classpaths, classpaths_count, false,
425 1 + (optimize ? 1 : 0) + (debug ? 1 : 0) + (directory != NULL ? 2 : 0)
426 + java_sources_count;
427 argv = (char **) xallocsa ((argc + 1) * sizeof (char *));
435 if (directory != NULL)
438 *argp++ = (char *) directory;
440 for (i = 0; i < java_sources_count; i++)
441 *argp++ = (char *) java_sources[i];
443 /* Ensure argv length was correctly calculated. */
444 if (argp - argv != argc)
449 char *command = shell_quote_argv (argv);
450 printf ("%s\n", command);
454 exitstatus = execute ("jikes", "jikes", argv, false, false, false,
456 err = (exitstatus != 0);
460 /* Reset CLASSPATH. */
461 reset_classpath (old_classpath);
467 error (0, 0, _("Java compiler not found, try installing gcj or set $JAVAC"));
471 if (old_JAVA_HOME != NULL)
473 xsetenv ("JAVA_HOME", old_JAVA_HOME, 1);
474 free (old_JAVA_HOME);