-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildkernel.sh
executable file
·418 lines (359 loc) · 12.2 KB
/
buildkernel.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/bin/bash -xE
# Copyright (c) 2019-2020 The Linux Foundation. All rights reserved.
# Not a Contribution.
#
# Copyright (C) 2019 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Usage:
# build/build.sh <make options>*
#
# The kernel is built in ${COMMON_OUT_DIR}/${KERNEL_DIR}.
set -e
export ROOT_DIR=$(readlink -f $(dirname $0)/../../..)
export MAKE_ARGS=$@
export COMMON_OUT_DIR=$(readlink -m ${OUT_DIR:-${ROOT_DIR}/out/${BRANCH}})
export OUT_DIR=$(readlink -m ${COMMON_OUT_DIR})
export MODULES_STAGING_DIR=$(readlink -m ${COMMON_OUT_DIR}/staging)
export KERNEL_PREBUILT_DIR=$(readlink -m ${KERNEL_DIR}/../ship_prebuilt)
export MODULES_PRIVATE_DIR=$(readlink -m ${COMMON_OUT_DIR}/private)
export DIST_DIR=$(readlink -m ${DIST_DIR:-${COMMON_OUT_DIR}/dist})
export UNSTRIPPED_DIR=${KERNEL_MODULES_OUT}/unstripped
export CLANG_TRIPLE CROSS_COMPILE CROSS_COMPILE_ARM32 ARCH SUBARCH
#Setting up for build
PREBUILT_KERNEL_IMAGE=$(basename ${TARGET_PREBUILT_INT_KERNEL})
IMAGE_FILE_PATH=arch/${ARCH}/boot
KERNEL_GEN_HEADERS=include
ARCH_GEN_HEADERS=arch/${ARCH}/include
ARCH_GEN_HEADERS_LOC=arch/${ARCH}
KERNEL_SCRIPTS=scripts
FILES="
vmlinux
System.map
"
PRIMARY_KERN_BINS=${KERNEL_PREBUILT_DIR}/primary_kernel
SECONDARY_KERN_BINS=${KERNEL_PREBUILT_DIR}/secondary_kernel
KERN_SHA1_LOC=${KERNEL_PREBUILT_DIR}/kernel_sha1.txt
#defconfig
make_defconfig()
{
if [ -z "${SKIP_DEFCONFIG}" ] ; then
echo "======================"
echo "Building defconfig"
set -x
(cd ${KERNEL_DIR} && \
${MAKE_PATH}make O=${OUT_DIR} ${MAKE_ARGS} HOSTCFLAGS="${TARGET_INCLUDES}" HOSTLDFLAGS="${TARGET_LINCLUDES}" ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} ${DEFCONFIG})
set +x
fi
}
#Install headers
headers_install()
{
echo "======================"
echo "Installing kernel headers"
set -x
(cd ${OUT_DIR} && \
${MAKE_PATH}make HOSTCFLAGS="${TARGET_INCLUDES}" HOSTLDFLAGS="${TARGET_LINCLUDES}" ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} O=${OUT_DIR} ${CC_ARG} ${MAKE_ARGS} headers_install)
set +x
}
# Building Kernel
build_kernel()
{
echo "======================"
echo "Building kernel"
set -x
if [ -f "${ROOT_DIR}/prebuilts/build-tools/linux-x86/bin/toybox" ]; then
NCORES=$(${ROOT_DIR}/prebuilts/build-tools/linux-x86/bin/toybox nproc)
else
NCORES=8
fi
(cd ${OUT_DIR} && \
${MAKE_PATH}make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} HOSTCFLAGS="${TARGET_INCLUDES}" HOSTLDFLAGS="${TARGET_LINCLUDES}" O=${OUT_DIR} ${CC_ARG} ${MAKE_ARGS} -j${NCORES})
set +x
}
# Modules Install
modules_install()
{
echo "======================"
echo "Installing kernel modules"
rm -rf ${MODULES_STAGING_DIR}
mkdir -p ${MODULES_STAGING_DIR}
set -x
(cd ${OUT_DIR} && \
${MAKE_PATH}make O=${OUT_DIR} ${CC_ARG} INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=${MODULES_STAGING_DIR} ${MAKE_ARGS} modules_install)
set +x
}
archive_kernel_modules()
{
echo "======================"
pushd ${KERNEL_MODULES_OUT}
# Zip the vendor-ramdisk kernel modules
FINAL_RAMDISK_KERNEL_MODULES=""
for MODULE in ${VENDOR_RAMDISK_KERNEL_MODULES}; do
if [ -f "${MODULE}" ]; then
FINAL_RAMDISK_KERNEL_MODULES="${FINAL_RAMDISK_KERNEL_MODULES} ${MODULE}"
fi
done
echo "Archiving vendor ramdisk kernel modules: "
echo ${FINAL_RAMDISK_KERNEL_MODULES}
if [ ! -z "${FINAL_RAMDISK_KERNEL_MODULES}" ]; then
zip -rq ${VENDOR_RAMDISK_KERNEL_MODULES_ARCHIVE} ${FINAL_RAMDISK_KERNEL_MODULES}
fi
# Filter-out the modules in vendor-ramdisk and zip the vendor kernel modules
VENDOR_KERNEL_MODULES=`ls *.ko`
for MODULE in ${FINAL_RAMDISK_KERNEL_MODULES}; do
VENDOR_KERNEL_MODULES=`echo ${VENDOR_KERNEL_MODULES} | sed "s/${MODULE}//g"`
done
echo "Archiving vendor kernel modules: "
echo ${VENDOR_KERNEL_MODULES}
# Also package the modules.blocklist file
set -x
BLOCKLIST_FILE=""
if [ -f "modules.blocklist" ]; then
BLOCKLIST_FILE="modules.blocklist"
fi
zip -rq ${VENDOR_KERNEL_MODULES_ARCHIVE} ${VENDOR_KERNEL_MODULES} ${BLOCKLIST_FILE}
set +x
popd
}
copy_modules_to_prebuilt()
{
PREBUILT_OUT=$1
# Clean the DLKM directory to remove stale modules
rm -rf ${KERNEL_MODULES_OUT}/*.ko
rm -rf ${KERNEL_MODULES_OUT}/*.zip
mkdir -p ${KERNEL_MODULES_OUT}
MODULES=$(find ${MODULES_STAGING_DIR} -type f -name "*.ko")
if ${HAS_MODULES}; then
echo "======================"
echo " Copying modules files"
for FILE in ${MODULES}; do
echo "${FILE#${KERNEL_MODULES_OUT}/}"
cp -p ${FILE} ${KERNEL_MODULES_OUT}
# Copy for prebuilt
if [ ! -e ${PREBUILT_OUT}/${KERNEL_MODULES_OUT} ]; then
mkdir -p ${PREBUILT_OUT}/${KERNEL_MODULES_OUT}
fi
cp -p ${FILE} ${PREBUILT_OUT}/${KERNEL_MODULES_OUT}
done
fi
echo "Copying module signing certificate into prebuilt"
if [[ ! -e ${PREBUILT_OUT}/certs ]]; then
mkdir -p ${PREBUILT_OUT}/certs
fi
cp -p ${OUT_DIR}/certs/*.* ${PREBUILT_OUT}/certs/
# Copy the modules.blocklist file
set -x
BLOCKLIST_FILE=${PWD}/device/qcom/kernelscripts/modules_blocklist/modules.blocklist.${TARGET_PRODUCT}
if [ -f "${BLOCKLIST_FILE}" ]; then
cp ${BLOCKLIST_FILE} ${KERNEL_MODULES_OUT}/modules.blocklist
sed -i -e '/blocklist/ s/-/_/g' ${KERNEL_MODULES_OUT}/modules.blocklist
fi
set +x
}
copy_all_to_prebuilt()
{
PREBUILT_OUT=$1
echo ${PREBUILT_OUT}
if [[ ! -e ${PREBUILT_OUT} ]]; then
mkdir -p ${PREBUILT_OUT}
fi
if ${HAS_MODULES}; then
copy_modules_to_prebuilt ${PREBUILT_OUT}
fi
#copy necessary files from the out directory
echo "============="
echo "Copying files to prebuilt"
for FILE in ${FILES}; do
if [ -f ${OUT_DIR}/${FILE} ]; then
# Copy for prebuilt
echo "$FILE ${PREBUILT_OUT}"
cp -p ${OUT_DIR}/${FILE} ${PREBUILT_OUT}/
echo $FILE copied to ${PREBUILT_OUT}
else
echo "$FILE does not exist, skipping"
fi
done
#copy kernel image
echo "============="
echo "Copying kernel image to prebuilt"
if [ ! -e ${PREBUILT_OUT}/${IMAGE_FILE_PATH} ]; then
mkdir -p ${PREBUILT_OUT}/${IMAGE_FILE_PATH}
fi
cp -p ${OUT_DIR}/${IMAGE_FILE_PATH}/${PREBUILT_KERNEL_IMAGE} ${PREBUILT_OUT}/${IMAGE_FILE_PATH}/${PREBUILT_KERNEL_IMAGE}
#copy dtbo images to prebuilt
if [ -d ${OUT_DIR}/${IMAGE_FILE_PATH}/dts/vendor/${DTS_VENDOR} ]; then
echo "============="
echo "Copying target dtb/dtbo files to prebuilt"
if [ ! -e ${PREBUILT_OUT}/${IMAGE_FILE_PATH}/dts/vendor/${DTS_VENDOR} ]; then
mkdir -p ${PREBUILT_OUT}/${IMAGE_FILE_PATH}/dts/vendor/${DTS_VENDOR}
fi
cp -p -r ${OUT_DIR}/${IMAGE_FILE_PATH}/dts/vendor/${DTS_VENDOR}/*.dtb ${PREBUILT_OUT}/${IMAGE_FILE_PATH}/dts/vendor/${DTS_VENDOR}/
dir=${OUT_DIR}/${IMAGE_FILE_PATH}/dts/vendor/${DTS_VENDOR}/
count=`ls -1 $dir/*.dtbo 2>/dev/null | wc -l`
if [ $count != 0 ]; then
cp -p -r ${OUT_DIR}/${IMAGE_FILE_PATH}/dts/vendor/${DTS_VENDOR}/*.dtbo ${PREBUILT_OUT}/${IMAGE_FILE_PATH}/dts/vendor/${DTS_VENDOR}/
fi
fi
#copy arch generated headers
echo "============="
echo "Copying arch-specific generated headers to prebuilt"
cp -p -r ${OUT_DIR}/${ARCH_GEN_HEADERS} ${PREBUILT_OUT}/${ARCH_GEN_HEADERS_LOC}
#copy kernel generated headers
echo "============="
echo "Copying kernel generated headers to prebuilt"
cp -p -r ${OUT_DIR}/${KERNEL_GEN_HEADERS} ${PREBUILT_OUT}
#copy userspace facing headers
echo "============"
echo "Copying userspace headers to prebuilt"
mkdir -p ${PREBUILT_OUT}/usr
cp -p -r ${KERNEL_HEADERS_INSTALL}/include ${PREBUILT_OUT}/usr
#copy kernel scripts
echo "============"
echo "Copying kernel scripts to prebuilt"
cp -p -r ${OUT_DIR}/${KERNEL_SCRIPTS} ${PREBUILT_OUT}
}
copy_from_prebuilt()
{
PREBUILT_OUT=$1
cd ${ROOT_DIR}
if [ ! -e ${OUT_DIR} ]; then
mkdir -p ${OUT_DIR}
fi
#Copy userspace headers
echo "============"
echo "Copying userspace headers from prebuilt"
mkdir -p ${KERNEL_HEADERS_INSTALL}
cp -p -r ${PREBUILT_OUT}/usr/include ${ROOT_DIR}/${KERNEL_HEADERS_INSTALL}
#Copy files, such as System.map, vmlinux, etc
echo "============"
echo "Copying kernel files from prebuilt"
cd ${PREBUILT_OUT}
for FILE in ${FILES}; do
if [ -f ${PREBUILT_OUT}/$FILE ]; then
# Copy for prebuilt
echo " $FILE ${PREBUILT_OUT}"
echo ${PREBUILT_OUT}/${FILE}
cp -p ${PREBUILT_OUT}/${FILE} ${OUT_DIR}/
echo $FILE copied to ${PREBUILT_OUT}
else
echo "$FILE does not exist, skipping"
fi
done
#copy kernel image
echo "============"
echo "Copying kernel image from prebuilt"
if [ ! -e ${OUT_DIR}/${IMAGE_FILE_PATH} ]; then
mkdir -p ${OUT_DIR}/${IMAGE_FILE_PATH}
fi
cp -p ${PREBUILT_OUT}/${IMAGE_FILE_PATH}/${PREBUILT_KERNEL_IMAGE} ${OUT_DIR}/${IMAGE_FILE_PATH}/${PREBUILT_KERNEL_IMAGE}
#copy dtbo images from prebuilt
if [ -d ${OUT_DIR}/${IMAGE_FILE_PATH}/dts/vendor/${DTS_VENDOR} ]; then
echo "============="
echo "Copying dtb/dtbo files from prebuilt"
if [ ! -e ${OUT_DIR}/${IMAGE_FILE_PATH}/dts/vendor/${DTS_VENDOR} ]; then
mkdir -p ${OUT_DIR}/${IMAGE_FILE_PATH}/dts/vendor/${DTS_VENDOR}
fi
cp -p -r ${PREBUILT_OUT}/${IMAGE_FILE_PATH}/dts/vendor/${DTS_VENDOR}/*.dtb ${OUT_DIR}/${IMAGE_FILE_PATH}/dts/vendor/${DTS_VENDOR}/
cp -p -r ${PREBUILT_OUT}/${IMAGE_FILE_PATH}/dts/vendor/${DTS_VENDOR}/*.dtbo ${OUT_DIR}/${IMAGE_FILE_PATH}/dts/vendor/${DTS_VENDOR}/
fi
#copy arch generated headers, and kernel generated headers
echo "============"
echo "Copying arch-specific generated headers from prebuilt"
cp -p -r ${PREBUILT_OUT}/${ARCH_GEN_HEADERS} ${OUT_DIR}/${ARCH_GEN_HEADERS_LOC}
echo "============"
echo "Copying kernel generated headers from prebuilt"
cp -p -r ${PREBUILT_OUT}/${KERNEL_GEN_HEADERS} ${OUT_DIR}
#copy modules
echo "============"
echo "Copying kernel modules from prebuilt"
cd ${ROOT_DIR}
MODULES=$(find ${PREBUILT_OUT} -type f -name "*.ko")
if [ ! -e ${KERNEL_MODULES_OUT} ]; then
mkdir -p ${KERNEL_MODULES_OUT}
fi
for FILE in ${MODULES}; do
echo "Copy ${FILE#${KERNEL_MODULES_OUT}/}"
cp -p ${FILE} ${KERNEL_MODULES_OUT}
done
#copy scripts directory
echo "============"
echo "Copying kernel scripts from prebuilt"
cp -p -r ${PREBUILT_OUT}/${KERNEL_SCRIPTS} ${OUT_DIR}
echo "Copying sign certificates from prebuilt"
if [[ ! -e ${OUT_DIR}/certs ]]; then
mkdir -p ${OUT_DIR}/certs
fi
cp -p -r ${PREBUILT_OUT}/certs ${OUT_DIR}
}
save_unstripped_modules()
{
echo "======================"
echo "Creating a copy of unstripped modules"
rm -rf ${UNSTRIPPED_DIR}
mkdir -p ${UNSTRIPPED_DIR}
set -x
(cd ${OUT_DIR} && \
${MAKE_PATH}make O=${OUT_DIR} ${CC_ARG} INSTALL_MOD_PATH=${UNSTRIPPED_DIR} ${MAKE_ARGS} modules_install)
MODULES=$(find ${UNSTRIPPED_DIR} -type f -name "*.ko")
for MODULE in ${MODULES}; do
# Replace all hyphens in module name with underscores
MODULE_NAME=$(basename ${MODULE})
CORRECTED_NAME=$(echo "${MODULE_NAME//-/$'_'}")
cp -p ${MODULE} ${UNSTRIPPED_DIR}
# Rename all modules with hyphens to names wth underscores
# As t32 expects module names with underscores to match
# symbols of the modules actually loaded, rename and save
# modules in unstripped folder for debugging
if [ ${MODULE_NAME} != ${CORRECTED_NAME} ]; then
mv ${UNSTRIPPED_DIR}/${MODULE_NAME} ${UNSTRIPPED_DIR}/${CORRECTED_NAME}
fi
done
# Remove the /lib/modules/$(uname -r) hierarchy
rm -rf ${UNSTRIPPED_DIR}/lib
set +x
}
#script starts executing here
if [ -n "${CC}" ]; then
CC_ARG="CC=${CC}"
fi
#choose between secondary and primary kernel image
if [[ ${DEFCONFIG} == *"perf_defconfig" ]]; then
KERNEL_BINS=${SECONDARY_KERN_BINS}
else
KERNEL_BINS=${PRIMARY_KERN_BINS}
fi
#use prebuilts if we want to use them, and they are available
if [ ! -z ${USE_PREBUILT_KERNEL} ] && [ -d ${KERNEL_BINS} ]; then
make_defconfig
copy_from_prebuilt ${KERNEL_BINS}
exit 0
fi
#use kernel source for building
if [ "${HEADERS_INSTALL}" -ne "0" ]; then
make_defconfig
headers_install
else
make_defconfig
headers_install
build_kernel
if ${HAS_MODULES}; then
modules_install
fi
copy_all_to_prebuilt ${KERNEL_BINS}
if ${HAS_MODULES}; then
archive_kernel_modules
save_unstripped_modules
fi
fi
exit 0