-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.py
347 lines (290 loc) · 14.5 KB
/
setup.py
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import os
import sys
from setuptools import setup
# Check if we want Python-only installation
PYTHON_ONLY = os.environ.get("PYTHON_ONLY", "0").lower() in ("1", "true", "yes")
if not PYTHON_ONLY:
import subprocess
import site
import shutil
from setuptools import Extension
from setuptools.command.build_ext import build_ext
import tomli
def load_config():
"""Load configuration from pyproject.toml."""
try:
with open("pyproject.toml", "rb") as f:
pyproject = tomli.load(f)
return {
'vcpkg': pyproject.get("tool", {}).get("vcpkg", {}),
'cmake': pyproject.get("tool", {}).get("cmake", {})
}
except FileNotFoundError:
print("Warning: pyproject.toml not found")
return {'vcpkg': {}, 'cmake': {}}
class CMakeExtension(Extension):
def __init__(self, name, sourcedir=""):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(os.path.dirname(__file__))
class CMakeBuild(build_ext):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
config = load_config()
self.vcpkg_config = config['vcpkg']
self.cmake_config = config['cmake']
def run(self):
try:
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)
)
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
self._setup_vcpkg()
for ext in self.extensions:
self.build_extension(ext)
def _setup_vcpkg(self):
"""Setup vcpkg with configuration from pyproject.toml."""
vcpkg_root = os.path.abspath(os.path.join(self.build_temp, '.vcpkg'))
os.environ['VCPKG_ROOT'] = vcpkg_root
repository = self.vcpkg_config.get("repository", "https://github.com/Microsoft/vcpkg.git")
revision = self.vcpkg_config.get("revision")
manifest_path = self.vcpkg_config.get("manifest")
try:
if os.path.exists(vcpkg_root):
shutil.rmtree(vcpkg_root)
print(f"Cloning vcpkg repository...")
subprocess.check_call(
["git", "clone", repository, vcpkg_root],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
if revision:
print(f"Checking out revision {revision}")
subprocess.check_call(
["git", "fetch", "--all"],
cwd=vcpkg_root,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
subprocess.check_call(
["git", "reset", "--hard", revision],
cwd=vcpkg_root,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
if manifest_path:
manifest_path = os.path.abspath(manifest_path)
if os.path.exists(manifest_path):
print(f"Using vcpkg.json from {manifest_path}")
vcpkg_json_target = os.path.join(vcpkg_root, 'vcpkg.json')
shutil.copy2(manifest_path, vcpkg_json_target)
else:
print(f"Warning: Specified vcpkg.json at {manifest_path} not found")
print("Bootstrapping vcpkg...")
bootstrap_script = 'bootstrap-vcpkg.bat' if sys.platform.startswith('win32') else 'bootstrap-vcpkg.sh'
bootstrap_path = os.path.join(vcpkg_root, bootstrap_script)
if not os.path.exists(bootstrap_path):
raise RuntimeError(f"Bootstrap script not found at {bootstrap_path}")
if not sys.platform.startswith('win32'):
os.chmod(bootstrap_path, 0o755)
result = subprocess.run(
[os.path.abspath(bootstrap_path)],
cwd=vcpkg_root,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
if result.returncode != 0:
print("Bootstrap stdout:", result.stdout)
print("Bootstrap stderr:", result.stderr)
raise RuntimeError("vcpkg bootstrap failed")
else:
print("vcpkg bootstrap successful")
if manifest_path and os.path.exists(os.path.join(vcpkg_root, 'vcpkg.json')):
self._install_manifest_mode(vcpkg_root)
else:
self._install_vcpkg_packages(vcpkg_root)
except subprocess.CalledProcessError as e:
print(f"Command failed: {e.cmd}")
print(f"Output: {e.output.decode() if e.output else 'No output'}")
print(f"Error: {e.stderr.decode() if e.stderr else 'No error output'}")
raise RuntimeError("Failed to setup vcpkg") from e
except Exception as e:
print(f"Error setting up vcpkg: {str(e)}")
raise RuntimeError("Failed to setup vcpkg") from e
def _install_manifest_mode(self, vcpkg_root):
"""Install packages using vcpkg manifest mode."""
vcpkg_exe = os.path.abspath(os.path.join(
vcpkg_root,
'vcpkg.exe' if sys.platform.startswith('win32') else './vcpkg'
))
if not os.path.exists(vcpkg_exe) and not sys.platform.startswith('win32'):
vcpkg_exe = os.path.abspath(os.path.join(vcpkg_root, 'vcpkg'))
if not os.path.exists(vcpkg_exe):
raise RuntimeError(f"vcpkg executable not found at {vcpkg_exe}")
if not sys.platform.startswith('win32'):
os.chmod(vcpkg_exe, 0o755)
print("Installing dependencies from vcpkg.json...")
result = subprocess.run(
[vcpkg_exe, "install", "--clean-after-build"],
cwd=vcpkg_root,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
if result.returncode != 0:
print("Installation failed")
print("stdout:", result.stdout)
print("stderr:", result.stderr)
raise RuntimeError("Failed to install vcpkg dependencies")
else:
print("Successfully installed all dependencies from vcpkg.json")
self._integrate_vcpkg(vcpkg_exe, vcpkg_root)
def _install_vcpkg_packages(self, vcpkg_root):
"""Install vcpkg packages based on platform."""
vcpkg_exe = os.path.join(vcpkg_root, 'vcpkg.exe' if sys.platform.startswith('win32') else 'vcpkg')
vcpkg_exe = os.path.abspath(vcpkg_exe)
if not os.path.exists(vcpkg_exe) and not sys.platform.startswith('win32'):
raise RuntimeError(f"vcpkg executable not found at {vcpkg_exe}")
if not sys.platform.startswith('win32'):
os.chmod(vcpkg_exe, 0o755)
packages = self.vcpkg_config.get("packages", {}).get("common", [])
if sys.platform.startswith('win32'):
packages.extend(self.vcpkg_config.get("packages", {}).get("windows", []))
elif sys.platform.startswith('darwin'):
packages.extend(self.vcpkg_config.get("packages", {}).get("macos", []))
else:
packages.extend(self.vcpkg_config.get("packages", {}).get("linux", []))
for package in packages:
try:
print(f"Installing {package}...")
result = subprocess.run(
[vcpkg_exe, "install", package, "--clean-after-build"],
cwd=vcpkg_root,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
if result.returncode != 0:
print(f"Warning: Failed to install {package}")
print("stdout:", result.stdout)
print("stderr:", result.stderr)
raise RuntimeError(f"Failed to install {package}")
else:
print(f"Successfully installed {package}")
except Exception as e:
print(f"Error installing {package}: {str(e)}")
raise RuntimeError(f"Failed to install vcpkg package {package}") from e
self._integrate_vcpkg(vcpkg_exe, vcpkg_root)
def _integrate_vcpkg(self, vcpkg_exe, vcpkg_root):
"""Integrate vcpkg with the system."""
try:
result = subprocess.run(
[vcpkg_exe, "integrate", "install"],
cwd=vcpkg_root,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
if result.returncode != 0:
print("Warning: vcpkg integration failed")
print("stdout:", result.stdout)
print("stderr:", result.stderr)
else:
print("vcpkg integration successful")
except Exception as e:
print(f"Warning: vcpkg integration failed: {str(e)}")
def build_extension(self, ext):
"""Build the extension using CMake."""
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
build_temp = os.path.join(self.build_temp, ext.name)
cmake_args, source_dir = self._get_cmake_args(ext, extdir, self._find_nanobind())
build_args = ["--config", self.cmake_config.get('build_type', 'Release')]
if target := self.cmake_config.get('target'):
build_args.extend(["--target", target])
os.makedirs(build_temp, exist_ok=True)
self._configure_cmake(ext, cmake_args, build_temp, self._get_build_env())
self._build_cmake(build_args, build_temp)
def _find_nanobind(self):
"""Find nanobind installation path."""
for path in site.getsitepackages():
candidate = os.path.join(path, 'nanobind', 'cmake')
if os.path.exists(os.path.join(candidate, 'nanobind-config.cmake')):
return candidate
raise RuntimeError("Could not find nanobind installation.")
def _get_cmake_args(self, ext, extdir, nanobind_path):
source_dir = os.path.abspath(os.path.join(
ext.sourcedir,
self.cmake_config.get("source_dir", "per_jsp/cpp")
))
args = [
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}",
f"-DPython_EXECUTABLE={sys.executable}",
f"-Dnanobind_DIR={nanobind_path}",
f"-DCMAKE_TOOLCHAIN_FILE={os.path.join(os.environ['VCPKG_ROOT'], 'scripts/buildsystems/vcpkg.cmake')}",
f"-DCMAKE_BUILD_TYPE={self.cmake_config.get('build_type', 'Release')}"
]
if sys.platform.startswith('darwin'):
macos_config = self.cmake_config.get('macos', {})
if 'deployment_target' in macos_config:
args.append(f"-DCMAKE_OSX_DEPLOYMENT_TARGET={macos_config['deployment_target']}")
if 'architectures' in macos_config:
args.append(f"-DCMAKE_OSX_ARCHITECTURES={';'.join(macos_config['architectures'])}")
if sys.platform.startswith('win32'):
windows_config = self.cmake_config.get('windows', {})
if 'generator_platform' in windows_config:
args.append(f"-A{windows_config['generator_platform']}")
if self.cmake_config.get('use_ninja', True):
ninja_path = shutil.which("ninja")
if ninja_path:
print(f"Found Ninja at: {ninja_path}")
args.extend(["-GNinja", f"-DCMAKE_MAKE_PROGRAM={ninja_path}"])
else:
print("Ninja not found, using default CMake generator")
return args, source_dir
def _get_build_env(self):
"""Configure build environment."""
env = os.environ.copy()
env["CXXFLAGS"] = f"{env.get('CXXFLAGS', '')} -DVERSION_INFO=\\\"{self.distribution.get_version()}\\\""
return env
def _configure_cmake(self, ext, cmake_args, build_temp, env):
cmake_args, source_dir = self._get_cmake_args(ext, os.path.abspath(
os.path.dirname(self.get_ext_fullpath(ext.name))),
self._find_nanobind()
)
if not os.path.exists(source_dir):
raise RuntimeError(f"CMake source directory {source_dir} does not exist")
if self.cmake_config.get('use_preset', False):
preset_name = self.cmake_config.get('preset_name', 'pip-install')
preset_file = os.path.join(source_dir, "CMakePresets.json")
if os.path.exists(preset_file):
cmake_args.extend(["--preset", preset_name])
else:
print(f"Warning: CMakePresets.json not found at {preset_file}, ignoring preset configuration")
os.makedirs(build_temp, exist_ok=True)
print(f"Configuring CMake with source directory: {source_dir}")
print(f"CMake arguments: {' '.join(cmake_args)}")
subprocess.check_call(
["cmake", source_dir] + cmake_args,
cwd=build_temp,
env=env
)
def _build_cmake(self, build_args, build_temp):
"""Build CMake project."""
subprocess.check_call(
["cmake", "--build", "."] + build_args,
cwd=build_temp
)
ext_modules = [CMakeExtension("per_jsp")]
cmdclass = {"build_ext": CMakeBuild}
else:
ext_modules = []
cmdclass = {}
setup(
ext_modules=ext_modules,
cmdclass=cmdclass,
zip_safe=False
)