Skip to content

Commit

Permalink
Don't use host's strip command when cross-compiling (#3333)
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski authored Jan 6, 2024
1 parent 7d773ea commit cc07409
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ fn hash_changed(
) -> Option<([u8; 8], PathBuf)> {
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher;
use std::io::Read;

let mut hasher = DefaultHasher::new();

Expand All @@ -45,19 +44,16 @@ fn hash_changed(
.chain(std::iter::once(Path::new("build.rs")));

for path in paths {
if let Ok(mut f) = std::fs::File::open(path) {
let mut buf = Vec::new();
f.read_to_end(&mut buf).unwrap();

if let Ok(buf) = std::fs::read(path) {
hasher.write(&buf);
} else {
panic!("Cannot open {}", path.display());
}
}

let strip = env::var("STRIP").unwrap_or_else(|_| "strip".to_string());

hasher.write(strip.as_bytes());
if let Some(cmd) = strip_command() {
hasher.write(cmd.as_bytes());
}

let hash = hasher.finish().to_be_bytes();

Expand Down Expand Up @@ -161,18 +157,13 @@ fn build_nasm_files() {

// Strip local symbols from the asm library since they
// confuse the debugger.
fn strip<P: AsRef<Path>>(obj: P) {
let strip = env::var("STRIP").unwrap_or_else(|_| "strip".to_string());

let mut cmd = std::process::Command::new(strip);

cmd.arg("-x").arg(obj.as_ref());

let _ = cmd.output();
if let Some(strip) = strip_command() {
let _ = std::process::Command::new(strip)
.arg("-x")
.arg(Path::new(&out_dir).join("librav1easm.a"))
.status();
}

strip(Path::new(&out_dir).join("librav1easm.a"));

std::fs::write(hash_path, &hash[..]).unwrap();
} else {
println!("cargo:rustc-link-search={out_dir}");
Expand All @@ -182,6 +173,27 @@ fn build_nasm_files() {
rerun_dir("src/ext/x86");
}

fn strip_command() -> Option<String> {
let target = env::var("TARGET").expect("TARGET");
// follows Cargo's naming convention for the linker setting
let normalized_target = target.replace('-', "_").to_uppercase();
let explicit_strip =
env::var(format!("CARGO_TARGET_{normalized_target}_STRIP"))
.ok()
.or_else(|| env::var("STRIP").ok());
if explicit_strip.is_some() {
return explicit_strip;
}

// strip command is target-specific, e.g. macOS's strip breaks MUSL's archives
let host = env::var("HOST").expect("HOST");
if host != target {
return None;
}

Some("strip".into())
}

#[cfg(feature = "asm")]
fn build_asm_files() {
use std::fs::File;
Expand Down

0 comments on commit cc07409

Please sign in to comment.