-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Allow cargo:rustc-env in build scripts #3929
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -305,12 +305,15 @@ fn rustc(cx: &mut Context, unit: &Unit, exec: Arc<Executor>) -> CargoResult<Work | |
// also need to be sure to add any -L paths for our plugins to the | ||
// dynamic library load path as a plugin's dynamic library may be | ||
// located somewhere in there. | ||
// Finally, if custom environment variables have been produced by | ||
// previous build scripts, we include them in the rustc invocation. | ||
if let Some(build_deps) = build_deps { | ||
let build_state = build_state.outputs.lock().unwrap(); | ||
add_native_deps(&mut rustc, &build_state, &build_deps, | ||
pass_l_flag, ¤t_id)?; | ||
add_plugin_deps(&mut rustc, &build_state, &build_deps, | ||
&root_output)?; | ||
add_custom_env(&mut rustc, &build_state, &build_deps, ¤t_id)?; | ||
} | ||
|
||
// FIXME(rust-lang/rust#18913): we probably shouldn't have to do | ||
|
@@ -420,6 +423,21 @@ fn rustc(cx: &mut Context, unit: &Unit, exec: Arc<Executor>) -> CargoResult<Work | |
} | ||
Ok(()) | ||
} | ||
|
||
// Add all custom environment variables present in `state` (after they've | ||
// been put there by one of the `build_scripts`) to the command provided. | ||
fn add_custom_env(rustc: &mut ProcessBuilder, | ||
build_state: &BuildMap, | ||
_: &BuildScripts, | ||
current_id: &PackageId) -> CargoResult<()> { | ||
let key = (current_id.clone(), Kind::Host); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes here you'll want to pass in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, so that's what host vs target means. Makes sense! Fixed. |
||
if let Some(output) = build_state.get(&key) { | ||
for &(ref name, ref value) in output.env.iter() { | ||
rustc.env(name, value); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// Link the compiled target (often of form foo-{metadata_hash}) to the | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1575,6 +1575,86 @@ fn cfg_override_doc() { | |
assert_that(&p.root().join("target/doc/bar/fn.bar.html"), existing_file()); | ||
} | ||
|
||
#[test] | ||
fn env_build() { | ||
let build = project("builder") | ||
.file("Cargo.toml", r#" | ||
[package] | ||
name = "builder" | ||
version = "0.0.1" | ||
authors = [] | ||
build = "build.rs" | ||
"#) | ||
.file("src/main.rs", r#" | ||
const FOO: &'static str = env!("FOO"); | ||
fn main() { | ||
println!("{}", FOO); | ||
} | ||
"#) | ||
.file("build.rs", r#" | ||
fn main() { | ||
println!("cargo:rustc-env=FOO=foo"); | ||
} | ||
"#); | ||
assert_that(build.cargo_process("build").arg("-v"), | ||
execs().with_status(0)); | ||
assert_that(build.cargo("run").arg("-v"), | ||
execs().with_status(0).with_stdout("foo\n")); | ||
} | ||
|
||
#[test] | ||
fn env_test() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem is that this test still fails at the build step (with |
||
let p = project("foo") | ||
.file("Cargo.toml", r#" | ||
[package] | ||
name = "foo" | ||
version = "0.0.1" | ||
authors = [] | ||
build = "build.rs" | ||
"#) | ||
.file("build.rs", r#" | ||
fn main() { | ||
println!("cargo:rustc-env=FOO=foo"); | ||
} | ||
"#) | ||
.file("src/lib.rs", r#" | ||
pub const FOO: &'static str = env!("FOO"); | ||
"#) | ||
.file("tests/test.rs", r#" | ||
extern crate foo; | ||
|
||
#[test] | ||
fn test_foo() { | ||
assert_eq!("foo", foo::FOO); | ||
} | ||
"#); | ||
assert_that(p.cargo_process("test").arg("-v"), | ||
execs().with_stderr(format!("\ | ||
[COMPILING] foo v0.0.1 ({dir}) | ||
[RUNNING] [..] build.rs [..] | ||
[RUNNING] `[..][/]build-script-build` | ||
[RUNNING] [..] --cfg foo[..] | ||
[RUNNING] [..] --cfg foo[..] | ||
[RUNNING] [..] --cfg foo[..] | ||
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] | ||
[RUNNING] `[..][/]foo-[..][EXE]` | ||
[RUNNING] `[..][/]test-[..][EXE]` | ||
[DOCTEST] foo | ||
[RUNNING] [..] --cfg foo[..]", dir = p.url())) | ||
.with_stdout(" | ||
running 0 tests | ||
|
||
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured | ||
|
||
|
||
running 1 test | ||
test test_foo ... ok | ||
|
||
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured | ||
|
||
")); | ||
} | ||
|
||
#[test] | ||
fn flags_go_into_tests() { | ||
let p = project("foo") | ||
|
@@ -1816,7 +1896,7 @@ fn fresh_builds_possible_with_link_libs() { | |
rustc-flags = \"-l z -L ./\" | ||
", target)) | ||
.file("build.rs", ""); | ||
|
||
assert_that(p.cargo_process("build").arg("-v"), | ||
execs().with_status(0).with_stderr("\ | ||
[COMPILING] foo v0.5.0 ([..] | ||
|
@@ -1857,7 +1937,7 @@ fn fresh_builds_possible_with_multiple_metadata_overrides() { | |
e = \"\" | ||
", target)) | ||
.file("build.rs", ""); | ||
|
||
assert_that(p.cargo_process("build").arg("-v"), | ||
execs().with_status(0).with_stderr("\ | ||
[COMPILING] foo v0.5.0 ([..] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah feel free to just remvoe this parameter if it's not used.