forked from volcengine/OpenViking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
202 lines (175 loc) · 7.67 KB
/
setup.py
File metadata and controls
202 lines (175 loc) · 7.67 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
import os
import shutil
import sys
import sysconfig
from pathlib import Path
import pybind11
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
CMAKE_PATH = shutil.which("cmake") or "cmake"
C_COMPILER_PATH = shutil.which("gcc") or "gcc"
CXX_COMPILER_PATH = shutil.which("g++") or "g++"
ENGINE_SOURCE_DIR = "src/"
class CMakeBuildExtension(build_ext):
"""Custom CMake build extension that builds AGFS and C++ extensions."""
def run(self):
self.build_agfs()
self.cmake_executable = CMAKE_PATH
for ext in self.extensions:
self.build_extension(ext)
def _copy_binary(self, src, dst):
"""Helper to copy binary and set permissions."""
print(f"Copying AGFS binary from {src} to {dst}")
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(str(src), str(dst))
if sys.platform != "win32":
os.chmod(str(dst), 0o755)
def build_agfs(self):
"""Build AGFS server and binding library from source."""
# Paths
binary_name = "agfs-server.exe" if sys.platform == "win32" else "agfs-server"
if sys.platform == "win32":
lib_name = "libagfsbinding.dll"
elif sys.platform == "darwin":
lib_name = "libagfsbinding.dylib"
else:
lib_name = "libagfsbinding.so"
agfs_server_dir = Path("third_party/agfs/agfs-server").resolve()
# Target in source tree (for development/install)
agfs_bin_dir = Path("openviking/bin").resolve()
agfs_lib_dir = Path("openviking/lib").resolve()
agfs_target_binary = agfs_bin_dir / binary_name
agfs_target_lib = agfs_lib_dir / lib_name
# 1. Try to build from source
if agfs_server_dir.exists() and shutil.which("go"):
print("Building AGFS from source...")
import subprocess
# Build server
try:
print(f"Building AGFS server: {binary_name}")
build_args = (
["go", "build", "-o", f"build/{binary_name}", "cmd/server/main.go"]
if sys.platform == "win32"
else ["make", "build"]
)
subprocess.run(
build_args,
cwd=str(agfs_server_dir),
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
agfs_built_binary = agfs_server_dir / "build" / binary_name
if agfs_built_binary.exists():
self._copy_binary(agfs_built_binary, agfs_target_binary)
print("[OK] AGFS server built successfully from source")
else:
raise FileNotFoundError(
f"Build succeeded but binary not found at {agfs_built_binary}"
)
except (subprocess.CalledProcessError, Exception) as e:
error_msg = f"Failed to build AGFS server from source: {e}"
if isinstance(e, subprocess.CalledProcessError):
if e.stdout:
error_msg += (
f"\nBuild stdout:\n{e.stdout.decode('utf-8', errors='replace')}"
)
if e.stderr:
error_msg += (
f"\nBuild stderr:\n{e.stderr.decode('utf-8', errors='replace')}"
)
print(f"[Warning] {error_msg}")
# Build binding library
try:
print(f"Building AGFS binding library: {lib_name}")
# Use CGO_ENABLED=1 for shared library
env = os.environ.copy()
env["CGO_ENABLED"] = "1"
lib_build_args = ["make", "build-lib"]
subprocess.run(
lib_build_args,
cwd=str(agfs_server_dir),
env=env,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
agfs_built_lib = agfs_server_dir / "build" / lib_name
if agfs_built_lib.exists():
self._copy_binary(agfs_built_lib, agfs_target_lib)
print("[OK] AGFS binding library built successfully")
else:
print(f"[Warning] Binding library not found at {agfs_built_lib}")
except Exception as e:
print(f"[Warning] Failed to build AGFS binding library: {e}")
if isinstance(e, subprocess.CalledProcessError):
if e.stdout:
print(f"Build stdout: {e.stdout.decode('utf-8', errors='replace')}")
if e.stderr:
print(f"Build stderr: {e.stderr.decode('utf-8', errors='replace')}")
else:
if not agfs_server_dir.exists():
raise FileNotFoundError(f"AGFS source directory not found at {agfs_server_dir}")
else:
raise RuntimeError("Go compiler not found. Please install Go to build AGFS.")
# 2. Ensure binaries are copied to the build directory (where wheel is packaged from)
if self.build_lib:
agfs_bin_dir_build = Path(self.build_lib) / "openviking"
if agfs_target_binary.exists():
self._copy_binary(agfs_target_binary, agfs_bin_dir_build / "bin" / binary_name)
if agfs_target_lib.exists():
self._copy_binary(agfs_target_lib, agfs_bin_dir_build / "lib" / lib_name)
def build_extension(self, ext):
"""Build a single C++ extension module using CMake."""
ext_fullpath = Path(self.get_ext_fullpath(ext.name))
ext_dir = ext_fullpath.parent.resolve()
build_dir = Path(self.build_temp) / "cmake_build"
build_dir.mkdir(parents=True, exist_ok=True)
cmake_args = [
f"-S{Path(ENGINE_SOURCE_DIR).resolve()}",
f"-B{build_dir}",
"-DCMAKE_BUILD_TYPE=Release",
f"-DPY_OUTPUT_DIR={ext_dir}",
"-DCMAKE_VERBOSE_MAKEFILE=ON",
"-DCMAKE_INSTALL_RPATH=$ORIGIN",
f"-DPython3_EXECUTABLE={sys.executable}",
f"-DPython3_INCLUDE_DIRS={sysconfig.get_path('include')}",
f"-DPython3_LIBRARIES={sysconfig.get_config_vars().get('LIBRARY')}",
f"-Dpybind11_DIR={pybind11.get_cmake_dir()}",
f"-DCMAKE_C_COMPILER={C_COMPILER_PATH}",
f"-DCMAKE_CXX_COMPILER={CXX_COMPILER_PATH}",
f"-DOV_X86_SIMD_LEVEL={os.environ.get('OV_X86_SIMD_LEVEL', 'AVX2')}",
]
if sys.platform == "darwin":
cmake_args.append("-DCMAKE_OSX_DEPLOYMENT_TARGET=10.15")
elif sys.platform == "win32":
cmake_args.extend(["-G", "MinGW Makefiles"])
self.spawn([self.cmake_executable] + cmake_args)
build_args = ["--build", str(build_dir), "--config", "Release", f"-j{os.cpu_count() or 4}"]
self.spawn([self.cmake_executable] + build_args)
setup(
install_requires=[
f"pyagfs @ file://localhost/{os.path.abspath('third_party/agfs/agfs-sdk/python')}"
],
ext_modules=[
Extension(
name="openviking.storage.vectordb.engine",
sources=[],
)
],
cmdclass={
"build_ext": CMakeBuildExtension,
},
package_data={
"openviking": [
"bin/agfs-server",
"bin/agfs-server.exe",
"bin/libagfsbinding.so",
"bin/libagfsbinding.dylib",
"bin/libagfsbinding.dll",
"bin/ov", # 新增
"bin/ov.exe", # 新增
],
},
include_package_data=True,
)