forked from olilarkin/skia-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-libuv.py
More file actions
238 lines (185 loc) · 7.32 KB
/
build-libuv.py
File metadata and controls
238 lines (185 loc) · 7.32 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
#!/usr/bin/env python3
"""
Build libuv static library for multiple platforms.
libuv is a multi-platform support library with a focus on asynchronous I/O.
Used by Node.js and other projects for event loop and async operations.
Source: https://github.com/libuv/libuv
"""
import argparse
import os
import shutil
import subprocess
import sys
import urllib.request
import zipfile
from pathlib import Path
LIBUV_VERSION = "1.51.0"
LIBUV_URL = f"https://github.com/libuv/libuv/archive/refs/tags/v{LIBUV_VERSION}.zip"
def parse_args():
parser = argparse.ArgumentParser(description="Build libuv static library")
parser.add_argument("platform", choices=["mac", "linux", "win"], help="Target platform")
parser.add_argument("-archs", help="Target architectures (comma separated)", default="x64")
parser.add_argument("-config", choices=["Release", "Debug"], default="Release")
parser.add_argument("-out", help="Output directory", default="build")
parser.add_argument("-version", help="libuv version", default=LIBUV_VERSION)
return parser.parse_args()
def run_command(cmd, cwd=None, env=None, shell=False):
print(f"Running: {cmd if shell else ' '.join(cmd)}")
try:
subprocess.check_call(cmd, cwd=cwd, env=env, shell=shell)
except subprocess.CalledProcessError as e:
print(f"Command failed with exit code {e.returncode}")
sys.exit(1)
def download_libuv(version, dest_dir):
"""Download and extract libuv source."""
url = f"https://github.com/libuv/libuv/archive/refs/tags/v{version}.zip"
zip_path = dest_dir / f"libuv-{version}.zip"
extract_dir = dest_dir / f"libuv-{version}"
if extract_dir.exists():
print(f"libuv source already exists at {extract_dir}")
return extract_dir
dest_dir.mkdir(parents=True, exist_ok=True)
print(f"Downloading libuv v{version}...")
urllib.request.urlretrieve(url, zip_path)
print(f"Extracting to {dest_dir}...")
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(dest_dir)
zip_path.unlink() # Remove zip after extraction
return extract_dir
def get_cmake_arch_flags(platform, arch):
"""Get CMake flags for target architecture."""
flags = []
if platform == "mac":
if arch == "arm64":
flags.extend(["-DCMAKE_OSX_ARCHITECTURES=arm64"])
elif arch in ("x64", "x86_64"):
flags.extend(["-DCMAKE_OSX_ARCHITECTURES=x86_64"])
# Set minimum macOS version
flags.extend(["-DCMAKE_OSX_DEPLOYMENT_TARGET=10.15"])
elif platform == "win":
# Visual Studio generator handles arch via -A flag
if arch in ("x64", "x86_64"):
flags.extend(["-A", "x64"])
elif arch == "x86":
flags.extend(["-A", "Win32"])
# Linux x64 is the default, no special flags needed
return flags
def build_libuv(source_dir, build_dir, platform, arch, config):
"""Build libuv using CMake."""
cmake_build_dir = build_dir / f"cmake-build-{platform}-{arch}"
cmake_build_dir.mkdir(parents=True, exist_ok=True)
# Base CMake arguments
cmake_args = [
"cmake",
str(source_dir),
f"-DCMAKE_BUILD_TYPE={config}",
"-DLIBUV_BUILD_TESTS=OFF",
"-DLIBUV_BUILD_BENCH=OFF",
"-DBUILD_TESTING=OFF",
]
# Platform-specific flags
if platform == "win":
# Use static CRT (/MT) for consistency with other libraries
cmake_args.extend([
"-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded",
"-DCMAKE_POLICY_DEFAULT_CMP0091=NEW",
])
else:
# Use Ninja on Unix for faster builds
cmake_args.extend(["-G", "Ninja"])
# Position independent code for static library
cmake_args.append("-DCMAKE_POSITION_INDEPENDENT_CODE=ON")
# Architecture-specific flags
cmake_args.extend(get_cmake_arch_flags(platform, arch))
# Configure
run_command(cmake_args, cwd=cmake_build_dir)
# Build
build_cmd = ["cmake", "--build", ".", "--config", config]
if platform != "win":
build_cmd.extend(["--parallel"])
run_command(build_cmd, cwd=cmake_build_dir)
return cmake_build_dir
def copy_outputs(cmake_build_dir, output_dir, platform, arch, config):
"""Copy built library and headers to output directory."""
# Determine library name and location
if platform == "win":
lib_name = "uv_a.lib" # libuv static lib on Windows
lib_src = cmake_build_dir / config / lib_name
if not lib_src.exists():
# Try without config subdirectory
lib_src = cmake_build_dir / lib_name
lib_dest_name = "libuv.lib"
else:
lib_name = "libuv_a.a" # libuv static lib on Unix
lib_src = cmake_build_dir / lib_name
if not lib_src.exists():
lib_src = cmake_build_dir / "libuv.a"
lib_dest_name = "libuv.a"
if not lib_src.exists():
# Search for the library
print(f"Looking for library in {cmake_build_dir}...")
for f in cmake_build_dir.rglob("*.a" if platform != "win" else "*.lib"):
print(f" Found: {f}")
if "uv" in f.name.lower():
lib_src = f
break
if not lib_src.exists():
print(f"Error: Could not find built library")
print(f"Searched in: {cmake_build_dir}")
sys.exit(1)
# Create output directories
lib_dir = output_dir / f"libuv-{platform}" / "lib"
if platform == "mac":
lib_dir = lib_dir / arch
lib_dir.mkdir(parents=True, exist_ok=True)
include_dir = output_dir / "include"
include_dir.mkdir(parents=True, exist_ok=True)
# Copy library
lib_dest = lib_dir / lib_dest_name
print(f"Copying {lib_src} -> {lib_dest}")
shutil.copy2(lib_src, lib_dest)
return lib_dest
def copy_headers(source_dir, output_dir):
"""Copy libuv headers."""
include_src = source_dir / "include"
include_dest = output_dir / "include"
include_dest.mkdir(parents=True, exist_ok=True)
# Copy all headers
for header in include_src.glob("*.h"):
dest = include_dest / header.name
print(f"Copying header: {header.name}")
shutil.copy2(header, dest)
# Copy uv/ subdirectory if it exists
uv_subdir = include_src / "uv"
if uv_subdir.exists():
uv_dest = include_dest / "uv"
if uv_dest.exists():
shutil.rmtree(uv_dest)
shutil.copytree(uv_subdir, uv_dest)
print(f"Copied uv/ header directory")
def main():
args = parse_args()
root_dir = Path(__file__).parent.absolute()
third_party_dir = root_dir / "third_party"
build_dir = Path(args.out).absolute()
# Download libuv source
source_dir = download_libuv(args.version, third_party_dir)
archs = [a.strip() for a in args.archs.split(",")]
for arch in archs:
print(f"\n{'='*60}")
print(f"Building libuv for {args.platform} {arch} ({args.config})")
print(f"{'='*60}\n")
# Build
cmake_build_dir = build_libuv(
source_dir, build_dir, args.platform, arch, args.config
)
# Copy outputs
copy_outputs(cmake_build_dir, build_dir, args.platform, arch, args.config)
# Copy headers (only once, not per-arch)
copy_headers(source_dir, build_dir)
print(f"\n{'='*60}")
print("Build complete!")
print(f"Output: {build_dir}")
print(f"{'='*60}\n")
if __name__ == "__main__":
main()