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 20, 2024
1 parent 144d34f commit 631255f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 184 deletions.
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"],
)
33 changes: 22 additions & 11 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,10 @@ def _cargo_build_script_impl(ctx):
if not workspace_name:
workspace_name = ctx.workspace_name

if not is_runfiles_enabled(ctx.attr):
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)
# Create a custom runfiles directory to represent `CARGO_MANIFEST_DIR`.
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)

streams = struct(
stdout = ctx.actions.declare_file(ctx.label.name + ".stdout.log"),
Expand Down Expand Up @@ -569,9 +582,7 @@ cargo_build_script = rule(
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.

0 comments on commit 631255f

Please sign in to comment.