Skip to content

Commit

Permalink
Update cargo_build_script to work without runfiles support. (bazelb…
Browse files Browse the repository at this point in the history
…uild#2871)

Partially addresses bazelbuild#1156
  • Loading branch information
UebelAndre committed Sep 19, 2024
1 parent 6ec890f commit d60d7c2
Show file tree
Hide file tree
Showing 22 changed files with 470 additions and 180 deletions.
45 changes: 43 additions & 2 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ default_windows_targets: &default_windows_targets
- "//..."
- "-//test/proto/..."
- "-//test/unit/pipelined_compilation/..."
default_windows_no_runfiles_targets: &default_windows_no_runfiles_targets
- "--" # Allows negative patterns; hack for https://github.com/bazelbuild/continuous-integration/pull/245
- "//..."
# TODO: https://github.com/bazelbuild/rules_rust/issues/1156
- "-//crate_universe/..."
- "-//test/chained_direct_deps:mod3_doc_test"
- "-//test/out_dir_in_tests:demo_lib_doc_test"
- "-//test/proto/..."
- "-//test/rustc_env_files:output_test"
- "-//test/test_env_launcher:test"
- "-//test/test_env:test_manifest_dir"
- "-//test/test_env:test_run"
- "-//test/unit/pipelined_compilation/..."
- "-//test/unit/rustdoc/..."
- "-//tools/runfiles/..."
crate_universe_vendor_example_targets: &crate_universe_vendor_example_targets
- "//vendor_external:crates_vendor"
- "//vendor_local_manifests:crates_vendor"
Expand All @@ -58,26 +73,39 @@ split_coverage_postprocessing_shell_commands: &split_coverage_postprocessing_she
- echo "build --//rust/settings:experimental_use_coverage_metadata_files" >> user.bazelrc
tasks:
ubuntu2004:
platform: ubuntu2004
build_targets: *default_linux_targets
test_targets: *default_linux_targets
coverage_targets: *default_linux_targets
post_shell_commands: *coverage_validation_post_shell_commands
run_targets:
- //test:query_test_binary
rbe_ubuntu2004:
platform: rbe_ubuntu2004
shell_commands:
- sed -i 's/^# load("@bazel_ci_rules/load("@bazel_ci_rules/' WORKSPACE.bazel
- sed -i 's/^# rbe_preconfig/rbe_preconfig/' WORKSPACE.bazel
build_targets: *default_linux_targets
test_targets: *default_linux_targets
macos:
platform: macos
build_targets: *default_macos_targets
test_targets: *default_macos_targets
coverage_targets: *default_macos_targets
post_shell_commands: *coverage_validation_post_shell_commands
windows:
platform: windows
build_targets: *default_windows_targets
test_targets: *default_windows_targets
windows_no_runfiles:
name: No Runfiles
platform: windows
build_flags:
- "--noenable_runfiles"
test_flags:
- "--noenable_runfiles"
build_targets: *default_windows_no_runfiles_targets
test_targets: *default_windows_no_runfiles_targets
ubuntu2004_split_coverage_postprocessing:
name: Split Coverage Postprocessing
platform: ubuntu2004
Expand Down Expand Up @@ -171,6 +199,19 @@ tasks:
- "--config=clippy"
build_targets: *default_windows_targets
test_targets: *default_windows_targets
windows_no_runfiles_with_aspects:
name: No Runfiles With Aspects
platform: windows
build_flags:
- "--noenable_runfiles"
- "--config=rustfmt"
- "--config=clippy"
test_flags:
- "--noenable_runfiles"
- "--config=rustfmt"
- "--config=clippy"
build_targets: *default_windows_no_runfiles_targets
test_targets: *default_windows_no_runfiles_targets
windows_rolling_with_aspects:
name: "Windows Rolling Bazel Version With Aspects"
platform: windows
Expand Down Expand Up @@ -610,7 +651,7 @@ tasks:
environment:
# This ndk version matches with rules_android_ndk repo's CI
# https://github.com/bazelbuild/rules_android_ndk/blob/877c68ef34c9f3353028bf490d269230c1990483/.bazelci/presubmit.yml#L37
# The ndk is installed by this script
# The ndk is installed by this script
# https://github.com/bazelbuild/continuous-integration/blob/ba56013373821feadd9f2eaa6b81eb19528795f0/macos/mac-android.sh
ANDROID_NDK_HOME: /opt/android-ndk-r25b
android_examples_macos:
Expand All @@ -625,7 +666,7 @@ tasks:
environment:
# This ndk version matches with rules_android_ndk repo's CI
# https://github.com/bazelbuild/rules_android_ndk/blob/877c68ef34c9f3353028bf490d269230c1990483/.bazelci/presubmit.yml#L42
# The ndk is installed by this script
# The ndk is installed by this script
# https://github.com/bazelbuild/continuous-integration/blob/ba56013373821feadd9f2eaa6b81eb19528795f0/macos/mac-android.sh
ANDROID_NDK_HOME: /Users/buildkite/android-ndk-r25b
ios_examples:
Expand Down
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{:#?}", e, cmd));
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: 6 additions & 0 deletions cargo/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
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"],
)
74 changes: 61 additions & 13 deletions cargo/private/cargo_build_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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 @@ -198,6 +199,39 @@ def _feature_enabled(ctx, feature_name, default = False):

return default

def _rlocationpath(file, workspace_name):
if file.short_path.startswith("../"):
return file.short_path[len("../"):]

return "{}/{}".format(workspace_name, file.short_path)

def _create_runfiles_dir(ctx, script):
runfiles_dir = ctx.actions.declare_directory("{}.cargo_runfiles".format(ctx.label.name))

workspace_name = ctx.label.workspace_name
if not workspace_name:
workspace_name = ctx.workspace_name

def _runfiles_map(file):
return "{}={}".format(file.path, _rlocationpath(file, workspace_name))

runfiles = script[DefaultInfo].default_runfiles

args = ctx.actions.args()
args.use_param_file("@%s", use_always = True)
args.add(runfiles_dir.path)
args.add_all(runfiles.files, map_each = _runfiles_map, allow_closure = True)

ctx.actions.run(
mnemonic = "CargoBuildScriptRunfilesDir",
executable = ctx.executable._runfiles_maker,
arguments = [args],
inputs = runfiles.files,
outputs = [runfiles_dir],
)

return runfiles_dir

def _cargo_build_script_impl(ctx):
"""The implementation for the `cargo_build_script` rule.
Expand All @@ -208,16 +242,35 @@ 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")
dep_env_out = ctx.actions.declare_file(ctx.label.name + ".depenv")
flags_out = ctx.actions.declare_file(ctx.label.name + ".flags")
link_flags = ctx.actions.declare_file(ctx.label.name + ".linkflags")
link_search_paths = ctx.actions.declare_file(ctx.label.name + ".linksearchpaths") # rustc-link-search, propagated from transitive dependencies
manifest_dir = "%s.runfiles/%s/%s" % (script.path, ctx.label.workspace_name or ctx.workspace_name, ctx.label.package)
compilation_mode_opt_level = get_compilation_mode_opts(ctx, toolchain).opt_level

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:
workspace_name = ctx.workspace_name

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)
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"),
stderr = ctx.actions.declare_file(ctx.label.name + ".stderr.log"),
Expand Down Expand Up @@ -331,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 @@ -343,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 Down Expand Up @@ -379,6 +421,7 @@ def _cargo_build_script_impl(ctx):
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 Expand Up @@ -520,7 +563,12 @@ cargo_build_script = rule(
"_experimental_symlink_execroot": attr.label(
default = Label("//cargo/settings:experimental_symlink_execroot"),
),
},
"_runfiles_maker": attr.label(
cfg = "exec",
executable = True,
default = Label("//cargo/private/runfiles_maker"),
),
} | runfiles_enabled_attr(),
fragments = ["cpp"],
toolchains = [
str(Label("//rust:toolchain_type")),
Expand Down
Loading

0 comments on commit d60d7c2

Please sign in to comment.