blob: 67d4d1615f56d5b45b1084c406201be50421f0cd [file] [log] [blame]
Matthias Maennich6652d742019-02-01 22:20:44 +00001#!/bin/bash
2
3# Copyright (C) 2019 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Usage:
18# build/build.sh <make options>*
19# or:
20# OUT_DIR=<out dir> DIST_DIR=<dist dir> build/build.sh <make options>*
21#
22# Example:
23# OUT_DIR=output DIST_DIR=dist build/build.sh -j24
24#
25#
26# The following environment variables are considered during execution:
27#
28# BUILD_CONFIG
29# Build config file to initialize the build environment from. The location
30# is to be defined relative to the repo root directory.
31# Defaults to 'build.config'.
32#
33# OUT_DIR
34# Base output directory for the kernel build.
35# Defaults to <REPO_ROOT>/out/<BRANCH>.
36#
37# DIST_DIR
38# Base output directory for the kernel distribution.
39# Defaults to <OUT_DIR>/dist
40#
41# EXT_MODULES
42# Space separated list of external kernel modules to be build.
43#
44# UNSTRIPPED_MODULES
45# Space separated list of modules to be copied to <DIST_DIR>/unstripped
46# for debugging purposes.
47#
48# CC
Matthias Maennich0646c8d2019-03-20 11:03:51 +000049# Override compiler to be used. (e.g. CC=clang) Specifying CC=gcc
50# effectively unsets CC to fall back to the default gcc detected by kbuild
51# (including any target triplet). To use a custom 'gcc' from PATH, use an
52# absolute path, e.g. CC=/usr/local/bin/gcc
Matthias Maennich6652d742019-02-01 22:20:44 +000053#
54# LD
55# Override linker (flags) to be used.
56#
Matthias Maennich44509b02019-05-28 12:26:58 +010057# ABI_DEFINITION
58# Location of the abi definition file relative to <REPO_ROOT>/KERNEL_DIR
59# If defined (usually in build.config), also copy that abi definition to
60# <OUT_DIR>/dist/abi.out when creating the distribution.
61#
Matthias Maennich6652d742019-02-01 22:20:44 +000062# Environment variables to influence the stages of the kernel build.
63#
64# SKIP_MRPROPER
65# if defined, skip `make mrproper`
66#
67# SKIP_DEFCONFIG
68# if defined, skip `make defconfig`
69#
Matthias Maenniche4edd6f2019-02-13 12:36:27 +000070# PRE_DEFCONFIG_CMDS
71# Command evaluated before `make defconfig`
72#
Matthias Maennich6652d742019-02-01 22:20:44 +000073# POST_DEFCONFIG_CMDS
74# Command evaluated after `make defconfig` and before `make`.
75#
Matthias Maenniche4edd6f2019-02-13 12:36:27 +000076# POST_KERNEL_BUILD_CMDS
77# Command evaluated after `make`.
78#
Matthias Maennich6652d742019-02-01 22:20:44 +000079# IN_KERNEL_MODULES
80# if defined, install kernel modules
81#
82# SKIP_EXT_MODULES
83# if defined, skip building and installing of external modules
84#
85# EXTRA_CMDS
86# Command evaluated after building and installing kernel and modules.
87#
88# SKIP_CP_KERNEL_HDR
89# if defined, skip installing kernel headers.
90#
Hridya Valsarajue46a9372019-05-13 16:38:14 -070091# BUILD_BOOT_IMG
92# if defined, build a boot.img binary that can be flashed into the 'boot'
93# partition of an Android device. The boot image contains a header as per the
94# format defined by https://source.android.com/devices/bootloader/boot-image-header
95# followed by several components like kernel, ramdisk, DTB etc. The ramdisk
96# component comprises of a GKI ramdisk cpio archive concatenated with a
97# vendor ramdisk cpio archive which is then gzipped. It is expected that
98# all components are present in ${DIST_DIR}.
99#
100# When the BUILD_BOOT_IMG flag is defined, the following flags that point to the
101# various components needed to build a boot.img also need to be defined.
102# - MKBOOTIMG_PATH=<path to the mkbootimg.py script which builds boot.img>
103# - GKI_RAMDISK_PREBUILT_BINARY=<Name of the GKI ramdisk prebuilt which includes
104# the generic ramdisk components like init and the non-device-specific rc files>
105# - VENDOR_RAMDISK_BINARY=<Name of the vendor ramdisk binary which includes the
106# device-specific components of ramdisk like the fstab file and the
107# device-specific rc files.>
108# - KERNEL_BINARY=<name of kernel binary, eg. Image.lz4, Image.gz etc>
109# - BOOT_IMAGE_HEADER_VERSION=<version of the boot image header>
110#
Matthias Maennich6652d742019-02-01 22:20:44 +0000111# Note: For historic reasons, internally, OUT_DIR will be copied into
112# COMMON_OUT_DIR, and OUT_DIR will be then set to
113# ${COMMON_OUT_DIR}/${KERNEL_DIR}. This has been done to accommodate existing
114# build.config files that expect ${OUT_DIR} to point to the output directory of
115# the kernel build.
116#
117# The kernel is built in ${COMMON_OUT_DIR}/${KERNEL_DIR}.
118# Out-of-tree modules are built in ${COMMON_OUT_DIR}/${EXT_MOD} where
119# ${EXT_MOD} is the path to the module source code.
120
121set -e
122
123# rel_path <to> <from>
124# Generate relative directory path to reach directory <to> from <from>
125function rel_path() {
126 local to=$1
127 local from=$2
128 local path=
129 local stem=
130 local prevstem=
131 [ -n "$to" ] || return 1
132 [ -n "$from" ] || return 1
133 to=$(readlink -e "$to")
134 from=$(readlink -e "$from")
135 [ -n "$to" ] || return 1
136 [ -n "$from" ] || return 1
137 stem=${from}/
138 while [ "${to#$stem}" == "${to}" -a "${stem}" != "${prevstem}" ]; do
139 prevstem=$stem
140 stem=$(readlink -e "${stem}/..")
141 [ "${stem%/}" == "${stem}" ] && stem=${stem}/
142 path=${path}../
143 done
144 echo ${path}${to#$stem}
145}
146
147export ROOT_DIR=$(readlink -f $(dirname $0)/..)
148
149# For module file Signing with the kernel (if needed)
150FILE_SIGN_BIN=scripts/sign-file
151SIGN_SEC=certs/signing_key.pem
152SIGN_CERT=certs/signing_key.x509
153SIGN_ALGO=sha512
154
155source "${ROOT_DIR}/build/envsetup.sh"
156
157export MAKE_ARGS=$@
Matthias Maennich6652d742019-02-01 22:20:44 +0000158export MODULES_STAGING_DIR=$(readlink -m ${COMMON_OUT_DIR}/staging)
159export MODULES_PRIVATE_DIR=$(readlink -m ${COMMON_OUT_DIR}/private)
Matthias Maennich6652d742019-02-01 22:20:44 +0000160export UNSTRIPPED_DIR=${DIST_DIR}/unstripped
161export KERNEL_UAPI_HEADERS_DIR=$(readlink -m ${COMMON_OUT_DIR}/kernel_uapi_headers)
162
163cd ${ROOT_DIR}
164
165export CLANG_TRIPLE CROSS_COMPILE CROSS_COMPILE_ARM32 ARCH SUBARCH
166
Matthias Maennich0646c8d2019-03-20 11:03:51 +0000167# CC=gcc is effectively a fallback to the default gcc including any target
168# triplets. If the user wants to use a custom compiler, they are still able to
169# pass an absolute path, e.g. CC=/usr/bin/gcc.
170[ "${CC}" == "gcc" ] && unset CC
171
Matthias Maennich151047e2019-03-18 15:01:03 +0000172if [ -n "${CC}" ]; then
173 CC_ARG="CC=${CC}"
174fi
175
176if [ -n "${LD}" ]; then
177 LD_ARG="LD=${LD}"
178fi
179
180CC_LD_ARG="${CC_ARG} ${LD_ARG}"
181
Matthias Maennich6652d742019-02-01 22:20:44 +0000182mkdir -p ${OUT_DIR}
183echo "========================================================"
184echo " Setting up for build"
185if [ -z "${SKIP_MRPROPER}" ] ; then
186 set -x
Matthias Maennich151047e2019-03-18 15:01:03 +0000187 (cd ${KERNEL_DIR} && make ${CC_LD_ARG} O=${OUT_DIR} mrproper)
Matthias Maennich6652d742019-02-01 22:20:44 +0000188 set +x
189fi
190
Petri Gynther0172f352019-02-12 11:52:50 -0800191if [ "${PRE_DEFCONFIG_CMDS}" != "" ]; then
192 echo "========================================================"
193 echo " Running pre-defconfig command(s):"
194 set -x
195 eval ${PRE_DEFCONFIG_CMDS}
196 set +x
197fi
198
Matthias Maennich6652d742019-02-01 22:20:44 +0000199if [ -z "${SKIP_DEFCONFIG}" ] ; then
200set -x
Matthias Maennich151047e2019-03-18 15:01:03 +0000201(cd ${KERNEL_DIR} && make ${CC_LD_ARG} O=${OUT_DIR} ${DEFCONFIG})
Matthias Maennich6652d742019-02-01 22:20:44 +0000202set +x
203
204if [ "${POST_DEFCONFIG_CMDS}" != "" ]; then
205 echo "========================================================"
206 echo " Running pre-make command(s):"
207 set -x
208 eval ${POST_DEFCONFIG_CMDS}
209 set +x
210fi
211fi
212
213echo "========================================================"
214echo " Building kernel"
215
Matthias Maennich6652d742019-02-01 22:20:44 +0000216set -x
217(cd ${OUT_DIR} && \
Matthias Maennich151047e2019-03-18 15:01:03 +0000218 make O=${OUT_DIR} ${CC_LD_ARG} -j$(nproc) $@)
Matthias Maennich6652d742019-02-01 22:20:44 +0000219set +x
220
Petri Gynther0172f352019-02-12 11:52:50 -0800221if [ "${POST_KERNEL_BUILD_CMDS}" != "" ]; then
222 echo "========================================================"
223 echo " Running post-kernel-build command(s):"
224 set -x
225 eval ${POST_KERNEL_BUILD_CMDS}
226 set +x
227fi
228
Matthias Maennich6652d742019-02-01 22:20:44 +0000229rm -rf ${MODULES_STAGING_DIR}
230mkdir -p ${MODULES_STAGING_DIR}
231
232if [ -n "${IN_KERNEL_MODULES}" ]; then
233 echo "========================================================"
234 echo " Installing kernel modules into staging directory"
235
236 (cd ${OUT_DIR} && \
Matthias Maennich151047e2019-03-18 15:01:03 +0000237 make O=${OUT_DIR} ${CC_LD_ARG} INSTALL_MOD_STRIP=1 \
Matthias Maennich6652d742019-02-01 22:20:44 +0000238 INSTALL_MOD_PATH=${MODULES_STAGING_DIR} modules_install)
239fi
240
241if [[ -z "${SKIP_EXT_MODULES}" ]] && [[ "${EXT_MODULES}" != "" ]]; then
242 echo "========================================================"
243 echo " Building external modules and installing them into staging directory"
244
245 for EXT_MOD in ${EXT_MODULES}; do
246 # The path that we pass in via the variable M needs to be a relative path
247 # relative to the kernel source directory. The source files will then be
248 # looked for in ${KERNEL_DIR}/${EXT_MOD_REL} and the object files (i.e. .o
249 # and .ko) files will be stored in ${OUT_DIR}/${EXT_MOD_REL}. If we
250 # instead set M to an absolute path, then object (i.e. .o and .ko) files
251 # are stored in the module source directory which is not what we want.
252 EXT_MOD_REL=$(rel_path ${ROOT_DIR}/${EXT_MOD} ${KERNEL_DIR})
253 # The output directory must exist before we invoke make. Otherwise, the
254 # build system behaves horribly wrong.
255 mkdir -p ${OUT_DIR}/${EXT_MOD_REL}
256 set -x
257 make -C ${EXT_MOD} M=${EXT_MOD_REL} KERNEL_SRC=${ROOT_DIR}/${KERNEL_DIR} \
Matthias Maennich151047e2019-03-18 15:01:03 +0000258 O=${OUT_DIR} ${CC_LD_ARG} -j$(nproc) "$@"
Matthias Maennich6652d742019-02-01 22:20:44 +0000259 make -C ${EXT_MOD} M=${EXT_MOD_REL} KERNEL_SRC=${ROOT_DIR}/${KERNEL_DIR} \
Matthias Maennich151047e2019-03-18 15:01:03 +0000260 O=${OUT_DIR} ${CC_LD_ARG} INSTALL_MOD_STRIP=1 \
Matthias Maennich6652d742019-02-01 22:20:44 +0000261 INSTALL_MOD_PATH=${MODULES_STAGING_DIR} modules_install
262 set +x
263 done
264
265fi
266
267if [ "${EXTRA_CMDS}" != "" ]; then
268 echo "========================================================"
269 echo " Running extra build command(s):"
270 set -x
271 eval ${EXTRA_CMDS}
272 set +x
273fi
274
275OVERLAYS_OUT=""
276for ODM_DIR in ${ODM_DIRS}; do
277 OVERLAY_DIR=${ROOT_DIR}/device/${ODM_DIR}/overlays
278
279 if [ -d ${OVERLAY_DIR} ]; then
280 OVERLAY_OUT_DIR=${OUT_DIR}/overlays/${ODM_DIR}
281 mkdir -p ${OVERLAY_OUT_DIR}
282 make -C ${OVERLAY_DIR} DTC=${OUT_DIR}/scripts/dtc/dtc OUT_DIR=${OVERLAY_OUT_DIR}
283 OVERLAYS=$(find ${OVERLAY_OUT_DIR} -name "*.dtbo")
284 OVERLAYS_OUT="$OVERLAYS_OUT $OVERLAYS"
285 fi
286done
287
288mkdir -p ${DIST_DIR}
289echo "========================================================"
290echo " Copying files"
291for FILE in ${FILES}; do
292 if [ -f ${OUT_DIR}/${FILE} ]; then
293 echo " $FILE"
294 cp -p ${OUT_DIR}/${FILE} ${DIST_DIR}/
295 else
296 echo " $FILE does not exist, skipping"
297 fi
298done
299
300for FILE in ${OVERLAYS_OUT}; do
301 OVERLAY_DIST_DIR=${DIST_DIR}/$(dirname ${FILE#${OUT_DIR}/overlays/})
302 echo " ${FILE#${OUT_DIR}/}"
303 mkdir -p ${OVERLAY_DIST_DIR}
304 cp ${FILE} ${OVERLAY_DIST_DIR}/
305done
306
307MODULES=$(find ${MODULES_STAGING_DIR} -type f -name "*.ko")
308if [ -n "${MODULES}" ]; then
309 echo "========================================================"
310 echo " Copying modules files"
311 if [ -n "${IN_KERNEL_MODULES}" -o "${EXT_MODULES}" != "" ]; then
312 for FILE in ${MODULES}; do
313 echo " ${FILE#${MODULES_STAGING_DIR}/}"
314 cp -p ${FILE} ${DIST_DIR}
315 done
316 fi
317fi
318
319if [ "${UNSTRIPPED_MODULES}" != "" ]; then
320 echo "========================================================"
321 echo " Copying unstripped module files for debugging purposes (not loaded on device)"
322 mkdir -p ${UNSTRIPPED_DIR}
323 for MODULE in ${UNSTRIPPED_MODULES}; do
324 find ${MODULES_PRIVATE_DIR} -name ${MODULE} -exec cp {} ${UNSTRIPPED_DIR} \;
325 done
326fi
327
328if [ -z "${SKIP_CP_KERNEL_HDR}" ]; then
329 echo "========================================================"
330 echo " Installing UAPI kernel headers:"
331 mkdir -p "${KERNEL_UAPI_HEADERS_DIR}/usr"
Matthias Maennich151047e2019-03-18 15:01:03 +0000332 make -C ${OUT_DIR} O=${OUT_DIR} ${CC_LD_ARG} INSTALL_HDR_PATH="${KERNEL_UAPI_HEADERS_DIR}/usr" -j$(nproc) headers_install
Matthias Maennich6652d742019-02-01 22:20:44 +0000333 # The kernel makefiles create files named ..install.cmd and .install which
334 # are only side products. We don't want those. Let's delete them.
335 find ${KERNEL_UAPI_HEADERS_DIR} \( -name ..install.cmd -o -name .install \) -exec rm '{}' +
336 KERNEL_UAPI_HEADERS_TAR=${DIST_DIR}/kernel-uapi-headers.tar.gz
337 echo " Copying kernel UAPI headers to ${KERNEL_UAPI_HEADERS_TAR}"
338 tar -czf ${KERNEL_UAPI_HEADERS_TAR} --directory=${KERNEL_UAPI_HEADERS_DIR} usr/
339fi
340
341if [ -z "${SKIP_CP_KERNEL_HDR}" ] ; then
Matthias Maennichbec886a2019-04-15 07:23:47 +0100342 echo "========================================================"
343 KERNEL_HEADERS_TAR=${DIST_DIR}/kernel-headers.tar.gz
344 echo " Copying kernel headers to ${KERNEL_HEADERS_TAR}"
345 pushd $ROOT_DIR/$KERNEL_DIR
346 find arch include $OUT_DIR -name *.h -print0 \
347 | tar -czf $KERNEL_HEADERS_TAR \
348 --absolute-names \
349 --dereference \
350 --transform "s,.*$OUT_DIR,," \
351 --transform "s,^,kernel-headers/," \
352 --null -T -
353 popd
Matthias Maennich6652d742019-02-01 22:20:44 +0000354fi
355
Matthias Maennich44509b02019-05-28 12:26:58 +0100356# Copy the abi_${arch}.out file from the sources into the dist dir
357if [ -n "${ABI_DEFINITION}" ]; then
358 echo "========================================================"
359 echo " Copying abi definition to ${DIST_DIR}/abi.out"
360 pushd $ROOT_DIR/$KERNEL_DIR
361 cp "${ABI_DEFINITION}" ${DIST_DIR}/abi.out
362 popd
363fi
364
Matthias Maennich6652d742019-02-01 22:20:44 +0000365echo "========================================================"
366echo " Files copied to ${DIST_DIR}"
367
Hridya Valsarajue46a9372019-05-13 16:38:14 -0700368if [ ! -z "${BUILD_BOOT_IMG}" ] ; then
369
370 DTB_FILE_LIST=$(find ${DIST_DIR} -name "*.dtb")
371 if [ -z "${DTB_FILE_LIST}" ]; then
372 echo "No *.dtb files found in ${DIST_DIR}"
373 exit 1
374 fi
375 cat $DTB_FILE_LIST > ${DIST_DIR}/dtb.img
376
377 if [ ! -f "${DIST_DIR}/$GKI_RAMDISK_PREBUILT_BINARY" ]; then
378 echo "GKI ramdisk prebuilt binary" \
379 "(GKI_RAMDISK_PREBUILT_BINARY = $GKI_RAMDISK_PREBUILT_BINARY)" \
380 "not present in ${DIST_DIR}"
381 exit 1
382 fi
383 if [ ! -f "${DIST_DIR}/$VENDOR_RAMDISK_BINARY" ]; then
384 echo "vendor ramdisk binary(VENDOR_RAMDISK_BINARY = $VENDOR_RAMDISK_BINARY)" \
385 "not present in ${DIST_DIR}"
386 exit 1
387 fi
388
389 cat ${DIST_DIR}/$GKI_RAMDISK_PREBUILT_BINARY ${DIST_DIR}/$VENDOR_RAMDISK_BINARY \
390 > ${DIST_DIR}/ramdisk.cpio
391 gzip -f ${DIST_DIR}/ramdisk.cpio > ${DIST_DIR}/ramdisk
392
393 if [ ! -x "$MKBOOTIMG_PATH" ]; then
394 echo "mkbootimg.py script not found or not executable. MKBOOTIMG_PATH = $MKBOOTIMG_PATH"
395 exit 1
396 fi
397
398 if [ ! -f "${DIST_DIR}/$KERNEL_BINARY" ]; then
399 echo "kernel binary(KERNEL_BINARY = $KERNEL_BINARY) not present in ${DIST_DIR}"
400 exit 1
401 fi
402
403 if [ -z "${BOOT_IMAGE_HEADER_VERSION}" ]; then
404 echo "BOOT_IMAGE_HEADER_VERSION must specify the boot image header version"
405 exit 1
406 fi
407
408 (set -x; $MKBOOTIMG_PATH --kernel ${DIST_DIR}/$KERNEL_BINARY \
409 --ramdisk ${DIST_DIR}/ramdisk \
410 --dtb ${DIST_DIR}/dtb.img --header_version $BOOT_IMAGE_HEADER_VERSION \
411 -o ${DIST_DIR}/boot.img
412 )
413 set +x
414 echo "boot image created at ${DIST_DIR}/boot.img"
415fi
416
417
Matthias Maennich6652d742019-02-01 22:20:44 +0000418# No trace_printk use on build server build
419if readelf -a ${DIST_DIR}/vmlinux 2>&1 | grep -q trace_printk_fmt; then
420 echo "========================================================"
421 echo "WARN: Found trace_printk usage in vmlinux."
422 echo ""
423 echo "trace_printk will cause trace_printk_init_buffers executed in kernel"
424 echo "start, which will increase memory and lead warning shown during boot."
425 echo "We should not carry trace_printk in production kernel."
426 echo ""
427 if [ ! -z "${STOP_SHIP_TRACEPRINTK}" ]; then
428 echo "ERROR: stop ship on trace_printk usage." 1>&2
429 exit 1
430 fi
431fi