Skip to content

Commit

Permalink
Fix Java tests to use Java toolchain resolution.
Browse files Browse the repository at this point in the history
The flag is flipped for tests, old configuration options `javabase` and `java_toolchain` are marked deprecated. All the tests are fixed to execute using new toolchain resolution.

Changed `local_java_repository` to generate `java_runtime` with `java_home` parameter set (instead of srcs). This behaves better, because `java_stub_template.txt` then does not need runfiles to execute produced `deploy.jar`. Kept the same version with `srcs` in the `jdk.WORKSPACE`.

Related to bazelbuild#4592.

Closes bazelbuild#12695.

PiperOrigin-RevId: 347969628
  • Loading branch information
comius authored and copybara-github committed Dec 17, 2020
1 parent 467e1d4 commit f5b6abc
Show file tree
Hide file tree
Showing 19 changed files with 99 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand All @@ -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 "
Expand Down Expand Up @@ -629,31 +633,31 @@ 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;

@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;

@Option(
name = "java_language_version",
defaultValue = "8",
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
effectTags = {OptionEffectTag.UNKNOWN},
help = "The Java language version")
public String javaLanguageVersion;

@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
11 changes: 2 additions & 9 deletions src/test/py/bazel/windows_remote_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])',
Expand All @@ -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', [
Expand All @@ -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
Expand Down
91 changes: 13 additions & 78 deletions src/test/shell/bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -235,7 +221,6 @@ sh_test(
timeout = "eternal",
srcs = ["bazel_java_test.sh"],
args = [
"@bazel_tools//tools/jdk:toolchain",
"released",
"released",
],
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
9 changes: 3 additions & 6 deletions src/test/shell/bazel/bazel_coverage_java_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
9 changes: 5 additions & 4 deletions src/test/shell/bazel/bazel_example_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading

0 comments on commit f5b6abc

Please sign in to comment.