forked from libcsp/libcsp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
78 lines (67 loc) · 2.78 KB
/
Copy pathsetup.py
File metadata and controls
78 lines (67 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import sys
import os
import subprocess
from setuptools import setup, Extension, find_packages
from setuptools.command.build_ext import build_ext
# This small extension class just holds data about where the source is
class CMakeExtension(Extension):
def __init__(self, name, sourcedir="", cmake_args=None):
# We pass an empty list of sources to Extension since CMake will handle it
super().__init__(name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
self.cmake_args = cmake_args or []
# Our custom build_ext command that runs your own CMake/Ninja calls
class CMakeBuild(build_ext):
def build_extension(self, ext):
# Where setuptools expects the final .so/.pyd to be placed
ext_fullpath = self.get_ext_fullpath(ext.name)
ext_dir = os.path.dirname(os.path.abspath(ext_fullpath))
# Name of a temporary build folder (setuptools normally uses self.build_temp)
build_temp = os.path.join(self.build_temp, ext.name)
os.makedirs(build_temp, exist_ok=True)
# If you have a Debug vs Release distinction
cfg = "Debug" if self.debug else "Release"
# Example arguments you might pass to CMake
cmake_args = [
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={ext_dir}",
f"-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY={ext_dir}",
f"-DCMAKE_RUNTIME_OUTPUT_DIRECTORY={ext_dir}",
f"-DCMAKE_BUILD_TYPE={cfg}",
f"-DPython3_EXECUTABLE={sys.executable}",
"-DCSP_BUFFER_SIZE=600",
"-G",
"Ninja",
] + ext.cmake_args
# 1) Configure step
subprocess.check_call(["cmake", ext.sourcedir] + cmake_args, cwd=build_temp)
# 2) Build step
subprocess.check_call(["ninja"], cwd=build_temp)
# On some platforms (e.g. Windows/MSVC multi-config generators),
# you might need to set CMAKE_LIBRARY_OUTPUT_DIRECTORY_<CONFIG>
# or otherwise handle the built artifact location.
#
# If needed, you can move or rename the .so/.pyd from build_temp to ext_fullpath.
# But if you've set *OUTPUT_DIRECTORY* to ext_dir above,
# it should already appear in the correct place.
setup(
name="libcsp",
version="0.1.0",
install_requires=["wheel"],
# Register our extension and tell it about any special CMake args
ext_modules=[
CMakeExtension(
name="libcsp",
sourcedir=".", # or wherever your main CMakeLists.txt is
cmake_args=[
"-DCSP_BUILD_SAMPLES=ON",
"-DCSP_ENABLE_PYTHON3_BINDINGS=ON",
"-DCSP_USE_RTABLE=ON",
"-DCSP_REPRODUCIBLE_BUILDS=ON",
],
),
],
# Hook in our custom command
cmdclass={
"build_ext": CMakeBuild,
},
)