Skip to content

Fix issues causing the Rustc process wrapper to be built non-determ… #2216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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__"],
)
31 changes: 31 additions & 0 deletions util/process_wrapper/process_wrapper.bat
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unclear if this actually results in deterministic outputs. I do not have a windows machine to verify.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@ECHO OFF
SETLOCAL enabledelayedexpansion

SET command=%*

:: Resolve the `${pwd}` placeholders
SET command=!command:${pwd}=%CD%!

:: Strip out the leading `--` argument.
SET command=!command:~3!

:: Find the rustc.exe argument and sanitize it's path
for %%A in (%*) do (
SET arg=%%~A
if "!arg:~-9!"=="rustc.exe" (
SET sanitized=!arg:/=\!

SET command=!sanitized! !command:%%~A=!
goto :break
)
)

:break

%command%

:: Capture the exit code of rustc.exe
SET exit_code=!errorlevel!

:: Exit with the same exit code
EXIT /b %exit_code%
18 changes: 18 additions & 0 deletions util/process_wrapper/process_wrapper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/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

exec "${args[@]}"