-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add flag to refer to a
sh_toolchain
for process wrapper bootstrap s…
…hebangs (#2694) This introduces the flag `--@rules_rust//rust/settings:experimental_use_sh_toolchain_for_bootstrap_process_wrapper` which can be used to embed the shell path from `@bazel_tools//tools/sh:toolchain_type` in the rustc bootstrap process wrapper.
- Loading branch information
1 parent
9c67294
commit d83c865
Showing
9 changed files
with
202 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
load("//rust:defs.bzl", "rust_test") | ||
load(":process_wrapper_bootstrap_test.bzl", "process_wrapper_bootstrap_test_suite") | ||
|
||
rust_test( | ||
name = "bootstrap_process_wrapper_test", | ||
srcs = ["bootstrap_process_wrapper_test.rs"], | ||
data = ["//util/process_wrapper/private:process_wrapper.sh"], | ||
edition = "2021", | ||
deps = ["//tools/runfiles"], | ||
) | ||
|
||
process_wrapper_bootstrap_test_suite(name = "process_wrapper_bootstrap_test_suite") |
23 changes: 23 additions & 0 deletions
23
test/process_wrapper_bootstrap/bootstrap_process_wrapper_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//! Tests for the boostrap process wrapper | ||
use std::fs::read_to_string; | ||
|
||
use runfiles::Runfiles; | ||
|
||
/// Test that the shell process wrapper starts with the expected shebang to | ||
/// avoid breaking the contract with the `bootstrap_process_wrapper` rule. | ||
#[test] | ||
fn test_shebang() { | ||
let rfiles = Runfiles::create().unwrap(); | ||
|
||
let script = runfiles::rlocation!( | ||
rfiles, | ||
"rules_rust/util/process_wrapper/private/process_wrapper.sh" | ||
); | ||
|
||
let content = read_to_string(script).unwrap(); | ||
assert!( | ||
content.starts_with("#!/usr/bin/env bash"), | ||
"The shell script does not start with the expected shebang." | ||
) | ||
} |
78 changes: 78 additions & 0 deletions
78
test/process_wrapper_bootstrap/process_wrapper_bootstrap_test.bzl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
"""Starlark unit tests for the bootstrap process wrapper""" | ||
|
||
load("@bazel_skylib//lib:unittest.bzl", "analysistest") | ||
load("//test/unit:common.bzl", "assert_action_mnemonic") | ||
|
||
def _enable_sh_toolchain_test_impl(ctx): | ||
env = analysistest.begin(ctx) | ||
target = analysistest.target_under_test(env) | ||
|
||
if ctx.attr.expected_ext == ".bat": | ||
assert_action_mnemonic(env, target.actions[0], "ExecutableSymlink") | ||
else: | ||
assert_action_mnemonic(env, target.actions[0], "TemplateExpand") | ||
|
||
return analysistest.end(env) | ||
|
||
_enable_sh_toolchain_test = analysistest.make( | ||
_enable_sh_toolchain_test_impl, | ||
config_settings = { | ||
str(Label("//rust/settings:experimental_use_sh_toolchain_for_bootstrap_process_wrapper")): True, | ||
}, | ||
attrs = { | ||
"expected_ext": attr.string( | ||
doc = "The expected extension for the bootstrap script.", | ||
mandatory = True, | ||
values = [ | ||
".bat", | ||
".sh", | ||
], | ||
), | ||
}, | ||
) | ||
|
||
def _disable_sh_toolchain_test_impl(ctx): | ||
env = analysistest.begin(ctx) | ||
target = analysistest.target_under_test(env) | ||
|
||
assert_action_mnemonic(env, target.actions[0], "ExecutableSymlink") | ||
|
||
return analysistest.end(env) | ||
|
||
_disable_sh_toolchain_test = analysistest.make( | ||
_disable_sh_toolchain_test_impl, | ||
config_settings = { | ||
str(Label("//rust/settings:experimental_use_sh_toolchain_for_bootstrap_process_wrapper")): False, | ||
}, | ||
) | ||
|
||
def process_wrapper_bootstrap_test_suite(name, **kwargs): | ||
"""Entry-point macro called from the BUILD file. | ||
Args: | ||
name (str): Name of the macro. | ||
**kwargs (dict): Additional keyword arguments. | ||
""" | ||
|
||
_enable_sh_toolchain_test( | ||
name = "enable_sh_toolchain_test", | ||
target_under_test = Label("//util/process_wrapper:bootstrap_process_wrapper"), | ||
expected_ext = select({ | ||
"@platforms//os:windows": ".bat", | ||
"//conditions:default": ".sh", | ||
}), | ||
) | ||
|
||
_disable_sh_toolchain_test( | ||
name = "disable_sh_toolchain_test", | ||
target_under_test = Label("//util/process_wrapper:bootstrap_process_wrapper"), | ||
) | ||
|
||
native.test_suite( | ||
name = name, | ||
tests = [ | ||
":disable_sh_toolchain_test", | ||
":enable_sh_toolchain_test", | ||
], | ||
**kwargs | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
exports_files([ | ||
"process_wrapper.sh", | ||
"process_wrapper.bat", | ||
]) |
73 changes: 73 additions & 0 deletions
73
util/process_wrapper/private/bootstrap_process_wrapper.bzl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"""Bootstrap rustc process wrapper""" | ||
|
||
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") | ||
|
||
def _bootstrap_process_wrapper_impl_unix(ctx): | ||
output = ctx.actions.declare_file("{}.sh".format(ctx.label.name)) | ||
|
||
setting = ctx.attr._use_sh_toolchain_for_bootstrap_process_wrapper[BuildSettingInfo].value | ||
sh_toolchain = ctx.toolchains["@bazel_tools//tools/sh:toolchain_type"] | ||
if setting and sh_toolchain: | ||
shebang = "#!{}".format(sh_toolchain.path) | ||
ctx.actions.expand_template( | ||
output = output, | ||
template = ctx.file._bash, | ||
substitutions = { | ||
# Replace the shebang with one constructed from the configured | ||
# shell toolchain. | ||
"#!/usr/bin/env bash": shebang, | ||
}, | ||
) | ||
else: | ||
ctx.actions.symlink( | ||
output = output, | ||
target_file = ctx.file._bash, | ||
is_executable = True, | ||
) | ||
|
||
return [DefaultInfo( | ||
files = depset([output]), | ||
executable = output, | ||
)] | ||
|
||
def _bootstrap_process_wrapper_impl_windows(ctx): | ||
output = ctx.actions.declare_file("{}.bat".format(ctx.label.name)) | ||
ctx.actions.symlink( | ||
output = output, | ||
target_file = ctx.file._batch, | ||
is_executable = True, | ||
) | ||
|
||
return [DefaultInfo( | ||
files = depset([output]), | ||
executable = output, | ||
)] | ||
|
||
def _bootstrap_process_wrapper_impl(ctx): | ||
if ctx.attr.is_windows: | ||
return _bootstrap_process_wrapper_impl_windows(ctx) | ||
return _bootstrap_process_wrapper_impl_unix(ctx) | ||
|
||
bootstrap_process_wrapper = rule( | ||
doc = "A rule which produces a bootstrapping script for the rustc process wrapper.", | ||
implementation = _bootstrap_process_wrapper_impl, | ||
attrs = { | ||
"is_windows": attr.bool( | ||
doc = "Indicate whether or not the target platform is windows.", | ||
mandatory = True, | ||
), | ||
"_bash": attr.label( | ||
allow_single_file = True, | ||
default = Label("//util/process_wrapper/private:process_wrapper.sh"), | ||
), | ||
"_batch": attr.label( | ||
allow_single_file = True, | ||
default = Label("//util/process_wrapper/private:process_wrapper.bat"), | ||
), | ||
"_use_sh_toolchain_for_bootstrap_process_wrapper": attr.label( | ||
default = Label("//rust/settings:experimental_use_sh_toolchain_for_bootstrap_process_wrapper"), | ||
), | ||
}, | ||
toolchains = [config_common.toolchain_type("@bazel_tools//tools/sh:toolchain_type", mandatory = False)], | ||
executable = True, | ||
) |
File renamed without changes.
File renamed without changes.