-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added standalone targets for
rust_toolchain
components
- Loading branch information
1 parent
ed64716
commit 2567774
Showing
6 changed files
with
199 additions
and
37 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,81 @@ | ||
"""A module defining toolchain utilities""" | ||
|
||
def _toolchain_files_impl(ctx): | ||
toolchain = ctx.toolchains[str(Label("//rust:toolchain"))] | ||
|
||
runfiles = None | ||
if ctx.attr.tool == "cargo": | ||
files = depset([toolchain.cargo]) | ||
runfiles = ctx.runfiles( | ||
files = [ | ||
toolchain.cargo, | ||
toolchain.rustc, | ||
], | ||
transitive_files = toolchain.rustc_lib.files, | ||
) | ||
elif ctx.attr.tool == "clippy": | ||
files = depset([toolchain.clippy_driver]) | ||
runfiles = ctx.runfiles( | ||
files = [ | ||
toolchain.clippy_driver, | ||
toolchain.rustc, | ||
], | ||
transitive_files = toolchain.rustc_lib.files, | ||
) | ||
elif ctx.attr.tool == "rustc": | ||
files = depset([toolchain.rustc]) | ||
runfiles = ctx.runfiles( | ||
files = [toolchain.rustc], | ||
transitive_files = toolchain.rustc_lib.files, | ||
) | ||
elif ctx.attr.tool == "rustdoc": | ||
files = depset([toolchain.rust_doc]) | ||
runfiles = ctx.runfiles( | ||
files = [toolchain.rust_doc], | ||
transitive_files = toolchain.rustc_lib.files, | ||
) | ||
elif ctx.attr.tool == "rustfmt": | ||
files = depset([toolchain.rustfmt]) | ||
runfiles = ctx.runfiles( | ||
files = [toolchain.rustfmt], | ||
transitive_files = toolchain.rustc_lib.files, | ||
) | ||
elif ctx.attr.tool == "rustc_lib": | ||
files = toolchain.rustc_lib.files | ||
elif ctx.attr.tool == "rustc_srcs": | ||
files = toolchain.rustc_srcs.files | ||
elif ctx.attr.tool == "rust_lib" or ctx.attr.tool == "rust_stdlib": | ||
files = toolchain.rust_lib.files | ||
else: | ||
fail("Unsupported tool: ", ctx.attr.tool) | ||
|
||
return [DefaultInfo( | ||
files = files, | ||
runfiles = runfiles, | ||
)] | ||
|
||
toolchain_files = rule( | ||
doc = "A rule for fetching files from a rust toolchain.", | ||
implementation = _toolchain_files_impl, | ||
attrs = { | ||
"tool": attr.string( | ||
doc = "The desired tool to get form the current rust_toolchain", | ||
values = [ | ||
"cargo", | ||
"clippy", | ||
"rustc", | ||
"rustdoc", | ||
"rustfmt", | ||
"rustc_lib", | ||
"rustc_srcs", | ||
"rust_lib", | ||
"rust_stdlib", | ||
], | ||
mandatory = True, | ||
), | ||
}, | ||
toolchains = [ | ||
str(Label("//rust:toolchain")), | ||
], | ||
incompatible_use_toolchain_transition = True, | ||
) |
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,43 @@ | ||
load("//rust/private:toolchain_utils.bzl", "toolchain_files") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
toolchain_files( | ||
name = "current_exec_cargo_files", | ||
tool = "cargo", | ||
) | ||
|
||
toolchain_files( | ||
name = "current_exec_clippy_files", | ||
tool = "clippy", | ||
) | ||
|
||
toolchain_files( | ||
name = "current_exec_rustc_files", | ||
tool = "rustc", | ||
) | ||
|
||
toolchain_files( | ||
name = "current_exec_rustdoc_files", | ||
tool = "rustdoc", | ||
) | ||
|
||
toolchain_files( | ||
name = "current_exec_rustfmt_files", | ||
tool = "rustfmt", | ||
) | ||
|
||
toolchain_files( | ||
name = "current_exec_rustc_lib_files", | ||
tool = "rustc_lib", | ||
) | ||
|
||
toolchain_files( | ||
name = "current_exec_rustc_srcs_files", | ||
tool = "rustc_srcs", | ||
) | ||
|
||
toolchain_files( | ||
name = "current_exec_rust_stdlib_files", | ||
tool = "rust_stdlib", | ||
) |
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,43 @@ | ||
# Executable targets will output a pattern similar to the following | ||
# cargo 1.53.0 (4369396ce 2021-04-27) | ||
# Also Note, rustc_srcs is too big for this test | ||
_FILES = { | ||
"cargo": ("--executable", r"^cargo [0-9\.]\+ ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"), | ||
"clippy": ("--executable", r"^clippy [0-9\.]\+ ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"), | ||
"rust_stdlib": ("--files", r"\.rlib"), | ||
"rustc": ("--executable", r"^rustc [0-9\.]\+ ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"), | ||
"rustc_lib": ("--files", r"rustc_driver"), | ||
"rustdoc": ("--executable", r"^rustdoc [0-9\.]\+ ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"), | ||
"rustfmt": ("--executable", r"^rustfmt [0-9\.]\+\-stable ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"), | ||
} | ||
|
||
# Generate a list manifest for all files in the filegroup | ||
[ | ||
genrule( | ||
name = "{}_manifest_genrule".format(files), | ||
srcs = ["//rust/toolchain:current_exec_{}_files".format(files)], | ||
outs = ["{}_manifest".format(files)], | ||
cmd = "for file in $(rootpaths //rust/toolchain:current_exec_{}_files); do echo $$file >> $@; done".format(files), | ||
) | ||
for files in _FILES | ||
if "--files" in _FILES[files] | ||
] | ||
|
||
# Test that all toolchain tools are executable targets | ||
[ | ||
sh_test( | ||
name = tool + "_test", | ||
srcs = ["current_exec_files_test.sh"], | ||
args = [ | ||
"$(rootpath //rust/toolchain:current_exec_{}_files)".format(tool) if "--executable" == arg else "$(rootpath {}_manifest)".format(tool), | ||
arg, | ||
"'{}'".format(pattern), | ||
], | ||
data = [ | ||
"//rust/toolchain:current_exec_{}_files".format(tool), | ||
] + ( | ||
["{}_manifest".format(tool)] if "--files" == arg else [] | ||
), | ||
) | ||
for tool, (arg, pattern) in _FILES.items() | ||
] |
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,28 @@ | ||
#!/bin/bash | ||
|
||
set -xeuo pipefail | ||
|
||
TARGET="$1" | ||
OPTION="$2" | ||
|
||
# To parse this argument on windows it must be wrapped in quotes but | ||
# these quotes should not be passed to grep. Remove them here. | ||
PATTERN="$(echo -n "$3" | sed "s/'//g")" | ||
|
||
if [[ "${OPTION}" == "--executable" ]]; then | ||
# Clippy requires this environment variable is set | ||
export SYSROOT="" | ||
|
||
"${TARGET}" --version | ||
"${TARGET}" --version | grep "${PATTERN}" | ||
exit 0 | ||
fi | ||
|
||
if [[ "${OPTION}" == "--files" ]]; then | ||
cat "${TARGET}" | ||
grep "${PATTERN}" "${TARGET}" | ||
exit 0 | ||
fi | ||
|
||
echo "Unexpected option: ${OPTION}" | ||
exit 1 |
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