-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.bash
223 lines (198 loc) · 4.82 KB
/
helpers.bash
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
# Build helpers for Choco Kernel
# Copyright (C) 2021 Mikhael Khrustik <misha@myrt.co>
#
# This script must be *sourced* from a Bash in order to function.
set -e
# Declare all side effects variables
_int_vars+=(
_int_functions
_out_dir
_release_dir
_flasher_dir
_zip_file
_arch
_kmake_flags
)
# Declare all side-effects functions
_int_functions+=(
croot
log
success
clone_toolchain
kmake
kernel_build
dtb_build
regenerate_defconfig
pack
master_build
publish
unsetup
_generate_changelog
_generate_json
_configure
_local_version
_get_repo
)
# Internal variables
_out_dir="$_bootstrap_path/out"
_release_dir="$_bootstrap_path/release"
_flasher_dir="$_bootstrap_path/flasher"
_zip_file="$_release_dir/${defconfig/_defconfig/}.zip"
_arch="arm64"
# Build flags
_kmake_flags=(
-j"$threads"
ARCH="$_arch"
O="out"
CC="clang"
CLANG_TRIPLE="aarch64-linux-gnu-"
CROSS_COMPILE="aarch64-linux-android-"
CROSS_COMPILE_ARM32="arm-linux-androideabi-"
AR="llvm-ar"
NM="llvm-nm"
OBJCOPY="llvm-objcopy"
OBJDUMP="llvm-objdump"
STRIP="llvm-strip"
)
# Go to kernel root directory
function croot() {
cd $_bootstrap_path
}
# Print information message to CLI
function log() {
echo -e "\e[90m$1\e[0m"
}
# Print success message to CLI
function success() {
echo -e "\e[32m$1\e[0m"
}
# Download toolchain to $toolchain_path
function clone_toolchain() {
_get_repo "$llvm_url" "$_llvm_dir"
_get_repo "$gcc32_url" "$_gcc32_dir"
_get_repo "$gcc64_url" "$_gcc64_dir"
}
# Main wrapper for all `make` functions
function kmake() {
make "${_kmake_flags[@]}" "$@"
}
# Build kernel image
function kernel_build() {
kmake
success "Kernel builded successfully"
}
# Build combined device tree
function dtb_build() {
find "$_out_dir/arch/arm64/boot/dts/vendor/qcom" \
-name '*.dtb' \
-exec cat {} + > "$_out_dir/arch/arm64/boot/dtb"
success "DTB builded successfully"
}
# Sync configuration file with tree
function regenerate_defconfig() {
_configure
cp -f "$_out_dir/defconfig" \
"$_bootstrap_path/arch/$_arch/configs/$defconfig"
success "defconfig saved successfully"
}
# Pack flashable archive from builded kernel and dtb
function pack() {
cp -f $_out_dir/arch/arm64/boot/{Image.gz,dtb} "$_flasher_dir"
rm -f "$_zip_file"
cd "$_flasher_dir"
zip -r9 $_zip_file *
croot
success "Flashable archive packed successfully"
}
# Full kernel pipeline
function master_build() {
_configure
kernel_build
dtb_build
pack
success "Master pipeline completed successfully"
}
# Publish FKM build
function publish() {
_generate_json
_generate_changelog
surge --project "$_release_dir" \
-d "$surge_url" \
--token "$surge_token"
}
# Unsetup Choco environment
function unsetup() {
# Restore PATH
export PATH="$_old_path"
# Unset functions
for func in "${_int_functions[@]}"; do
unset -f "$func" > /dev/null 2>&1
done
# Unset variables
for var in "${_int_vars[@]}"; do
unset -v "$var" > /dev/null 2>&1
done
echo -e "\e[32mEnvironment successfully unsetuped\e[0m"
}
# Export git history to changelog
function _generate_changelog() {
commit_count=50
git log -n $commit_count \
--pretty=format:'* %s' > "$_release_dir/changelog.txt"
log "Changelog generated successfully"
}
# Generate FKM JSON file
function _generate_json() {
hash="$(sha1sum $_zip_file | cut -d' ' -f1)"
cat <<- EOF > "$_release_dir/index.html"
{
"kernel": {
"name": "$fkm_name",
"sha1": "$hash",
"link": "$surge_url/choco.zip",
"version": "$(date '+%Y-%m-%d-%H-%M')$(_local_version)",
"date": "$(date '+%Y-%m-%d')",
"changelog_url": "$surge_url/changelog.txt"
},
"support": {
"link": "https://github.com/mishamyrt/kebab-choco-kernel/issues"
}
}
EOF
log "Release JSON generated successfully"
}
# Generate defconfig
function _configure() {
kmake "$defconfig"
kmake savedefconfig
log "defconfig generated successfully"
}
# Format build local version
function _local_version() {
branch="$(git rev-parse --abbrev-ref HEAD)"
local_version="${branch/upstream\//}"
local_version="${local_version/\//-}"
local_version="${local_version/$trunk_name/}"
if (("${#local_version}" > 0)); then
if [[ ${local_version:0:1} != "-" ]]; then
local_version="-$local_version"
fi
fi
echo $local_version
}
# Shallow clone
function _get_repo() {
if [ -d "$2" ]; then
cd $2
if git rev-parse --git-dir 2> /dev/null; then
log "$1 already downloaded"
croot
else
croot
rm -rf "$2"
git clone -j"$threads" --depth=1 "$1" "$2"
fi
else
git clone -j"$threads" --depth=1 "$1" "$2"
fi
}