-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
132 lines (107 loc) · 3.48 KB
/
tasks.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
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
import os
import subprocess
import argparse
import platform
import shutil
BUILD_DIR_NAME = "build"
PROJECT_NAME = "voxels"
EXECUTABLE_NAME = "voxels"
COMPILE_DB_NAME = "compile_commands.json"
BUILD_MODES = ["Debug", "Release", "RelWithDebInfo"]
def cmake_generate(mode):
print(f"Running CMake in {mode} mode")
cmake_command = [
"cmake",
"-G",
"Ninja",
'-DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=lld"',
'-DCMAKE_SHARED_LINKER_FLAGS="-fuse-ld=lld"',
f"-DCMAKE_BUILD_TYPE={mode}",
"../..",
]
cmake_process = subprocess.Popen(cmake_command)
cmake_process.wait()
if cmake_process.returncode != 0:
print("CMake configuration failed. Exiting...")
exit(1)
def find_executable_path(directory, executable_name):
for root, _, files in os.walk(directory):
if executable_name in files:
return os.path.join(root, executable_name)
return None
def run(build_dir):
executable_name = EXECUTABLE_NAME
if platform.system() == "Windows":
executable_name += ".exe"
executable_path = find_executable_path(build_dir, executable_name)
if not executable_path:
print("Error finding executable, exiting...")
exit(1)
subprocess.run(executable_path, shell=True)
def cmake_build(mode):
print(f"Building in {mode} mode")
if platform.system() == "Windows":
parallel_flag = "/m:"
else:
parallel_flag = "-j"
cmake_build_command = [
"cmake",
"--build",
".",
"--",
f"{parallel_flag} {str(os.cpu_count() if os.cpu_count() else 1)}",
]
build_process = subprocess.Popen(cmake_build_command)
build_process.wait()
if build_process.returncode != 0:
print("Build failed. Exiting...")
exit(1)
def clean():
for mode in BUILD_MODES:
dir_name = f"build-{mode.lower()}"
if os.path.exists(dir_name):
shutil.rmtree(dir_name)
os.remove(COMPILE_DB_NAME)
def main():
parser = argparse.ArgumentParser(description=f"{PROJECT_NAME} build tools")
parser.add_argument(
"-r", "--run", help="Build and run the program", action="store_true"
)
parser.add_argument("-b", "--build", help="Build the program", action="store_true")
parser.add_argument(
"--mode",
help="CMake build type (default Debug)",
type=str,
choices=BUILD_MODES,
metavar="BUILD_MODE",
default="Debug",
)
parser.add_argument("-c", help="Configure", action="store_true")
parser.add_argument("--clean", help="Remove all build files", action="store_true")
args = parser.parse_args()
if not (args.c or args.clean or args.mode or args.run):
parser.print_usage()
return
if args.clean:
clean()
build_dir = None
if args.c:
build_dir = f"{BUILD_DIR_NAME}/{args.mode}"
os.makedirs(build_dir, exist_ok=True)
os.chdir(build_dir)
cmake_generate(args.mode)
os.chdir("../..")
if args.build or args.run:
build_dir = f"{BUILD_DIR_NAME}/{args.mode}"
os.chdir(build_dir)
cmake_build(args.mode)
# symlink
os.chdir("../..")
if os.path.exists(COMPILE_DB_NAME) or os.path.islink(COMPILE_DB_NAME):
os.remove(COMPILE_DB_NAME)
os.symlink(os.path.join(build_dir, COMPILE_DB_NAME), COMPILE_DB_NAME)
if args.run:
print("Running", build_dir)
run(build_dir)
if __name__ == "__main__":
main()