forked from nathanchance/env
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi-kernel
More file actions
executable file
·253 lines (209 loc) · 6.47 KB
/
pi-kernel
File metadata and controls
executable file
·253 lines (209 loc) · 6.47 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
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
#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Copyright (C) 2018-2019 Nathan Chancellor
#
# Script to build and install a Raspberry Pi kernel image
######################
# #
# HELPER FUNCTIONS #
# #
######################
# Echo to stderr
function errcho() {
(>&2 echo "${@}")
}
# Kernel make function
function kmake() {
if [[ -n ${PI} ]]; then
make "${JOBS_FLAG}" \
O=out \
"${@}"
else
if [[ -n ${CLANG} ]]; then
PATH="${CBL_FOLDER}/usr/bin:${PATH}" \
make ARCH=arm \
CC="${CCACHE} clang" \
HOSTCC=clang \
CROSS_COMPILE=arm-linux-gnueabi- \
"${JOBS_FLAG}" \
O=out \
"${@}"
else
make ARCH="${ARCH}" \
CROSS_COMPILE="${CCACHE} ${GCC_TOOLCHAIN%gcc}" \
"${JOBS_FLAG}" \
O=out \
"${@}"
fi
fi
}
#######################
# #
# PRIMARY FUNCTIONS #
# #
#######################
# Gather parameters
function parse_parameters() {
PARAMS="${*}"
START=$(date +%s)
while (( ${#} )); do
case ${1} in
# Architecture to build
"-a"|"--arch")
shift && enforce_value "${@}"
ARCH=${1} ;;
# Use Clang instead of GCC
"-c"|"--clang")
CLANG=true ;;
# Show full compilation
"-D"|"--debug")
VERBOSITY=3 ;;
# Only show errors
"-e"|"--errors")
VERBOSITY=1
RG_LOOK_FOR="error:" ;;
# Specify which GCC toolchain to use
"-g"|"--gcc-toolchain")
shift && enforce_value "${@}"
GCC_TOOLCHAIN_FOLDER=${1} ;;
# Install the kernel if on the Raspberry Pi
"-i"|"--install")
INSTALL=true ;;
# Just show if build was successful or not
"-r"|"--show-only-result")
SHOW_ONLY_RESULT=true ;;
# Send Telegram message
"-t"|"--telegram")
TG=true ;;
# Show only warnings and errors during compilation
"-w"|"--warnings")
VERBOSITY=2
RG_LOOK_FOR="error:|warning:" ;;
esac
shift
done
# Make sure we're actually in a kernel folder
[[ ! -f Makefile ]] && die "This must be run in a kernel tree!"
# Architecture differences
[[ -z ${ARCH} ]] && ARCH=arm
if [[ ${ARCH} = "arm" ]]; then
DEFCONFIG=bcm2709_defconfig
IMAGE=zImage
else
DEFCONFIG=bcmrpi3_defconfig
IMAGE=Image
fi
# Are we on the Pi itself?
[[ "$(whoami)" = "pi" ]] && PI=true
}
# Set toolchains
function setup_toolchains() {
[[ -n ${PI} || -n ${CLANG} ]] && return
[[ -z ${GCC_TOOLCHAIN_FOLDER} ]] && GCC_TOOLCHAIN_FOLDER=${TC_FOLDER}/btg-gcc-8.2.1-${ARCH}
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!"
}
# Basic build function
function build() {
if [[ -n ${PI} ]]; then
cd "${HOME}/repos/kernel" || die "Kernel source doesn't exist!"
else
SOURCE_FOLDER=${KERNEL_FOLDER}/pi
cd "${SOURCE_FOLDER}" || die "Kernel source doesn't exist!"
fi
{
echo "\`\`\`"
echo "Raspberry Pi kernel build starting..."
echo
echo "Version $(make kernelversion)"
echo "\`\`\`"
} > "${TG_MSG_FILE}"
[[ -n ${TG} ]] && tg_msg "$(cat "${TG_MSG_FILE}")"
# Build kernel image
rm -rf out
kmake "${DEFCONFIG}"
kmake "${IMAGE}" modules dtbs
if [[ -z ${PI} ]]; then
mkdir -p "${SOURCE_FOLDER}/out/pi_root"
kmake INSTALL_MOD_PATH="${SOURCE_FOLDER}/out/pi_root" modules_install
fi
}
# Compilation function
function compile() {
# Start hiding output and tracking time
START="$(date +"%s")"
[[ -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") build |& rg --color=never "${RG_LOOK_FOR}" ;;
*) build &> /dev/null ;;
esac
# Find final image
FINAL_IMAGE=$(find out -name "${IMAGE}" | tail -1)
}
# Install kernel on Raspberry PI
function install() {
if [[ -n ${INSTALL} && -n ${PI} && -f out/arch/arm/boot/zImage ]]; then
sudo make O=out modules_install
sudo cp out/arch/arm/boot/dts/*.dtb /boot/
sudo cp out/arch/arm/boot/dts/overlays/*.dtb* /boot/overlays/
sudo cp arch/arm/boot/dts/overlays/README /boot/overlays/
sudo cp out/arch/arm/boot/zImage /boot/kernel-nathan.img
fi
}
# Report success
function report_result() {
TIME_STRING="$(format_time "${START}" "$(date +"%s")")"
[[ -n ${SHOW_ONLY_RESULT} ]] && SCRIPT_COMMAND="$(basename "${0}") ${PARAMS} | "
if [[ -f ${FINAL_IMAGE} ]]; then
{
echo "\`\`\`"
echo "Raspberry Pi kernel build completed successfully!"
echo
echo "Duration: ${TIME_STRING,,}"
echo "\`\`\`"
} > "${TG_MSG_FILE}"
[[ -n ${TG} ]] && tg_msg "$(cat "${TG_MSG_FILE}")"
echo
errcho "${SCRIPT_COMMAND}${GRN}BUILD SUCCESSFUL IN ${TIME_STRING}${RST}"
echo
echo "${BOLD}Image:${RST} ${FINAL_IMAGE}"
echo
formatted_kernel_version full
else
{
echo "\`\`\`"
echo "Raspberry Pi kernel build failed!"
echo
echo "Duration: ${TIME_STRING,,}"
echo "\`\`\`"
} > "${TG_MSG_FILE}"
[[ -n ${TG} ]] && tg_msg "$(cat "${TG_MSG_FILE}")"
>&2 die "${RST}${SCRIPT_COMMAND}${RED}BUILD FAILED IN ${TIME_STRING}!"
fi
# Alert of script end
echo "\a"
exit 0
}
# Setup
source "$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" || return; pwd)/common"
source "${SCRIPTS_FOLDER}/snippets/fkv"
trap 'echo; die "Manually aborted!"' SIGINT SIGTERM
# Message file for Telegram messages
TG_MSG_FILE=$(mktemp)
trap 'rm -f "${TG_MSG_FILE}"' EXIT
# Main
parse_parameters "${@}"
setup_toolchains
compile
install
report_result