-
Notifications
You must be signed in to change notification settings - Fork 1
/
build_kernel.sh
executable file
·216 lines (180 loc) · 5.99 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
#!/bin/bash
################################################################################
#
# build_kernel.sh
#
# Copyright (c) 2021-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"
# Workspace directory & relevant temp folders.
WORKSPACE_DIR="$(mktemp -d)"
TOOLCHAIN_DIR="${WORKSPACE_DIR}/toolchain"
PLATFORM_EXTRACT_DIR="${WORKSPACE_DIR}/src"
WORKSPACE_OUT_DIR="${PLATFORM_EXTRACT_DIR}/out/target/product/trona/obj/KERNEL_OBJ"
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="-j24"
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
}
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 "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 --single-branch -b "${TOOLCHAIN_BRANCH}" "${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="${TOOLCHAIN_DIR}/bin/${TOOLCHAIN_PREFIX}"
CC="${CLANG_COMPILER_PATH}/bin/clang"
if [[ -n "${KERNEL_SUBPATH}" ]]
then
MAKE_ARGS="-C ${KERNEL_SUBPATH}"
fi
MAKE_ARGS="-C ${KERNEL_SUBPATH} O=${WORKSPACE_OUT_DIR} ARCH=${TARGET_ARCH}"
MAKE_ARGS1="-C ${KERNEL_SUBPATH} O=${WORKSPACE_OUT_DIR} ARCH=${TARGET_ARCH} CROSS_COMPILE=${CCOMPILE} CLANG_TRIPLE=aarch64-linux-gnu- CC=${CC}"
echo "MAKE_ARGS: ${MAKE_ARGS}"
echo "MAKE_ARGS1: ${MAKE_ARGS1}"
# 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: output config, for reference
echo ".config contents"
echo "---------------------------------------------------------------------"
cat "${OUTPUT_CFG}"
echo "---------------------------------------------------------------------"
# Step 3: full make
echo "Running full make"
make ${PARALLEL_EXECUTION} ${MAKE_ARGS1}
if [[ $? != 0 ]]; then
echo "ERROR: Failed to build kernel" >&2
exit 1
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}"
setup_output_dir
TARGET_DIR="$(cd "${TARGET_DIR}" && pwd)"
display_config
# Phase 2: Set up environment
if [ -n "${TOOLCHAIN_NAME}" ] && [ -n "$(ls -A /tmp/${TOOLCHAIN_NAME})" ]; then
TOOLCHAIN_DIR="/tmp/${TOOLCHAIN_NAME}"
else
download_toolchain
fi
extract_tarball
# Phase 3: build
exec_build_kernel
# Phase 4: move to output
copy_to_output
# Phase 5: verify output
validate_output