feat: set up delvewheel as the default repair wheel command for Windows (#2831)

* Add a default repair wheel command for Windows

* Install `delvewheel` into Windows build tools

* Add delvewheel to constraints file

* Regenerate constraints

* Docs

* Suggest how to disable Windows wheel repair

* Add tests

* Run `delvewheel` with verbose mode as default

* Add note about telling delvewheel where to look

Suggested-by: Nils <nilsnolde@proton.me>

* Partially revert 81374bb8fc43acefed76cb13d761a9e6cf6afa58

* Add `pip` and `uv` cases for build installations

* Ignore `python-native.dll` for GraalPy

* Ignore MSVC DLLs, try Windows amd64 graalpy happy

* Fix last remaining Windows test failure (famous last words?)

* Update constraints

* Drop workaround unneeded with delvewheel v1.12.1

* Update docs/options.md

Co-authored-by: Aohan Dang <adang1345@gmail.com>

* Drop `test_delvewheel_default_on_windows`

* Partially revert "Update constraints"

This partially reverts commit 2dc4c0f4951858959e213463d90431a5ca03b96f.

* Add a test case when repair command is `""`

* Add back test that checks delvewheel run/disable

* Add a C project with a missing DLL

* Add tests for `missing_dll_project`

* Partially undo virtualenv/python-discovery update

* Fix test

* Discard changes to cibuildwheel/resources/constraints-pyodide312.txt

* Discard changes to cibuildwheel/resources/constraints-pyodide314.txt

* Fix bad merge of constraints

* Discard changes to cibuildwheel/resources/constraints-pyodide313.txt

* Manually add more GraalPy pip markers

* Dependency parsing was too naïve

* Maybe a better way to invoke the compiler on Windows

* Can we get away without a DLL?

---------

Co-authored-by: Aohan Dang <adang1345@gmail.com>
This commit is contained in:
Agriya Khetarpal
2026-05-13 18:18:30 -04:00
committed by GitHub
co-authored by Aohan Dang
parent 6d9f4c9545
commit 78869d0cf6
19 changed files with 273 additions and 29 deletions
+8 -2
View File
@@ -10,7 +10,7 @@ from cibuildwheel.util import resources
from . import test_projects, utils
VERSION_REGEX = r"([\w-]+)==([^\s]+)"
VERSION_REGEX = r"([\w-]+)==([^;\s]+)"
CHECK_VERSIONS_SCRIPT = """\
'''
@@ -73,7 +73,13 @@ def test_check_versions_script(
def get_versions_from_constraint_file(constraint_file: Path) -> dict[str, str]:
constraint_file_text = constraint_file.read_text(encoding="utf-8")
return dict(re.findall(VERSION_REGEX, constraint_file_text))
# Use the first occurrence of each package name. The marker-bearing lines
# (such as the GraalPy-specific pip pin) come after the default pin, so the
# first match is the one that applies to the majority of builds.
result: dict[str, str] = {}
for name, version in re.findall(VERSION_REGEX, constraint_file_text):
result.setdefault(name, version)
return result
@pytest.mark.parametrize("python_version", ["3.9", "3.13"])
+2 -2
View File
@@ -1,5 +1,5 @@
from .base import TestProject
from .meson import new_meson_project
from .setuptools import new_c_project
from .setuptools import new_c_project, new_c_project_with_missing_dll
__all__ = ("TestProject", "new_c_project", "new_meson_project")
__all__ = ("TestProject", "new_c_project", "new_c_project_with_missing_dll", "new_meson_project")
+91
View File
@@ -3,6 +3,79 @@ import jinja2
from .base import TestProject
from .c import SPAM_C_TEMPLATE
_SPAM_C_WITH_MISSING_DLL = """\
#include <Python.h>
int cibwtest_add(int a, int b);
static PyObject *spam_filter(PyObject *self, PyObject *args)
{
const char *content;
int sts;
if (!PyArg_ParseTuple(args, "s", &content))
return NULL;
sts = strcmp(content, "spam") != 0;
cibwtest_add(0, 0);
return PyLong_FromLong(sts);
}
static PyMethodDef module_methods[] = {
{"filter", (PyCFunction)spam_filter, METH_VARARGS, "Execute a shell command."},
{NULL}
};
PyMODINIT_FUNC PyInit_spam(void)
{
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT, "spam", "Example module", -1, module_methods,
};
return PyModule_Create(&moduledef);
}
"""
_SETUP_PY_WITH_MISSING_DLL = """\
import subprocess
from pathlib import Path
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext as _orig_build_ext
here = Path(__file__).parent
dll_dir = here / "_cibwtest_dll"
class build_ext(_orig_build_ext):
def build_extensions(self):
if not self.compiler.initialized:
self.compiler.initialize()
dll_dir.mkdir(exist_ok=True)
machine = {
"win-arm64": "ARM64",
"win-amd64": "X64",
"win32": "X86",
}.get(self.plat_name, "X64")
def_file = dll_dir / "cibwtest.def"
def_file.write_text("EXPORTS\\n cibwtest_add\\n")
subprocess.check_call([
self.compiler.lib,
f"/def:{def_file}",
"/name:cibwtest.dll",
f"/out:{dll_dir / 'cibwtest.lib'}",
f"/machine:{machine}",
])
super().build_extensions()
setup(
ext_modules=[Extension(
"spam",
sources=["spam.c"],
libraries=["cibwtest"],
library_dirs=[str(dll_dir)],
)],
cmdclass={"build_ext": build_ext},
)
"""
SETUP_PY_TEMPLATE = r"""
import os
import sys
@@ -38,6 +111,24 @@ version = 0.1.0
"""
def new_c_project_with_missing_dll() -> TestProject:
"""
A Windows-only test project whose extension links against cibwtest.dll, a DLL
built into a subdirectory that is not on PATH. delvewheel will find the import
in the PE table but cannot locate the file, so repair fails by default.
Setting repair-wheel-command to "" disables repair and lets the build succeed.
"""
project = TestProject()
project.files.update(
{
"spam.c": _SPAM_C_WITH_MISSING_DLL,
"setup.py": _SETUP_PY_WITH_MISSING_DLL,
"setup.cfg": jinja2.Template(SETUP_CFG_TEMPLATE),
}
)
return project
def new_c_project(
*,
spam_c_top_level_add: str = "",
+70
View File
@@ -8,6 +8,76 @@ import pytest
from . import test_projects, utils
basic_project = test_projects.new_c_project()
missing_dll_project = test_projects.new_c_project_with_missing_dll()
def test_delvewheel_runs_by_default(tmp_path: Path, capfd: pytest.CaptureFixture[str]) -> None:
if utils.get_platform() != "windows":
pytest.skip("This test is only relevant to Windows")
skip_if_no_msvc()
project_dir = tmp_path / "project"
basic_project.generate(project_dir)
utils.cibuildwheel_run(project_dir, add_args=["--archs", "native"], single_python=True)
captured = capfd.readouterr()
assert "Repairing wheel" in captured.out
def test_delvewheel_disabled_by_empty_repair_command(
tmp_path: Path, capfd: pytest.CaptureFixture[str]
) -> None:
if utils.get_platform() != "windows":
pytest.skip("This test is only relevant to Windows")
skip_if_no_msvc()
project_dir = tmp_path / "project"
basic_project.generate(project_dir)
utils.cibuildwheel_run(
project_dir,
add_args=["--archs", "native"],
add_env={"CIBW_REPAIR_WHEEL_COMMAND_WINDOWS": ""},
single_python=True,
)
captured = capfd.readouterr()
assert "Repairing wheel" not in captured.out
def test_delvewheel_fails_when_dll_is_missing(tmp_path: Path) -> None:
if utils.get_platform() != "windows":
pytest.skip("This test is only relevant to Windows")
skip_if_no_msvc()
project_dir = tmp_path / "project"
missing_dll_project.generate(project_dir)
with pytest.raises(subprocess.CalledProcessError):
utils.cibuildwheel_run(project_dir, add_args=["--archs", "native"], single_python=True)
def test_delvewheel_disabled_allows_build_with_missing_dll(tmp_path: Path) -> None:
if utils.get_platform() != "windows":
pytest.skip("This test is only relevant to Windows")
skip_if_no_msvc()
project_dir = tmp_path / "project"
missing_dll_project.generate(project_dir)
actual_wheels = utils.cibuildwheel_run(
project_dir,
add_args=["--archs", "native"],
add_env={"CIBW_REPAIR_WHEEL_COMMAND_WINDOWS": ""},
single_python=True,
)
assert len(actual_wheels) == 1
def skip_if_no_msvc(arm64: bool = False) -> None: