Skip to content

Commit b4e0ffb

Browse files
committed
Include current_exe in hash for linker wrapper path
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.
1 parent 6e6a046 commit b4e0ffb

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

src/zig.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use std::str;
1212
use anyhow::{anyhow, bail, Context, Result};
1313
use fs_err as fs;
1414
use path_slash::PathBufExt;
15+
#[cfg(not(target_family = "unix"))]
16+
use path_slash::PathExt;
1517
use serde::Deserialize;
1618
use target_lexicon::{Architecture, Environment, OperatingSystem, Triple};
1719

@@ -1368,14 +1370,20 @@ pub fn prepare_zig_linker(target: &str) -> Result<ZigWrapper> {
13681370
}
13691371
}
13701372

1373+
let current_exe = if let Ok(exe) = env::var("CARGO_BIN_EXE_cargo-zigbuild") {
1374+
PathBuf::from(exe)
1375+
} else {
1376+
env::current_exe()?
1377+
};
13711378
let cc_args_str = cc_args.join(" ");
1372-
let hash = crc::Crc::<u16>::new(&crc::CRC_16_IBM_SDLC).checksum(cc_args_str.as_bytes());
1379+
let hash_input = current_exe.display().to_string() + &cc_args_str;
1380+
let hash = crc::Crc::<u16>::new(&crc::CRC_16_IBM_SDLC).checksum(hash_input.as_bytes());
13731381
let zig_cc = zig_linker_dir.join(format!("zigcc-{file_target}-{:x}.{file_ext}", hash));
13741382
let zig_cxx = zig_linker_dir.join(format!("zigcxx-{file_target}-{:x}.{file_ext}", hash));
13751383
let zig_ranlib = zig_linker_dir.join(format!("zigranlib.{file_ext}"));
1376-
write_linker_wrapper(&zig_cc, "cc", &cc_args_str)?;
1377-
write_linker_wrapper(&zig_cxx, "c++", &cc_args_str)?;
1378-
write_linker_wrapper(&zig_ranlib, "ranlib", "")?;
1384+
write_linker_wrapper(&current_exe, &zig_cc, "cc", &cc_args_str)?;
1385+
write_linker_wrapper(&current_exe, &zig_cxx, "c++", &cc_args_str)?;
1386+
write_linker_wrapper(&current_exe, &zig_ranlib, "ranlib", "")?;
13791387

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

14251433
/// Write a zig cc wrapper batch script for unix
14261434
#[cfg(target_family = "unix")]
1427-
fn write_linker_wrapper(path: &Path, command: &str, args: &str) -> Result<()> {
1435+
fn write_linker_wrapper(
1436+
current_exe: &Path,
1437+
path: &Path,
1438+
command: &str,
1439+
args: &str,
1440+
) -> Result<()> {
14281441
let mut buf = Vec::<u8>::new();
1429-
let current_exe = if let Ok(exe) = env::var("CARGO_BIN_EXE_cargo-zigbuild") {
1430-
PathBuf::from(exe)
1431-
} else {
1432-
env::current_exe()?
1433-
};
14341442
writeln!(&mut buf, "#!/bin/sh")?;
14351443
writeln!(
14361444
&mut buf,
@@ -1458,13 +1466,13 @@ fn write_linker_wrapper(path: &Path, command: &str, args: &str) -> Result<()> {
14581466

14591467
/// Write a zig cc wrapper batch script for windows
14601468
#[cfg(not(target_family = "unix"))]
1461-
fn write_linker_wrapper(path: &Path, command: &str, args: &str) -> Result<()> {
1469+
fn write_linker_wrapper(
1470+
current_exe: &Path,
1471+
path: &Path,
1472+
command: &str,
1473+
args: &str,
1474+
) -> Result<()> {
14621475
let mut buf = Vec::<u8>::new();
1463-
let current_exe = if let Ok(exe) = env::var("CARGO_BIN_EXE_cargo-zigbuild") {
1464-
PathBuf::from(exe)
1465-
} else {
1466-
env::current_exe()?
1467-
};
14681476
let current_exe = if is_mingw_shell() {
14691477
current_exe.to_slash_lossy().to_string()
14701478
} else {

0 commit comments

Comments
 (0)