-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
326 lines (279 loc) · 10.9 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
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
import argparse
import errno
import os
import shutil
import stat
import subprocess
import sys
ANDROID_NDK_HOME = os.environ.get("ANDROID_NDK_HOME", "C:/Microsoft/AndroidSDK/ndk/27.0.12077973")
ANDROID_TOOLCHAIN_FILE = os.path.join(ANDROID_NDK_HOME, "build/cmake/android.toolchain.cmake")
DAISY_TOOLCHAIN_FILE = "_deps/libdaisy-src/cmake/toolchains/stm32h750xx.cmake"
def parse_args():
parser = argparse.ArgumentParser(
description="build the barelymusician library",
allow_abbrev=False,
)
parser.add_argument(
"-c",
"--config",
choices=["debug", "release", "relwithdebuginfo", "asan", "msan", "tsan"],
default="release",
help="specify the build configuration (defaults to release)",
)
parser.add_argument(
"--linux",
action="store_true",
default=sys.platform.startswith("linux"),
help="build the linux targets (defaults to true on linux)",
)
parser.add_argument(
"--mac",
action="store_true",
default=sys.platform.startswith("darwin"),
help="build the mac targets (defaults to true on mac)",
)
parser.add_argument(
"--windows",
action="store_true",
default=sys.platform.startswith("win"),
help="build the windows targets (defaults to true on windows)",
)
parser.add_argument(
"--android",
action="store_true",
help="build the android targets (requires android_abis and android_min_api to be valid)",
)
parser.add_argument(
"--android_abis",
choices=["armeabi-v7a", "arm64-v8a", "x86", "x86_64"],
nargs="+",
default=["armeabi-v7a", "arm64-v8a", "x86", "x86_64"],
help="specify the abis for the android targets (defaults to all)",
)
parser.add_argument(
"--android_min_api",
type=int,
default=21,
help="specify the minimum api level for the android targets (defaults to 21)",
)
parser.add_argument(
"--daisy",
action="store_true",
help="build and run the daisy program (requires daisy_toolchain_prefix to be valid)",
)
parser.add_argument(
"--daisy_toolchain_prefix",
default="C:/Program Files/DaisyToolchain",
help="specify the daisy toolchain prefix for the daisy program",
)
parser.add_argument(
"--unity",
action="store_true",
help="build the unity native plugins for the selected platforms",
)
parser.add_argument(
"--examples",
action="store_true",
help="build the examples for the selected platforms",
)
parser.add_argument(
"--run_demo",
default=None,
help="specify the examples demo to run (defaults to none)",
)
parser.add_argument(
"--benchmark",
action="store_true",
help="build the benchmarks for the selected platforms",
)
parser.add_argument(
"--benchmark_out",
default="benchmark.json",
help="specify the json output file path for the benchmarks",
)
parser.add_argument(
"--benchmark_compare",
default="benchmark_other.json",
help="specify the other json file path to compare for the benchmarks",
)
parser.add_argument(
"--test",
action="store_true",
help="build and run the unit tests for the selected platforms",
)
parser.add_argument(
"--clean",
action="store_true",
help="clean the previous build before the new build",
)
parser.add_argument(
"--skip_generate",
action="store_true",
help="skip the cmake generate step",
)
parser.add_argument(
"--skip_build",
action="store_true",
help="skip the cmake build step",
)
return parser.parse_args()
def clean(build_dir):
print("Cleaning the previous build...")
def onerror(func, path, excinfo):
if func in (os.unlink, os.rmdir) and excinfo[1].errno == errno.EACCES:
os.chmod(path, stat.S_IWRITE)
func(path)
shutil.rmtree(build_dir, onerror=onerror)
def run_command(command, cwd):
print(command)
subprocess.run(command, check=True, cwd=cwd, shell=True)
def build_platform(args, config, source_dir, build_dir, cmake_options):
if not os.path.exists(build_dir):
os.makedirs(build_dir)
if not args.skip_generate:
generate_command = f'cmake -S "{source_dir}" -B "{build_dir}" {" ".join(cmake_options)}'
run_command(generate_command, build_dir)
if not args.skip_build:
build_command = f'cmake --build "{build_dir}" --config {config}'
if args.run_demo:
build_command += f" --target examples_demo_{args.run_demo}"
run_command(build_command, build_dir)
def get_build_config(args):
if args.config == "release":
return "Release"
else:
return "Debug"
def build(args, source_dir, build_dir):
if args.clean and os.path.exists(build_dir):
clean(build_dir)
if args.skip_generate and args.skip_build:
return
config = get_build_config(args)
if args.daisy:
daisy_build_dir = os.path.join(build_dir, "Daisy")
daisy_cmake_options = [
'-G "Unix Makefiles"',
f'-DCMAKE_TOOLCHAIN_FILE="{DAISY_TOOLCHAIN_FILE}"',
f'-DTOOLCHAIN_PREFIX="{args.daisy_toolchain_prefix}"',
"-DENABLE_DAISY=ON",
]
build_platform(args, config, source_dir, daisy_build_dir, daisy_cmake_options)
common_cmake_options = []
if args.unity:
common_cmake_options.append("-DENABLE_UNITY=ON")
if args.android:
print("Building the Android targets...")
for android_abi in args.android_abis:
android_build_dir = os.path.join(build_dir, "Android", android_abi)
android_cmake_options = [
'-G "Ninja"',
f'-DCMAKE_TOOLCHAIN_FILE="{ANDROID_TOOLCHAIN_FILE}"',
f'-DANDROID_ABI="{android_abi}"',
f'-DANDROID_NDK="{ANDROID_NDK_HOME}"',
f'-DANDROID_PLATFORM="android-{args.android_min_api}"',
f'-DCMAKE_BUILD_TYPE="{config}"',
] + common_cmake_options
build_platform(args, config, source_dir, android_build_dir, android_cmake_options)
if args.config == "asan":
common_cmake_options.append("-DENABLE_ASAN=ON")
elif args.config == "msan":
common_cmake_options.append("-DENABLE_MSAN=ON")
elif args.config == "tsan":
common_cmake_options.append("-DENABLE_TSAN=ON")
if args.benchmark:
common_cmake_options.append("-DENABLE_BENCHMARKS=ON")
if args.test:
common_cmake_options.append("-DENABLE_TESTS=ON -DGTEST_COLOR=1")
if args.examples or args.run_demo:
common_cmake_options.append("-DENABLE_EXAMPLES=ON")
if args.linux:
print("Building the Linux targets...")
linux_build_dir = os.path.join(build_dir, "Linux")
linux_cmake_options = [
'-G "Unix Makefiles"',
f'-DCMAKE_BUILD_TYPE="{config}"',
] + common_cmake_options
build_platform(args, config, source_dir, linux_build_dir, linux_cmake_options)
if args.mac:
print("Building the Mac targets...")
mac_build_dir = os.path.join(build_dir, "Mac")
mac_cmake_options = ['-G "Xcode"'] + common_cmake_options
build_platform(args, config, source_dir, mac_build_dir, mac_cmake_options)
if args.windows:
print("Building the Windows targets...")
windows_build_dir = os.path.join(build_dir, "Windows")
windows_cmake_options = ['-G "Visual Studio 17 2022"'] + common_cmake_options
build_platform(args, config, source_dir, windows_build_dir, windows_cmake_options)
def run_daisy_program(build_dir):
input("Running the Daisy program, press enter to continue...")
daisy_build_dir = os.path.join(build_dir, "Daisy")
daisy_make_command = "make barelymusiciandaisy_program_dfu"
run_command(daisy_make_command, daisy_build_dir)
def run_demo(args, build_dir):
input("Running the demo, press enter to continue...")
for platform in os.listdir(build_dir):
if (
(platform == "Linux" and sys.platform.startswith("linux"))
or (platform == "Mac" and sys.platform.startswith("darwin"))
or (platform == "Windows" and sys.platform.startswith("win"))
):
demo_dir = f"{build_dir}/{platform}/bin"
if platform != "Linux":
demo_dir = os.path.join(demo_dir, f"{get_build_config(args)}")
demo_path = os.path.join(demo_dir, args.run_demo)
if platform == "Windows":
demo_path += ".exe"
run_command(demo_path, demo_dir)
break
def run_tests(build_dir):
ctest_command = "ctest --output-on-failure"
for platform in os.listdir(build_dir):
if (
(platform == "Linux" and sys.platform.startswith("linux"))
or (platform == "Mac" and sys.platform.startswith("darwin"))
or (platform == "Windows" and sys.platform.startswith("win"))
):
run_command(ctest_command, os.path.join(build_dir, platform))
def run_benchmarks(args, build_dir):
for platform in os.listdir(build_dir):
if (
(platform == "Linux" and sys.platform.startswith("linux"))
or (platform == "Mac" and sys.platform.startswith("darwin"))
or (platform == "Windows" and sys.platform.startswith("win"))
):
benchmark_dir = f"{build_dir}/{platform}/src"
if platform != "Linux":
benchmark_dir = os.path.join(benchmark_dir, f"{get_build_config(args)}")
benchmark_path = os.path.join(benchmark_dir, "barelymusician_benchmark")
if platform == "Windows":
benchmark_path += ".exe"
benchmark_command = (
f"{benchmark_path} --benchmark_out={args.benchmark_out} --benchmark_out_format=json"
)
run_command(benchmark_command, benchmark_dir)
if os.path.exists(
os.path.join(benchmark_dir, args.benchmark_compare)
) or os.path.exists(args.benchmark_compare):
compare_path = (
f"{build_dir}/{platform}/_deps/benchmark-src/tools/compare.py benchmarks"
)
compare_command = (
f"python {compare_path} {args.benchmark_compare} {args.benchmark_out}"
)
run_command(compare_command, benchmark_dir)
break
def main():
args = parse_args()
root_dir = os.path.abspath(os.path.dirname(__file__))
build_dir = os.path.join(root_dir, "build")
build(args, root_dir, build_dir)
if args.test:
run_tests(build_dir)
if args.benchmark:
run_benchmarks(args, build_dir)
if args.daisy:
run_daisy_program(build_dir)
if args.run_demo is not None:
run_demo(args, build_dir)
if __name__ == "__main__":
main()