Skip to content

Commit 1a07767

Browse files
committed
add failing test for CARGO_CFG_TARGET_FEATURE
When setting target features via rustflags via `[build]` config key, cargo correctly propagates them to rustc (via -C flag) and to build.rs (via CARGO_CFG_TARGET_FEATURE env var). However, if `[target.cfg]` is used instead, build.rs doesn't get the flags (rustc still gets them though).
1 parent 8dea819 commit 1a07767

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

tests/testsuite/build_script_env.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,38 @@ fn rustc_bootstrap() {
173173
.with_status(101)
174174
.run();
175175
}
176+
177+
#[cargo_test]
178+
#[cfg(target_arch = "x86_64")]
179+
fn build_script_sees_cfg_target_feature() {
180+
let build_rs = r#"
181+
fn main() {
182+
let cfg = std::env::var("CARGO_CFG_TARGET_FEATURE").unwrap();
183+
eprintln!("CARGO_CFG_TARGET_FEATURE={cfg}");
184+
}
185+
"#;
186+
187+
let configs = [
188+
r#"
189+
[build]
190+
rustflags = ["-Ctarget-feature=+sse4.1,+sse4.2"]
191+
"#,
192+
r#"
193+
[target.'cfg(target_arch = "x86_64")']
194+
rustflags = ["-Ctarget-feature=+sse4.1,+sse4.2"]
195+
"#,
196+
];
197+
198+
for config in configs {
199+
let p = project()
200+
.file(".cargo/config.toml", config)
201+
.file("src/lib.rs", r#""#)
202+
.file("build.rs", build_rs)
203+
.build();
204+
205+
p.cargo("build -vv")
206+
.with_stderr_contains("[foo 0.0.1] CARGO_CFG_TARGET_FEATURE=[..]sse4.2[..]")
207+
.with_stderr_contains("[..]-Ctarget-feature=[..]+sse4.2[..]")
208+
.run();
209+
}
210+
}

0 commit comments

Comments
 (0)