This repository was archived by the owner on Oct 12, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmake.py
More file actions
382 lines (281 loc) · 8.04 KB
/
Copy pathmake.py
File metadata and controls
382 lines (281 loc) · 8.04 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/env python3
import argparse
import os
import platform
import shlex
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent
CONFIG_FILE = ROOT / "profiles.toml"
class Colors:
RESET = "\033[0m"
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
def color_enabled():
if os.environ.get("NO_COLOR") is not None:
return False
if os.environ.get("FORCE_COLOR") is not None:
return os.environ["FORCE_COLOR"] != "0"
return sys.stdout.isatty()
USE_COLOR = color_enabled()
def log(level: str, message: str):
colors = {
"info": Colors.BLUE,
"error": Colors.RED,
}
label = level
if USE_COLOR:
label = f"{colors.get(level, '')}{level}{Colors.RESET}"
print(f"{label}: {message}")
def info(message: str):
log("info", message)
def error(message: str):
log("error", message)
def load_profiles():
try:
import tomllib
except ImportError:
import tomli as tomllib
try:
with CONFIG_FILE.open("rb") as file:
return tomllib.load(file)
except FileNotFoundError:
error(f"configuration file not found: {CONFIG_FILE}")
sys.exit(1)
except tomllib.TOMLDecodeError as exc:
error(f"invalid configuration in {CONFIG_FILE}: {exc}")
sys.exit(1)
CONFIG = load_profiles()
def run(command: list[str], *, cwd: Path = ROOT):
formatted = " ".join(shlex.quote(str(arg)) for arg in command)
info(f"running: {formatted}")
try:
subprocess.run(
command,
cwd=cwd,
check=True,
)
except FileNotFoundError:
error(f"command not found: {command[0]}")
sys.exit(1)
except subprocess.CalledProcessError as exc:
error(f"command failed with exit code {exc.returncode}")
sys.exit(exc.returncode)
def require_command(command: str):
if shutil.which(command) is None:
error(f"required command not found: {command}")
sys.exit(1)
def default_toolchain():
match platform.system():
case "Windows":
return "mingw"
case "Linux":
return "gcc"
case "Darwin":
return "clang"
case system:
error(f"unsupported host platform: {system}")
sys.exit(1)
def python_executable():
if sys.platform == "win32":
return VENV_DIR / "Scripts" / "python.exe"
return VENV_DIR / "bin" / "python"
def build_directory():
return ROOT / "build" / f"cmake-build-{PROFILE_NAME}-{TOOLCHAIN_NAME}"
def is_multi_config_generator(generator: str) -> bool:
if not generator:
return False
return generator.startswith("Visual Studio") or generator == "Xcode"
def validate_configuration():
toolchains = CONFIG.get("toolchains", {})
profiles = CONFIG.get("profiles", {})
if TOOLCHAIN_NAME not in toolchains:
error(f"unknown toolchain: {TOOLCHAIN_NAME}")
error(f"available toolchains: {', '.join(sorted(toolchains))}")
sys.exit(1)
if PROFILE_NAME not in profiles:
error(f"unknown profile: {PROFILE_NAME}")
error(f"available profiles: {', '.join(sorted(profiles))}")
sys.exit(1)
toolchain = toolchains[TOOLCHAIN_NAME]
profile = profiles[PROFILE_NAME]
build_type = profile.get("build_type")
if not build_type:
error(f"profile '{PROFILE_NAME}' has no 'build_type'")
sys.exit(1)
generator = toolchain.get("generator")
if generator is not None and not isinstance(generator, str):
error(f"invalid generator for toolchain '{TOOLCHAIN_NAME}'")
sys.exit(1)
def cmake_configure_command():
generator = TOOLCHAIN.get("generator")
command = [
"cmake",
"-S",
str(ROOT),
"-B",
str(build_directory()),
]
if generator:
command.extend(["-G", generator])
if not is_multi_config_generator(generator):
command.append(f"-DCMAKE_BUILD_TYPE={PROFILE['build_type']}")
return command
def cmake_build_command():
generator = TOOLCHAIN.get("generator")
command = [
"cmake",
"--build",
str(build_directory()),
]
if is_multi_config_generator(generator):
command.extend(
[
"--config",
PROFILE["build_type"],
]
)
if FORCE_BUILD:
command.append("--clean-first")
return command
def subcommand_setup():
require_command("cmake")
build_directory().mkdir(parents=True, exist_ok=True)
venv = python_executable()
if not venv.exists():
info(f"creating virtual environment: {VENV_DIR}")
run(
[
sys.executable,
"-m",
"venv",
str(VENV_DIR),
]
)
else:
info(f"using existing virtual environment: {VENV_DIR}")
if not venv.exists():
error(f"virtual environment was not created: {venv}")
sys.exit(1)
run(
[
str(venv),
"-m",
"pip",
"install",
"--upgrade",
"pip",
]
)
run(
[
str(venv),
"-m",
"pip",
"install",
"-r",
str(ROOT / "requirements.dev.txt"),
]
)
run(cmake_configure_command())
def subcommand_build():
require_command("cmake")
if not build_directory().exists():
error(f"build directory does not exist: {build_directory()}")
error("run the 'setup' command first")
sys.exit(1)
run(cmake_build_command())
def subcommand_test():
venv = python_executable()
if not venv.exists():
error(f"virtual environment not found: {VENV_DIR}")
error("run the 'setup' command first")
sys.exit(1)
if not build_directory().exists():
error(f"build directory does not exist: {build_directory()}")
error("run the 'setup' command first")
sys.exit(1)
run(
[
str(venv),
"-m",
"pytest",
f"--build-dir={build_directory()}",
"-v",
]
)
def main():
parser = argparse.ArgumentParser(
prog="make.py",
description="Lambda Discipline build tool",
)
parser.add_argument(
"--venv-dir",
default=ROOT / "venv",
type=Path,
help="Python virtual environment directory",
)
parser.add_argument(
"--toolchain",
default=default_toolchain(),
help="toolchain name",
)
parser.add_argument(
"--profile",
default=CONFIG["defaults"]["profile"],
help="build profile",
)
parser.add_argument(
"--force",
action="store_true",
help="clean before building",
)
subparsers = parser.add_subparsers(
dest="command",
required=True,
)
subparsers.add_parser(
"setup",
help="create the environment and configure CMake",
)
subparsers.add_parser(
"build",
help="build the project",
)
subparsers.add_parser(
"test",
help="run the test suite",
)
args = parser.parse_args()
global VENV_DIR
global FORCE_BUILD
global TOOLCHAIN_NAME
global PROFILE_NAME
global TOOLCHAIN
global PROFILE
VENV_DIR = args.venv_dir.resolve()
FORCE_BUILD = args.force
TOOLCHAIN_NAME = args.toolchain
PROFILE_NAME = args.profile
validate_configuration()
TOOLCHAIN = CONFIG["toolchains"][TOOLCHAIN_NAME]
PROFILE = CONFIG["profiles"][PROFILE_NAME]
info(f"toolchain: {TOOLCHAIN_NAME}")
info(f"profile: {PROFILE_NAME}")
info(f"build directory: {build_directory()}")
info(f"virtual environment: {VENV_DIR}")
match args.command:
case "setup":
subcommand_setup()
case "build":
subcommand_build()
case "test":
subcommand_test()
case _:
pass
if __name__ == "__main__":
main()