-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
build.py
executable file
·136 lines (103 loc) · 4.61 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
#!/usr/bin/env python3
# @echo 'Generic Targets:'
# @echo ' bl - builds U-Boot and TF-A and copies them to the bl output directory'
# @echo ' tfa - builds Arm Trusted Firmware-A (BL1, BL2, BL31)'
# @echo ' bl33/uboot - builds U-Boot (BL33)'
# @echo ' tools-vm - builds linux/tools/vm'
# TODO: Shallow clone
import os
import argparse
from argparse import ArgumentParser, _HelpAction
from enum import Enum
import subprocess
class Arch(str, Enum):
X86_64 = "x86_64"
ARM64 = "arm64"
def handle_build(args, env):
subprocess.run(["make", args.target], env=env)
def handle_run(args, env):
if args.gdb:
env['GDB'] = '1'
if args.mem:
env['MEM'] = args.mem
if args.cpu:
env['CPU'] = str(args.cpu)
if args.extra_qemu_args:
env["QEMU_EXTRA_ARGS"] = args.extra_qemu_args
if args.extra_kernel_cmdline:
env["QEMU_EXTRA_KERNEL_CMDLINE"] = args.extra_kernel_cmdline
subprocess.run(["make", "run"], env=env)
def handle_clean(args, env):
if args.all:
subprocess.run(["make", "clean"], env=env)
else:
subprocess.run(["make", f"{args.target}_clean"], env=env)
def handle_rootfs(args, env):
print(args)
class HelpAction(argparse.Action):
def __init__(self, option_strings, subparsers, parent_parser, *args, **kwargs):
self.subparsers = subparsers
self.parent_parser = parent_parser
super(HelpAction, self).__init__(option_strings=option_strings, *args, **kwargs)
def __call__(self, parser, namespace, values, option_string=None):
parser.print_help()
f = self.parent_parser._get_formatter()
f.add_arguments(self.parent_parser._actions)
lines = f.format_help().split('\n')
for l in lines:
print(" " + l)
for k, v in self.subparsers.choices.items():
f = v._get_formatter()
f.start_section(f"{k} options")
actions = [a for a in v._actions if a not in self.parent_parser._actions and not isinstance(a, _HelpAction)]
f.add_arguments(actions)
f.end_section()
print(f.format_help())
exit(0)
def parse_args():
parser = ArgumentParser(description='TODO', add_help=False)
subparsers = parser.add_subparsers()
parent_parser = ArgumentParser(add_help=False)
parser.add_argument('-h', '--help', action=HelpAction, subparsers=subparsers, parent_parser=parent_parser, nargs=0)
# TODO: How to add parent_parser defaults?
parent_parser.add_argument('-v', '--verbose', action='store_true')
parent_parser.add_argument('-a', '--arch', type=Arch, choices=[a.value for a in Arch], default=Arch.X86_64.value)
parent_parser.add_argument('--ack', action='store_true')
parent_parser.add_argument('-t', '--target', choices=['linux', 'ack'], default='linux')
build_parser = subparsers.add_parser('build', parents=[parent_parser], help="Build a kernel image")
build_parser.set_defaults(func=handle_build)
# TODO: mutually exclusive
build_parser.add_argument('-c', '--defconfig', action='store_true')
build_parser.add_argument('-m', '--modules', action='store_true')
run_parser = subparsers.add_parser('run', parents=[parent_parser], help="Run a kernel")
run_parser.set_defaults(func=handle_run)
run_parser.add_argument('-g', '--gdb', action='store_true')
run_parser.add_argument('-c', '--cpu', type=int)
run_parser.add_argument('-m', '--mem')
run_parser.add_argument('--rootfs')
run_parser.add_argument('--initramfs')
run_parser.add_argument('--extra-qemu-args')
run_parser.add_argument('--extra-kernel-cmdline')
clean_parser = subparsers.add_parser('clean', parents=[parent_parser], help="Clean built artifacts")
clean_parser.set_defaults(func=handle_clean)
clean_parser.add_argument('--all', action='store_true')
rootfs_parser = subparsers.add_parser('rootfs')
rootfs_parser.set_defaults(func=handle_rootfs)
rootfs_subparser = rootfs_parser.add_subparsers(dest="rootfs_command")
mount_subparser = rootfs_subparser.add_parser('mount')
unmount_subparser = rootfs_subparser.add_parser('unmount')
initramfs_subparser = rootfs_subparser.add_parser('initramfs')
image_subparser = rootfs_subparser.add_parser('image')
return parser.parse_args()
def main():
args = parse_args()
print(args)
env = os.environ.copy()
# env['ARCH'] = args.arch
# if args.verbose:
# env['VERBOSE'] = '1'
# if args.target == "ack":
# env['ACK'] = '1'
args.func(args, env)
if __name__ == '__main__':
main()