I'm using Poetry v1.4.1 with Cython 3.0.5. My package contains a .pyx
file that I wish to compile. If I'm using pyximport
instead of pre-compiling, importing takes a long time. However, if I'm building (with the code below) using poetry build
, instead of a version- & platform-agnostic wheel file my_package-1.0.0-py3-none-any.whl
, I get the very specific wheel my_package-1.0.0-cp39-cp39-manylinux_2_31_x86_64.whl
.
Is there a way to build the wheel that's fully-agnostic? Maybe cythonize/compile C files at the time of the wheel's installation somehow?
Thanks!
Code:
pyproject.toml
(partial):
...
[tool.poetry.build]
generate-setup-file = false
script = 'build.py'
[build-system]
requires = ["poetry-core", "Cython", "numpy"]
build-backend = "poetry.core.masonry.api"
build.py
:
import os
import shutil
from distutils.core import Distribution
import warnings
if os.environ.get("NO_BUILD", False):
exit(0)
try:
from Cython.Build import build_ext, cythonize
import numpy
ext_modules = cythonize("my_package/**/*.pyx", output_dir="build", include_path=["dmcommon"],
build_dir="build", aliases={'NUMPY': numpy.get_include()})
dist = Distribution({"ext_modules": ext_modules})
cmd = build_ext(dist)
cmd.ensure_finalized()
cmd.run()
# Move compiled near origin files.
for output in cmd.get_outputs():
relative_extension = os.path.relpath(output, cmd.build_lib)
relative_output_folder = os.path.dirname(relative_extension)
os.makedirs(relative_output_folder, exist_ok=True)
shutil.copyfile(output, relative_extension)
except Exception as ex:
warnings.warn(f"Failed to build Cython extensions: {ex}")