-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.py
65 lines (49 loc) · 1.89 KB
/
script.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
import subprocess
def shell(args, **kwargs):
print("::", args)
return subprocess.run(args, shell=True, check=True, **kwargs)
def command_init(build_type, directory, clang, rest_args="", **kwargs):
args = f"cmake -B {directory} -DCMAKE_BUILD_TYPE={build_type}"
if clang:
args = "CC=clang CXX=clang++ LDFLAGS=-fuse-ld=lld " + args
shell(args + " " + rest_args)
def command_build(directory, target, parallel, rest_args="", **kwargs):
args = f"cmake --build {directory} -j {parallel}"
if target:
args += f" -t {target}"
shell(args + " " + rest_args)
def command_run(directory, no_build, executable, rest_args="", **kwargs):
if not no_build:
command_build(directory, **kwargs)
args = f"{directory}/{executable}" + " " + rest_args
shell(args)
def main_cli():
import sys, os, argparse
rest_args = ""
if sys.argv.count("--"):
i = sys.argv.index("--")
rest_args = " ".join(sys.argv[i + 1:])
sys.argv[i:] = []
parser = argparse.ArgumentParser()
parser.add_argument("command", type=str, choices=["init", "build", "run"])
parser.add_argument("--clang", action="store_true", default=False)
parser.add_argument("--directory", type=str)
parser.add_argument("--build-type", type=str, default="Release")
parser.add_argument("--parallel", type=int, default=(os.cpu_count() or 1))
parser.add_argument("--target", type=str)
parser.add_argument("--no-build", action="store_true", default=False)
parser.add_argument("--executable", type=str, default="main")
args = parser.parse_args()
args.rest_args = rest_args
if args.directory is None:
args.directory = f"build/{args.build_type}"
func = globals()[f"command_{args.command}"]
try:
sys.exit(func(**args.__dict__))
except KeyboardInterrupt:
print("-- KeyboardInterrupt --")
except subprocess.CalledProcessError as e:
print(f"-- {e} --")
sys.exit(1)
if __name__ == "__main__":
main_cli()