Skip to content
This repository has been archived by the owner on Nov 29, 2022. It is now read-only.

Commit

Permalink
Use java toolchains
Browse files Browse the repository at this point in the history
The --javabase, and --java_toolchain options are removed in Bazel 5.0.
bazelbuild/bazel#7849

Also removed javacopt as the default java options includes "-g", which
generates all debugging information (source,lines,vars). See
https://cs.opensource.google/bazel/bazel/+/refs/tags/5.0.0:tools/jdk/default_java_toolchain.bzl;l=49

Two jdk11 toolchains are registered (in addition to the runtime
toolchain). *java8_definition and *java11_definition have different
values for source and target version compatibility. These toolchains can
be switched by using different values of --java_language_version.

Bug: 220902606
Test: presubmit passes
Change-Id: I3c59eb277279b40cc3371e3986c9a5980c370e3c
  • Loading branch information
Tom Renn committed Mar 24, 2022
1 parent 1d5c8e1 commit 3a967de
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
2 changes: 0 additions & 2 deletions bazel/bazel.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ def _iml_module_jar_impl(
deps = java_deps + kotlin_providers,
javac_opts = java_common.default_javac_opts(java_toolchain = java_toolchain) + ctx.attr.javacopts,
java_toolchain = java_toolchain,
host_javabase = find_java_runtime_toolchain(ctx, ctx.attr._host_javabase),
sourcepath = sourcepath,
# TODO(b/216385876) After updating to Bazel 5.0, use enable_compile_jar_action = use_ijar,
)
Expand Down Expand Up @@ -371,7 +370,6 @@ _iml_module_ = rule(
"data": attr.label_list(allow_files = True),
"test_data": attr.label_list(allow_files = True),
"_java_toolchain": attr.label(default = Label("@bazel_tools//tools/jdk:current_java_toolchain")),
"_host_javabase": attr.label(default = Label("@bazel_tools//tools/jdk:current_host_java_runtime")),
"_zipper": attr.label(
default = Label("@bazel_tools//tools/zip:zipper"),
cfg = "host",
Expand Down
10 changes: 4 additions & 6 deletions bazel/common.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ build:datasize_aspect --output_groups=+data_size

build --incompatible_strict_action_env # Ensures actions don't pick up env changes
build --sandbox_tmpfs_path=/tmp
build --javacopt="-g:source,lines,vars" # Keep debug info
# TODO (b/141269926): this generates a warning on windows when building for host,
# but is required when building for android.
build --copt=-fvisibility=hidden
Expand All @@ -58,11 +57,10 @@ build --define=grpc_no_ares=true
# finds make in the ndk-build script (see b/72179674).
build --test_env=SHELL

# Use prebuilt JDK when doing local bazel build/test
build --host_javabase=//prebuilts/studio/jdk:jdk11_runtime
build --javabase=//prebuilts/studio/jdk:jdk11_runtime
build --host_java_toolchain=@bazel_tools//tools/jdk:legacy_toolchain
build --java_toolchain=@bazel_tools//tools/jdk:legacy_toolchain
# Bazel will find the matching java toolchain registered in WORKSPACE.
build --java_language_version=8
build --java_runtime_version=jdk11
build --tool_java_runtime_version=jdk11

# Use prebuilt native toolchain
build --crosstool_top=@native_toolchain//:toolchain
Expand Down
12 changes: 4 additions & 8 deletions bazel/kotlin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,13 @@ def kotlin_test(
)

# Creates actions to generate the sources jar
def _sources(ctx, srcs, source_jars, jar, java_toolchain, host_javabase):
def _sources(ctx, srcs, source_jars, jar, java_toolchain):
java_common.pack_sources(
ctx.actions,
output_source_jar = jar,
sources = srcs,
source_jars = source_jars,
java_toolchain = java_toolchain,
host_javabase = host_javabase,
)

# Creates actions to generate a resources_jar from the given resources.
Expand Down Expand Up @@ -297,7 +296,6 @@ def _kotlin_library_impl(ctx):
jars += [resources_jar]

java_toolchain = find_java_toolchain(ctx, ctx.attr._java_toolchain)
host_javabase = find_java_runtime_toolchain(ctx, ctx.attr._host_javabase)

# Java
if java_srcs or source_jars:
Expand All @@ -310,8 +308,7 @@ def _kotlin_library_impl(ctx):
deps = deps + kotlin_providers,
javac_opts = java_common.default_javac_opts(java_toolchain = java_toolchain) + ctx.attr.javacopts,
java_toolchain = java_toolchain,
host_javabase = host_javabase,
plugins = [plugin[JavaInfo] for plugin in ctx.attr.plugins],
plugins = [plugin[JavaPluginInfo] for plugin in ctx.attr.plugins],
# TODO(b/216385876) After updating to Bazel 5.0, use enable_compile_jar_action = use_ijar,
)

Expand All @@ -337,7 +334,7 @@ def _kotlin_library_impl(ctx):
allow_duplicates = True,
)

_sources(ctx, java_srcs + kotlin_srcs, source_jars, ctx.outputs.source_jar, java_toolchain, host_javabase)
_sources(ctx, java_srcs + kotlin_srcs, source_jars, ctx.outputs.source_jar, java_toolchain)

java_info = JavaInfo(
output_jar = ctx.outputs.jar,
Expand Down Expand Up @@ -382,11 +379,10 @@ _kotlin_library = rule(
"kotlin_use_ir": attr.bool(),
"compress_resources": attr.bool(),
"plugins": attr.label_list(
providers = [JavaInfo],
providers = [JavaPluginInfo],
),
"stdlib": attr.label(),
"_java_toolchain": attr.label(default = Label("@bazel_tools//tools/jdk:current_java_toolchain")),
"_host_javabase": attr.label(default = Label("@bazel_tools//tools/jdk:current_host_java_runtime")),
"_bootclasspath": attr.label(
# Use JDK 8 because AGP still needs to support it (b/166472930).
default = Label("//prebuilts/studio/jdk:bootclasspath"),
Expand Down
3 changes: 3 additions & 0 deletions bazel/toplevel.WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ register_toolchains(
"@native_toolchain//:cc-toolchain-x64_windows-clang-cl",
"//tools/base/bazel/toolchains/darwin:python_toolchain",
"//tools/base/bazel/toolchains/darwin:python_toolchain_10.13",
"//prebuilts/studio/jdk:runtime_toolchain_definition",
"//prebuilts/studio/jdk:jdk11_toolchain_java8_definition",
"//prebuilts/studio/jdk:jdk11_toolchain_java11_definition",
)

local_repository(
Expand Down

0 comments on commit 3a967de

Please sign in to comment.