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/>. */
30 #include <sys/types.h>
33 #include "javaversion.h"
35 #include "spawn-pipe.h"
36 #include "wait-process.h"
37 #include "classpath.h"
40 #include "binary-io.h"
41 #include "safe-read.h"
44 #include "concat-filename.h"
45 #include "fwriteerror.h"
46 #include "clean-temp.h"
48 #include "xvasprintf.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
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
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. */
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 */
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"));
125 /* Return a snippet of code that should compile in the given source version. */
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"));
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
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)
151 error (EXIT_FAILURE
, 0, _("invalid source_version argument to compile_java_class"));
155 /* ======================= Target version dependent ======================= */
157 /* Convert a target version to an index. */
158 #define TARGET_VERSION_BOUND 6 /* exclusive upper bound */
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"));
170 /* Return the class file version number corresponding to a given target
173 corresponding_classfile_version (const char *target_version
)
175 if (strcmp (target_version
, "1.1") == 0)
177 if (strcmp (target_version
, "1.2") == 0)
179 if (strcmp (target_version
, "1.3") == 0)
181 if (strcmp (target_version
, "1.4") == 0)
183 if (strcmp (target_version
, "1.5") == 0)
185 if (strcmp (target_version
, "1.6") == 0)
187 error (EXIT_FAILURE
, 0, _("invalid target_version argument to compile_java_class"));
191 /* ======================== Compilation subroutines ======================== */
193 /* Try to compile a set of Java sources with $JAVAC.
194 Return a failure indicator (true upon error). */
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. */
208 unsigned int command_length
;
215 command_length
= strlen (javac
);
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
]);
226 command
= (char *) xmalloca (command_length
);
228 /* Don't shell_quote $JAVAC, because it may consist of a command
230 memcpy (p
, javac
, strlen (javac
));
234 memcpy (p
, " -O", 3);
239 memcpy (p
, " -g", 3);
242 if (directory
!= NULL
)
244 memcpy (p
, " -d ", 4);
246 p
= shell_quote_copy (p
, directory
);
248 for (i
= 0; i
< java_sources_count
; i
++)
251 p
= shell_quote_copy (p
, java_sources
[i
]);
254 /* Ensure command_length was correctly calculated. */
255 if (p
- command
> command_length
)
259 printf ("%s\n", command
);
265 exitstatus
= execute (javac
, "/bin/sh", argv
, false, false, false,
266 null_stderr
, true, true, NULL
);
267 err
= (exitstatus
!= 0);
274 /* Try to compile a set of Java sources with gcj.
275 Return a failure indicator (true upon error). */
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
)
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 *));
304 if (no_assert_option
)
305 *argp
++ = "-fno-assert";
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
;
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
;
328 if (directory
!= NULL
)
331 *argp
++ = (char *) directory
;
333 for (i
= 0; i
< java_sources_count
; i
++)
334 *argp
++ = (char *) java_sources
[i
];
336 /* Ensure argv length was correctly calculated. */
337 if (argp
- argv
!= argc
)
342 char *command
= shell_quote_argv (argv
);
343 printf ("%s\n", command
);
347 exitstatus
= execute ("gcj", "gcj", argv
, false, false, false, null_stderr
,
349 err
= (exitstatus
!= 0);
351 if (ftarget_arg
!= NULL
)
353 if (fsource_arg
!= NULL
)
360 /* Try to compile a set of Java sources with javac.
361 Return a failure indicator (true upon error). */
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
)
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 *));
388 *argp
++ = (char *) source_version
;
393 *argp
++ = (char *) target_version
;
399 if (directory
!= NULL
)
402 *argp
++ = (char *) directory
;
404 for (i
= 0; i
< java_sources_count
; i
++)
405 *argp
++ = (char *) java_sources
[i
];
407 /* Ensure argv length was correctly calculated. */
408 if (argp
- argv
!= argc
)
413 char *command
= shell_quote_argv (argv
);
414 printf ("%s\n", command
);
418 exitstatus
= execute ("javac", "javac", argv
, false, false, false,
419 null_stderr
, true, true, NULL
);
420 err
= (exitstatus
!= 0);
427 /* Try to compile a set of Java sources with jikes.
428 Return a failure indicator (true upon error). */
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
)
444 1 + (optimize
? 1 : 0) + (debug
? 1 : 0) + (directory
!= NULL
? 2 : 0)
445 + java_sources_count
;
446 argv
= (char **) xmalloca ((argc
+ 1) * sizeof (char *));
454 if (directory
!= NULL
)
457 *argp
++ = (char *) directory
;
459 for (i
= 0; i
< java_sources_count
; i
++)
460 *argp
++ = (char *) java_sources
[i
];
462 /* Ensure argv length was correctly calculated. */
463 if (argp
- argv
!= argc
)
468 char *command
= shell_quote_argv (argv
);
469 printf ("%s\n", command
);
473 exitstatus
= execute ("jikes", "jikes", argv
, false, false, false,
474 null_stderr
, true, true, NULL
);
475 err
= (exitstatus
!= 0);
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
487 Return a failure indicator (true upon error). */
489 write_temp_file (struct temp_dir
*tmpdir
, const char *file_name
,
490 const char *contents
)
494 register_temp_file (tmpdir
, file_name
);
495 fp
= fopen_temp (file_name
, "w");
498 error (0, errno
, _("failed to create \"%s\""), file_name
);
499 unregister_temp_file (tmpdir
, file_name
);
502 fputs (contents
, fp
);
503 if (fwriteerror_temp (fp
))
505 error (0, errno
, _("error while writing \"%s\" file"), file_name
);
511 /* Return the class file version number of a class file on disk. */
513 get_classfile_version (const char *compiled_file_name
)
515 unsigned char header
[8];
518 /* Open the class file. */
519 fd
= open (compiled_file_name
, O_RDONLY
| O_BINARY
, 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)
536 /* Could not get the class file version. Return a very large one. */
540 /* Return true if $JAVAC is a version of gcj. */
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
;
563 /* Setup the command "$JAVAC --version". */
564 command_length
= strlen (javac
) + 1 + 9 + 1;
565 command
= (char *) xmalloca (command_length
);
567 /* Don't shell_quote $JAVAC, because it may consist of a command
569 memcpy (p
, javac
, strlen (javac
));
571 memcpy (p
, " --version", 1 + 9 + 1);
573 /* Ensure command_length was correctly calculated. */
574 if (p
- command
> command_length
)
577 /* Call $JAVAC --version 2>/dev/null. */
582 child
= create_pipe_in (javac
, "/bin/sh", argv
, DEV_NULL
, true, true,
587 /* Retrieve its result. */
588 fp
= fdopen (fd
[0], "r");
592 line
= NULL
; linesize
= 0;
593 linelen
= getline (&line
, &linesize
, fp
);
594 if (linelen
== (size_t)(-1))
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
);
605 /* Remove zombie process from process list, and retrieve exit status. */
607 wait_subprocess (child
, javac
, true, true, true, false, NULL
);
609 envjavac_gcj
= false;
614 envjavac_tested
= true;
620 /* Return true if $JAVAC, known to be a version of gcj, is a version >= 4.3
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
;
645 /* Setup the command "$JAVAC --version". */
646 command_length
= strlen (javac
) + 1 + 9 + 1;
647 command
= (char *) xmalloca (command_length
);
649 /* Don't shell_quote $JAVAC, because it may consist of a command
651 memcpy (p
, javac
, strlen (javac
));
653 memcpy (p
, " --version", 1 + 9 + 1);
655 /* Ensure command_length was correctly calculated. */
656 if (p
- command
> command_length
)
659 /* Call $JAVAC --version 2>/dev/null. */
664 child
= create_pipe_in (javac
, "/bin/sh", argv
, DEV_NULL
, true, true,
669 /* Retrieve its result. */
670 fp
= fdopen (fd
[0], "r");
674 line
= NULL
; linesize
= 0;
675 linelen
= getline (&line
, &linesize
, fp
);
676 if (linelen
== (size_t)(-1))
682 while (*p
!= '\0' && !(*p
>= '0' && *p
<= '9'))
685 !(*p
== '4' && p
[1] == '.' && p
[2] >= '0' && p
[2] <= '2')
686 && (*p
>= '4' && *p
<= '9');
690 /* Remove zombie process from process list, and retrieve exit status. */
692 wait_subprocess (child
, javac
, true, true, true, false, NULL
);
694 envjavac_gcj43
= false;
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). */
709 is_envjavac_gcj43_usable (const char *javac
,
710 const char *source_version
,
711 const char *target_version
,
713 bool *fsource_option_p
, bool *ftarget_option_p
)
715 /* The cache depends on the source_version and target_version. */
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
)
731 struct temp_dir
*tmpdir
;
732 char *conftest_file_name
;
733 char *compiled_file_name
;
734 const char *java_sources
[1];
737 tmpdir
= create_temp_dir ("java", NULL
, false);
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
);
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. */
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
);
786 xconcatenated_filename (tmpdir
->dir_name
,
789 if (write_temp_file (tmpdir
, conftest_file_name
, failcode
))
791 free (conftest_file_name
);
793 cleanup_temp_dir (tmpdir
);
798 xconcatenated_filename (tmpdir
->dir_name
,
799 "conftestfail.class",
801 register_temp_file (tmpdir
, compiled_file_name
);
803 java_sources
[0] = conftest_file_name
;
804 if (!compile_using_envjavac (javac
,
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
,
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;
827 resultp
->usable
= true;
831 /* Try with -fsource and -ftarget options. */
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;
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
;
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). */
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
)
880 struct temp_dir
*tmpdir
;
881 char *conftest_file_name
;
882 char *compiled_file_name
;
883 const char *java_sources
[1];
886 tmpdir
= create_temp_dir ("java", NULL
, false);
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
);
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
;
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). */
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];
944 char *javac_noassert
;
945 bool javac_noassert_works
;
947 tmpdir
= create_temp_dir ("java", NULL
, false);
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
);
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. */
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;
987 javac_noassert_works
= false;
989 free (compiled_file_name
);
990 free (conftest_file_name
);
992 if (javac_works
&& javac_noassert_works
)
995 xconcatenated_filename (tmpdir
->dir_name
, "conftestfail.java",
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
);
1006 compiled_file_name
=
1007 xconcatenated_filename (tmpdir
->dir_name
, "conftestfail.class",
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. */
1030 free (compiled_file_name
);
1031 free (conftest_file_name
);
1034 cleanup_temp_dir (tmpdir
);
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
;
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). */
1059 is_envjavac_nongcj_usable (const char *javac
,
1060 const char *source_version
,
1061 const char *target_version
,
1063 bool *source_option_p
, bool *target_option_p
)
1065 /* The cache depends on the source_version and target_version. */
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
)
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);
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
);
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",
1139 if (write_temp_file (tmpdir
, conftest_file_name
, failcode
))
1141 free (conftest_file_name
);
1142 free (javac_source
);
1143 cleanup_temp_dir (tmpdir
);
1147 compiled_file_name
=
1148 xconcatenated_filename (tmpdir
->dir_name
,
1149 "conftestfail.class",
1151 register_temp_file (tmpdir
, compiled_file_name
);
1153 java_sources
[0] = conftest_file_name
;
1154 if (!compile_using_envjavac (javac
,
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
,
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;
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
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",
1223 if (write_temp_file (tmpdir
, conftest_file_name
,
1226 free (conftest_file_name
);
1227 free (javac_target_source
);
1228 free (javac_target
);
1229 cleanup_temp_dir (tmpdir
);
1233 compiled_file_name
=
1234 xconcatenated_filename (tmpdir
->dir_name
,
1235 "conftestfail.class",
1237 register_temp_file (tmpdir
, compiled_file_name
);
1239 java_sources
[0] = conftest_file_name
;
1240 if (!compile_using_envjavac (javac_target
,
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
,
1252 false, false, false,
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;
1270 /* Maybe this -target option requires a -source option? Try with
1271 -target and -source options. (Supported by Sun javac 1.4 and
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
;
1312 is_gcj_present (void)
1314 static bool gcj_tested
;
1315 static bool gcj_present
;
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" */
1329 argv
[1] = "--version";
1331 child
= create_pipe_in ("gcj", "gcj", argv
, DEV_NULL
, true, true,
1333 gcj_present
= false;
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
1343 while (safe_read (fd
[0], &c
[count
], 1) > 0)
1345 if (c
[count
] == '\n')
1349 if (!(c
[0] >= '0' && c
[0] <= '9'))
1351 gcj_present
= (c
[0] >= '3');
1356 if (c
[0] == '3' && c
[1] == '.'
1357 && (c
[2] == '0' || c
[2] == '1'))
1358 gcj_present
= false;
1362 while (safe_read (fd
[0], &c
[0], 1) > 0)
1367 /* Remove zombie process from process list, and retrieve exit
1370 wait_subprocess (child
, "gcj", false, true, true, false, NULL
);
1371 if (exitstatus
!= 0)
1372 gcj_present
= false;
1377 /* See if libgcj.jar is well installed. */
1378 struct temp_dir
*tmpdir
;
1380 tmpdir
= create_temp_dir ("java", NULL
, false);
1382 gcj_present
= false;
1385 char *conftest_file_name
;
1387 conftest_file_name
=
1388 xconcatenated_filename (tmpdir
->dir_name
, "conftestlib.java",
1390 if (write_temp_file (tmpdir
, conftest_file_name
,
1391 "public class conftestlib {\n"
1392 " public static void main (String[] args) {\n"
1395 gcj_present
= false;
1398 char *compiled_file_name
;
1399 const char *java_sources
[1];
1401 compiled_file_name
=
1402 xconcatenated_filename (tmpdir
->dir_name
,
1403 "conftestlib.class",
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
,
1411 false, false, false, true))
1412 gcj_present
= false;
1414 free (compiled_file_name
);
1416 free (conftest_file_name
);
1418 cleanup_temp_dir (tmpdir
);
1430 static bool 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]'" */
1445 argv
[1] = "--version";
1447 child
= create_pipe_in ("gcj", "gcj", argv
, DEV_NULL
, true, true,
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". */
1459 while (safe_read (fd
[0], &c
[count
], 1) > 0)
1461 if (c
[count
] == '\n')
1465 if (!(c
[0] >= '0' && c
[0] <= '9'))
1467 gcj_43
= (c
[0] >= '4');
1472 if (c
[0] == '4' && c
[1] == '.' && c
[2] >= '0' && c
[2] <= '2')
1477 while (safe_read (fd
[0], &c
[0], 1) > 0)
1482 /* Remove zombie process from process list, and retrieve exit
1485 wait_subprocess (child
, "gcj", false, true, true, false, NULL
);
1486 if (exitstatus
!= 0)
1496 /* Test whether gcj >= 4.3 can be used, and whether it needs a -fsource and/or
1498 Return a failure indicator (true upon error). */
1500 is_gcj43_usable (const char *source_version
,
1501 const char *target_version
,
1503 bool *fsource_option_p
, bool *ftarget_option_p
)
1505 /* The cache depends on the source_version and target_version. */
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
)
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);
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
);
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",
1575 if (write_temp_file (tmpdir
, conftest_file_name
, failcode
))
1577 free (conftest_file_name
);
1578 cleanup_temp_dir (tmpdir
);
1582 compiled_file_name
=
1583 xconcatenated_filename (tmpdir
->dir_name
,
1584 "conftestfail.class",
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
,
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
,
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;
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
,
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
;
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). */
1652 is_oldgcj_14_14_usable (bool *usablep
)
1654 static bool gcj_tested
;
1655 static bool gcj_usable
;
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);
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
);
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. */
1691 free (compiled_file_name
);
1692 free (conftest_file_name
);
1694 cleanup_temp_dir (tmpdir
);
1699 *usablep
= gcj_usable
;
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). */
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
;
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);
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
);
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. */
1748 gcj_need_no_assert_option
= true;
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. */
1762 gcj_need_no_assert_option
= false;
1766 free (compiled_file_name
);
1767 free (conftest_file_name
);
1769 cleanup_temp_dir (tmpdir
);
1774 *usablep
= gcj_usable
;
1775 *need_no_assert_option_p
= gcj_need_no_assert_option
;
1780 is_javac_present (void)
1782 static bool javac_tested
;
1783 static bool javac_present
;
1787 /* Test for presence of javac: "javac 2> /dev/null ; test $? -le 2" */
1793 exitstatus
= execute ("javac", "javac", argv
, false, false, true, true,
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
1804 Return a failure indicator (true upon error). */
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. */
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
)
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);
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
);
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",
1882 if (write_temp_file (tmpdir
, conftest_file_name
, failcode
))
1884 free (conftest_file_name
);
1885 cleanup_temp_dir (tmpdir
);
1889 compiled_file_name
=
1890 xconcatenated_filename (tmpdir
->dir_name
,
1891 "conftestfail.class",
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
,
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
,
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;
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
,
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
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
,
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",
1963 if (write_temp_file (tmpdir
, conftest_file_name
,
1966 free (conftest_file_name
);
1967 cleanup_temp_dir (tmpdir
);
1971 compiled_file_name
=
1972 xconcatenated_filename (tmpdir
->dir_name
,
1973 "conftestfail.class",
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
,
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
,
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;
2007 /* Maybe this -target option requires a -source option? Try with
2008 -target and -source options. (Supported by Sun javac 1.4 and
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
,
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
;
2044 is_jikes_present (void)
2046 static bool jikes_tested
;
2047 static bool jikes_present
;
2051 /* Test for presence of jikes: "jikes 2> /dev/null ; test $? = 1" */
2057 exitstatus
= execute ("jikes", "jikes", argv
, false, false, true, true,
2059 jikes_present
= (exitstatus
== 0 || exitstatus
== 1);
2060 jikes_tested
= true;
2063 return jikes_present
;
2066 /* ============================= Main function ============================= */
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
,
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
2104 if (is_envjavac_gcj43_usable (javac
,
2105 source_version
, target_version
,
2107 &fsource_option
, &ftarget_option
))
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
))
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
,
2141 /* It's not gcj. Assume the classfile versions are correct. */
2142 if (is_envjavac_nongcj_usable (javac
,
2143 source_version
, target_version
,
2145 &source_option
, &target_option
))
2154 char *old_classpath
;
2155 char *javac_with_options
;
2157 /* Set CLASSPATH. */
2159 set_classpath (classpaths
, classpaths_count
, false, verbose
);
2161 javac_with_options
=
2163 ? xasprintf ("%s -fno-assert", javac
)
2164 : xasprintf ("%s%s%s%s%s%s%s%s%s",
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
,
2180 free (javac_with_options
);
2182 /* Reset CLASSPATH. */
2183 reset_classpath (old_classpath
);
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 ();
2211 /* It's a version of gcj >= 4.3. Assume the classfile versions
2213 if (is_gcj43_usable (source_version
, target_version
,
2214 &usable
, &fsource_option
, &ftarget_option
))
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
2226 if (strcmp (target_version
, "1.4") == 0
2227 && strcmp (source_version
, "1.4") == 0)
2229 if (is_oldgcj_14_14_usable (&usable
))
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
))
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. */
2255 set_classpath (classpaths
, classpaths_count
, use_minimal_classpath
,
2258 err
= compile_using_gcj (java_sources
, java_sources_count
,
2260 fsource_option
, source_version
,
2261 ftarget_option
, target_version
,
2262 directory
, optimize
, debug
, verbose
, false);
2264 /* Reset CLASSPATH. */
2265 reset_classpath (old_classpath
);
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
))
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
2296 set_classpath (classpaths
, classpaths_count
, use_minimal_classpath
,
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
,
2305 /* Reset CLASSPATH. */
2306 reset_classpath (old_classpath
);
2312 if (is_jikes_present ())
2314 /* Test whether it supports the desired target-version and
2316 bool usable
= (strcmp (source_version
, "1.3") == 0);
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. */
2328 set_classpath (classpaths
, classpaths_count
, false, verbose
);
2330 err
= compile_using_jikes (java_sources
, java_sources_count
,
2331 directory
, optimize
, debug
, verbose
,
2334 /* Reset CLASSPATH. */
2335 reset_classpath (old_classpath
);
2341 error (0, 0, _("Java compiler not found, try installing gcj or set $JAVAC"));
2345 if (old_JAVA_HOME
!= NULL
)
2347 xsetenv ("JAVA_HOME", old_JAVA_HOME
, 1);
2348 free (old_JAVA_HOME
);