-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_kernel.sh
executable file
·272 lines (227 loc) · 7.3 KB
/
build_kernel.sh
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
#!/bin/bash
################################################################################
#
# build_kernel.sh
#
# Copyright (c) 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
################################################################################
################################################################################
#
# I N P U T
#
################################################################################
# Folder for platform tarball.
PLATFORM_TARBALL="${1}"
# Target directory for output artifacts.
TARGET_DIR="${2}"
################################################################################
#
# V A R I A B L E S
#
################################################################################
# Retrieve the directory where the script is currently held
SCRIPT_BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Configuration file for the build.
CONFIG_FILE="${SCRIPT_BASE_DIR}/build_kernel_config.sh"
# Prebuilts include folder
PREBUILTS_FOLDER="${SCRIPT_BASE_DIR}/prebuilt"
# Workspace directory & relevant temp folders.
WORKSPACE_DIR="$(mktemp -d)"
TOOLCHAIN_DIR="${WORKSPACE_DIR}/toolchain"
PLATFORM_EXTRACT_DIR="${WORKSPACE_DIR}/src"
WORKSPACE_OUT_DIR="${WORKSPACE_DIR}/out"
for d in "${TOOLCHAIN_DIR}" "${PLATFORM_EXTRACT_DIR}" "$WORKSPACE_OUT_DIR"
do
mkdir -p "${d}"
done
# Remove workspace directory upon completion.
trap "rm -rf $WORKSPACE_DIR" EXIT
PARALLEL_EXECUTION="-j8"
function usage {
echo "Usage: ${BASH_SOURCE[0]} path_to_platform_tar output_folder" 1>&2
exit 1
}
function validate_input_params {
if [[ ! -f "${PLATFORM_TARBALL}" ]]
then
echo "ERROR: Platform tarball not found."
usage
fi
if [[ ! -f "${CONFIG_FILE}" ]]
then
echo "ERROR: Could not find config file ${CONFIG_FILE}. Please check" \
"that you have extracted the build script properly and try again."
usage
fi
if [[ ! -d "${PREBUILTS_FOLDER}" ]]
then
echo "ERROR: Missing prebuilts folder ${PREBUILTS_FOLDER}"
usage
fi
}
function validate_cross_compiler {
if [[ ! -d "${CROSS_COMPILER_PATH}" || \
! -d "${CROSS_COMPILER_PATH}/bin" ]]
then
echo "ERROR: Invalid or missing path to cross compiler" \
"${CROSS_COMPILER_PATH}. Please check and update" \
"build_kernel_config.sh"
usage
fi
}
function display_config {
echo "-------------------------------------------------------------------------"
echo "SOURCE TARBALL: ${PLATFORM_TARBALL}"
echo "TARGET DIRECTORY: ${TARGET_DIR}"
echo "KERNEL SUBPATH: ${KERNEL_SUBPATH}"
echo "DEFINITION CONFIG: ${DEFCONFIG_NAME}"
echo "TARGET ARCHITECTURE: ${TARGET_ARCH}"
echo "CROSS COMPILER PATH: ${CROSS_COMPILER_PATH}"
echo "TOOLCHAIN REPO: ${TOOLCHAIN_REPO}"
echo "TOOLCHAIN PREFIX: ${TOOLCHAIN_PREFIX}"
echo "-------------------------------------------------------------------------"
echo "Sleeping 3 seconds before continuing."
sleep 3
}
function setup_output_dir {
if [[ -d "${TARGET_DIR}" ]]
then
FILECOUNT=$(find "${TARGET_DIR}" -type f | wc -l)
if [[ ${FILECOUNT} -gt 0 ]]
then
echo "ERROR: Destination folder is not empty. Refusing to build" \
"to a non-clean target"
exit 3
fi
else
echo "Making target directory ${TARGET_DIR}"
mkdir -p "${TARGET_DIR}"
if [[ $? -ne 0 ]]
then
echo "ERROR: Could not make target directory ${TARGET_DIR}"
exit 1
fi
fi
}
function download_toolchain {
echo "Cloning toolchain ${TOOLCHAIN_REPO} to ${TOOLCHAIN_DIR}"
git clone -b pie-release "${TOOLCHAIN_REPO}" "${TOOLCHAIN_DIR}"
if [[ $? -ne 0 ]]
then
echo "ERROR: Could not clone toolchain from ${TOOLCHAIN_REPO}."
exit 2
fi
}
function extract_tarball {
echo "Extracting tarball to ${PLATFORM_EXTRACT_DIR}"
tar xf "${PLATFORM_TARBALL}" -C ${PLATFORM_EXTRACT_DIR}
}
function exec_build_kernel {
CCOMPILE="${CROSS_COMPILER_PATH}/bin/${TOOLCHAIN_PREFIX}"
if [[ -n "${KERNEL_SUBPATH}" ]]
then
MAKE_ARGS="-C ${KERNEL_SUBPATH}"
fi
MAKE_ARGS="${MAKE_ARGS} O=${WORKSPACE_OUT_DIR} ARCH=${TARGET_ARCH} CROSS_COMPILE=${CCOMPILE}"
echo "Base make args: ${MAKE_ARGS}"
# Move into the build base folder.
pushd "${PLATFORM_EXTRACT_DIR}"
# Step 1: defconfig
echo "Make defconfig: make ${MAKE_ARGS} ${DEFCONFIG_NAME}"
make ${MAKE_ARGS} ${DEFCONFIG_NAME}
# Step 2: check trapz:
local KERNEL_FOLDER="${PLATFORM_EXTRACT_DIR}/${KERNEL_SUBPATH}"
local TRAPZ_CFG="${KERNEL_FOLDER}/arch/${TARGET_ARCH}/configs/trapz.config"
local OUTPUT_CFG="${WORKSPACE_OUT_DIR}/.config"
if [[ -f $TRAPZ_CFG ]]
then
echo "Adding trapz config to config"
cat "${TRAPZ_CFG}" >> "${OUTPUT_CFG}"
else
echo "No trapz config found."
fi
# Step 3: run oldconfig
echo "Make oldconfig: make ${MAKE_ARGS} oldconfig"
make ${MAKE_ARGS} oldconfig
# Step 3A: output config, for reference
echo ".config contents"
echo "---------------------------------------------------------------------"
cat "${OUTPUT_CFG}"
echo "---------------------------------------------------------------------"
# Step 4: Make headers install
echo "Make headers install: make ${MAKE_ARGS} headers_install"
make ${MAKE_ARGS} headers_install
# Step 5: install prebuilt headers
if [[ -d "${PREBUILTS_FOLDER}" ]]
then
echo "Copying prebuilts to ${WORKSPACE_OUT_DIR}"
cp -av ${PREBUILTS_FOLDER}/* "${WORKSPACE_OUT_DIR}"
fi
# Step 6: dtbs
if [[ -n "${MAKE_DTBS}" ]]
then
echo "Make dtbs: make ${MAKE_ARGS} dtbs"
make ${MAKE_ARGS} dtbs
fi
# Step 7: full make
echo "Running full make"
if [[ -n "${ZIMAGE_TARGET}" ]]
then
make ${MAKE_ARGS} ${PARALLEL_EXECUTION} USE_TRAPZ=true zImage
else
make ${MAKE_ARGS} ${PARALLEL_EXECUTION} USE_TRAPZ=true
fi
if [[ $? -ne 0 ]]
then
echo "Build failed"
exit 10
fi
popd
}
function copy_to_output {
echo "Copying files to output"
pushd "${WORKSPACE_OUT_DIR}"
find "./arch/"${TARGET_ARCH}"/boot" -type f | sed 's/^\.\///' | while read CPFILE
do
local BASEDIR="$(dirname "${CPFILE}")"
if [[ ! -d "${TARGET_DIR}/${BASEDIR}" ]]
then
mkdir -p "${TARGET_DIR}/${BASEDIR}"
fi
cp -v "${CPFILE}" "${TARGET_DIR}/${CPFILE}"
done
popd
}
function validate_output {
echo "Listing output files"
local IFS=":"
for IMAGE in ${KERNEL_IMAGES};do
if [ ! -f ${TARGET_DIR}/${IMAGE} ]; then
echo "ERROR: Missing kernel output image ${IMAGE}" >&2
exit 1
fi
ls -l ${TARGET_DIR}/${IMAGE}
done
}
################################################################################
#
# M A I N
#
################################################################################
# Phase 1: Set up execution
validate_input_params
source "${CONFIG_FILE}"
validate_cross_compiler
setup_output_dir
TARGET_DIR="$(cd "${TARGET_DIR}" && pwd)"
display_config
# Phase 2: Set up environment
download_toolchain
extract_tarball
# Phase 3: build
exec_build_kernel
# Phase 4: move to output
copy_to_output
# Phase 5: verify output
validate_output