Skip to content

Commit

Permalink
Fix issues causing the Rustc process wrapper to be built non-determ…
Browse files Browse the repository at this point in the history
…inistically
  • Loading branch information
UebelAndre committed Oct 26, 2023
1 parent 1557205 commit 95cc7f3
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 3 deletions.
7 changes: 7 additions & 0 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,13 @@ def _common_attrs_for_binary_without_process_wrapper(attrs):
cfg = "exec",
)

new_attr["_bootstrap_process_wrapper"] = attr.label(
default = Label("//util/process_wrapper:bootstrap_process_wrapper"),
executable = True,
allow_single_file = True,
cfg = "exec",
)

# fix stamp = 0
new_attr["stamp"] = attr.int(
doc = dedent("""\
Expand Down
8 changes: 5 additions & 3 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1300,16 +1300,16 @@ def rustc_compile_action(
),
toolchain = "@rules_rust//rust:toolchain_type",
)
else:
elif hasattr(ctx.executable, "_bootstrap_process_wrapper"):
# Run without process_wrapper
if build_env_files or build_flags_files or stamp or build_metadata:
fail("build_env_files, build_flags_files, stamp, build_metadata are not supported when building without process_wrapper")
ctx.actions.run(
executable = toolchain.rustc,
executable = ctx.executable._bootstrap_process_wrapper,
inputs = compile_inputs,
outputs = action_outputs,
env = env,
arguments = [args.rustc_flags],
arguments = [args.rustc_path, args.rustc_flags],
mnemonic = "Rustc",
progress_message = "Compiling Rust (without process_wrapper) {} {}{} ({} files)".format(
crate_info.type,
Expand All @@ -1319,6 +1319,8 @@ def rustc_compile_action(
),
toolchain = "@rules_rust//rust:toolchain_type",
)
else:
fail("No process wrapper was defined for {}".format(ctx.label))

if experimental_use_cc_common_link:
# Wrap the main `.o` file into a compilation output suitable for
Expand Down
46 changes: 46 additions & 0 deletions util/process_wrapper/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,12 +1,45 @@
load("@bazel_skylib//lib:selects.bzl", "selects")
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
load("//rust:defs.bzl", "rust_test")

# buildifier: disable=bzl-visibility
load("//rust/private:rust.bzl", "rust_binary_without_process_wrapper")

config_setting(
name = "compilation_mode_opt",
values = {"compilation_mode": "opt"},
)

selects.config_setting_group(
name = "opt_linux",
match_all = [
":compilation_mode_opt",
"@platforms//os:linux",
],
visibility = ["@rules_rust_tinyjson//:__pkg__"],
)

selects.config_setting_group(
name = "opt_macos",
match_all = [
":compilation_mode_opt",
"@platforms//os:macos",
],
visibility = ["@rules_rust_tinyjson//:__pkg__"],
)

rust_binary_without_process_wrapper(
name = "process_wrapper",
srcs = glob(["*.rs"]),
edition = "2018",
# To ensure the process wrapper is produced deterministically
# debug info, which is known to sometimes have host specific
# paths embedded in this section, is stripped out.
rustc_flags = select({
":opt_linux": ["-Cstrip=debuginfo"],
":opt_macos": ["-Cstrip=debuginfo"],
"//conditions:default": [],
}),
visibility = ["//visibility:public"],
deps = [
"@rules_rust_tinyjson//:tinyjson",
Expand All @@ -18,3 +51,16 @@ rust_test(
crate = ":process_wrapper",
edition = "2018",
)

native_binary(
name = "bootstrap_process_wrapper",
src = select({
"@platforms//os:windows": "process_wrapper.bat",
"//conditions:default": "process_wrapper.sh",
}),
out = select({
"@platforms//os:windows": "process_wrapper.bat",
"//conditions:default": "process_wrapper.sh",
}),
visibility = ["//visibility:public"],
)
8 changes: 8 additions & 0 deletions util/process_wrapper/BUILD.tinyjson.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@ rust_library_without_process_wrapper(
name = "tinyjson",
srcs = glob(["src/*.rs"]),
edition = "2018",
# To ensure the process wrapper is produced deterministically
# debug info, which is known to sometimes have host specific
# paths embedded in this section, is stripped out.
rustc_flags = select({
"@rules_rust//util/process_wrapper:opt_linux": ["-Cstrip=debuginfo"],
"@rules_rust//util/process_wrapper:opt_macos": ["-Cstrip=debuginfo"],
"//conditions:default": [],
}),
visibility = ["@rules_rust//util/process_wrapper:__pkg__"],
)
32 changes: 32 additions & 0 deletions util/process_wrapper/process_wrapper.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@ECHO OFF
SETLOCAL enabledelayedexpansion

:: The first argument is expected to be `--`, this must be skipped
SET "skip_first_arg=1"

:: Initialize a counter to track arguments
SET "arg_count=1"

:: Pass the arguments to the provided command, modifying the second argument
for %%A in (%*) do (
if !skip_first_arg! equ 1 (
SET "skip_first_arg=0"
) else if !arg_count! equ 2 (
:: Ensure the rustc.exe path is executable by sanitizing any `/` to `\`
SET "arg=%%~A"
SET "arg=!arg:/=\!"
SET "command_args=!command_args! !arg!"
) else (
:: Account for any arguments which contain `${pwd}` (a place holder for the current working directiron)
set "arg=%%~A"
set "arg=!arg:${pwd}=%CD%!"
set "command_args=!command_args! !arg!"
)
SET /a "arg_count+=1"
)

:: append "--remap-path-prefix" with the current working directory
SET "command_args=!command_args! "--remap-path-prefix=%CD%="

:: Execute the provided command with arguments
%command_args%
20 changes: 20 additions & 0 deletions util/process_wrapper/process_wrapper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

set -euo pipefail

# Skip the first argument which is expected to be `--`
shift

args=()

for arg in "$@"; do
# Check if the argument contains "${PWD}" and replace it with the actual value of PWD
if [[ "${arg}" == *'${pwd}'* ]]; then
arg="${arg//\$\{pwd\}/$PWD}"
fi
args+=("${arg}")
done

set -x

exec "${args[@]}"

0 comments on commit 95cc7f3

Please sign in to comment.