diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE index 7cfa929e0b50df..727433f280011f 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE @@ -8,6 +8,7 @@ maybe( local_java_repository, name = "local_jdk", java_home = DEFAULT_SYSTEM_JAVABASE, + build_file = "@bazel_tools//tools/jdk:jdk.BUILD", ) # OpenJDK distributions that should only be downloaded on demand (e.g. when diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java index b5b629e9cfd946..9c8cdcd97daa5d 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java @@ -74,11 +74,12 @@ public ImportDepsCheckingLevelConverter() { private static final String DEFAULT_JAVABASE = "@bazel_tools//tools/jdk:jdk"; + @Deprecated @Option( name = "javabase", defaultValue = DEFAULT_JAVABASE, converter = LabelConverter.class, - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, + documentationCategory = OptionDocumentationCategory.UNDOCUMENTED, effectTags = {OptionEffectTag.UNKNOWN}, help = "JAVABASE used for the JDK invoked by Blaze. This is the " @@ -88,29 +89,32 @@ public ImportDepsCheckingLevelConverter() { private static final String DEFAULT_JAVA_TOOLCHAIN = "@bazel_tools//tools/jdk:remote_toolchain"; + @Deprecated @Option( name = "java_toolchain", defaultValue = DEFAULT_JAVA_TOOLCHAIN, converter = LabelConverter.class, - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, + documentationCategory = OptionDocumentationCategory.UNDOCUMENTED, effectTags = {OptionEffectTag.UNKNOWN}, help = "The name of the toolchain rule for Java.") public Label javaToolchain; + @Deprecated @Option( name = "host_java_toolchain", defaultValue = DEFAULT_JAVA_TOOLCHAIN, converter = LabelConverter.class, - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, + documentationCategory = OptionDocumentationCategory.UNDOCUMENTED, effectTags = {OptionEffectTag.UNKNOWN}, help = "The Java toolchain used to build tools that are executed during a build.") public Label hostJavaToolchain; + @Deprecated @Option( name = "host_javabase", defaultValue = "null", converter = LabelConverter.class, - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, + documentationCategory = OptionDocumentationCategory.UNDOCUMENTED, effectTags = {OptionEffectTag.UNKNOWN}, help = "JAVABASE used for the host JDK. This is the java_runtime which is used to execute " @@ -629,7 +633,7 @@ public ImportDepsCheckingLevelConverter() { @Option( name = "java_runtime_version", defaultValue = "local_jdk", - documentationCategory = OptionDocumentationCategory.UNDOCUMENTED, + documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, effectTags = {OptionEffectTag.UNKNOWN}, help = "The Java runtime version") public String javaRuntimeVersion; @@ -637,7 +641,7 @@ public ImportDepsCheckingLevelConverter() { @Option( name = "tool_java_runtime_version", defaultValue = "remotejdk_11", - documentationCategory = OptionDocumentationCategory.UNDOCUMENTED, + documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, effectTags = {OptionEffectTag.UNKNOWN}, help = "The Java runtime version used to execute tools during the build") public String hostJavaRuntimeVersion; @@ -645,7 +649,7 @@ public ImportDepsCheckingLevelConverter() { @Option( name = "java_language_version", defaultValue = "8", - documentationCategory = OptionDocumentationCategory.UNDOCUMENTED, + documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, effectTags = {OptionEffectTag.UNKNOWN}, help = "The Java language version") public String javaLanguageVersion; @@ -653,7 +657,7 @@ public ImportDepsCheckingLevelConverter() { @Option( name = "tool_java_language_version", defaultValue = "8", - documentationCategory = OptionDocumentationCategory.UNDOCUMENTED, + documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, effectTags = {OptionEffectTag.UNKNOWN}, help = "The Java language version used to build tools that are executed during a build") public String hostJavaLanguageVersion; diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java index 64541c317a9d36..1164eb084c345e 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java @@ -105,8 +105,7 @@ public void testJavaRuntimeProviderJavaAbsolute() throws Exception { " )", "jrule = rule(_impl, attrs = { '_java_runtime': attr.label(default=Label('//a:alias'))})"); - useConfiguration( - "--javabase=//a:jvm", "--extra_toolchains=//a:all", "--platforms=//a:platform"); + useConfiguration("--extra_toolchains=//a:all", "--platforms=//a:platform"); ConfiguredTarget ct = getConfiguredTarget("//a:r"); StructImpl myInfo = getMyInfoFromTarget(ct); String javaHomeExecPath = (String) myInfo.getValue("java_home_exec_path"); @@ -162,8 +161,7 @@ public void testJavaRuntimeProviderJavaHermetic() throws Exception { " )", "jrule = rule(_impl, attrs = { '_java_runtime': attr.label(default=Label('//a:alias'))})"); - useConfiguration( - "--javabase=//a:jvm", "--extra_toolchains=//a:all", "--platforms=//a:platform"); + useConfiguration("--extra_toolchains=//a:all", "--platforms=//a:platform"); ConfiguredTarget ct = getConfiguredTarget("//a:r"); StructImpl myInfo = getMyInfoFromTarget(ct); String javaHomeExecPath = (String) myInfo.getValue("java_home_exec_path"); @@ -220,8 +218,7 @@ public void testJavaRuntimeProviderJavaGenerated() throws Exception { " )", "jrule = rule(_impl, attrs = { '_java_runtime': attr.label(default=Label('//a:alias'))})"); - useConfiguration( - "--javabase=//a:jvm", "--extra_toolchains=//a:all", "--platforms=//a:platform"); + useConfiguration("--extra_toolchains=//a:all", "--platforms=//a:platform"); // TODO(b/129637690): the runtime shouldn't be resolved in the host config ConfiguredTarget genrule = getHostConfiguredTarget("//a:gen"); ConfiguredTarget ct = getConfiguredTarget("//a:r"); @@ -1803,7 +1800,6 @@ public void javaToolchainFlag_set() throws Exception { "java_toolchain_alias(name='alias')", "myrule(name='myrule')"); useConfiguration( - "--java_toolchain=//java/com/google/test:toolchain", "--extra_toolchains=//java/com/google/test:all", "--platforms=//java/com/google/test:platform"); ConfiguredTarget configuredTarget = getConfiguredTarget("//foo:myrule"); @@ -2194,8 +2190,7 @@ public void testJavaRuntimeProviderFiles() throws Exception { " )", "jrule = rule(_impl, attrs = { '_java_runtime': attr.label(default=Label('//a:alias'))})"); - useConfiguration( - "--javabase=//a:jvm", "--extra_toolchains=//a:all", "--platforms=//a:platform"); + useConfiguration("--extra_toolchains=//a:all", "--platforms=//a:platform"); ConfiguredTarget ct = getConfiguredTarget("//a:r"); Depset files = (Depset) ct.get("files"); assertThat(prettyArtifactNames(files.toList(Artifact.class))).containsExactly("a/a.txt"); diff --git a/src/test/py/bazel/windows_remote_test.py b/src/test/py/bazel/windows_remote_test.py index bc1262a774d57f..ddbd3d73031206 100644 --- a/src/test/py/bazel/windows_remote_test.py +++ b/src/test/py/bazel/windows_remote_test.py @@ -173,7 +173,6 @@ def testJavaTestRunsRemotely(self): # it elsewhere, add --test_env=JAVA_HOME to your Bazel invocation to fix this # test. def testJavaTestWithRuntimeRunsRemotely(self): - java_home = os.getenv('JAVA_HOME', 'c:/openjdk') self.CreateWorkspaceWithDefaultRepos('WORKSPACE') self.ScratchFile('foo/BUILD', [ 'package(default_visibility = ["//visibility:public"])', @@ -184,11 +183,6 @@ def testJavaTestWithRuntimeRunsRemotely(self): ' use_testrunner = 0,', ' data = ["//bar:bar.txt"],', ')', - 'java_runtime(', - ' name = "jdk8",', - ' srcs = [],', - ' java_home = ' + repr(java_home) + ',', - ')', ]) self.ScratchFile( 'foo/TestFoo.java', [ @@ -203,9 +197,8 @@ def testJavaTestWithRuntimeRunsRemotely(self): self.ScratchFile('bar/bar.txt', ['hello']) # Test. - exit_code, stdout, stderr = self._RunRemoteBazel([ - 'test', '--test_output=all', '--javabase=//foo:jdk8', '//foo:foo_test' - ]) + exit_code, stdout, stderr = self._RunRemoteBazel( + ['test', '--test_output=all', '//foo:foo_test']) self.AssertExitCode(exit_code, 0, stderr, stdout) # Genrules are notably different than tests because RUNFILES_DIR is not set diff --git a/src/test/shell/bazel/BUILD b/src/test/shell/bazel/BUILD index b850beab6a94d6..3d90a813d6048a 100644 --- a/src/test/shell/bazel/BUILD +++ b/src/test/shell/bazel/BUILD @@ -182,17 +182,10 @@ sh_test( name = "bazel_java14_test", srcs = ["bazel_java14_test.sh"], args = [ - # --java_toolchain and --host_java_toolchain values - "@bazel_tools//tools/jdk:toolchain_jdk_14", # java_tools zip to test "src/java_tools.zip", "src/java_tools_prebuilt.zip", - ] + select({ - # --javabase and --host_javabase values - "//src/conditions:darwin": ["@openjdk14_darwin_archive//:runtime"], - "//src/conditions:windows": ["@openjdk14_windows_archive//:runtime"], - "//src/conditions:linux": ["@openjdk14_linux_archive//:runtime"], - }), + ], data = [ ":test-deps", "//src:java_tools_prebuilt_zip", @@ -207,17 +200,10 @@ sh_test( name = "bazel_java15_test", srcs = ["bazel_java15_test.sh"], args = [ - # --java_toolchain and --host_java_toolchain values - "@bazel_tools//tools/jdk:toolchain_jdk_15", # java_tools zip to test "src/java_tools.zip", "src/java_tools_prebuilt.zip", - ] + select({ - # --javabase and --host_javabase values - "//src/conditions:darwin": ["@openjdk15_darwin_archive//:runtime"], - "//src/conditions:windows": ["@openjdk15_windows_archive//:runtime"], - "//src/conditions:linux": ["@openjdk15_linux_archive//:runtime"], - }), + ], data = [ ":test-deps", "//src:java_tools_prebuilt_zip", @@ -235,7 +221,6 @@ sh_test( timeout = "eternal", srcs = ["bazel_java_test.sh"], args = [ - "@bazel_tools//tools/jdk:toolchain", "released", "released", ], @@ -256,47 +241,12 @@ JAVA_VERSIONS = ("11", "14", "15") timeout = "eternal", srcs = ["bazel_java_test.sh"], args = [ - # --java_toolchain - "@bazel_tools//tools/jdk:toolchain", # java_tools zips to test "src/java_tools.zip", "src/java_tools_prebuilt.zip", - # --javabase value - ] + select({ - "//src/conditions:darwin": ["@openjdk" + java_version + "_darwin_archive//:runtime"], - "//src/conditions:windows": ["@openjdk" + java_version + "_windows_archive//:runtime"], - "//src/conditions:linux": ["@openjdk" + java_version + "_linux_archive//:runtime"], - }), - data = [ - ":test-deps", - "//src:java_tools_prebuilt_zip", - "//src:java_tools_zip", - "//src/test/shell/bazel/testdata:jdk_http_archives_filegroup", - "@bazel_tools//tools/bash/runfiles", + # --java_runtime_version value + java_version, ], - exec_compatible_with = ["//:highcpu_machine"], - ) - for java_version in JAVA_VERSIONS -] - -[ - sh_test( - name = "bazel_java_test_jdk" + java_version + "_prebuilt_toolchain_head", - size = "large", - timeout = "eternal", - srcs = ["bazel_java_test.sh"], - args = [ - # --java_toolchain - "@bazel_tools//tools/jdk:prebuilt_toolchain", - # java_tools zips to test - "src/java_tools.zip", - "src/java_tools_prebuilt.zip", - # --javabase value - ] + select({ - "//src/conditions:darwin": ["@openjdk" + java_version + "_darwin_archive//:runtime"], - "//src/conditions:windows": ["@openjdk" + java_version + "_windows_archive//:runtime"], - "//src/conditions:linux": ["@openjdk" + java_version + "_linux_archive//:runtime"], - }), data = [ ":test-deps", "//src:java_tools_prebuilt_zip", @@ -316,17 +266,12 @@ JAVA_VERSIONS = ("11", "14", "15") timeout = "eternal", srcs = ["bazel_java_test.sh"], args = [ - # --java_toolchain - "@bazel_tools//tools/jdk:toolchain", # java_tools zips to test "$(LOCAL_JAVA_TOOLS_ZIP_URL)", "$(LOCAL_JAVA_TOOLS_PREBUILT_ZIP_URL)", - # --javabase value - ] + select({ - "//src/conditions:darwin": ["@openjdk" + java_version + "_darwin_archive//:runtime"], - "//src/conditions:windows": ["@openjdk" + java_version + "_windows_archive//:runtime"], - "//src/conditions:linux": ["@openjdk" + java_version + "_linux_archive//:runtime"], - }), + # --java_runtime_version value + java_version, + ], data = [ ":test-deps", "//src/test/shell/bazel/testdata:jdk_http_archives_filegroup", @@ -505,7 +450,6 @@ sh_test( name = "bazel_coverage_java_test", srcs = ["bazel_coverage_java_test.sh"], args = [ - "@bazel_tools//tools/jdk:toolchain", "released", "released", "released", @@ -525,18 +469,14 @@ sh_test( name = "bazel_coverage_java_jdk" + java_version + "_toolchain_released_test", srcs = ["bazel_coverage_java_test.sh"], args = [ - "@bazel_tools//tools/jdk:toolchain", # java_tools zip to test "released", "released", # coverage_report_generator to test "released", - # --javabase value - ] + select({ - "//src/conditions:darwin": ["@openjdk" + java_version + "_darwin_archive//:runtime"], - "//src/conditions:windows": ["@openjdk" + java_version + "_windows_archive//:runtime"], - "//src/conditions:linux": ["@openjdk" + java_version + "_linux_archive//:runtime"], - }), + # --java_runtime_version value + java_version, + ], data = [ ":test-deps", "//src/test/shell/bazel/testdata:jdk_http_archives_filegroup", @@ -554,19 +494,14 @@ sh_test( name = "bazel_coverage_java_jdk" + java_version + "_toolchain_head_test", srcs = ["bazel_coverage_java_test.sh"], args = [ - # --java_toolchain - "@bazel_tools//tools/jdk:toolchain", # java_tools zips to test "src/java_tools.zip", "src/java_tools_prebuilt.zip", # coverage output generator to test "tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator/coverage", - # --javabase value - ] + select({ - "//src/conditions:darwin": ["@openjdk" + java_version + "_darwin_archive//:runtime"], - "//src/conditions:windows": ["@openjdk" + java_version + "_windows_archive//:runtime"], - "//src/conditions:linux": ["@openjdk" + java_version + "_linux_archive//:runtime"], - }), + # --java_runtime_version value + java_version, + ], data = [ ":test-deps", "//src:java_tools_prebuilt_zip", diff --git a/src/test/shell/bazel/bazel_coverage_java_test.sh b/src/test/shell/bazel/bazel_coverage_java_test.sh index 8d16aa815231be..735b943271b3d2 100755 --- a/src/test/shell/bazel/bazel_coverage_java_test.sh +++ b/src/test/shell/bazel/bazel_coverage_java_test.sh @@ -21,9 +21,6 @@ CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${CURRENT_DIR}/../integration_test_setup.sh" \ || { echo "integration_test_setup.sh not found!" >&2; exit 1; } -JAVA_TOOLCHAIN="$1"; shift -add_to_bazelrc "build --java_toolchain=${JAVA_TOOLCHAIN}" -add_to_bazelrc "build --host_java_toolchain=${JAVA_TOOLCHAIN}" JAVA_TOOLS_ZIP="$1"; shift if [[ "${JAVA_TOOLS_ZIP}" != "released" ]]; then @@ -52,9 +49,9 @@ if [[ "${COVERAGE_GENERATOR_DIR}" != "released" ]]; then fi if [[ $# -gt 0 ]]; then - JAVABASE_VALUE="$1"; shift - add_to_bazelrc "build --javabase=${JAVABASE_VALUE}" - add_to_bazelrc "build --host_javabase=${JAVABASE_VALUE}" + JAVA_RUNTIME_VERSION="$1"; shift + add_to_bazelrc "build --java_runtime_version=${JAVA_RUNTIME_VERSION}" + add_to_bazelrc "build --tool_java_runtime_version=${JAVA_RUNTIME_VERSION}" fi function set_up() { diff --git a/src/test/shell/bazel/bazel_example_test.sh b/src/test/shell/bazel/bazel_example_test.sh index ade7e11aecade6..cb6fb4c20b5ab6 100755 --- a/src/test/shell/bazel/bazel_example_test.sh +++ b/src/test/shell/bazel/bazel_example_test.sh @@ -105,11 +105,12 @@ function test_java_test_with_junitrunner() { } function test_genrule_and_genquery() { - # The --javabase flag is to force the tools/jdk:jdk label to be used - # so it appears in the dependency list. - assert_build_output ./bazel-bin/examples/gen/genquery examples/gen:genquery --javabase=@bazel_tools//tools/jdk + # With toolchain resolution java runtime only appears in cquery results. + # //tools/jdk:jdk label appears in the dependency list while --javabase + # is still available, because of migration rules. + assert_build_output ./bazel-bin/examples/gen/genquery examples/gen:genquery local want=./bazel-genfiles/examples/gen/genrule.txt - assert_build_output $want examples/gen:genrule --javabase=@bazel_tools//tools/jdk + assert_build_output $want examples/gen:genrule diff $want ./bazel-bin/examples/gen/genquery \ || fail "genrule and genquery output differs" diff --git a/src/test/shell/bazel/bazel_java14_test.sh b/src/test/shell/bazel/bazel_java14_test.sh index b9db4895e8ad0e..53e57e8c110c78 100755 --- a/src/test/shell/bazel/bazel_java14_test.sh +++ b/src/test/shell/bazel/bazel_java14_test.sh @@ -54,14 +54,11 @@ if "$is_windows"; then export MSYS2_ARG_CONV_EXCL="*" fi -JAVA_TOOLCHAIN="$1"; shift JAVA_TOOLS_ZIP="$1"; shift JAVA_TOOLS_PREBUILT_ZIP="$1"; shift -JAVA_RUNTIME="$1"; shift echo "JAVA_TOOLS_ZIP=$JAVA_TOOLS_ZIP" - JAVA_TOOLS_RLOCATION=$(rlocation io_bazel/$JAVA_TOOLS_ZIP) if "$is_windows"; then @@ -74,10 +71,6 @@ fi JAVA_TOOLS_ZIP_FILE_URL=${JAVA_TOOLS_ZIP_FILE_URL:-} JAVA_TOOLS_PREBUILT_ZIP_FILE_URL=${JAVA_TOOLS_PREBUILT_ZIP_FILE_URL:-} -add_to_bazelrc "build --java_toolchain=${JAVA_TOOLCHAIN}" -add_to_bazelrc "build --host_java_toolchain=${JAVA_TOOLCHAIN}" -add_to_bazelrc "build --javabase=${JAVA_RUNTIME}" -add_to_bazelrc "build --host_javabase=${JAVA_RUNTIME}" function set_up() { cat >>WORKSPACE <>WORKSPACE <"${TEST_log}" \ @@ -115,8 +113,6 @@ public class JavaBinary { } EOF bazel run java/main:JavaBinary \ - --java_toolchain=@bazel_tools//tools/jdk:toolchain_java11 \ - --javabase=@bazel_tools//tools/jdk:remote_jdk11 \ --java_language_version=11 \ --java_runtime_version=11 \ --verbose_failures -s &>"${TEST_log}" \ @@ -153,8 +149,7 @@ public class JavaBinary { } EOF bazel coverage java/main:JavaBinary \ - --java_toolchain=//java/main:default_toolchain \ - --javabase=@bazel_tools//tools/jdk:remote_jdk11 \ + --java_runtime_version=11 \ --extra_toolchains=//java/main:default_toolchain_definition \ --verbose_failures -s &>"${TEST_log}" \ && fail "Coverage succeeded even when jacocorunner not set" diff --git a/src/test/shell/bazel/bazel_with_jdk_test.sh b/src/test/shell/bazel/bazel_with_jdk_test.sh index afca3f4fce224e..8e800cce9a978a 100755 --- a/src/test/shell/bazel/bazel_with_jdk_test.sh +++ b/src/test/shell/bazel/bazel_with_jdk_test.sh @@ -90,11 +90,7 @@ function set_up() { setup_bazelrc fi - # The default test setup adds a --host_javabase flag, which prevents us from - # actually using the bundled one. Remove it. - fgrep -v -- "--host_javabase" "$TEST_TMPDIR/bazelrc" > "$TEST_TMPDIR/bazelrc.new" - mv "$TEST_TMPDIR/bazelrc.new" "$TEST_TMPDIR/bazelrc" - # ... but ensure JAVA_HOME is set, so we can find a default --javabase + # ... but ensure JAVA_HOME is set, so we can find a default local jdk export JAVA_HOME="${javabase}" } diff --git a/src/test/shell/bazel/remote/remote_execution_test.sh b/src/test/shell/bazel/remote/remote_execution_test.sh index 6ed2b6231d3b0f..b05cafe240356c 100755 --- a/src/test/shell/bazel/remote/remote_execution_test.sh +++ b/src/test/shell/bazel/remote/remote_execution_test.sh @@ -2289,12 +2289,9 @@ EOF function test_rbe_coverage_produces_report() { mkdir -p java/factorial - JAVA_TOOLCHAIN="@bazel_tools//tools/jdk:toolchain" - add_to_bazelrc "build --java_toolchain=${JAVA_TOOLCHAIN}" - add_to_bazelrc "build --host_java_toolchain=${JAVA_TOOLCHAIN}" if is_darwin; then - add_to_bazelrc "build --javabase=@openjdk14_darwin_archive//:runtime" - add_to_bazelrc "build --host_javabase=@openjdk14_darwin_archive//:runtime" + add_to_bazelrc "build --java_runtime_version=14" + add_to_bazelrc "build --tool_java_runtime_version=14" fi JAVA_TOOLS_ZIP="released" COVERAGE_GENERATOR_DIR="released" diff --git a/src/test/shell/integration/bazel_java_test.sh b/src/test/shell/integration/bazel_java_test.sh index 6136cafb8afd43..f31f82f36a01ca 100755 --- a/src/test/shell/integration/bazel_java_test.sh +++ b/src/test/shell/integration/bazel_java_test.sh @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -# Tests that --server_javabase/--host_javabase and --javabase work as expected +# Tests that --server_javabase and Java toolchain resolution work as expected # for Bazel with the embedded JDK. # Load the test setup defined in the parent directory @@ -68,7 +68,7 @@ EOF # We expect the given host_javabase does not appear in the command line of # java_library actions. - bazel aquery --output=text --host_javabase=@host_javabase//:jdk --tool_java_runtime_version='host_javabase' //java:javalib >& $TEST_log + bazel aquery --output=text --tool_java_runtime_version='host_javabase' //java:javalib >& $TEST_log expect_log "exec external/remotejdk11_.*/bin/java" expect_not_log "exec external/host_javabase/bin/java" @@ -77,7 +77,7 @@ EOF expect_not_log "exec external/embedded_jdk/bin/java" expect_log "exec external/remotejdk11_.*/bin/java" - bazel aquery --output=text --host_javabase=@host_javabase//:jdk --tool_java_runtime_version='host_javabase' \ + bazel aquery --output=text --tool_java_runtime_version='host_javabase' \ //java:javalib >& $TEST_log expect_log "exec external/remotejdk11_.*/bin/java" expect_not_log "exec external/host_javabase/bin/java" @@ -87,7 +87,7 @@ EOF expect_log "exec external/remotejdk11_.*/bin/java" # If we change language version to 15, we expect runtime to change - bazel aquery --incompatible_use_toolchain_resolution_for_java_rules --output=text --host_javabase=@host_javabase//:jdk \ + bazel aquery --incompatible_use_toolchain_resolution_for_java_rules --output=text \ --tool_java_runtime_version='host_javabase' --java_language_version=15 \ //java:javalib >& $TEST_log expect_not_log "exec external/remotejdk11_.*/bin/java" @@ -135,7 +135,7 @@ EOF # We expect the given host_javabase does not appear in the command line of # java_library actions. - bazel aquery --output=text --host_javabase=@host_javabase//:jdk --tool_java_runtime_version='host_javabase' 'deps(//java:sample,1)' >& $TEST_log + bazel aquery --output=text --tool_java_runtime_version='host_javabase' 'deps(//java:sample,1)' >& $TEST_log expect_log "exec external/remotejdk11_.*/bin/java" expect_not_log "exec external/host_javabase/bin/java" @@ -145,7 +145,7 @@ EOF expect_not_log "exec external/embedded_jdk/bin/java" expect_log "exec external/remotejdk11_.*/bin/java" - bazel aquery --output=text --host_javabase=@host_javabase//:jdk --tool_java_runtime_version='host_javabase' \ + bazel aquery --output=text --tool_java_runtime_version='host_javabase' \ 'deps(//java:sample,1)' >& $TEST_log expect_log "exec external/remotejdk11_.*/bin/java" expect_not_log "exec external/host_javabase/bin/java" @@ -197,9 +197,9 @@ public class HelloWorld {} EOF # Check that the RHS javabase appears in the launcher. - bazel build --java_toolchain=//:toolchain --javabase=@javabase//:jdk --extra_toolchains=//:toolchain_definition --java_runtime_version=javabase //java:javabin + bazel build --extra_toolchains=//:toolchain_definition --java_runtime_version=javabase //java:javabin cat bazel-bin/java/javabin >& $TEST_log - expect_log "JAVABIN=.*/javabase/bin/java}" + expect_log "JAVABIN=.*/zoo/bin/java" # Check that we use local_jdk when it's not specified. bazel build //java:javabin @@ -251,13 +251,18 @@ function test_no_javabase() { } function test_genrule() { - mkdir -p foo/bin bar/bin - cat << EOF > BUILD -java_runtime( + cat << EOF > WORKSPACE +load("@bazel_tools//tools/jdk:local_java_repository.bzl", "local_java_repository") +local_java_repository( name = "foo_javabase", java_home = "$PWD/foo", - visibility = ["//visibility:public"], + version = "11", ) +EOF + + mkdir -p foo/bin bar/bin + cat << EOF > BUILD + java_runtime( name = "bar_runtime", @@ -305,7 +310,7 @@ EOF # Setting the javabase should not change the use of :bar_runtime from the # roolchains attribute. bazel cquery --max_config_changes_to_show=0 --implicit_deps \ - 'deps(//:with_java)' --host_javabase=:foo_javabase >& $TEST_log + 'deps(//:with_java)' --tool_java_runtime_version=foo_javabase >& $TEST_log expect_not_log "foo" expect_log "bar" expect_not_log "embedded_jdk" diff --git a/src/test/shell/integration/java_integration_test.sh b/src/test/shell/integration/java_integration_test.sh index 4e43d746606ded..43e0d736259f51 100755 --- a/src/test/shell/integration/java_integration_test.sh +++ b/src/test/shell/integration/java_integration_test.sh @@ -26,7 +26,6 @@ source "${CURRENT_DIR}/../integration_test_setup.sh" \ # Load the helper utils. source "${CURRENT_DIR}/java_integration_test_utils.sh" \ || { echo "java_integration_test_utils.sh not found!" >&2; exit 1; } - set -eu declare -r runfiles_relative_javabase="$1" @@ -263,12 +262,11 @@ java_runtime( java_home='$javabase', ) EOF - create_java_test_platforms # Set javabase to an absolute path. bazel build //$pkg/java/hello:hello //$pkg/java/hello:hello_deploy.jar \ - "$stamp_arg" --javabase="//$pkg/jvm:runtime" \ + "$stamp_arg" \ --extra_toolchains="//$pkg/jvm:all,//tools/jdk:all" \ --platforms="//$pkg/jvm:platform" \ "$embed_label" >&"$TEST_log" \ @@ -278,7 +276,6 @@ EOF # The stub script follows symlinks, so copy the files. cp ${PRODUCT_NAME}-bin/$pkg/java/hello/hello $pkg/ugly/ cp ${PRODUCT_NAME}-bin/$pkg/java/hello/hello_deploy.jar $pkg/ugly/ - $pkg/ugly/hello build.target build.time build.timestamp \ main.class=hello.Hello "$expected_build_data" >> $TEST_log 2>&1 expect_log 'Hello, World!' diff --git a/src/test/shell/testenv.sh b/src/test/shell/testenv.sh index e827826df26c7f..a61de7b8070fdf 100755 --- a/src/test/shell/testenv.sh +++ b/src/test/shell/testenv.sh @@ -272,6 +272,8 @@ build --sandbox_tmpfs_path=/tmp build --incompatible_skip_genfiles_symlink=false +build --incompatible_use_toolchain_resolution_for_java_rules + ${EXTRA_BAZELRC:-} EOF diff --git a/tools/jdk/BUILD.tools b/tools/jdk/BUILD.tools index ab39c8050065c7..0faec24163e674 100644 --- a/tools/jdk/BUILD.tools +++ b/tools/jdk/BUILD.tools @@ -339,6 +339,7 @@ bootclasspath( default_java_toolchain( name = "toolchain", configuration = DEFAULT_TOOLCHAIN_CONFIGURATION, + toolchain_definition = False, ) alias( @@ -384,11 +385,13 @@ default_java_toolchain( java_runtime = ":current_host_java_runtime", source_version = "8", target_version = "8", + toolchain_definition = False, ) default_java_toolchain( name = "prebuilt_toolchain", configuration = PREBUILT_TOOLCHAIN_CONFIGURATION, + toolchain_definition = False, ) filegroup( diff --git a/tools/jdk/default_java_toolchain.bzl b/tools/jdk/default_java_toolchain.bzl index 453ad8e0c34e86..4792afd8a9a6b9 100644 --- a/tools/jdk/default_java_toolchain.bzl +++ b/tools/jdk/default_java_toolchain.bzl @@ -156,7 +156,7 @@ NONPREBUILT_TOOLCHAIN_CONFIGURATION = dict( java_runtime = "@bazel_tools//tools/jdk:remote_jdk11", ) -def default_java_toolchain(name, configuration = DEFAULT_TOOLCHAIN_CONFIGURATION, **kwargs): +def default_java_toolchain(name, configuration = DEFAULT_TOOLCHAIN_CONFIGURATION, toolchain_definition = True, **kwargs): """Defines a remote java_toolchain with appropriate defaults for Bazel.""" toolchain_args = dict(_BASE_TOOLCHAIN_CONFIGURATION) @@ -166,18 +166,18 @@ def default_java_toolchain(name, configuration = DEFAULT_TOOLCHAIN_CONFIGURATION name = name, **toolchain_args ) - - native.config_setting( - name = name + "_version_setting", - values = {"java_language_version": toolchain_args["source_version"]}, - visibility = ["//visibility:private"], - ) - native.toolchain( - name = name + "_definition", - toolchain_type = "@bazel_tools//tools/jdk:toolchain_type", - target_settings = [name + "_version_setting"], - toolchain = name, - ) + if toolchain_definition: + native.config_setting( + name = name + "_version_setting", + values = {"java_language_version": toolchain_args["source_version"]}, + visibility = ["//visibility:private"], + ) + native.toolchain( + name = name + "_definition", + toolchain_type = "@bazel_tools//tools/jdk:toolchain_type", + target_settings = [name + "_version_setting"], + toolchain = name, + ) def java_runtime_files(name, srcs): """Copies the given sources out of the current Java runtime.""" diff --git a/tools/jdk/local_java_repository.bzl b/tools/jdk/local_java_repository.bzl index bf99ecb1e30aa8..b354d9a42244cb 100644 --- a/tools/jdk/local_java_repository.bzl +++ b/tools/jdk/local_java_repository.bzl @@ -51,9 +51,18 @@ def _local_java_repository_impl(repository_ctx): if java_bin.exists: version = repository_ctx.attr.version if repository_ctx.attr.version != "" else _detect_java_version(repository_ctx, java_bin) + if repository_ctx.attr.build_file != None: + build_file = repository_ctx.read(repository_ctx.path(repository_ctx.attr.build_file)) + else: + build_file = """ +java_runtime( + name = "jdk", + java_home = "{java_home}" +) """.format(java_home = java_home) + repository_ctx.file( "BUILD.bazel", - repository_ctx.read(repository_ctx.path(repository_ctx.attr._build_file)) + + build_file + """ config_setting( name = "name_setting", @@ -127,11 +136,11 @@ _local_java_repository_rule = repository_rule( attrs = { "java_home": attr.string(), "version": attr.string(), - "_build_file": attr.label(default = "@bazel_tools//tools/jdk:jdk.BUILD"), + "build_file": attr.label(), }, ) -def local_java_repository(name, java_home, version = ""): +def local_java_repository(name, java_home, version = "", build_file = None): """Imports and registers a local JDK. Toolchain resolution is constrained with --java_runtime_version flag @@ -140,7 +149,8 @@ def local_java_repository(name, java_home, version = ""): Args: name: A unique name for this rule. java_home: Location of the JDK imported. + build_file: optionally BUILD file template version: optionally java version """ - _local_java_repository_rule(name = name, java_home = java_home, version = version) + _local_java_repository_rule(name = name, java_home = java_home, version = version, build_file = build_file) native.register_toolchains("@" + name + "//:toolchain")