Skip to content

Commit c5ee4d2

Browse files
committed
provide correct paths to build scripts
As build scripts are built in a build script subfolder, the paths need to be one level up.
1 parent b1ed4f7 commit c5ee4d2

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

examples/env_locations/BUILD

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ load(
22
"@io_bazel_rules_rust//rust:rust.bzl",
33
"rust_test",
44
)
5+
load("@io_bazel_rules_rust//cargo:cargo_build_script.bzl", "cargo_build_script")
56

67
# generate a file
78
genrule(
@@ -10,22 +11,35 @@ genrule(
1011
cmd = "echo hello > $@",
1112
)
1213

14+
_env = {
15+
"GENERATED_DATA": "$(rootpath generated.data)",
16+
"SOME_TOOL": "$(rootpath @com_google_protobuf//:protoc)",
17+
}
18+
19+
_data = [
20+
# provide the generated file as a dep
21+
"generated.data",
22+
# we should also be able to access external binaries
23+
# such as protoc.
24+
"@com_google_protobuf//:protoc",
25+
]
26+
27+
cargo_build_script(
28+
name = "build",
29+
srcs = ["build.rs"],
30+
data = _data,
31+
rustc_env = _env,
32+
)
33+
1334
rust_test(
1435
name = "test",
1536
srcs = [
1637
"main.rs",
1738
],
18-
data = [
19-
# provide the generated file as a dep
20-
"generated.data",
21-
# we should also be able to access external binaries
22-
# such as protoc.
23-
"@com_google_protobuf//:protoc",
24-
],
39+
data = _data,
2540
edition = "2018",
26-
rustc_env = {
27-
# and pass in the locations to rustc
28-
"GENERATED_DATA": "$(rootpath generated.data)",
29-
"SOME_TOOL": "$(rootpath @com_google_protobuf//:protoc)",
30-
},
41+
rustc_env = _env,
42+
deps = [
43+
":build",
44+
],
3145
)

examples/env_locations/build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let generated_data = std::fs::read_to_string(env!("GENERATED_DATA")).unwrap();
3+
// our generated data file should be readable
4+
assert_eq!(generated_data, "hello\n");
5+
// and we should be able to read (and thus execute) our tool
6+
assert_eq!(std::fs::read(env!("SOME_TOOL")).unwrap().is_empty(), false);
7+
}

rust/private/rustc.bzl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,23 @@ def get_linker_and_args(ctx, cc_toolchain, feature_configuration, rpaths):
311311

312312
return ld, link_args, link_env
313313

314+
def _expand_location(ctx, data, is_build_script, env_string):
315+
expanded = ctx.expand_location(env_string, data)
316+
if env_string != expanded and is_build_script:
317+
# locations are relative to parent folder when building
318+
# a build script
319+
expanded = "../" + expanded
320+
return expanded
321+
314322
def _expand_locations(ctx, env, aspect):
315323
"Expand $(location ...) references in user-provided env vars."
316324
if aspect:
317325
data = getattr(ctx.rule.attr, "data", [])
318326
else:
319327
data = getattr(ctx.attr, "data", [])
320-
return dict([(k, ctx.expand_location(v, data)) for (k, v) in env.items()])
328+
is_build_script = ctx.label.name.endswith("_script_")
329+
330+
return dict([(k, _expand_location(ctx, data, is_build_script, v)) for (k, v) in env.items()])
321331

322332
def _process_build_scripts(
323333
ctx,

0 commit comments

Comments
 (0)