diegoferigo / cmake-build-extension

Setuptools extension to build and package CMake projects
https://pypi.org/project/cmake-build-extension/
MIT License
67 stars 16 forks source link

Fix importing build extension for Python >= 3.8 in Windows #12

Closed diegoferigo closed 3 years ago

diegoferigo commented 3 years ago

Fixes #8

Apparently the problem is not a bug but a change of behavior in Windows. Refer to add_dll_directory for more details.

Differently from many, I implemented this fix using a context manager that automatically closes the dll cookies. Since this functionality is necessary to properly import SWIG bindings in the affected platform (I suspect that also pybind11 is affected), I added the context manager to cmake-build-extension so that it can be used downstream. The only change is that, if the context manager is used e.g. in the __init__.py file, the downstream package has to mark cmake-build-extension as install_require and no longer as only setup_require (or, in a more modern setup, in the requires field of pyproject.toml).

from cmake_build_extension import build_extension_env

with build_extension_env():
    # Assuming that 'bindings' is the name of the SWIG / pybind11 module
    from . import bindings

Related resources from which this PR was inspired: