93 lines
3.5 KiB
Python
93 lines
3.5 KiB
Python
import os
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from setuptools import Extension, setup
|
|
from setuptools.command.build_ext import build_ext
|
|
|
|
# Convert distutils Windows platform specifiers to CMake -A arguments
|
|
PLAT_TO_CMAKE = {
|
|
"win32": "Win32",
|
|
"win-amd64": "x64",
|
|
"win-arm32": "ARM",
|
|
"win-arm64": "ARM64",
|
|
}
|
|
|
|
|
|
# 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 get_cmake_args(self, ext: CMakeExtension):
|
|
# 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) # type: ignore[no-untyped-call]
|
|
extdir = ext_fullpath.parent.resolve()
|
|
|
|
cmake_args = [
|
|
f"-DSTARTERPP_ENABLE_TEST=OFF",
|
|
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}{os.sep}",
|
|
f"-DCMAKE_BUILD_TYPE=Release", # not used on MSVC, but no harm
|
|
]
|
|
|
|
if "CMAKE_ARGS" in os.environ:
|
|
cmake_args += [item for item in os.environ["CMAKE_ARGS"].split(" ") if item]
|
|
|
|
if sys.platform.startswith("darwin"):
|
|
# Cross-compile support for macOS - respect ARCHFLAGS if set
|
|
archs = re.findall(r"-arch (\S+)", os.environ.get("ARCHFLAGS", ""))
|
|
if archs:
|
|
cmake_args += ["-DCMAKE_OSX_ARCHITECTURES={}".format(";".join(archs))]
|
|
|
|
return cmake_args
|
|
|
|
def get_build_args(self):
|
|
build_args = []
|
|
|
|
# Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level
|
|
# across all generators.
|
|
if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
|
|
# self.parallel is a Python 3 only way to set parallel jobs by hand
|
|
# using -j in the build_ext call, not supported by pip or PyPA-build.
|
|
if hasattr(self, "parallel") and self.parallel:
|
|
# CMake 3.12+ only.
|
|
build_args += [f"-j{self.parallel}"]
|
|
return build_args
|
|
|
|
def build_extension(self, ext: CMakeExtension) -> None:
|
|
build_temp = Path(self.build_temp) / ext.name
|
|
if not build_temp.exists():
|
|
build_temp.mkdir(parents=True)
|
|
|
|
subprocess.run(
|
|
["cmake", ext.sourcedir] + self.get_cmake_args(ext), cwd=build_temp, check=True
|
|
)
|
|
subprocess.run(
|
|
["cmake", "--build", "."] + self.get_build_args(), cwd=build_temp, check=True
|
|
)
|
|
|
|
|
|
# The information here can also be placed in setup.cfg - better separation of
|
|
# logic and declaration, and simpler if you include description/version in a file.
|
|
setup(
|
|
name="starterpp",
|
|
version="1.0.0",
|
|
author="Break Yang",
|
|
author_email="breakds@gmail.com",
|
|
description="An example nix powered pybind11 starter skeleton",
|
|
long_description="An example nix powered pybind11 starter skeleton",
|
|
# Here ``sourcedir`` is supposed to contain the top-level CMakeLists.txt. In
|
|
# this case since the CMakeLists.txt is at the same directory of setup.py,
|
|
# we just specify "" as the relative source directory.
|
|
ext_modules=[CMakeExtension("starterpp", sourcedir="")],
|
|
cmdclass={"build_ext": CMakeBuild},
|
|
zip_safe=False,
|
|
python_requires=">=3.8",
|
|
)
|