Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(native_image): clean up construction of inputs #70

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 34 additions & 27 deletions internal/native_image/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -103,32 +103,43 @@ def _graal_binary_implementation(ctx):
dep[JavaInfo].transitive_runtime_jars
for dep in ctx.attr.deps
])
all_deps = depset([], transitive = [classpath_depset])

if graal_attr == None:
direct_inputs = []
transitive_inputs = [classpath_depset]

# if we aren't handed a specific `native-image` tool binary, or we are running
# under the modern `native_image` rule, then attempt to resolve via toolchains...
if graal_attr == None and not (ctx.attr._legacy_rule):
# resolve via toolchains
info = ctx.toolchains[_GVM_TOOLCHAIN_TYPE].graalvm
graal_attr = info.native_image_bin

# but fall back to explicitly-provided tool, which should override, with the
# remainder of the resolved toolchain present
resolved_graal = graal_attr or info.native_image_bin
gvm_toolchain = info
extra_tool_deps.append(info.gvm_files)

graal_inputs, _ = ctx.resolve_tools(tools = [
graal_attr,
resolved_graal,
] + extra_tool_deps)

graal = graal_inputs.to_list()[0]
all_deps = depset(transitive = [
classpath_depset,
gvm_toolchain.gvm_files.files,
])
else:
# otherwise, use the legacy code path

# if we're using an explicit tool, add it to the direct inputs
if graal_attr:
direct_inputs.append(graal_inputs)

# add toolchain files to transitive inputs
transitive_inputs.append(gvm_toolchain.gvm_files[DefaultInfo].files)
elif graal_attr != None:
# otherwise, use the legacy code path. the `graal` value is used in the run
# `executable` so it isn't listed in deps for earlier Bazel versions.
graal_inputs, _, _ = ctx.resolve_command(tools = [
graal_attr,
])
graal = graal_inputs[0]

if graal_attr == None:
direct_inputs.append(graal)
else:
# still failed to resolve: cannot resolve via either toolchains or attributes.
fail("""
No `native-image` tool found. Please either define a `native_image_tool` in your target,
Expand Down Expand Up @@ -158,6 +169,7 @@ def _graal_binary_implementation(ctx):
feature_configuration = feature_configuration,
action_name = CPP_LINK_DYNAMIC_LIBRARY_ACTION_NAME,
)
transitive_inputs.append(cc_toolchain.all_files)

env = {}
path_set = {}
Expand Down Expand Up @@ -251,38 +263,33 @@ def _graal_binary_implementation(ctx):

if ctx.attr.reflection_configuration != None:
args.add(ctx.file.reflection_configuration, format = "-H:ReflectionConfigurationFiles=%s")
all_deps = depset([ctx.file.reflection_configuration], transitive = [all_deps])
direct_inputs.append(ctx.file.reflection_configuration)

if ctx.attr.include_resources != None:
args.add(ctx.attr.include_resources, format = "-H:IncludeResources=%s")

if ctx.attr.jni_configuration != None:
args.add(ctx.file.jni_configuration, format = "-H:JNIConfigurationFiles=%s")
all_deps = depset([ctx.file.jni_configuration], transitive = [all_deps])
direct_inputs.append(ctx.file.jni_configuration)
args.add("-H:+JNI")

inputs = depset(direct_inputs, transitive = transitive_inputs)

run_params = {
"outputs": [binary],
"arguments": [args],
"executable": graal,
"inputs": inputs,
"mnemonic": "NativeImage",
"env": env,
}

# fix: on bazel4, the `tools` parameter is expected to be a `File or FilesToRunProvider`, whereas
# on later versions it is expected to be a `depset`. there is also no `toolchain` parameter at all.
if not ctx.attr._legacy_rule:
run_params.update(**{
"inputs": all_deps,
"use_default_shell_env": ctx.attr.enable_default_shell_env,
"progress_message": "Compiling native image %{label}",
"toolchain": Label(_GVM_TOOLCHAIN_TYPE),
"tools": [
ctx.attr._cc_toolchain.files,
],
})
else:
run_params["inputs"] = classpath_depset
run_params.update(
use_default_shell_env = ctx.attr.enable_default_shell_env,
progress_message = "Compiling native image %{label}",
toolchain = Label(_GVM_TOOLCHAIN_TYPE),
)

ctx.actions.run(
**run_params
Expand Down
Loading