Skip to content

Commit

Permalink
Add a bootclasspath attribute to java_ rules
Browse files Browse the repository at this point in the history
If set, it overrides the `BootClassPathInfo` used for compilation, instead of using the default from the `java_toolchain`.

PiperOrigin-RevId: 583461301
Change-Id: I7d66034824769552957ec89f37e6048581f0b1a8
  • Loading branch information
cushon authored and copybara-github committed Nov 17, 2023
1 parent 79ae49c commit ca84e54
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Common code for reuse across java_* rules

load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/java/android_lint.bzl", "android_lint_subrule")
load(":common/java/boot_class_path_info.bzl", "BootClassPathInfo")
load(":common/java/compile_action.bzl", "compile_action")
load(":common/java/java_common.bzl", "java_common")
load(":common/java/java_common_internal_for_builtins.bzl", "target_kind")
Expand Down Expand Up @@ -74,7 +75,8 @@ def basic_java_library(
coverage_config = None,
proguard_specs = None,
add_exports = [],
add_opens = []):
add_opens = [],
bootclasspath = None):
"""
Creates actions that compile and lint Java sources, sets up coverage and returns JavaInfo, InstrumentedFilesInfo and output groups.
Expand Down Expand Up @@ -108,6 +110,7 @@ def basic_java_library(
Proguard validation is done only when the parameter is set.
add_exports: (list[str]) Allow this library to access the given <module>/<package>.
add_opens: (list[str]) Allow this library to reflectively access the given <module>/<package>.
bootclasspath: (Target) The JDK APIs to compile this library against.
Returns:
(dict[str, Provider],
{files_to_build: list[File],
Expand Down Expand Up @@ -146,6 +149,7 @@ def basic_java_library(
enable_compile_jar_action,
add_exports = add_exports,
add_opens = add_opens,
bootclasspath = bootclasspath[BootClassPathInfo] if bootclasspath else None,
)
target = {"JavaInfo": java_info}

Expand Down
7 changes: 5 additions & 2 deletions src/main/starlark/builtins_bzl/common/java/compile_action.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
Java compile action
"""

load(":common/java/java_semantics.bzl", "semantics")
load(":common/java/java_common_internal_for_builtins.bzl", _compile_private_for_builtins = "compile")
load(":common/java/java_semantics.bzl", "semantics")

def _filter_strict_deps(mode):
return "error" if mode in ["strict", "default"] else mode
Expand Down Expand Up @@ -56,7 +56,8 @@ def compile_action(
strict_deps = "ERROR",
enable_compile_jar_action = True,
add_exports = [],
add_opens = []):
add_opens = [],
bootclasspath = None):
"""
Creates actions that compile Java sources, produce source jar, and produce header jar and returns JavaInfo.
Expand Down Expand Up @@ -117,6 +118,7 @@ def compile_action(
by non-library targets such as binaries that do not have dependants.
add_exports: (list[str]) Allow this library to access the given <module>/<package>.
add_opens: (list[str]) Allow this library to reflectively access the given <module>/<package>.
bootclasspath: (BootClassPathInfo) The set of JDK APIs to compile this library against.
Returns:
((JavaInfo, {files_to_build: list[File],
Expand Down Expand Up @@ -152,6 +154,7 @@ def compile_action(
enable_compile_jar_action = enable_compile_jar_action,
add_exports = add_exports,
add_opens = add_opens,
bootclasspath = bootclasspath,
)

compilation_info = struct(
Expand Down
6 changes: 6 additions & 0 deletions src/main/starlark/builtins_bzl/common/java/java_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ load(":common/cc/cc_common.bzl", "cc_common")
load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/cc/semantics.bzl", cc_semantics = "semantics")
load(":common/java/basic_java_library.bzl", "BASIC_JAVA_LIBRARY_IMPLICIT_ATTRS", "basic_java_library", "collect_deps")
load(":common/java/boot_class_path_info.bzl", "BootClassPathInfo")
load(":common/java/java_common.bzl", "java_common")
load(
":common/java/java_common_internal_for_builtins.bzl",
Expand Down Expand Up @@ -125,6 +126,7 @@ def basic_java_binary(
coverage_config = coverage_config,
add_exports = ctx.attr.add_exports,
add_opens = ctx.attr.add_opens,
bootclasspath = ctx.attr.bootclasspath,
)
java_info = target["JavaInfo"]
compilation_info = java_info.compilation_info
Expand Down Expand Up @@ -532,6 +534,10 @@ BASIC_JAVA_BINARY_ATTRIBUTES = merge_attrs(
allow_files = False,
# TODO(b/295221112): add back CcLauncherInfo
),
"bootclasspath": attr.label(
providers = [BootClassPathInfo],
flags = ["SKIP_CONSTRAINTS_OVERRIDE"],
),
"neverlink": attr.bool(),
"javacopts": attr.string_list(),
"add_exports": attr.string_list(),
Expand Down
11 changes: 10 additions & 1 deletion src/main/starlark/builtins_bzl/common/java/java_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Definition of java_library rule.
load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/java/android_lint.bzl", "android_lint_subrule")
load(":common/java/basic_java_library.bzl", "BASIC_JAVA_LIBRARY_IMPLICIT_ATTRS", "basic_java_library", "construct_defaultinfo")
load(":common/java/boot_class_path_info.bzl", "BootClassPathInfo")
load(":common/java/java_info.bzl", "JavaInfo", "JavaPluginInfo")
load(":common/java/java_semantics.bzl", "semantics")
load(":common/rule_util.bzl", "merge_attrs")
Expand All @@ -36,7 +37,8 @@ def bazel_java_library_rule(
neverlink = False,
proguard_specs = [],
add_exports = [],
add_opens = []):
add_opens = [],
bootclasspath = None):
"""Implements java_library.
Use this call when you need to produce a fully fledged java_library from
Expand All @@ -57,6 +59,7 @@ def bazel_java_library_rule(
proguard_specs: (list[File]) Files to be used as Proguard specification.
add_exports: (list[str]) Allow this library to access the given <module>/<package>.
add_opens: (list[str]) Allow this library to reflectively access the given <module>/<package>.
bootclasspath: (Target) The JDK APIs to compile this library against.
Returns:
(dict[str, provider]) A list containing DefaultInfo, JavaInfo,
InstrumentedFilesInfo, OutputGroupsInfo, ProguardSpecProvider providers.
Expand All @@ -80,6 +83,7 @@ def bazel_java_library_rule(
proguard_specs = proguard_specs,
add_exports = add_exports,
add_opens = add_opens,
bootclasspath = bootclasspath,
)

target["DefaultInfo"] = construct_defaultinfo(
Expand Down Expand Up @@ -109,6 +113,7 @@ def _proxy(ctx):
ctx.files.proguard_specs,
ctx.attr.add_exports,
ctx.attr.add_opens,
ctx.attr.bootclasspath,
).values()

JAVA_LIBRARY_IMPLICIT_ATTRS = BASIC_JAVA_LIBRARY_IMPLICIT_ATTRS
Expand Down Expand Up @@ -156,6 +161,10 @@ JAVA_LIBRARY_ATTRS = merge_attrs(
providers = [JavaPluginInfo],
cfg = "exec",
),
"bootclasspath": attr.label(
providers = [BootClassPathInfo],
flags = ["SKIP_CONSTRAINTS_OVERRIDE"],
),
"javacopts": attr.string_list(),
"neverlink": attr.bool(),
"resource_strip_prefix": attr.string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ public static List<String> getSourcePathEntries(JavaCompileAction javac) throws
return getOptions(javac).getSourcePath();
}

public static List<String> getBootclasspath(JavaCompileAction javac) throws Exception {
return getOptions(javac).getBootClassPath();
}

/** Returns the JavaBuilder command line, up to the main class or deploy jar. */
public static List<String> getJavacCommand(JavaCompileAction action) throws Exception {
List<String> args = action.getCommandLines().allArguments();
Expand Down

0 comments on commit ca84e54

Please sign in to comment.