Skip to content

Commit

Permalink
Remove deploy jar Java runtime dependency for non-executable targets
Browse files Browse the repository at this point in the history
This allows deploy jars for `java_binary` targets with `create_executable = False` to compile for target platforms for which no standalone Java runtime is available (e.g. Android).

Along the way, this removes a redundant check (`runtime.hermetic_files` is never `None`) and ensures that files coming from the hermetic runtime are only added if `hermetic` is set to `True`.

Work towards #17085

Closes #19140.

PiperOrigin-RevId: 553855531
Change-Id: I56ce730190498e2adb2f8acba709f02ec9c13ef4
  • Loading branch information
fmeum authored and copybara-github committed Aug 4, 2023
1 parent 5ca89d4 commit 132d249
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ def _bazel_deploy_jars_impl(ctx):
return []

deploy_jars = make_deploy_jars_rule(implementation = _bazel_deploy_jars_impl)

deploy_jars_nonexec = make_deploy_jars_rule(implementation = _bazel_deploy_jars_impl, create_executable = False)
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ load(":bazel/java/bazel_java_binary.bzl", _java_test = "java_test", java_bin_exe
load(":bazel/java/bazel_java_binary_nolauncher.bzl", java_bin_exec_no_launcher_flag = "java_binary", java_test_no_launcher = "java_test")
load(":bazel/java/bazel_java_binary_custom_launcher.bzl", java_bin_exec_custom_launcher = "java_binary", java_test_custom_launcher = "java_test")
load(":bazel/java/bazel_java_binary_nonexec.bzl", java_bin_nonexec = "java_binary")
load(":bazel/java/bazel_java_binary_deploy_jar.bzl", "deploy_jars")
load(":bazel/java/bazel_java_binary_deploy_jar.bzl", "deploy_jars", "deploy_jars_nonexec")
load(":common/java/java_binary_wrapper.bzl", "register_java_binary_rules")

def java_binary(**kwargs):
Expand All @@ -32,6 +32,7 @@ def java_binary(**kwargs):
java_bin_exec_no_launcher_flag,
java_bin_exec_custom_launcher,
rule_deploy_jars = deploy_jars,
rule_deploy_jars_nonexec = deploy_jars_nonexec,
**kwargs
)

Expand All @@ -42,6 +43,7 @@ def java_test(**kwargs):
java_test_no_launcher,
java_test_custom_launcher,
rule_deploy_jars = deploy_jars,
rule_deploy_jars_nonexec = deploy_jars_nonexec,
is_test_rule_class = True,
**kwargs
)
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ def create_deploy_archive(
extra_args = []):
""" Creates a deploy jar
Requires a Java runtime toolchain if and only if hermetic is True.
Args:
ctx: (RuleContext) The rule context
launcher: (File) the launcher artifact
Expand All @@ -167,8 +169,6 @@ def create_deploy_archive(
add_opens: (depset)
extra_args: (list[Args]) Optional arguments for the deploy jar action
"""
runtime = semantics.find_java_runtime_toolchain(ctx)

input_files = []
input_files.extend(build_info_files)

Expand Down Expand Up @@ -216,18 +216,20 @@ def create_deploy_archive(
if multi_release:
args.add("--multi_release")

if hermetic and runtime.lib_modules != None and runtime.hermetic_files != None:
java_home = runtime.java_home
lib_modules = runtime.lib_modules
hermetic_files = runtime.hermetic_files
args.add("--hermetic_java_home", java_home)
args.add("--jdk_lib_modules", lib_modules)
args.add_all("--resources", hermetic_files)
input_files.append(lib_modules)
transitive_input_files.append(hermetic_files)
if hermetic:
runtime = semantics.find_java_runtime_toolchain(ctx)
if runtime.lib_modules != None:
java_home = runtime.java_home
lib_modules = runtime.lib_modules
hermetic_files = runtime.hermetic_files
args.add("--hermetic_java_home", java_home)
args.add("--jdk_lib_modules", lib_modules)
args.add_all("--resources", hermetic_files)
input_files.append(lib_modules)
transitive_input_files.append(hermetic_files)

if shared_archive == None:
shared_archive = runtime.default_cds
if shared_archive == None:
shared_archive = runtime.default_cds

if shared_archive:
input_files.append(shared_archive)
Expand Down Expand Up @@ -256,15 +258,19 @@ def _implicit_outputs(binary):
"unstrippeddeployjar": "%s_deploy.jar.unstripped" % binary_name,
}

def make_deploy_jars_rule(implementation):
def make_deploy_jars_rule(implementation, *, create_executable = True):
"""Creates the deploy jar auxiliary rule for java_binary
Args:
implementation: (Function) The rule implementation function
create_executable: (bool) The value of the create_executable attribute of java_binary
Returns:
The deploy jar rule class
"""
toolchains = [semantics.JAVA_TOOLCHAIN] + cc_helper.use_cpp_toolchain()
if create_executable:
toolchains.append(semantics.JAVA_RUNTIME_TOOLCHAIN)
return rule(
implementation = implementation,
attrs = {
Expand All @@ -276,12 +282,11 @@ def make_deploy_jars_rule(implementation):
),
"_cc_toolchain": attr.label(default = "@" + cc_semantics.get_repo() + "//tools/cpp:current_cc_toolchain"),
"_java_toolchain_type": attr.label(default = semantics.JAVA_TOOLCHAIN_TYPE),
"_java_runtime_toolchain_type": attr.label(default = semantics.JAVA_RUNTIME_TOOLCHAIN_TYPE),
"_build_info_translator": attr.label(
default = semantics.BUILD_INFO_TRANSLATOR_LABEL,
),
},
outputs = _implicit_outputs,
fragments = ["java"],
toolchains = [semantics.JAVA_TOOLCHAIN, semantics.JAVA_RUNTIME_TOOLCHAIN] + cc_helper.use_cpp_toolchain(),
toolchains = toolchains,
)
19 changes: 16 additions & 3 deletions src/main/starlark/builtins_bzl/common/java/java_binary_wrapper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,34 @@ the supplied value of the `create_executable` attribute.

_DEPLOY_JAR_RULE_NAME_SUFFIX = "_deployjars_internal_rule"

def register_java_binary_rules(rule_exec, rule_nonexec, rule_nolauncher, rule_customlauncher, rule_deploy_jars = None, is_test_rule_class = False, **kwargs):
def register_java_binary_rules(
rule_exec,
rule_nonexec,
rule_nolauncher,
rule_customlauncher,
rule_deploy_jars = None,
rule_deploy_jars_nonexec = None,
is_test_rule_class = False,
**kwargs):
"""Registers the correct java_binary rule and deploy jar rule
Args:
rule_exec: (Rule) The executable java_binary rule
rule_nonexec: (Rule) The non-executable java_binary rule
rule_nolauncher: (Rule) The executable java_binary rule without launcher flag resolution
rule_customlauncher: (Rule) The executable java_binary rule with a custom launcher attr set
rule_deploy_jars: (Rule) The auxiliary deploy jars rule
rule_deploy_jars: (Rule) The auxiliary deploy jars rule for create_executable = True
rule_deploy_jars_nonexec: (Rule) The auxiliary deploy jars rule for create_executable = False
is_test_rule_class: (bool) If this is a test rule
**kwargs: Actual args to instantiate the rule
"""

create_executable = "create_executable" not in kwargs or kwargs["create_executable"]

# TODO(hvd): migrate depot to integers / maybe use decompose_select_list()
if "stamp" in kwargs and type(kwargs["stamp"]) == type(True):
kwargs["stamp"] = 1 if kwargs["stamp"] else 0
if "create_executable" in kwargs and not kwargs["create_executable"]:
if not create_executable:
rule_nonexec(**kwargs)
elif "use_launcher" in kwargs and not kwargs["use_launcher"]:
rule_nolauncher(**kwargs)
Expand All @@ -45,6 +56,8 @@ def register_java_binary_rules(rule_exec, rule_nonexec, rule_nolauncher, rule_cu
else:
rule_exec(**kwargs)

if not create_executable:
rule_deploy_jars = rule_deploy_jars_nonexec
if rule_deploy_jars and (
not kwargs.get("tags", []) or "nodeployjar" not in kwargs.get("tags", [])
):
Expand Down

0 comments on commit 132d249

Please sign in to comment.