-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild_android_kernel.sh
More file actions
executable file
·201 lines (197 loc) · 6.58 KB
/
build_android_kernel.sh
File metadata and controls
executable file
·201 lines (197 loc) · 6.58 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
192
193
194
195
196
197
198
199
200
201
#I have included some version of this script with the source code for Android nethunter kernels I maintain,
#but as I am constantly adding new fixes and improvements, I have decided to keep a copy in this repo.
#Set the definitions accordingly for your own kernel builds.
#
#!/usr/bin/env bash
#define colors
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
clear_color='\033[0m'
#help output
usage() {
printf "${green}Usage: ${clear_color}./build_android_kernel.sh -m [laurel|gingko] [-c] [-h] [-b]\n"
printf "${green}-m: ${clear_color} model to build for, Xiaomi Mi A3 (laurel) or Redmi Note 8 (gingko) ${red}(mandatory flag)${clear_color}\n"
printf "${green}-h: ${clear_color}display this help message\n"
printf "${green}-c: ${clear_color}configure the kernel instead of using the Nethunter defconfig\n"
printf "${green}-b: ${clear_color}give the build a lower priority (allowing you to continue to use your computer during build) \n"
exit 1
}
SECONDS=0 # built-in bash timer
topdir="$(pwd)"
tc_dir="$topdir/toolchain"
tc_url="https://github.com/Neutron-Toolchains/clang-build-catalogue/releases/download/10032024/neutron-clang-10032024.tar.zst"
ak3_dir="$topdir/AnyKernel3"
date_string="$(date '+%Y%m%d-%H%M')"
export KBUILD_BUILD_USER="$(whoami)"
export KBUILD_BUILD_HOST="$(hostname)"
make_options='ARCH=arm64 CC=clang AS=llvm-as CROSS_COMPILE=aarch64-linux-gnu- CROSS_COMPILE_ARM32=arm-linux-gnueabi- LLVM=1 LLVM_IAS=1 DTC_EXT=/usr/bin/dtc'
make_prefix=""
while getopts "m:chb" opt; do
case $opt in
m)
model=$OPTARG
if [ "$model" == "laurel" ]; then
nh_config="nethunter_laurel_sprout_defconfig"
ak3_branch="laurel_sprout"
zipname="laurel-sprout-nethunter-$date_string.zip"
tarball="laurel-sprout-modules-$date_string.tar"
elif [ "$model" == "gingko" ]; then
nh_config="nethunter_gingko_defconfig"
ak3_branch="gingko"
zipname="gingko-nethunter-$date_string.zip"
tarball="gingko-modules-$date_string.tar"
else
printf "${red}Unrecognized option for -m,${clear_color}\nPlease type either ${green}laurel${clear_color} or ${green}gingko${clear_color}\n"
exit 1
fi
config_command="$nh_config"
;;
c)
#still copy the defconfig to .config before running nconfig
cp $topdir/arch/arm64/configs/$nh_config $topdir/.config
config_command="nconfig"
;;
h)
usage
;;
b)
make_prefix="ionice -c 3 chrt --idle 0 nice -n19"
;;
\?)
printf "${red}Unrecognized option: ${clear_color}-$OPTARG\n"
usage
;;
esac
done
shift "$(( OPTIND - 1 ))"
if [ -z "$model" ]; then
printf "${red}Missing -m option\n${clear_color}"
usage
exit 1
fi
#define confirm/deny functions
confirm() {
printf "${green}yes${clear_color}\n"
}
deny() {
printf "${red}no${clear_color}\n"
}
#environment sanity checks
printf "${yellow}Checking that operating system is Linux... ${clear_color}"
if [ "$(uname -s)" == "Linux" ]; then
confirm
else
deny
printf "${red}Building not supported on operating systems other than Linux. Quitting${clear_color}\n"
exit 1
fi
printf "${yellow}Checking that architecture is x86_64... ${clear_color}"
if [ "$(uname -m)" == "x86_64" ]; then
confirm
else
deny
printf "${red}Building not supported on architectures other than x86_64. Quitting${clear_color}\n"
exit 1
fi
check_installation() {
printf "${yellow}Checking for $program... "
if command -v $program >/dev/null 2>&1; then
confirm
else
deny
printf "${red}Please install $program and run this script again${clear_color}\n"
exit 1
fi
}
programs="git curl grep bash bison flex python3 gzip tar xz make gcc perl awk swig pkg-config zstd dtc"
for p in $programs; do
program=$p
check_installation
done
#pull submodules
printf "${green}Updating kernel source${clear_color}\n"
git pull
printf "${green}Updating submodules${clear_color}\n"
git submodule init
git submodule update --recursive --remote
if [ ! -d "$tc_dir" ]; then
printf "${red}Toolchain directory not found! ${yellow}Downloading to $tc_dir...${clear_color}\n"
mkdir -p $tc_dir
cd $tc_dir
curl -fsSL $tc_url | tar --zstd -xvf -
if [ ! -e $tc_dir/bin/clang-19 ]; then
printf "${red}Something went wrong${clear_color}\n"
exit 1
else
printf "${green}Success! ${clear_color}\n"
fi
else
if [ ! -e $tc_dir/bin/clang-19 ]; then
printf "${yellow}$tc_dir/bin does not contain expected files...\n"
printf "If you want to continue anyway, perhaps using a different version of clang, type \'continue\' now: "
read continue_anyway
if [ "$continue_anyway" == "continue" ]; then
printf "\nContinuing...${clear_color}\n"
else
printf "\n${red}Build cancelled${clear_color}\n"
exit 1
fi
fi
fi
#get anykernel3 branch
if [ ! -d "$ak3_dir" ]; then
printf "${green}Cloning AnyKernel3 repo...${clear_color}\n"
git clone https://github.com/akabul0us/AnyKernel3 -b $ak3_branch $ak3_dir
else
printf "${yellow}AnyKernel3 repo already in source tree${clear_color}\n"
fi
export PATH="$tc_dir/bin:$PATH"
cd $topdir
#see if we want to clean up artifacts from a previous build
printf "${yellow}Checking for build artifacts...\n${clear_color}"
find . -name *.o > /dev/null
found="$?"
if [ "$found" -eq 0 ]; then
printf "${yellow}Build artifacts found -- clean before continuing?${clear_color} (y/N)\n"
read make_clean
if [ "$make_clean" == "y" ]; then
make ${make_options} clean
else
printf "${yellow}Continuing without cleaning${clear_color}\n"
fi
fi
printf "${green}Running configuration...${clear_color}\n"
make ${make_prefix} ${make_options} -j$(nproc --all) $config_command
printf "\n${green}Starting compilation... ${clear_color}\n"
make ${make_prefix} ${make_options} -j$(nproc --all)
kernel="arch/arm64/boot/Image.gz-dtb"
#some builds won't have the concatenated image, so allow the script to find the other option
kernel_2="arch/arm64/boot/Image.gz"
if [ ! -f "$kernel" ]; then
if [ -f "$kernel_2" ]; then
kernel="$kernel_2"
else
printf "${red}Build failed${clear_color}\n"
exit 1
fi
else
printf "${green}\nKernel compiled succesfully! ${clear_color}Zipping up...\n"
cd $ak3_dir
cp $topdir/$kernel .
zip -r9 "../$zipname" * -x .git README.md *placeholder
printf "\nCompleted in${green} $((SECONDS / 60)) minute(s) and $((SECONDS % 60)) second(s)! ${clear_color}\n"
echo "Zip: $zipname"
fi
#pack modules tarball
cd $topdir
printf "${yellow}Looking for compiled modules...${clear_color}\n"
module_paths="$(find . -name *.ko | sed ':a;N;$!ba;s/\n/ /g')"
if [ ! -d "$topdir/modules" ]; then
mkdir -p $topdir/modules
fi
cp $module_paths $topdir/modules/
cd $topdir/modules
tar cvf ../$tarball *
gzip -9 ../$tarball
printf "${green}Modules tarball $topdir/$tarball.gz created${clear_color}\n"