Skip to content

Commit 9f60229

Browse files
committed
Auto merge of #1288 - alexcrichton:issue-1058, r=huonw
Previously the build script's profile was used instead of the target's build script, causing the wrong environment variables to get set. Closes #1058
2 parents 918a415 + f91bc16 commit 9f60229

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/cargo/ops/cargo_rustc/custom_build.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@ pub fn prepare(pkg: &Package, target: &Target, req: Platform,
5050
let to_exec = script_output.join(to_exec);
5151

5252
// Start preparing the process to execute, starting out with some
53-
// environment variables.
54-
let profile = target.profile();
53+
// environment variables. Note that the profile-related environment
54+
// variables are not set with this the build script's profile but rather the
55+
// package's profile (some target which isn't a build script).
56+
let profile_target = pkg.targets().iter().find(|t| {
57+
cx.is_relevant_target(t) && !t.profile().is_custom_build()
58+
}).unwrap_or(target);
59+
let profile = cx.profile(profile_target);
5560
let to_exec = CString::from_slice(to_exec.as_vec());
5661
let p = try!(super::process(CommandType::Host(to_exec), pkg, target, cx));
5762
let mut p = p.env("OUT_DIR", Some(&build_output))

tests/test_cargo_compile_custom_build.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,3 +1077,26 @@ test!(build_script_with_dynamic_native_dependency {
10771077
assert_that(foo.cargo_process("build").env("SRC", Some(lib.as_vec())),
10781078
execs().with_status(0));
10791079
});
1080+
1081+
test!(profile_and_opt_level_set_correctly {
1082+
let build = project("builder")
1083+
.file("Cargo.toml", r#"
1084+
[package]
1085+
name = "builder"
1086+
version = "0.0.1"
1087+
authors = []
1088+
build = "build.rs"
1089+
"#)
1090+
.file("src/lib.rs", "")
1091+
.file("build.rs", r#"
1092+
use std::env;
1093+
1094+
fn main() {
1095+
assert_eq!(env::var("OPT_LEVEL").unwrap(), "3");
1096+
assert_eq!(env::var("PROFILE").unwrap(), "bench");
1097+
assert_eq!(env::var("DEBUG").unwrap(), "false");
1098+
}
1099+
"#);
1100+
assert_that(build.cargo_process("bench"),
1101+
execs().with_status(0));
1102+
});

0 commit comments

Comments
 (0)