Skip to content

Commit

Permalink
Update cargo_build_script to always render custom runfiles dirs.
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Sep 24, 2024
1 parent 144d34f commit 9a35c22
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 183 deletions.
4 changes: 3 additions & 1 deletion cargo/cargo_build_script_runner/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ impl BuildScriptOutput {
pub fn outputs_from_command(
cmd: &mut Command,
) -> Result<(Vec<BuildScriptOutput>, Output), Output> {
let child_output = cmd.output().expect("Unable to start binary");
let child_output = cmd
.output()
.unwrap_or_else(|e| panic!("Unable to start command:\n{:#?}\n{:?}", cmd, e));
if child_output.status.success() {
let reader = BufReader::new(child_output.stdout.as_slice());
let output = Self::outputs_from_reader(reader);
Expand Down
6 changes: 0 additions & 6 deletions cargo/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load(":runfiles_enabled.bzl", "runfiles_enabled_build_setting")

bzl_library(
name = "bzl_lib",
srcs = glob(["**/*.bzl"]),
visibility = ["//:__subpackages__"],
)

runfiles_enabled_build_setting(
name = "runfiles_enabled",
visibility = ["//visibility:public"],
)
38 changes: 30 additions & 8 deletions cargo/private/cargo_build_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ load(
"find_toolchain",
_name_to_crate_name = "name_to_crate_name",
)
load(":runfiles_enabled.bzl", "is_runfiles_enabled", "runfiles_enabled_attr")

# Reexport for cargo_build_script_wrapper.bzl
name_to_crate_name = _name_to_crate_name
Expand Down Expand Up @@ -206,6 +205,23 @@ def _rlocationpath(file, workspace_name):
return "{}/{}".format(workspace_name, file.short_path)

def _create_runfiles_dir(ctx, script):
"""Create a runfiles directory to represent `CARGO_MANIFEST_DIR`.
Due to the inability to forcibly generate runfiles directories for use as inputs
to actions, this function creates a custom runfiles directory that can more
consistently be relied upon as an input. For more details see:
https://github.com/bazelbuild/bazel/issues/15486
If runfiles directories can ever be more directly treated as an input this function
can be retired.
Args:
ctx (ctx): The rule's context object
script (Target): The `cargo_build_script.script` target.
Returns:
File: The directory created
"""
runfiles_dir = ctx.actions.declare_directory("{}.cargo_runfiles".format(ctx.label.name))

# External repos always fall into the `../` branch of `_rlocationpath`.
Expand Down Expand Up @@ -264,13 +280,18 @@ def _cargo_build_script_impl(ctx):
if not workspace_name:
workspace_name = ctx.workspace_name

if not is_runfiles_enabled(ctx.attr):
# Relying on runfiles directories is unreliable when passing data to
# dependent actions. Instead, an explicit directory should be created
# until more reliable functionality is implemented in Bazel:
# https://github.com/bazelbuild/bazel/issues/15486
incompatible_runfiles_cargo_manifest_dir = ctx.attr._incompatible_runfiles_cargo_manifest_dir[BuildSettingInfo].value
if incompatible_runfiles_cargo_manifest_dir:
script_data.append(ctx.attr.script[DefaultInfo].default_runfiles.files)
manifest_dir = "{}.runfiles/{}/{}".format(script.path, workspace_name, ctx.label.package)
else:
runfiles_dir = _create_runfiles_dir(ctx, ctx.attr.script)
script_data.append(depset([runfiles_dir]))
manifest_dir = "{}/{}/{}".format(runfiles_dir.path, workspace_name, ctx.label.package)
else:
script_data.append(ctx.attr.script[DefaultInfo].default_runfiles.files)
manifest_dir = "{}.runfiles/{}/{}".format(script.path, workspace_name, ctx.label.package)

streams = struct(
stdout = ctx.actions.declare_file(ctx.label.name + ".stdout.log"),
Expand Down Expand Up @@ -564,14 +585,15 @@ cargo_build_script = rule(
"_experimental_symlink_execroot": attr.label(
default = Label("//cargo/settings:experimental_symlink_execroot"),
),
"_incompatible_runfiles_cargo_manifest_dir": attr.label(
default = Label("//cargo/settings:incompatible_runfiles_cargo_manifest_dir"),
),
"_runfiles_maker": attr.label(
cfg = "exec",
executable = True,
default = Label("//cargo/private/runfiles_maker"),
),
} | runfiles_enabled_attr(
default = Label("//cargo/private:runfiles_enabled"),
),
},
fragments = ["cpp"],
toolchains = [
str(Label("//rust:toolchain_type")),
Expand Down
167 changes: 0 additions & 167 deletions cargo/private/runfiles_enabled.bzl

This file was deleted.

2 changes: 1 addition & 1 deletion cargo/private/runfiles_maker/runfiles_maker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn main() {
.expect("Failed to create output directory");
std::fs::copy(src, &out_dest).unwrap_or_else(|e| {
panic!(
"Failed to copy file {} -> {}\n{:?}",
"Failed to copy file `{} -> {}`\n{:?}",
src.display(),
out_dest.display(),
e
Expand Down
9 changes: 9 additions & 0 deletions cargo/settings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@ bool_flag(
name = "experimental_symlink_execroot",
build_setting_default = False,
)

# A flag which causes `cargo_build_script` to write an explicit `CARGO_MANFIEST_DIR`
# directory from an action instead of using runfiles directories which cannot be
# passed to downstream actions.
# https://github.com/bazelbuild/bazel/issues/15486
bool_flag(
name = "incompatible_runfiles_cargo_manifest_dir",
build_setting_default = False,
)

0 comments on commit 9a35c22

Please sign in to comment.