-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·340 lines (269 loc) · 9.93 KB
/
Copy pathbuild.py
File metadata and controls
executable file
·340 lines (269 loc) · 9.93 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env python3
from pathlib import Path
import subprocess
import argparse
import tempfile
import shutil
import signal
import time
import yaml
import re
import os
KERNEL_BUILDER_VERSION_FRIENDLY = "1.0"
CONFIG_FILE = "config.yaml" # hardcoded, because it checks the variants available for help
# config
with open(CONFIG_FILE, 'r') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
build_variants = ['all']
build_variants += config['build_variants']
# argparse
epilog = f'kernel-builder {KERNEL_BUILDER_VERSION_FRIENDLY} by reesa <meow@reesa.cc> (https://github.com/itzreesa/kernel-builder)'
parser = argparse.ArgumentParser(
prog='kernel-builder',
description='A tool for building kernels for android devices!',
epilog=epilog
)
parser.add_argument("-d", "--work-dir",
help="change working directory",
default=".",
nargs="?",
type=str)
parser.add_argument("-v", "--verbose",
help="toggle more verbose output",
action="store_true",
default=False,
required=False)
#parser.add_argument("-s", "--silent",
# help="disables all printed output, for scripting",
# action="store_true",
# default=False,
# required=False)
parser.add_argument("-V", "--version",
help="print the version and quit",
action="store_true",
default=False,
required=False)
subparsers = parser.add_subparsers(
title="action",
description="actions",
dest="action",
required=True
)
parser_init = subparsers.add_parser(
"init",
description="downloads all the required tools and the kernel tree",
epilog=epilog
)
parser_init.add_argument("-d", "--dry-run",
help="only print what would happen after using the init action",
action="store_true",
default=False,
required=False)
parser_build = subparsers.add_parser(
"build",
description="builds the kernel",
epilog=epilog
)
parser_build.add_argument("variant",
help="device variant",
choices=build_variants,
type=str)
parser_build.add_argument("-r", "--remake-config",
help="force remake .config file",
action="store_true",
default=False,
required=False)
parser_build.add_argument("-f", "--force-rebuild",
help="force rebuild if out directory exists",
action="store_true",
default=False,
required=False)
parser_clean = subparsers.add_parser(
"clean",
description="cleans a selected variants out directory",
epilog=epilog
)
parser_clean.add_argument("variant",
help="device variant",
choices=build_variants,
type=str)
parser_package = subparsers.add_parser(
"package",
description="packages an AnyKernel3 zip for the variant selected, must be built before",
epilog=epilog
)
parser_package.add_argument("variant",
help="device variant",
choices=build_variants,
type=str)
# functions
def convert_time(t) -> str:
if t >= 60:
mins = t // 60
secs = t - (mins * 60)
return f"{mins}m {secs}s"
return f"{t}s"
def file_re_sub(path, variant):
with open(path.absolute(), "r") as f:
data = f.readlines()
for i, line in enumerate(data):
data[i] = re.sub("DEVICE_REPLACE", variant, line)
with open(path.absolute(), "w") as f:
f.writelines(data)
def validate_config():
if not config["kernel_repo_url"]:
print("kernel_repo_url not specified!")
return 0
if not config["kernel_repo_branch"]:
print("kernel_repo_branch not specified!")
return 0
if not config["clang_repo_url"]:
print("clang_repo_url not specified!")
return 0
if not config["clang_repo_branch"]:
print("clang_repo_branch not specified!")
return 0
def init_workdir(path, verbose, dry_run):
os.chdir(path)
kernel_path = Path("kernel_tree")
clang_path = Path("clang")
anykernel_path = Path("anykernel")
try:
_ = subprocess.run(["git", "-v"], capture_output=True, text=True)
except FileNotFoundError:
print("error: git not found! please install it using your package manager.")
git_location = shutil.which("git")
if verbose:
print(f"> git clone --recurse-submodules {config["kernel_repo_url"]} --depth=1 -b {config["kernel_repo_branch"]} {kernel_path.absolute()}")
subprocess.call([git_location, "clone", "--recurse-submodules", config["kernel_repo_url"], "--depth=1", "-b", config["kernel_repo_branch"], kernel_path.absolute()])
if verbose:
print(f"> git clone {config["clang_repo_url"]} --depth=1 -b {config["clang_repo_branch"]} {clang_path.absolute()}")
subprocess.call([git_location, "clone", config["clang_repo_url"], "--depth=1", "-b", config["clang_repo_branch"], clang_path.absolute()])
if verbose:
print(f"> git clone https://github.com/osm0sis/AnyKernel3 --depth=1 {anykernel_path.absolute()}")
subprocess.call([git_location, "clone", "https://github.com/osm0sis/AnyKernel3", "--depth=1", anykernel_path.absolute()])
print("done!")
def build_helper(path, verbose, variant, remake_config, force_rebuild):
os.chdir(path)
kernel_path = Path("kernel_tree")
if not kernel_path.exists():
print("error: kernel directory not set up properly. use ./build.py init")
return 1
clang_path = Path("clang") / "bin"
if not clang_path.exists():
print("error: clang directory not set up properly. use ./build.py init")
return 1
os.environ["PATH"] = f"{clang_path.absolute()}:{os.environ["PATH"]}"
if verbose:
print(f"> PATH = {os.environ["PATH"]}")
os.chdir(kernel_path)
variants = [variant]
if variant == "all":
variants = config["build_variants"]
make_location = shutil.which("make")
bash_location = shutil.which("bash")
time_start_all = time.time()
for variant in variants:
print(f" === building variant {variant}")
out_folder = config["make_output"].replace("$VARIANT", variant)
cmd_make_base = [make_location, f"O={out_folder}"] + config["make_parameters"]
cmd_make_configs = cmd_make_base + config["kernel_configs"]
for i, p in enumerate(cmd_make_configs):
if "$VARIANT" in p:
cmd_make_configs[i] = p.replace("$VARIANT", variant)
out_folder = Path(out_folder)
if out_folder.exists() and not force_rebuild:
print(f" === build variant {variant} already built")
continue
dot_config = out_folder / ".config"
time_start_variant = time.time()
if not dot_config.exists() or remake_config:
cmd = ' '.join(cmd_make_configs)
if verbose:
print(f"> {cmd}")
try:
p = subprocess.Popen(cmd, shell=True, executable=bash_location, env=os.environ.copy())
p.wait()
except KeyboardInterrupt:
p.send_signal(signal.SIGINT)
print(" === INTERRUPT!")
return 1
cmd = ' '.join(cmd_make_base) + f" -j{config["make_jobs"]}"
if verbose:
print(f"> {cmd}")
try:
p = subprocess.Popen(cmd, shell=True, executable=bash_location, env=os.environ.copy())
p.wait()
except KeyboardInterrupt:
p.send_signal(signal.SIGINT)
print(" === INTERRUPT!")
return 1
time_variant = time.time() - time_start_variant
print(f" === built variant {variant} in {convert_time(time_variant)}")
time_all = time.time() - time_start_all
print(f"done. {convert_time(time_all)}")
def clean_workdir(path, variant):
os.chdir(path)
kernel_path = Path("kernel_tree")
if not kernel_path.exists():
print("error: kernel directory not set up properly. use ./build.py init")
return 1
os.chdir(kernel_path)
variants = [variant]
if variant == "all":
variants = config["build_variants"]
for variant in variants:
out_folder = config["make_output"].replace("$VARIANT", variant)
variant_out_path = Path(out_folder)
if not variant_out_path.exists():
print(f"warn: out folder for variant {variant} does not exist.")
continue
shutil.rmtree(variant_out_path.absolute())
print("cleaned.")
def package_kernel(path, verbose, variant):
anykernel_script = Path(__file__).parent / "anykernel.sh"
os.chdir(path)
kernel_path = Path("kernel_tree")
anykernel_path = Path("anykernel")
variants = [variant]
if variant == "all":
variants = config["build_variants"]
for variant in variants:
with tempfile.TemporaryDirectory() as wd:
twd = Path(wd)
paths_to_copy = ["META-INF", "tools", "LICENSE"]
for p in paths_to_copy:
to_copy = anykernel_path / p
to_copy.copy_into(twd)
anykernel_script.copy_into(twd)
anykernel_script_place = Path(twd) / "anykernel.sh"
file_re_sub(anykernel_script_place, variant)
out_folder = config["make_output"].replace("$VARIANT", variant)
image_path = kernel_path / out_folder
image_path = Path(image_path).joinpath(config["image_path"])
image_path.copy_into(twd)
shutil.make_archive(f"kernel-{variant}", "zip", twd)
print(f"packaged: kernel-{variant}.zip")
def main():
args = parser.parse_args()
if args.version:
print(KERNEL_BUILDER_VERSION_FRIENDLY)
exit(0)
print(args)
work_dir = Path(args.work_dir)
if not work_dir.exists():
work_dir.mkdir(parents=True, exist_ok=True)
if not work_dir.is_dir():
print("error: work dir path is not a directory!")
validate_config()
match args.action:
case "init":
init_workdir(work_dir, args.verbose, args.dry_run)
case "build":
build_helper(work_dir, args.verbose, args.variant, args.remake_config, args.force_rebuild)
case "clean":
clean_workdir(work_dir, args.variant)
case "package":
package_kernel(work_dir, args.verbose, args.variant)
if __name__ == '__main__':
main()