Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
253 lines (212 loc) · 8.7 KB
/
Copy pathsetup.py
File metadata and controls
253 lines (212 loc) · 8.7 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
from setuptools import setup, Extension, find_packages
from setuptools.command.build_ext import build_ext
import os
import subprocess
import sys
import shutil
class FlatbuffersExtension(Extension):
def __init__(self, name, sourcedir=""):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
class CMakeExtension(Extension):
def __init__(self, name, sourcedir=""):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
class CMakeBuild(build_ext):
def build_flatbuffers(self):
install_dir = os.path.abspath(
os.path.join(self.build_temp, "flatbuffers-install")
)
try:
subprocess.check_output(
[os.path.join(install_dir, "bin", "flatc"), "--version"]
)
return None
except OSError:
print("Building Flatbuffers from source...")
code_url = (
"https://github.com/google/flatbuffers/archive/refs/tags/v24.3.25.zip"
)
code_dir = os.path.abspath(
os.path.join(self.build_temp, "flatbuffers-source")
)
build_dir = os.path.abspath(
os.path.join(self.build_temp, "flatbuffers-build")
)
# Clean any previous failed builds
if os.path.exists(code_dir):
shutil.rmtree(code_dir)
if os.path.exists(build_dir):
shutil.rmtree(build_dir)
if os.path.exists(install_dir):
shutil.rmtree(install_dir)
os.makedirs(code_dir, exist_ok=True)
os.makedirs(build_dir, exist_ok=True)
os.makedirs(install_dir, exist_ok=True)
# Download and verify the zip file exists
zip_path = os.path.join(code_dir, "flatbuffers.zip")
subprocess.check_call(["curl", "-L", code_url, "-o", zip_path])
if not os.path.exists(zip_path):
raise RuntimeError(
f"Failed to download Flatbuffers source to {zip_path}"
)
# Extract with better error handling
try:
subprocess.check_call(["unzip", "-o", "-q", zip_path, "-d", code_dir])
except subprocess.CalledProcessError:
print(f"Failed to extract {zip_path} to {code_dir}")
raise
# Build flatbuffers with install prefix
cmake_args = [
"cmake",
os.path.join(code_dir, "flatbuffers-24.3.25"),
f"-DCMAKE_INSTALL_PREFIX={install_dir}",
"-G",
"Unix Makefiles",
"-DCMAKE_BUILD_TYPE=Release",
]
try:
subprocess.check_call(cmake_args, cwd=build_dir)
subprocess.check_call(["cmake", "--build", "."], cwd=build_dir)
subprocess.check_call(["cmake", "--install", "."], cwd=build_dir)
except subprocess.CalledProcessError:
print(f"Failed to build Flatbuffers in {build_dir}")
raise
return install_dir
def run(self):
try:
subprocess.check_output(["cmake", "--version"])
except OSError:
raise RuntimeError("CMake must be installed")
# Build or verify flatbuffers installation
self.build_flatbuffers()
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext):
# Get the build directory for temporary files
build_temp = os.path.abspath(self.build_temp)
# Get the installation directory (site-packages)
# This is where we want our .so files to end up
install_dir = os.path.dirname(os.path.abspath(self.get_ext_fullpath(ext.name)))
if not hasattr(self, "_flatbuffers_dir"):
self._flatbuffers_dir = self.build_flatbuffers()
cmake_args = [
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={build_temp}",
# Ensure CMake uses the same interpreter invoking setup.py.
# `find_package(Python ...)` honors `Python_EXECUTABLE`.
f"-DPython_EXECUTABLE={sys.executable}",
]
if self._flatbuffers_dir:
flatc = os.path.join(self._flatbuffers_dir, "bin", "flatc")
cmake_args.extend(
[
f"-DFLATBUFFERS_ROOT={self._flatbuffers_dir}",
f"-DFLATC_EXECUTABLE={flatc}",
]
)
cfg = "Debug" if self.debug else "Release"
build_args = ["--config", cfg]
# Unix/Linux only configuration
cmake_args += [f"-DCMAKE_BUILD_TYPE={cfg}"]
build_args += ["--", "-j2"]
if not os.path.exists(build_temp):
os.makedirs(build_temp)
subprocess.check_call(["cmake", ext.sourcedir] + cmake_args, cwd=build_temp)
subprocess.check_call(["cmake", "--build", "."] + build_args, cwd=build_temp)
# Determine library extension based on platform
if sys.platform == "darwin":
# MODULE (.so bundle), helper libs as SHARED (.dylib)
main_ext = ".so"
helper_ext = ".dylib"
main_target_ext = ".so"
helper_target_ext = ".dylib"
elif sys.platform == "win32":
main_ext = ".pyd"
helper_ext = ".pyd"
main_target_ext = ".pyd"
helper_target_ext = ".pyd"
else:
main_ext = ".so"
helper_ext = ".so"
main_target_ext = ".so"
helper_target_ext = ".so"
package_dir = os.path.join(install_dir, "sauerkraut")
os.makedirs(package_dir, exist_ok=True)
# Copy main module
main_src = os.path.join(build_temp, f"_sauerkraut{main_ext}")
if os.path.exists(main_src):
dst = os.path.join(package_dir, f"_sauerkraut{main_target_ext}")
self.copy_file(main_src, dst)
# Copy helper libraries
for lib_name in ["greenlet_compat", "serdes"]:
src = os.path.join(build_temp, f"{lib_name}{helper_ext}")
if os.path.exists(src):
target_name = f"{lib_name}{helper_target_ext}"
dst = os.path.join(package_dir, target_name)
self.copy_file(src, dst)
def _get_python_library(self):
"""Helper function to find the Python library path."""
# Check common library name patterns
py_ver = f"{sys.version_info.major}.{sys.version_info.minor}"
library_names = [
f"libpython{py_ver}.so",
f"libpython{py_ver}d.so",
f"libpython{py_ver}m.so",
f"python{py_ver}.dll",
]
lib_dirs = [
os.path.join(sys.prefix, "lib"),
os.path.join(sys.prefix, "libs"),
os.path.join(sys.base_prefix, "lib"),
os.path.join(sys.base_prefix, "libs"),
]
# Try to find the library
for lib_dir in lib_dirs:
for name in library_names:
library_path = os.path.join(lib_dir, name)
if os.path.exists(library_path):
return library_path
return None
def get_outputs(self):
"""Get the path to the built library."""
# Get the built library path from the CMake build directory
if sys.platform == "win32":
lib_name = "_sauerkraut.pyd"
else:
lib_name = "_sauerkraut.so"
# Look in the CMake build directory
library_path = os.path.join(self.build_temp, lib_name)
# Also check lib.* directory as a fallback
if not os.path.exists(library_path):
# Find any lib.* directory
build_dir = os.path.dirname(self.build_temp)
lib_dirs = [d for d in os.listdir(build_dir) if d.startswith("lib.")]
if lib_dirs:
alt_path = os.path.join(build_dir, lib_dirs[0], lib_name)
if os.path.exists(alt_path):
library_path = alt_path
if not os.path.exists(library_path):
raise RuntimeError(f"Built library not found at {library_path}")
return [library_path]
setup(
name="sauerkraut",
version="0.1.0",
author="Zane Fink",
author_email="zanef2@illinois.edu",
description="Serialization for Python's Control State",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
packages=find_packages(),
ext_modules=[CMakeExtension("_sauerkraut")],
cmdclass={
"build_ext": CMakeBuild,
},
zip_safe=False,
python_requires=">=3.13",
install_requires=[
"bytecode @ git+https://github.com/ZwFink/bytecode.git",
"numpy",
"dill",
"greenlet",
],
)