Skip to content

Commit

Permalink
Include current_exe in hash for linker wrapper path
Browse files Browse the repository at this point in the history
Ensures that unique linker wrapper scripts are generated even when
current_exe is different. Fixes an issue with concurrent builds driven
through maturin.

Closes #318.
  • Loading branch information
tuxu committed Feb 13, 2025
1 parent 6e6a046 commit fb95efa
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/zig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1368,14 +1368,20 @@ pub fn prepare_zig_linker(target: &str) -> Result<ZigWrapper> {
}
}

let current_exe = if let Ok(exe) = env::var("CARGO_BIN_EXE_cargo-zigbuild") {
PathBuf::from(exe)
} else {
env::current_exe()?
};
let cc_args_str = cc_args.join(" ");
let hash = crc::Crc::<u16>::new(&crc::CRC_16_IBM_SDLC).checksum(cc_args_str.as_bytes());
let hash_input = current_exe.display().to_string() + &cc_args_str;
let hash = crc::Crc::<u16>::new(&crc::CRC_16_IBM_SDLC).checksum(hash_input.as_bytes());
let zig_cc = zig_linker_dir.join(format!("zigcc-{file_target}-{:x}.{file_ext}", hash));
let zig_cxx = zig_linker_dir.join(format!("zigcxx-{file_target}-{:x}.{file_ext}", hash));
let zig_ranlib = zig_linker_dir.join(format!("zigranlib.{file_ext}"));
write_linker_wrapper(&zig_cc, "cc", &cc_args_str)?;
write_linker_wrapper(&zig_cxx, "c++", &cc_args_str)?;
write_linker_wrapper(&zig_ranlib, "ranlib", "")?;
write_linker_wrapper(&current_exe, &zig_cc, "cc", &cc_args_str)?;
write_linker_wrapper(&current_exe, &zig_cxx, "c++", &cc_args_str)?;
write_linker_wrapper(&current_exe, &zig_ranlib, "ranlib", "")?;

let exe_ext = if cfg!(windows) { ".exe" } else { "" };
let zig_ar = zig_linker_dir.join(format!("ar{exe_ext}"));
Expand Down Expand Up @@ -1424,13 +1430,13 @@ fn symlink_wrapper(target: &Path) -> Result<()> {

/// Write a zig cc wrapper batch script for unix
#[cfg(target_family = "unix")]
fn write_linker_wrapper(path: &Path, command: &str, args: &str) -> Result<()> {
fn write_linker_wrapper(
current_exe: &PathBuf,
path: &Path,
command: &str,
args: &str,
) -> Result<()> {
let mut buf = Vec::<u8>::new();
let current_exe = if let Ok(exe) = env::var("CARGO_BIN_EXE_cargo-zigbuild") {
PathBuf::from(exe)
} else {
env::current_exe()?
};
writeln!(&mut buf, "#!/bin/sh")?;
writeln!(
&mut buf,
Expand Down Expand Up @@ -1458,13 +1464,13 @@ fn write_linker_wrapper(path: &Path, command: &str, args: &str) -> Result<()> {

/// Write a zig cc wrapper batch script for windows
#[cfg(not(target_family = "unix"))]
fn write_linker_wrapper(path: &Path, command: &str, args: &str) -> Result<()> {
fn write_linker_wrapper(
current_exe: &PathBuf,
path: &Path,
command: &str,
args: &str,
) -> Result<()> {
let mut buf = Vec::<u8>::new();
let current_exe = if let Ok(exe) = env::var("CARGO_BIN_EXE_cargo-zigbuild") {
PathBuf::from(exe)
} else {
env::current_exe()?
};
let current_exe = if is_mingw_shell() {
current_exe.to_slash_lossy().to_string()
} else {
Expand Down

0 comments on commit fb95efa

Please sign in to comment.