Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
350 lines (293 loc) · 11.8 KB
/
Copy pathsetup.py
File metadata and controls
350 lines (293 loc) · 11.8 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
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
348
349
350
import platform
import subprocess
import os
import sys
from setuptools import setup, find_packages
from setuptools.command.build_ext import build_ext
from setuptools.command.sdist import sdist
from wheel.bdist_wheel import bdist_wheel
IS_MACOS = platform.system() == "Darwin"
IS_WINDOWS = platform.system() == "Windows"
def copy_dependencies():
try:
from scripts.copy_core_deps import copy_dependencies
copy_dependencies()
except ImportError:
return
class CustomBuildExt(build_ext):
def run(self):
copy_dependencies()
print("\n" + "="*80)
print("CUSTOM BUILD EXTENSION RUNNING WITH GCC + AVX SUPPORT")
print("="*80)
# Manually compile and link the shared library
setup_dir = os.path.dirname(os.path.abspath(__file__))
print(f"Setup directory: {setup_dir}")
sources = ['db.c', 'minheap.c', 'distance.c',
'file.c', 'cJSON.c', 'sqlite3.c', 'utils.c', 'query_convert.c', 'paginate.c']
source_paths = [os.path.join(
setup_dir, 'src', 'core', 'src', src) for src in sources]
# Verify source files exist
for src in source_paths:
if not os.path.exists(src):
print(f"ERROR: Source file does not exist: {src}")
return
else:
print(f"Found source file: {src}")
# Create a directory for object files
os.makedirs('obj', exist_ok=True)
print("Created obj directory")
# Create output directory
output_dir = os.path.join(setup_dir, 'src', 'tinyvec', 'core')
os.makedirs(output_dir, exist_ok=True)
print(f"Output directory: {output_dir}")
# Common flags for all platforms
compile_flags = [
'-O3', # Optimization level
'-fPIC', # Position Independent Code
]
# Determine compiler and platform-specific settings
if IS_WINDOWS:
# Use mingw32 on Windows
compiler = 'gcc'
print("\nUsing MinGW GCC on Windows with AVX support")
# Add AVX instructions for Windows
compile_flags.extend([
'-mavx', # Enable AVX instructions
'-mavx2', # Enable AVX2 instructions
'-mfma' # Enable FMA instructions
])
# Check if GCC is available
try:
subprocess.run(
[compiler, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
print(f"{compiler} is available")
except (subprocess.SubprocessError, FileNotFoundError):
print(f"ERROR: {compiler} not found in PATH")
print("Make sure you have MinGW installed and in your PATH")
return
lib_name = "tinyveclib.dll"
elif IS_MACOS:
compiler = 'gcc'
print("\nUsing GCC on macOS")
# Check if GCC is available
try:
subprocess.run(
[compiler, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
print(f"{compiler} is available")
except (subprocess.SubprocessError, FileNotFoundError):
# Try with clang as fallback on macOS
compiler = 'clang'
try:
subprocess.run(
[compiler, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
print(f"Using {compiler} instead")
except (subprocess.SubprocessError, FileNotFoundError):
print(f"ERROR: Neither gcc nor clang found in PATH")
print("Consider installing GCC with: brew install gcc")
return
# Check architecture and set appropriate flags
is_arm = platform.machine().startswith('arm')
if is_arm:
# ARM Mac (Apple Silicon)
print("Building for ARM64 architecture")
compile_flags.extend(['-arch', 'arm64'])
else:
# Intel Mac
print("Building for x86_64 architecture")
compile_flags.extend([
'-mavx',
'-mavx2',
'-mfma',
'-arch', 'x86_64'
])
lib_name = "tinyveclib.dylib"
else:
# Linux or other Unix
compiler = 'gcc'
print("\nUsing GCC on Linux/Unix with AVX support")
# Add AVX instructions for Linux
compile_flags.extend([
'-mavx', # Enable AVX instructions
'-mavx2', # Enable AVX2 instructions
'-mfma' # Enable FMA instructions
])
# Check if GCC is available
try:
subprocess.run(
[compiler, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
print(f"{compiler} is available")
except (subprocess.SubprocessError, FileNotFoundError):
print(f"ERROR: {compiler} not found in PATH")
print("Install GCC with your package manager")
return
lib_name = "tinyveclib.so"
# Compile object files
obj_files = []
for src in source_paths:
obj_file = os.path.join(
'obj', os.path.basename(src).replace('.c', '.o'))
obj_files.append(obj_file)
if os.path.basename(src) == 'sqlite3.c' and os.path.exists(obj_file):
print(f"Found existing sqlite3.o, skipping compilation")
continue
compile_cmd = [
compiler, *compile_flags, '-c', src, '-o', obj_file
]
print(f"\nCompiling: {os.path.basename(src)}")
print(f"Command: {' '.join(compile_cmd)}")
try:
result = subprocess.run(compile_cmd,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True)
if result.stdout:
print(f"Compilation output: {result.stdout}")
# Verify object file was created
if os.path.exists(obj_file):
print(f"Created object file: {obj_file}")
else:
print(f"ERROR: Failed to create object file: {obj_file}")
return
except subprocess.CalledProcessError as e:
print(f"Compilation ERROR: {e}")
if e.stdout:
print(f"STDOUT: {e.stdout}")
if e.stderr:
print(f"STDERR: {e.stderr}")
return
# Link the shared library
output_path = os.path.join(output_dir, lib_name)
print("\n" + "-"*40)
print(f"LINKING SHARED LIBRARY: {output_path}")
print("-"*40)
if IS_WINDOWS:
# MinGW linking on Windows
link_cmd = [
compiler,
'-shared',
'-o', output_path,
*obj_files,
*compile_flags
]
elif IS_MACOS:
# macOS linking
link_cmd = [
compiler,
'-shared',
'-dynamiclib',
'-o', output_path,
*obj_files,
*compile_flags,
'-Wl,-install_name,@rpath/' + lib_name
]
else:
# Linux linking
link_cmd = [
compiler,
'-shared',
'-o', output_path,
*obj_files,
*compile_flags
]
print(f"Link command: {' '.join(link_cmd)}")
try:
result = subprocess.run(link_cmd,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True)
if result.stdout:
print(f"Linking output: {result.stdout}")
# Verify library was created
if os.path.exists(output_path):
print(f"SUCCESS: Created shared library: {output_path}")
print(f"Library size: {os.path.getsize(output_path)} bytes")
else:
print(f"ERROR: Failed to create shared library: {output_path}")
return
except subprocess.CalledProcessError as e:
print(f"Linking ERROR: {e}")
if e.stdout:
print(f"STDOUT: {e.stdout}")
if e.stderr:
print(f"STDERR: {e.stderr}")
return
# Clean up object files
print("\nCleaning up...")
for obj in obj_files:
try:
# Skip cleanup for sqlite3.o
if os.path.basename(obj) == 'sqlite3.o':
print(f"Keeping: {obj}")
continue
if os.path.exists(obj):
os.remove(obj)
print(f"Cleaned up: {obj}")
except OSError as e:
print(f"Failed to clean up {obj}: {e}")
try:
if os.path.exists('obj') and len(os.listdir('obj')) == 0:
os.rmdir('obj')
print("Removed obj directory")
except OSError as e:
print(f"Failed to remove obj directory: {e}")
print("\nCustomBuildExt completed")
# Custom sdist to ensure the shared library is included in the source distribution
class CustomSdist(sdist):
def run(self):
# Create a comprehensive MANIFEST.in file
setup_dir = os.path.dirname(os.path.abspath(__file__))
manifest_dir = os.path.join(setup_dir, "MANIFEST.in")
with open(manifest_dir, "w") as f:
# C source files
f.write("recursive-include src/core/src *.c *.h\n")
f.write("recursive-include src/core/include *.h\n")
# Build scripts / configuration files
f.write("include setup.py\n")
f.write("include README*\n")
f.write("include LICENSE*\n")
# Don't include compiled binary files in source dist
f.write("exclude src/tinyvec/core/*.dll\n")
f.write("exclude src/tinyvec/core/*.so\n")
f.write("exclude src/tinyvec/core/*.dylib\n")
print(f"Created comprehensive MANIFEST.in file for source distribution")
super().run()
# Custom wheel to ensure the shared library is included in the wheel
class CustomBdistWheel(bdist_wheel):
def run(self):
self.run_command('build_ext')
super().run()
def finalize_options(self):
super().finalize_options()
# Mark the wheel as platform-specific
self.root_is_pure = False
def get_tag(self):
# Override the tag generation
python, abi, plat = super().get_tag()
# Keep the platform tag for Windows but set Python and ABI tags to 'any'
if IS_WINDOWS:
python = 'py3'
abi = 'none'
# plat remains the same (win_amd64 or win32)
return python, abi, plat
setup(
name="tinyvecdb",
version="0.2.5",
description="TinyVecDB is a high performance, lightweight, embedded vector database for similarity search.",
cmdclass={
'build_ext': CustomBuildExt,
'sdist': CustomSdist,
'bdist_wheel': CustomBdistWheel,
},
package_dir={"": "src"},
packages=find_packages(where="src"),
package_data={
"tinyvec.core": ["*.dll", "*.so", "*.dylib"],
},
include_package_data=True,
zip_safe=False,
)
if __name__ == "__main__" and "build_ext" in sys.argv:
copy_dependencies()