Skip to content

Commit

Permalink
Merge branch 'main' into fix-sysroot
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinh Tran committed Oct 27, 2023
2 parents e3d900c + 249b821 commit c5c3904
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 7 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
14 changes: 8 additions & 6 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,7 @@ def rustc_compile_action(
action_outputs.append(dsym_folder)

sysroot_arg = ctx.actions.args()
sysroot_arg.add("--sysroot", toolchain.sysroot)
sysroot_arg.add(toolchain.sysroot_short_path, format = "--sysroot=${{pwd}}/%s")

if ctx.executable._process_wrapper:
# Run as normal
Expand All @@ -1274,7 +1274,7 @@ def rustc_compile_action(
inputs = compile_inputs,
outputs = action_outputs,
env = env,
arguments = [sysroot_arg] + args.all,
arguments = [args.process_wrapper_flags, args.rustc_path, sysroot_arg, args.rustc_flags],
mnemonic = "Rustc",
progress_message = "Compiling Rust {} {}{} ({} files)".format(
crate_info.type,
Expand All @@ -1290,7 +1290,7 @@ def rustc_compile_action(
inputs = compile_inputs,
outputs = [build_metadata],
env = env,
arguments = [sysroot_arg] + args_metadata.all,
arguments = [args_metadata.process_wrapper_flags, args_metadata.rustc_path, sysroot_arg, args_metadata.rustc_flags],
mnemonic = "RustcMetadata",
progress_message = "Compiling Rust metadata {} {}{} ({} files)".format(
crate_info.type,
Expand All @@ -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 = [sysroot_arg, 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
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[@]}"
2 changes: 1 addition & 1 deletion version.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""The version of rules_rust."""

VERSION = "0.29.1"
VERSION = "0.30.0"

0 comments on commit c5c3904

Please sign in to comment.