Skip to content

Commit 43200e5

Browse files
build: add build script in Python
1 parent 7c27a65 commit 43200e5

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

scripts/build.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#! /usr/bin/python
2+
from sys import argv
3+
from os.path import basename
4+
from lib import (
5+
cout, BuildOptions, die, StatusCode, working_dir,
6+
change_dir_to, cerr, create_dir, BuildExtraOptions
7+
)
8+
from pathlib import Path
9+
from subprocess import run
10+
11+
def show_help() -> None:
12+
cout((
13+
f"[?] Usage: {basename(__file__)} [Build Option]\n"
14+
f"[+] Available Build Option {(" | ".join(BuildOptions.__members__))}"
15+
))
16+
17+
def build_from_mode(mode: str, build_option: str,
18+
refesh_cache: bool, verbose: bool) -> None:
19+
scripts_dir = "scripts"
20+
current_dir = working_dir()
21+
22+
if current_dir == scripts_dir:
23+
try:
24+
change_dir_to("..")
25+
26+
except Exception as error:
27+
cerr(f"Cannot change directory to '{scripts_dir}', error: {error}")
28+
die(StatusCode.CHANGE_DIR_FAILED)
29+
30+
build_path = Path("build")
31+
if not build_path.exists():
32+
try:
33+
create_dir(build_path)
34+
35+
except Exception as error:
36+
cerr(f"Create '{build_path}' failed, error: {error}")
37+
die(StatusCode.CREATE_DIR_FAILED)
38+
39+
try:
40+
change_dir_to(build_path)
41+
except Exception as error:
42+
cerr(f"Cannot change directory to '{scripts_dir}', error: {error}")
43+
die(StatusCode.CHANGE_DIR_FAILED)
44+
45+
if not Path("CMakeCache.txt").exists() or refesh_cache:
46+
try:
47+
48+
run(["cmake", f"-D{build_option}=ON", ".."])
49+
50+
except Exception as error:
51+
cerr(f"Failed to build from CMake, with error: {error}")
52+
die(StatusCode.SUBPROCESS_FAILED)
53+
54+
try:
55+
build_args = ["cmake", "--build", ".", f"--config", mode]
56+
if verbose:
57+
build_args.append("--verbose")
58+
59+
run(build_args)
60+
61+
except Exception as error:
62+
cerr(f"Failed to build from CMake, with error: {error}")
63+
die(StatusCode.SUBPROCESS_FAILED)
64+
65+
def parse_from_cli() -> None:
66+
if len(argv) < 2:
67+
show_help()
68+
die(StatusCode.MISSING_ARGUMENT)
69+
70+
if argv[1].upper() not in BuildOptions.__members__:
71+
show_help()
72+
die(StatusCode.ARGUMENT_NOT_FOUND)
73+
74+
refesh_cache = False
75+
verbose = False
76+
if len(argv) > 2:
77+
match argv[2]:
78+
case BuildExtraOptions.REFESH_CACHE:
79+
refesh_cache = True
80+
81+
case BuildExtraOptions.VERBOSE:
82+
verbose = True
83+
84+
case _:
85+
cout((
86+
f"[+] Extra argument not found: {argv[2]}\n"
87+
f"[+] Available argument(s):\n"
88+
f"[+] {(" | ".join(BuildExtraOptions.__members__.values()))}"
89+
))
90+
die(StatusCode.ARGUMENT_NOT_FOUND)
91+
92+
build_mode = "Debug" if argv[1].upper() == BuildOptions.WHEEL_DEBUG else "Release"
93+
build_from_mode(build_mode, argv[1], refesh_cache, verbose)
94+
95+
parse_from_cli()

0 commit comments

Comments
 (0)