-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsetup.py
More file actions
93 lines (72 loc) · 3.16 KB
/
Copy pathsetup.py
File metadata and controls
93 lines (72 loc) · 3.16 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
import glob
import os
from pathlib import Path
import subprocess
import sys
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"
vcpkg_root = os.environ.get("VCPKG_ROOT")
if not vcpkg_root:
raise RuntimeError("VCPKG_ROOT environment variable is not set")
cmake_args = [
f"-DCMAKE_TOOLCHAIN_FILE={vcpkg_root}/scripts/buildsystems/vcpkg.cmake",
f"-DCMAKE_BUILD_TYPE={cfg}",
"-DBUILD_PYTHON=ON",
"-DBUILD_TESTING=OFF",
f"-DPython3_EXECUTABLE={sys.executable}",
]
extra_args = [
arg for arg in os.environ.get("CMAKE_ARGS", "").split() if arg
]
cmake_args += extra_args
# macOS: <filesystem> requires a deployment target of 10.15+, but
# setuptools defaults MACOSX_DEPLOYMENT_TARGET to the Python build's
# value (often 10.9). Pin a modern target unless the caller overrides.
if sys.platform == "darwin" and not any(
"CMAKE_OSX_DEPLOYMENT_TARGET" in arg for arg in cmake_args
):
target = os.environ.get("MACOSX_DEPLOYMENT_TARGET", "11.0")
cmake_args.append(f"-DCMAKE_OSX_DEPLOYMENT_TARGET={target}")
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)
py_build_dir = Path(build_dir) / "python"
patterns = ("__init__*.pyd", "__init__*.so")
matching_files = []
for pattern in patterns:
matching_files.extend(py_build_dir.glob(f"**/{pattern}"))
if matching_files:
break
# 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.__init__")],
cmdclass=dict(build_ext=CMakeBuild),
)