Skip to content

Commit

Permalink
#16: bindings: Specify cmake build in setup.py
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrepebay committed Nov 7, 2023
1 parent 8389024 commit 9060ddb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
71 changes: 42 additions & 29 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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,
)

0 comments on commit 9060ddb

Please sign in to comment.