From 25d7f94e8ea5599373a5e46a96538b6387e7687c Mon Sep 17 00:00:00 2001 From: Nils Husung Date: Fri, 26 Apr 2024 19:45:59 +0200 Subject: [PATCH] Fix Windows i686 build --- bindings/python/build/ffi.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/bindings/python/build/ffi.py b/bindings/python/build/ffi.py index 1af8708..fe369a5 100644 --- a/bindings/python/build/ffi.py +++ b/bindings/python/build/ffi.py @@ -1,6 +1,7 @@ """This script is run form setup.py (in the project root)""" import os +import platform import sys from enum import Enum from pathlib import Path @@ -69,6 +70,7 @@ class LinkMode(Enum): @staticmethod def from_env() -> "LinkMode": + """Read from the OXIDD_PYFFI_LINK_MODE environment variable""" if container_build: return LinkMode.STATIC @@ -90,6 +92,12 @@ def from_env() -> "LinkMode": "`static`, `shared-system`, and `shared-dev`.", ) + def crate_type(self) -> str: + """Associated Rust crate type""" + if self == LinkMode.STATIC: + return "staticlib" + return "cdylib" + build_mode = LinkMode.from_env() @@ -101,7 +109,21 @@ def from_env() -> "LinkMode": if build_mode != LinkMode.SHARED_SYSTEM: cargo_bin = which("cargo") print("building crates/oxidd-ffi ...") - run(cargo_bin, "build", f"--profile={profile}", "--package=oxidd-ffi") + + # Fix win32 build on win64 + cargo_additional_args = [] + if platform.system() == "Windows" and sys.maxsize <= 0x1_0000_0000: + cargo_additional_args = ["--target=i686-pc-windows-msvc"] + lib_dir = target_dir / "i686-pc-windows-msvc" / profile + + # Use --crate-type=... to avoid missing linker errors when cross-compiling + run( + cargo_bin, + "rustc", + f"--profile={profile}", + "--package=oxidd-ffi", + f"--crate-type={build_mode.crate_type()}", + ) print("running cbindgen ...") run( @@ -113,9 +135,8 @@ def from_env() -> "LinkMode": def read_cdefs(header: Path) -> str: - """ - Remove C macros and include directives from the include header since CFFI cannot - deal with them. + """Remove C macros and include directives from the include header since CFFI + cannot deal with them. """ res = ""