This repository has been archived by the owner on Jan 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.sh
executable file
·303 lines (279 loc) · 7.63 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
#! /usr/bin/env bash
#
# Rissu Kernel Project
# A special build script for Rissu's kernel
#
# << If unset, you can override if u want
[ -z $IS_CI ] && IS_CI=false
[ -z $DO_CLEAN ] && DO_CLEAN=false
[ -z $LTO ] && LTO=none
[ -z $DEFAULT_KSU_REPO ] && DEFAULT_KSU_REPO="https://raw.githubusercontent.com/rsuntk/KernelSU/main/kernel/setup.sh"
[ -z $DEFAULT_AK3_REPO ] && DEFAULT_AK3_REPO="https://github.com/rsuntk/AnyKernel3.git"
[ -z $DEVICE ] && DEVICE="Unknown"
[ -z $IMAGE ] && IMAGE="$(pwd)/out/arch/arm64/boot/Image"
# special rissu's path. linked to his toolchains
if [ -d /rsuntk ]; then
export CROSS_COMPILE=/rsuntk/toolchains/google/bin/aarch64-linux-android-
export PATH=/rsuntk/toolchains/clang-12/bin:$PATH
fi
# start of default args
DEFAULT_ARGS="
CONFIG_SECTION_MISMATCH_WARN_ONLY=y
ARCH=arm64
"
export ARCH=arm64
export CLANG_TRIPLE=aarch64-linux-gnu-
# end of default args
pr_invalid() {
echo -e "[-] Invalid args: $@"
exit
}
pr_err() {
echo -e "[-] $@"
exit
}
pr_info() {
echo -e "[+] $@"
}
strip() { # fmt: strip <module>
llvm-strip $@ --strip-unneeded
}
setconfig() { # fmt: setconfig enable/disable <NAME>
[ -e $(pwd)/.config ] && config_file="$(pwd)/.config" || config_file="$(pwd)/out/.config"
if [ -d $(pwd)/scripts ]; then
[ "$1" = "enable" ] && pr_info "Enabling CONFIG_`echo $2` .." || pr_info "Disabling CONFIG_`echo $2`"
chmod +x ./scripts/config && ./scripts/config --file `echo $config_file` --`echo $1` CONFIG_`echo $2`
else
echo "! Folder scripts not found!"
exit
fi
}
clone_ak3() {
[ ! -d $(pwd)/AnyKernel3 ] && git clone $DEFAULT_AK3_REPO --depth=1
rm -rf AnyKernel3/.git
}
gen_getutsrelease() {
# generate simple c file
if [ ! -e utsrelease.c ]; then
echo "/* Generated file by `basename $0` */
#include <stdio.h>
#ifdef __OUT__
#include \"out/include/generated/utsrelease.h\"
#else
#include \"include/generated/utsrelease.h\"
#endif
char utsrelease[] = UTS_RELEASE;
int main() {
printf(\"%s\n\", utsrelease);
return 0;
}" > utsrelease.c
fi
}
usage() {
echo -e "Usage: bash `basename $0` <build_target> <-j | --jobs> <(job_count)> <defconfig>"
printf "\tbuild_target: dirty, kernel, config, clean\n"
printf "\t-j or --jobs: <int>\n"
[ -d arch/$ARCH/configs ] && printf "\tavailable defconfig: `ls arch/arm64/configs`\n"
echo ""
printf "NOTE: Run: \texport CROSS_COMPILE=\"<PATH_TO_ANDROID_CC>\"\n"
printf "\t\texport PATH=\"<PATH_TO_LLVM>\"\n"
printf "before running this script!\n"
printf "\n"
printf "Misc:\n"
printf "\tPOST_BUILD_CLEAN: Clean post build: (opt:boolean)\n"
printf "\tLTO: Use Link-time Optimization; options: (opt: none, thin, full)\n"
printf "\tLLVM: Use all llvm toolchains to build: (opt: 1)\n"
printf "\tLLVM_IAS: Use llvm integrated assembler: (opt: 1)\n"
exit;
}
# if first arg starts with "clean"
if [[ "$1" = "clean" ]]; then
[ $# -gt 1 ] && pr_err "Excess argument, only need one argument."
pr_info "Cleaning dirs"
if [ -d $(pwd)/out ]; then
rm -rf out
elif [ -f $(pwd)/.config ]; then
make clean
make mrproper
else
pr_err "No need clean."
fi
pr_err "All clean."
elif [[ "$1" = "dirty" ]]; then
if [ $# -gt 3 ]; then
pr_err "Excess argument, only need three argument."
fi
pr_err "Starting dirty build"
FIRST_JOB="$2"
JOB_COUNT="$3"
if [ "$FIRST_JOB" = "-j" ] || [ "$FIRST_JOB" = "--jobs" ]; then
if [ ! -z $JOB_COUNT ]; then
ALLOC_JOB=$JOB_COUNT
else
pr_invalid $3
fi
else
pr_invalid $2
fi
make -j`echo $ALLOC_JOB` -C $(pwd) O=$(pwd)/out `echo $DEFAULT_ARGS`
elif [[ "$1" = "ak3" ]]; then
if [ $# -gt 1 ]; then
pr_err "Excess argument, only need one argument."
fi
clone_ak3;
else
[ $# != 4 ] && usage;
fi
[ "$KERNELSU" = "true" ] && curl -LSs $DEFAULT_KSU_REPO | bash -s main || pr_info "KernelSU is disabled. Add 'KERNELSU=true' or 'export KERNELSU=true' to enable"
BUILD_TARGET="$1"
FIRST_JOB="$2"
JOB_COUNT="$3"
DEFCONFIG="$4"
if [ "$BUILD_TARGET" = "kernel" ]; then
BUILD="kernel"
elif [ "$BUILD_TARGET" = "defconfig" ]; then
BUILD="defconfig"
else
pr_invalid $1
fi
if [ "$FIRST_JOB" = "-j" ] || [ "$FIRST_JOB" = "--jobs" ]; then
if [ ! -z $JOB_COUNT ]; then
ALLOC_JOB=$JOB_COUNT
else
pr_invalid $3
fi
else
pr_invalid $2
fi
if [ ! -z "$DEFCONFIG" ]; then
BUILD_DEFCONFIG="$DEFCONFIG"
else
pr_invalid $4
fi
if [ "$LLVM" = "1" ]; then
LLVM_="true"
DEFAULT_ARGS+=" LLVM=1"
export LLVM=1
if [ "$LLVM_IAS" = "1" ]; then
LLVM_IAS_="true"
DEFAULT_ARGS+=" LLVM_IAS=1"
export LLVM_IAS=1
fi
else
LLVM_="false"
if [ "$LLVM_IAS" != "1" ]; then
LLVM_IAS_="false"
fi
fi
pr_sum() {
[ -z $KBUILD_BUILD_USER ] && KBUILD_BUILD_USER="`whoami`"
[ -z $KBUILD_BUILD_HOST ] && KBUILD_BUILD_HOST="`hostname`"
echo ""
echo -e "Host Arch: `uname -m`"
echo -e "Host Kernel: `uname -r`"
echo -e "Host gnumake: `make -v | grep -e "GNU Make"`"
echo ""
echo -e "Linux version: `make kernelversion`"
echo -e "Kernel builder user: $KBUILD_BUILD_USER"
echo -e "Kernel builder host: $KBUILD_BUILD_HOST"
echo -e "Build date: `date`"
echo -e "Build target: `echo $BUILD`"
echo -e "Arch: $ARCH"
echo -e "Defconfig: $BUILD_DEFCONFIG"
echo -e "Allocated core: $ALLOC_JOB"
echo ""
echo -e "LLVM: $LLVM_"
echo -e "LLVM_IAS: $LLVM_IAS_"
echo ""
echo -e "LTO: $LTO"
echo ""
}
pr_post_build() {
echo ""
echo -e "## Build $@ at `date` ##"
echo ""
[ "$@" = "failed" ] && exit
}
post_build_clean() {
if [ -e $AK3 ]; then
rm -rf $AK3/Image
rm -rf $AK3/modules/vendor/lib/modules/*.ko
#sed -i "s/do\.modules=.*/do.modules=0/" "$(pwd)/AnyKernel3/anykernel.sh"
echo "stub" > $AK3/modules/vendor/lib/modules/stub
fi
rm getutsrel
rm utsrelease.c
# clean out folder
rm -rf out
make clean
make mrproper
}
post_build() {
if [ -d $(pwd)/.git ]; then
GITSHA=$(git rev-parse --short HEAD)
else
GITSHA="localbuild"
fi
AK3="$(pwd)/AnyKernel3"
DATE=$(date +'%Y%m%d%H%M%S')
ZIP_FMT="AnyKernel3-`echo $DEVICE`_$GITSHA-$DATE"
clone_ak3;
if [ -d $AK3 ]; then
echo "- Creating AnyKernel3"
gen_getutsrelease;
if [ -d $(pwd)/out ]; then
gcc -D__OUT__ -CC utsrelease.c -o getutsrel
else
gcc -CC utsrelease.c -o getutsrel
fi
UTSRELEASE=$(./getutsrel)
sed -i "s/kernel\.string=.*/kernel.string=$UTSRELEASE/" "$AK3/anykernel.sh"
sed -i "s/BLOCK=.*/BLOCK=\/dev\/block\/platform\/12100000.dwmmc0\/by-name\/boot;/" "$AK3/anykernel.sh"
cp $IMAGE $AK3
cd $AK3
zip -r9 ../`echo $ZIP_FMT`.zip *
# CI will clean itself post-build, so we don't need to clean
# Also avoiding small AnyKernel3 zip issue!
if [ "$IS_CI" != "true" ] && [ "$DO_CLEAN" = "true" ]; then
pr_info "Host is not Automated CI, cleaning dirs"
post_build_clean;
fi
cd ..
pr_err "Build done. Thanks for using this build script :)"
fi
}
handle_lto() {
if [[ "$LTO" = "thin" ]]; then
pr_info "LTO: Thin"
setconfig disable LTO_NONE
setconfig enable LTO
setconfig enable THINLTO
setconfig enable LTO_CLANG
setconfig enable ARCH_SUPPORTS_LTO_CLANG
setconfig enable ARCH_SUPPORTS_THINLTO
elif [[ "$LTO" = "full" ]]; then
pr_info "LTO: Full"
setconfig disable LTO_NONE
setconfig enable LTO
setconfig disable THINLTO
setconfig enable LTO_CLANG
setconfig enable ARCH_SUPPORTS_LTO_CLANG
setconfig enable ARCH_SUPPORTS_THINLTO
fi
}
# call summary
pr_sum
if [ "$BUILD" = "kernel" ]; then
make -j`echo $ALLOC_JOB` -C $(pwd) O=$(pwd)/out `echo $DEFAULT_ARGS` `echo $BUILD_DEFCONFIG`
[ "$KERNELSU" = "true" ] && setconfig enable KSU
[ "$LTO" != "none" ] && handle_lto || pr_info "LTO not set";
make -j`echo $ALLOC_JOB` -C $(pwd) O=$(pwd)/out `echo $DEFAULT_ARGS`
if [ -e $IMAGE ]; then
pr_post_build "completed"
post_build
else
pr_post_build "failed"
fi
elif [ "$BUILD" = "defconfig" ]; then
make -j`echo $ALLOC_JOB` -C $(pwd) O=$(pwd)/out `echo $DEFAULT_ARGS` `echo $BUILD_DEFCONFIG`
fi