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