forked from nathanchance/env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-kernel
executable file
·281 lines (225 loc) · 8.65 KB
/
build-kernel
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
#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Copyright (C) 2017-2018 Nathan Chancellor
#
# Script to build a zImage from a kernel tree
######################
# #
# HELPER FUNCTIONS #
# #
######################
# Echo to stderr
function errcho() {
(>&2 echo "${@}")
}
# Export version variable
function evv() {
FILE=out/include/generated/compile.h
export "$(grep "${1}" "${FILE}" | cut -d'"' -f1 | awk '{print $2}')"="$(grep "${1}" "${FILE}" | cut -d'"' -f2)"
}
# Kernel make function
function kmake() {
# Set make variable
MAKE="make ${JOBS_FLAG} O=out ARCH=${ARCH}"
if [[ -n ${CLANG} ]]; then
PATH=${BIN_FOLDER}:${PATH} ${MAKE} CC="${CCACHE} ${CLANG_TOOLCHAIN}" \
CLANG_TRIPLE=aarch64-linux-gnu- \
CROSS_COMPILE="${GCC_TOOLCHAIN%gcc}" \
CROSS_COMPILE_ARM32="${GCC_32_BIT_TOOLCHAIN%gcc}" \
HOSTCC="${CCACHE} ${CLANG_TOOLCHAIN}" \
KBUILD_COMPILER_STRING="${CLANG_VERSION}" \
"${@}"
else
PATH=${BIN_FOLDER}:${PATH} ${MAKE} CROSS_COMPILE="${CCACHE} ${GCC_TOOLCHAIN%gcc}" \
CROSS_COMPILE_ARM32="${GCC_32_BIT_TOOLCHAIN%gcc}" \
"${@}"
fi
}
# Show the version as if looking at "/proc/version"
function parse_version() {
evv UTS_VERSION
evv LINUX_COMPILE_BY
evv LINUX_COMPILE_HOST
evv LINUX_COMPILER
VERSION=$(cat out/include/config/kernel.release)
echo "Linux version ${VERSION} (${LINUX_COMPILE_BY}@${LINUX_COMPILE_HOST}) (${LINUX_COMPILER}) ${UTS_VERSION}"
}
#######################
# #
# PRIMARY FUNCTIONS #
# #
#######################
# Gather parameters
function parse_parameters() {
PARAMS="${*}"
START=$(date +%s)
while [[ ${#} -ge 1 ]]; do
case ${1} in
# Architecture to build
"-a"|"--arch")
shift && enforce_value "${@}"
ARCH=${1} ;;
# Use Clang for compiling the kernel
"-c"|"--clang")
CLANG=true ;;
# Specify which Clang toolchain to use
"-ct"|"--clang-toolchain")
shift && enforce_value "${@}"
CLANG_TOOLCHAIN_FOLDER=${1} ;;
# Config to build
"-d"|"--defconfig")
shift && enforce_value "${@}"
DEFCONFIG=${1} ;;
# Show full compilation
"-D"|"--debug")
VERBOSITY=3 ;;
# Only show errors
"-e"|"--errors")
VERBOSITY=1
AG_LOOK_FOR="error:" ;;
# Kernel folder
"-f"|"--folder")
shift && enforce_value "${@}"
FOLDER=${1} ;;
# Specify which GCC toolchain to use
"-gt"|"--gcc-toolchain")
shift && enforce_value "${@}"
GCC_TOOLCHAIN_FOLDER=${1} ;;
# 32-bit GCC toolchain to use for compiling
"-gt-32"|"--gcc-32-bit-toolchain")
shift && enforce_value "${@}"
GCC_32_BIT_TOOLCHAIN_FOLDER=${1} ;;
# Just show if build was successful or not
"-r"|"--show-only-result")
SHOW_ONLY_RESULT=true ;;
# Upload image to transfer.sh
"-u"|"--upload")
UPLOAD=true ;;
# Show only warnings and errors during compilation
"-w"|"--warnings")
VERBOSITY=2
AG_LOOK_FOR="error:|warning:" ;;
# Compile with -Werror
"-Werror")
WERROR=true ;;
# Disable -Werror
"-Wno-error")
NO_WERROR=true ;;
esac
shift
done
# Error out if we aren't in a tree with a Makefile
if [[ -n ${FOLDER} ]]; then
cd "${FOLDER}" || die "Folder requested doesn't exist!"
fi
[[ ! -f Makefile ]] && die "This must be run in a kernel tree!"
# Error out if defconfig wasn't supplied
[[ -z ${DEFCONFIG} ]] && die "Please supply a defconfig!"
# Defaults
[[ -z ${ARCH} ]] && ARCH=arm64
[[ -z ${GCC_TOOLCHAIN_FOLDER} ]] && GCC_TOOLCHAIN_FOLDER=${TC_FOLDER}/aosp-gcc-${ARCH}
}
# Set toolchains
function setup_toolchains() {
# GCC (64-bit or 32-bit)
if [[ ! -d ${GCC_TOOLCHAIN_FOLDER} ]]; then
GCC_TOOLCHAIN_FOLDER=${TC_FOLDER}/${GCC_TOOLCHAIN_FOLDER}
[[ ! -d ${GCC_TOOLCHAIN_FOLDER} ]] && die "Invalid 64-bit GCC folder specified!"
fi
GCC_TOOLCHAIN=$(find "${GCC_TOOLCHAIN_FOLDER}/bin" \( -type f -o -type l \) -name '*-gcc' | head -n1)
[[ -z ${GCC_TOOLCHAIN} ]] && die "64-bit GCC toolchain could not be found!"
# GCC 32-bit for compat VDSO
if [[ "${PWD}$(git cb)" =~ "wahoo" || "${PWD}$(git cb)" =~ "marlin" ]]; then
[[ -z ${GCC_32_BIT_TOOLCHAIN_FOLDER} ]] && GCC_32_BIT_TOOLCHAIN_FOLDER=${TC_FOLDER}/aosp-gcc-arm
if [[ ! -d ${GCC_32_BIT_TOOLCHAIN_FOLDER} ]]; then
GCC_32_BIT_TOOLCHAIN_FOLDER=${TC_FOLDER}/${GCC_32_BIT_TOOLCHAIN_FOLDER}
[[ ! -d ${GCC_32_BIT_TOOLCHAIN_FOLDER} ]] && die "Invalid 32-bit GCC folder specified!"
fi
GCC_32_BIT_TOOLCHAIN=$(find "${GCC_32_BIT_TOOLCHAIN_FOLDER}/bin" \( -type f -o -type l \) -name '*-gcc' | head -n1)
[[ -z ${GCC_32_BIT_TOOLCHAIN} ]] && die "32-bit GCC toolchain could not be found!"
fi
# Clang
if [[ -n ${CLANG} ]]; then
[[ -z ${CLANG_TOOLCHAIN_FOLDER} ]] && CLANG_TOOLCHAIN_FOLDER=${TC_FOLDER}/aosp-clang/clang-4053586
if [[ ! -d ${CLANG_TOOLCHAIN_FOLDER} ]]; then
CLANG_TOOLCHAIN_FOLDER=${TC_FOLDER}/${CLANG_TOOLCHAIN_FOLDER}
[[ ! -d ${CLANG_TOOLCHAIN_FOLDER} ]] && die "Invalid Clang folder specified!"
fi
CLANG_TOOLCHAIN=${CLANG_TOOLCHAIN_FOLDER}/bin/clang
[[ ! -f ${CLANG_TOOLCHAIN} ]] && die "Clang toolchain could not be found!"
CLANG_VERSION=$(clang_version "${CLANG_TOOLCHAIN}")
fi
}
# Basic build function
function build() {
# Clean up from last compile
rm -rf out && mkdir -p out
# Build kernel image
kmake "${DEFCONFIG}" |& ag --no-color -v "format-overflow"
if [[ -n ${WERROR} ]]; then
./scripts/config --file out/.config -e CC_WERROR
kmake olddefconfig
fi
if [[ -n ${NO_WERROR} ]]; then
./scripts/config --file out/.config -d CC_WERROR
kmake olddefconfig
fi
if [[ "${PWD}" =~ "op6" ]]; then
./scripts/config --file out/.config -e BUILD_ARM64_DT_OVERLAY
kmake olddefconfig
NEEDS_EXTERNAL_DTC=true
fi
mkavenv
kmake ${NEEDS_EXTERNAL_DTC:+ "DTC_EXT=dtc"} |& ag --no-color -v "dts"
rmvenv
}
# Compilation function
function compile() {
# Start hiding output
[[ -n ${SHOW_ONLY_RESULT} ]] && exec > /dev/null
# Show the base version we are making
header "BUILDING $(make CROSS_COMPILE="" kernelversion)"
# Show compilation based on flags
case ${VERBOSITY} in
"3")
build ;;
"1"|"2")
DISABLED_WARNINGS=( "which has unmet direct dependencies"
"choice value used outside its choice group"
"reassigning to symbol"
"changes choice state" )
for ITEM in "${DISABLED_WARNINGS[@]}"; do AG_IGNORE="${AG_IGNORE}${ITEM}|"; done
build |& ag --nocolor "${AG_LOOK_FOR}" |& ag -v "${AG_IGNORE/%|}" ;;
*)
build &> /dev/null ;;
esac
# Find final image
FINAL_IMAGE=$(find out -name 'Image.*-dtb')
[[ -z ${FINAL_IMAGE} ]] && FINAL_IMAGE=$(find out -name 'Image.*' | tail -1)
}
# Report success
function report_result() {
END=$(date +%s)
[[ -n ${SHOW_ONLY_RESULT} ]] && SCRIPT_COMMAND="$(basename "${0}") ${PARAMS} | "
if [[ -f ${FINAL_IMAGE} ]]; then
echo
errcho "${SCRIPT_COMMAND}${GRN}BUILD SUCCESSFUL IN $(format_time "${START}" "${END}")${RST}"
echo
echo "${BOLD}IMAGE:${RST} ${FINAL_IMAGE}"
echo
echo "${BOLD}VERSION:${RST} $(parse_version)"
else
>&2 die "${RST}${SCRIPT_COMMAND}${RED}BUILD FAILED IN $(format_time "${START}" "${END}")!"
fi
[[ -n ${UPLOAD} ]] && transfer "${FINAL_IMAGE}"
# Alert of script end
echo "\n\a"
}
source "$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" || return; pwd)/common"
trap 'echo; die "Manually aborted!"' SIGINT SIGTERM
parse_parameters "${@}"
setup_toolchains
compile
report_result