Skip to content

Commit

Permalink
Merge pull request #291 from dtolnay/target
Browse files Browse the repository at this point in the history
Rely on $TARGET always set during build script execution
  • Loading branch information
dtolnay authored Oct 18, 2024
2 parents 909f751 + 2d76092 commit 7c399e0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
7 changes: 2 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ fn main() -> io::Result<()> {
println!("cargo:rustc-check-cfg=cfg(host_os, values(\"windows\"))");

let out_dir = env::var_os("OUT_DIR").unwrap();
let target = env::var("TARGET").ok();
let target = env::var("TARGET").unwrap();
let path = Path::new(&out_dir).join("target");
let value = match target {
Some(target) => format!(r#"Some("{}")"#, target.escape_debug()),
None => "None".to_owned(),
};
let value = format!(r#""{}""#, target.escape_debug());
fs::write(path, value)?;

let host = env::var_os("HOST").unwrap();
Expand Down
8 changes: 3 additions & 5 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ fn features(project: &Project) -> Vec<String> {

fn target() -> Vec<&'static str> {
#[cfg(not(host_os = "windows"))]
const TARGET: Option<&str> = include!(concat!(env!("OUT_DIR"), "/target"));
const TARGET: &str = include!(concat!(env!("OUT_DIR"), "/target"));

#[cfg(host_os = "windows")]
const TARGET: Option<&str> = include!(concat!(env!("OUT_DIR"), "\\target"));
const TARGET: &str = include!(concat!(env!("OUT_DIR"), "\\target"));

// When --target flag is passed, cargo does not pass RUSTFLAGS to rustc when
// building proc-macro and build script even if the host and target triples
Expand All @@ -204,9 +204,7 @@ fn target() -> Vec<&'static str> {
// Therefore, expose a cfg to always treat the target as host.
if cfg!(trybuild_no_target) {
vec![]
} else if let Some(target) = TARGET {
vec!["--target", target]
} else {
vec![]
vec!["--target", TARGET]
}
}

0 comments on commit 7c399e0

Please sign in to comment.