Skip to content

Commit 176aff7

Browse files
authored
Add tools attr with exec transition for cargo_build_script (#885)
If tools are passed in `data`, they must be built in the `exec` configuration in order to run on the host machine when the target != host. I _think_ this should be a fairly safe change, unless someone is doing something with the tools passed to the build script that would require them to run on the target configuration, but short of copying these tools into the out_dir I don't know what that would be. And -- that would be better handled by depending directly on these tools in the data attr of the library or binary anyway.
1 parent d2bf64f commit 176aff7

File tree

6 files changed

+62
-10
lines changed

6 files changed

+62
-10
lines changed

cargo/cargo_build_script.bzl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,16 @@ def _build_script_impl(ctx):
120120
ctx,
121121
ctx.attr.build_script_env,
122122
getattr(ctx.attr, "data", []) +
123-
getattr(ctx.attr, "compile_data", []),
123+
getattr(ctx.attr, "compile_data", []) +
124+
getattr(ctx.attr, "tools", []),
124125
))
125126

126127
tools = depset(
127128
direct = [
128129
script,
129130
ctx.executable._cargo_build_script_runner,
130131
toolchain.rustc,
131-
] + ctx.files.data + ([toolchain.target_json] if toolchain.target_json else []),
132+
] + ctx.files.data + ctx.files.tools + ([toolchain.target_json] if toolchain.target_json else []),
132133
transitive = toolchain_tools,
133134
)
134135

@@ -195,7 +196,7 @@ _build_script_run = rule(
195196
doc = "The list of rust features that the build script should consider activated.",
196197
),
197198
"data": attr.label_list(
198-
doc = "Data or tools required by the build script.",
199+
doc = "Data required by the build script.",
199200
allow_files = True,
200201
),
201202
"deps": attr.label_list(
@@ -214,6 +215,11 @@ _build_script_run = rule(
214215
mandatory = True,
215216
cfg = "exec",
216217
),
218+
"tools": attr.label_list(
219+
doc = "Tools required by the build script.",
220+
allow_files = True,
221+
cfg = "exec",
222+
),
217223
"version": attr.string(
218224
doc = "The semantic version (semver) of the crate",
219225
),
@@ -242,6 +248,7 @@ def cargo_build_script(
242248
deps = [],
243249
build_script_env = {},
244250
data = [],
251+
tools = [],
245252
links = None,
246253
rustc_env = {},
247254
visibility = None,
@@ -311,7 +318,8 @@ def cargo_build_script(
311318
version (str, optional): The semantic version (semver) of the crate.
312319
deps (list, optional): The dependencies of the crate.
313320
build_script_env (dict, optional): Environment variables for build scripts.
314-
data (list, optional): Files or tools needed by the build script.
321+
data (list, optional): Files needed by the build script.
322+
tools (list, optional): Tools (executables) needed by the build script.
315323
links (str, optional): Name of the native library this crate links against.
316324
rustc_env (dict, optional): Environment variables to set in rustc when compiling the build script.
317325
visibility (list of label, optional): Visibility to apply to the generated build script output.
@@ -346,6 +354,7 @@ def cargo_build_script(
346354
links = links,
347355
deps = deps,
348356
data = data,
357+
tools = tools,
349358
visibility = visibility,
350359
tags = tags,
351360
)

docs/cargo.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ A rule for bootstrapping a Rust binary using [Cargo](https://doc.rust-lang.org/c
3737
## cargo_build_script
3838

3939
<pre>
40-
cargo_build_script(<a href="#cargo_build_script-name">name</a>, <a href="#cargo_build_script-crate_features">crate_features</a>, <a href="#cargo_build_script-version">version</a>, <a href="#cargo_build_script-deps">deps</a>, <a href="#cargo_build_script-build_script_env">build_script_env</a>, <a href="#cargo_build_script-data">data</a>, <a href="#cargo_build_script-links">links</a>, <a href="#cargo_build_script-rustc_env">rustc_env</a>,
41-
<a href="#cargo_build_script-visibility">visibility</a>, <a href="#cargo_build_script-tags">tags</a>, <a href="#cargo_build_script-kwargs">kwargs</a>)
40+
cargo_build_script(<a href="#cargo_build_script-name">name</a>, <a href="#cargo_build_script-crate_features">crate_features</a>, <a href="#cargo_build_script-version">version</a>, <a href="#cargo_build_script-deps">deps</a>, <a href="#cargo_build_script-build_script_env">build_script_env</a>, <a href="#cargo_build_script-data">data</a>, <a href="#cargo_build_script-tools">tools</a>, <a href="#cargo_build_script-links">links</a>,
41+
<a href="#cargo_build_script-rustc_env">rustc_env</a>, <a href="#cargo_build_script-visibility">visibility</a>, <a href="#cargo_build_script-tags">tags</a>, <a href="#cargo_build_script-kwargs">kwargs</a>)
4242
</pre>
4343

4444
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
109109
| <a id="cargo_build_script-version"></a>version | The semantic version (semver) of the crate. | <code>None</code> |
110110
| <a id="cargo_build_script-deps"></a>deps | The dependencies of the crate. | <code>[]</code> |
111111
| <a id="cargo_build_script-build_script_env"></a>build_script_env | Environment variables for build scripts. | <code>{}</code> |
112-
| <a id="cargo_build_script-data"></a>data | Files or tools needed by the build script. | <code>[]</code> |
112+
| <a id="cargo_build_script-data"></a>data | Files needed by the build script. | <code>[]</code> |
113+
| <a id="cargo_build_script-tools"></a>tools | Tools (executables) needed by the build script. | <code>[]</code> |
113114
| <a id="cargo_build_script-links"></a>links | Name of the native library this crate links against. | <code>None</code> |
114115
| <a id="cargo_build_script-rustc_env"></a>rustc_env | Environment variables to set in rustc when compiling the build script. | <code>{}</code> |
115116
| <a id="cargo_build_script-visibility"></a>visibility | Visibility to apply to the generated build script output. | <code>None</code> |

docs/flatten.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,8 +1396,8 @@ A test rule for performing `rustfmt --check` on a set of targets
13961396
## cargo_build_script
13971397

13981398
<pre>
1399-
cargo_build_script(<a href="#cargo_build_script-name">name</a>, <a href="#cargo_build_script-crate_features">crate_features</a>, <a href="#cargo_build_script-version">version</a>, <a href="#cargo_build_script-deps">deps</a>, <a href="#cargo_build_script-build_script_env">build_script_env</a>, <a href="#cargo_build_script-data">data</a>, <a href="#cargo_build_script-links">links</a>, <a href="#cargo_build_script-rustc_env">rustc_env</a>,
1400-
<a href="#cargo_build_script-visibility">visibility</a>, <a href="#cargo_build_script-tags">tags</a>, <a href="#cargo_build_script-kwargs">kwargs</a>)
1399+
cargo_build_script(<a href="#cargo_build_script-name">name</a>, <a href="#cargo_build_script-crate_features">crate_features</a>, <a href="#cargo_build_script-version">version</a>, <a href="#cargo_build_script-deps">deps</a>, <a href="#cargo_build_script-build_script_env">build_script_env</a>, <a href="#cargo_build_script-data">data</a>, <a href="#cargo_build_script-tools">tools</a>, <a href="#cargo_build_script-links">links</a>,
1400+
<a href="#cargo_build_script-rustc_env">rustc_env</a>, <a href="#cargo_build_script-visibility">visibility</a>, <a href="#cargo_build_script-tags">tags</a>, <a href="#cargo_build_script-kwargs">kwargs</a>)
14011401
</pre>
14021402

14031403
Compile and execute a rust build script to generate build attributes
@@ -1468,7 +1468,8 @@ The `hello_lib` target will be build with the flags and the environment variable
14681468
| <a id="cargo_build_script-version"></a>version | The semantic version (semver) of the crate. | <code>None</code> |
14691469
| <a id="cargo_build_script-deps"></a>deps | The dependencies of the crate. | <code>[]</code> |
14701470
| <a id="cargo_build_script-build_script_env"></a>build_script_env | Environment variables for build scripts. | <code>{}</code> |
1471-
| <a id="cargo_build_script-data"></a>data | Files or tools needed by the build script. | <code>[]</code> |
1471+
| <a id="cargo_build_script-data"></a>data | Files needed by the build script. | <code>[]</code> |
1472+
| <a id="cargo_build_script-tools"></a>tools | Tools (executables) needed by the build script. | <code>[]</code> |
14721473
| <a id="cargo_build_script-links"></a>links | Name of the native library this crate links against. | <code>None</code> |
14731474
| <a id="cargo_build_script-rustc_env"></a>rustc_env | Environment variables to set in rustc when compiling the build script. | <code>{}</code> |
14741475
| <a id="cargo_build_script-visibility"></a>visibility | Visibility to apply to the generated build script output. | <code>None</code> |

test/cargo_build_script/BUILD.bazel

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Tests for the cargo_build_script rule"""
2+
3+
load("//cargo:cargo_build_script.bzl", "cargo_build_script")
4+
load("//rust:rust.bzl", "rust_test")
5+
6+
# Test that tools are built in the exec configuration.
7+
cargo_build_script(
8+
name = "tools_exec_build_rs",
9+
srcs = ["build.rs"],
10+
build_script_env = {"TOOL": "$(execpath :tool)"},
11+
tools = [":tool"],
12+
)
13+
14+
genrule(
15+
name = "tool",
16+
srcs = [],
17+
outs = ["tool-file"],
18+
cmd = "touch $@",
19+
)
20+
21+
rust_test(
22+
name = "tools_exec",
23+
srcs = ["tools_exec.rs"],
24+
deps = [":tools_exec_build_rs"],
25+
)

test/cargo_build_script/build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
// Pass the TOOL_PATH along to the rust_test so we can assert on it.
3+
println!(
4+
"cargo:rustc-env=TOOL_PATH={}",
5+
std::env::var("TOOL").unwrap()
6+
);
7+
}

test/cargo_build_script/tools_exec.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[test]
2+
pub fn test_tool_exec() {
3+
let tool_path = env!("TOOL_PATH");
4+
assert!(
5+
tool_path.contains("-exec-"),
6+
"tool_path did not contain '-exec-': {}",
7+
tool_path
8+
);
9+
}

0 commit comments

Comments
 (0)