-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbuild.py
More file actions
142 lines (117 loc) · 3.84 KB
/
build.py
File metadata and controls
142 lines (117 loc) · 3.84 KB
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
#!/usr/bin/env python3
import argparse
import logging
import os
import sys
from multiprocessing import cpu_count
from typing import List, Dict
from yanniszark_common.cmdutils import run
log = logging.getLogger(__name__)
def parse_args():
parser = argparse.ArgumentParser("Kernel build helper")
# Add build subcommand
subparsers = parser.add_subparsers(dest="command")
base_subparser = argparse.ArgumentParser(add_help=False)
base_subparser.add_argument(
"--debug", action="store_true", help="Enable debug mode"
)
base_subparser.add_argument(
"--no-clang",
action="store_true",
default=False,
help="Use clang as the compiler",
)
base_subparser.add_argument(
"--enable-mglru", action="store_true", help="Enable MGLRU"
)
build_parser = subparsers.add_parser(
"build", help="Build the kernel", parents=[base_subparser]
)
install_parser = subparsers.add_parser(
"install", help="Install the kernel", parents=[base_subparser]
)
return parser.parse_args()
def edit_config_file(config_options: Dict[str, str], path=".config"):
for config_opt, action in config_options.items():
if action == "y":
run(["./scripts/config", "-e", config_opt])
elif action == "m":
run(["./scripts/config", "-m", config_opt])
elif action == "n":
run(["./scripts/config", "-d", config_opt])
else:
raise ValueError(f"Invalid action {action} for config option {config_opt}")
def add_default_config_options():
config_options = {
"CONFIG_RANDOMIZE_BASE": "n",
}
edit_config_file(config_options)
def add_debug_config_options():
debug_config_options = {
"CONFIG_DEBUG_KERNEL": "y",
"CONFIG_DEBUG_SLAB": "y",
"CONFIG_DEBUG_PAGEALLOC": "y", # might be too slow
"CONFIG_DEBUG_SPINLOCK": "y",
"CONFIG_DEBUG_SPINLOCK_SLEEP": "y",
"CONFIG_DEBUG_LOCKDEP": "y",
"CONFIG_PROVE_LOCKING": "y",
"CONFIG_LOCK_STAT": "y",
"CONFIG_INIT_DEBUG": "y",
"CONFIG_DEBUG_INFO": "y",
"CONFIG_DEBUG_STACKOVERFLOW": "y",
"CONFIG_DEBUG_STACK_USAGE": "y",
# "CONFIG_DEBUG_KMEMLEAK": "y",
# General opts
"CONFIG_RANDOMIZE_BASE": "n",
}
edit_config_file(debug_config_options)
def add_mglru_config_options(enabled=False):
# CONFIG_LRU_GEN=y
# CONFIG_LRU_GEN_ENABLED=y
option = "y" if enabled else "n"
mglru_config_options = {
"CONFIG_LRU_GEN": option,
"CONFIG_LRU_GEN_ENABLED": option,
}
edit_config_file(mglru_config_options)
def make(args: List[str] = [], env=None, parallel=True, sudo=False):
cmd = ["make"]
if sudo:
cmd = ["sudo"] + cmd
if parallel:
cmd += ["-j", str(cpu_count())]
cmd += args
if not env:
env = os.environ()
run(cmd, env=env)
def build(args, llvm_env):
log.info("Building the kernel")
add_default_config_options()
if args.debug:
add_debug_config_options()
add_mglru_config_options(args.enable_mglru)
make(env=llvm_env)
run(["python3", "./scripts/clang-tools/gen_compile_commands.py"])
def main():
global log
logging.basicConfig(level=logging.INFO)
args = parse_args()
if not args.no_clang:
llvm_envvars = {
"LLVM": "1",
"CC": "ccache clang",
"KBUILD_BUILD_TIMESTAMP": "",
}
else:
llvm_envvars = {}
llvm_env = os.environ.copy()
llvm_env.update(llvm_envvars)
if args.command == "build":
build(args, llvm_env)
elif args.command == "install":
build(args, llvm_env)
log.info("Installing the kernel")
make(["modules_install"], env=llvm_env, sudo=True)
make(["install"], env=llvm_env, sudo=True)
if __name__ == "__main__":
sys.exit(main())