-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.sh
executable file
·364 lines (344 loc) · 10.5 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
#!/usr/bin/env bash
# Copyright (c) 2021 CloudedQuartz
# Copyright (c) 2021-2024 Diaz1401
REV="Wed Jun 26 03:05:38 PM WIB 2024"
echo -e "${YELLOW}Revision ===> ${BLUE}${REV}${WHITE}"
ARG=$@
ERRORMSG="\
Usage: ./build.sh argument\n\
Available argument:\n\
clang use Clang/LLVM\n\
aosp use Android Clang/LLVM (use with 'clang' option)\n\
gcc use GCC\n\
opt enable cat_optimize\n\
lto enable LTO\n\
pgo_gen enable profiling\n\
pgo_use enable PGO\n\
dce enable dead code and data elimination\n\
keep keep toolchain\n\
beta download experimental toolchain\n\
stable download stable toolchain\n\
beta-TAG spesific experimental toolchain tag\n\
stable-TAG spesific stable toolchain tag\n\n\
valid stable toolchain tag:\n\
https://github.com/mengkernel/clang-stable/releases\n\
https://github.com/mengkernel/gcc-stable/releases\n\
valid experimental toolchain tag:\n\
https://github.com/mengkernel/clang/releases\n\
https://github.com/mengkernel/gcc/releases"
CLANG=false
AOSP=false
GCC=false
CAT=false
LTO=false
PGO_GEN=false
PGO_USE=false
DCE=false
TAG=""
BETA=false
STABLE=false
LATEST=true
KEEP=false
if [ -z "$ARG" ]; then
echo -e "$ERRORMSG"
exit 1
else
for i in $ARG; do
case "$i" in
clang) CLANG=true ;;
aosp) AOSP=true ;;
gcc) GCC=true ;;
opt) CAT=true ;;
lto) LTO=true ;;
pgo_gen) PGO_GEN=true ;;
pgo_use) PGO_USE=true ;;
dce) DCE=true ;;
keep) KEEP=true ;;
beta) BETA=true ;;
stable) STABLE=true ;;
beta-*) LATEST=false BETA=true TAG=$(echo "$i" | sed s/beta-//g) ;;
stable-*) LATEST=false STABLE=true TAG=$(echo "$i" | sed s/stable-//g) ;;
*)
echo -e "$ERRORMSG"
exit 1
;;
esac
done
if ! $GCC && ! $CLANG; then
echo "Toolchain not specified"
exit 1
elif $GCC && $CLANG; then
echo "Do not use both GCC and Clang"
exit 1
elif $PGO_GEN && $PGO_USE; then
echo "Do not use both PGO_GEN & PGO_USE"
exit 1
elif ! $STABLE && ! $BETA; then
echo "Specify stable or beta"
exit 1
elif $STABLE && $BETA; then
echo "Do not use both stable and beta"
exit 1
elif ! $CLANG && $AOSP; then
echo "Do not use 'aosp' without 'clang'"
exit 1
fi
fi
# Silence all safe.directory warnings
git config --global --add safe.directory '*'
KERNEL_NAME=Kucing
KERNEL_DIR=$(pwd)
NPROC=$(nproc --all)
AK3=${KERNEL_DIR}/AnyKernel3
TOOLCHAIN=${KERNEL_DIR}/toolchain
LOG=${KERNEL_DIR}/log.txt
KERNEL_DTB=${KERNEL_DIR}/out/arch/arm64/boot/dtb
KERNEL_IMG=${KERNEL_DIR}/out/arch/arm64/boot/Image
KERNEL_IMG_DTB=${KERNEL_DIR}/out/arch/arm64/boot/Image-dtb
KERNEL_IMG_GZ_DTB=${KERNEL_DIR}/out/arch/arm64/boot/Image.gz-dtb
KERNEL_DTBO=${KERNEL_DIR}/out/arch/arm64/boot/dtbo.img
TELEGRAM_CHAT=-1001180467256
#unused TELEGRAM_TOKEN=${TELEGRAM_TOKEN}
DATE=$(date +"%Y%m%d")
COMMIT=$(git log --pretty=format:"%s" -1)
COMMIT_SHA=$(git rev-parse --short HEAD)
KERNEL_BRANCH=$(git rev-parse --abbrev-ref HEAD)
BUILD_DATE=$(date)
KBUILD_BUILD_USER=Diaz
PATH=${TOOLCHAIN}/bin:${TOOLCHAIN}/aarch64-linux-gnu/bin:${PATH}
AOSP_CLANG_VERSION="clang-r510928"
# Colors
WHITE='\033[0m'
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
export NPROC KERNEL_NAME KERNEL_DIR AK3 TOOLCHAIN LOG KERNEL_DTB KERNEL_IMG KERNEL_IMG_DTB KERNEL_IMG_GZ_DTB KERNEL_DTBO TELEGRAM_CHAT DATE COMMIT COMMIT_SHA KERNEL_BRANCH BUILD_DATE KBUILD_BUILD_USER PATH WHITE RED GREEN YELLOW BLUE CLANG GCC CAT LTO PGO_GEN PGO_USE STABLE BETA LATEST TAG
echo "LC_ALL=en_US.UTF-8" | sudo tee -a /etc/environment
echo "en_US.UTF-8 UTF-8" | sudo tee -a /etc/locale.gen
echo "LANG=en_US.UTF-8" | sudo tee -a /etc/locale.conf
sudo locale-gen en_US.UTF-8
#
# Clone Toolchain
clone_tc() {
if $KEEP; then
echo -e "${YELLOW}===> ${BLUE}Keep existing toolchain${WHITE}"
else
echo -e "${YELLOW}===> ${BLUE}Removing old toolchain${WHITE}"
rm -rf $TOOLCHAIN
fi
echo -e "${YELLOW}===> ${BLUE}Downloading Toolchain${WHITE}"
mkdir -p "$TOOLCHAIN"
if $GCC; then
if $LATEST; then
if $STABLE; then
curl -s https://github.com/gitapi/repos/mengkernel/gcc-stable/releases/latest |
grep "browser_download_url" |
cut -d '"' -f4 |
wget -qO gcc.tar.zst -i -
else
curl -s https://github.com/gitapi/repos/mengkernel/gcc/releases/latest |
grep "browser_download_url" |
cut -d '"' -f4 |
wget -qO gcc.tar.zst -i -
fi
else
if $STABLE; then
wget -qO gcc.tar.zst https://github.com/mengkernel/gcc-stable/releases/download/${TAG}/gcc.tar.zst
else
wget -qO gcc.tar.zst https://github.com/mengkernel/gcc/releases/download/${TAG}/gcc.tar.zst
fi
fi
tar xf gcc.tar.zst -C $TOOLCHAIN
elif $AOSP; then
wget -qO clang.tar.gz https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86/+archive/refs/heads/main/${AOSP_CLANG_VERSION}.tar.gz
tar xf clang.tar.gz -C $TOOLCHAIN
else
if $LATEST; then
if $STABLE; then
curl -s https://github.com/gitapi/repos/mengkernel/clang-stable/releases/latest |
grep "browser_download_url" |
cut -d '"' -f4 |
wget -qO clang.tar.zst -i -
else
curl -s https://github.com/gitapi/repos/mengkernel/clang/releases/latest |
grep "browser_download_url" |
cut -d '"' -f4 |
wget -qO clang.tar.zst -i -
fi
else
if $STABLE; then
wget -qO clang.tar.zst https://github.com/mengkernel/clang-stable/releases/download/${TAG}/clang.tar.zst
else
wget -qO clang.tar.zst https://github.com/mengkernel/clang/releases/download/${TAG}/clang.tar.zst
fi
fi
tar xf clang.tar.zst -C $TOOLCHAIN
fi
}
#
# Clones anykernel
clone_ak() {
if [ -a "$AK3" ]; then
echo -e "${YELLOW}===> ${BLUE}AnyKernel3 exist${WHITE}"
echo -e "${YELLOW}===> ${BLUE}Try to update repo${WHITE}"
cd $AK3
git pull
cd -
else
echo -e "${YELLOW}===> ${BLUE}Cloning AnyKernel3${WHITE}"
git clone -q --depth=1 -b alioth https://github.com/mengkernel/AnyKernel3.git $AK3
fi
}
#
# send_info - sends text to telegram
send_info() {
CAPTION=$(echo -e \
"Build started
Date: <code>${BUILD_DATE}</code>
HEAD: <code>${COMMIT_SHA}</code>
Commit: <code>${COMMIT}</code>
Branch: <code>${KERNEL_BRANCH}</code>
")
curl -s "https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage" \
-F parse_mode=html \
-F text="$CAPTION" \
-F chat_id="$TELEGRAM_CHAT" >/dev/null 2>&1
}
#
# send_file - uploads file to telegram
send_file() {
curl -F document=@"$1" "https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendDocument" \
-F chat_id="$TELEGRAM_CHAT" \
-F caption="$2" \
-F parse_mode=html >/dev/null 2>&1
}
#
# send_files - uploads multiple files to telegram in one message
send_files() {
i=1
for files in $@; do
if [ $i -eq 1 ]; then
attach="{\"type\": \"document\", \"media\": \"attach://file$i\"}"
arg="-F file$i=@$files"
else
attach="$attach, {\"type\": \"document\", \"media\": \"attach://file$i\"}"
arg="$arg -F file$i=@$files"
fi
((i++))
done
curl "https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMediaGroup" \
-F chat_id="$TELEGRAM_CHAT" \
-F "media=[$attach]" \
$arg >/dev/null 2>&1
}
#
# build_kernel
build_kernel() {
cd $KERNEL_DIR
BUILD_START=$(date +"%s")
if $LTO; then
if $GCC; then
./scripts/config --file arch/arm64/configs/cat_defconfig -d LTO_CLANG -e LTO_GCC
else
./scripts/config --file arch/arm64/configs/cat_defconfig -d LTO_GCC -e LTO_CLANG
fi
fi
if $CAT; then
./scripts/config --file arch/arm64/configs/cat_defconfig -e CAT_OPTIMIZE
fi
if $PGO_GEN; then
./scripts/config --file arch/arm64/configs/cat_defconfig -d PGO_USE -e PGO_GEN
fi
if $PGO_USE; then
./scripts/config --file arch/arm64/configs/cat_defconfig -d PGO_GEN -e PGO_USE
fi
if $DCE; then
./scripts/config --file arch/arm64/configs/cat_defconfig -e LD_DEAD_CODE_DATA_ELIMINATION
fi
if $GCC; then
make -j${NPROC} O=out cat_defconfig CROSS_COMPILE=aarch64-linux-gnu- |& tee -a $LOG
make -j${NPROC} O=out CROSS_COMPILE=aarch64-linux-gnu- |& tee -a $LOG
else
make -j${NPROC} O=out cat_defconfig LLVM=1 LLVM_IAS=1 CROSS_COMPILE=aarch64-linux-gnu- |& tee -a $LOG
make -j${NPROC} O=out LLVM=1 LLVM_IAS=1 CROSS_COMPILE=aarch64-linux-gnu- |& tee -a $LOG
fi
BUILD_END=$(date +"%s")
DIFF=$((BUILD_END - BUILD_START))
}
#
# build_end - creates and sends zip
build_end() {
rm -rf ${AK3}/*.zip ${AK3}/dtb* ${AK3}/Image*
if [ -a "$KERNEL_IMG_GZ_DTB" ]; then
mv $KERNEL_IMG_GZ_DTB $AK3
elif [ -a "$KERNEL_IMG_DTB" ]; then
mv $KERNEL_IMG_DTB $AK3
elif [ -a "$KERNEL_IMG" ]; then
mv $KERNEL_IMG $AK3
else
echo -e "${YELLOW}===> ${RED}Build failed, sad${WHITE}"
echo -e "${YELLOW}===> ${GREEN}Send build log to Telegram${WHITE}"
send_files $LOG
exit 1
fi
echo -e "${YELLOW}===> ${GREEN}Build success, generating flashable zip...${WHITE}"
find ${KERNEL_DIR}/out/arch/arm64/boot/dts/vendor/qcom -name '*.dtb' -exec cat {} + >$KERNEL_DTB
ls ${KERNEL_DIR}/out/arch/arm64/boot/
cp $KERNEL_DTBO $AK3
cp $KERNEL_DTB $AK3
cd $AK3
DTBO_NAME=${KERNEL_NAME}-DTBO-${DATE}-${COMMIT_SHA}.img
DTB_NAME=${KERNEL_NAME}-DTB-${DATE}-${COMMIT_SHA}
ZIP_NAME=${KERNEL_NAME}-${DATE}-${COMMIT_SHA}.zip
if $CLANG; then
ZIP_NAME=CLANG-${ZIP_NAME}
fi
if $GCC; then
ZIP_NAME=GCC-${ZIP_NAME}
fi
if $CAT; then
ZIP_NAME=OPT-${ZIP_NAME}
fi
if $LTO; then
ZIP_NAME=LTO-${ZIP_NAME}
fi
if $DCE; then
ZIP_NAME=DCE-${ZIP_NAME}
fi
if $PGO_GEN; then
ZIP_NAME=PGO_GEN-${ZIP_NAME}
fi
if $PGO_USE; then
ZIP_NAME=PGO_USE-${ZIP_NAME}
fi
zip -r9 KERNEL-$ZIP_NAME * -x .git .github LICENSE README.md dtb*
zip -r9 DTBO-$ZIP_NAME * -x .git .github LICENSE README.md *Image* *.zip
echo -e "${YELLOW}===> ${BLUE}Send kernel to Telegram${WHITE}"
send_file KERNEL-$ZIP_NAME "Time taken: <code>$((DIFF / 60))m $((DIFF % 60))s</code>"
echo -e "${YELLOW}===> ${WHITE}Zip name: ${GREEN}${ZIP_NAME}"
send_files DTBO-$ZIP_NAME $LOG ${KERNEL_DIR}/out/.config
}
COMMIT=$(git log --pretty=format:"%s" -1)
COMMIT_SHA=$(git rev-parse --short HEAD)
KERNEL_BRANCH=$(git rev-parse --abbrev-ref HEAD)
BUILD_DATE=$(date)
CAPTION=$(echo -e \
"Build started
Date: <code>$BUILD_DATE</code>
HEAD: <code>$COMMIT_SHA</code>
Commit: <code>$COMMIT</code>
Branch: <code>$KERNEL_BRANCH</code>
")
#
# build_all - run build script
build_all() {
send_info
build_kernel
build_end
}
#
# compile time
clone_tc
clone_ak
build_all