-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.py
72 lines (55 loc) · 2.34 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
import glob
import os
from pathlib import Path
import subprocess
from setuptools import setup, Extension
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 build_extension(self, ext):
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
if not extdir.endswith(os.path.sep):
extdir += os.path.sep
build_dir = os.path.abspath(os.path.join(ext.sourcedir, "build"))
cfg = "Debug" if self.debug else "Release"
cmake_args = [
"--preset=default",
f"-DCMAKE_BUILD_TYPE={cfg}",
"-DBUILD_PYTHON=ON",
"-DBUILD_TESTING=OFF",
]
if self.compiler.compiler_type == "msvc":
cmake_args.append("-DVCPKG_TARGET_TRIPLET=x64-windows-static")
build_args = ["--config", cfg]
if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
if hasattr(self, "parallel") and self.parallel:
build_args += [f"-j{self.parallel}"]
os.makedirs(build_dir, exist_ok=True)
subprocess.check_call(["cmake", ext.sourcedir] + cmake_args, cwd=build_dir)
subprocess.check_call(["cmake", "--build", "."] + build_args, cwd=build_dir)
if self.compiler.compiler_type == "msvc":
built_ext = str(Path(build_dir) / "python" / cfg / "acquire_zarr*.pyd")
else:
built_ext = str(Path(build_dir) / "python" / "acquire_zarr*.so")
matching_files = glob.glob(built_ext)
if not matching_files:
raise RuntimeError(f"Could not find any files matching {built_ext}")
# Move the built extension to the correct location
dst = self.get_ext_fullpath(ext.name)
self.move_file(matching_files, dst)
def move_file(self, src_files, dst, level=1):
import shutil
try:
os.makedirs(os.path.dirname(dst), exist_ok=True)
for filename in src_files:
shutil.copy2(filename, dst)
except Exception as e:
print(f"Error moving files to {dst}: {e}")
raise
setup(
ext_modules=[CMakeExtension("acquire_zarr")],
cmdclass=dict(build_ext=CMakeBuild),
)