-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild.py
More file actions
328 lines (269 loc) · 10.4 KB
/
build.py
File metadata and controls
328 lines (269 loc) · 10.4 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
#!/usr/bin/env python3
import argparse
import os
import sys
import subprocess
import platform
import shutil
from pathlib import Path
# Add script directory to sys.path to allow importing from scripts/
script_dir = Path(__file__).parent.resolve()
sys.path.append(str(script_dir))
from scripts.utils.msvc import get_msvc_env
def log(message, level="info"):
colors = {
"info": "\033[96m", # Cyan
"success": "\033[92m", # Green
"warning": "\033[93m", # Yellow
"error": "\033[91m", # Red
"reset": "\033[0m",
}
# Fallback for Windows cmd if ANSI not supported (though Win10+ supports it)
if platform.system() == "Windows" and not os.environ.get("WT_SESSION"):
# Simple check, might not cover all cases but good enough
pass
print(f"{colors.get(level, colors['reset'])}{message}{colors['reset']}")
def run_command(cmd, env=None, cwd=None, check=True, exit_on_error=True):
cmd_str = " ".join(cmd)
log(f"Executing: {cmd_str}", "info")
try:
return subprocess.run(cmd, env=env, cwd=cwd, check=check)
except subprocess.CalledProcessError as e:
if exit_on_error:
log(f"Command failed with exit code {e.returncode}", "error")
sys.exit(e.returncode)
raise e
def get_cmake_preset(config, os_name):
"""
Determine the CMake preset based on configuration and OS.
Using naming convention: [os/compiler]-[arch]-[config]
e.g., msvc-x64-debug, linux-x64-release
"""
config = config.lower()
if os_name == "Windows":
return f"msvc-x64-{config}"
elif os_name == "Linux":
return f"linux-{config}"
elif os_name == "Darwin":
return f"macos-{config}"
else:
return f"default-{config}"
def run_configure(cmake_exe, preset, env, project_root, extra_args=None):
log("\n=== Action: configure ===", "info")
cmd = [cmake_exe, "--preset", preset]
if extra_args:
cmd.extend(extra_args)
run_command(cmd, env=env, cwd=project_root)
def ensure_configured(cmake_exe, preset, env, project_root, extra_args=None):
build_dir_name = preset
if (
platform.system() == "Linux"
and preset.startswith("linux-")
and "x64" not in preset
):
build_dir_name = preset.replace("linux-", "linux-x64-")
build_dir = project_root / "build" / build_dir_name
if not (build_dir / "CMakeCache.txt").exists():
log(
f"Build directory {build_dir} not configured. Running configure...",
"warning",
)
run_configure(cmake_exe, preset, env, project_root, extra_args)
def run_build(cmake_exe, preset, target, jobs, env, project_root):
log("\n=== Action: build ===", "info")
cmd = [cmake_exe, "--build", "--preset", preset]
if jobs:
cmd.extend(["--parallel", str(jobs)])
if target != "all":
cmd.extend(["--target", target])
try:
run_command(cmd, env=env, cwd=project_root)
except subprocess.CalledProcessError:
log("\nBuild failed!", "error")
# Check if it might be due to missing configuration
build_dir_name = preset # Simplified check, actual build dir depends on preset
log(
"Hint: If the build directory does not exist, run with '--action configure' first.",
"warning",
)
sys.exit(1)
def run_test(ctest_exe, preset, regex, label, env, project_root, build_dir):
log("\n=== Action: test ===", "info")
if label == "e2e":
exe_name = (
"FaceFusionCpp.exe" if platform.system() == "Windows" else "FaceFusionCpp"
)
# Determine the bin directory based on the preset name (which corresponds to the build/bin/{preset} structure)
bin_dir_name = build_dir.name
executable = project_root / "build" / "bin" / bin_dir_name / exe_name
e2e_script = project_root / "tests" / "e2e" / "scripts" / "run_e2e.py"
if not executable.exists():
log(f"Executable not found at {executable}. Please build first.", "error")
sys.exit(1)
cmd = [sys.executable, str(e2e_script), "--executable", str(executable)]
if regex:
cmd.extend(["--filter", regex])
run_command(cmd, env=env, cwd=project_root)
return
cmd = [ctest_exe, "--preset", preset, "--no-tests=error"]
# Determine test filter
test_filter = None
if regex:
test_filter = regex
if regex:
cmd.extend(["-R", regex])
if label:
cmd.extend(["-L", label])
if not test_filter:
test_filter = f"label:{label}"
try:
run_command(cmd, env=env, cwd=project_root, exit_on_error=False)
except subprocess.CalledProcessError as e:
filter_msg = f" '{test_filter}'" if test_filter else ""
if e.returncode == 8:
log(f"\nNo tests matched the pattern{filter_msg}.", "warning")
log("Check if the test target is correctly registered and named.", "info")
else:
log(f"Tests failed with exit code {e.returncode}", "error")
sys.exit(e.returncode)
def run_install(cmake_exe, build_dir, env, project_root):
log("\n=== Action: install ===", "info")
cmd = [cmake_exe, "--install", str(build_dir)]
run_command(cmd, env=env, cwd=project_root)
def run_package(cpack_exe, build_dir, env):
log("\n=== Action: package ===", "info")
cpack_config = build_dir / "CPackConfig.cmake"
if cpack_config.exists():
cmd = [cpack_exe, "--config", str(cpack_config), "-V"]
run_command(cmd, env=env, cwd=build_dir)
else:
log(f"CPackConfig.cmake not found in {build_dir}", "error")
sys.exit(1)
def main():
parser = argparse.ArgumentParser(
description="Cross-platform build script for FaceFusionCpp"
)
parser.add_argument(
"--config",
choices=["Debug", "Release"],
default="Debug",
help="Build configuration",
)
parser.add_argument("--target", default="all", help="Build target")
parser.add_argument(
"--action",
choices=["configure", "build", "test", "install", "package"],
default="build",
help="Action to perform (default: build)",
)
parser.add_argument("--preset", help="Override CMake preset")
parser.add_argument(
"--clean", action="store_true", help="Clean build directory before starting"
)
parser.add_argument(
"--test-regex",
help="Regex for tests to run (passed to ctest -R). If specified, --target is ignored for testing.",
)
parser.add_argument(
"--test-label",
help="Label for tests to run (passed to ctest -L).",
)
parser.add_argument(
"--no-build",
action="store_true",
help="Skip the build step when action is test",
)
parser.add_argument(
"-j",
"--jobs",
type=int,
help="Number of parallel build jobs",
)
parser.add_argument(
"--enable-tests",
action="store_true",
help="Enable building tests (activates 'test' feature in vcpkg and BUILD_TESTING=ON)",
)
parser.add_argument(
"--enable-gpu",
action="store_true",
help="Enable GPU support (activates FACEFUSION_ENABLE_GPU=ON)",
)
args = parser.parse_args()
project_root = script_dir
os_name = platform.system()
# 1. Environment Setup
env = os.environ.copy()
if os_name == "Windows":
log("Detecting MSVC environment...", "info")
env = get_msvc_env()
# Determine parallel jobs
jobs = args.jobs
if jobs is None:
# Default to max available cores
jobs = os.cpu_count() or 1
log(f"Parallel jobs: {jobs}", "info")
# 2. Determine Preset
preset = args.preset if args.preset else get_cmake_preset(args.config, os_name)
log(f"Configuration: {args.config}", "info")
log(f"Preset: {preset}", "info")
# 3. Clean if requested
# We need to guess the build dir path based on preset convention
# This might be fragile if preset defines a different binaryDir, but common convention holds.
build_dir_name = preset
if (
platform.system() == "Linux"
and preset.startswith("linux-")
and "x64" not in preset
):
# Fix for linux-debug -> linux-x64-debug convention in CMakePresets.json
build_dir_name = preset.replace("linux-", "linux-x64-")
build_dir = project_root / "build" / build_dir_name
if args.clean and build_dir.exists():
log(f"Cleaning build directory: {build_dir}", "warning")
shutil.rmtree(build_dir)
# 4. Actions
cmake_exe = "cmake"
ctest_exe = "ctest"
cpack_exe = "cpack"
# Determine CMake arguments
extra_cmake_args = []
if args.enable_tests or args.action == "test":
extra_cmake_args.extend(
["-DVCPKG_MANIFEST_FEATURES=test", "-DBUILD_TESTING=ON"]
)
else:
extra_cmake_args.append("-DBUILD_TESTING=OFF")
if args.enable_gpu:
extra_cmake_args.append("-DFACEFUSION_ENABLE_GPU=ON")
# Action Dispatcher
if args.action == "configure":
run_configure(cmake_exe, preset, env, project_root, extra_cmake_args)
elif args.action == "build":
ensure_configured(cmake_exe, preset, env, project_root, extra_cmake_args)
run_build(cmake_exe, preset, args.target, jobs, env, project_root)
elif args.action == "test":
if not args.no_build:
ensure_configured(cmake_exe, preset, env, project_root, extra_cmake_args)
run_build(cmake_exe, preset, args.target, jobs, env, project_root)
else:
log("Skipping build step as requested.", "info")
# Determine regex for test
regex = args.test_regex
if not regex and args.target != "all":
regex = args.target
run_test(
ctest_exe, preset, regex, args.test_label, env, project_root, build_dir
)
elif args.action == "install":
ensure_configured(cmake_exe, preset, env, project_root, extra_cmake_args)
run_install(cmake_exe, build_dir, env, project_root)
elif args.action == "package":
# Ensure configured and built before packaging
ensure_configured(cmake_exe, preset, env, project_root, extra_cmake_args)
log("Running build before packaging...", "info")
run_build(cmake_exe, preset, "all", jobs, env, project_root)
run_package(cpack_exe, build_dir, env)
log("\nOperation completed successfully!", "success")
if __name__ == "__main__":
main()