Skip to content

Commit

Permalink
Add predefined configurations to java_toolchain_default
Browse files Browse the repository at this point in the history
In order for java_toolchain_default to be more usable, predefined configurations are supplied: `JVM8_TOOLCHAIN_CONFIGURATION`, `JAVABUILDER_TOOLCHAIN_CONFIGURATION`, `VANILLA_TOOLCHAIN_CONFIGURATION`, `PREBUILT_TOOLCHAIN_CONFIGURATION`.

Those configurations are going to be needed once `java_runtime` is attached to `java_toolchain`, because predefined toolchain in `BUILD.java_tools` (e.g. vanilla_toolchain) will need to be removed (as they don't have a default choice of `java_runtime`).

After that the user can use `java_toolchain_default(name="toolchain_vanilla", configuration = VANILLA_TOOLCHAIN_CONFIGURATION, java_runtime = ...)`.

Additionally to this, `jvm_opts` parameter is automatically converted to absolute labels. For example from `--patch-module=java.compiler=$(location //:java_compiler_jar)` to `--patch-module=java.compiler=$(location @bazel_java_tools_linux//:java_compiler_jar)`.

Also `java_toolchain_default.bzl` is renamed to `java_toolchain_default.bzl.java_tools` to make it accessible only from @java_tools repository and not @bazel_tools. In the latter it is was never supposed to work.

Closes #12441.

PiperOrigin-RevId: 342053742
  • Loading branch information
comius authored and copybara-github committed Nov 12, 2020
1 parent 3e38144 commit e68b1e3
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 273 deletions.
22 changes: 15 additions & 7 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -629,12 +629,20 @@ JAVA_VERSIONS = ("11",)
# to build new Java 14 features. This is not used atm, as the toolchain for
# javac 14 was duplicated, but it might be used in future Bazel releases to
# support new javac release, so that we preserve this step for now.
genrule(
name = "create_java_tools_build_java" + java_version,
srcs = ["//tools/jdk:BUILD.java_tools"],
outs = ["remote_java_tools_java" + java_version + "/BUILD"],
cmd = "sed 's/JAVA_LANGUAGE_LEVEL/" + java_version + "/g' $< > $@",
)
[
genrule(
name = "create_java_tools_build_java" + java_version,
srcs = ["//tools/jdk:BUILD.java_tools"],
outs = ["remote_java_tools_java" + java_version + "/BUILD"],
cmd = "sed 's/JAVA_LANGUAGE_LEVEL/" + java_version + "/g' $< > $@",
),
genrule(
name = "create_java_tools_default_java" + java_version,
srcs = ["//tools/jdk:java_toolchain_default.bzl.java_tools"],
outs = ["remote_java_tools_java" + java_version + "/java_toolchain_default.bzl"],
cmd = "cp $< $@",
),
]
for java_version in JAVA_VERSIONS
]

Expand All @@ -643,7 +651,7 @@ JAVA_VERSIONS = ("11",)
name = "java_tools_java" + java_version + "_build_zip",
srcs = [
"remote_java_tools_java" + java_version + "/BUILD",
"//tools/jdk:java_toolchain_default.bzl",
"remote_java_tools_java" + java_version + "/java_toolchain_default.bzl",
],
outs = ["java_tools_java_" + java_version + "_build.zip"],
cmd = "zip -qjX $@ $(SRCS)",
Expand Down
145 changes: 95 additions & 50 deletions src/test/shell/bazel/bazel_java_tools_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ if "$is_windows"; then
export MSYS2_ARG_CONV_EXCL="*"
fi

function set_up() {
local java_tools_rlocation=$(rlocation io_bazel/src/java_tools_${JAVA_TOOLS_JAVA_VERSION}.zip)
local java_tools_zip_file_url="file://${java_tools_rlocation}"
if "$is_windows"; then
java_tools_zip_file_url="file:///${java_tools_rlocation}"
fi
cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "local_java_tools",
urls = ["${java_tools_zip_file_url}"]
)
EOF
}

function expect_path_in_java_tools() {
path="$1"; shift

Expand Down Expand Up @@ -165,75 +180,105 @@ function test_java_tools_has_proguard() {
}

function test_java_tools_toolchain_builds() {
local java_tools_rlocation=$(rlocation io_bazel/src/java_tools_${JAVA_TOOLS_JAVA_VERSION}.zip)
local java_tools_zip_file_url="file://${java_tools_rlocation}"
if "$is_windows"; then
java_tools_zip_file_url="file:///${java_tools_rlocation}"
fi
cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "local_java_tools",
urls = ["${java_tools_zip_file_url}"]
)
EOF
bazel build @local_java_tools//:toolchain || fail "toolchain failed to build"
}

function test_java_tools_singlejar_builds() {
local java_tools_rlocation=$(rlocation io_bazel/src/java_tools_${JAVA_TOOLS_JAVA_VERSION}.zip)
local java_tools_zip_file_url="file://${java_tools_rlocation}"
if "$is_windows"; then
java_tools_zip_file_url="file:///${java_tools_rlocation}"
fi
cat >WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "local_java_tools",
urls = ["${java_tools_zip_file_url}"]
)
EOF
bazel build @local_java_tools//:singlejar_cc_bin || fail "singlejar failed to build"
}

function test_java_tools_ijar_builds() {
local java_tools_rlocation=$(rlocation io_bazel/src/java_tools_${JAVA_TOOLS_JAVA_VERSION}.zip)
local java_tools_zip_file_url="file://${java_tools_rlocation}"
if "$is_windows"; then
java_tools_zip_file_url="file:///${java_tools_rlocation}"
fi
cat >WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "local_java_tools",
urls = ["${java_tools_zip_file_url}"]
)
EOF
bazel build @local_java_tools//:ijar_cc_binary || fail "ijar failed to build"
}


function test_java_toolchain_default() {
local java_tools_rlocation=$(rlocation io_bazel/src/java_tools_${JAVA_TOOLS_JAVA_VERSION}.zip)
local java_tools_zip_file_url="file://${java_tools_rlocation}"
if "$is_windows"; then
java_tools_zip_file_url="file:///${java_tools_rlocation}"
fi
cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "local_java_tools",
urls = ["${java_tools_zip_file_url}"]
)
EOF
function test_java_toolchain_default_manualConfiguration() {
cat > BUILD <<EOF
load("@local_java_tools//:java_toolchain_default.bzl", "java_toolchain_default")
java_toolchain_default(
name = "vanilla",
javabuilder = ["//:VanillaJavaBuilder"],
jvm_opts = [],
)
EOF
bazel build //:vanilla || fail "java_toolchain_default target failed to build"
}

function test_java_toolchain_default_manualConfigurationWithLocation() {
cat > BUILD <<EOF
load("@local_java_tools//:java_toolchain_default.bzl", "java_toolchain_default", "JDK9_JVM_OPTS")
java_toolchain_default(
name = "toolchain",
javac = ["//:javac_jar"],
jvm_opts = [
# In JDK9 we have seen a ~30% slow down in JavaBuilder performance when using
# G1 collector and having compact strings enabled.
"-XX:+UseParallelOldGC",
"-XX:-CompactStrings",
# override the javac in the JDK.
"--patch-module=java.compiler=\$(location //:java_compiler_jar)",
"--patch-module=jdk.compiler=\$(location //:jdk_compiler_jar)",
] + JDK9_JVM_OPTS,
tools = [
"//:java_compiler_jar",
"//:jdk_compiler_jar",
],
)
EOF
bazel build //:toolchain || fail "java_toolchain_default target failed to build"
}

function test_java_toolchain_default_jvm8Toolchain() {
cat > BUILD <<EOF
load("@local_java_tools//:java_toolchain_default.bzl", "java_toolchain_default", "JVM8_TOOLCHAIN_CONFIGURATION")
java_toolchain_default(
name = "jvm8_toolchain",
configuration = JVM8_TOOLCHAIN_CONFIGURATION,
)
EOF
bazel build //:jvm8_toolchain || fail "java_toolchain_default target failed to build"
}

function test_java_toolchain_default_javabuilderToolchain() {
cat > BUILD <<EOF
load("@local_java_tools//:java_toolchain_default.bzl", "java_toolchain_default", "JAVABUILDER_TOOLCHAIN_CONFIGURATION")
java_toolchain_default(
name = "javabuilder_toolchain",
configuration = JAVABUILDER_TOOLCHAIN_CONFIGURATION,
)
EOF
bazel build //:javabuilder_toolchain || fail "java_toolchain_default target failed to build"
}

function test_java_toolchain_default_vanillaToolchain() {
cat > BUILD <<EOF
load("@local_java_tools//:java_toolchain_default.bzl", "java_toolchain_default", "VANILLA_TOOLCHAIN_CONFIGURATION")
java_toolchain_default(
name = "vanilla_toolchain",
configuration = VANILLA_TOOLCHAIN_CONFIGURATION,
)
EOF
bazel build //:vanilla_toolchain || fail "java_toolchain_default target failed to build"
}

function test_java_toolchain_default_prebuiltToolchain() {
cat > BUILD <<EOF
load("@local_java_tools//:java_toolchain_default.bzl", "java_toolchain_default", "PREBUILT_TOOLCHAIN_CONFIGURATION")
java_toolchain_default(
name = "prebuilt_toolchain",
configuration = PREBUILT_TOOLCHAIN_CONFIGURATION,
)
EOF
bazel build //:prebuilt_toolchain || fail "java_toolchain_default target failed to build"
}

function test_java_toolchain_default_notInTools() {
cat > BUILD <<EOF
load("@bazel_tools//tools/jdk:java_toolchain_default.bzl", "java_toolchain_default")
filegroup(
name = "dummy",
)
EOF
(bazel build //:dummy && fail "java_toolchain_default accessible from @bazel_tools") || true
}

run_suite "Java tools archive tests"
2 changes: 1 addition & 1 deletion tools/jdk/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ filegroup(
"default_java_toolchain.bzl",
"fail_rule.bzl",
"java_toolchain_alias.bzl",
"java_toolchain_default.bzl",
"java_toolchain_default.bzl.java_tools",
"jdk.BUILD",
"local_java_repository.bzl",
"nosystemjdk/README",
Expand Down
Loading

0 comments on commit e68b1e3

Please sign in to comment.