Skip to content

Commit 8cca226

Browse files
authored
Added standalone targets for rust_toolchain components. (#792)
* Added standalone targets for `rust_toolchain` components * Address PR feedback
1 parent 404b84b commit 8cca226

File tree

6 files changed

+199
-37
lines changed

6 files changed

+199
-37
lines changed

crate_universe/private/bootstrap/BUILD.bazel

+2-24
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,7 @@
1-
alias(
2-
name = "cargo",
3-
actual = select({
4-
"@rules_rust//rust/platform:aarch64-apple-darwin": "@rust_darwin_aarch64//:cargo",
5-
"@rules_rust//rust/platform:aarch64-unknown-linux-gnu": "@rust_linux_aarch64//:cargo",
6-
"@rules_rust//rust/platform:x86_64-apple-darwin": "@rust_darwin_x86_64//:cargo",
7-
"@rules_rust//rust/platform:x86_64-pc-windows-msvc": "@rust_windows_x86_64//:cargo",
8-
"@rules_rust//rust/platform:x86_64-unknown-linux-gnu": "@rust_linux_x86_64//:cargo",
9-
}),
10-
)
11-
12-
alias(
13-
name = "rustc",
14-
actual = select({
15-
"@rules_rust//rust/platform:aarch64-apple-darwin": "@rust_darwin_aarch64//:rustc",
16-
"@rules_rust//rust/platform:aarch64-unknown-linux-gnu": "@rust_linux_aarch64//:rustc",
17-
"@rules_rust//rust/platform:x86_64-apple-darwin": "@rust_darwin_x86_64//:rustc",
18-
"@rules_rust//rust/platform:x86_64-pc-windows-msvc": "@rust_windows_x86_64//:rustc",
19-
"@rules_rust//rust/platform:x86_64-unknown-linux-gnu": "@rust_linux_x86_64//:rustc",
20-
}),
21-
)
22-
231
_COMMON_ENV = {
24-
"CARGO": "$${PWD}/$(execpath :cargo)",
2+
"CARGO": "$${PWD}/$(execpath //rust/toolchain:current_exec_cargo_files)",
253
"DETECT_CHANGES": "true",
26-
"RUSTC": "$${PWD}/$(execpath :rustc)",
4+
"RUSTC": "$${PWD}/$(execpath //rust/toolchain:current_exec_rustc_files)",
275
}
286

297
_ENV = select({

rust/private/toolchain_utils.bzl

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""A module defining toolchain utilities"""
2+
3+
def _toolchain_files_impl(ctx):
4+
toolchain = ctx.toolchains[str(Label("//rust:toolchain"))]
5+
6+
runfiles = None
7+
if ctx.attr.tool == "cargo":
8+
files = depset([toolchain.cargo])
9+
runfiles = ctx.runfiles(
10+
files = [
11+
toolchain.cargo,
12+
toolchain.rustc,
13+
],
14+
transitive_files = toolchain.rustc_lib.files,
15+
)
16+
elif ctx.attr.tool == "clippy":
17+
files = depset([toolchain.clippy_driver])
18+
runfiles = ctx.runfiles(
19+
files = [
20+
toolchain.clippy_driver,
21+
toolchain.rustc,
22+
],
23+
transitive_files = toolchain.rustc_lib.files,
24+
)
25+
elif ctx.attr.tool == "rustc":
26+
files = depset([toolchain.rustc])
27+
runfiles = ctx.runfiles(
28+
files = [toolchain.rustc],
29+
transitive_files = toolchain.rustc_lib.files,
30+
)
31+
elif ctx.attr.tool == "rustdoc":
32+
files = depset([toolchain.rust_doc])
33+
runfiles = ctx.runfiles(
34+
files = [toolchain.rust_doc],
35+
transitive_files = toolchain.rustc_lib.files,
36+
)
37+
elif ctx.attr.tool == "rustfmt":
38+
files = depset([toolchain.rustfmt])
39+
runfiles = ctx.runfiles(
40+
files = [toolchain.rustfmt],
41+
transitive_files = toolchain.rustc_lib.files,
42+
)
43+
elif ctx.attr.tool == "rustc_lib":
44+
files = toolchain.rustc_lib.files
45+
elif ctx.attr.tool == "rustc_srcs":
46+
files = toolchain.rustc_srcs.files
47+
elif ctx.attr.tool == "rust_lib" or ctx.attr.tool == "rust_stdlib":
48+
files = toolchain.rust_lib.files
49+
else:
50+
fail("Unsupported tool: ", ctx.attr.tool)
51+
52+
return [DefaultInfo(
53+
files = files,
54+
runfiles = runfiles,
55+
)]
56+
57+
toolchain_files = rule(
58+
doc = "A rule for fetching files from a rust toolchain.",
59+
implementation = _toolchain_files_impl,
60+
attrs = {
61+
"tool": attr.string(
62+
doc = "The desired tool to get form the current rust_toolchain",
63+
values = [
64+
"cargo",
65+
"clippy",
66+
"rustc",
67+
"rustdoc",
68+
"rustfmt",
69+
"rustc_lib",
70+
"rustc_srcs",
71+
"rust_lib",
72+
"rust_stdlib",
73+
],
74+
mandatory = True,
75+
),
76+
},
77+
toolchains = [
78+
str(Label("//rust:toolchain")),
79+
],
80+
incompatible_use_toolchain_transition = True,
81+
)

rust/toolchain/BUILD.bazel

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
load("//rust/private:toolchain_utils.bzl", "toolchain_files")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
toolchain_files(
6+
name = "current_exec_cargo_files",
7+
tool = "cargo",
8+
)
9+
10+
toolchain_files(
11+
name = "current_exec_clippy_files",
12+
tool = "clippy",
13+
)
14+
15+
toolchain_files(
16+
name = "current_exec_rustc_files",
17+
tool = "rustc",
18+
)
19+
20+
toolchain_files(
21+
name = "current_exec_rustdoc_files",
22+
tool = "rustdoc",
23+
)
24+
25+
toolchain_files(
26+
name = "current_exec_rustfmt_files",
27+
tool = "rustfmt",
28+
)
29+
30+
toolchain_files(
31+
name = "current_exec_rustc_lib_files",
32+
tool = "rustc_lib",
33+
)
34+
35+
toolchain_files(
36+
name = "current_exec_rustc_srcs_files",
37+
tool = "rustc_srcs",
38+
)
39+
40+
toolchain_files(
41+
name = "current_exec_rust_stdlib_files",
42+
tool = "rust_stdlib",
43+
)
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Executable targets will output a pattern similar to the following
2+
# cargo 1.53.0 (4369396ce 2021-04-27)
3+
# Also Note, rustc_srcs is too big for this test
4+
_FILES = {
5+
"cargo": ("--executable", r"^cargo [0-9\.]\+ ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
6+
"clippy": ("--executable", r"^clippy [0-9\.]\+ ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
7+
"rust_stdlib": ("--files", r"\.rlib"),
8+
"rustc": ("--executable", r"^rustc [0-9\.]\+ ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
9+
"rustc_lib": ("--files", r"rustc_driver"),
10+
"rustdoc": ("--executable", r"^rustdoc [0-9\.]\+ ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
11+
"rustfmt": ("--executable", r"^rustfmt [0-9\.]\+\-stable ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
12+
}
13+
14+
# Generate a list manifest for all files in the filegroup
15+
[
16+
genrule(
17+
name = "{}_manifest_genrule".format(files),
18+
srcs = ["//rust/toolchain:current_exec_{}_files".format(files)],
19+
outs = ["{}_manifest".format(files)],
20+
cmd = "for file in $(rootpaths //rust/toolchain:current_exec_{}_files); do echo $$file >> $@; done".format(files),
21+
)
22+
for files in _FILES
23+
if "--files" in _FILES[files]
24+
]
25+
26+
# Test that all toolchain tools are executable targets
27+
[
28+
sh_test(
29+
name = tool + "_test",
30+
srcs = ["current_exec_files_test.sh"],
31+
args = [
32+
"$(rootpath //rust/toolchain:current_exec_{}_files)".format(tool) if "--executable" == arg else "$(rootpath {}_manifest)".format(tool),
33+
arg,
34+
"'{}'".format(pattern),
35+
],
36+
data = [
37+
"//rust/toolchain:current_exec_{}_files".format(tool),
38+
] + (
39+
["{}_manifest".format(tool)] if "--files" == arg else []
40+
),
41+
)
42+
for tool, (arg, pattern) in _FILES.items()
43+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
TARGET="$1"
6+
OPTION="$2"
7+
8+
# To parse this argument on windows it must be wrapped in quotes but
9+
# these quotes should not be passed to grep. Remove them here.
10+
PATTERN="$(echo -n "$3" | sed "s/'//g")"
11+
12+
if [[ "${OPTION}" == "--executable" ]]; then
13+
# Clippy requires this environment variable is set
14+
export SYSROOT=""
15+
16+
"${TARGET}" --version
17+
"${TARGET}" --version | grep "${PATTERN}"
18+
exit 0
19+
fi
20+
21+
if [[ "${OPTION}" == "--files" ]]; then
22+
cat "${TARGET}"
23+
grep "${PATTERN}" "${TARGET}"
24+
exit 0
25+
fi
26+
27+
echo "Unexpected option: ${OPTION}"
28+
exit 1

tools/rustfmt/BUILD.bazel

+2-13
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,19 @@ package(default_visibility = ["//visibility:public"])
44

55
exports_files(["rustfmt.toml"])
66

7-
alias(
8-
name = "rustfmt_bin",
9-
actual = select({
10-
"@rules_rust//rust/platform:aarch64-apple-darwin": "@rust_darwin_aarch64//:rustfmt_bin",
11-
"@rules_rust//rust/platform:aarch64-unknown-linux-gnu": "@rust_linux_aarch64//:rustfmt_bin",
12-
"@rules_rust//rust/platform:x86_64-apple-darwin": "@rust_darwin_x86_64//:rustfmt_bin",
13-
"@rules_rust//rust/platform:x86_64-pc-windows-msvc": "@rust_windows_x86_64//:rustfmt_bin",
14-
"@rules_rust//rust/platform:x86_64-unknown-linux-gnu": "@rust_linux_x86_64//:rustfmt_bin",
15-
}),
16-
)
17-
187
rust_library(
198
name = "rustfmt_lib",
209
srcs = glob(
2110
["srcs/**/*.rs"],
2211
exclude = ["srcs/**/*main.rs"],
2312
),
2413
data = [
25-
":rustfmt_bin",
2614
"//:rustfmt.toml",
15+
"//rust/toolchain:current_exec_rustfmt_files",
2716
],
2817
edition = "2018",
2918
rustc_env = {
30-
"RUSTFMT": "$(rootpath :rustfmt_bin)",
19+
"RUSTFMT": "$(rootpath //rust/toolchain:current_exec_rustfmt_files)",
3120
"RUSTFMT_CONFIG": "$(rootpath //:rustfmt.toml)",
3221
},
3322
)

0 commit comments

Comments
 (0)