Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to pass environment variables to rustc #315

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,14 @@ _rust_common_attrs = {
"rustc_flags": attr.string_list(
doc = "List of compiler flags passed to `rustc`.",
),
"rustc_env": attr.string_list(
doc = _tidy("""
List of environment variables passed to `rustc`.

This should have a similar effect to the `cargo:rustc-env=VAR=VALUE`
declaration which can be emitted from build scripts.
"""),
),
"version": attr.string(
doc = "A version to inject in the cargo environment variable.",
default = "0.0.0",
Expand Down
11 changes: 10 additions & 1 deletion rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def _get_rustc_env(ctx, toolchain):
patch, pre = patch.split("-", 1)
else:
pre = ""
return {

env = {
"CARGO_PKG_VERSION": version,
"CARGO_PKG_VERSION_MAJOR": major,
"CARGO_PKG_VERSION_MINOR": minor,
Expand All @@ -76,6 +77,14 @@ def _get_rustc_env(ctx, toolchain):
"CARGO_CFG_TARGET_ARCH": toolchain.target_arch,
}

# Add any additional explicitly passed environment variables.
extra_envs = []
for kv in getattr(ctx.attr, "rustc_env", []):
k, v = kv.split("=")
Copy link
Contributor

@tommilligan tommilligan May 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail for values that contain an = character, such as A=B=C, which should be parsed to ["A", "B=C"]. This could be fixed with

k, v = kv.split("=", 1)

extra_envs.append([k, v])
env.update(extra_envs)
return env

def _get_compilation_mode_opts(ctx, toolchain):
comp_mode = ctx.var["COMPILATION_MODE"]
if not comp_mode in toolchain.compilation_mode_opts:
Expand Down
7 changes: 6 additions & 1 deletion test/build_env/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ load(
)

rust_test(
name = "conflicting_deps_test",
name = "environment_variable_test",
srcs = ["tests/manifest_dir.rs"],
data = ["src/manifest_dir_file.txt"],

rustc_env = [
"ARBITRARY_ENV1=Value1",
"ARBITRARY_ENV2=Value2",
],
)
6 changes: 6 additions & 0 deletions test/build_env/tests/manifest_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ pub fn test_manifest_dir() {
let expected = "This file tests that CARGO_MANIFEST_DIR is set for the build environment\n";
assert_eq!(actual, expected);
}

#[test]
pub fn test_arbitrary_env() {
assert_eq!(env!("ARBITRARY_ENV1"), "Value1");
assert_eq!(env!("ARBITRARY_ENV2"), "Value2");
}