blob: aaf26a2e206346b104fbbaa4ff9539b3907a897f [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
49# Override compiler to be used. (e.g. CC=clang)
50#
51# LD
52# Override linker (flags) to be used.
53#
54# Environment variables to influence the stages of the kernel build.
55#
56# SKIP_MRPROPER
57# if defined, skip `make mrproper`
58#
59# SKIP_DEFCONFIG
60# if defined, skip `make defconfig`
61#
Matthias Maenniche4edd6f2019-02-13 12:36:27 +000062# PRE_DEFCONFIG_CMDS
63# Command evaluated before `make defconfig`
64#
Matthias Maennich6652d742019-02-01 22:20:44 +000065# POST_DEFCONFIG_CMDS
66# Command evaluated after `make defconfig` and before `make`.
67#
Matthias Maenniche4edd6f2019-02-13 12:36:27 +000068# POST_KERNEL_BUILD_CMDS
69# Command evaluated after `make`.
70#
Matthias Maennich6652d742019-02-01 22:20:44 +000071# IN_KERNEL_MODULES
72# if defined, install kernel modules
73#
74# SKIP_EXT_MODULES
75# if defined, skip building and installing of external modules
76#
77# EXTRA_CMDS
78# Command evaluated after building and installing kernel and modules.
79#
80# SKIP_CP_KERNEL_HDR
81# if defined, skip installing kernel headers.
82#
83# Note: For historic reasons, internally, OUT_DIR will be copied into
84# COMMON_OUT_DIR, and OUT_DIR will be then set to
85# ${COMMON_OUT_DIR}/${KERNEL_DIR}. This has been done to accommodate existing
86# build.config files that expect ${OUT_DIR} to point to the output directory of
87# the kernel build.
88#
89# The kernel is built in ${COMMON_OUT_DIR}/${KERNEL_DIR}.
90# Out-of-tree modules are built in ${COMMON_OUT_DIR}/${EXT_MOD} where
91# ${EXT_MOD} is the path to the module source code.
92
93set -e
94
95# rel_path <to> <from>
96# Generate relative directory path to reach directory <to> from <from>
97function rel_path() {
98 local to=$1
99 local from=$2
100 local path=
101 local stem=
102 local prevstem=
103 [ -n "$to" ] || return 1
104 [ -n "$from" ] || return 1
105 to=$(readlink -e "$to")
106 from=$(readlink -e "$from")
107 [ -n "$to" ] || return 1
108 [ -n "$from" ] || return 1
109 stem=${from}/
110 while [ "${to#$stem}" == "${to}" -a "${stem}" != "${prevstem}" ]; do
111 prevstem=$stem
112 stem=$(readlink -e "${stem}/..")
113 [ "${stem%/}" == "${stem}" ] && stem=${stem}/
114 path=${path}../
115 done
116 echo ${path}${to#$stem}
117}
118
119export ROOT_DIR=$(readlink -f $(dirname $0)/..)
120
121# For module file Signing with the kernel (if needed)
122FILE_SIGN_BIN=scripts/sign-file
123SIGN_SEC=certs/signing_key.pem
124SIGN_CERT=certs/signing_key.x509
125SIGN_ALGO=sha512
126
127source "${ROOT_DIR}/build/envsetup.sh"
128
129export MAKE_ARGS=$@
130export COMMON_OUT_DIR=$(readlink -m ${OUT_DIR:-${ROOT_DIR}/out/${BRANCH}})
131export OUT_DIR=$(readlink -m ${COMMON_OUT_DIR}/${KERNEL_DIR})
132export MODULES_STAGING_DIR=$(readlink -m ${COMMON_OUT_DIR}/staging)
133export MODULES_PRIVATE_DIR=$(readlink -m ${COMMON_OUT_DIR}/private)
134export DIST_DIR=$(readlink -m ${DIST_DIR:-${COMMON_OUT_DIR}/dist})
135export UNSTRIPPED_DIR=${DIST_DIR}/unstripped
136export KERNEL_UAPI_HEADERS_DIR=$(readlink -m ${COMMON_OUT_DIR}/kernel_uapi_headers)
137
138cd ${ROOT_DIR}
139
140export CLANG_TRIPLE CROSS_COMPILE CROSS_COMPILE_ARM32 ARCH SUBARCH
141
142mkdir -p ${OUT_DIR}
143echo "========================================================"
144echo " Setting up for build"
145if [ -z "${SKIP_MRPROPER}" ] ; then
146 set -x
147 (cd ${KERNEL_DIR} && make O=${OUT_DIR} mrproper)
148 set +x
149fi
150
Petri Gynther0172f352019-02-12 11:52:50 -0800151if [ "${PRE_DEFCONFIG_CMDS}" != "" ]; then
152 echo "========================================================"
153 echo " Running pre-defconfig command(s):"
154 set -x
155 eval ${PRE_DEFCONFIG_CMDS}
156 set +x
157fi
158
Matthias Maennich6652d742019-02-01 22:20:44 +0000159if [ -z "${SKIP_DEFCONFIG}" ] ; then
160set -x
161(cd ${KERNEL_DIR} && make O=${OUT_DIR} ${DEFCONFIG})
162set +x
163
164if [ "${POST_DEFCONFIG_CMDS}" != "" ]; then
165 echo "========================================================"
166 echo " Running pre-make command(s):"
167 set -x
168 eval ${POST_DEFCONFIG_CMDS}
169 set +x
170fi
171fi
172
173echo "========================================================"
174echo " Building kernel"
175
176if [ -n "${CC}" ]; then
177 CC_ARG="CC=${CC}"
178fi
179
180if [ -n "${LD}" ]; then
181 LD_ARG="LD=${LD}"
182fi
183
184set -x
185(cd ${OUT_DIR} && \
186 make O=${OUT_DIR} ${CC_ARG} ${LD_ARG} -j$(nproc) $@)
187set +x
188
Petri Gynther0172f352019-02-12 11:52:50 -0800189if [ "${POST_KERNEL_BUILD_CMDS}" != "" ]; then
190 echo "========================================================"
191 echo " Running post-kernel-build command(s):"
192 set -x
193 eval ${POST_KERNEL_BUILD_CMDS}
194 set +x
195fi
196
Matthias Maennich6652d742019-02-01 22:20:44 +0000197rm -rf ${MODULES_STAGING_DIR}
198mkdir -p ${MODULES_STAGING_DIR}
199
200if [ -n "${IN_KERNEL_MODULES}" ]; then
201 echo "========================================================"
202 echo " Installing kernel modules into staging directory"
203
204 (cd ${OUT_DIR} && \
205 make O=${OUT_DIR} ${CC_ARG} ${LD_ARG} INSTALL_MOD_STRIP=1 \
206 INSTALL_MOD_PATH=${MODULES_STAGING_DIR} modules_install)
207fi
208
209if [[ -z "${SKIP_EXT_MODULES}" ]] && [[ "${EXT_MODULES}" != "" ]]; then
210 echo "========================================================"
211 echo " Building external modules and installing them into staging directory"
212
213 for EXT_MOD in ${EXT_MODULES}; do
214 # The path that we pass in via the variable M needs to be a relative path
215 # relative to the kernel source directory. The source files will then be
216 # looked for in ${KERNEL_DIR}/${EXT_MOD_REL} and the object files (i.e. .o
217 # and .ko) files will be stored in ${OUT_DIR}/${EXT_MOD_REL}. If we
218 # instead set M to an absolute path, then object (i.e. .o and .ko) files
219 # are stored in the module source directory which is not what we want.
220 EXT_MOD_REL=$(rel_path ${ROOT_DIR}/${EXT_MOD} ${KERNEL_DIR})
221 # The output directory must exist before we invoke make. Otherwise, the
222 # build system behaves horribly wrong.
223 mkdir -p ${OUT_DIR}/${EXT_MOD_REL}
224 set -x
225 make -C ${EXT_MOD} M=${EXT_MOD_REL} KERNEL_SRC=${ROOT_DIR}/${KERNEL_DIR} \
226 O=${OUT_DIR} ${CC_ARG} ${LD_ARG} -j$(nproc) "$@"
227 make -C ${EXT_MOD} M=${EXT_MOD_REL} KERNEL_SRC=${ROOT_DIR}/${KERNEL_DIR} \
228 O=${OUT_DIR} ${CC_ARG} ${LD_ARG} INSTALL_MOD_STRIP=1 \
229 INSTALL_MOD_PATH=${MODULES_STAGING_DIR} modules_install
230 set +x
231 done
232
233fi
234
235if [ "${EXTRA_CMDS}" != "" ]; then
236 echo "========================================================"
237 echo " Running extra build command(s):"
238 set -x
239 eval ${EXTRA_CMDS}
240 set +x
241fi
242
243OVERLAYS_OUT=""
244for ODM_DIR in ${ODM_DIRS}; do
245 OVERLAY_DIR=${ROOT_DIR}/device/${ODM_DIR}/overlays
246
247 if [ -d ${OVERLAY_DIR} ]; then
248 OVERLAY_OUT_DIR=${OUT_DIR}/overlays/${ODM_DIR}
249 mkdir -p ${OVERLAY_OUT_DIR}
250 make -C ${OVERLAY_DIR} DTC=${OUT_DIR}/scripts/dtc/dtc OUT_DIR=${OVERLAY_OUT_DIR}
251 OVERLAYS=$(find ${OVERLAY_OUT_DIR} -name "*.dtbo")
252 OVERLAYS_OUT="$OVERLAYS_OUT $OVERLAYS"
253 fi
254done
255
256mkdir -p ${DIST_DIR}
257echo "========================================================"
258echo " Copying files"
259for FILE in ${FILES}; do
260 if [ -f ${OUT_DIR}/${FILE} ]; then
261 echo " $FILE"
262 cp -p ${OUT_DIR}/${FILE} ${DIST_DIR}/
263 else
264 echo " $FILE does not exist, skipping"
265 fi
266done
267
268for FILE in ${OVERLAYS_OUT}; do
269 OVERLAY_DIST_DIR=${DIST_DIR}/$(dirname ${FILE#${OUT_DIR}/overlays/})
270 echo " ${FILE#${OUT_DIR}/}"
271 mkdir -p ${OVERLAY_DIST_DIR}
272 cp ${FILE} ${OVERLAY_DIST_DIR}/
273done
274
275MODULES=$(find ${MODULES_STAGING_DIR} -type f -name "*.ko")
276if [ -n "${MODULES}" ]; then
277 echo "========================================================"
278 echo " Copying modules files"
279 if [ -n "${IN_KERNEL_MODULES}" -o "${EXT_MODULES}" != "" ]; then
280 for FILE in ${MODULES}; do
281 echo " ${FILE#${MODULES_STAGING_DIR}/}"
282 cp -p ${FILE} ${DIST_DIR}
283 done
284 fi
285fi
286
287if [ "${UNSTRIPPED_MODULES}" != "" ]; then
288 echo "========================================================"
289 echo " Copying unstripped module files for debugging purposes (not loaded on device)"
290 mkdir -p ${UNSTRIPPED_DIR}
291 for MODULE in ${UNSTRIPPED_MODULES}; do
292 find ${MODULES_PRIVATE_DIR} -name ${MODULE} -exec cp {} ${UNSTRIPPED_DIR} \;
293 done
294fi
295
296if [ -z "${SKIP_CP_KERNEL_HDR}" ]; then
297 echo "========================================================"
298 echo " Installing UAPI kernel headers:"
299 mkdir -p "${KERNEL_UAPI_HEADERS_DIR}/usr"
300 make -C ${OUT_DIR} O=${OUT_DIR} ${CC_ARG} INSTALL_HDR_PATH="${KERNEL_UAPI_HEADERS_DIR}/usr" -j$(nproc) headers_install
301 # The kernel makefiles create files named ..install.cmd and .install which
302 # are only side products. We don't want those. Let's delete them.
303 find ${KERNEL_UAPI_HEADERS_DIR} \( -name ..install.cmd -o -name .install \) -exec rm '{}' +
304 KERNEL_UAPI_HEADERS_TAR=${DIST_DIR}/kernel-uapi-headers.tar.gz
305 echo " Copying kernel UAPI headers to ${KERNEL_UAPI_HEADERS_TAR}"
306 tar -czf ${KERNEL_UAPI_HEADERS_TAR} --directory=${KERNEL_UAPI_HEADERS_DIR} usr/
307fi
308
309if [ -z "${SKIP_CP_KERNEL_HDR}" ] ; then
310 echo "========================================================"
311 KERNEL_HEADERS_TAR=${DIST_DIR}/kernel-headers.tar.gz
312 echo " Copying kernel headers to ${KERNEL_HEADERS_TAR}"
313 TMP_DIR="/tmp"
314 TMP_KERNEL_HEADERS_CHILD="kernel-headers"
315 TMP_KERNEL_HEADERS_DIR=$TMP_DIR/$TMP_KERNEL_HEADERS_CHILD
316 CURDIR=$(pwd)
317 mkdir -p $TMP_KERNEL_HEADERS_DIR
318 cd $ROOT_DIR/$KERNEL_DIR; find arch -name *.h -exec cp --parents {} $TMP_KERNEL_HEADERS_DIR \;
319 cd $ROOT_DIR/$KERNEL_DIR; find include -name *.h -exec cp --parents {} $TMP_KERNEL_HEADERS_DIR \;
320 cd $OUT_DIR; find -name *.h -exec cp --parents {} $TMP_KERNEL_HEADERS_DIR \;
321 tar -czvf $KERNEL_HEADERS_TAR --directory=$TMP_DIR $TMP_KERNEL_HEADERS_CHILD > /dev/null 2>&1
322 rm -rf $TMP_KERNEL_HEADERS_DIR
323 cd $CURDIR
324fi
325
326echo "========================================================"
327echo " Files copied to ${DIST_DIR}"
328
329# No trace_printk use on build server build
330if readelf -a ${DIST_DIR}/vmlinux 2>&1 | grep -q trace_printk_fmt; then
331 echo "========================================================"
332 echo "WARN: Found trace_printk usage in vmlinux."
333 echo ""
334 echo "trace_printk will cause trace_printk_init_buffers executed in kernel"
335 echo "start, which will increase memory and lead warning shown during boot."
336 echo "We should not carry trace_printk in production kernel."
337 echo ""
338 if [ ! -z "${STOP_SHIP_TRACEPRINTK}" ]; then
339 echo "ERROR: stop ship on trace_printk usage." 1>&2
340 exit 1
341 fi
342fi