-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsetup.py
More file actions
173 lines (137 loc) · 5.62 KB
/
setup.py
File metadata and controls
173 lines (137 loc) · 5.62 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
import contextlib
import os
import os.path
from os.path import join as pjoin
import sys
import sysconfig
from setuptools import setup, Extension, Distribution
from Cython.Distutils import build_ext as _build_ext
import Cython
if Cython.__version__ < '3':
raise Exception(
'Please update your Cython version. Supported Cython >= 3')
# --- Utility Functions ---
ext_suffix = sysconfig.get_config_var('EXT_SUFFIX')
@contextlib.contextmanager
def changed_dir(dirname):
oldcwd = os.getcwd()
os.chdir(dirname)
try:
yield
finally:
os.chdir(oldcwd)
# --- Custom build_ext Command ---
class build_ext(_build_ext):
_found_modules = []
# Define custom options needed for CMake
user_options = ([
('build-type=', None, 'build type (debug or release), default release'),
] + _build_ext.user_options)
def initialize_options(self):
_build_ext.initialize_options(self)
# Default to Release for Python bindings (avoids ASan issues)
self.build_type = "Release"
def build_extensions(self):
# Remove the dummy extension before building
self.extensions = [ext for ext in self.extensions if ext.name != '__dummy__']
_build_ext.build_extensions(self)
def run(self):
# 1. Run CMake build/install
self._run_cmake()
# 2. Let the parent class handle the rest
_build_ext.run(self)
def _run_cmake(self):
"""Run CMake to configure, build, and install the C++ targets."""
# The directory containing this setup.py
source = os.path.dirname(os.path.abspath(__file__))
# Get setuptools build directories
build_cmd = self.get_finalized_command('build')
saved_cwd = os.getcwd()
build_lib = pjoin(saved_cwd, build_cmd.build_lib)
# Install directly to setuptools' build_lib directory
# This way setuptools finds the files without manual copying
install_prefix = pjoin(build_lib, "silodb")
# Configuration name (e.g., Debug, Release)
config_name = self.build_type.capitalize()
# Reuse the existing cmake build directory (populated by `make dependencies`)
# so that silolib is not recompiled for each Python version
build_dir = pjoin(source, "build", config_name)
if not os.path.isdir(build_dir):
self.mkpath(build_dir)
# Change to the build_dir directory
with changed_dir(build_dir):
# --- CONFIGURE ---
cmake_options = [
f'-DCMAKE_INSTALL_PREFIX={install_prefix}',
f'-DPython3_EXECUTABLE={sys.executable}',
# Explicitly provide include dir so CMake finds headers for
# python-build-standalone installs (used by uv) on Linux
f'-DPython3_INCLUDE_DIR={sysconfig.get_path("include")}',
f'-DCMAKE_BUILD_TYPE={config_name}',
'-DBUILD_PYTHON_BINDINGS=ON',
]
print(f"-- Running cmake to configure project in {build_dir}")
print(f"-- Will install to: {install_prefix}")
self.spawn(['cmake'] + cmake_options + [source])
# --- BUILD AND INSTALL ---
print("-- Running cmake --build")
build_tool_args = []
if sys.platform != 'win32':
build_tool_args.append('--')
parallel = str(os.cpu_count() or 1)
build_tool_args.append(f'-j{parallel}')
# Build all targets
self.spawn(['cmake', '--build', '.', '--config', config_name] + build_tool_args)
# Install to setuptools' build directory
print(f"-- Running cmake --build --target install")
self.spawn(['cmake', '--build', '.', '--config', config_name, '--target', 'install'] + build_tool_args)
print(f"-- CMake install finished. Files staged in: {install_prefix}")
# Discover which modules were actually built
self._found_modules = []
module_names = ['database']
for name in module_names:
built_path = pjoin(install_prefix, name + ext_suffix)
if os.path.exists(built_path):
self._found_modules.append(name)
print(f"-- Found built module: {name}")
def _get_build_dir(self):
"""Get the package directory from build_py command"""
build_py = self.get_finalized_command('build_py')
return build_py.get_package_dir('silodb')
def get_outputs(self):
"""Returns the list of built extension files (.so/.pyd)"""
# Return paths to the built modules in setuptools' build directory
return [pjoin(self._get_build_dir(), name + ext_suffix)
for name in getattr(self, '_found_modules', [])]
# --- Distribution Setup ---
class BinaryDistribution(Distribution):
"""Custom distribution class to signal that this package has extension modules."""
def has_ext_modules(foo):
return True
setup(
name="silodb",
version="0.1.0",
packages=["silodb"],
package_dir={"silodb": "python/silodb"},
package_data={
"silodb": [
"*.so", "*.pyd", # Compiled extensions
"*.pxd", "*.pyx", # Cython source headers
"libsilolib.so" # Core C++ library
],
},
distclass=BinaryDistribution,
# Dummy extension is mandatory to trigger the 'build_ext' command
ext_modules=[Extension('__dummy__', sources=[])],
cmdclass={
'build_ext': build_ext
},
python_requires=">=3.8",
install_requires=[
"Cython>=3.0",
"pyarrow",
"pyroaring",
],
zip_safe=False,
)