Skip to content

Commit

Permalink
Automated rollback of commit 37eb1d7.
Browse files Browse the repository at this point in the history
*** Reason for rollback ***

Breaks multiple downstream projects, see https://buildkite.com/bazel/bazel-auto-sheriff-face-with-cowboy-hat/builds/364#c671c11c-f885-4527-829f-057b0e29d29c

*** Original change description ***

Attach Java runtime to Java toolchains

Defining a new rule "java_runtime_version_alias", which uses a custom configuration flag transition to select a specific version of remote JDK.

Added a weird "selected_java_runtime" attribute, which needs to exist until the incompatible_use_java_toolchain_resolution is flipped.

Added the test for the attached runtime and fixing the test for host_javabase (now it is only modified for targets in exec...

***

PiperOrigin-RevId: 346949769
  • Loading branch information
comius authored and copybara-github committed Dec 11, 2020
1 parent f48d46c commit ddfc932
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 193 deletions.
23 changes: 0 additions & 23 deletions scripts/bootstrap/BUILD.bootstrap

This file was deleted.

9 changes: 3 additions & 6 deletions scripts/bootstrap/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,12 @@ _BAZEL_ARGS="--spawn_strategy=standalone \
--strategy=Javac=worker --worker_quit_after_build --ignore_unsupported_sandboxing \
--compilation_mode=opt \
--distdir=derived/distdir \
--java_toolchain=//scripts/bootstrap:bootstrap_toolchain \
--host_java_toolchain=//scripts/bootstrap:bootstrap_toolchain \
--incompatible_use_toolchain_resolution_for_java_rules \
--extra_toolchains=//scripts/bootstrap:bootstrap_toolchain_definition \
--java_toolchain=//src/java_tools/buildjar:bootstrap_toolchain \
--host_java_toolchain=//src/java_tools/buildjar:bootstrap_toolchain \
--extra_toolchains=//src/java_tools/buildjar:bootstrap_toolchain_definition \
${DIST_BOOTSTRAP_ARGS:-} \
${EXTRA_BAZEL_ARGS:-}"

cp scripts/bootstrap/BUILD.bootstrap scripts/bootstrap/BUILD

if [ -z "${BAZEL-}" ]; then
function _run_bootstrapping_bazel() {
local command=$1
Expand Down
22 changes: 22 additions & 0 deletions src/java_tools/buildjar/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_java//java:defs.bzl", "java_binary")
load("@bazel_tools//tools/jdk:default_java_toolchain.bzl", "default_java_toolchain")

# Description:
# JavaBuilder and java tools used by Bazel
Expand Down Expand Up @@ -53,3 +54,24 @@ filegroup(
tags = ["manual"],
visibility = ["//visibility:public"],
)

# This toolchain is used to bootstrap Bazel.
default_java_toolchain(
name = "bootstrap_toolchain",
bootclasspath = ["@bazel_tools//tools/jdk:platformclasspath.jar"],
genclass = ["bootstrap_genclass_deploy.jar"],
ijar = ["//third_party/ijar"],
javabuilder = ["bootstrap_VanillaJavaBuilder_deploy.jar"],
jvm_opts = [
# Prevent "Could not reserve enough space for object heap" errors on Windows.
"-Xmx512m",
# Using tiered compilation improves performance of Javac when not using the worker mode.
"-XX:+TieredCompilation",
"-XX:TieredStopAtLevel=1",
],
singlejar = ["//src/java_tools/singlejar:bootstrap_deploy.jar"],
source_version = "8",
tags = ["manual"],
target_version = "8",
visibility = ["//visibility:public"],
)
5 changes: 1 addition & 4 deletions src/java_tools/singlejar/BUILD
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# Description:
# SingleJar combines multiple zip files and additional files
# into a single zip file.
package(default_visibility = [
"//scripts/bootstrap:__subpackages__",
"//src/java_tools:__subpackages__",
])
package(default_visibility = ["//src/java_tools:__subpackages__"])

filegroup(
name = "srcs",
Expand Down
3 changes: 2 additions & 1 deletion src/test/shell/bazel/bazel_java14_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ public class Javac14Example {
}
}
EOF
bazel run java/main:Javac14Example --java_language_version=14 --java_runtime_version=14 --test_output=all --verbose_failures &>"${TEST_log}"
# TODO(ilist): remove tool_java_runtime_version after java_runtime is attached to the toolchain
bazel run java/main:Javac14Example --java_language_version=14 --java_runtime_version=14 --tool_java_runtime_version=14 --test_output=all --verbose_failures &>"${TEST_log}"
expect_log "0"
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/shell/bazel/bazel_java15_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ public class Javac15Example {
}
}
EOF
bazel run java/main:Javac15Example --java_language_version=15 --java_runtime_version=15 --test_output=all --verbose_failures &>"${TEST_log}"
# TODO(ilist): remove tool_java_runtime_version after java_runtime is attached to the toolchain
bazel run java/main:Javac15Example --java_language_version=15 --java_runtime_version=15 --tool_java_runtime_version=15 --test_output=all --verbose_failures &>"${TEST_log}"
expect_log "^Hello,\$"
expect_log "^World\$"
}
Expand Down
1 change: 0 additions & 1 deletion src/test/shell/bazel/bazel_java_test_defaults.sh
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ EOF
bazel coverage java/main:JavaBinary \
--java_toolchain=//java/main:default_toolchain \
--javabase=@bazel_tools//tools/jdk:remote_jdk11 \
--extra_toolchains=//java/main:default_toolchain_definition \
--verbose_failures -s &>"${TEST_log}" \
&& fail "Coverage succeeded even when jacocorunner not set"
expect_log "jacocorunner not set in java_toolchain:"
Expand Down
85 changes: 7 additions & 78 deletions src/test/shell/integration/bazel_java_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ EOF
expect_log "java-home: .*/embedded_tools/jdk"
}

function test_toolchain_javabase() {
function test_host_javabase() {
cat << EOF >> WORKSPACE
load("@bazel_tools//tools/jdk:local_java_repository.bzl", "local_java_repository")
local_java_repository(
Expand All @@ -66,98 +66,27 @@ EOF
mkdir -p foobar/bin
touch foobar/bin/java

# We expect the given host_javabase does not appear in the command line of
# We expect the given host_javabase to 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
expect_log "exec external/remotejdk11_.*/bin/java"
expect_not_log "exec external/host_javabase/bin/java"
expect_log "exec external/host_javabase/bin/java"

# If we don't specify anything, we expect the remote JDK to be used.
# If we don't specify anything, we expect the embedded JDK to be used.
# Note that this will change in the future but is the current state.
bazel aquery --output=text //java:javalib >& $TEST_log
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' \
//java:javalib >& $TEST_log
expect_log "exec external/remotejdk11_.*/bin/java"
expect_not_log "exec external/host_javabase/bin/java"
expect_log "exec external/host_javabase/bin/java"
expect_not_log "exec external/remotejdk_.*/bin/java"

bazel aquery --output=text \
//java:javalib >& $TEST_log
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 \
--tool_java_runtime_version='host_javabase' --java_language_version=15 \
//java:javalib >& $TEST_log
expect_not_log "exec external/remotejdk11_.*/bin/java"
expect_log "exec external/remotejdk15_.*/bin/java"
}

function test_host_javabase() {
cat << EOF >> WORKSPACE
load("@bazel_tools//tools/jdk:local_java_repository.bzl", "local_java_repository")
local_java_repository(
name = "host_javabase",
java_home = "$PWD/foobar",
version = "11",
)
EOF
mkdir -p java
cat >> java/rule.bzl <<EOF
def _sample_rule_impl(ctx):
return []
sample_rule = rule(
implementation = _sample_rule_impl,
attrs = {
"dep": attr.label(cfg = 'exec'),
},
)
EOF

cat << EOF > java/BUILD
load(":rule.bzl", "sample_rule")
java_library(
name = "javalib",
srcs = ["HelloWorld.java"],
)
sample_rule(
name = 'sample',
dep = ':javalib',
)
EOF
touch java/HelloWorld.java

mkdir -p foobar/bin
touch foobar/bin/java

# 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
expect_log "exec external/remotejdk11_.*/bin/java"
expect_not_log "exec external/host_javabase/bin/java"

# If we don't specify anything, we expect the embedded JDK to be used.
# Note that this will change in the future but is the current state.
bazel aquery --output=text 'deps(//java:sample,1)' >& $TEST_log
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' \
'deps(//java:sample,1)' >& $TEST_log
expect_log "exec external/remotejdk11_.*/bin/java"
expect_not_log "exec external/host_javabase/bin/java"


bazel aquery --output=text \
'deps(//java:sample,1)' >& $TEST_log
expect_log "exec external/remotejdk11_.*/bin/java"
}



function test_javabase() {
cat << EOF >> WORKSPACE
load("@bazel_tools//tools/jdk:local_java_repository.bzl", "local_java_repository")
Expand Down
38 changes: 2 additions & 36 deletions tools/jdk/BUILD.tools
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ load(
"//tools/jdk:java_toolchain_alias.bzl",
"java_host_runtime_alias",
"java_runtime_alias",
"java_runtime_version_alias",
"java_toolchain_alias",
"legacy_java_runtime_alias",
"legacy_java_toolchain_alias",
Expand Down Expand Up @@ -378,7 +377,6 @@ RELEASES = (8, 9, 10, 11)
default_java_toolchain(
name = "toolchain_jdk_14",
configuration = dict(),
java_runtime = "@bazel_tools//tools/jdk:remotejdk_14",
source_version = "14",
target_version = "14",
)
Expand All @@ -387,7 +385,6 @@ default_java_toolchain(
default_java_toolchain(
name = "toolchain_jdk_15",
configuration = dict(),
java_runtime = "@bazel_tools//tools/jdk:remotejdk_15",
source_version = "15",
target_version = "15",
)
Expand Down Expand Up @@ -423,10 +420,9 @@ py_test(
)

# A JDK 11 for use as a --host_javabase.
java_runtime_version_alias(
alias(
name = "remote_jdk11",
runtime_version = "remotejdk_11",
selected_java_runtime = select(
actual = select(
{
"//src/conditions:darwin": "@remotejdk11_macos//:jdk",
"//src/conditions:windows": "@remotejdk11_win//:jdk",
Expand All @@ -440,33 +436,3 @@ java_runtime_version_alias(
),
visibility = ["//visibility:public"],
)

java_runtime_version_alias(
name = "remotejdk_14",
runtime_version = "remotejdk_14",
selected_java_runtime = select(
{
"//src/conditions:darwin": "@remotejdk14_macos//:jdk",
"//src/conditions:windows": "@remotejdk14_win//:jdk",
"//src/conditions:linux_x86_64": "@remotejdk14_linux//:jdk",
},
no_match_error = "Could not find a JDK for host execution environment, please explicitly" +
" provide one using `--host_javabase.`",
),
visibility = ["//visibility:public"],
)

java_runtime_version_alias(
name = "remotejdk_15",
runtime_version = "remotejdk_15",
selected_java_runtime = select(
{
"//src/conditions:darwin": "@remotejdk15_macos//:jdk",
"//src/conditions:windows": "@remotejdk15_win//:jdk",
"//src/conditions:linux_x86_64": "@remotejdk15_linux//:jdk",
},
no_match_error = "Could not find a JDK for host execution environment, please explicitly" +
" provide one using `--host_javabase.`",
),
visibility = ["//visibility:public"],
)
1 change: 0 additions & 1 deletion tools/jdk/default_java_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ _BASE_TOOLCHAIN_CONFIGURATION = dict(
bootclasspath = ["@bazel_tools//tools/jdk:platformclasspath"],
source_version = "8",
target_version = "8",
java_runtime = "@bazel_tools//tools/jdk:remote_jdk11",
)

JVM8_TOOLCHAIN_CONFIGURATION = dict(
Expand Down
42 changes: 0 additions & 42 deletions tools/jdk/java_toolchain_alias.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -72,48 +72,6 @@ java_host_runtime_alias = rule(
},
)

def _java_runtime_version_alias(ctx):
"""An alias fixing a specific version of java_runtime."""
if java_common.is_java_toolchain_resolution_enabled_do_not_use(ctx = ctx):
toolchain = ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"]
else:
toolchain = ctx.attr.selected_java_runtime[java_common.JavaRuntimeInfo]
return [
toolchain,
platform_common.TemplateVariableInfo({
"JAVA": str(toolchain.java_executable_exec_path),
"JAVABASE": str(toolchain.java_home),
}),
# See b/65239471 for related discussion of handling toolchain runfiles/data.
DefaultInfo(
runfiles = ctx.runfiles(transitive_files = toolchain.files),
files = toolchain.files,
),
]

def _java_runtime_transition_impl(settings, attr):
return {"//command_line_option:java_runtime_version": attr.runtime_version}

_java_runtime_transition = transition(
implementation = _java_runtime_transition_impl,
inputs = [],
outputs = ["//command_line_option:java_runtime_version"],
)

java_runtime_version_alias = rule(
implementation = _java_runtime_version_alias,
toolchains = ["@bazel_tools//tools/jdk:runtime_toolchain_type"],
attrs = {
"runtime_version": attr.string(mandatory = True),
# TODO(ilist): remove after java toolchain resolution flag is flipped
"selected_java_runtime": attr.label(mandatory = True),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
cfg = _java_runtime_transition,
)

def _java_toolchain_alias(ctx):
"""An experimental implementation of java_toolchain_alias using toolchain resolution."""
if java_common.is_java_toolchain_resolution_enabled_do_not_use(ctx = ctx):
Expand Down

0 comments on commit ddfc932

Please sign in to comment.