diff --git a/pyproject.toml b/pyproject.toml index 7cf34d7214..456c3ce81d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [build-system] -requires = ["setuptools>=42", "wheel", "nanobind>=1.3.2"] +requires = ["setuptools>=42", "wheel", "nanobind>=1.3.2", "cmake>=3.17.0"] build-backend = "setuptools.build_meta" [project] diff --git a/setup.py b/setup.py index 8e1135fdbc..d8dbbe97fe 100644 --- a/setup.py +++ b/setup.py @@ -1,31 +1,42 @@ -from setuptools import setup, Extension -from setuptools.command.build_ext import build_ext +import os +import re +import subprocess import sys -import setuptools -import nanobind - -class get_nanobind_include(object): - """Helper class to determine the nanobind include path - The purpose of this class is to postpone importing nanobind - until it is actually installed, so that the `get_include()` - method can be invoked.""" - - def __str__(self): - import nanobind - return nanobind.include_dir() - -ext_modules = [ - Extension( - 'vttv', - ['bindings/python/tv.cc'], - include_dirs=[ - # Path to nanobind headers - get_nanobind_include(), - './' # Assuming the root of your C++ project is the current directory - ], - language='c++' - ), -] +from setuptools import setup, Extension, find_packages +from setuptools.command.build_ext import build_ext + + +class CMakeExtension(Extension): + def __init__(self, name, sourcedir=''): + Extension.__init__(self, name, sources=[]) + self.sourcedir = os.path.abspath(sourcedir) + + +class CMakeBuild(build_ext): + def run(self): + try: + out = subprocess.check_output(['cmake', '--version']) + except OSError: + raise RuntimeError("CMake must be installed to build the following extensions: " + + ", ".join(e.name for e in self.extensions)) + + for ext in self.extensions: + self.build_extension(ext) + + def build_extension(self, ext): + extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) + cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir, + '-DPYTHON_EXECUTABLE=' + sys.executable] + + cfg = 'Debug' if self.debug else 'Release' + build_args = ['--config', cfg] + + if not os.path.exists(self.build_temp): + os.makedirs(self.build_temp) + + subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp) + subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=self.build_temp) + setup( name='vttv', @@ -36,7 +47,9 @@ def __str__(self): description='Virtual Transport Task Visualizer', long_description='', ext_modules=ext_modules, - setup_requires=['nanobind>=1.3.2'], - cmdclass={'build_ext': build_ext}, + setup_requires=['nanobind>=1.3.2', 'cmake>=3.17.0'], + packages=find_packages(), + ext_modules=[CMakeExtension('vttv', 'bindings/python')], + cmdclass=dict(build_ext=CMakeBuild), zip_safe=False, ) \ No newline at end of file