glob: fix heap buffer overflow
[gnulib.git] / lib / javacomp.c
blob58340a438d6a5db56d3b55687e1fe835320f6d82
1 /* Compile a Java program.
2 Copyright (C) 2001-2003, 2006-2017 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 3 of the License, or
8 (at your option) any later version.
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, see <https://www.gnu.org/licenses/>. */
18 #include <config.h>
19 #include <alloca.h>
21 /* Specification. */
22 #include "javacomp.h"
24 #include <errno.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
33 #include "javaversion.h"
34 #include "execute.h"
35 #include "spawn-pipe.h"
36 #include "wait-process.h"
37 #include "classpath.h"
38 #include "xsetenv.h"
39 #include "sh-quote.h"
40 #include "binary-io.h"
41 #include "safe-read.h"
42 #include "xalloc.h"
43 #include "xmalloca.h"
44 #include "concat-filename.h"
45 #include "fwriteerror.h"
46 #include "clean-temp.h"
47 #include "error.h"
48 #include "xvasprintf.h"
49 #include "c-strstr.h"
50 #include "gettext.h"
52 #define _(str) gettext (str)
55 /* Survey of Java compilers.
57 A = does it work without CLASSPATH being set
58 C = option to set CLASSPATH, other than setting it in the environment
59 O = option for optimizing
60 g = option for debugging
61 T = test for presence
63 Program from A C O g T
65 $JAVAC unknown N n/a -O -g true
66 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
67 javac JDK 1.1.8 Y -classpath P -O -g javac 2>/dev/null; test $? = 1
68 javac JDK 1.3.0 Y -classpath P -O -g javac 2>/dev/null; test $? -le 2
69 jikes Jikes 1.14 N -classpath P -O -g jikes 2>/dev/null; test $? = 1
71 All compilers support the option "-d DIRECTORY" for the base directory
72 of the classes to be written.
74 The CLASSPATH is a colon separated list of pathnames. (On Windows: a
75 semicolon separated list of pathnames.)
77 We try the Java compilers in the following order:
78 1. getenv ("JAVAC"), because the user must be able to override our
79 preferences,
80 2. "gcj -C", because it is a completely free compiler,
81 3. "javac", because it is a standard compiler,
82 4. "jikes", comes last because it has some deviating interpretation
83 of the Java Language Specification and because it requires a
84 CLASSPATH environment variable.
86 We unset the JAVA_HOME environment variable, because a wrong setting of
87 this variable can confuse the JDK's javac.
90 /* Return the default target_version. */
91 static const char *
92 default_target_version (void)
94 /* Use a cache. Assumes that the PATH environment variable doesn't change
95 during the lifetime of the program. */
96 static const char *java_version_cache;
97 if (java_version_cache == NULL)
99 /* Determine the version from the found JVM. */
100 java_version_cache = javaexec_version ();
101 if (java_version_cache == NULL
102 || !(java_version_cache[0] == '1' && java_version_cache[1] == '.'
103 && (java_version_cache[2] >= '1' && java_version_cache[2] <= '6')
104 && java_version_cache[3] == '\0'))
105 java_version_cache = "1.1";
107 return java_version_cache;
110 /* ======================= Source version dependent ======================= */
112 /* Convert a source version to an index. */
113 #define SOURCE_VERSION_BOUND 3 /* exclusive upper bound */
114 static unsigned int
115 source_version_index (const char *source_version)
117 if (source_version[0] == '1' && source_version[1] == '.'
118 && (source_version[2] >= '3' && source_version[2] <= '5')
119 && source_version[3] == '\0')
120 return source_version[2] - '3';
121 error (EXIT_FAILURE, 0, _("invalid source_version argument to compile_java_class"));
122 return 0;
125 /* Return a snippet of code that should compile in the given source version. */
126 static const char *
127 get_goodcode_snippet (const char *source_version)
129 if (strcmp (source_version, "1.3") == 0)
130 return "class conftest {}\n";
131 if (strcmp (source_version, "1.4") == 0)
132 return "class conftest { static { assert(true); } }\n";
133 if (strcmp (source_version, "1.5") == 0)
134 return "class conftest<T> { T foo() { return null; } }\n";
135 error (EXIT_FAILURE, 0, _("invalid source_version argument to compile_java_class"));
136 return NULL;
139 /* Return a snippet of code that should fail to compile in the given source
140 version, or NULL (standing for a snippet that would fail to compile with
141 any compiler). */
142 static const char *
143 get_failcode_snippet (const char *source_version)
145 if (strcmp (source_version, "1.3") == 0)
146 return "class conftestfail { static { assert(true); } }\n";
147 if (strcmp (source_version, "1.4") == 0)
148 return "class conftestfail<T> { T foo() { return null; } }\n";
149 if (strcmp (source_version, "1.5") == 0)
150 return NULL;
151 error (EXIT_FAILURE, 0, _("invalid source_version argument to compile_java_class"));
152 return NULL;
155 /* ======================= Target version dependent ======================= */
157 /* Convert a target version to an index. */
158 #define TARGET_VERSION_BOUND 6 /* exclusive upper bound */
159 static unsigned int
160 target_version_index (const char *target_version)
162 if (target_version[0] == '1' && target_version[1] == '.'
163 && (target_version[2] >= '1' && target_version[2] <= '6')
164 && target_version[3] == '\0')
165 return target_version[2] - '1';
166 error (EXIT_FAILURE, 0, _("invalid target_version argument to compile_java_class"));
167 return 0;
170 /* Return the class file version number corresponding to a given target
171 version. */
172 static int
173 corresponding_classfile_version (const char *target_version)
175 if (strcmp (target_version, "1.1") == 0)
176 return 45;
177 if (strcmp (target_version, "1.2") == 0)
178 return 46;
179 if (strcmp (target_version, "1.3") == 0)
180 return 47;
181 if (strcmp (target_version, "1.4") == 0)
182 return 48;
183 if (strcmp (target_version, "1.5") == 0)
184 return 49;
185 if (strcmp (target_version, "1.6") == 0)
186 return 50;
187 error (EXIT_FAILURE, 0, _("invalid target_version argument to compile_java_class"));
188 return 0;
191 /* ======================== Compilation subroutines ======================== */
193 /* Try to compile a set of Java sources with $JAVAC.
194 Return a failure indicator (true upon error). */
195 static bool
196 compile_using_envjavac (const char *javac,
197 const char * const *java_sources,
198 unsigned int java_sources_count,
199 const char *directory,
200 bool optimize, bool debug,
201 bool verbose, bool null_stderr)
203 /* Because $JAVAC may consist of a command and options, we use the
204 shell. Because $JAVAC has been set by the user, we leave all
205 environment variables in place, including JAVA_HOME, and we don't
206 erase the user's CLASSPATH. */
207 bool err;
208 unsigned int command_length;
209 char *command;
210 char *argv[4];
211 int exitstatus;
212 unsigned int i;
213 char *p;
215 command_length = strlen (javac);
216 if (optimize)
217 command_length += 3;
218 if (debug)
219 command_length += 3;
220 if (directory != NULL)
221 command_length += 4 + shell_quote_length (directory);
222 for (i = 0; i < java_sources_count; i++)
223 command_length += 1 + shell_quote_length (java_sources[i]);
224 command_length += 1;
226 command = (char *) xmalloca (command_length);
227 p = command;
228 /* Don't shell_quote $JAVAC, because it may consist of a command
229 and options. */
230 memcpy (p, javac, strlen (javac));
231 p += strlen (javac);
232 if (optimize)
234 memcpy (p, " -O", 3);
235 p += 3;
237 if (debug)
239 memcpy (p, " -g", 3);
240 p += 3;
242 if (directory != NULL)
244 memcpy (p, " -d ", 4);
245 p += 4;
246 p = shell_quote_copy (p, directory);
248 for (i = 0; i < java_sources_count; i++)
250 *p++ = ' ';
251 p = shell_quote_copy (p, java_sources[i]);
253 *p++ = '\0';
254 /* Ensure command_length was correctly calculated. */
255 if (p - command > command_length)
256 abort ();
258 if (verbose)
259 printf ("%s\n", command);
261 argv[0] = "/bin/sh";
262 argv[1] = "-c";
263 argv[2] = command;
264 argv[3] = NULL;
265 exitstatus = execute (javac, "/bin/sh", argv, false, false, false,
266 null_stderr, true, true, NULL);
267 err = (exitstatus != 0);
269 freea (command);
271 return err;
274 /* Try to compile a set of Java sources with gcj.
275 Return a failure indicator (true upon error). */
276 static bool
277 compile_using_gcj (const char * const *java_sources,
278 unsigned int java_sources_count,
279 bool no_assert_option,
280 bool fsource_option, const char *source_version,
281 bool ftarget_option, const char *target_version,
282 const char *directory,
283 bool optimize, bool debug,
284 bool verbose, bool null_stderr)
286 bool err;
287 unsigned int argc;
288 char **argv;
289 char **argp;
290 char *fsource_arg;
291 char *ftarget_arg;
292 int exitstatus;
293 unsigned int i;
295 argc =
296 2 + (no_assert_option ? 1 : 0) + (fsource_option ? 1 : 0)
297 + (ftarget_option ? 1 : 0) + (optimize ? 1 : 0) + (debug ? 1 : 0)
298 + (directory != NULL ? 2 : 0) + java_sources_count;
299 argv = (char **) xmalloca ((argc + 1) * sizeof (char *));
301 argp = argv;
302 *argp++ = "gcj";
303 *argp++ = "-C";
304 if (no_assert_option)
305 *argp++ = "-fno-assert";
306 if (fsource_option)
308 fsource_arg = (char *) xmalloca (9 + strlen (source_version) + 1);
309 memcpy (fsource_arg, "-fsource=", 9);
310 strcpy (fsource_arg + 9, source_version);
311 *argp++ = fsource_arg;
313 else
314 fsource_arg = NULL;
315 if (ftarget_option)
317 ftarget_arg = (char *) xmalloca (9 + strlen (target_version) + 1);
318 memcpy (ftarget_arg, "-ftarget=", 9);
319 strcpy (ftarget_arg + 9, target_version);
320 *argp++ = ftarget_arg;
322 else
323 ftarget_arg = NULL;
324 if (optimize)
325 *argp++ = "-O";
326 if (debug)
327 *argp++ = "-g";
328 if (directory != NULL)
330 *argp++ = "-d";
331 *argp++ = (char *) directory;
333 for (i = 0; i < java_sources_count; i++)
334 *argp++ = (char *) java_sources[i];
335 *argp = NULL;
336 /* Ensure argv length was correctly calculated. */
337 if (argp - argv != argc)
338 abort ();
340 if (verbose)
342 char *command = shell_quote_argv (argv);
343 printf ("%s\n", command);
344 free (command);
347 exitstatus = execute ("gcj", "gcj", argv, false, false, false, null_stderr,
348 true, true, NULL);
349 err = (exitstatus != 0);
351 if (ftarget_arg != NULL)
352 freea (ftarget_arg);
353 if (fsource_arg != NULL)
354 freea (fsource_arg);
355 freea (argv);
357 return err;
360 /* Try to compile a set of Java sources with javac.
361 Return a failure indicator (true upon error). */
362 static bool
363 compile_using_javac (const char * const *java_sources,
364 unsigned int java_sources_count,
365 bool source_option, const char *source_version,
366 bool target_option, const char *target_version,
367 const char *directory,
368 bool optimize, bool debug,
369 bool verbose, bool null_stderr)
371 bool err;
372 unsigned int argc;
373 char **argv;
374 char **argp;
375 int exitstatus;
376 unsigned int i;
378 argc =
379 1 + (source_option ? 2 : 0) + (target_option ? 2 : 0) + (optimize ? 1 : 0)
380 + (debug ? 1 : 0) + (directory != NULL ? 2 : 0) + java_sources_count;
381 argv = (char **) xmalloca ((argc + 1) * sizeof (char *));
383 argp = argv;
384 *argp++ = "javac";
385 if (source_option)
387 *argp++ = "-source";
388 *argp++ = (char *) source_version;
390 if (target_option)
392 *argp++ = "-target";
393 *argp++ = (char *) target_version;
395 if (optimize)
396 *argp++ = "-O";
397 if (debug)
398 *argp++ = "-g";
399 if (directory != NULL)
401 *argp++ = "-d";
402 *argp++ = (char *) directory;
404 for (i = 0; i < java_sources_count; i++)
405 *argp++ = (char *) java_sources[i];
406 *argp = NULL;
407 /* Ensure argv length was correctly calculated. */
408 if (argp - argv != argc)
409 abort ();
411 if (verbose)
413 char *command = shell_quote_argv (argv);
414 printf ("%s\n", command);
415 free (command);
418 exitstatus = execute ("javac", "javac", argv, false, false, false,
419 null_stderr, true, true, NULL);
420 err = (exitstatus != 0);
422 freea (argv);
424 return err;
427 /* Try to compile a set of Java sources with jikes.
428 Return a failure indicator (true upon error). */
429 static bool
430 compile_using_jikes (const char * const *java_sources,
431 unsigned int java_sources_count,
432 const char *directory,
433 bool optimize, bool debug,
434 bool verbose, bool null_stderr)
436 bool err;
437 unsigned int argc;
438 char **argv;
439 char **argp;
440 int exitstatus;
441 unsigned int i;
443 argc =
444 1 + (optimize ? 1 : 0) + (debug ? 1 : 0) + (directory != NULL ? 2 : 0)
445 + java_sources_count;
446 argv = (char **) xmalloca ((argc + 1) * sizeof (char *));
448 argp = argv;
449 *argp++ = "jikes";
450 if (optimize)
451 *argp++ = "-O";
452 if (debug)
453 *argp++ = "-g";
454 if (directory != NULL)
456 *argp++ = "-d";
457 *argp++ = (char *) directory;
459 for (i = 0; i < java_sources_count; i++)
460 *argp++ = (char *) java_sources[i];
461 *argp = NULL;
462 /* Ensure argv length was correctly calculated. */
463 if (argp - argv != argc)
464 abort ();
466 if (verbose)
468 char *command = shell_quote_argv (argv);
469 printf ("%s\n", command);
470 free (command);
473 exitstatus = execute ("jikes", "jikes", argv, false, false, false,
474 null_stderr, true, true, NULL);
475 err = (exitstatus != 0);
477 freea (argv);
479 return err;
482 /* ====================== Usability test subroutines ====================== */
484 /* Write a given contents to a temporary file.
485 FILE_NAME is the name of a file inside TMPDIR that is known not to exist
486 yet.
487 Return a failure indicator (true upon error). */
488 static bool
489 write_temp_file (struct temp_dir *tmpdir, const char *file_name,
490 const char *contents)
492 FILE *fp;
494 register_temp_file (tmpdir, file_name);
495 fp = fopen_temp (file_name, "w");
496 if (fp == NULL)
498 error (0, errno, _("failed to create \"%s\""), file_name);
499 unregister_temp_file (tmpdir, file_name);
500 return true;
502 fputs (contents, fp);
503 if (fwriteerror_temp (fp))
505 error (0, errno, _("error while writing \"%s\" file"), file_name);
506 return true;
508 return false;
511 /* Return the class file version number of a class file on disk. */
512 static int
513 get_classfile_version (const char *compiled_file_name)
515 unsigned char header[8];
516 int fd;
518 /* Open the class file. */
519 fd = open (compiled_file_name, O_RDONLY | O_BINARY, 0);
520 if (fd >= 0)
522 /* Read its first 8 bytes. */
523 if (safe_read (fd, header, 8) == 8)
525 /* Verify the class file signature. */
526 if (header[0] == 0xCA && header[1] == 0xFE
527 && header[2] == 0xBA && header[3] == 0xBE)
529 close (fd);
530 return header[7];
533 close (fd);
536 /* Could not get the class file version. Return a very large one. */
537 return INT_MAX;
540 /* Return true if $JAVAC is a version of gcj. */
541 static bool
542 is_envjavac_gcj (const char *javac)
544 static bool envjavac_tested;
545 static bool envjavac_gcj;
547 if (!envjavac_tested)
549 /* Test whether $JAVAC is gcj:
550 "$JAVAC --version 2>/dev/null | sed -e 1q | grep gcj > /dev/null" */
551 unsigned int command_length;
552 char *command;
553 char *argv[4];
554 pid_t child;
555 int fd[1];
556 FILE *fp;
557 char *line;
558 size_t linesize;
559 size_t linelen;
560 int exitstatus;
561 char *p;
563 /* Setup the command "$JAVAC --version". */
564 command_length = strlen (javac) + 1 + 9 + 1;
565 command = (char *) xmalloca (command_length);
566 p = command;
567 /* Don't shell_quote $JAVAC, because it may consist of a command
568 and options. */
569 memcpy (p, javac, strlen (javac));
570 p += strlen (javac);
571 memcpy (p, " --version", 1 + 9 + 1);
572 p += 1 + 9 + 1;
573 /* Ensure command_length was correctly calculated. */
574 if (p - command > command_length)
575 abort ();
577 /* Call $JAVAC --version 2>/dev/null. */
578 argv[0] = "/bin/sh";
579 argv[1] = "-c";
580 argv[2] = command;
581 argv[3] = NULL;
582 child = create_pipe_in (javac, "/bin/sh", argv, DEV_NULL, true, true,
583 false, fd);
584 if (child == -1)
585 goto failed;
587 /* Retrieve its result. */
588 fp = fdopen (fd[0], "r");
589 if (fp == NULL)
590 goto failed;
592 line = NULL; linesize = 0;
593 linelen = getline (&line, &linesize, fp);
594 if (linelen == (size_t)(-1))
596 fclose (fp);
597 goto failed;
599 /* It is safe to call c_strstr() instead of strstr() here; see the
600 comments in c-strstr.h. */
601 envjavac_gcj = (c_strstr (line, "gcj") != NULL);
603 fclose (fp);
605 /* Remove zombie process from process list, and retrieve exit status. */
606 exitstatus =
607 wait_subprocess (child, javac, true, true, true, false, NULL);
608 if (exitstatus != 0)
609 envjavac_gcj = false;
611 failed:
612 freea (command);
614 envjavac_tested = true;
617 return envjavac_gcj;
620 /* Return true if $JAVAC, known to be a version of gcj, is a version >= 4.3
621 of gcj. */
622 static bool
623 is_envjavac_gcj43 (const char *javac)
625 static bool envjavac_tested;
626 static bool envjavac_gcj43;
628 if (!envjavac_tested)
630 /* Test whether $JAVAC is gcj:
631 "$JAVAC --version 2>/dev/null | sed -e 's,^[^0-9]*,,' -e 1q \
632 | sed -e '/^4\.[012]/d' | grep '^[4-9]' >/dev/null" */
633 unsigned int command_length;
634 char *command;
635 char *argv[4];
636 pid_t child;
637 int fd[1];
638 FILE *fp;
639 char *line;
640 size_t linesize;
641 size_t linelen;
642 int exitstatus;
643 char *p;
645 /* Setup the command "$JAVAC --version". */
646 command_length = strlen (javac) + 1 + 9 + 1;
647 command = (char *) xmalloca (command_length);
648 p = command;
649 /* Don't shell_quote $JAVAC, because it may consist of a command
650 and options. */
651 memcpy (p, javac, strlen (javac));
652 p += strlen (javac);
653 memcpy (p, " --version", 1 + 9 + 1);
654 p += 1 + 9 + 1;
655 /* Ensure command_length was correctly calculated. */
656 if (p - command > command_length)
657 abort ();
659 /* Call $JAVAC --version 2>/dev/null. */
660 argv[0] = "/bin/sh";
661 argv[1] = "-c";
662 argv[2] = command;
663 argv[3] = NULL;
664 child = create_pipe_in (javac, "/bin/sh", argv, DEV_NULL, true, true,
665 false, fd);
666 if (child == -1)
667 goto failed;
669 /* Retrieve its result. */
670 fp = fdopen (fd[0], "r");
671 if (fp == NULL)
672 goto failed;
674 line = NULL; linesize = 0;
675 linelen = getline (&line, &linesize, fp);
676 if (linelen == (size_t)(-1))
678 fclose (fp);
679 goto failed;
681 p = line;
682 while (*p != '\0' && !(*p >= '0' && *p <= '9'))
683 p++;
684 envjavac_gcj43 =
685 !(*p == '4' && p[1] == '.' && p[2] >= '0' && p[2] <= '2')
686 && (*p >= '4' && *p <= '9');
688 fclose (fp);
690 /* Remove zombie process from process list, and retrieve exit status. */
691 exitstatus =
692 wait_subprocess (child, javac, true, true, true, false, NULL);
693 if (exitstatus != 0)
694 envjavac_gcj43 = false;
696 failed:
697 freea (command);
699 envjavac_tested = true;
702 return envjavac_gcj43;
705 /* Test whether $JAVAC, known to be a version of gcj >= 4.3, can be used, and
706 whether it needs a -fsource and/or -ftarget option.
707 Return a failure indicator (true upon error). */
708 static bool
709 is_envjavac_gcj43_usable (const char *javac,
710 const char *source_version,
711 const char *target_version,
712 bool *usablep,
713 bool *fsource_option_p, bool *ftarget_option_p)
715 /* The cache depends on the source_version and target_version. */
716 struct result_t
718 bool tested;
719 bool usable;
720 bool fsource_option;
721 bool ftarget_option;
723 static struct result_t result_cache[SOURCE_VERSION_BOUND][TARGET_VERSION_BOUND];
724 struct result_t *resultp;
726 resultp = &result_cache[source_version_index (source_version)]
727 [target_version_index (target_version)];
728 if (!resultp->tested)
730 /* Try $JAVAC. */
731 struct temp_dir *tmpdir;
732 char *conftest_file_name;
733 char *compiled_file_name;
734 const char *java_sources[1];
735 struct stat statbuf;
737 tmpdir = create_temp_dir ("java", NULL, false);
738 if (tmpdir == NULL)
739 return true;
741 conftest_file_name =
742 xconcatenated_filename (tmpdir->dir_name, "conftest.java", NULL);
743 if (write_temp_file (tmpdir, conftest_file_name,
744 get_goodcode_snippet (source_version)))
746 free (conftest_file_name);
747 cleanup_temp_dir (tmpdir);
748 return true;
751 compiled_file_name =
752 xconcatenated_filename (tmpdir->dir_name, "conftest.class", NULL);
753 register_temp_file (tmpdir, compiled_file_name);
755 java_sources[0] = conftest_file_name;
756 if (!compile_using_envjavac (javac,
757 java_sources, 1, tmpdir->dir_name,
758 false, false, false, true)
759 && stat (compiled_file_name, &statbuf) >= 0
760 && get_classfile_version (compiled_file_name)
761 <= corresponding_classfile_version (target_version))
763 /* $JAVAC compiled conftest.java successfully. */
764 /* Try adding -fsource option if it is useful. */
765 char *javac_source =
766 xasprintf ("%s -fsource=%s", javac, source_version);
768 unlink (compiled_file_name);
770 java_sources[0] = conftest_file_name;
771 if (!compile_using_envjavac (javac_source,
772 java_sources, 1, tmpdir->dir_name,
773 false, false, false, true)
774 && stat (compiled_file_name, &statbuf) >= 0
775 && get_classfile_version (compiled_file_name)
776 <= corresponding_classfile_version (target_version))
778 const char *failcode = get_failcode_snippet (source_version);
780 if (failcode != NULL)
782 free (compiled_file_name);
783 free (conftest_file_name);
785 conftest_file_name =
786 xconcatenated_filename (tmpdir->dir_name,
787 "conftestfail.java",
788 NULL);
789 if (write_temp_file (tmpdir, conftest_file_name, failcode))
791 free (conftest_file_name);
792 free (javac_source);
793 cleanup_temp_dir (tmpdir);
794 return true;
797 compiled_file_name =
798 xconcatenated_filename (tmpdir->dir_name,
799 "conftestfail.class",
800 NULL);
801 register_temp_file (tmpdir, compiled_file_name);
803 java_sources[0] = conftest_file_name;
804 if (!compile_using_envjavac (javac,
805 java_sources, 1,
806 tmpdir->dir_name,
807 false, false, false, true)
808 && stat (compiled_file_name, &statbuf) >= 0)
810 unlink (compiled_file_name);
812 java_sources[0] = conftest_file_name;
813 if (compile_using_envjavac (javac_source,
814 java_sources, 1,
815 tmpdir->dir_name,
816 false, false, false, true))
817 /* $JAVAC compiled conftestfail.java successfully, and
818 "$JAVAC -fsource=$source_version" rejects it. So
819 the -fsource option is useful. */
820 resultp->fsource_option = true;
825 free (javac_source);
827 resultp->usable = true;
829 else
831 /* Try with -fsource and -ftarget options. */
832 char *javac_target =
833 xasprintf ("%s -fsource=%s -ftarget=%s",
834 javac, source_version, target_version);
836 unlink (compiled_file_name);
838 java_sources[0] = conftest_file_name;
839 if (!compile_using_envjavac (javac_target,
840 java_sources, 1, tmpdir->dir_name,
841 false, false, false, true)
842 && stat (compiled_file_name, &statbuf) >= 0
843 && get_classfile_version (compiled_file_name)
844 <= corresponding_classfile_version (target_version))
846 /* "$JAVAC -fsource $source_version -ftarget $target_version"
847 compiled conftest.java successfully. */
848 resultp->fsource_option = true;
849 resultp->ftarget_option = true;
850 resultp->usable = true;
853 free (javac_target);
856 free (compiled_file_name);
857 free (conftest_file_name);
859 resultp->tested = true;
862 *usablep = resultp->usable;
863 *fsource_option_p = resultp->fsource_option;
864 *ftarget_option_p = resultp->ftarget_option;
865 return false;
868 /* Test whether $JAVAC, known to be a version of gcj < 4.3, can be used for
869 compiling with target_version = 1.4 and source_version = 1.4.
870 Return a failure indicator (true upon error). */
871 static bool
872 is_envjavac_oldgcj_14_14_usable (const char *javac, bool *usablep)
874 static bool envjavac_tested;
875 static bool envjavac_usable;
877 if (!envjavac_tested)
879 /* Try $JAVAC. */
880 struct temp_dir *tmpdir;
881 char *conftest_file_name;
882 char *compiled_file_name;
883 const char *java_sources[1];
884 struct stat statbuf;
886 tmpdir = create_temp_dir ("java", NULL, false);
887 if (tmpdir == NULL)
888 return true;
890 conftest_file_name =
891 xconcatenated_filename (tmpdir->dir_name, "conftest.java", NULL);
892 if (write_temp_file (tmpdir, conftest_file_name,
893 get_goodcode_snippet ("1.4")))
895 free (conftest_file_name);
896 cleanup_temp_dir (tmpdir);
897 return true;
900 compiled_file_name =
901 xconcatenated_filename (tmpdir->dir_name, "conftest.class", NULL);
902 register_temp_file (tmpdir, compiled_file_name);
904 java_sources[0] = conftest_file_name;
905 if (!compile_using_envjavac (javac, java_sources, 1, tmpdir->dir_name,
906 false, false, false, true)
907 && stat (compiled_file_name, &statbuf) >= 0)
908 /* Compilation succeeded. */
909 envjavac_usable = true;
911 free (compiled_file_name);
912 free (conftest_file_name);
914 cleanup_temp_dir (tmpdir);
916 envjavac_tested = true;
919 *usablep = envjavac_usable;
920 return false;
923 /* Test whether $JAVAC, known to be a version of gcj < 4.3, can be used for
924 compiling with target_version = 1.4 and source_version = 1.3.
925 Return a failure indicator (true upon error). */
926 static bool
927 is_envjavac_oldgcj_14_13_usable (const char *javac,
928 bool *usablep, bool *need_no_assert_option_p)
930 static bool envjavac_tested;
931 static bool envjavac_usable;
932 static bool envjavac_need_no_assert_option;
934 if (!envjavac_tested)
936 /* Try $JAVAC and "$JAVAC -fno-assert". But add -fno-assert only if
937 it makes a difference. (It could already be part of $JAVAC.) */
938 struct temp_dir *tmpdir;
939 char *conftest_file_name;
940 char *compiled_file_name;
941 const char *java_sources[1];
942 struct stat statbuf;
943 bool javac_works;
944 char *javac_noassert;
945 bool javac_noassert_works;
947 tmpdir = create_temp_dir ("java", NULL, false);
948 if (tmpdir == NULL)
949 return true;
951 conftest_file_name =
952 xconcatenated_filename (tmpdir->dir_name, "conftest.java", NULL);
953 if (write_temp_file (tmpdir, conftest_file_name,
954 get_goodcode_snippet ("1.3")))
956 free (conftest_file_name);
957 cleanup_temp_dir (tmpdir);
958 return true;
961 compiled_file_name =
962 xconcatenated_filename (tmpdir->dir_name, "conftest.class", NULL);
963 register_temp_file (tmpdir, compiled_file_name);
965 java_sources[0] = conftest_file_name;
966 if (!compile_using_envjavac (javac,
967 java_sources, 1, tmpdir->dir_name,
968 false, false, false, true)
969 && stat (compiled_file_name, &statbuf) >= 0)
970 /* Compilation succeeded. */
971 javac_works = true;
972 else
973 javac_works = false;
975 unlink (compiled_file_name);
977 javac_noassert = xasprintf ("%s -fno-assert", javac);
979 java_sources[0] = conftest_file_name;
980 if (!compile_using_envjavac (javac_noassert,
981 java_sources, 1, tmpdir->dir_name,
982 false, false, false, true)
983 && stat (compiled_file_name, &statbuf) >= 0)
984 /* Compilation succeeded. */
985 javac_noassert_works = true;
986 else
987 javac_noassert_works = false;
989 free (compiled_file_name);
990 free (conftest_file_name);
992 if (javac_works && javac_noassert_works)
994 conftest_file_name =
995 xconcatenated_filename (tmpdir->dir_name, "conftestfail.java",
996 NULL);
997 if (write_temp_file (tmpdir, conftest_file_name,
998 get_failcode_snippet ("1.3")))
1000 free (conftest_file_name);
1001 free (javac_noassert);
1002 cleanup_temp_dir (tmpdir);
1003 return true;
1006 compiled_file_name =
1007 xconcatenated_filename (tmpdir->dir_name, "conftestfail.class",
1008 NULL);
1009 register_temp_file (tmpdir, compiled_file_name);
1011 java_sources[0] = conftest_file_name;
1012 if (!compile_using_envjavac (javac,
1013 java_sources, 1, tmpdir->dir_name,
1014 false, false, false, true)
1015 && stat (compiled_file_name, &statbuf) >= 0)
1017 /* Compilation succeeded. */
1018 unlink (compiled_file_name);
1020 java_sources[0] = conftest_file_name;
1021 if (!(!compile_using_envjavac (javac_noassert,
1022 java_sources, 1, tmpdir->dir_name,
1023 false, false, false, true)
1024 && stat (compiled_file_name, &statbuf) >= 0))
1025 /* Compilation failed. */
1026 /* "$JAVAC -fno-assert" works better than $JAVAC. */
1027 javac_works = true;
1030 free (compiled_file_name);
1031 free (conftest_file_name);
1034 cleanup_temp_dir (tmpdir);
1036 if (javac_works)
1038 envjavac_usable = true;
1039 envjavac_need_no_assert_option = false;
1041 else if (javac_noassert_works)
1043 envjavac_usable = true;
1044 envjavac_need_no_assert_option = true;
1047 envjavac_tested = true;
1050 *usablep = envjavac_usable;
1051 *need_no_assert_option_p = envjavac_need_no_assert_option;
1052 return false;
1055 /* Test whether $JAVAC, known to be not a version of gcj, can be used, and
1056 whether it needs a -source and/or -target option.
1057 Return a failure indicator (true upon error). */
1058 static bool
1059 is_envjavac_nongcj_usable (const char *javac,
1060 const char *source_version,
1061 const char *target_version,
1062 bool *usablep,
1063 bool *source_option_p, bool *target_option_p)
1065 /* The cache depends on the source_version and target_version. */
1066 struct result_t
1068 bool tested;
1069 bool usable;
1070 bool source_option;
1071 bool target_option;
1073 static struct result_t result_cache[SOURCE_VERSION_BOUND][TARGET_VERSION_BOUND];
1074 struct result_t *resultp;
1076 resultp = &result_cache[source_version_index (source_version)]
1077 [target_version_index (target_version)];
1078 if (!resultp->tested)
1080 /* Try $JAVAC. */
1081 struct temp_dir *tmpdir;
1082 char *conftest_file_name;
1083 char *compiled_file_name;
1084 const char *java_sources[1];
1085 struct stat statbuf;
1087 tmpdir = create_temp_dir ("java", NULL, false);
1088 if (tmpdir == NULL)
1089 return true;
1091 conftest_file_name =
1092 xconcatenated_filename (tmpdir->dir_name, "conftest.java", NULL);
1093 if (write_temp_file (tmpdir, conftest_file_name,
1094 get_goodcode_snippet (source_version)))
1096 free (conftest_file_name);
1097 cleanup_temp_dir (tmpdir);
1098 return true;
1101 compiled_file_name =
1102 xconcatenated_filename (tmpdir->dir_name, "conftest.class", NULL);
1103 register_temp_file (tmpdir, compiled_file_name);
1105 java_sources[0] = conftest_file_name;
1106 if (!compile_using_envjavac (javac,
1107 java_sources, 1, tmpdir->dir_name,
1108 false, false, false, true)
1109 && stat (compiled_file_name, &statbuf) >= 0
1110 && get_classfile_version (compiled_file_name)
1111 <= corresponding_classfile_version (target_version))
1113 /* $JAVAC compiled conftest.java successfully. */
1114 /* Try adding -source option if it is useful. */
1115 char *javac_source =
1116 xasprintf ("%s -source %s", javac, source_version);
1118 unlink (compiled_file_name);
1120 java_sources[0] = conftest_file_name;
1121 if (!compile_using_envjavac (javac_source,
1122 java_sources, 1, tmpdir->dir_name,
1123 false, false, false, true)
1124 && stat (compiled_file_name, &statbuf) >= 0
1125 && get_classfile_version (compiled_file_name)
1126 <= corresponding_classfile_version (target_version))
1128 const char *failcode = get_failcode_snippet (source_version);
1130 if (failcode != NULL)
1132 free (compiled_file_name);
1133 free (conftest_file_name);
1135 conftest_file_name =
1136 xconcatenated_filename (tmpdir->dir_name,
1137 "conftestfail.java",
1138 NULL);
1139 if (write_temp_file (tmpdir, conftest_file_name, failcode))
1141 free (conftest_file_name);
1142 free (javac_source);
1143 cleanup_temp_dir (tmpdir);
1144 return true;
1147 compiled_file_name =
1148 xconcatenated_filename (tmpdir->dir_name,
1149 "conftestfail.class",
1150 NULL);
1151 register_temp_file (tmpdir, compiled_file_name);
1153 java_sources[0] = conftest_file_name;
1154 if (!compile_using_envjavac (javac,
1155 java_sources, 1,
1156 tmpdir->dir_name,
1157 false, false, false, true)
1158 && stat (compiled_file_name, &statbuf) >= 0)
1160 unlink (compiled_file_name);
1162 java_sources[0] = conftest_file_name;
1163 if (compile_using_envjavac (javac_source,
1164 java_sources, 1,
1165 tmpdir->dir_name,
1166 false, false, false, true))
1167 /* $JAVAC compiled conftestfail.java successfully, and
1168 "$JAVAC -source $source_version" rejects it. So the
1169 -source option is useful. */
1170 resultp->source_option = true;
1175 free (javac_source);
1177 resultp->usable = true;
1179 else
1181 /* Try with -target option alone. (Sun javac 1.3.1 has the -target
1182 option but no -source option.) */
1183 char *javac_target =
1184 xasprintf ("%s -target %s", javac, target_version);
1186 unlink (compiled_file_name);
1188 java_sources[0] = conftest_file_name;
1189 if (!compile_using_envjavac (javac_target,
1190 java_sources, 1, tmpdir->dir_name,
1191 false, false, false, true)
1192 && stat (compiled_file_name, &statbuf) >= 0
1193 && get_classfile_version (compiled_file_name)
1194 <= corresponding_classfile_version (target_version))
1196 /* "$JAVAC -target $target_version" compiled conftest.java
1197 successfully. */
1198 /* Try adding -source option if it is useful. */
1199 char *javac_target_source =
1200 xasprintf ("%s -source %s", javac_target, source_version);
1202 unlink (compiled_file_name);
1204 java_sources[0] = conftest_file_name;
1205 if (!compile_using_envjavac (javac_target_source,
1206 java_sources, 1, tmpdir->dir_name,
1207 false, false, false, true)
1208 && stat (compiled_file_name, &statbuf) >= 0
1209 && get_classfile_version (compiled_file_name)
1210 <= corresponding_classfile_version (target_version))
1212 const char *failcode = get_failcode_snippet (source_version);
1214 if (failcode != NULL)
1216 free (compiled_file_name);
1217 free (conftest_file_name);
1219 conftest_file_name =
1220 xconcatenated_filename (tmpdir->dir_name,
1221 "conftestfail.java",
1222 NULL);
1223 if (write_temp_file (tmpdir, conftest_file_name,
1224 failcode))
1226 free (conftest_file_name);
1227 free (javac_target_source);
1228 free (javac_target);
1229 cleanup_temp_dir (tmpdir);
1230 return true;
1233 compiled_file_name =
1234 xconcatenated_filename (tmpdir->dir_name,
1235 "conftestfail.class",
1236 NULL);
1237 register_temp_file (tmpdir, compiled_file_name);
1239 java_sources[0] = conftest_file_name;
1240 if (!compile_using_envjavac (javac_target,
1241 java_sources, 1,
1242 tmpdir->dir_name,
1243 false, false, false, true)
1244 && stat (compiled_file_name, &statbuf) >= 0)
1246 unlink (compiled_file_name);
1248 java_sources[0] = conftest_file_name;
1249 if (compile_using_envjavac (javac_target_source,
1250 java_sources, 1,
1251 tmpdir->dir_name,
1252 false, false, false,
1253 true))
1254 /* "$JAVAC -target $target_version" compiled
1255 conftestfail.java successfully, and
1256 "$JAVAC -target $target_version -source $source_version"
1257 rejects it. So the -source option is useful. */
1258 resultp->source_option = true;
1263 free (javac_target_source);
1265 resultp->target_option = true;
1266 resultp->usable = true;
1268 else
1270 /* Maybe this -target option requires a -source option? Try with
1271 -target and -source options. (Supported by Sun javac 1.4 and
1272 higher.) */
1273 char *javac_target_source =
1274 xasprintf ("%s -source %s", javac_target, source_version);
1276 unlink (compiled_file_name);
1278 java_sources[0] = conftest_file_name;
1279 if (!compile_using_envjavac (javac_target_source,
1280 java_sources, 1, tmpdir->dir_name,
1281 false, false, false, true)
1282 && stat (compiled_file_name, &statbuf) >= 0
1283 && get_classfile_version (compiled_file_name)
1284 <= corresponding_classfile_version (target_version))
1286 /* "$JAVAC -target $target_version -source $source_version"
1287 compiled conftest.java successfully. */
1288 resultp->source_option = true;
1289 resultp->target_option = true;
1290 resultp->usable = true;
1293 free (javac_target_source);
1296 free (javac_target);
1299 free (compiled_file_name);
1300 free (conftest_file_name);
1302 resultp->tested = true;
1305 *usablep = resultp->usable;
1306 *source_option_p = resultp->source_option;
1307 *target_option_p = resultp->target_option;
1308 return false;
1311 static bool
1312 is_gcj_present (void)
1314 static bool gcj_tested;
1315 static bool gcj_present;
1317 if (!gcj_tested)
1319 /* Test for presence of gcj:
1320 "gcj --version 2> /dev/null | \
1321 sed -e 's,^[^0-9]*,,' -e 1q | \
1322 sed -e '/^3\.[01]/d' | grep '^[3-9]' > /dev/null" */
1323 char *argv[3];
1324 pid_t child;
1325 int fd[1];
1326 int exitstatus;
1328 argv[0] = "gcj";
1329 argv[1] = "--version";
1330 argv[2] = NULL;
1331 child = create_pipe_in ("gcj", "gcj", argv, DEV_NULL, true, true,
1332 false, fd);
1333 gcj_present = false;
1334 if (child != -1)
1336 /* Read the subprocess output, drop all lines except the first,
1337 drop all characters before the first digit, and test whether
1338 the remaining string starts with a digit >= 3, but not with
1339 "3.0" or "3.1". */
1340 char c[3];
1341 size_t count = 0;
1343 while (safe_read (fd[0], &c[count], 1) > 0)
1345 if (c[count] == '\n')
1346 break;
1347 if (count == 0)
1349 if (!(c[0] >= '0' && c[0] <= '9'))
1350 continue;
1351 gcj_present = (c[0] >= '3');
1353 count++;
1354 if (count == 3)
1356 if (c[0] == '3' && c[1] == '.'
1357 && (c[2] == '0' || c[2] == '1'))
1358 gcj_present = false;
1359 break;
1362 while (safe_read (fd[0], &c[0], 1) > 0)
1365 close (fd[0]);
1367 /* Remove zombie process from process list, and retrieve exit
1368 status. */
1369 exitstatus =
1370 wait_subprocess (child, "gcj", false, true, true, false, NULL);
1371 if (exitstatus != 0)
1372 gcj_present = false;
1375 if (gcj_present)
1377 /* See if libgcj.jar is well installed. */
1378 struct temp_dir *tmpdir;
1380 tmpdir = create_temp_dir ("java", NULL, false);
1381 if (tmpdir == NULL)
1382 gcj_present = false;
1383 else
1385 char *conftest_file_name;
1387 conftest_file_name =
1388 xconcatenated_filename (tmpdir->dir_name, "conftestlib.java",
1389 NULL);
1390 if (write_temp_file (tmpdir, conftest_file_name,
1391 "public class conftestlib {\n"
1392 " public static void main (String[] args) {\n"
1393 " }\n"
1394 "}\n"))
1395 gcj_present = false;
1396 else
1398 char *compiled_file_name;
1399 const char *java_sources[1];
1401 compiled_file_name =
1402 xconcatenated_filename (tmpdir->dir_name,
1403 "conftestlib.class",
1404 NULL);
1405 register_temp_file (tmpdir, compiled_file_name);
1407 java_sources[0] = conftest_file_name;
1408 if (compile_using_gcj (java_sources, 1, false,
1409 false, NULL, false, NULL,
1410 tmpdir->dir_name,
1411 false, false, false, true))
1412 gcj_present = false;
1414 free (compiled_file_name);
1416 free (conftest_file_name);
1418 cleanup_temp_dir (tmpdir);
1421 gcj_tested = true;
1424 return gcj_present;
1427 static bool
1428 is_gcj_43 (void)
1430 static bool gcj_tested;
1431 static bool gcj_43;
1433 if (!gcj_tested)
1435 /* Test for presence of gcj:
1436 "gcj --version 2> /dev/null | \
1437 sed -e 's,^[^0-9]*,,' -e 1q | \
1438 sed -e '/^4\.[012]/d' | grep '^[4-9]'" */
1439 char *argv[3];
1440 pid_t child;
1441 int fd[1];
1442 int exitstatus;
1444 argv[0] = "gcj";
1445 argv[1] = "--version";
1446 argv[2] = NULL;
1447 child = create_pipe_in ("gcj", "gcj", argv, DEV_NULL, true, true,
1448 false, fd);
1449 gcj_43 = false;
1450 if (child != -1)
1452 /* Read the subprocess output, drop all lines except the first,
1453 drop all characters before the first digit, and test whether
1454 the remaining string starts with a digit >= 4, but not with
1455 "4.0" or "4.1" or "4.2". */
1456 char c[3];
1457 size_t count = 0;
1459 while (safe_read (fd[0], &c[count], 1) > 0)
1461 if (c[count] == '\n')
1462 break;
1463 if (count == 0)
1465 if (!(c[0] >= '0' && c[0] <= '9'))
1466 continue;
1467 gcj_43 = (c[0] >= '4');
1469 count++;
1470 if (count == 3)
1472 if (c[0] == '4' && c[1] == '.' && c[2] >= '0' && c[2] <= '2')
1473 gcj_43 = false;
1474 break;
1477 while (safe_read (fd[0], &c[0], 1) > 0)
1480 close (fd[0]);
1482 /* Remove zombie process from process list, and retrieve exit
1483 status. */
1484 exitstatus =
1485 wait_subprocess (child, "gcj", false, true, true, false, NULL);
1486 if (exitstatus != 0)
1487 gcj_43 = false;
1490 gcj_tested = true;
1493 return gcj_43;
1496 /* Test whether gcj >= 4.3 can be used, and whether it needs a -fsource and/or
1497 -ftarget option.
1498 Return a failure indicator (true upon error). */
1499 static bool
1500 is_gcj43_usable (const char *source_version,
1501 const char *target_version,
1502 bool *usablep,
1503 bool *fsource_option_p, bool *ftarget_option_p)
1505 /* The cache depends on the source_version and target_version. */
1506 struct result_t
1508 bool tested;
1509 bool usable;
1510 bool fsource_option;
1511 bool ftarget_option;
1513 static struct result_t result_cache[SOURCE_VERSION_BOUND][TARGET_VERSION_BOUND];
1514 struct result_t *resultp;
1516 resultp = &result_cache[source_version_index (source_version)]
1517 [target_version_index (target_version)];
1518 if (!resultp->tested)
1520 /* Try gcj. */
1521 struct temp_dir *tmpdir;
1522 char *conftest_file_name;
1523 char *compiled_file_name;
1524 const char *java_sources[1];
1525 struct stat statbuf;
1527 tmpdir = create_temp_dir ("java", NULL, false);
1528 if (tmpdir == NULL)
1529 return true;
1531 conftest_file_name =
1532 xconcatenated_filename (tmpdir->dir_name, "conftest.java", NULL);
1533 if (write_temp_file (tmpdir, conftest_file_name,
1534 get_goodcode_snippet (source_version)))
1536 free (conftest_file_name);
1537 cleanup_temp_dir (tmpdir);
1538 return true;
1541 compiled_file_name =
1542 xconcatenated_filename (tmpdir->dir_name, "conftest.class", NULL);
1543 register_temp_file (tmpdir, compiled_file_name);
1545 java_sources[0] = conftest_file_name;
1546 if (!compile_using_gcj (java_sources, 1, false, false, NULL, false, NULL,
1547 tmpdir->dir_name, false, false, false, true)
1548 && stat (compiled_file_name, &statbuf) >= 0
1549 && get_classfile_version (compiled_file_name)
1550 <= corresponding_classfile_version (target_version))
1552 /* gcj compiled conftest.java successfully. */
1553 /* Try adding -fsource option if it is useful. */
1554 unlink (compiled_file_name);
1556 java_sources[0] = conftest_file_name;
1557 if (!compile_using_gcj (java_sources, 1,
1558 false, true, source_version, false, NULL,
1559 tmpdir->dir_name, false, false, false, true)
1560 && stat (compiled_file_name, &statbuf) >= 0
1561 && get_classfile_version (compiled_file_name)
1562 <= corresponding_classfile_version (target_version))
1564 const char *failcode = get_failcode_snippet (source_version);
1566 if (failcode != NULL)
1568 free (compiled_file_name);
1569 free (conftest_file_name);
1571 conftest_file_name =
1572 xconcatenated_filename (tmpdir->dir_name,
1573 "conftestfail.java",
1574 NULL);
1575 if (write_temp_file (tmpdir, conftest_file_name, failcode))
1577 free (conftest_file_name);
1578 cleanup_temp_dir (tmpdir);
1579 return true;
1582 compiled_file_name =
1583 xconcatenated_filename (tmpdir->dir_name,
1584 "conftestfail.class",
1585 NULL);
1586 register_temp_file (tmpdir, compiled_file_name);
1588 java_sources[0] = conftest_file_name;
1589 if (!compile_using_gcj (java_sources, 1,
1590 false, false, NULL, false, NULL,
1591 tmpdir->dir_name,
1592 false, false, false, true)
1593 && stat (compiled_file_name, &statbuf) >= 0)
1595 unlink (compiled_file_name);
1597 java_sources[0] = conftest_file_name;
1598 if (compile_using_gcj (java_sources, 1,
1599 false, true, source_version,
1600 false, NULL,
1601 tmpdir->dir_name,
1602 false, false, false, true))
1603 /* gcj compiled conftestfail.java successfully, and
1604 "gcj -fsource=$source_version" rejects it. So
1605 the -fsource option is useful. */
1606 resultp->fsource_option = true;
1611 resultp->usable = true;
1613 else
1615 /* Try with -fsource and -ftarget options. */
1616 unlink (compiled_file_name);
1618 java_sources[0] = conftest_file_name;
1619 if (!compile_using_gcj (java_sources, 1,
1620 false, true, source_version,
1621 true, target_version,
1622 tmpdir->dir_name,
1623 false, false, false, true)
1624 && stat (compiled_file_name, &statbuf) >= 0
1625 && get_classfile_version (compiled_file_name)
1626 <= corresponding_classfile_version (target_version))
1628 /* "gcj -fsource $source_version -ftarget $target_version"
1629 compiled conftest.java successfully. */
1630 resultp->fsource_option = true;
1631 resultp->ftarget_option = true;
1632 resultp->usable = true;
1636 free (compiled_file_name);
1637 free (conftest_file_name);
1639 resultp->tested = true;
1642 *usablep = resultp->usable;
1643 *fsource_option_p = resultp->fsource_option;
1644 *ftarget_option_p = resultp->ftarget_option;
1645 return false;
1648 /* Test whether gcj < 4.3 can be used for compiling with target_version = 1.4
1649 and source_version = 1.4.
1650 Return a failure indicator (true upon error). */
1651 static bool
1652 is_oldgcj_14_14_usable (bool *usablep)
1654 static bool gcj_tested;
1655 static bool gcj_usable;
1657 if (!gcj_tested)
1659 /* Try gcj. */
1660 struct temp_dir *tmpdir;
1661 char *conftest_file_name;
1662 char *compiled_file_name;
1663 const char *java_sources[1];
1664 struct stat statbuf;
1666 tmpdir = create_temp_dir ("java", NULL, false);
1667 if (tmpdir == NULL)
1668 return true;
1670 conftest_file_name =
1671 xconcatenated_filename (tmpdir->dir_name, "conftest.java", NULL);
1672 if (write_temp_file (tmpdir, conftest_file_name,
1673 get_goodcode_snippet ("1.4")))
1675 free (conftest_file_name);
1676 cleanup_temp_dir (tmpdir);
1677 return true;
1680 compiled_file_name =
1681 xconcatenated_filename (tmpdir->dir_name, "conftest.class", NULL);
1682 register_temp_file (tmpdir, compiled_file_name);
1684 java_sources[0] = conftest_file_name;
1685 if (!compile_using_gcj (java_sources, 1, false, false, NULL, false, NULL,
1686 tmpdir->dir_name, false, false, false, true)
1687 && stat (compiled_file_name, &statbuf) >= 0)
1688 /* Compilation succeeded. */
1689 gcj_usable = true;
1691 free (compiled_file_name);
1692 free (conftest_file_name);
1694 cleanup_temp_dir (tmpdir);
1696 gcj_tested = true;
1699 *usablep = gcj_usable;
1700 return false;
1703 /* Test whether gcj < 4.3 can be used for compiling with target_version = 1.4
1704 and source_version = 1.3.
1705 Return a failure indicator (true upon error). */
1706 static bool
1707 is_oldgcj_14_13_usable (bool *usablep, bool *need_no_assert_option_p)
1709 static bool gcj_tested;
1710 static bool gcj_usable;
1711 static bool gcj_need_no_assert_option;
1713 if (!gcj_tested)
1715 /* Try gcj and "gcj -fno-assert". But add -fno-assert only if
1716 it works (not gcj < 3.3). */
1717 struct temp_dir *tmpdir;
1718 char *conftest_file_name;
1719 char *compiled_file_name;
1720 const char *java_sources[1];
1721 struct stat statbuf;
1723 tmpdir = create_temp_dir ("java", NULL, false);
1724 if (tmpdir == NULL)
1725 return true;
1727 conftest_file_name =
1728 xconcatenated_filename (tmpdir->dir_name, "conftest.java", NULL);
1729 if (write_temp_file (tmpdir, conftest_file_name,
1730 get_goodcode_snippet ("1.3")))
1732 free (conftest_file_name);
1733 cleanup_temp_dir (tmpdir);
1734 return true;
1737 compiled_file_name =
1738 xconcatenated_filename (tmpdir->dir_name, "conftest.class", NULL);
1739 register_temp_file (tmpdir, compiled_file_name);
1741 java_sources[0] = conftest_file_name;
1742 if (!compile_using_gcj (java_sources, 1, true, false, NULL, false, NULL,
1743 tmpdir->dir_name, false, false, false, true)
1744 && stat (compiled_file_name, &statbuf) >= 0)
1745 /* Compilation succeeded. */
1747 gcj_usable = true;
1748 gcj_need_no_assert_option = true;
1750 else
1752 unlink (compiled_file_name);
1754 java_sources[0] = conftest_file_name;
1755 if (!compile_using_gcj (java_sources, 1, false,
1756 false, NULL, false, NULL,
1757 tmpdir->dir_name, false, false, false, true)
1758 && stat (compiled_file_name, &statbuf) >= 0)
1759 /* Compilation succeeded. */
1761 gcj_usable = true;
1762 gcj_need_no_assert_option = false;
1766 free (compiled_file_name);
1767 free (conftest_file_name);
1769 cleanup_temp_dir (tmpdir);
1771 gcj_tested = true;
1774 *usablep = gcj_usable;
1775 *need_no_assert_option_p = gcj_need_no_assert_option;
1776 return false;
1779 static bool
1780 is_javac_present (void)
1782 static bool javac_tested;
1783 static bool javac_present;
1785 if (!javac_tested)
1787 /* Test for presence of javac: "javac 2> /dev/null ; test $? -le 2" */
1788 char *argv[2];
1789 int exitstatus;
1791 argv[0] = "javac";
1792 argv[1] = NULL;
1793 exitstatus = execute ("javac", "javac", argv, false, false, true, true,
1794 true, false, NULL);
1795 javac_present = (exitstatus == 0 || exitstatus == 1 || exitstatus == 2);
1796 javac_tested = true;
1799 return javac_present;
1802 /* Test whether javac can be used and whether it needs a -source and/or
1803 -target option.
1804 Return a failure indicator (true upon error). */
1805 static bool
1806 is_javac_usable (const char *source_version, const char *target_version,
1807 bool *usablep, bool *source_option_p, bool *target_option_p)
1809 /* The cache depends on the source_version and target_version. */
1810 struct result_t
1812 bool tested;
1813 bool usable;
1814 bool source_option;
1815 bool target_option;
1817 static struct result_t result_cache[SOURCE_VERSION_BOUND][TARGET_VERSION_BOUND];
1818 struct result_t *resultp;
1820 resultp = &result_cache[source_version_index (source_version)]
1821 [target_version_index (target_version)];
1822 if (!resultp->tested)
1824 /* Try javac. */
1825 struct temp_dir *tmpdir;
1826 char *conftest_file_name;
1827 char *compiled_file_name;
1828 const char *java_sources[1];
1829 struct stat statbuf;
1831 tmpdir = create_temp_dir ("java", NULL, false);
1832 if (tmpdir == NULL)
1833 return true;
1835 conftest_file_name =
1836 xconcatenated_filename (tmpdir->dir_name, "conftest.java", NULL);
1837 if (write_temp_file (tmpdir, conftest_file_name,
1838 get_goodcode_snippet (source_version)))
1840 free (conftest_file_name);
1841 cleanup_temp_dir (tmpdir);
1842 return true;
1845 compiled_file_name =
1846 xconcatenated_filename (tmpdir->dir_name, "conftest.class", NULL);
1847 register_temp_file (tmpdir, compiled_file_name);
1849 java_sources[0] = conftest_file_name;
1850 if (!compile_using_javac (java_sources, 1,
1851 false, source_version,
1852 false, target_version,
1853 tmpdir->dir_name, false, false, false, true)
1854 && stat (compiled_file_name, &statbuf) >= 0
1855 && get_classfile_version (compiled_file_name)
1856 <= corresponding_classfile_version (target_version))
1858 /* javac compiled conftest.java successfully. */
1859 /* Try adding -source option if it is useful. */
1860 unlink (compiled_file_name);
1862 java_sources[0] = conftest_file_name;
1863 if (!compile_using_javac (java_sources, 1,
1864 true, source_version,
1865 false, target_version,
1866 tmpdir->dir_name, false, false, false, true)
1867 && stat (compiled_file_name, &statbuf) >= 0
1868 && get_classfile_version (compiled_file_name)
1869 <= corresponding_classfile_version (target_version))
1871 const char *failcode = get_failcode_snippet (source_version);
1873 if (failcode != NULL)
1875 free (compiled_file_name);
1876 free (conftest_file_name);
1878 conftest_file_name =
1879 xconcatenated_filename (tmpdir->dir_name,
1880 "conftestfail.java",
1881 NULL);
1882 if (write_temp_file (tmpdir, conftest_file_name, failcode))
1884 free (conftest_file_name);
1885 cleanup_temp_dir (tmpdir);
1886 return true;
1889 compiled_file_name =
1890 xconcatenated_filename (tmpdir->dir_name,
1891 "conftestfail.class",
1892 NULL);
1893 register_temp_file (tmpdir, compiled_file_name);
1895 java_sources[0] = conftest_file_name;
1896 if (!compile_using_javac (java_sources, 1,
1897 false, source_version,
1898 false, target_version,
1899 tmpdir->dir_name,
1900 false, false, false, true)
1901 && stat (compiled_file_name, &statbuf) >= 0)
1903 unlink (compiled_file_name);
1905 java_sources[0] = conftest_file_name;
1906 if (compile_using_javac (java_sources, 1,
1907 true, source_version,
1908 false, target_version,
1909 tmpdir->dir_name,
1910 false, false, false, true))
1911 /* javac compiled conftestfail.java successfully, and
1912 "javac -source $source_version" rejects it. So the
1913 -source option is useful. */
1914 resultp->source_option = true;
1919 resultp->usable = true;
1921 else
1923 /* Try with -target option alone. (Sun javac 1.3.1 has the -target
1924 option but no -source option.) */
1925 unlink (compiled_file_name);
1927 java_sources[0] = conftest_file_name;
1928 if (!compile_using_javac (java_sources, 1,
1929 false, source_version,
1930 true, target_version,
1931 tmpdir->dir_name,
1932 false, false, false, true)
1933 && stat (compiled_file_name, &statbuf) >= 0
1934 && get_classfile_version (compiled_file_name)
1935 <= corresponding_classfile_version (target_version))
1937 /* "javac -target $target_version" compiled conftest.java
1938 successfully. */
1939 /* Try adding -source option if it is useful. */
1940 unlink (compiled_file_name);
1942 java_sources[0] = conftest_file_name;
1943 if (!compile_using_javac (java_sources, 1,
1944 true, source_version,
1945 true, target_version,
1946 tmpdir->dir_name,
1947 false, false, false, true)
1948 && stat (compiled_file_name, &statbuf) >= 0
1949 && get_classfile_version (compiled_file_name)
1950 <= corresponding_classfile_version (target_version))
1952 const char *failcode = get_failcode_snippet (source_version);
1954 if (failcode != NULL)
1956 free (compiled_file_name);
1957 free (conftest_file_name);
1959 conftest_file_name =
1960 xconcatenated_filename (tmpdir->dir_name,
1961 "conftestfail.java",
1962 NULL);
1963 if (write_temp_file (tmpdir, conftest_file_name,
1964 failcode))
1966 free (conftest_file_name);
1967 cleanup_temp_dir (tmpdir);
1968 return true;
1971 compiled_file_name =
1972 xconcatenated_filename (tmpdir->dir_name,
1973 "conftestfail.class",
1974 NULL);
1975 register_temp_file (tmpdir, compiled_file_name);
1977 java_sources[0] = conftest_file_name;
1978 if (!compile_using_javac (java_sources, 1,
1979 false, source_version,
1980 true, target_version,
1981 tmpdir->dir_name,
1982 false, false, false, true)
1983 && stat (compiled_file_name, &statbuf) >= 0)
1985 unlink (compiled_file_name);
1987 java_sources[0] = conftest_file_name;
1988 if (compile_using_javac (java_sources, 1,
1989 true, source_version,
1990 true, target_version,
1991 tmpdir->dir_name,
1992 false, false, false, true))
1993 /* "javac -target $target_version" compiled
1994 conftestfail.java successfully, and
1995 "javac -target $target_version -source $source_version"
1996 rejects it. So the -source option is useful. */
1997 resultp->source_option = true;
2002 resultp->target_option = true;
2003 resultp->usable = true;
2005 else
2007 /* Maybe this -target option requires a -source option? Try with
2008 -target and -source options. (Supported by Sun javac 1.4 and
2009 higher.) */
2010 unlink (compiled_file_name);
2012 java_sources[0] = conftest_file_name;
2013 if (!compile_using_javac (java_sources, 1,
2014 true, source_version,
2015 true, target_version,
2016 tmpdir->dir_name,
2017 false, false, false, true)
2018 && stat (compiled_file_name, &statbuf) >= 0
2019 && get_classfile_version (compiled_file_name)
2020 <= corresponding_classfile_version (target_version))
2022 /* "javac -target $target_version -source $source_version"
2023 compiled conftest.java successfully. */
2024 resultp->source_option = true;
2025 resultp->target_option = true;
2026 resultp->usable = true;
2031 free (compiled_file_name);
2032 free (conftest_file_name);
2034 resultp->tested = true;
2037 *usablep = resultp->usable;
2038 *source_option_p = resultp->source_option;
2039 *target_option_p = resultp->target_option;
2040 return false;
2043 static bool
2044 is_jikes_present (void)
2046 static bool jikes_tested;
2047 static bool jikes_present;
2049 if (!jikes_tested)
2051 /* Test for presence of jikes: "jikes 2> /dev/null ; test $? = 1" */
2052 char *argv[2];
2053 int exitstatus;
2055 argv[0] = "jikes";
2056 argv[1] = NULL;
2057 exitstatus = execute ("jikes", "jikes", argv, false, false, true, true,
2058 true, false, NULL);
2059 jikes_present = (exitstatus == 0 || exitstatus == 1);
2060 jikes_tested = true;
2063 return jikes_present;
2066 /* ============================= Main function ============================= */
2068 bool
2069 compile_java_class (const char * const *java_sources,
2070 unsigned int java_sources_count,
2071 const char * const *classpaths,
2072 unsigned int classpaths_count,
2073 const char *source_version,
2074 const char *target_version,
2075 const char *directory,
2076 bool optimize, bool debug,
2077 bool use_minimal_classpath,
2078 bool verbose)
2080 bool err = false;
2081 char *old_JAVA_HOME;
2084 const char *javac = getenv ("JAVAC");
2085 if (javac != NULL && javac[0] != '\0')
2087 bool usable = false;
2088 bool no_assert_option = false;
2089 bool source_option = false;
2090 bool target_option = false;
2091 bool fsource_option = false;
2092 bool ftarget_option = false;
2094 if (target_version == NULL)
2095 target_version = default_target_version ();
2097 if (is_envjavac_gcj (javac))
2099 /* It's a version of gcj. */
2100 if (is_envjavac_gcj43 (javac))
2102 /* It's a version of gcj >= 4.3. Assume the classfile versions
2103 are correct. */
2104 if (is_envjavac_gcj43_usable (javac,
2105 source_version, target_version,
2106 &usable,
2107 &fsource_option, &ftarget_option))
2109 err = true;
2110 goto done1;
2113 else
2115 /* It's a version of gcj < 4.3. Ignore the version of the
2116 class files that it creates. */
2117 if (strcmp (target_version, "1.4") == 0
2118 && strcmp (source_version, "1.4") == 0)
2120 if (is_envjavac_oldgcj_14_14_usable (javac, &usable))
2122 err = true;
2123 goto done1;
2126 else if (strcmp (target_version, "1.4") == 0
2127 && strcmp (source_version, "1.3") == 0)
2129 if (is_envjavac_oldgcj_14_13_usable (javac,
2130 &usable,
2131 &no_assert_option))
2133 err = true;
2134 goto done1;
2139 else
2141 /* It's not gcj. Assume the classfile versions are correct. */
2142 if (is_envjavac_nongcj_usable (javac,
2143 source_version, target_version,
2144 &usable,
2145 &source_option, &target_option))
2147 err = true;
2148 goto done1;
2152 if (usable)
2154 char *old_classpath;
2155 char *javac_with_options;
2157 /* Set CLASSPATH. */
2158 old_classpath =
2159 set_classpath (classpaths, classpaths_count, false, verbose);
2161 javac_with_options =
2162 (no_assert_option
2163 ? xasprintf ("%s -fno-assert", javac)
2164 : xasprintf ("%s%s%s%s%s%s%s%s%s",
2165 javac,
2166 source_option ? " -source " : "",
2167 source_option ? source_version : "",
2168 target_option ? " -target " : "",
2169 target_option ? target_version : "",
2170 fsource_option ? " -fsource=" : "",
2171 fsource_option ? source_version : "",
2172 ftarget_option ? " -ftarget=" : "",
2173 ftarget_option ? target_version : ""));
2175 err = compile_using_envjavac (javac_with_options,
2176 java_sources, java_sources_count,
2177 directory, optimize, debug, verbose,
2178 false);
2180 free (javac_with_options);
2182 /* Reset CLASSPATH. */
2183 reset_classpath (old_classpath);
2185 goto done1;
2190 /* Unset the JAVA_HOME environment variable. */
2191 old_JAVA_HOME = getenv ("JAVA_HOME");
2192 if (old_JAVA_HOME != NULL)
2194 old_JAVA_HOME = xstrdup (old_JAVA_HOME);
2195 unsetenv ("JAVA_HOME");
2198 if (is_gcj_present ())
2200 /* It's a version of gcj. */
2201 bool usable = false;
2202 bool no_assert_option = false;
2203 bool fsource_option = false;
2204 bool ftarget_option = false;
2206 if (target_version == NULL)
2207 target_version = default_target_version ();
2209 if (is_gcj_43 ())
2211 /* It's a version of gcj >= 4.3. Assume the classfile versions
2212 are correct. */
2213 if (is_gcj43_usable (source_version, target_version,
2214 &usable, &fsource_option, &ftarget_option))
2216 err = true;
2217 goto done1;
2220 else
2222 /* It's a version of gcj < 4.3. Ignore the version of the class
2223 files that it creates.
2224 Test whether it supports the desired target-version and
2225 source-version. */
2226 if (strcmp (target_version, "1.4") == 0
2227 && strcmp (source_version, "1.4") == 0)
2229 if (is_oldgcj_14_14_usable (&usable))
2231 err = true;
2232 goto done1;
2235 else if (strcmp (target_version, "1.4") == 0
2236 && strcmp (source_version, "1.3") == 0)
2238 if (is_oldgcj_14_13_usable (&usable, &no_assert_option))
2240 err = true;
2241 goto done1;
2246 if (usable)
2248 char *old_classpath;
2250 /* Set CLASSPATH. We could also use the --CLASSPATH=... option
2251 of gcj. Note that --classpath=... option is different: its
2252 argument should also contain gcj's libgcj.jar, but we don't
2253 know its location. */
2254 old_classpath =
2255 set_classpath (classpaths, classpaths_count, use_minimal_classpath,
2256 verbose);
2258 err = compile_using_gcj (java_sources, java_sources_count,
2259 no_assert_option,
2260 fsource_option, source_version,
2261 ftarget_option, target_version,
2262 directory, optimize, debug, verbose, false);
2264 /* Reset CLASSPATH. */
2265 reset_classpath (old_classpath);
2267 goto done2;
2271 if (is_javac_present ())
2273 bool usable = false;
2274 bool source_option = false;
2275 bool target_option = false;
2277 if (target_version == NULL)
2278 target_version = default_target_version ();
2280 if (is_javac_usable (source_version, target_version,
2281 &usable, &source_option, &target_option))
2283 err = true;
2284 goto done1;
2287 if (usable)
2289 char *old_classpath;
2291 /* Set CLASSPATH. We don't use the "-classpath ..." option because
2292 in JDK 1.1.x its argument should also contain the JDK's
2293 classes.zip, but we don't know its location. (In JDK 1.3.0 it
2294 would work.) */
2295 old_classpath =
2296 set_classpath (classpaths, classpaths_count, use_minimal_classpath,
2297 verbose);
2299 err = compile_using_javac (java_sources, java_sources_count,
2300 source_option, source_version,
2301 target_option, target_version,
2302 directory, optimize, debug, verbose,
2303 false);
2305 /* Reset CLASSPATH. */
2306 reset_classpath (old_classpath);
2308 goto done2;
2312 if (is_jikes_present ())
2314 /* Test whether it supports the desired target-version and
2315 source-version. */
2316 bool usable = (strcmp (source_version, "1.3") == 0);
2318 if (usable)
2320 char *old_classpath;
2322 /* Set CLASSPATH. We could also use the "-classpath ..." option.
2323 Since jikes doesn't come with its own standard library, it
2324 needs a classes.zip or rt.jar or libgcj.jar in the CLASSPATH.
2325 To increase the chance of success, we reuse the current CLASSPATH
2326 if the user has set it. */
2327 old_classpath =
2328 set_classpath (classpaths, classpaths_count, false, verbose);
2330 err = compile_using_jikes (java_sources, java_sources_count,
2331 directory, optimize, debug, verbose,
2332 false);
2334 /* Reset CLASSPATH. */
2335 reset_classpath (old_classpath);
2337 goto done2;
2341 error (0, 0, _("Java compiler not found, try installing gcj or set $JAVAC"));
2342 err = true;
2344 done2:
2345 if (old_JAVA_HOME != NULL)
2347 xsetenv ("JAVA_HOME", old_JAVA_HOME, 1);
2348 free (old_JAVA_HOME);
2351 done1:
2352 return err;