-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·78 lines (60 loc) · 2.22 KB
/
build.py
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
#!/usr/bin/env python3
# Description: Build script for the project
import subprocess
import os
GREEN = "\u001b[32m"
RED = "\u001b[31m"
YELLOW = "\u001b[33m"
CLEAR = "\u001b[0m"
ENV = os.environ.copy()
SCRIPT_ENV_VARS = []
def run(command, output: bool = False, include_env: bool = False):
result = subprocess.run(command, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=ENV if include_env else None)
# check if the command was successful
if result.returncode != 0:
raise Exception(f"{RED}\nBUILD - Command failed: {' '.join(command)}{CLEAR}\nReason:\n{result.stdout.decode('utf-8')}")
if output:
print(GREEN + f"BUILD - Command output:\n{CLEAR}{result.stdout.decode('utf-8')}")
def set_env_var(name: str, value: str):
ENV[name] = value
SCRIPT_ENV_VARS.append(name)
def main():
# set environment variables
set_env_var("CC", "clang")
set_env_var("CXX", "clang++")
def meson():
print(f"{GREEN}BUILD - Environment variables:{CLEAR}")
for env_var in SCRIPT_ENV_VARS: print(f" - {env_var}={ENV[env_var]}")
# if build directory exists, delete it
if os.path.exists("build"):
run(["rm", "-rf", "build"])
command = ["meson", "setup", "build"]
print(f"{GREEN}BUILD - Meson command: {CLEAR}" + " ".join(command))
run(command, output=True, include_env=True)
def ninja():
command = ["ninja", "-C", "build"]
print(f"{GREEN}BUILD - Ninja command: {CLEAR}" + " ".join(command))
run(command, output=True)
def clean():
print(f"{GREEN}BUILD - Cleaning build directory{CLEAR}")
run(["rm", "-rf", "build"])
import argparse
parser = argparse.ArgumentParser(description="Build script for the project")
parser.add_argument("-m", "--meson", action="store_true", help="Run meson")
parser.add_argument("-n", "--ninja", action="store_true", help="Run ninja")
parser.add_argument("-c", "--clean", action="store_true", help="Clean build directory")
args = parser.parse_args()
# validate arguments
if not args.meson and not args.ninja and not args.clean:
print(f"{RED}BUILD - No arguments provided{CLEAR}")
parser.print_help()
exit(1)
if args.meson:
meson()
if args.ninja:
ninja()
if args.clean:
clean()
print(f"{YELLOW}BUILD - BUILD SCRIPT FINISHED{CLEAR}")
if __name__ == "__main__":
main()