-
Notifications
You must be signed in to change notification settings - Fork 2
/
kramel.sh
578 lines (536 loc) · 15.8 KB
/
kramel.sh
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#!/usr/bin/env bash
# Written by: cyberknight777
# YAKB v1.0
# Copyright (c) 2022-2023 Cyber Knight <[email protected]>
#
# GNU GENERAL PUBLIC LICENSE
# Version 3, 29 June 2007
#
# Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
# Everyone is permitted to copy and distribute verbatim copies
# of this license document, but changing it is not allowed.
# Some Placeholders: [!] [*] [✓] [✗]
# Default defconfig to use for builds.
export CONFIG=raphael_defconfig
# Default directory where kernel is located in.
KDIR=$(pwd)
export KDIR
# Default linker to use for builds.
export LINKER="ld.lld"
# Device name.
export DEVICE="Redmi K20 Pro"
# Date of build.
DATE=$(date +"%Y-%m-%d")
export DATE
# Device codename.
export CODENAME="raphael"
# Builder name.
export BUILDER="Pc1598"
# Kernel repository URL.
export REPO_URL="https://github.com/Pc1598/kernel_xiaomi_raphael"
# Commit hash of HEAD.
COMMIT_HASH=$(git rev-parse --short HEAD)
export COMMIT_HASH
# Build status. Set 1 for release builds. | Set 0 for bleeding edge builds.
if [ "${RELEASE}" == 1 ]; then
export STATUS="Release"
export CHATID=-1001361882613
export re="rc"
else
export STATUS="Bleeding-Edge"
export CHATID=-1001564538644
export re="r"
fi
# Telegram Information. Set 1 to enable. | Set 0 to disable.
export TGI=1
# Number of jobs to run.
PROCS=$(nproc --all)
export PROCS
# Compiler to use for builds.
export COMPILER=clang
# Requirements
if [ "${CI}" == 0 ]; then
if ! hash dialog make curl wget unzip find 2>/dev/null; then
echo -e "\n\e[1;31m[✗] Install dialog, make, curl, wget, unzip, and find! \e[0m"
exit 1
fi
fi
if [[ "${COMPILER}" == gcc ]]; then
if [ ! -d "${KDIR}/gcc64" ]; then
git clone https://github.com/cyberknight777/gcc-arm64 --depth=1 gcc64
fi
if [ ! -d "${KDIR}/gcc32" ]; then
git clone https://github.com/cyberknight777/gcc-arm --depth=1 gcc32
fi
KBUILD_COMPILER_STRING=$("${KDIR}"/gcc64/bin/aarch64-elf-gcc --version | head -n 1)
export KBUILD_COMPILER_STRING
export PATH="${KDIR}"/gcc32/bin:"${KDIR}"/gcc64/bin:/usr/bin/:${PATH}
MAKE+=(
O=out
CROSS_COMPILE=aarch64-elf-
CROSS_COMPILE_ARM32=arm-eabi-
LD="${KDIR}"/gcc64/bin/aarch64-elf-"${LINKER}"
AR=aarch64-elf-ar
AS=aarch64-elf-as
NM=aarch64-elf-nm
OBJDUMP=aarch64-elf-objdump
OBJCOPY=aarch64-elf-objcopy
CC=aarch64-elf-gcc
)
elif [[ "${COMPILER}" == clang ]]; then
if [ ! -f "${KDIR}/neutron-clang/bin/clang" ]; then
rm -rf "${KDIR}"/neutron-clang
mkdir "${KDIR}"/neutron-clang
cd "${KDIR}"/neutron-clang || exit 1
bash <(curl -s "https://raw.githubusercontent.com/Neutron-Toolchains/antman/main/antman") -S
cd "${KDIR}" || exit 1
fi
KBUILD_COMPILER_STRING=$("${KDIR}"/neutron-clang/bin/clang -v 2>&1 | head -n 1 | sed 's/(https..*//' | sed 's/ version//')
export KBUILD_COMPILER_STRING
export PATH=$KDIR/neutron-clang/bin/:/usr/bin/:${PATH}
MAKE+=(
O=out
LLVM=1
LLVM_IAS=1
)
fi
if [ ! -d "${KDIR}/anykernel3-topaz/" ]; then
git clone --depth=1 https://github.com/Pc1598/AnyKernel3-Topaz -b master anykernel3-topaz
fi
if [ ! -f "${KDIR}/version" ]; then
echo -e "\n\e[1;31m[✗] version file not found!!! Read https://github.com/cyberknight777/YAKB#version-file for more information.\e[0m"
exit 1
fi
KBUILD_BUILD_VERSION=$(grep num= version | cut -d= -f2)
export KBUILD_BUILD_VERSION
export KBUILD_BUILD_USER="Pc1598"
export KBUILD_BUILD_HOST="builder"
VERSION=$(grep ver= version | cut -d= -f2)
kver="${KBUILD_BUILD_VERSION}"
zipn=Topaz-raphael-"${VERSION}"
# A function to exit on SIGINT.
exit_on_signal_SIGINT() {
echo -e "\n\n\e[1;31m[✗] Received INTR call - Exiting...\e[0m"
exit 0
}
trap exit_on_signal_SIGINT SIGINT
# A function to send message(s) via Telegram's BOT api.
tg() {
curl -sX POST https://api.telegram.org/bot"${TOKEN}"/sendMessage \
-d chat_id="${CHATID}" \
-d parse_mode=Markdown \
-d disable_web_page_preview=true \
-d text="$1" &>/dev/null
}
# A function to send file(s) via Telegram's BOT api.
tgs() {
MD5=$(md5sum "$1" | cut -d' ' -f1)
curl -fsSL -X POST -F document=@"$1" https://api.telegram.org/bot"${TOKEN}"/sendDocument \
-F "chat_id=${CHATID}" \
-F "parse_mode=Markdown" \
-F "caption=$2 | *MD5*: \`$MD5\`"
}
# A function to clean kernel source prior building.
clean() {
echo -e "\n\e[1;93m[*] Cleaning source and out/ directory! \e[0m"
make clean && make mrproper && rm -rf "${KDIR}"/out
echo -e "\n\e[1;32m[✓] Source cleaned and out/ removed! \e[0m"
}
# A function to regenerate defconfig.
rgn() {
echo -e "\n\e[1;93m[*] Regenerating defconfig! \e[0m"
make "${MAKE[@]}" $CONFIG
cp -rf "${KDIR}"/out/.config "${KDIR}"/arch/arm64/configs/$CONFIG
echo -e "\n\e[1;32m[✓] Defconfig regenerated! \e[0m"
}
# A function to open a menu based program to update current config.
mcfg() {
rgn
echo -e "\n\e[1;93m[*] Making Menuconfig! \e[0m"
make "${MAKE[@]}" menuconfig
cp -rf "${KDIR}"/out/.config "${KDIR}"/arch/arm64/configs/$CONFIG
echo -e "\n\e[1;32m[✓] Saved Modifications! \e[0m"
}
# A function to build the kernel.
img() {
if [[ "${TGI}" == "1" ]]; then
tg "
*Build Number*: \`${kver}\`
*Status*: \`${STATUS}\`
*Builder*: \`${BUILDER}\`
*Core count*: \`$(nproc --all)\`
*Device*: \`${DEVICE} [${CODENAME}]\`
*Kernel Version*: \`$(make kernelversion 2>/dev/null)\`
*Date*: \`$(date)\`
*Zip Name*: \`${zipn}\`
*Compiler*: \`${KBUILD_COMPILER_STRING}\`
*Linker*: \`$("${KDIR}"/neutron-clang/bin/${LINKER} -v | head -n1 | sed 's/(compatible with [^)]*)//' |
head -n 1 | perl -pe 's/\(http.*?\)//gs' | sed -e 's/ */ /g' -e 's/[[:space:]]*$//')\`
*Branch*: \`$(git rev-parse --abbrev-ref HEAD)\`
*Last Commit*: [${COMMIT_HASH}](${REPO_URL}/commit/${COMMIT_HASH})
"
fi
rgn
echo -e "\n\e[1;93m[*] Building Kernel! \e[0m"
BUILD_START=$(date +"%s")
time make -j"$PROCS" "${MAKE[@]}" Image.gz-dtb 2>&1 | tee log.txt
BUILD_END=$(date +"%s")
DIFF=$((BUILD_END - BUILD_START))
if [ -f "${KDIR}/out/arch/arm64/boot/Image.gz-dtb" ]; then
if [[ "${TGI}" == "1" ]]; then
tg "*Kernel Built after $((DIFF / 60)) minute(s) and $((DIFF % 60)) second(s)*"
fi
echo -e "\n\e[1;32m[✓] Kernel built after $((DIFF / 60)) minute(s) and $((DIFF % 60)) second(s)! \e[0m"
else
if [[ "${TGI}" == "1" ]]; then
tgs "log.txt" "*Build failed*"
fi
echo -e "\n\e[1;31m[✗] Build Failed! \e[0m"
exit 1
fi
}
# A function to build DTBs.
dtb() {
rgn
echo -e "\n\e[1;93m[*] Building DTBS! \e[0m"
time make -j"$PROCS" "${MAKE[@]}" dtbs dtbo.img
echo -e "\n\e[1;32m[✓] Built DTBS! \e[0m"
}
# A function to build out-of-tree modules.
mod() {
if [[ "${TGI}" == "1" ]]; then
tg "*Building Modules!*"
fi
rgn
echo -e "\n\e[1;93m[*] Building Modules! \e[0m"
mkdir -p "${KDIR}"/out/modules
make "${MAKE[@]}" modules_prepare
make -j"$PROCS" "${MAKE[@]}" modules INSTALL_MOD_PATH="${KDIR}"/out/modules
make "${MAKE[@]}" modules_install INSTALL_MOD_PATH="${KDIR}"/out/modules
find "${KDIR}"/out/modules -type f -iname '*.ko' -exec cp {} "${KDIR}"/anykernel3-topaz/modules/system/lib/modules/ \;
echo -e "\n\e[1;32m[✓] Built Modules! \e[0m"
}
# A function to build an AnyKernel3 zip.
mkzip() {
if [[ "${TGI}" == "1" ]]; then
tg "*Building zip!*"
fi
echo -e "\n\e[1;93m[*] Building zip! \e[0m"
mv "${KDIR}"/out/arch/arm64/boot/dtbo.img "${KDIR}"/anykernel3-topaz
mv "${KDIR}"/out/arch/arm64/boot/Image.gz-dtb "${KDIR}"/anykernel3-topaz
cd "${KDIR}"/anykernel3-topaz || exit 1
zip -r9 "$zipn".zip . -x ".git*" -x "README.md" -x "LICENSE" -x "*.zip"
echo -e "\n\e[1;32m[✓] Built zip! \e[0m"
if [[ "${OTA}" == "1" ]]; then
git clone https://github.com/cyberknight777/op7_json.git
cd op7_json || exit 1
echo "https://cyberknight777:[email protected]" > .pwd
git config credential.helper "store --file .pwd"
sha1=$(sha1sum ../"${zipn}".zip | cut -d ' ' -f1)
if [[ "${RELEASE}" != "1" ]]; then
rm changelog_r.md
wget "${CL_LINK}/raw" -O changelog_r.md
echo "
{
\"kernel\": {
\"name\": \"Topaz\",
\"version\": \"$VERSION\",
\"link\": \"https://github.com/cyberknight777/op7_json/releases/download/$VERSION/$zipn.zip\",
\"changelog_url\": \"https://raw.githubusercontent.com/cyberknight777/op7_json/master/changelog_r.md\",
\"date\": \"$DATE\",
\"sha1\": \"$sha1\"
},
\"support\": {
\"link\": \"https://t.me/genhodee\"
}
}
" > DragonHeart-r.json
git add DragonHeart-r.json changelog_r.md || exit 1
git commit -s -m "DragonHeart: Update $CODENAME to $VERSION release" -m "- This is a bleeding edge release."
git commit --amend --reset-author --no-edit
git push
gh release create "${VERSION}" -t "DragonHeart for $CODENAME [BLEEDING EDGE] - $VERSION"
gh release upload "${VERSION}" ../"${zipn}.zip"
else
rm changelog.md
wget "${CL_LINK}"/raw -O changelog.md
echo "
{
\"kernel\": {
\"name\": \"DragonHeart\",
\"version\": \"$VERSION\",
\"link\": \"https://github.com/cyberknight777/op7_json/releases/download/$VERSION/$zipn.zip\",
\"changelog_url\": \"https://raw.githubusercontent.com/cyberknight777/op7_json/master/changelog.md\",
\"date\": \"$DATE\",
\"sha1\": \"$sha1\"
},
\"support\": {
\"link\": \"https://t.me/knightschat\"
}
}
" > DragonHeart-rc.json
git add DragonHeart-rc.json changelog.md || exit 1
git commit -s -m "DragonHeart: Update $CODENAME to $VERSION release" -m "- This is a stable release."
git commit --amend --reset-author --no-edit
git push
gh release create "${VERSION}" -t "DragonHeart for $CODENAME [RELEASE] - $VERSION"
gh release upload "${VERSION}" ../"${zipn}.zip"
fi
cd ../ || exit 1
fi
if [[ "${TGI}" == "1" ]]; then
tgs "${zipn}.zip" "*#${kver} ${KBUILD_COMPILER_STRING}*"
tg "
*Build*: https://github.com/cyberknight777/op7\_json/releases/download/$VERSION/$zipn.zip
*Changelog*: https://github.com/cyberknight777/op7\_json/blob/master/changelog\_${re}.md
*OTA*: https://raw.githubusercontent.com/cyberknight777/op7\_json/master/DragonHeart-${re}.json
"
fi
}
# A function to build specific objects.
obj() {
rgn
echo -e "\n\e[1;93m[*] Building ${1}! \e[0m"
time make -j"$PROCS" "${MAKE[@]}" "$1"
echo -e "\n\e[1;32m[✓] Built ${1}! \e[0m"
}
# A function to uprev localversion in defconfig.
upr() {
echo -e "\n\e[1;93m[*] Bumping localversion to -DragonHeart-${1}! \e[0m"
"${KDIR}"/scripts/config --file "${KDIR}"/arch/arm64/configs/$CONFIG --set-str CONFIG_LOCALVERSION "-DragonHeart-${1}"
rgn
if [ "${CI}" == "0" ]; then
git add arch/arm64/configs/$CONFIG
git commit -S -s -m "raphael_defconfig: Bump to \`${1}\`"
fi
echo -e "\n\e[1;32m[✓] Bumped localversion to -DragonHeart-${1}! \e[0m"
}
# A function to showcase the options provided for args-based usage.
helpmenu() {
echo -e "\n\e[1m
usage: bash $0 <arg>
example: bash $0 mcfg
example: bash $0 mcfg img
example: bash $0 mcfg img mkzip
example: bash $0 --obj=drivers/android/binder.o
example: bash $0 --obj=kernel/sched/
example: bash $0 --upr=r16
mcfg Runs make menuconfig
img Builds Kernel
dtb Builds dtb(o).img
mod Builds out-of-tree modules
mkzip Builds anykernel3 zip
--obj Builds specific driver/subsystem
rgn Regenerates defconfig
--upr Uprevs kernel version in defconfig
\e[0m"
}
# A function to setup menu-based usage.
ndialog() {
HEIGHT=16
WIDTH=40
CHOICE_HEIGHT=30
BACKTITLE="Yet Another Kernel Builder"
TITLE="YAKB v1.0"
MENU="Choose one of the following options: "
OPTIONS=(1 "Build kernel"
2 "Build DTBs"
3 "Build modules"
4 "Open menuconfig"
5 "Regenerate defconfig"
6 "Uprev localversion"
7 "Build AnyKernel3 zip"
8 "Build a specific object"
9 "Clean"
10 "Exit"
)
CHOICE=$(dialog --clear \
--backtitle "$BACKTITLE" \
--title "$TITLE" \
--menu "$MENU" \
$HEIGHT $WIDTH $CHOICE_HEIGHT \
"${OPTIONS[@]}" \
2>&1 >/dev/tty)
clear
case "$CHOICE" in
1)
clear
img
echo -ne "\e[1mPress enter to continue or 0 to exit! \e[0m"
read -r a1
if [ "$a1" == "0" ]; then
exit 0
else
clear
ndialog
fi
;;
2)
clear
dtb
echo -ne "\e[1mPress enter to continue or 0 to exit! \e[0m"
read -r a1
if [ "$a1" == "0" ]; then
exit 0
else
clear
ndialog
fi
;;
3)
clear
mod
echo -ne "\e[1mPress enter to continue or 0 to exit! \e[0m"
read -r a1
if [ "$a1" == "0" ]; then
exit 0
else
clear
ndialog
fi
;;
4)
clear
mcfg
echo -ne "\e[1mPress enter to continue or 0 to exit! \e[0m"
read -r a1
if [ "$a1" == "0" ]; then
exit 0
else
clear
ndialog
fi
;;
5)
clear
rgn
echo -ne "\e[1mPress enter to continue or 0 to exit! \e[0m"
read -r a1
if [ "$a1" == "0" ]; then
exit 0
else
clear
ndialog
fi
;;
6)
dialog --inputbox --stdout "Enter version number: " 15 50 | tee .t
ver=$(cat .t)
clear
upr "$ver"
rm .t
echo -ne "\e[1mPress enter to continue or 0 to exit! \e[0m"
read -r a1
if [ "$a1" == "0" ]; then
exit 0
else
clear
ndialog
fi
;;
7)
mkzip
echo -ne "\e[1mPress enter to continue or 0 to exit! \e[0m"
read -r a1
if [ "$a1" == "0" ]; then
exit 0
else
clear
ndialog
fi
;;
8)
dialog --inputbox --stdout "Enter object path: " 15 50 | tee .f
ob=$(cat .f)
if [ -z "$ob" ]; then
dialog --inputbox --stdout "Enter object path: " 15 50 | tee .f
fi
clear
obj "$ob"
rm .f
echo -ne "\e[1mPress enter to continue or 0 to exit! \e[0m"
read -r a1
if [ "$a1" == "0" ]; then
exit 0
else
clear
ndialog
fi
;;
9)
clear
clean
img
echo -ne "\e[1mPress enter to continue or 0 to exit! \e[0m"
read -r a1
if [ "$a1" == "0" ]; then
exit 0
else
clear
ndialog
fi
;;
10)
echo -e "\n\e[1m Exiting YAKB...\e[0m"
sleep 3
exit 0
;;
esac
}
if [ "${CI}" == 1 ]; then
upr "${VERSION}"
fi
if [[ -z $* ]]; then
ndialog
fi
for arg in "$@"; do
case "${arg}" in
"mcfg")
mcfg
;;
"img")
img
;;
"dtb")
dtb
;;
"mod")
mod
;;
"mkzip")
mkzip
;;
"--obj="*)
object="${arg#*=}"
if [[ -z "$object" ]]; then
echo "Use --obj=filename.o"
exit 1
else
obj "$object"
fi
;;
"rgn")
rgn
;;
"--upr="*)
vers="${arg#*=}"
if [[ -z "$vers" ]]; then
echo "Use --upr=version"
exit 1
else
upr "$vers"
fi
;;
"clean")
clean
;;
"help")
helpmenu
exit 1
;;
*)
helpmenu
exit 1
;;
esac
done