From c42f81d37f51fde51830f1cb12edc1e5f1b4417f Mon Sep 17 00:00:00 2001 From: David Marcin Date: Tue, 10 Aug 2021 16:37:23 -0700 Subject: [PATCH 1/6] Add exec transition for cargo_build_script data --- cargo/cargo_build_script.bzl | 1 + 1 file changed, 1 insertion(+) diff --git a/cargo/cargo_build_script.bzl b/cargo/cargo_build_script.bzl index b4ca4087c1..4059c0e8a0 100644 --- a/cargo/cargo_build_script.bzl +++ b/cargo/cargo_build_script.bzl @@ -197,6 +197,7 @@ _build_script_run = rule( "data": attr.label_list( doc = "Data or tools required by the build script.", allow_files = True, + cfg = "exec", ), "deps": attr.label_list( doc = "The Rust dependencies of the crate", From 9bcaf742004eb2ac241c07b0c41918a09e2315c4 Mon Sep 17 00:00:00 2001 From: David Marcin Date: Thu, 12 Aug 2021 01:50:27 -0700 Subject: [PATCH 2/6] Add tools attr --- cargo/cargo_build_script.bzl | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/cargo/cargo_build_script.bzl b/cargo/cargo_build_script.bzl index 4059c0e8a0..9268eddec9 100644 --- a/cargo/cargo_build_script.bzl +++ b/cargo/cargo_build_script.bzl @@ -120,7 +120,8 @@ def _build_script_impl(ctx): ctx, ctx.attr.build_script_env, getattr(ctx.attr, "data", []) + - getattr(ctx.attr, "compile_data", []), + getattr(ctx.attr, "compile_data", []) + + getattr(ctx.attr, "tools", []), )) tools = depset( @@ -128,7 +129,7 @@ def _build_script_impl(ctx): script, ctx.executable._cargo_build_script_runner, toolchain.rustc, - ] + ctx.files.data + ([toolchain.target_json] if toolchain.target_json else []), + ] + ctx.files.data + ctx.files.tools + ([toolchain.target_json] if toolchain.target_json else []), transitive = toolchain_tools, ) @@ -195,9 +196,8 @@ _build_script_run = rule( doc = "The list of rust features that the build script should consider activated.", ), "data": attr.label_list( - doc = "Data or tools required by the build script.", + doc = "Data required by the build script.", allow_files = True, - cfg = "exec", ), "deps": attr.label_list( doc = "The Rust dependencies of the crate", @@ -215,6 +215,11 @@ _build_script_run = rule( mandatory = True, cfg = "exec", ), + "tools": attr.label_list( + doc = "Tools required by the build script.", + allow_files = True, + cfg = "exec", + ), "version": attr.string( doc = "The semantic version (semver) of the crate", ), @@ -243,6 +248,7 @@ def cargo_build_script( deps = [], build_script_env = {}, data = [], + tools = [], links = None, rustc_env = {}, **kwargs): @@ -310,7 +316,8 @@ def cargo_build_script( version (str, optional): The semantic version (semver) of the crate. deps (list, optional): The dependencies of the crate. build_script_env (dict, optional): Environment variables for build scripts. - data (list, optional): Files or tools needed by the build script. + data (list, optional): Files needed by the build script. + tools (list, optional): Tools (executables) needed by the build script. links (str, optional): Name of the native library this crate links against. rustc_env (dict, optional): Environment variables to set in rustc when compiling the build script. **kwargs: Forwards to the underlying `rust_binary` rule. @@ -342,6 +349,7 @@ def cargo_build_script( links = links, deps = deps, data = data, + tools = tools, ) def _name_to_pkg_name(name): From 918d91aae5b59d35dd9704276ab88b01e290a65e Mon Sep 17 00:00:00 2001 From: David Marcin Date: Thu, 12 Aug 2021 01:51:50 -0700 Subject: [PATCH 3/6] Regenerate documentation --- docs/cargo_build_script.md | 7 ++++--- docs/flatten.md | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/cargo_build_script.md b/docs/cargo_build_script.md index 264cccadfa..959e226d1c 100644 --- a/docs/cargo_build_script.md +++ b/docs/cargo_build_script.md @@ -8,8 +8,8 @@ ## cargo_build_script
-cargo_build_script(name, crate_features, version, deps, build_script_env, data, links, rustc_env,
-                   kwargs)
+cargo_build_script(name, crate_features, version, deps, build_script_env, data, tools, links,
+                   rustc_env, kwargs)
 
Compile and execute a rust build script to generate build attributes @@ -80,7 +80,8 @@ The `hello_lib` target will be build with the flags and the environment variable | version | The semantic version (semver) of the crate. | None | | deps | The dependencies of the crate. | [] | | build_script_env | Environment variables for build scripts. | {} | -| data | Files or tools needed by the build script. | [] | +| data | Files needed by the build script. | [] | +| tools | Tools (executables) needed by the build script. | [] | | links | Name of the native library this crate links against. | None | | rustc_env | Environment variables to set in rustc when compiling the build script. | {} | | kwargs | Forwards to the underlying rust_binary rule. | none | diff --git a/docs/flatten.md b/docs/flatten.md index 0eb53ef21d..95d97eea8e 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -1366,8 +1366,8 @@ A test rule for performing `rustfmt --check` on a set of targets ## cargo_build_script
-cargo_build_script(name, crate_features, version, deps, build_script_env, data, links, rustc_env,
-                   kwargs)
+cargo_build_script(name, crate_features, version, deps, build_script_env, data, tools, links,
+                   rustc_env, kwargs)
 
Compile and execute a rust build script to generate build attributes @@ -1438,7 +1438,8 @@ The `hello_lib` target will be build with the flags and the environment variable | version | The semantic version (semver) of the crate. | None | | deps | The dependencies of the crate. | [] | | build_script_env | Environment variables for build scripts. | {} | -| data | Files or tools needed by the build script. | [] | +| data | Files needed by the build script. | [] | +| tools | Tools (executables) needed by the build script. | [] | | links | Name of the native library this crate links against. | None | | rustc_env | Environment variables to set in rustc when compiling the build script. | {} | | kwargs | Forwards to the underlying rust_binary rule. | none | From 45b3072b0ae9afbce86fbffd45ec2261246bbf50 Mon Sep 17 00:00:00 2001 From: David Marcin Date: Fri, 20 Aug 2021 15:38:29 -0700 Subject: [PATCH 4/6] Add test --- test/cargo_build_script/BUILD.bazel | 25 +++++++++++++++++++++++++ test/cargo_build_script/build.rs | 4 ++++ test/cargo_build_script/tools_exec.rs | 5 +++++ 3 files changed, 34 insertions(+) create mode 100644 test/cargo_build_script/BUILD.bazel create mode 100644 test/cargo_build_script/build.rs create mode 100644 test/cargo_build_script/tools_exec.rs diff --git a/test/cargo_build_script/BUILD.bazel b/test/cargo_build_script/BUILD.bazel new file mode 100644 index 0000000000..27945372b9 --- /dev/null +++ b/test/cargo_build_script/BUILD.bazel @@ -0,0 +1,25 @@ +"""Tests for the cargo_build_script rule""" + +load("//cargo:cargo_build_script.bzl", "cargo_build_script") +load("//rust:rust.bzl", "rust_test") + +# Test that tools are built in the exec configuration. +cargo_build_script( + name = "tools_exec_build_rs", + srcs = ["build.rs"], + build_script_env = {"TOOL": "$(execpath :tool)"}, + tools = [":tool"], +) + +genrule( + name = "tool", + srcs = [], + outs = ["tool-file"], + cmd = "touch $@", +) + +rust_test( + name = "tools_exec", + srcs = ["tools_exec.rs"], + deps = [":tools_exec_build_rs"], +) diff --git a/test/cargo_build_script/build.rs b/test/cargo_build_script/build.rs new file mode 100644 index 0000000000..1445366edc --- /dev/null +++ b/test/cargo_build_script/build.rs @@ -0,0 +1,4 @@ +fn main() { + // Pass the TOOL_PATH along to the rust_test so we can assert on it. + println!("cargo:rustc-env=TOOL_PATH={}", std::env::var("TOOL").unwrap()); +} \ No newline at end of file diff --git a/test/cargo_build_script/tools_exec.rs b/test/cargo_build_script/tools_exec.rs new file mode 100644 index 0000000000..8f76c3c150 --- /dev/null +++ b/test/cargo_build_script/tools_exec.rs @@ -0,0 +1,5 @@ +#[test] +pub fn test_tool_exec() { + let tool_path = env!("TOOL_PATH"); + assert!(tool_path.contains("-exec-"), "tool_path did not contain '-exec-': {}", tool_path); +} From eb32405b36d2d3a244d0988ab45a20b7108a2e54 Mon Sep 17 00:00:00 2001 From: David Marcin Date: Fri, 20 Aug 2021 15:40:01 -0700 Subject: [PATCH 5/6] Regenerate documentation --- docs/cargo.md | 7 +-- docs/cargo_build_script.md | 89 -------------------------------------- 2 files changed, 4 insertions(+), 92 deletions(-) delete mode 100644 docs/cargo_build_script.md diff --git a/docs/cargo.md b/docs/cargo.md index 31113260b2..2457c4ceb9 100644 --- a/docs/cargo.md +++ b/docs/cargo.md @@ -37,8 +37,8 @@ A rule for bootstrapping a Rust binary using [Cargo](https://doc.rust-lang.org/c ## cargo_build_script
-cargo_build_script(name, crate_features, version, deps, build_script_env, data, links, rustc_env,
-                   kwargs)
+cargo_build_script(name, crate_features, version, deps, build_script_env, data, tools, links,
+                   rustc_env, kwargs)
 
Compile and execute a rust build script to generate build attributes @@ -109,7 +109,8 @@ The `hello_lib` target will be build with the flags and the environment variable | version | The semantic version (semver) of the crate. | None | | deps | The dependencies of the crate. | [] | | build_script_env | Environment variables for build scripts. | {} | -| data | Files or tools needed by the build script. | [] | +| data | Files needed by the build script. | [] | +| tools | Tools (executables) needed by the build script. | [] | | links | Name of the native library this crate links against. | None | | rustc_env | Environment variables to set in rustc when compiling the build script. | {} | | kwargs | Forwards to the underlying rust_binary rule. | none | diff --git a/docs/cargo_build_script.md b/docs/cargo_build_script.md deleted file mode 100644 index 959e226d1c..0000000000 --- a/docs/cargo_build_script.md +++ /dev/null @@ -1,89 +0,0 @@ - -# Cargo Build Script - -* [cargo_build_script](#cargo_build_script) - - - -## cargo_build_script - -
-cargo_build_script(name, crate_features, version, deps, build_script_env, data, tools, links,
-                   rustc_env, kwargs)
-
- -Compile and execute a rust build script to generate build attributes - -This rules take the same arguments as rust_binary. - -Example: - -Suppose you have a crate with a cargo build script `build.rs`: - -```output -[workspace]/ - hello_lib/ - BUILD - build.rs - src/ - lib.rs -``` - -Then you want to use the build script in the following: - -`hello_lib/BUILD`: -```python -package(default_visibility = ["//visibility:public"]) - -load("@rules_rust//rust:rust.bzl", "rust_binary", "rust_library") -load("@rules_rust//cargo:cargo_build_script.bzl", "cargo_build_script") - -# This will run the build script from the root of the workspace, and -# collect the outputs. -cargo_build_script( - name = "build_script", - srcs = ["build.rs"], - # Optional environment variables passed during build.rs compilation - rustc_env = { - "CARGO_PKG_VERSION": "0.1.2", - }, - # Optional environment variables passed during build.rs execution. - # Note that as the build script's working directory is not execroot, - # execpath/location will return an absolute path, instead of a relative - # one. - build_script_env = { - "SOME_TOOL_OR_FILE": "$(execpath @tool//:binary)" - } - # Optional data/tool dependencies - data = ["@tool//:binary"], -) - -rust_library( - name = "hello_lib", - srcs = [ - "src/lib.rs", - ], - deps = [":build_script"], -) -``` - -The `hello_lib` target will be build with the flags and the environment variables declared by the build script in addition to the file generated by it. - - -**PARAMETERS** - - -| Name | Description | Default Value | -| :------------- | :------------- | :------------- | -| name | The name for the underlying rule. This should be the name of the package being compiled, optionally with a suffix of _build_script. | none | -| crate_features | A list of features to enable for the build script. | [] | -| version | The semantic version (semver) of the crate. | None | -| deps | The dependencies of the crate. | [] | -| build_script_env | Environment variables for build scripts. | {} | -| data | Files needed by the build script. | [] | -| tools | Tools (executables) needed by the build script. | [] | -| links | Name of the native library this crate links against. | None | -| rustc_env | Environment variables to set in rustc when compiling the build script. | {} | -| kwargs | Forwards to the underlying rust_binary rule. | none | - - From d51da84e3421030cd26e9903a7352e88bd7226af Mon Sep 17 00:00:00 2001 From: David Marcin Date: Fri, 20 Aug 2021 15:41:31 -0700 Subject: [PATCH 6/6] Fix lint --- test/cargo_build_script/build.rs | 7 +++++-- test/cargo_build_script/tools_exec.rs | 6 +++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/test/cargo_build_script/build.rs b/test/cargo_build_script/build.rs index 1445366edc..7b8b980d0f 100644 --- a/test/cargo_build_script/build.rs +++ b/test/cargo_build_script/build.rs @@ -1,4 +1,7 @@ fn main() { // Pass the TOOL_PATH along to the rust_test so we can assert on it. - println!("cargo:rustc-env=TOOL_PATH={}", std::env::var("TOOL").unwrap()); -} \ No newline at end of file + println!( + "cargo:rustc-env=TOOL_PATH={}", + std::env::var("TOOL").unwrap() + ); +} diff --git a/test/cargo_build_script/tools_exec.rs b/test/cargo_build_script/tools_exec.rs index 8f76c3c150..6b06920825 100644 --- a/test/cargo_build_script/tools_exec.rs +++ b/test/cargo_build_script/tools_exec.rs @@ -1,5 +1,9 @@ #[test] pub fn test_tool_exec() { let tool_path = env!("TOOL_PATH"); - assert!(tool_path.contains("-exec-"), "tool_path did not contain '-exec-': {}", tool_path); + assert!( + tool_path.contains("-exec-"), + "tool_path did not contain '-exec-': {}", + tool_path + ); }