Skip to content

Commit 762e2f5

Browse files
committed
fix python abi library name for mingw
usually mingw version of python are shipped with `lib` prefix, so we need to add it for proper linking
1 parent d01e618 commit 762e2f5

4 files changed

Lines changed: 53 additions & 27 deletions

File tree

noxfile.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1363,16 +1363,22 @@ def _check_raw_dylib_macro(session: nox.Session):
13631363
max_minor = int(max_version.split(".")[1])
13641364

13651365
# Build the set of DLL names that default_lib_name_windows can produce
1366-
expected_dlls = {"python3", "python3_d"}
1366+
expected_dlls = {"python3", "python3_d", "libpython3", "libpython3_d"}
13671367
for minor in range(min_minor, max_minor + 1):
13681368
expected_dlls.add(f"python3{minor}")
13691369
expected_dlls.add(f"python3{minor}_d")
1370+
expected_dlls.add(f"libpython3{minor}")
1371+
expected_dlls.add(f"libpython3{minor}_d")
13701372
if minor >= 13:
13711373
expected_dlls.add(f"python3{minor}t")
13721374
expected_dlls.add(f"python3{minor}t_d")
1375+
expected_dlls.add(f"libpython3{minor}t")
1376+
expected_dlls.add(f"libpython3{minor}t_d")
13731377
if minor >= 15:
13741378
expected_dlls.add("python3t")
13751379
expected_dlls.add("python3t_d")
1380+
expected_dlls.add("libpython3t")
1381+
expected_dlls.add("libpython3t_d")
13761382

13771383
# PyPy DLL names (libpypy3.X-c.dll)
13781384
pypy_min, pypy_max = _parse_supported_interpreter_version("pypy")

pyo3-build-config/src/impl_.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,6 +2314,9 @@ fn default_lib_name_for_target(abi: PythonAbi, target: &Triple) -> String {
23142314
}
23152315

23162316
fn default_lib_name_windows(abi: PythonAbi, mingw: bool, debug: bool) -> Result<String> {
2317+
// set `lib` prefix for mingw, as its python abi library is shipped prefixed
2318+
let lib_prefix = if mingw { "lib" } else { "" };
2319+
23172320
if abi.implementation.is_pypy() {
23182321
// PyPy on Windows ships `libpypy3.X-c.dll` (e.g. `libpypy3.11-c.dll`),
23192322
// not CPython's `pythonXY.dll`. With raw-dylib linking we need the real
@@ -2326,8 +2329,8 @@ fn default_lib_name_windows(abi: PythonAbi, mingw: bool, debug: bool) -> Result<
23262329
// CPython bug: linking against python3_d.dll raises error
23272330
// https://github.com/python/cpython/issues/101614
23282331
Ok(format!(
2329-
"python{}{}_d",
2330-
abi.version.major, abi.version.minor
2332+
"{}python{}{}_d",
2333+
lib_prefix, abi.version.major, abi.version.minor
23312334
))
23322335
} else if abi.kind == PythonAbiKind::Stable(StableAbi::Abi3)
23332336
|| abi.kind == PythonAbiKind::Stable(StableAbi::Abi3t)
@@ -2340,34 +2343,43 @@ fn default_lib_name_windows(abi: PythonAbi, mingw: bool, debug: bool) -> Result<
23402343
if abi.kind == PythonAbiKind::Stable(StableAbi::Abi3t) {
23412344
lib_name = lib_name.replace("python3", "python3t");
23422345
}
2343-
Ok(lib_name)
2346+
Ok(format!("{}{}", lib_prefix, lib_name))
23442347
} else if mingw {
23452348
ensure!(
23462349
!abi.kind.is_free_threaded(),
23472350
"MinGW free-threaded builds are not currently tested or supported"
23482351
);
23492352
// https://packages.msys2.org/base/mingw-w64-python
2350-
Ok(format!("python{}.{}", abi.version.major, abi.version.minor))
2353+
Ok(format!(
2354+
"{}python{}.{}",
2355+
lib_prefix, abi.version.major, abi.version.minor
2356+
))
23512357
} else if abi.kind().is_free_threaded() {
23522358
#[expect(deprecated, reason = "using constant internally")]
23532359
{
23542360
ensure!(abi.version() >= PythonVersion::PY313, "Cannot compile extensions for the free-threaded build on Python versions earlier than 3.13, found {}.{}", abi.version.major, abi.version.minor);
23552361
}
23562362
if debug {
23572363
Ok(format!(
2358-
"python{}{}t_d",
2359-
abi.version.major, abi.version.minor
2364+
"{}python{}{}t_d",
2365+
lib_prefix, abi.version.major, abi.version.minor
23602366
))
23612367
} else {
2362-
Ok(format!("python{}{}t", abi.version.major, abi.version.minor))
2368+
Ok(format!(
2369+
"{}python{}{}t",
2370+
lib_prefix, abi.version.major, abi.version.minor
2371+
))
23632372
}
23642373
} else if debug {
23652374
Ok(format!(
2366-
"python{}{}_d",
2367-
abi.version.major, abi.version.minor
2375+
"{}python{}{}_d",
2376+
lib_prefix, abi.version.major, abi.version.minor
23682377
))
23692378
} else {
2370-
Ok(format!("python{}{}", abi.version.major, abi.version.minor))
2379+
Ok(format!(
2380+
"{}python{}{}",
2381+
lib_prefix, abi.version.major, abi.version.minor
2382+
))
23712383
}
23722384
}
23732385

@@ -3290,7 +3302,7 @@ mod tests {
32903302
false,
32913303
)
32923304
.unwrap(),
3293-
"python3.9",
3305+
"libpython3.9",
32943306
);
32953307
assert_eq!(
32963308
super::default_lib_name_windows(
@@ -3302,7 +3314,7 @@ mod tests {
33023314
false,
33033315
)
33043316
.unwrap(),
3305-
"python3",
3317+
"libpython3",
33063318
);
33073319
assert_eq!(
33083320
super::default_lib_name_windows(

pyo3-build-config/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,21 @@ pub fn print_expected_cfgs() {
171171
"python3_d".to_string(),
172172
"python3t".to_string(),
173173
"python3t_d".to_string(),
174+
"libpython3".to_string(),
175+
"libpython3_d".to_string(),
176+
"libpython3t".to_string(),
177+
"libpython3t_d".to_string(),
174178
];
175179
for i in impl_::MINIMUM_SUPPORTED_VERSION.minor..=impl_::STABLE_ABI_MAX_MINOR + 1 {
176180
dll_names.push(format!("python3{i}"));
177181
dll_names.push(format!("python3{i}_d"));
182+
dll_names.push(format!("libpython3{i}"));
183+
dll_names.push(format!("libpython3{i}_d"));
178184
if i >= 13 {
179185
dll_names.push(format!("python3{i}t"));
180186
dll_names.push(format!("python3{i}t_d"));
187+
dll_names.push(format!("libpython3{i}t"));
188+
dll_names.push(format!("libpython3{i}t_d"));
181189
}
182190
}
183191
// PyPy DLL names (libpypy3.X-c.dll)

pyo3-ffi/src/impl_/macros.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -257,21 +257,21 @@ macro_rules! extern_libpython {
257257
($abi:literal { $($body:tt)* }) => {
258258
extern_libpython!(@impl $abi { $($body)* }
259259
// abi3
260-
"python3", "python3_d",
260+
"python3", "python3_d", "libpython3", "libpython3_d",
261261
// abi3t
262-
"python3t", "python3t_d",
262+
"python3t", "python3t_d", "libpython3t", "libpython3t_d",
263263
// Python 3.9 - 3.15
264-
"python39", "python39_d",
265-
"python310", "python310_d",
266-
"python311", "python311_d",
267-
"python312", "python312_d",
268-
"python313", "python313_d",
269-
"python314", "python314_d",
270-
"python315", "python315_d",
264+
"python39", "python39_d", "libpython39", "libpython39_d",
265+
"python310", "python310_d", "libpython310", "libpython310_d",
266+
"python311", "python311_d", "libpython311", "libpython311_d",
267+
"python312", "python312_d", "libpython312", "libpython312_d",
268+
"python313", "python313_d", "libpython313", "libpython313_d",
269+
"python314", "python314_d", "libpython314", "libpython314_d",
270+
"python315", "python315_d", "libpython315", "libpython315_d",
271271
// free-threaded builds (3.13+)
272-
"python313t", "python313t_d",
273-
"python314t", "python314t_d",
274-
"python315t", "python315t_d",
272+
"python313t", "python313t_d", "libpython313t", "libpython313t_d",
273+
"python314t", "python314t_d", "libpython314t", "libpython314t_d",
274+
"python315t", "python315t_d", "libpython315t", "libpython315t_d",
275275
// PyPy (DLL is libpypy3.X-c.dll, not pythonXY.dll)
276276
"libpypy3.11-c",
277277
);
@@ -287,9 +287,9 @@ macro_rules! extern_libpython {
287287
(@impl $abi:literal { $($body:tt)* } $($dll:literal),* $(,)?) => {
288288
$(
289289
#[cfg_attr(all(windows, target_arch = "x86", pyo3_dll = $dll),
290-
link(name = $dll, kind = "raw-dylib", import_name_type = "undecorated"))]
290+
link(name = $dll, kind = "raw-dylib", modifiers = "+verbatim", import_name_type = "undecorated"))]
291291
#[cfg_attr(all(windows, not(target_arch = "x86"), pyo3_dll = $dll),
292-
link(name = $dll, kind = "raw-dylib"))]
292+
link(name = $dll, kind = "raw-dylib", modifiers = "+verbatim"))]
293293
)*
294294
extern $abi {
295295
extern_libpython_items! { $($body)* }

0 commit comments

Comments
 (0)