switch to cmake

I'm sick of the way the simple extension builder in setup.py doesn't
understand header dependencies.
This commit is contained in:
Jeff Epler 2025-03-17 09:25:45 -05:00
parent b19b6c7cb0
commit 8d2e9c079b
6 changed files with 83 additions and 18 deletions

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "pybind11"]
path = pybind11
url = https://github.com/pybind/pybind11.git

12
CMakeLists.txt Normal file
View file

@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.4...3.18)
project("_piomatter")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION FALSE)
add_subdirectory(pybind11)
pybind11_add_module("_piomatter"
src/pymain.cpp src/piolib/pio_rp1.c src/piolib/piolib.c)
target_include_directories("_piomatter" PRIVATE
${CMAKE_CURRENT_LIST_DIR}/src/include
${CMAKE_CURRENT_LIST_DIR}/src/piolib/include)
set_property(TARGET "_piomatter" PROPERTY
CXX_STANDARD 20)

5
MANIFEST.in Normal file
View file

@ -0,0 +1,5 @@
include README.md LICENSE pybind11/LICENSE
graft pybind11/include
graft pybind11/tools
graft src
global-include CMakeLists.txt *.cmake

1
pybind11 Submodule

@ -0,0 +1 @@
Subproject commit a2e59f0e7065404b44dfe92a28aca47ba1378dc4

View file

@ -1,7 +1,7 @@
[build-system]
requires = [
"cmake>=3.12",
"setuptools>=42",
"pybind11>=2.10.0",
"setuptools_scm[toml]>=6.2",
]
build-backend = "setuptools.build_meta"

View file

@ -1,36 +1,80 @@
# Available at setup time due to pyproject.toml
from pybind11.setup_helpers import Pybind11Extension, build_ext
from setuptools import setup
import multiprocessing
import os
import subprocess
import sys
from pathlib import Path
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
from setuptools_scm import get_version
__version__ = get_version()
# A CMakeExtension needs a sourcedir instead of a file list.
# The name must be the _single_ output extension from the CMake build.
# If you need multiple extensions, see scikit-build.
class CMakeExtension(Extension):
def __init__(self, name: str, sourcedir: str = "") -> None:
super().__init__(name, sources=[])
self.sourcedir = os.fspath(Path(sourcedir).resolve())
class CMakeBuild(build_ext):
def build_extension(self, ext: CMakeExtension) -> None:
# Must be in this form due to bug in .resolve() only fixed in Python 3.10+
ext_fullpath = Path.cwd() / self.get_ext_fullpath(ext.name)
extdir = ext_fullpath.parent.resolve()
# Using this requires trailing slash for auto-detection & inclusion of
# auxiliary "native" libs
debug = int(os.environ.get("DEBUG", 0)) if self.debug is None else self.debug
cfg = "Debug" if debug else "Release"
# Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
# EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code
# from Python.
cmake_args = [
"-GUnix Makefiles",
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}{os.sep}",
f"-DPYTHON_EXECUTABLE={sys.executable}",
f"-DCMAKE_BUILD_TYPE={cfg}", # not used on MSVC, but no harm
]
build_args = []
# Adding CMake arguments set as environment variable
# (needed e.g. to build for ARM OSx on conda-forge)
if "CMAKE_ARGS" in os.environ:
cmake_args += [item for item in os.environ["CMAKE_ARGS"].split(" ") if item]
build_args += [f"-j{multiprocessing.cpu_count()}"]
build_temp = Path(self.build_temp) / ext.name
if not build_temp.exists():
build_temp.mkdir(parents=True)
subprocess.run(
["cmake", ext.sourcedir, *cmake_args], cwd=build_temp, check=True
)
subprocess.run(
["make", *build_args], cwd=build_temp, check=True
)
# The main interface is through Pybind11Extension.
# * You can add cxx_std=11/14/17, and then build_ext can be removed.
# * You can set include_pybind11=false to add the include directory yourself,
# say from a submodule.
ext_modules = [
Pybind11Extension("adafruit_blinka_raspberry_pi5_piomatter._piomatter",
["src/pymain.cpp", "src/piolib/piolib.c", "src/piolib/pio_rp1.c"],
define_macros = [('VERSION_INFO', __version__)],
include_dirs = ['./src/include', './src/piolib/include'],
cxx_std=20,
# use this setting when debugging
extra_compile_args = ["-g3", "-Og"],
),
]
setup(
name="Adafruit-Blinka-Raspberry-Pi5-Piomatter",
version=__version__,
url="https://github.com/adafruit/Adafruit_Blinka_Raspberry_Pi5_Piomatter",
description="HUB75 matrix driver for Raspberry Pi 5 using PIO",
long_description="A pio-based driver",
ext_modules=ext_modules,
# Currently, build_ext only provides an optional "highest supported C++
# level" feature, but in the future it may provide more features.
cmdclass={"build_ext": build_ext},
ext_modules=[CMakeExtension("adafruit_blinka_raspberry_pi5_piomatter._piomatter")],
cmdclass={"build_ext": CMakeBuild},
zip_safe=False,
python_requires=">=3.11",
packages=['adafruit_blinka_raspberry_pi5_piomatter'],