Skip to content

Commit

Permalink
Update default Java language version
Browse files Browse the repository at this point in the history
Use `--java_language_version=''` to mean the toolchain-defined default version,
instead of hard-coding a default value of the flag.

PiperOrigin-RevId: 495444503
Change-Id: Ie6554d85a8c64d2ecd78ff9a977f48d23edbc52b
  • Loading branch information
cushon authored and copybara-github committed Dec 15, 2022
1 parent e4060f1 commit f23440b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion scripts/bootstrap/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ _BAZEL_ARGS="--spawn_strategy=standalone \
--strategy=Javac=worker --worker_quit_after_build --ignore_unsupported_sandboxing \
--compilation_mode=opt \
--distdir=derived/distdir \
--extra_toolchains=//scripts/bootstrap:bootstrap_toolchain_definition \
--extra_toolchains=//scripts/bootstrap:all \
${DIST_BOOTSTRAP_ARGS:-} \
${EXTRA_BAZEL_ARGS:-}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,15 +543,15 @@ public ImportDepsCheckingLevelConverter() {

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

@Option(
name = "tool_java_language_version",
defaultValue = "8",
defaultValue = "",
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
effectTags = {OptionEffectTag.UNKNOWN},
help = "The Java language version used to execute the tools that are needed during a build")
Expand Down
18 changes: 14 additions & 4 deletions src/test/py/bazel/bzlmod/bazel_module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,20 @@ def writeBazelrcFile(self, allow_yanked_versions=True):
# bazel_tools can work.
'common --registry=https://bcr.bazel.build',
'common --verbose_failures',
] + ([
# Disable yanked version check so we are not affected BCR changes.
'common --allow_yanked_versions=all',
] if allow_yanked_versions else []))
# Set an explicit Java language version
'common --java_language_version=8',
'common --tool_java_language_version=8',
]
+ (
[
# Disable yanked version check so we are not affected BCR
# changes.
'common --allow_yanked_versions=all',
]
if allow_yanked_versions
else []
),
)

def writeMainProjectFiles(self):
self.ScratchFile('aaa.patch', [
Expand Down
2 changes: 1 addition & 1 deletion src/test/shell/bazel/bazel_java_test_defaults.sh
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public class JavaBinary {
EOF
bazel coverage java/main:JavaBinary \
--java_runtime_version=11 \
--extra_toolchains=//java/main:default_toolchain_definition \
--extra_toolchains=//java/main:all \
--verbose_failures -s &>"${TEST_log}" \
&& fail "Coverage succeeded even when jacocorunner not set"
expect_log "jacocorunner not set in java_toolchain:"
Expand Down
2 changes: 1 addition & 1 deletion src/test/shell/integration/bazel_java_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public class HelloWorld {}
EOF

# Check that the RHS javabase appears in the launcher.
bazel build --extra_toolchains=//:toolchain_definition --java_runtime_version=javabase //java:javabin
bazel build --extra_toolchains=//:all --java_runtime_version=javabase //java:javabin
cat bazel-bin/java/javabin >& $TEST_log
expect_log "JAVABIN=.*/zoo/bin/java"

Expand Down
2 changes: 1 addition & 1 deletion tools/jdk/BUILD.tools
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,4 @@ java_runtime_version_alias(
visibility = ["//visibility:public"],
)

exports_files(["java_stub_template.txt"])
exports_files(["java_stub_template.txt"])
20 changes: 19 additions & 1 deletion tools/jdk/default_java_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ NONPREBUILT_TOOLCHAIN_CONFIGURATION = dict(
singlejar = ["@remote_java_tools//:singlejar_cc_bin"],
)

_DEFAULT_SOURCE_VERSION = "8"

def default_java_toolchain(name, configuration = DEFAULT_TOOLCHAIN_CONFIGURATION, toolchain_definition = True, exec_compatible_with = [], target_compatible_with = [], **kwargs):
"""Defines a remote java_toolchain with appropriate defaults for Bazel.
Expand All @@ -143,9 +145,25 @@ def default_java_toolchain(name, configuration = DEFAULT_TOOLCHAIN_CONFIGURATION
**toolchain_args
)
if toolchain_definition:
source_version = toolchain_args["source_version"]
if source_version == _DEFAULT_SOURCE_VERSION:
native.config_setting(
name = name + "_default_version_setting",
values = {"java_language_version": ""},
visibility = ["//visibility:private"],
)
native.toolchain(
name = name + "_default_definition",
toolchain_type = "@bazel_tools//tools/jdk:toolchain_type",
target_settings = [name + "_default_version_setting"],
toolchain = name,
exec_compatible_with = exec_compatible_with,
target_compatible_with = target_compatible_with,
)

native.config_setting(
name = name + "_version_setting",
values = {"java_language_version": toolchain_args["source_version"]},
values = {"java_language_version": source_version},
visibility = ["//visibility:private"],
)
native.toolchain(
Expand Down

0 comments on commit f23440b

Please sign in to comment.