-
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.
Added tests for pipelining with custom rules
- Loading branch information
Showing
5 changed files
with
191 additions
and
21 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,3 @@ | ||
pub fn to_wrap() { | ||
eprintln!("something"); | ||
} |
5 changes: 5 additions & 0 deletions
5
test/unit/pipelined_compilation/custom_rule_test/uses_wrapper.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,5 @@ | ||
use wrapper::wrap; | ||
|
||
pub fn calls_wrap() { | ||
wrap(); | ||
} |
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,106 @@ | ||
"""A custom rule that wraps a crate called to_wrap.""" | ||
|
||
# buildifier: disable=bzl-visibility | ||
load("//rust/private:common.bzl", "rust_common") | ||
|
||
# buildifier: disable=bzl-visibility | ||
load("//rust/private:providers.bzl", "BuildInfo", "CrateInfo", "DepInfo", "DepVariantInfo") | ||
|
||
# buildifier: disable=bzl-visibility | ||
load("//rust/private:rustc.bzl", "rustc_compile_action") | ||
|
||
def _wrap_impl(ctx): | ||
rs_file = ctx.actions.declare_file(ctx.label.name + "_wrapped.rs") | ||
crate_name = ctx.attr.crate_name if ctx.attr.crate_name else ctx.label.name | ||
ctx.actions.run_shell( | ||
outputs = [rs_file], | ||
command = """cat <<EOF > {} | ||
// crate_name: {} | ||
use to_wrap::to_wrap; | ||
pub fn wrap() {{ | ||
to_wrap(); | ||
}} | ||
EOF | ||
""".format(rs_file.path, crate_name), | ||
mnemonic = "WriteWrapperRsFile", | ||
) | ||
|
||
toolchain = ctx.toolchains[Label("//rust:toolchain")] | ||
|
||
# Determine unique hash for this rlib | ||
output_hash = repr(hash(rs_file.path)) | ||
crate_type = "rlib" | ||
|
||
rust_lib_name = "{prefix}{name}-{lib_hash}{extension}".format( | ||
prefix = "lib", | ||
name = crate_name, | ||
lib_hash = output_hash, | ||
extension = ".rlib", | ||
) | ||
rust_metadata_name = "{prefix}{name}-{lib_hash}{extension}".format( | ||
prefix = "lib", | ||
name = crate_name, | ||
lib_hash = output_hash, | ||
extension = ".rmeta", | ||
) | ||
|
||
tgt = ctx.attr.target | ||
deps = [DepVariantInfo( | ||
crate_info = tgt[CrateInfo] if CrateInfo in tgt else None, | ||
dep_info = tgt[DepInfo] if DepInfo in tgt else None, | ||
build_info = tgt[BuildInfo] if BuildInfo in tgt else None, | ||
cc_info = tgt[CcInfo] if CcInfo in tgt else None, | ||
)] | ||
|
||
rust_lib = ctx.actions.declare_file(rust_lib_name) | ||
rust_metadata = None | ||
if ctx.attr.generate_metadata: | ||
rust_metadata = ctx.actions.declare_file(rust_metadata_name) | ||
return rustc_compile_action( | ||
ctx = ctx, | ||
attr = ctx.attr, | ||
toolchain = toolchain, | ||
crate_info = rust_common.create_crate_info( | ||
name = crate_name, | ||
type = crate_type, | ||
root = rs_file, | ||
srcs = depset([rs_file]), | ||
deps = depset(deps), | ||
proc_macro_deps = depset([]), | ||
aliases = {}, | ||
output = rust_lib, | ||
metadata = rust_metadata, | ||
owner = ctx.label, | ||
edition = "2018", | ||
compile_data = depset([]), | ||
rustc_env = {}, | ||
is_test = False, | ||
), | ||
output_hash = output_hash, | ||
force_all_deps_direct = True, | ||
) | ||
|
||
wrap = rule( | ||
implementation = _wrap_impl, | ||
attrs = { | ||
"target": attr.label(), | ||
"crate_name": attr.string(), | ||
"generate_metadata": attr.bool(default = False), | ||
"_cc_toolchain": attr.label( | ||
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"), | ||
), | ||
"_error_format": attr.label( | ||
default = Label("//:error_format"), | ||
), | ||
"_process_wrapper": attr.label( | ||
default = Label("//util/process_wrapper"), | ||
executable = True, | ||
allow_single_file = True, | ||
cfg = "exec", | ||
), | ||
}, | ||
toolchains = ["@rules_rust//rust:toolchain", "@bazel_tools//tools/cpp:toolchain_type"], | ||
incompatible_use_toolchain_transition = True, | ||
fragments = ["cpp"], | ||
) |