Skip to content

Commit

Permalink
Track build script compile data through to rustc actions
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Sep 19, 2024
1 parent 5b98341 commit d277e02
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
25 changes: 12 additions & 13 deletions cargo/private/cargo_build_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ def _cargo_build_script_impl(ctx):
list: A list containing a BuildInfo provider
"""
script = ctx.executable.script
script_info = ctx.attr.script[CargoBuildScriptRunfilesInfo]
toolchain = find_toolchain(ctx)
out_dir = ctx.actions.declare_directory(ctx.label.name + ".out_dir")
env_out = ctx.actions.declare_file(ctx.label.name + ".env")
Expand All @@ -251,7 +252,14 @@ def _cargo_build_script_impl(ctx):
link_search_paths = ctx.actions.declare_file(ctx.label.name + ".linksearchpaths") # rustc-link-search, propagated from transitive dependencies
compilation_mode_opt_level = get_compilation_mode_opts(ctx, toolchain).opt_level

build_script_inputs = []
script_tools = []
script_data = []
for target in script_info.data:
script_data.append(target[DefaultInfo].files)
script_data.append(target[DefaultInfo].default_runfiles.files)
for target in script_info.tools:
script_tools.append(target[DefaultInfo].files)
script_tools.append(target[DefaultInfo].default_runfiles.files)

workspace_name = ctx.label.workspace_name
if not workspace_name:
Expand All @@ -260,7 +268,7 @@ def _cargo_build_script_impl(ctx):
manifest_dir = "{}.runfiles/{}/{}".format(script.path, workspace_name, ctx.label.package)
if not is_runfiles_enabled(ctx.attr):
runfiles_dir = _create_runfiles_dir(ctx, ctx.attr.script)
build_script_inputs.append(runfiles_dir)
script_data.append(depset([runfiles_dir]))
manifest_dir = "{}/{}/{}".format(runfiles_dir.path, workspace_name, ctx.label.package)

streams = struct(
Expand Down Expand Up @@ -376,8 +384,6 @@ def _cargo_build_script_impl(ctx):
variables = getattr(target[platform_common.TemplateVariableInfo], "variables", depset([]))
env.update(variables)

script_info = ctx.attr.script[CargoBuildScriptRunfilesInfo]

_merge_env_dict(env, expand_dict_value_locations(
ctx,
ctx.attr.build_script_env,
Expand All @@ -388,15 +394,6 @@ def _cargo_build_script_impl(ctx):
script_info.tools,
))

script_tools = []
script_data = []
for target in script_info.data:
script_data.append(target[DefaultInfo].files)
script_data.append(target[DefaultInfo].default_runfiles.files)
for target in script_info.tools:
script_tools.append(target[DefaultInfo].files)
script_tools.append(target[DefaultInfo].default_runfiles.files)

tools = depset(
direct = [
script,
Expand All @@ -423,6 +420,8 @@ def _cargo_build_script_impl(ctx):
args.add(streams.stderr)
args.add(ctx.attr.rundir)

build_script_inputs = []

for dep in ctx.attr.link_deps:
if rust_common.dep_info in dep and dep[rust_common.dep_info].dep_env:
dep_env_file = dep[rust_common.dep_info].dep_env
Expand Down
22 changes: 15 additions & 7 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ def collect_deps(
transitive_build_infos.append(depset([build_info]))
if build_info.link_search_paths:
transitive_link_search_paths.append(depset([build_info.link_search_paths]))
transitive_data.append(build_info.compile_data)
else:
fail("rust targets can only depend on rust_library, rust_*_library or cc_library " +
"targets.")
Expand Down Expand Up @@ -470,14 +471,12 @@ def get_linker_and_args(ctx, attr, crate_type, cc_toolchain, feature_configurati
def _process_build_scripts(
build_info,
dep_info,
compile_inputs,
include_link_flags = True):
"""Gathers the outputs from a target's `cargo_build_script` action.
Args:
build_info (BuildInfo): The target Build's dependency info.
dep_info (DepInfo): The Depinfo provider form the target Crate's set of inputs.
compile_inputs (depset): A set of all files that will participate in the build.
include_link_flags (bool, optional): Whether to include flags like `-l` that instruct the linker to search for a library.
Returns:
Expand All @@ -488,8 +487,7 @@ def _process_build_scripts(
- (depset[File]): All direct and transitive build flags from the current build info.
"""
extra_inputs, out_dir, build_env_file, build_flags_files = _create_extra_input_args(build_info, dep_info, include_link_flags = include_link_flags)
compile_inputs = depset(transitive = [extra_inputs, compile_inputs])
return compile_inputs, out_dir, build_env_file, build_flags_files
return extra_inputs, out_dir, build_env_file, build_flags_files

def _symlink_for_ambiguous_lib(actions, toolchain, crate_info, lib):
"""Constructs a disambiguating symlink for a library dependency.
Expand Down Expand Up @@ -780,13 +778,18 @@ def collect_inputs(
],
)

build_script_compile_inputs, out_dir, build_env_file, build_flags_files = _process_build_scripts(
build_info = build_info,
dep_info = dep_info,
include_link_flags = include_link_flags,
)

# For backwards compatibility, we also check the value of the `rustc_env_files` attribute when
# `crate_info.rustc_env_files` is not populated.
build_env_files = crate_info.rustc_env_files if crate_info.rustc_env_files else getattr(files, "rustc_env_files", [])
compile_inputs, out_dir, build_env_file, build_flags_files = _process_build_scripts(build_info, dep_info, compile_inputs, include_link_flags = include_link_flags)
if build_env_file:
build_env_files = [f for f in build_env_files] + [build_env_file]
compile_inputs = depset(build_env_files, transitive = [compile_inputs])
compile_inputs = depset(build_env_files, transitive = [build_script_compile_inputs, compile_inputs])
return compile_inputs, out_dir, build_env_files, build_flags_files, linkstamp_outs, ambiguous_libs

def construct_arguments(
Expand Down Expand Up @@ -1731,8 +1734,13 @@ def _create_extra_input_args(build_info, dep_info, include_link_flags = True):

input_depsets.append(build_info.compile_data)

out_dir_compile_inputs = depset(
input_files,
transitive = [dep_info.link_search_path_files, dep_info.transitive_data] + input_depsets,
)

return (
depset(input_files, transitive = [dep_info.link_search_path_files] + input_depsets),
out_dir_compile_inputs,
out_dir,
build_env_file,
depset(build_flags_files, transitive = [dep_info.link_search_path_files]),
Expand Down

0 comments on commit d277e02

Please sign in to comment.