Skip to content

Commit 990bcc8

Browse files
authored
Merge pull request #31 from ravenexp/mingw-dlltool-conf
feat: Add MinGW `dlltool` program name config env var
2 parents a3b4216 + 0a63347 commit 990bcc8

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ for MinGW-w64 and MSVC (cross-)compile targets.
88
This crate **does not require** Python 3 distribution files
99
to be present on the (cross-)compile host system.
1010

11+
This crate uses the binutils `dlltool` program to generate
12+
the Python DLL import libraries for MinGW-w64 targets.
13+
Setting `PYO3_MINGW_DLLTOOL` environment variable overrides
14+
the default `dlltool` command name for the target.
15+
1116
**Note:** MSVC cross-compile targets require either LLVM binutils
1217
or Zig to be available on the host system.
1318
More specifically, `python3-dll-a` requires `llvm-dlltool` executable

src/lib.rs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
//! This crate **does not require** Python 3 distribution files
99
//! to be present on the (cross-)compile host system.
1010
//!
11+
//! This crate uses the binutils `dlltool` program to generate
12+
//! the Python DLL import libraries for MinGW-w64 targets.
13+
//! Setting `PYO3_MINGW_DLLTOOL` environment variable overrides
14+
//! the default `dlltool` command name for the target.
15+
//!
1116
//! **Note:** MSVC cross-compile targets require either LLVM binutils
1217
//! or Zig to be available on the host system.
1318
//! More specifically, `python3-dll-a` requires `llvm-dlltool` executable
@@ -335,19 +340,14 @@ impl DllToolCommand {
335340
return Ok(DllToolCommand::Zig { command, machine });
336341
}
337342

338-
match (arch, env) {
339-
// 64-bit MinGW-w64 (aka `x86_64-pc-windows-gnu`)
340-
("x86_64", "gnu") => Ok(DllToolCommand::Mingw {
341-
command: Command::new(DLLTOOL_GNU),
342-
}),
343-
344-
// 32-bit MinGW-w64 (aka `i686-pc-windows-gnu`)
345-
("x86", "gnu") => Ok(DllToolCommand::Mingw {
346-
command: Command::new(DLLTOOL_GNU_32),
343+
match env {
344+
// 64-bit and 32-bit MinGW-w64 (aka `{x86_64,i686}-pc-windows-gnu`)
345+
"gnu" => Ok(DllToolCommand::Mingw {
346+
command: get_mingw_dlltool(arch)?,
347347
}),
348348

349349
// MSVC ABI (multiarch)
350-
(_, "msvc") => {
350+
"msvc" => {
351351
if let Some(command) = find_lib_exe(arch) {
352352
// MSVC tools use their own target architecture names...
353353
let machine = match arch {
@@ -366,7 +366,7 @@ impl DllToolCommand {
366366
}
367367
}
368368
_ => {
369-
let msg = format!("Unsupported target arch '{}' or env ABI '{}'", arch, env);
369+
let msg = format!("Unsupported target env ABI '{}'", env);
370370
Err(Error::new(ErrorKind::Other, msg))
371371
}
372372
}
@@ -439,6 +439,31 @@ impl DllToolCommand {
439439
}
440440
}
441441

442+
/// Chooses the appropriate MinGW-w64 `dlltool` executable
443+
/// for the target architecture.
444+
///
445+
/// Examines the user-provided `PYO3_MINGW_DLLTOOL` environment variable first
446+
/// and falls back to the default MinGW-w64 arch prefixes.
447+
fn get_mingw_dlltool(arch: &str) -> Result<Command> {
448+
if let Ok(user_dlltool) = env::var("PYO3_MINGW_DLLTOOL") {
449+
Ok(Command::new(&user_dlltool))
450+
} else {
451+
let prefix_dlltool = match arch {
452+
// 64-bit MinGW-w64 (aka `x86_64-pc-windows-gnu`)
453+
"x86_64" => Ok(DLLTOOL_GNU),
454+
// 32-bit MinGW-w64 (aka `i686-pc-windows-gnu`)
455+
"x86" => Ok(DLLTOOL_GNU_32),
456+
// AArch64?
457+
_ => {
458+
let msg = format!("Unsupported MinGW target arch '{}'", arch);
459+
Err(Error::new(ErrorKind::Other, msg))
460+
}
461+
}?;
462+
463+
Ok(Command::new(prefix_dlltool))
464+
}
465+
}
466+
442467
/// Finds the `zig` executable (when built by ``maturin --zig`).
443468
///
444469
/// Examines the `ZIG_COMMAND` environment variable

0 commit comments

Comments
 (0)