-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
executable file
·122 lines (97 loc) · 3.66 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
#! /bin/python3
import argparse
import re
import subprocess
import yaml
class Build:
def __init__(self, config):
self.cc = try_default(config, "cc", "ccache clang")
self.cc_path = try_default(config, "cc_path", "")
self.llvm = try_default(config, "llvm", 1)
self.llvm_ias = try_default(config, "llvm_ias", self.llvm)
self.as_path = try_default(config, "as_path", None)
self.ld_path = try_default(config, "ld_path", None)
self.llvm_version = try_default(config, "llvm_version", 16)
self.gcc_version = try_default(config, "gcc_version", 12)
self.type = try_default(config, "type", "tftp-boot")
self.devkit = try_default(config, "devkit", "lowmem")
self.binutils_path = try_default(config, "binutils_path", "")
self.dryrun = False
def simple_build(self):
command = [
"make",
f'LINUX_CC="CC={self.cc}"',
f"GCC_VERSION={self.gcc_version}",
f"LLVM_VERSION={self.llvm_version}",
f'LINUX_IAS="LLVM_IAS={self.llvm_ias}"',
f"CLANG={self.llvm}",
f"{self.type}",
f"DEVKIT={self.devkit}",
]
if self.ld_path != None:
command.append(f'LINUX_LD="LD={self.ld_path}"')
if self.as_path != None:
command.append(f'LINUX_AS="AS={self.as_path}"')
stdout = []
if self.dry_run == True:
print(command)
return (0, stdout)
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
)
# spit out stdout so the program seems alive
while True:
if process.stdout != None:
output = process.stdout.readline()
stdout.append(output)
print(output.strip())
return_code = process.poll()
if return_code is not None:
# once done, we need to dump out w/e is left
for output in process.stdout.readlines():
stdout.append(output)
print(output.strip())
break
return (process.returncode, stdout)
def dry_run(self):
self.dry_run = True
def try_default(config, key, default):
try:
return config[key]
except KeyError:
return default
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="kbuild helper")
parser.add_argument(
"-c", "--config", help="yaml config file", default="conf/targets.yaml"
)
parser.add_argument("-t", "--target", help="the target config", default="default")
parser.add_argument("--dry-run", help="dry run", default=False, action="store_true")
parser.add_argument("--pattern", help="pattern to match in stderr")
parser.add_argument(
"--linux-dir", help="directory to build linux from", default="/stuff/linux"
)
args = parser.parse_args()
with open(args.config, "r") as stream:
try:
config = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
target_config = config["targets"][args.target]
build = Build(target_config)
if args.dry_run == True:
build.dry_run()
rc, stdout = build.simple_build()
if args.pattern != None:
print(f"pattern matches for {args.pattern}:\n")
lines = len(stdout)
matches = 0
for line in stdout:
match = re.search(args.pattern, line)
if match:
matches = matches + 1
print(line.strip())
print(f"{matches} matches in {lines} lines")