-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
233 lines (192 loc) · 8.24 KB
/
setup.py
File metadata and controls
233 lines (192 loc) · 8.24 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
"""Custom build script to download and compile Valkey."""
import platform
import shutil
import subprocess
import tarfile
import urllib.request
from pathlib import Path
from setuptools import setup
from setuptools.command.bdist_wheel import bdist_wheel
from setuptools.command.build_py import build_py
# Valkey version to bundle
VALKEY_VERSION = "9.0.3"
def get_platform_tag():
"""Get the proper platform tag for the wheel."""
system = platform.system().lower()
machine = platform.machine().lower()
# Normalize machine
if machine in ("x86_64", "amd64"):
machine = "x86_64"
elif machine in ("aarch64", "arm64"):
if system == "linux":
machine = "aarch64"
else: # darwin
machine = "arm64"
if system == "linux":
# Use manylinux_2_28 (compatible with glibc 2.28+, released 2018)
# Works on: Ubuntu 20.04+, Debian 10+, RHEL 8+, etc.
return f"manylinux_2_28_{machine}"
elif system == "darwin":
# macOS version minimums
if machine == "arm64":
return f"macosx_11_0_{machine}" # macOS 11 Big Sur minimum for ARM64
else:
return f"macosx_10_13_{machine}" # macOS 10.13 High Sierra minimum for x86_64
else:
raise RuntimeError(f"Unsupported platform: {system}")
class BdistWheelCommand(bdist_wheel):
"""Custom bdist_wheel to set platform tag."""
def finalize_options(self):
"""Set the platform name for the wheel."""
super().finalize_options()
# Override platform tag
self.plat_name = get_platform_tag()
self.plat_name_supplied = True
class BuildValkeyCommand(build_py):
"""Custom build command to download and compile Valkey."""
def run(self):
"""Build Valkey binary and then run standard build."""
self.announce("Downloading and building Valkey...", level=3)
try:
self.build_valkey()
except Exception as e:
self.warn(f"Failed to build Valkey: {e}")
raise
# Continue with standard build
super().run()
def build_valkey(self):
"""Download Valkey source and compile it."""
# Create build directory
build_dir = Path("build")
build_dir.mkdir(exist_ok=True)
# Download Valkey source
tarball_path = build_dir / f"valkey-{VALKEY_VERSION}.tar.gz"
if not tarball_path.exists():
self.announce(f"Downloading Valkey {VALKEY_VERSION}...", level=3)
url = f"https://github.com/valkey-io/valkey/archive/refs/tags/{VALKEY_VERSION}.tar.gz"
urllib.request.urlretrieve(url, tarball_path)
self.announce("Download complete", level=3)
else:
self.announce("Using cached Valkey tarball", level=3)
# Extract tarball
valkey_src_dir = build_dir / f"valkey-{VALKEY_VERSION}"
if not valkey_src_dir.exists():
self.announce("Extracting Valkey source...", level=3)
with tarfile.open(tarball_path, "r:gz") as tar:
tar.extractall(build_dir, filter="data")
self.announce("Extraction complete", level=3)
else:
self.announce("Using cached Valkey source", level=3)
# Compile Valkey with static linking
self.announce(
"Compiling Valkey with static linking (this may take a few minutes)...", level=3
)
# Determine number of parallel jobs
try:
import multiprocessing
cpu_count = multiprocessing.cpu_count()
except Exception:
cpu_count = 1
# Try static linking first
# BUILD_TLS=no: Disable TLS to avoid OpenSSL dependency
# MALLOC=libc: Use standard libc malloc instead of jemalloc (simpler)
# These flags create a self-contained static binary
static_make_args = [
"make",
"-C",
str(valkey_src_dir),
f"-j{cpu_count}",
"BUILD_TLS=no",
"MALLOC=libc",
"valkey-server",
]
# Add LDFLAGS for static linking on Linux
system = platform.system().lower()
if system == "linux":
# Try full static linking on Linux
static_make_args.insert(3, "LDFLAGS=-static")
self.announce("Attempting full static linking on Linux...", level=3)
result = subprocess.run(static_make_args, capture_output=True, text=True)
if result.returncode != 0:
# If static linking failed, try without -static flag
if system == "linux" and "-static" in " ".join(static_make_args):
self.announce("Full static linking failed, trying dynamic linking...", level=3)
static_make_args = [arg for arg in static_make_args if arg != "LDFLAGS=-static"]
result = subprocess.run(static_make_args, capture_output=True, text=True)
if result.returncode != 0:
self.warn(f"Make stdout: {result.stdout}")
self.warn(f"Make stderr: {result.stderr}")
raise RuntimeError(f"Failed to compile Valkey: {result.stderr}")
self.announce("Valkey compilation complete", level=3)
# Determine target platform
system = platform.system().lower()
machine = platform.machine().lower()
# Normalize architecture naming
if machine in ("x86_64", "amd64"):
machine = "x86_64"
elif machine in ("aarch64", "arm64"):
if system == "linux":
machine = "aarch64"
else: # darwin
machine = "arm64"
else:
raise RuntimeError(
f"Unsupported architecture: {machine}. Supported: x86_64, aarch64, arm64"
)
if system not in ("linux", "darwin"):
raise RuntimeError(f"Unsupported operating system: {system}. Supported: Linux, macOS")
# Copy binary to package
binary_src = valkey_src_dir / "src" / "valkey-server"
target_dir = Path("src") / "valkeylite" / "_binaries" / f"{system}-{machine}"
target_dir.mkdir(parents=True, exist_ok=True)
binary_dst = target_dir / "valkey-server"
self.announce(f"Copying binary to {binary_dst}...", level=3)
shutil.copy2(binary_src, binary_dst)
# Strip debug symbols to reduce size
if shutil.which("strip"):
self.announce("Stripping debug symbols...", level=3)
try:
subprocess.run(["strip", str(binary_dst)], check=True)
self.announce("Binary stripped successfully", level=3)
except subprocess.CalledProcessError:
self.warn("Failed to strip binary (non-fatal)")
# Set executable permissions
binary_dst.chmod(0o755)
# Get binary size
size_mb = binary_dst.stat().st_size / (1024 * 1024)
self.announce(f"Valkey binary ready: {binary_dst} ({size_mb:.1f} MB)", level=3)
# Check if binary is statically linked (Linux only)
if system == "linux":
self._check_binary_linking(binary_dst)
def _check_binary_linking(self, binary_path):
"""Check if the binary is statically or dynamically linked."""
try:
result = subprocess.run(
["ldd", str(binary_path)],
capture_output=True,
text=True,
)
if result.returncode == 0:
# ldd succeeded, binary is dynamically linked
self.announce("Binary is dynamically linked. Dependencies:", level=2)
for line in result.stdout.split("\n"):
if line.strip():
self.announce(f" {line}", level=2)
self.announce(
"Note: For manylinux compatibility, you should run auditwheel:", level=2
)
self.announce(" auditwheel repair dist/*.whl", level=2)
else:
# ldd failed, likely statically linked
self.announce("Binary appears to be statically linked (good!)", level=3)
except FileNotFoundError:
# ldd not available
pass
# Run setup
if __name__ == "__main__":
setup(
cmdclass={
"build_py": BuildValkeyCommand,
"bdist_wheel": BdistWheelCommand,
}
)