diff --git a/scripts/build_python_instance_base.py b/scripts/build_python_instance_base.py index c7091ed3..7f7fcd1f 100644 --- a/scripts/build_python_instance_base.py +++ b/scripts/build_python_instance_base.py @@ -449,6 +449,10 @@ def build_dependency_module(self, module_source_dir: Path) -> None: dependency_build.itk_module_deps = None # Prevent recursion dependency_build.run() + def wheel_repair_settings(self) -> dict[str, str]: + """Return the platform's wheel repair options, which change the wheels.""" + return {} + def build_inputs(self) -> dict[str, str]: """Return the ITK commit, package version, and fingerprint of this build.""" cfg = self.config.to_environment() @@ -461,13 +465,7 @@ def build_inputs(self) -> dict[str, str]: settings.update({f"{group}:{k}": str(v) for k, v in definitions.items()}) for key in ("USE_CCACHE", "MANYLINUX_VERSION", "CONTAINER_SOURCE"): settings[key] = cfg.get(key, "") - # The platform builders' wheel repair options. - for option in ( - "windows_extra_lib_paths", - "windows_exclude_libs", - "exclude_libs", - ): - settings[option] = str(getattr(self, option, "")) + settings.update(self.wheel_repair_settings()) git = str(self.config.git_executable) module_state = None if self.module_source_dir is not None: diff --git a/scripts/linux_build_python_instance.py b/scripts/linux_build_python_instance.py index f26a12b5..6e047175 100644 --- a/scripts/linux_build_python_instance.py +++ b/scripts/linux_build_python_instance.py @@ -7,6 +7,7 @@ from posix_build_python_instance import PosixBuildPythonInstance from wheel_builder_utils import ( remove_tree, + split_semicolon_list, ) @@ -17,14 +18,15 @@ class LinuxBuildPythonInstance(PosixBuildPythonInstance): Linux-specific compiler/target triple configuration. """ - #: ``--exclude-libs``: libraries ``auditwheel`` must not bundle. - exclude_libs: list[str] | str = "" - def __init__(self, *, exclude_libs: list[str] | str = "", **kwargs: Any) -> None: """Take ``--exclude-libs``; the rest are the base's.""" super().__init__(**kwargs) self.exclude_libs = exclude_libs + def wheel_repair_settings(self) -> dict[str, str]: + """Return ``--exclude-libs``, which changes what auditwheel bundles.""" + return {"exclude_libs": ";".join(split_semicolon_list(self.exclude_libs))} + def builds_manylinux(self) -> bool: """True when the wheels are repaired and tagged for manylinux.""" return bool(self.target.manylinux_version) @@ -166,12 +168,8 @@ def fixup_wheel(self, filepath: str, remote_module_wheel: bool = False) -> None: "--plat", self.target.wheel_plat_tag, ] - excludes = self.exclude_libs - if isinstance(excludes, list): - excludes = ";".join(excludes) - for lib in excludes.split(";"): - if lib: - cmd += ["--exclude", lib] + for lib in split_semicolon_list(self.exclude_libs): + cmd += ["--exclude", lib] cmd += [str(filepath), "-w", str(output_dir)] # Provide LD_LIBRARY_PATH for oneTBB and common system paths extra_lib = str(self._tbb_prefix / "lib") diff --git a/scripts/module_wheel_init.py b/scripts/module_wheel_init.py index 10cc078e..840f4bb4 100644 --- a/scripts/module_wheel_init.py +++ b/scripts/module_wheel_init.py @@ -17,14 +17,16 @@ from pathlib import Path ITK_INIT = "itk/__init__.py" -_CONFIG = re.compile(r"^itk/Configuration/(?P[^/]+)Config\.py$") +_MODULE_CONFIG_MEMBER = re.compile(r"^itk/Configuration/(?P[^/]+)Config\.py$") def module_init_name(wheel: Path) -> str: """Return the ``itk/__init___.py`` member ITK runs for *wheel*.""" with zipfile.ZipFile(wheel) as zf: names = zf.namelist() - modules = sorted(m["module"] for n in names if (m := _CONFIG.match(n))) + modules = sorted( + m["module"] for n in names if (m := _MODULE_CONFIG_MEMBER.match(n)) + ) if modules: return f"itk/__init_{modules[0].lower()}__.py" metadata = next(n for n in names if n.endswith(".dist-info/METADATA")) diff --git a/scripts/wheel_builder_utils.py b/scripts/wheel_builder_utils.py index 2f96f3e9..206e8d3f 100644 --- a/scripts/wheel_builder_utils.py +++ b/scripts/wheel_builder_utils.py @@ -553,3 +553,9 @@ def get_default_platform_build(default_python_version: str = "py311") -> str: if sys.platform.startswith(platform_prefix): return f"{pixi_os}-{default_python_version}" return f"unknown-{default_python_version}" + + +def split_semicolon_list(value: Sequence[str] | str) -> list[str]: + """Return the names in a ``;``-delimited option, given as a string or argparse list.""" + joined = value if isinstance(value, str) else ";".join(value) + return [name.strip() for name in joined.split(";") if name.strip()] diff --git a/scripts/windows_build_python_instance.py b/scripts/windows_build_python_instance.py index 2c8241e0..c0ba20dc 100644 --- a/scripts/windows_build_python_instance.py +++ b/scripts/windows_build_python_instance.py @@ -13,6 +13,7 @@ from cmake_argument_builder import CMakeArgumentBuilder from module_wheel_init import ITK_INIT, module_init_name, rename_wheel_member from msvc_path_trim import msvc_trim_definitions +from wheel_builder_utils import split_semicolon_list ARCHIVE_EXCLUDES = ( "*.o", @@ -92,18 +93,9 @@ def post_build_fixup(self) -> None: def _delvewheel_lib_paths(self) -> str: """Return the ``--lib-paths`` directories and oneTBB's, semicolon-delimited.""" - # append the oneTBB-prefix\\bin directory for fixing wheels built with local oneTBB - search_lib_paths = ( - [ - s - for s in str(self.windows_extra_lib_paths[0]).rstrip(";").split(";") - if s - ] - if self.windows_extra_lib_paths - else [] - ) + search_lib_paths = split_semicolon_list(self.windows_extra_lib_paths) search_lib_paths.append(str(self._tbb_prefix / "bin")) - return ";".join(map(str, search_lib_paths)) + return ";".join(search_lib_paths) def fixup_wheels(self, lib_paths: str = "") -> None: """Repair the ``itk_core`` wheels, which link oneTBB.""" @@ -177,12 +169,20 @@ def fixup_wheel( repaired_wheel.replace(output_dir / repaired_wheel.name) shutil.rmtree(staging_dir, ignore_errors=True) + def wheel_repair_settings(self) -> dict[str, str]: + """Return ``--lib-paths`` and ``--exclude-libs``, which change what delvewheel bundles.""" + return { + "windows_extra_lib_paths": ";".join( + split_semicolon_list(self.windows_extra_lib_paths) + ), + "windows_exclude_libs": ";".join( + split_semicolon_list(self.windows_exclude_libs) + ), + } + def _no_dll_arguments(self) -> list[str]: """Return ``--no-dll`` with the ``--exclude-libs`` value, if one was given.""" - excludes = self.windows_exclude_libs - if isinstance(excludes, list): - excludes = ";".join(excludes) - excludes = excludes.strip(";") + excludes = ";".join(split_semicolon_list(self.windows_exclude_libs)) return ["--no-dll", excludes] if excludes else [] def build_tarball(self) -> None: diff --git a/tests/conftest.py b/tests/conftest.py index dce35d76..74579aed 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -85,23 +85,27 @@ def macos_builder(tmp_path: Path) -> MacOSBuildPythonInstance: @pytest.fixture def linux_builder(tmp_path: Path) -> LinuxBuildPythonInstance: """A manylinux x86_64 builder.""" - return _make_builder( + builder = _make_builder( LinuxBuildPythonInstance, tmp_path, "manylinux228-py311", TargetPlatform(OS.LINUX, Arch.X86_64, "_2_28"), ) + builder.exclude_libs = "" + return builder @pytest.fixture def linux_aarch64_builder(tmp_path: Path) -> LinuxBuildPythonInstance: """A manylinux aarch64 builder, covering the cross-build triple.""" - return _make_builder( + builder = _make_builder( LinuxBuildPythonInstance, tmp_path, "manylinux228-py311", TargetPlatform(OS.LINUX, Arch.AARCH64, "_2_28"), ) + builder.exclude_libs = "" + return builder @pytest.fixture diff --git a/tests/test_platform_builders.py b/tests/test_platform_builders.py index 387dc977..aedc585a 100644 --- a/tests/test_platform_builders.py +++ b/tests/test_platform_builders.py @@ -605,6 +605,13 @@ def fake_fixup_wheels(lib_paths: str) -> None: assert not any(len(e) == 1 for e in entries) +def test_windows_lib_paths_given_as_a_string_keep_whole_directories( + windows_builder: WindowsBuildPythonInstance, +) -> None: + windows_builder.windows_extra_lib_paths = "C:/CUDA/bin" + assert windows_builder._delvewheel_lib_paths().split(";")[0] == "C:/CUDA/bin" + + def test_ingested_remote_modules_are_enabled_and_can_be_overridden( linux_builder: LinuxBuildPythonInstance, ) -> None: @@ -842,3 +849,21 @@ def record(cmd: list[object], *args: object, **kwargs: object) -> int: assert len(calls) == 1 assert "cmake.define.ITKPythonPackage_WHEEL_PROFILE:STRING" in calls[0] assert builder.wheel_profile.name in calls[0] + + +def test_each_platform_reports_its_own_wheel_repair_settings( + linux_builder: LinuxBuildPythonInstance, + macos_builder: MacOSBuildPythonInstance, + windows_builder: WindowsBuildPythonInstance, +) -> None: + linux_builder.exclude_libs = ["libcuda.so.1;libnvrtc.so.12"] + windows_builder.windows_extra_lib_paths = ["C:/CUDA/bin;"] + windows_builder.windows_exclude_libs = "nvcuda.dll" + assert macos_builder.wheel_repair_settings() == {} + assert linux_builder.wheel_repair_settings() == { + "exclude_libs": "libcuda.so.1;libnvrtc.so.12" + } + assert windows_builder.wheel_repair_settings() == { + "windows_extra_lib_paths": "C:/CUDA/bin", + "windows_exclude_libs": "nvcuda.dll", + } diff --git a/tests/test_wheel_builder_utils.py b/tests/test_wheel_builder_utils.py index eee93a6f..51048305 100644 --- a/tests/test_wheel_builder_utils.py +++ b/tests/test_wheel_builder_utils.py @@ -19,6 +19,22 @@ def _completed( ) +@pytest.mark.parametrize( + ("value", "expected"), + [ + ("", []), + ([], []), + ("nvcuda.dll", ["nvcuda.dll"]), + ("C:/CUDA/bin", ["C:/CUDA/bin"]), + (["a.dll;b.dll;"], ["a.dll", "b.dll"]), + (["a.dll", "b.dll"], ["a.dll", "b.dll"]), + ("a.dll; ;b.dll", ["a.dll", "b.dll"]), + ], +) +def test_split_semicolon_list(value: Sequence[str] | str, expected: list[str]) -> None: + assert wheel_builder_utils.split_semicolon_list(value) == expected + + def test_which_required_raises_for_a_missing_executable() -> None: """A missing tool must not become the string 'None' in an argv list.""" with pytest.raises(RuntimeError, match="MISSING"):