forked from TelegramAt25/yuki_kernel_xiaomi_selene
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·418 lines (363 loc) · 11.4 KB
/
build.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
#!/usr/bin/bash
# Define some things
# Export Bot token and chat id
export SEND_TO_TG=1
export chat_id="-1002138024433"
export token="7034672132:AAHi8HPm41YxODjidVTjO0Wg7Nz9L18aMmk"
#!/bin/bash
# Membaca token bot dan chat ID dari variabel lingkungan
token="$token"
chat_id="$chat_id"
# Memeriksa apakah variabel lingkungan telah diatur
if [ -z "$token" ] || [ -z "$chat_id" ]; then
echo "Error: Token bot atau chat ID tidak diatur."
exit 1
fi
# Kernel common
export ARCH=arm64
export LINKER="ld.lld"
# Telegram API
export SEND_TO_TG=1
# Telegram && Output
export kver="KernelSU"
export CODENAME="selenes"
export DEVICE="Redmi 10 4G/Redmi Note 11 4G (${CODENAME})"
export BUILDER="äerichāndesu"
export BUILD_HOST="noticesa"
export TIMESTAMP=$(date +"%Y%m%d")
export KBUILD_COMPILER_STRING=$(./clang/bin/clang -v 2>&1 | head -n 1 | sed 's/(https..*//' | sed 's/ version//')
export zipn="Hydrogen-${CODENAME}-${TIMESTAMP}"
# Needed by script
export PATH="${PWD}/clang/bin:${PATH}"
PROCS=$(nproc --all)
# Text coloring
NOCOLOR='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
LIGHTGRAY='\033[0;37m'
DARKGRAY='\033[1;30m'
LIGHTRED='\033[1;31m'
LIGHTGREEN='\033[1;32m'
YELLOW='\033[1;33m'
LIGHTBLUE='\033[1;34m'
LIGHTPURPLE='\033[1;35m'
LIGHTCYAN='\033[1;36m'
WHITE='\033[1;37m'
# Check permission
script_permissions=$(stat -c %a "$0")
if [ "$script_permissions" -lt 777 ]; then
echo -e "${RED}error:${NOCOLOR} Don't have enough permission"
echo "run 'chmod 0777 origami_kernel_builder.sh' and rerun"
exit 126
fi
# Check dependencies
if ! hash make curl bc zip 2>/dev/null; then
echo -e "${RED}error:${NOCOLOR} Missing dependencies"
echo "Installing..."
sudo apt update
sudo apt install -y make curl bc zip
echo "Dependencies installed!"
else
echo "All dependencies present."
fi
# Periksa apakah folder clang sudah ada
if [ ! -d "${PWD}/clang" ]; then
echo "Mengkloning clang..."
# Ambil URL rilis terbaru
latest_release_url=$(curl -s "https://api.github.com/repos/ZyCromerZ/Clang/releases/latest" | grep '"browser_download_url":' | cut -d '"' -f 4)
# Unduh aset rilis terbaru
wget "$latest_release_url" -O "clang.tar.gz"
# Ekstrak arsip dan bersihkan
rm -rf clang && mkdir clang && tar -xvf clang.tar.gz -C clang && rm -rf clang.tar.gz
echo "clang dikloning!"
else
echo "Folder clang sudah ada."
fi
# Check anykernel
if [ ! -d "${PWD}/anykernel" ]; then
echo -e "${RED}error:${NOCOLOR} /anykernel not found!"
echo "Cloning AnyKernel3..."
git clone -b Hydrogen https://github.com/noticesax/AnyKernel3 anykernel
if [ $? -ne 0 ]; then
echo -e "${RED}error:${NOCOLOR} Failed to clone!"
exit 1
fi
echo "Cloned AnyKernel3!"
else
echo "anykernel directory exists."
fi
# Exit on interrupt
exit_on_signal_interrupt() {
echo -e "\n\n${RED}Got interrupt signal.${NOCOLOR}"
exit 130
}
trap exit_on_signal_interrupt SIGINT
help_msg() {
echo "Usage: bash build.sh --choose=[Function]"
echo ""
echo "Some functions on Origami Kernel Builder:"
echo "1. Build a whole Kernel"
echo "2. Regenerate defconfig"
echo "3. Open menuconfig"
echo "4. Clean"
echo ""
echo "Place this script inside the Kernel Tree."
}
send_msg_telegram() {
case "$1" in
1)
# Dapatkan URL repositori
repo_url=$(git config --get remote.origin.url)
# Dapatkan hash commit terakhir
commit_hash=$(git log --format="%h" -n 1)
# Buat URL commit
commit_url="${repo_url}/commit/${commit_hash}"
curl -s -X POST "https://api.telegram.org/bot$token/sendMessage" \
-d chat_id="$chat_id" \
-d "disable_web_page_preview=true" \
-d "parse_mode=html" \
-d text="<b>——${TIMESTAMP}——</b>
<b>*Build Triggered ${BUILD_HOST}</b>
<b>*Local Ver</b>: <code>TC/UDC</code>
<b>*Build status</b>: <code>${kver}</code>
<b>*Builder</b>: <code>${BUILDER}</code>
<b>*Device</b>: <code>${DEVICE}</code>
<b>*Kernel Ver</b>: <code>$(make kernelversion 2>/dev/null)</code>
<b>*Date</b>: <code>$(date)</code>
<b>*Zip</b>: <code>${zipn}</code>
<b>*Defconfig</b>: <code>${DEFCONFIG}</code>
<b>*Clang Ver</b>: <code>${KBUILD_COMPILER_STRING}</code>
<b>*Branch</b>: <code>$(git rev-parse --abbrev-ref HEAD)</code>
<b>*Last Commit</b>: <a href=\"${commit_url}\">${commit_hash}</a>" \
-o /dev/null
;;
2)
curl -s -F document=@./out/build.log "https://api.telegram.org/bot$token/sendDocument" \
-F chat_id="$chat_id" \
-F "disable_web_page_preview=true" \
-F "parse_mode=html" \
-F caption="Build failed after ${minutes} minutes and ${seconds} seconds." \
-o /dev/null \
-w "" >/dev/null 2>&1
;;
3)
curl -s -F document=@./out/target/"${zipn}".zip "https://api.telegram.org/bot$token/sendDocument" \
-F chat_id="$chat_id" \
-F "disable_web_page_preview=true" \
-F "parse_mode=html" \
-F caption="Fearless Build Succes: ${minutes}min & ${seconds}secs." \
-o /dev/null \
-w "" >/dev/null 2>&1
curl -s -F document=@./out/build.log "https://api.telegram.org/bot$token/sendDocument" \
-F chat_id="$chat_id" \
-F "disable_web_page_preview=true" \
-F "parse_mode=html" \
-F caption="Build log" \
-o /dev/null \
-w "" >/dev/null 2>&1
;;
esac
}
show_defconfigs() {
defconfig_path="./arch/${ARCH}/configs"
# Check if folder exists
if [ ! -d "$defconfig_path" ]; then
echo -e "${RED}FATAL:${NOCOLOR} Seems not a valid Kernel linux"
exit 2
fi
echo -e "Available defconfigs:\n"
# List defconfigs and assign them to an array
defconfigs=($(ls "$defconfig_path"))
# Display enumerated defconfigs
for ((i=0; i<${#defconfigs[@]}; i++)); do
echo -e "${LIGHTCYAN}$i: ${defconfigs[i]}${NOCOLOR}"
done
echo ""
read -p "Select the defconfig you want to process: " choice
# Check if the choice is within the range of files
if [ $choice -ge 0 ] && [ $choice -lt ${#defconfigs[@]} ]; then
export DEFCONFIG="${defconfigs[choice]}"
echo "Selected defconfig: $DEFCONFIG"
else
echo -e "${RED}error:${NOCOLOR} Invalid choice"
exit 1
fi
}
compile_kernel() {
rm ./out/arch/${ARCH}/boot/Image.gz-dtb 2>/dev/null
export KBUILD_BUILD_USER=${BUILDER}
export KBUILD_BUILD_HOST=${BUILD_HOST}
export LOCALVERSION=${localversion}
export CROSS_COMPILE_ARM32="arm-linux-gnueabi-"
export CROSS_COMPILE_COMPAT="arm-linux-gnueabi-"
export CROSS_COMPILE="aarch64-linux-gnu-"
make O=out ARCH=${ARCH} ${DEFCONFIG}
START=$(date +"%s")
make -j"$PROCS" O=out \
ARCH=${ARCH} \
LD="${LINKER}" \
AR=llvm-ar \
AS=llvm-as \
NM=llvm-nm \
OBJDUMP=llvm-objdump \
STRIP=llvm-strip \
CC="clang" \
CLANG_TRIPLE=aarch64-linux-gnu- \
CROSS_COMPILE=aarch64-linux-gnu- \
CROSS_COMPILE_ARM32=arm-linux-gnueabihf- \
CONFIG_NO_ERROR_ON_MISMATCH=y \
CONFIG_DEBUG_SECTION_MISMATCH=y \
V=0 2>&1 | tee out/build.log
END=$(date +"%s")
DIFF=$((END - START))
export minutes=$((DIFF / 60))
export seconds=$((DIFF % 60))
}
zip_kernel() {
# Move kernel image to anykernel zip
if [ ! -f "./out/arch/${ARCH}/boot/Image.gz-dtb" ]; then
cp ./out/arch/${ARCH}/boot/Image.gz ./anykernel
else
cp ./out/arch/${ARCH}/boot/Image.gz-dtb ./anykernel
fi
# Zip the kernel
cd ./anykernel
zip -r9 "${zipn}".zip * -x .git README.md *placeholder
cd ..
if [ ! -d "./out/target" ]; then
mkdir ./out/target
fi
# Clean up anykernel directory
if [ ! -f "./out/arch/${ARCH}/boot/Image.gz-dtb" ]; then
rm -f ./anykernel/Image.gz
else
rm -f ./anykernel/Image.gz-dtb
fi
rm -f ./anykernel/dtbo.img
# Move the kernel zip to ./out/target
mv ./anykernel/${zipn}.zip ./out/target
}
build_kernel() {
show_defconfigs
echo -e "${LIGHTBLUE}================================="
echo "Build Started on ${BUILD_HOST}"
echo "Build status: ${kver}"
echo "Builder: ${BUILDER}"
echo "Device: ${DEVICE}"
echo "Kernel Version: $(make kernelversion 2>/dev/null)"
echo "Date: $(date)"
echo "Zip Name: ${zipn}"
echo "Defconfig: ${DEFCONFIG}"
echo "Compiler: ${KBUILD_COMPILER_STRING}"
echo "Branch: $(git rev-parse --abbrev-ref HEAD)"
echo "Last Commit: $(git log --format="%s" -n 1): $(git log --format="%h" -n 1)"
echo -e "=================================${NOCOLOR}"
if [ "$SEND_TO_TG" -eq 1 ]; then
send_msg_telegram 1
fi
compile_kernel
if [ ! -f "./out/arch/${ARCH}/boot/Image.gz-dtb" ] && [ ! -f "./out/arch/${ARCH}/boot/Image.gz" ]; then
if [ "$SEND_TO_TG" -eq 1 ]; then
send_msg_telegram 2
fi
echo -e "${LIGHTBLUE}================================="
echo -e "${RED}Build failed${LIGHTBLUE} after ${minutes} minutes and ${seconds} seconds"
echo "See build log for troubleshooting."
echo -e "=================================${NOCOLOR}"
exit 1
fi
zip_kernel
echo -e "${LIGHTBLUE}================================="
echo "Build took ${minutes} minutes and ${seconds} seconds."
echo -e "=================================${NOCOLOR}"
if [ "$SEND_TO_TG" -eq 1 ]; then
send_msg_telegram 3
fi
}
regen_defconfig() {
show_defconfigs
make O=out ARCH=${ARCH} ${DEFCONFIG}
cp -rf ./out/.config ./arch/${ARCH}/configs/${DEFCONFIG}
}
open_menuconfig() {
show_defconfigs
make O=out ARCH=${ARCH} ${DEFCONFIG}
echo -e "${LIGHTGREEN}Note: Make sure you save the config with name '.config'"
echo -e " else the defconfig will not saved automatically.${NOCOLOR}"
# Removed the countdown loop
make O=out menuconfig
cp -rf ./out/.config ./arch/${ARCH}/configs/${DEFCONFIG}
}
execute_operation() {
loop_helper() {
read -p "Press enter to continue or type 0 for Quit: " a1
clear
if [[ "$a1" == "0" ]]; then
exit 0
else
bash "$0"
fi
}
case "$1" in
1) clear
build_kernel
loop_helper
;;
2) clear
regen_defconfig
loop_helper
;;
3) clear
open_menuconfig
loop_helper
;;
4) clear
make clean && make mrproper
loop_helper
;;
5) exit 0 && clear ;;
6) help_msg ;;
*) echo -e "${RED}error:${NOCOLOR} Invalid selection." && exit 1 ;;
esac
}
if [ $# -eq 0 ]; then
clear
echo -e "${LIGHTCYAN}What do you want to do today?"
echo ""
echo "1. Build a whole Kernel"
echo "2. Regenerate defconfig"
echo "3. Open menuconfig"
echo "4. Clean"
echo "5. Quit"
echo -e "${NOCOLOR}"
read -p "Choice the number: " choice
else
case "$1" in
--choose=1)
choice=1
;;
--choose=2)
choice=2
;;
--choose=3)
choice=3
;;
--choose=4)
choice=4
;;
--help)
choice=6
;;
*)
echo -e "${RED}error:${NOCOLOR} Not a valid argument"
echo "Try 'bash origami_kernel_builder.sh --help' for more information."
exit 1
;;
esac
fi
# Main script logic
execute_operation "$choice"