Use providers instead of structs for `java_toolchain` tools (#397) Bazel stores depsets in the fields of provider instances without their `Depset` wrapper objects, but it can't do so for structs. The tool structs of `java_toolchain` (`_javabuilder`, `_header_compiler`, `_header_compiler_direct`, `_bytecode_optimizer` and `_android_linter`) hold up to three depsets each and are retained for as long as the toolchain's configured target. Their fields stay the same, and Bazel's `JavaToolchainTool` reads them through `StructImpl`, which provider instances implement too. Verified with a heap dump of `bazel build --nobuild //src:bazel-dev` in the Bazel repo with this change overridden in, which no longer retains any `Depset` through a `struct(data, jvm_opts, tool)`. (ignore-relnotes) Closes #397 COPYBARA_INTEGRATE_REVIEW=https://github.com/bazelbuild/rules_java/pull/397 from fmeum:java-toolchain-tool-provider d9917d12cca97b64cef439ca8e2f9a052af873ad PiperOrigin-RevId: 1033343641 Change-Id: Idffbac74645ece9b2d04f9a1d5811bd51e4c56f4
diff --git a/java/common/rules/java_toolchain.bzl b/java/common/rules/java_toolchain.bzl index aa982f5..d148e5c 100644 --- a/java/common/rules/java_toolchain.bzl +++ b/java/common/rules/java_toolchain.bzl
@@ -78,6 +78,18 @@ init = _java_toolchain_info_init, ) +# Providers rather than structs since Bazel stores the depsets in their fields without the Depset +# wrapper objects. +_JavaToolchainToolInfo = provider( + doc = "An executable tool of the Java toolchain.", + fields = ["tool", "data", "jvm_opts"], +) + +_AndroidLinterInfo = provider( + doc = "The Android Lint tool of the Java toolchain.", + fields = ["tool", "tools", "data", "jvm_opts", "lint_opts", "runner_opts", "package_config"], +) + def _java_toolchain_impl(ctx): javac_opts_list = _get_javac_opts(ctx) bootclasspath_info = _get_bootclasspath_info(ctx) @@ -176,7 +188,7 @@ files_to_run = ctx.attr.android_lint_runner[DefaultInfo].files_to_run if not files_to_run or not files_to_run.executable: fail(ctx.attr.android_lint_runner.label, "does not refer to a valid executable target") - return struct( + return _AndroidLinterInfo( tool = files_to_run, tools = depset(ctx.files.android_lint_tools), data = depset(ctx.files.android_lint_data), @@ -194,7 +206,7 @@ if not files_to_run or not files_to_run.executable: fail(dep.label, "does not refer to a valid executable target") data = getattr(ctx.attr, data_attr) - return struct( + return _JavaToolchainToolInfo( tool = files_to_run, data = depset(getattr(ctx.files, data_attr)), jvm_opts = depset([ctx.expand_location(opt, data) for opt in getattr(ctx.attr, opts_attr)]), @@ -207,7 +219,7 @@ files_to_run = dep[DefaultInfo].files_to_run if not files_to_run or not files_to_run.executable: fail(dep.label, "does not refer to a valid executable target") - return struct(tool = files_to_run, data = depset(data), jvm_opts = depset(jvm_opts)) + return _JavaToolchainToolInfo(tool = files_to_run, data = depset(data), jvm_opts = depset(jvm_opts)) def _get_compatible_javacopts(ctx): result = {}