-
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.
$(location ...) expansion and runtime data support for build scripts
- rustc_env now expands $(rootpath //package:target), for passing the location of files that are needed at compile time - build_script_env now expands $(execpath //package:target), for passing the location of files or tools that are needed at build script runtime - cargo_script_build() now passes the data argument to the build script runner, so the data is available at runtime
- Loading branch information
Showing
9 changed files
with
146 additions
and
9 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
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,51 @@ | ||
load( | ||
"@io_bazel_rules_rust//rust:rust.bzl", | ||
"rust_test", | ||
) | ||
load("@io_bazel_rules_rust//cargo:cargo_build_script.bzl", "cargo_build_script") | ||
|
||
# generate a file | ||
genrule( | ||
name = "data_generator", | ||
outs = ["generated.data"], | ||
cmd = "echo hello > $@", | ||
) | ||
|
||
_data = [ | ||
# we should be able to read non-generated source/data files | ||
"source.file", | ||
# and generated files as well | ||
"generated.data", | ||
# we should also be able to access external binaries | ||
# such as protoc. | ||
"@com_google_protobuf//:protoc", | ||
] | ||
|
||
cargo_build_script( | ||
name = "build", | ||
srcs = ["build.rs"], | ||
build_script_env = { | ||
# both execpath and location should work | ||
"SOURCE_FILE": "$(execpath source.file)", | ||
"GENERATED_DATA": "$(location generated.data)", | ||
"SOME_TOOL": "$(execpath @com_google_protobuf//:protoc)", | ||
}, | ||
data = _data, | ||
) | ||
|
||
rust_test( | ||
name = "test", | ||
srcs = [ | ||
"main.rs", | ||
], | ||
data = _data, | ||
edition = "2018", | ||
rustc_env = { | ||
"SOURCE_FILE": "$(rootpath source.file)", | ||
"GENERATED_DATA": "$(rootpath generated.data)", | ||
"SOME_TOOL": "$(rootpath @com_google_protobuf//:protoc)", | ||
}, | ||
deps = [ | ||
":build", | ||
], | ||
) |
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,17 @@ | ||
use std::{fs,env}; | ||
|
||
fn main() { | ||
// our source file should be readable | ||
let path = env::var("SOURCE_FILE").unwrap(); | ||
let generated_data = fs::read_to_string(&path).unwrap(); | ||
assert_eq!(generated_data, "source\n"); | ||
|
||
// our generated data file should be readable | ||
let path = env::var("GENERATED_DATA").unwrap(); | ||
let generated_data = fs::read_to_string(&path).unwrap(); | ||
assert_eq!(generated_data, "hello\n"); | ||
|
||
// and we should be able to read (and thus execute) our tool | ||
let path = env::var("SOME_TOOL").unwrap(); | ||
assert_eq!(fs::read(&path).unwrap().is_empty(), false); | ||
} |
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,11 @@ | ||
#[test] | ||
fn test() { | ||
// our source file should be readable | ||
let source_file = std::fs::read_to_string(env!("SOURCE_FILE")).unwrap(); | ||
assert_eq!(source_file, "source\n"); | ||
// our generated data file should be readable | ||
let generated_data = std::fs::read_to_string(env!("GENERATED_DATA")).unwrap(); | ||
assert_eq!(generated_data, "hello\n"); | ||
// and we should be able to read (and thus execute) our tool | ||
assert_eq!(std::fs::read(env!("SOME_TOOL")).unwrap().is_empty(), false); | ||
} |
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 @@ | ||
source |
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
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