Skip to content

Commit 0b71425

Browse files
committed
Fix wheel builds to bind pybind11
1 parent d7bcee5 commit 0b71425

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

_build_support/cmake_extension.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ def _run(cmd):
5959
subprocess.run(cmd, check=True)
6060

6161

62+
def _remove_stale_runtime_files(lib_dir: Path) -> None:
63+
if not lib_dir.is_dir():
64+
return
65+
66+
for pattern in ("*.so", "*.pyd", "*.dll", "*.dylib", "*.lib", "*.exp"):
67+
for candidate in lib_dir.glob(pattern):
68+
if candidate.is_file():
69+
candidate.unlink()
70+
71+
6272
class BuildExtension(build_ext):
6373
def build_extension(self, ext: setuptools.extension.Extension):
6474
del ext
@@ -67,9 +77,11 @@ def build_extension(self, ext: setuptools.extension.Extension):
6777
build_lib = Path(self.build_lib).resolve()
6878
install_dir = build_lib / "wfloat"
6979
source_dir = _get_sherpa_onnx_source_dir()
80+
python_root_dir = Path(sys.prefix).resolve()
7081

7182
build_temp.mkdir(parents=True, exist_ok=True)
7283
build_lib.mkdir(parents=True, exist_ok=True)
84+
_remove_stale_runtime_files(install_dir / "lib")
7385

7486
user_cmake_args = shlex.split(os.environ.get("SHERPA_ONNX_CMAKE_ARGS", ""))
7587
default_cmake_args = [
@@ -87,10 +99,25 @@ def build_extension(self, ext: setuptools.extension.Extension):
8799
"-DSHERPA_ONNX_ENABLE_TTS=ON",
88100
"-DSHERPA_ONNX_ENABLE_BINARY=OFF",
89101
"-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF",
102+
"-DPYBIND11_FINDPYTHON=ON",
90103
]
91104

92105
if not any(arg.startswith("-DPYTHON_EXECUTABLE=") for arg in user_cmake_args):
93106
default_cmake_args.append(f"-DPYTHON_EXECUTABLE={sys.executable}")
107+
if not any(arg.startswith("-DPython_EXECUTABLE=") for arg in user_cmake_args):
108+
default_cmake_args.append(f"-DPython_EXECUTABLE={sys.executable}")
109+
if not any(arg.startswith("-DPython3_EXECUTABLE=") for arg in user_cmake_args):
110+
default_cmake_args.append(f"-DPython3_EXECUTABLE={sys.executable}")
111+
if not any(arg.startswith("-DPython_ROOT_DIR=") for arg in user_cmake_args):
112+
default_cmake_args.append(f"-DPython_ROOT_DIR={python_root_dir}")
113+
if not any(arg.startswith("-DPython3_ROOT_DIR=") for arg in user_cmake_args):
114+
default_cmake_args.append(f"-DPython3_ROOT_DIR={python_root_dir}")
115+
if platform.system() == "Darwin" and not any(
116+
arg.startswith("-DCMAKE_OSX_ARCHITECTURES=") for arg in user_cmake_args
117+
):
118+
machine = platform.machine().lower()
119+
if machine in ("arm64", "x86_64"):
120+
default_cmake_args.append(f"-DCMAKE_OSX_ARCHITECTURES={machine}")
94121

95122
configure_cmd = [
96123
"cmake",

0 commit comments

Comments
 (0)