diff --git a/.github/workflows/pip.yml b/.github/workflows/pip.yml index f4219ae1b0f6..f118350f6953 100644 --- a/.github/workflows/pip.yml +++ b/.github/workflows/pip.yml @@ -139,6 +139,7 @@ jobs: CMAKE_GENERATOR=Ninja CMAKE_PREFIX_PATH='${{ github.workspace }}\opt' SETUPTOOLS_SCM_OVERRIDES_FOR_HALIDE='{local_scheme="no-local-version"}' + CIBW_REPAIR_WHEEL_COMMAND_WINDOWS: delvewheel repair --ignore-existing -w {dest_dir} {wheel} CIBW_BEFORE_TEST_LINUX: pip install cmake ninja CIBW_TEST_COMMAND: > cmake -G Ninja -S {project}/python_bindings/apps -B build -DCMAKE_BUILD_TYPE=Release && diff --git a/python_bindings/src/halide/__init__.py b/python_bindings/src/halide/__init__.py index 4bbe643b9924..b0438e6bff68 100644 --- a/python_bindings/src/halide/__init__.py +++ b/python_bindings/src/halide/__init__.py @@ -1,16 +1,35 @@ -def patch_dll_dirs(): +def _preload_bundled_halide_library(): + # Force-load our own copy of the Halide runtime library by absolute path before + # importing halide_, so that halide_'s implicit load of the same library (by + # soname/module name) resolves to this already-loaded instance instead of + # searching LD_LIBRARY_PATH / PATH / the default dynamic linker paths, where a + # foreign, incompatible libHalide could shadow ours. + # See: https://github.com/halide/Halide/issues/8866 + import ctypes import os - if hasattr(os, "add_dll_directory"): - from pathlib import Path + from pathlib import Path - bin_dir = Path(__file__).parent / "bin" - if bin_dir.exists(): - os.add_dll_directory(str(bin_dir)) + root = Path(__file__).parent + bin_dir = root / "bin" + if hasattr(os, "add_dll_directory") and bin_dir.is_dir(): + os.add_dll_directory(str(bin_dir)) -patch_dll_dirs() -del patch_dll_dirs + for relpath in ( + "bin/Halide.dll", + "lib/libHalide.dylib", + "lib64/libHalide.so", + "lib/libHalide.so", + ): + lib_path = root / relpath + if lib_path.exists(): + ctypes.CDLL(str(lib_path)) + return + + +_preload_bundled_halide_library() +del _preload_bundled_halide_library from .halide_ import * # noqa: E402, F403