-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·191 lines (154 loc) · 6.1 KB
/
build.sh
File metadata and controls
executable file
·191 lines (154 loc) · 6.1 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
#!/usr/bin/env bash
#
# Script For Building Android Kernel
#
##----------------------------------------------------------##
# Specify Kernel Directory
KERNEL_DIR="$(pwd)"
# Device Name and Model
MODEL=Xiaomi
DEVICE=Miatoll
# Kernel Defconfig
DEFCONFIG=vendor/xiaomi/miatoll_defconfig
# Files
IMAGE=${KERNEL_DIR}/out/arch/arm64/boot/Image.gz
DTBO=${KERNEL_DIR}/out/arch/arm64/boot/dtbo.img
DTB=${KERNEL_DIR}/out/arch/arm64/boot/dts/qcom/cust-atoll-ab.dtb
# Verbose Build
VERBOSE=0
# Kernel Version
KERVER=$(make kernelversion)
COMMIT_HEAD=$(git log --oneline -1)
# Date and Time
DATE=$(TZ=Asia/Kolkata date +"%Y%m%d-%T")
TANGGAL=$(date +"%F%S")
# Final Zip Name
ZIPNAME=RedCherry
FINAL_ZIP=${ZIPNAME}-kernel-v${KERVER}-${DEVICE}-${TANGGAL}.zip
# Compiler and Linker
COMPILER=aosp
LINKER=ld.lld
# Log File
LOG_FILE="log.txt"
##------------------------------------------------------##
# Truncate log file at the start
: > "$LOG_FILE"
# Prevent Terminal Auto-Close on Errors
trap 'echo "Script finished. Press any key to exit..."; read -n 1' EXIT
##------------------------------------------------------##
# Clean Existing Files and Kernel Build Environment
function clean_previous_files() {
echo "Cleaning previous kernel files and build environment..." | tee -a "$LOG_FILE"
# Remove old zip files
rm -f AnyKernel3/RedCherry-kernel*.zip
# Remove previous kernel build outputs in AnyKernel3
rm -f AnyKernel3/Image.gz AnyKernel3/dtbo.img
rm -rf AnyKernel3/dtb/*
# Perform a clean build: make clean and make mrproper
make clean && make mrproper | tee -a "$LOG_FILE"
}
##------------------------------------------------------##
# Clone ToolChain
function cloneTC() {
echo "Cloning toolchain for compiler: $COMPILER" | tee -a "$LOG_FILE"
if [ ! -d "${KERNEL_DIR}/clang" ]; then
mkdir clang && cd clang || exit 1
echo "Downloading AOSP clang..."
wget "https://github.com/userariii/AOSP-clang/releases/download/clang-r530567/clang-r530567.tar.gz"
tar -xvf clang*
cd .. || exit 1
else
echo "Directory 'clang' already exists. Skipping download." | tee -a "$LOG_FILE"
fi
if [ ! -d "${KERNEL_DIR}/gcc" ]; then
git clone https://github.com/LineageOS/android_prebuilts_gcc_linux-x86_aarch64_aarch64-linux-android-4.9.git --depth=1 gcc
fi
if [ ! -d "${KERNEL_DIR}/gcc32" ]; then
git clone https://github.com/LineageOS/android_prebuilts_gcc_linux-x86_arm_arm-linux-androideabi-4.9.git --depth=1 gcc32
fi
PATH="${KERNEL_DIR}/clang/bin:${KERNEL_DIR}/gcc/bin:${KERNEL_DIR}/gcc32/bin:$PATH"
if [ ! -d AnyKernel3 ]; then
git clone --depth=1 https://github.com/userariii/AnyKernel3.git -b master
else
echo "Directory 'AnyKernel3' already exists. Skipping download." | tee -a "$LOG_FILE"
fi
}
##------------------------------------------------------##
# Determine the number of threads for compilation
function determine_threads() {
echo "Determining number of threads..." | tee -a "$LOG_FILE"
if [ "$(cat /sys/devices/system/cpu/smt/active)" = "1" ]; then
THREADS=$(expr $(nproc --all) \* 2)
else
THREADS=$(nproc --all)
fi
echo "Number of threads: $THREADS" | tee -a "$LOG_FILE"
}
##------------------------------------------------------##
# Export Variables
function exports() {
if [ -d "${KERNEL_DIR}/clang" ]; then
export KBUILD_COMPILER_STRING=$(${KERNEL_DIR}/clang/bin/clang --version | head -n 1)
fi
export ARCH=arm64
export SUBARCH=arm64
export LOCALVERSION="-${VERSION}"
export KBUILD_BUILD_HOST=Linux
export KBUILD_BUILD_USER="CRUECY"
export DISTRO=$(source /etc/os-release && echo "${NAME}")
}
##------------------------------------------------------##
# Compile the Kernel
function compile() {
START=$(date +"%s")
echo "Starting compilation..." | tee -a "$LOG_FILE"
make O=out $DEFCONFIG | tee -a "$LOG_FILE"
make -kj$THREADS O=out ARCH=arm64 CC=clang \
CROSS_COMPILE=aarch64-linux-gnu- CROSS_COMPILE_ARM32=arm-linux-gnueabi- HOSTCC=clang HOSTCXX=clang++ \
HOSTCFLAGS="-fuse-ld=lld -Wno-unused-command-line-argument" LD=$LINKER LLVM=1 LLVM_IAS=1 AR=llvm-ar \
NM=llvm-nm OBJCOPY=llvm-objcopy OBJDUMP=llvm-objdump STRIP=llvm-strip READELF=llvm-readelf OBJSIZE=llvm-size \
V=$VERBOSE 2>&1 | tee -a "$LOG_FILE"
END=$(date +"%s")
DIFF=$((END - START))
echo "Compilation took $DIFF seconds." | tee -a "$LOG_FILE"
}
##------------------------------------------------------##
# Zip the Kernel with Exception Handling
function zipping() {
echo "Starting zipping process..." | tee -a "$LOG_FILE"
# Ensure the dtb directory exists
mkdir -p AnyKernel3/dtb
# Remove any existing zip file with the same name to avoid duplicates
if [ -f "AnyKernel3/$FINAL_ZIP" ]; then
echo "Removing existing zip: $FINAL_ZIP" | tee -a "$LOG_FILE"
rm "AnyKernel3/$FINAL_ZIP"
fi
if [[ -f $IMAGE && -f $DTBO && -f $DTB ]]; then
cp "$IMAGE" AnyKernel3/
cp "$DTBO" AnyKernel3/
cp "$DTB" AnyKernel3/dtb/
cd AnyKernel3 || exit 1
# Create the zip only once
zip -r9 "$FINAL_ZIP" * | tee -a "../$LOG_FILE"
MD5CHECK=$(md5sum "$FINAL_ZIP" | cut -d' ' -f1)
echo "MD5: $MD5CHECK" | tee -a "../$LOG_FILE"
cd ..
else
echo "Error: One or more required files are missing!" | tee -a "$LOG_FILE"
[[ ! -f $IMAGE ]] && echo "Missing: $IMAGE" | tee -a "$LOG_FILE"
[[ ! -f $DTBO ]] && echo "Missing: $DTBO" | tee -a "$LOG_FILE"
[[ ! -f $DTB ]] && echo "Missing: $DTB" | tee -a "$LOG_FILE"
exit 1
fi
}
##------------------------------------------------------##
# Execute Functions
clean_previous_files # Clean up before building
cloneTC # Clone necessary toolchains
determine_threads # Determine the number of threads
exports # Export necessary variables
compile # Compile the kernel
zipping # Zip the compiled files
# Keep the terminal open after completion
echo "Script completed successfully. Press any key to exit..." | tee -a "$LOG_FILE"
read -n 1