diff --git a/cargo/private/cargo_build_script_wrapper.bzl b/cargo/private/cargo_build_script_wrapper.bzl index c82ffdb15d..1efee8c134 100644 --- a/cargo/private/cargo_build_script_wrapper.bzl +++ b/cargo/private/cargo_build_script_wrapper.bzl @@ -26,6 +26,7 @@ def cargo_build_script( links = None, rundir = None, rustc_env = {}, + rustc_env_files = [], rustc_flags = [], visibility = None, tags = None, @@ -113,6 +114,8 @@ def cargo_build_script( If set to `.`, the cargo build script will run in the exec root. rustc_env (dict, optional): Environment variables to set in rustc when compiling the build script. + rustc_env_files (list of label, optional): Files containing additional environment variables to set for rustc + when building the build script. rustc_flags (list, optional): List of compiler flags passed to `rustc`. visibility (list of label, optional): Visibility to apply to the generated build script output. tags: (list of str, optional): Tags to apply to the generated build script output. @@ -149,6 +152,7 @@ def cargo_build_script( data = data, compile_data = compile_data, rustc_env = rustc_env, + rustc_env_files = rustc_env_files, rustc_flags = rustc_flags, edition = edition, tags = binary_tags, diff --git a/docs/cargo.md b/docs/cargo.md index 8492420581..890c6bf582 100644 --- a/docs/cargo.md +++ b/docs/cargo.md @@ -66,7 +66,7 @@ A rule for generating variables for dependent `cargo_build_script`s without a bu
 cargo_build_script(name, edition, crate_name, crate_root, srcs, crate_features, version, deps,
                    link_deps, proc_macro_deps, build_script_env, data, compile_data, tools, links,
-                   rundir, rustc_env, rustc_flags, visibility, tags, aliases, kwargs)
+                   rundir, rustc_env, rustc_env_files, rustc_flags, visibility, tags, aliases, kwargs)
 
Compile and execute a rust build script to generate build attributes @@ -149,6 +149,7 @@ The `hello_lib` target will be build with the flags and the environment variable | links | Name of the native library this crate links against. | `None` | | rundir | A directory to cd to before the cargo_build_script is run. This should be a path relative to the exec root.

The default behaviour (and the behaviour if rundir is set to the empty string) is to change to the relative path corresponding to the cargo manifest directory, which replicates the normal behaviour of cargo so it is easy to write compatible build scripts.

If set to ., the cargo build script will run in the exec root. | `None` | | rustc_env | Environment variables to set in rustc when compiling the build script. | `{}` | +| rustc_env_files | Files containing additional environment variables to set for rustc when building the build script. | `[]` | | rustc_flags | List of compiler flags passed to rustc. | `[]` | | visibility | Visibility to apply to the generated build script output. | `None` | | tags | (list of str, optional): Tags to apply to the generated build script output. | `None` | diff --git a/docs/flatten.md b/docs/flatten.md index d11bb6f529..a2b8d0b52f 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -1547,7 +1547,7 @@ A collection of files either found within the `rust-stdlib` artifact or generate
 cargo_build_script(name, edition, crate_name, crate_root, srcs, crate_features, version, deps,
                    link_deps, proc_macro_deps, build_script_env, data, compile_data, tools, links,
-                   rundir, rustc_env, rustc_flags, visibility, tags, aliases, kwargs)
+                   rundir, rustc_env, rustc_env_files, rustc_flags, visibility, tags, aliases, kwargs)
 
Compile and execute a rust build script to generate build attributes @@ -1630,6 +1630,7 @@ The `hello_lib` target will be build with the flags and the environment variable | links | Name of the native library this crate links against. | `None` | | rundir | A directory to cd to before the cargo_build_script is run. This should be a path relative to the exec root.

The default behaviour (and the behaviour if rundir is set to the empty string) is to change to the relative path corresponding to the cargo manifest directory, which replicates the normal behaviour of cargo so it is easy to write compatible build scripts.

If set to ., the cargo build script will run in the exec root. | `None` | | rustc_env | Environment variables to set in rustc when compiling the build script. | `{}` | +| rustc_env_files | Files containing additional environment variables to set for rustc when building the build script. | `[]` | | rustc_flags | List of compiler flags passed to rustc. | `[]` | | visibility | Visibility to apply to the generated build script output. | `None` | | tags | (list of str, optional): Tags to apply to the generated build script output. | `None` | diff --git a/examples/hello_world/example_test.sh b/examples/hello_world/example_test.sh new file mode 100755 index 0000000000..e69de29bb2 diff --git a/test/cargo_build_script/rustc_env_files/BUILD.bazel b/test/cargo_build_script/rustc_env_files/BUILD.bazel new file mode 100644 index 0000000000..e00ccacba7 --- /dev/null +++ b/test/cargo_build_script/rustc_env_files/BUILD.bazel @@ -0,0 +1,31 @@ +load("@bazel_skylib//rules:write_file.bzl", "write_file") +load("//cargo:defs.bzl", "cargo_build_script") +load("//rust:defs.bzl", "rust_library", "rust_test") + +write_file( + name = "generate_rustc_env_file", + out = "rustc_env_file", + content = [ + "GREETING=Howdy", + "", + ], +) + +cargo_build_script( + name = "cargo_build_script", + srcs = ["build.rs"], + edition = "2018", + rustc_env_files = [":generate_rustc_env_file"], +) + +rust_library( + name = "test_lib", + srcs = ["test_lib.rs"], + edition = "2018", + deps = [":cargo_build_script"], +) + +rust_test( + name = "consume_build_script", + crate = ":test_lib", +) diff --git a/test/cargo_build_script/rustc_env_files/build.rs b/test/cargo_build_script/rustc_env_files/build.rs new file mode 100644 index 0000000000..ac12f121b5 --- /dev/null +++ b/test/cargo_build_script/rustc_env_files/build.rs @@ -0,0 +1,3 @@ +fn main() { + println!("cargo:rustc-env=FROM_BUILD_SCRIPT={}", env!("GREETING")); +} diff --git a/test/cargo_build_script/rustc_env_files/test_lib.rs b/test/cargo_build_script/rustc_env_files/test_lib.rs new file mode 100644 index 0000000000..c4bb6f8268 --- /dev/null +++ b/test/cargo_build_script/rustc_env_files/test_lib.rs @@ -0,0 +1,4 @@ +#[test] +fn check_env_set() { + assert_eq!("Howdy", env!("FROM_BUILD_SCRIPT")); +}