blob: 36ed01acda9100b4dbb5e8d418fbf50ae40528c6 [file] [log] [blame]
Mike Frysingerbaae8eb2013-01-15 01:34:26 -05001#!/bin/bash
Chris Sosa2b736002012-02-12 16:16:08 -08002# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rspangler@google.comd74220d2009-10-09 20:56:14 +00003# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
rspangler@google.comd74220d2009-10-09 20:56:14 +00006# All scripts should die on error unless commands are specifically excepted
7# by prefixing with '!' or surrounded by 'set +e' / 'set -e'.
rspangler@google.comd74220d2009-10-09 20:56:14 +00008
9# The number of jobs to pass to tools that can run in parallel (such as make
10# and dpkg-buildpackage
Alex Deymo135faa12015-01-21 11:20:13 -080011if [[ -z ${NUM_JOBS:-} ]]; then
Brian Harring28bb01f2012-05-09 15:26:09 -070012 NUM_JOBS=$(grep -c "^processor" /proc/cpuinfo)
13fi
14# Ensure that any sub scripts we invoke get the max proc count.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050015export NUM_JOBS
rspangler@google.comd74220d2009-10-09 20:56:14 +000016
Greg Spencer798d75f2011-02-01 22:04:49 -080017# Make sure we have the location and name of the calling script, using
18# the current value if it is already set.
Alex Deymo7ba56be2015-02-12 14:28:06 -080019: ${SCRIPT_LOCATION:=$(dirname "$(readlink -f -- "$0")")}
20: ${SCRIPT_NAME:=$(basename -- "$0")}
rspangler@google.comd74220d2009-10-09 20:56:14 +000021
Anton Staaf30acb0b2011-01-26 16:00:20 -080022# Detect whether we're inside a chroot or not
David James47e0e8a2015-03-11 15:24:10 -070023CHROOT_VERSION_FILE=/etc/cros_chroot_version
24if [[ -e ${CHROOT_VERSION_FILE} ]]; then
Anton Staaf30acb0b2011-01-26 16:00:20 -080025 INSIDE_CHROOT=1
rspangler@google.comd74220d2009-10-09 20:56:14 +000026else
Anton Staaf30acb0b2011-01-26 16:00:20 -080027 INSIDE_CHROOT=0
rspangler@google.comd74220d2009-10-09 20:56:14 +000028fi
29
Mike Frysinger669b28b2012-02-07 18:01:00 -050030# Determine and set up variables needed for fancy color output (if supported).
31V_BOLD_RED=
32V_BOLD_GREEN=
33V_BOLD_YELLOW=
Mike Frysinger669b28b2012-02-07 18:01:00 -050034V_VIDOFF=
35
Mike Frysinger8e477cd2019-08-21 23:59:12 -040036if [[ -t 1 ]]; then
Mike Frysinger669b28b2012-02-07 18:01:00 -050037 # order matters: we want VIDOFF last so that when we trace with `set -x`,
38 # our terminal doesn't bleed colors as bash dumps the values of vars.
Mike Frysinger8e477cd2019-08-21 23:59:12 -040039 V_BOLD_RED=$'\e[1;31m'
40 V_BOLD_GREEN=$'\e[1;32m'
41 V_BOLD_YELLOW=$'\e[1;33m'
42 V_VIDOFF=$'\e[m'
Mike Frysinger669b28b2012-02-07 18:01:00 -050043fi
44
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050045# Turn on bash debug support if available for backtraces.
46shopt -s extdebug 2>/dev/null
Brian Harring7f175a52012-03-02 05:37:00 -080047
Marc Herbertff9fa312015-01-16 17:18:53 -080048# Output a backtrace. Optional parameter allows hiding the last
49# frame(s) so functions like "die()" can hide their additional
50# frame(s) if they wish.
Mike Frysingerbbec7112019-08-22 00:44:29 -040051_dump_trace() {
Marc Herbertff9fa312015-01-16 17:18:53 -080052 # Default = 0 hidden frames: show everything except dump_trace
53 # frame itself.
54 local hidden_frames=${1:-0}
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050055 local j n p func src line args
56 p=${#BASH_ARGV[@]}
Marc Herbertff9fa312015-01-16 17:18:53 -080057
Marc Herbertf51cf8f2015-01-20 13:56:00 -080058 error "$(date)"
Marc Herbertee1fcbb2015-05-28 10:37:46 -070059 error "$(ps f -o pgid,ppid,pid,etime,cputime,%cpu,command)"
Marc Herbertf51cf8f2015-01-20 13:56:00 -080060
Marc Herbertff9fa312015-01-16 17:18:53 -080061 # Frame 0 is ourselves so it's always suppressed / does not count.
62 for (( n = ${#FUNCNAME[@]}; n > hidden_frames; --n )); do
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050063 func=${FUNCNAME[${n} - 1]}
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050064 line=${BASH_LINENO[${n} - 1]}
65 args=
66 if [[ -z ${BASH_ARGC[${n} -1]} ]]; then
67 args='(args unknown, no debug available)'
Brian Harring7f175a52012-03-02 05:37:00 -080068 else
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050069 for (( j = 0; j < ${BASH_ARGC[${n} -1]}; ++j )); do
70 args="${args:+${args} }'${BASH_ARGV[$(( p - j - 1 ))]}'"
71 done
72 ! (( p -= ${BASH_ARGC[${n} - 1]} ))
Brian Harring7f175a52012-03-02 05:37:00 -080073 fi
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050074 if [[ ${n} == ${#FUNCNAME[@]} ]]; then
Marc Herbertee1fcbb2015-05-28 10:37:46 -070075 error "Arguments of $$: ${0##/*} ${args}"
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050076 error "Backtrace: (most recent call is last)"
77 else
Marc Herbert2e8c5902015-01-15 20:17:31 -080078 src=${BASH_SOURCE[${n}]##*/}
Marc Herbertfa6cbe72015-01-20 14:09:09 -080079 curr_func=${FUNCNAME[${n}]}
80 error "$(printf ' %s:%s:%s(), called: %s %s ' \
81 "${src}" "${line}" "${curr_func}" "${func}" "${args}")"
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050082 fi
83 done
84}
Brian Harring7f175a52012-03-02 05:37:00 -080085
Mike Frysinger669b28b2012-02-07 18:01:00 -050086# Declare these asap so that code below can safely assume they exist.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040087_message() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050088 local prefix=$1
Brian Harring7f175a52012-03-02 05:37:00 -080089 shift
Mike Frysingerca7cd942019-09-08 04:40:45 -040090 printf '%s: ' "$(date +%H:%M:%S)" >&2
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050091 if [[ $# -eq 0 ]]; then
92 echo -e "${prefix}${CROS_LOG_PREFIX:-""}:${V_VIDOFF}" >&2
Brian Harring7f175a52012-03-02 05:37:00 -080093 return
94 fi
95 (
96 # Handle newlines in the message, prefixing each chunk correctly.
97 # Do this in a subshell to avoid having to track IFS/set -f state.
98 IFS="
99"
100 set +f
101 set -- $*
102 IFS=' '
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500103 if [[ $# -eq 0 ]]; then
Brian Harring7f175a52012-03-02 05:37:00 -0800104 # Empty line was requested.
105 set -- ''
106 fi
107 for line in "$@"; do
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500108 echo -e "${prefix}${CROS_LOG_PREFIX:-}: ${line}${V_VIDOFF}" >&2
Brian Harring7f175a52012-03-02 05:37:00 -0800109 done
110 )
111}
112
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400113info() {
Brian Harring7f175a52012-03-02 05:37:00 -0800114 _message "${V_BOLD_GREEN}INFO " "$*"
Mike Frysinger669b28b2012-02-07 18:01:00 -0500115}
116
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400117warn() {
Brian Harring7f175a52012-03-02 05:37:00 -0800118 _message "${V_BOLD_YELLOW}WARNING " "$*"
Mike Frysinger669b28b2012-02-07 18:01:00 -0500119}
120
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400121error() {
Brian Harring7f175a52012-03-02 05:37:00 -0800122 _message "${V_BOLD_RED}ERROR " "$*"
Mike Frysinger669b28b2012-02-07 18:01:00 -0500123}
124
Brian Harring7f175a52012-03-02 05:37:00 -0800125
126# For all die functions, they must explicitly force set +eu;
Alex Deymof9235952015-01-15 11:15:09 -0800127# no reason to have them cause their own crash if we're in the middle
Brian Harring7f175a52012-03-02 05:37:00 -0800128# of reporting an error condition then exiting.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400129die_err_trap() {
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400130 local result=${1:-$?}
131 local command=${2:-${BASH_COMMAND:-command unknown}}
Brian Harring7f175a52012-03-02 05:37:00 -0800132 set +e +u
133
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400134 if [[ ${result} == "0" ]]; then
135 # Let callers simplify by setting us as an EXIT trap handler.
136 return 0
137 fi
138
Brian Harring7f175a52012-03-02 05:37:00 -0800139 # Per the message, bash misreports 127 as 1 during err trap sometimes.
140 # Note this fact to ensure users don't place too much faith in the
141 # exit code in that case.
142 set -- "Command '${command}' exited with nonzero code: ${result}"
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500143 if [[ ${result} -eq 1 ]] && [[ -z $(type -t ${command}) ]]; then
144 set -- "$@" \
145 '(Note bash sometimes misreports "command not found" as exit code 1 '\
Brian Harring7f175a52012-03-02 05:37:00 -0800146'instead of 127)'
Brian Harring7f175a52012-03-02 05:37:00 -0800147 fi
Mike Frysingerbbec7112019-08-22 00:44:29 -0400148 _dump_trace 1
Brian Harring7f175a52012-03-02 05:37:00 -0800149 error
150 error "Command failed:"
151 DIE_PREFIX=' '
152 die_notrace "$@"
153}
154
155# Exit this script due to a failure, outputting a backtrace in the process.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400156die() {
Brian Harring7f175a52012-03-02 05:37:00 -0800157 set +e +u
Mike Frysingerbbec7112019-08-22 00:44:29 -0400158 _dump_trace 1
Brian Harring7f175a52012-03-02 05:37:00 -0800159 error
160 error "Error was:"
161 DIE_PREFIX=' '
162 die_notrace "$@"
163}
164
165# Exit this script w/out a backtrace.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400166die_notrace() {
Brian Harring7f175a52012-03-02 05:37:00 -0800167 set +e +u
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500168 if [[ $# -eq 0 ]]; then
Brian Harring7f175a52012-03-02 05:37:00 -0800169 set -- '(no error message given)'
170 fi
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500171 local line
Brian Harring7f175a52012-03-02 05:37:00 -0800172 for line in "$@"; do
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500173 error "${DIE_PREFIX}${line}"
Brian Harring7f175a52012-03-02 05:37:00 -0800174 done
Mike Frysinger669b28b2012-02-07 18:01:00 -0500175 exit 1
176}
177
David Jamesbf13ea82013-05-08 14:17:25 -0700178# Check for a single string in a list of space-separated strings.
179# e.g. has "foo" "foo bar baz" is true, but has "f" "foo bar baz" is not.
180has() { [[ " ${*:2} " == *" $1 "* ]]; }
181
Brian Harring9086a3f2013-01-14 01:43:16 -0800182# Directory locations inside the dev chroot; try the new default,
183# falling back to user specific paths if the upgrade has yet to
184# happen.
185_user="${USER}"
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500186[[ ${USER} == "root" ]] && _user="${SUDO_USER}"
Brian Harring9086a3f2013-01-14 01:43:16 -0800187_CHROOT_TRUNK_DIRS=( "/home/${_user}/trunk" /mnt/host/source )
188_DEPOT_TOOLS_DIRS=( "/home/${_user}/depot_tools" /mnt/host/depot_tools )
189unset _user
190
191_process_mount_pt() {
192 # Given 4 arguments; the root path, the variable to set,
193 # the old location, and the new; finally, forcing the upgrade is doable
194 # via if a 5th arg is provided.
195 # This will then try to migrate the old to new if we can do so right now
196 # (else leaving symlinks in place w/in the new), and will set $1 to the
197 # new location.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500198 local base=${1:-/} var=$2 old=$3 new=$4 force=${5:-false}
199 local _sudo=$([[ ${USER} != "root" ]] && echo sudo)
200 local val=${new}
Mike Frysinger2d9a97d2015-02-25 01:38:56 -0500201 if ${force} || [[ -L ${base}/${new} ]] || [[ ! -e ${base}/${new} ]]; then
Brian Harring9086a3f2013-01-14 01:43:16 -0800202 # Ok, it's either a symlink or this is the first run. Upgrade if we can-
203 # specifically, if we're outside the chroot and we can rmdir the old.
204 # If we cannot rmdir the old, that's due to a mount being bound to that
205 # point (even if we can't see it, it's there)- thus fallback to adding
206 # compat links.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500207 if ${force} || ( [[ ${INSIDE_CHROOT} -eq 0 ]] && \
208 ${_sudo} rmdir "${base}/${old}" 2>/dev/null ); then
Brian Harring9086a3f2013-01-14 01:43:16 -0800209 ${_sudo} rm -f "${base}/${new}" || :
210 ${_sudo} mkdir -p "${base}/${new}" "$(dirname "${base}/${old}" )"
211 ${_sudo} ln -s "${new}" "${base}/${old}"
212 else
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500213 if [[ ! -L ${base}/${new} ]]; then
Brian Harring9086a3f2013-01-14 01:43:16 -0800214 # We can't do the upgrade right now; install compatibility links.
215 ${_sudo} mkdir -p "$(dirname "${base}/${new}")" "${base}/${old}"
216 ${_sudo} ln -s "${old}" "${base}/${new}"
217 fi
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500218 val=${old}
Brian Harring9086a3f2013-01-14 01:43:16 -0800219 fi
220 fi
221 eval "${var}=\"${val}\""
222}
223
224set_chroot_trunk_dir() {
225 # This takes two optional arguments; the first being the path to the chroot
226 # base; this is only used by enter_chroot. The second argument is whether
227 # or not to force the new pathways; this is only used by make_chroot. Passing
228 # a non-null value for $2 forces the new paths.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500229 if [[ ${INSIDE_CHROOT} -eq 0 ]] && [[ -z ${1-} ]]; then
Brian Harring9086a3f2013-01-14 01:43:16 -0800230 # Can't do the upgrade, thus skip trying to do so.
231 CHROOT_TRUNK_DIR="${_CHROOT_TRUNK_DIRS[1]}"
232 DEPOT_TOOLS_DIR="${_DEPOT_TOOLS_DIRS[1]}"
233 return
234 fi
Alex Deymo76277de2015-01-22 11:02:13 -0800235 _process_mount_pt "${1:-}" CHROOT_TRUNK_DIR "${_CHROOT_TRUNK_DIRS[@]}" \
236 ${2:+true}
237 _process_mount_pt "${1:-}" DEPOT_TOOLS_DIR "${_DEPOT_TOOLS_DIRS[@]}" \
238 ${2:+true}
Brian Harring9086a3f2013-01-14 01:43:16 -0800239}
240
241set_chroot_trunk_dir
242
Anton Staaf30acb0b2011-01-26 16:00:20 -0800243# Construct a list of possible locations for the source tree. This list is
244# based on various environment variables and globals that may have been set
245# by the calling script.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400246get_gclient_root_list() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500247 if [[ ${INSIDE_CHROOT} -eq 1 ]]; then
Brian Harring2499bfb2012-12-18 16:23:31 -0800248 echo "${CHROOT_TRUNK_DIR}"
Anton Staaf30acb0b2011-01-26 16:00:20 -0800249 fi
250
Alex Deymo135faa12015-01-21 11:20:13 -0800251 if [[ -n ${COMMON_SH:-} ]]; then echo "$(dirname "${COMMON_SH}")/../.."; fi
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500252 if [[ -n ${BASH_SOURCE} ]]; then echo "$(dirname "${BASH_SOURCE}")/../.."; fi
Anton Staaf30acb0b2011-01-26 16:00:20 -0800253}
254
255# Based on the list of possible source locations we set GCLIENT_ROOT if it is
256# not already defined by looking for a src directory in each seach path
257# location. If we do not find a valid looking root we error out.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400258get_gclient_root() {
Alex Deymo135faa12015-01-21 11:20:13 -0800259 if [[ -n ${GCLIENT_ROOT:-} ]]; then
Anton Staaf30acb0b2011-01-26 16:00:20 -0800260 return
261 fi
262
263 for path in $(get_gclient_root_list); do
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500264 if [[ -d ${path}/src ]]; then
Anton Staaf30acb0b2011-01-26 16:00:20 -0800265 GCLIENT_ROOT=${path}
266 break
267 fi
268 done
269
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500270 if [[ -z ${GCLIENT_ROOT} ]]; then
Anton Staaf30acb0b2011-01-26 16:00:20 -0800271 # Using dash or sh, we don't know where we are. $0 refers to the calling
272 # script, not ourselves, so that doesn't help us.
273 echo "Unable to determine location for common.sh. If you are sourcing"
274 echo "common.sh from a script run via dash or sh, you must do it in the"
275 echo "following way:"
276 echo ' COMMON_SH="$(dirname "$0")/../../scripts/common.sh"'
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500277 echo ' . "${COMMON_SH}"'
Anton Staaf30acb0b2011-01-26 16:00:20 -0800278 echo "where the first line is the relative path from your script to"
279 echo "common.sh."
280 exit 1
281 fi
282}
283
David James76764882012-10-24 19:46:29 -0700284# Populate the ENVIRONMENT_WHITELIST array.
285load_environment_whitelist() {
286 set -f
287 ENVIRONMENT_WHITELIST=(
288 $("${GCLIENT_ROOT}/chromite/scripts/cros_env_whitelist")
289 )
290 set +f
291}
292
Anton Staaf30acb0b2011-01-26 16:00:20 -0800293# Find root of source tree
294get_gclient_root
295
rspangler@google.comd74220d2009-10-09 20:56:14 +0000296# Canonicalize the directories for the root dir and the calling script.
297# readlink is part of coreutils and should be present even in a bare chroot.
tedbo4f44d9e2010-01-08 17:26:11 -0800298# This is better than just using
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500299# FOO="$(cd ${FOO} ; pwd)"
tedbo4f44d9e2010-01-08 17:26:11 -0800300# since that leaves symbolic links intact.
rspangler@google.comd74220d2009-10-09 20:56:14 +0000301# Note that 'realpath' is equivalent to 'readlink -f'.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500302SCRIPT_LOCATION=$(readlink -f "${SCRIPT_LOCATION}")
303GCLIENT_ROOT=$(readlink -f "${GCLIENT_ROOT}")
rspangler@google.comd74220d2009-10-09 20:56:14 +0000304
305# Other directories should always be pathed down from GCLIENT_ROOT.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500306SRC_ROOT="${GCLIENT_ROOT}/src"
307SRC_INTERNAL="${GCLIENT_ROOT}/src-internal"
308SCRIPTS_DIR="${SRC_ROOT}/scripts"
309BUILD_LIBRARY_DIR="${SCRIPTS_DIR}/build_library"
Mike Frysinger66accd22017-09-13 03:50:30 -0400310CHROMITE_BIN="${GCLIENT_ROOT}/chromite/bin"
Alex Klein999443c2018-10-17 12:02:05 -0600311IMAGES_DIR="${GCLIENT_ROOT}/src/build/images"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000312
rspangler@google.comd74220d2009-10-09 20:56:14 +0000313# Load shflags
Zdenek Behan07d24222011-11-02 00:46:25 +0000314# NOTE: This code snippet is in particular used by the au-generator (which
315# stores shflags in ./lib/shflags/) and should not be touched.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500316if [[ -f ${SCRIPTS_DIR}/lib/shflags/shflags ]]; then
Mike Frysinger77c674b2012-02-07 18:05:07 -0500317 . "${SCRIPTS_DIR}/lib/shflags/shflags" || die "Couldn't find shflags"
Zdenek Behan07d24222011-11-02 00:46:25 +0000318else
319 . ./lib/shflags/shflags || die "Couldn't find shflags"
320fi
rspangler@google.comd74220d2009-10-09 20:56:14 +0000321
Bill Richardson10d27c22010-01-20 13:38:50 -0800322# Our local mirror
323DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000324
Bill Richardson10d27c22010-01-20 13:38:50 -0800325# Upstream mirrors and build suites come in 2 flavors
326# DEV - development chroot, used to build the chromeos image
327# IMG - bootable image, to run on actual hardware
rspangler@google.comd74220d2009-10-09 20:56:14 +0000328
Bill Richardson10d27c22010-01-20 13:38:50 -0800329DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
330DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"}
331
332DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
333DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000334
335# Default location for chroot
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500336DEFAULT_CHROOT_DIR=${CHROMEOS_CHROOT_DIR:-"${GCLIENT_ROOT}/chroot"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000337
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500338# All output files from build should go under ${DEFAULT_BUILD_ROOT}, so that
rspangler@google.comd74220d2009-10-09 20:56:14 +0000339# they don't pollute the source directory.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500340DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"${SRC_ROOT}/build"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000341
Chris Ching4bc95a12016-11-22 13:44:13 -0700342# Default location for event files
343DEFAULT_EVENT_DIR=${DEFAULT_EVENT_DIR:-"${DEFAULT_BUILD_ROOT}/events"}
344
345# Default event file. Format is YYYYDD.HHMM.json
346DEFAULT_EVENT_FILE=${DEFAULT_EVENT_FILE:-"${DEFAULT_EVENT_DIR}/$(date +%Y%m%d.%H%M.).json"}
347
Mike Frysingerc17a4932012-03-12 17:07:40 -0400348# Sets the default board variable for calling script.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500349if [[ -f ${GCLIENT_ROOT}/src/scripts/.default_board ]]; then
350 DEFAULT_BOARD=$(<"${GCLIENT_ROOT}/src/scripts/.default_board")
Mike Frysingerc17a4932012-03-12 17:07:40 -0400351 # Check for user typos like whitespace.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500352 if [[ -n ${DEFAULT_BOARD//[a-zA-Z0-9-_]} ]]; then
Mike Frysingerc17a4932012-03-12 17:07:40 -0400353 die ".default_board: invalid name detected; please fix:" \
354 "'${DEFAULT_BOARD}'"
355 fi
356fi
David McMahon49302942010-02-18 16:55:35 -0800357
Chris Sosab0f57322011-10-25 03:07:23 +0000358# Directory to store built images. Should be set by sourcing script when used.
359BUILD_DIR=
Simon Glass142ca062011-02-09 13:39:43 -0800360
Mike Frysinger34808e52017-01-11 21:14:08 -0500361# Path to the verified boot directory where we get signing related keys/scripts.
362VBOOT_DIR="${CHROOT_TRUNK_DIR}/src/platform/vboot_reference"
363VBOOT_TESTKEYS_DIR="${VBOOT_DIR}/tests/testkeys"
Mike Frysinger40cea032017-03-12 19:48:20 -0400364# We load these from the chroot rather than directly from the vboot source repo
365# so we work correctly even in a minilayout.
366VBOOT_DEVKEYS_DIR="/usr/share/vboot/devkeys"
367VBOOT_SIGNING_DIR="/usr/share/vboot/bin"
Mike Frysinger34808e52017-01-11 21:14:08 -0500368
Simon Glass142ca062011-02-09 13:39:43 -0800369# Standard filenames
Chris Sosab0f57322011-10-25 03:07:23 +0000370CHROMEOS_BASE_IMAGE_NAME="chromiumos_base_image.bin"
Simon Glass142ca062011-02-09 13:39:43 -0800371CHROMEOS_IMAGE_NAME="chromiumos_image.bin"
Chris Sosab0f57322011-10-25 03:07:23 +0000372CHROMEOS_DEVELOPER_IMAGE_NAME="chromiumos_image.bin"
Gilad Arnold08366272012-02-08 10:46:26 -0800373CHROMEOS_RECOVERY_IMAGE_NAME="recovery_image.bin"
Simon Glass142ca062011-02-09 13:39:43 -0800374CHROMEOS_TEST_IMAGE_NAME="chromiumos_test_image.bin"
Chris Sosab0f57322011-10-25 03:07:23 +0000375CHROMEOS_FACTORY_INSTALL_SHIM_NAME="factory_install_shim.bin"
Bertrand SIMONNETb17cd572015-04-01 10:27:37 -0700376SYSROOT_SETTINGS_FILE="/var/cache/edb/chromeos"
Simon Glass142ca062011-02-09 13:39:43 -0800377
Rahul Chaudhry306f6882015-05-13 15:14:13 -0700378# Install mask for portage ebuilds. Used by build_image and gmergefs.
Chris Masoned11ce172010-11-09 14:22:08 -0800379# TODO: Is /usr/local/autotest-chrome still used by anyone?
Hung-Te Lind32c59f2012-01-19 19:54:01 +0800380COMMON_INSTALL_MASK="
Daniel Erate82f07c2010-12-21 13:39:22 -0800381 *.a
Alex Deymo1856c4f2014-09-24 08:15:34 -0700382 *.c
383 *.cc
Rahul Chaudhry306f6882015-05-13 15:14:13 -0700384 *.go
Daniel Erate82f07c2010-12-21 13:39:22 -0800385 *.la
Rene Bolldorf32228222012-07-21 14:18:22 -0400386 *.h
Brian Norrisef0880d2016-05-19 12:39:56 -0700387 *.hh
Rene Bolldorf32228222012-07-21 14:18:22 -0400388 *.hpp
Kevin Cernekee6ab57a92015-06-25 15:43:06 -0700389 *.h++
Nam T. Nguyenaa15f142015-05-15 14:27:50 -0700390 *.hxx
Ryo Hashimoto44e6a252019-10-10 16:12:02 +0900391 *.proto
Mike Frysinger97c6ac92014-06-16 00:06:37 -0400392 */.keep*
Shuhei Takahashic62d3072019-01-29 18:04:35 +0900393 /build/libexec/tast
394 /build/share/tast
Daniel Erate82f07c2010-12-21 13:39:22 -0800395 /etc/init.d
396 /etc/runlevels
Qijiang Fan6834cba2018-04-04 16:41:56 +0900397 /etc/selinux/intermediates
Hung-Te Lin76272be2012-08-07 19:10:09 +0800398 /firmware
Mike Frysinger785f09d2013-09-25 22:11:21 -0400399 /lib/modules/*/vdso
Daniel Erate82f07c2010-12-21 13:39:22 -0800400 /lib/rc
Tomasz Figa435f0e52017-01-06 14:28:06 +0900401 /opt/google/containers/android/vendor/lib*/pkgconfig
Benjamin Gordonffcb2b12017-10-24 13:02:46 -0600402 /opt/google/containers/android/build
Nam T. Nguyenf1de1a72015-05-27 08:37:46 -0700403 /usr/bin/*-config
Daniel Erate82f07c2010-12-21 13:39:22 -0800404 /usr/bin/Xnest
405 /usr/bin/Xvfb
Sergei Datsenko29869d92019-11-12 14:26:27 +1100406 /usr/include
Daniel Erate82f07c2010-12-21 13:39:22 -0800407 /usr/lib/debug
Rahul Chaudhry306f6882015-05-13 15:14:13 -0700408 /usr/lib/gopath
Mike Frysinger27b5edb2012-08-08 18:52:06 -0400409 /usr/lib*/pkgconfig
Daniel Erate82f07c2010-12-21 13:39:22 -0800410 /usr/local/autotest-chrome
411 /usr/man
412 /usr/share/aclocal
Brian Norris0f3c4e22016-06-21 12:23:18 -0700413 /usr/share/cups/drv
Daniel Erate82f07c2010-12-21 13:39:22 -0800414 /usr/share/doc
415 /usr/share/gettext
416 /usr/share/gtk-2.0
417 /usr/share/gtk-doc
418 /usr/share/info
419 /usr/share/man
Brian Norris0f3c4e22016-06-21 12:23:18 -0700420 /usr/share/ppd
Daniel Erate82f07c2010-12-21 13:39:22 -0800421 /usr/share/openrc
422 /usr/share/pkgconfig
Ryan Harrisonb8bd5942013-02-02 13:39:43 -0500423 /usr/share/profiling
Daniel Erate82f07c2010-12-21 13:39:22 -0800424 /usr/share/readline
Chris Wolfed13775f2011-07-26 16:34:38 -0400425 /usr/src
Daniel Erate82f07c2010-12-21 13:39:22 -0800426 "
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700427
Hung-Te Lind32c59f2012-01-19 19:54:01 +0800428# Mask for base, dev, and test images (build_image, build_image --test)
429DEFAULT_INSTALL_MASK="
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500430 ${COMMON_INSTALL_MASK}
Don Garrettac1cd0d2013-07-18 18:04:06 -0700431 /boot/config-*
432 /boot/System.map-*
Aviv Kesheta1838ba2013-07-23 10:58:23 -0700433 /usr/local/build/autotest
Olof Johansson1222b2e2012-08-08 15:27:35 -0700434 /lib/modules/*/build
435 /lib/modules/*/source
Kees Cook74f163b2012-12-18 15:46:51 -0800436 test_*.ko
Hung-Te Lind32c59f2012-01-19 19:54:01 +0800437 "
438
Chris Sosac9422fa2012-03-05 15:58:07 -0800439# Mask for factory install shim (build_image factory_install)
Hung-Te Lind32c59f2012-01-19 19:54:01 +0800440FACTORY_SHIM_INSTALL_MASK="
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500441 ${DEFAULT_INSTALL_MASK}
Cheng-Han Yangc924c2c2018-11-02 17:02:29 +0800442 /opt/google/chrome
443 /opt/google/containers
Vic Yang982556a2012-12-11 11:41:54 +0800444 /usr/lib64/dri
Hung-Te Lin7f721ec2017-07-18 15:14:48 +0800445 /usr/lib/dri
Daniel Erate82f07c2010-12-21 13:39:22 -0800446 /usr/share/X11
Hung-Te Lin7f721ec2017-07-18 15:14:48 +0800447 /usr/share/chromeos-assets/[^i]*
448 /usr/share/chromeos-assets/i[^m]*
Daniel Erate82f07c2010-12-21 13:39:22 -0800449 /usr/share/fonts
Daniel Erate82f07c2010-12-21 13:39:22 -0800450 /usr/share/locale
Daniel Erate82f07c2010-12-21 13:39:22 -0800451 /usr/share/mime
Vic Yang982556a2012-12-11 11:41:54 +0800452 /usr/share/oem
Daniel Erate82f07c2010-12-21 13:39:22 -0800453 /usr/share/sounds
454 /usr/share/tts
455 /usr/share/zoneinfo
456 "
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800457
Sabin Floarese790ea72016-11-21 14:56:41 +0200458# Mask for images without systemd.
459SYSTEMD_INSTALL_MASK="
Chris Morin83226ab2018-12-07 19:53:48 -0800460 /lib/systemd/network
461 /usr/lib/systemd/system
Sabin Floarese790ea72016-11-21 14:56:41 +0200462"
463
rspangler@google.comd74220d2009-10-09 20:56:14 +0000464# -----------------------------------------------------------------------------
465# Functions
466
Don Garrett640a0582010-05-04 16:54:28 -0700467# Enter a chroot and restart the current script if needed
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400468restart_in_chroot_if_needed() {
David Rochberg3b910702010-12-02 10:45:21 -0500469 # NB: Pass in ARGV: restart_in_chroot_if_needed "$@"
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500470 if [[ ${INSIDE_CHROOT} -ne 1 ]]; then
Chris Sosafd2cdec2011-03-24 16:06:59 -0700471 # Get inside_chroot path for script.
472 local chroot_path="$(reinterpret_path_for_chroot "$0")"
Mike Frysinger66accd22017-09-13 03:50:30 -0400473 exec "${CHROMITE_BIN}/cros_sdk" -- "${chroot_path}" "$@"
Don Garrett640a0582010-05-04 16:54:28 -0700474 fi
475}
476
rspangler@google.comd74220d2009-10-09 20:56:14 +0000477# Fail unless we're inside the chroot. This guards against messing up your
478# workstation.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400479assert_inside_chroot() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500480 if [[ ${INSIDE_CHROOT} -ne 1 ]]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000481 echo "This script must be run inside the chroot. Run this first:"
Zdenek Behan2811c162011-08-13 00:47:38 +0200482 echo " cros_sdk"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000483 exit 1
484 fi
485}
486
487# Fail if we're inside the chroot. This guards against creating or entering
488# nested chroots, among other potential problems.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400489assert_outside_chroot() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500490 if [[ ${INSIDE_CHROOT} -ne 0 ]]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000491 echo "This script must be run outside the chroot."
492 exit 1
493 fi
494}
495
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400496assert_not_root_user() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500497 if [[ ${UID:-$(id -u)} == 0 ]]; then
derat@google.com86dcc8e2009-11-21 19:49:49 +0000498 echo "This script must be run as a non-root user."
499 exit 1
500 fi
501}
502
David James76764882012-10-24 19:46:29 -0700503assert_root_user() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500504 if [[ ${UID:-$(id -u)} != 0 ]] || [[ ${SUDO_USER:-root} == "root" ]]; then
David James76764882012-10-24 19:46:29 -0700505 die_notrace "This script must be run using sudo from a non-root user."
506 fi
507}
508
tedbo373c3902010-04-12 10:52:40 -0700509# Writes stdin to the given file name as root using sudo in overwrite mode.
510#
511# $1 - The output file name.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400512sudo_clobber() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500513 sudo tee "$1" >/dev/null
tedbo373c3902010-04-12 10:52:40 -0700514}
515
Mike Frysinger286b5922011-09-28 11:59:53 -0400516# Execute multiple commands in a single sudo. Generally will speed things
517# up by avoiding multiple calls to `sudo`. If any commands fail, we will
518# call die with the failing command. We can handle a max of ~100 commands,
519# but hopefully no one will ever try that many at once.
520#
521# $@ - The commands to execute, one per arg.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400522sudo_multi() {
Mike Frysinger286b5922011-09-28 11:59:53 -0400523 local i cmds
524
525 # Construct the shell code to execute. It'll be of the form:
526 # ... && ( ( command ) || exit <command index> ) && ...
527 # This way we know which command exited. The exit status of
528 # the underlying command is lost, but we never cared about it
529 # in the first place (other than it is non zero), so oh well.
530 for (( i = 1; i <= $#; ++i )); do
531 cmds+=" && ( ( ${!i} ) || exit $(( i + 10 )) )"
532 done
533
534 # Execute our constructed shell code.
535 sudo -- sh -c ":${cmds[*]}" && i=0 || i=$?
536
537 # See if this failed, and if so, print out the failing command.
538 if [[ $i -gt 10 ]]; then
539 : $(( i -= 10 ))
540 die "sudo_multi failed: ${!i}"
541 elif [[ $i -ne 0 ]]; then
542 die "sudo_multi failed for unknown reason $i"
543 fi
544}
545
Chris Masonebbccc242014-02-08 16:23:53 -0800546# Clears out stale shadow-utils locks in the given target root.
547sudo_clear_shadow_locks() {
548 info "Clearing shadow utils lockfiles under $1"
549 sudo rm -f "$1/etc/"{passwd,group,shadow,gshadow}.lock*
550}
551
Mike Frysinger1aa61242011-09-15 17:46:44 -0400552# Locate all mounts below a specified directory.
553#
554# $1 - The root tree.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400555sub_mounts() {
Mike Frysinger1aa61242011-09-15 17:46:44 -0400556 # Assume that `mount` outputs a list of mount points in the order
557 # that things were mounted (since it always has and hopefully always
558 # will). As such, we have to unmount in reverse order to cleanly
559 # unmount submounts (think /dev/pts and /dev).
Alex Deymo7ba56be2015-02-12 14:28:06 -0800560 awk -v path="$1" -v len="${#1}" \
Benjamin Gordon1d5afed2017-06-14 16:35:32 -0600561 '(substr($2, 1, len+1) == path ||
562 substr($2, 1, len+1) == (path "/")) { print $2 }' /proc/mounts | \
Zdenek Behan52970f42012-08-30 22:36:40 +0200563 tac | \
564 sed -e 's/\\040(deleted)$//'
565 # Hack(zbehan): If a bind mount's source is mysteriously removed,
566 # we'd end up with an orphaned mount with the above string in its name.
567 # It can only be seen through /proc/mounts and will stick around even
568 # when it should be gone already. crosbug.com/31250
Mike Frysinger1aa61242011-09-15 17:46:44 -0400569}
570
robotboy98912212010-04-12 14:08:14 -0700571# Unmounts a directory, if the unmount fails, warn, and then lazily unmount.
572#
573# $1 - The path to unmount.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400574safe_umount_tree() {
Alex Deymo7ba56be2015-02-12 14:28:06 -0800575 local mount_point="$1"
robotboy98912212010-04-12 14:08:14 -0700576
Alex Deymo7ba56be2015-02-12 14:28:06 -0800577 local mounts=( $(sub_mounts "${mount_point}") )
578
579 # Silently return if the mount_point was already unmounted.
580 if [[ ${#mounts[@]} -eq 0 ]]; then
Mike Frysingere8aec372011-09-21 00:03:22 -0400581 return 0
582 fi
583
Mike Frysinger1aa61242011-09-15 17:46:44 -0400584 # First try to unmount in one shot to speed things up.
Chirantan Ekbote30823e92016-06-29 11:39:03 -0700585 if LC_ALL=C safe_umount -d "${mounts[@]}"; then
586 return 0
David Jamesd9b67982012-10-26 09:46:50 -0700587 fi
588
589 # Well that didn't work, so lazy unmount remaining ones.
Alex Deymo7ba56be2015-02-12 14:28:06 -0800590 warn "Failed to unmount ${mounts[@]}, these are the processes using the" \
591 "mount points."
592 sudo fuser -vm "${mount_point}" || true
593
Mike Frysinger1aa61242011-09-15 17:46:44 -0400594 warn "Doing a lazy unmount"
Alex Deymo7ba56be2015-02-12 14:28:06 -0800595 if ! safe_umount -d -l "${mounts[@]}"; then
596 mounts=( $(sub_mounts "${mount_point}") )
597 die "Failed to lazily unmount ${mounts[@]}"
robotboy98912212010-04-12 14:08:14 -0700598 fi
599}
Chris Sosa702618f2010-05-14 12:52:32 -0700600
Brian Harringece65e02012-08-30 13:42:21 -0700601
David James76764882012-10-24 19:46:29 -0700602# Run umount as root.
Brian Harringece65e02012-08-30 13:42:21 -0700603safe_umount() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500604 $([[ ${UID:-$(id -u)} != 0 ]] && echo sudo) umount "$@"
Brian Harringece65e02012-08-30 13:42:21 -0700605}
606
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700607# Setup a loopback device for a file and scan for partitions, with retries.
608#
609# $1 - The file to back the new loopback device.
610# $2-$N - Additional arguments to pass to losetup.
611loopback_partscan() {
612 local lb_dev image="$1"
613 shift
LaMont Jones005f77b2019-10-11 14:04:55 -0600614
George Engelbrechtfcd236f2019-10-28 18:12:56 -0600615 # We set up a binary backoff for adding the partitions. We give 10 attempts
616 # each time nth time we fail, backing off by 1<<n. The maximum time then
617 # spent sleeping will be sum(2^n) from [0...10] which is 1023 seconds,
618 # or ~37 minutes.
619 local i partx_out partx_d_out sleep_seconds=1
620 for (( i = 0; i <= 10; i++ )); do
621 # Flush any dirty pages in the image before we do partx commands.
622 info "Running sync -f ${image}"
623 sync -f "${image}"
LaMont Jones005f77b2019-10-11 14:04:55 -0600624
George Engelbrechtfcd236f2019-10-28 18:12:56 -0600625 # This (perhaps) mounts the partitions as well.
626 lb_dev=$(sudo losetup --show -f "$@" "${image}")
Mike Frysingerc97934e2019-09-02 02:48:28 -0400627
George Engelbrechtfcd236f2019-10-28 18:12:56 -0600628 # Try to clean the slate by removing any existing parts (best effort).
629 partx_d_out=$(sudo partx -v -d "${lb_dev}") || true
Mike Frysingerc97934e2019-09-02 02:48:28 -0400630
George Engelbrechtfcd236f2019-10-28 18:12:56 -0600631 # Try to add the partitions back.
632 if ! partx_out=$(sudo partx -v -a "${lb_dev}"); then
633 local proc_parts
634 warn "Adding partitions with 'partx -v -a ${lb_dev}' failed."
635 warn "partx -d output:\n${partx_d_out}"
636 warn "partx -a output:\n${partx_out}"
637 proc_parts=$(cat /proc/partitions)
638 warn "/proc/partitions before detaching loopback:\n ${proc_parts}"
639 # Detach the image.
640 sudo losetup -d "${lb_dev}" || true
641 proc_parts=$(cat /proc/partitions)
642 warn "/proc/partitions after detaching loopback:\n ${proc_parts}"
643 warn "Sleeping ${sleep_seconds} before trying again."
644 sleep "${sleep_seconds}"
645 : $(( sleep_seconds <<= 1 ))
646 else
647 break
648 fi
649 done
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700650
Gabe Black7bb6c5a2015-03-16 20:23:06 -0700651 echo "${lb_dev}"
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700652}
653
654# Detach a loopback device set up earlier.
655#
Gabe Black01ba4de2015-03-11 16:34:56 -0700656# $1 - The loop device to detach.
657# $2-$N - Additional arguments to pass to losetup.
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700658loopback_detach() {
Mike Frysingeraf64ed52015-04-06 13:20:39 -0400659 # Retry the deletes before we detach. crbug.com/469259
660 local i
661 for (( i = 0; i < 10; i++ )); do
Mike Frysingerad3b2002019-08-21 23:35:59 -0400662 if sudo partx -d "$1"; then
Mike Frysingeraf64ed52015-04-06 13:20:39 -0400663 break
664 fi
665 warn "Sleeping & retrying ..."
666 sync
667 sleep 1
668 done
Mike Frysingerad3b2002019-08-21 23:35:59 -0400669 sudo losetup --detach "$@"
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700670}
671
Chris Sosa702618f2010-05-14 12:52:32 -0700672# Sets up symlinks for the developer root. It is necessary to symlink
673# usr and local since the developer root is mounted at /usr/local and
674# applications expect to be installed under /usr/local/bin, etc.
675# This avoids packages installing into /usr/local/usr/local/bin.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500676# $1 specifies the symlink target for the developer root.
677# $2 specifies the symlink target for the var directory.
678# $3 specifies the location of the stateful partition.
Chris Sosa702618f2010-05-14 12:52:32 -0700679setup_symlinks_on_root() {
680 # Give args better names.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500681 local dev_image_target=$1
682 local var_target=$2
683 local dev_image_root="$3/dev_image"
Chris Sosa702618f2010-05-14 12:52:32 -0700684
Mike Frysinger9e155382019-07-12 21:11:00 -0400685 # Make sure the dev_image dir itself exists.
686 if [[ ! -d "${dev_image_root}" ]]; then
687 sudo mkdir "${dev_image_root}"
688 fi
689
Chris Sosa702618f2010-05-14 12:52:32 -0700690 # If our var target is actually the standard var, we are cleaning up the
691 # symlinks (could also check for /usr/local for the dev_image_target).
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500692 if [[ ${var_target} == "/var" ]]; then
Chris Sosa702618f2010-05-14 12:52:32 -0700693 echo "Cleaning up /usr/local symlinks for ${dev_image_root}"
694 else
695 echo "Setting up symlinks for /usr/local for ${dev_image_root}"
696 fi
697
698 # Set up symlinks that should point to ${dev_image_target}.
Greg Spencer798d75f2011-02-01 22:04:49 -0800699 local path
Chris Sosa702618f2010-05-14 12:52:32 -0700700 for path in usr local; do
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500701 if [[ -h ${dev_image_root}/${path} ]]; then
Chris Sosa702618f2010-05-14 12:52:32 -0700702 sudo unlink "${dev_image_root}/${path}"
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500703 elif [[ -e ${dev_image_root}/${path} ]]; then
Chris Sosa702618f2010-05-14 12:52:32 -0700704 die "${dev_image_root}/${path} should be a symlink if exists"
705 fi
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400706 sudo ln -s "${dev_image_target}" "${dev_image_root}/${path}"
Chris Sosa702618f2010-05-14 12:52:32 -0700707 done
708
709 # Setup var symlink.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500710 if [[ -h ${dev_image_root}/var ]]; then
Chris Sosa702618f2010-05-14 12:52:32 -0700711 sudo unlink "${dev_image_root}/var"
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500712 elif [[ -e ${dev_image_root}/var ]]; then
Chris Sosa702618f2010-05-14 12:52:32 -0700713 die "${dev_image_root}/var should be a symlink if it exists"
714 fi
715
716 sudo ln -s "${var_target}" "${dev_image_root}/var"
717}
Nick Sandersd2509272010-06-16 03:50:04 -0700718
719# Get current timestamp. Assumes common.sh runs at startup.
720start_time=$(date +%s)
721
Matt Tennant298f61a2012-06-25 21:54:33 -0700722# Get time elapsed since start_time in seconds.
Mike Frysingerbbec7112019-08-22 00:44:29 -0400723_get_elapsed_seconds() {
Greg Spencer798d75f2011-02-01 22:04:49 -0800724 local end_time=$(date +%s)
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500725 local elapsed_seconds=$(( end_time - start_time ))
Matt Tennant298f61a2012-06-25 21:54:33 -0700726 echo ${elapsed_seconds}
727}
728
729# Print time elapsed since start_time.
730print_time_elapsed() {
731 # Optional first arg to specify elapsed_seconds. If not given, will
732 # recalculate elapsed time to now. Optional second arg to specify
733 # command name associated with elapsed time.
Mike Frysingerbbec7112019-08-22 00:44:29 -0400734 local elapsed_seconds=${1:-$(_get_elapsed_seconds)}
Matt Tennant298f61a2012-06-25 21:54:33 -0700735 local cmd_base=${2:-}
736
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500737 local minutes=$(( elapsed_seconds / 60 ))
738 local seconds=$(( elapsed_seconds % 60 ))
Matt Tennant298f61a2012-06-25 21:54:33 -0700739
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500740 if [[ -n ${cmd_base} ]]; then
Matt Tennant298f61a2012-06-25 21:54:33 -0700741 info "Elapsed time (${cmd_base}): ${minutes}m${seconds}s"
742 else
743 info "Elapsed time: ${minutes}m${seconds}s"
744 fi
745}
746
Matt Tennant298f61a2012-06-25 21:54:33 -0700747command_completed() {
748 # Call print_elapsed_time regardless.
Mike Frysingerbbec7112019-08-22 00:44:29 -0400749 local run_time=$(_get_elapsed_seconds)
Mike Frysinger3131d412019-03-02 14:09:37 -0500750 local cmd_base=$(basename "$0")
Matt Tennant298f61a2012-06-25 21:54:33 -0700751 print_time_elapsed ${run_time} ${cmd_base}
Nick Sandersd2509272010-06-16 03:50:04 -0700752}
Doug Anderson0c9e88d2010-10-19 14:49:39 -0700753
Albert Chaulk5d190f72013-07-12 11:42:18 -0700754# Load configuration files that allow board-specific overrides of default
755# functionality to be specified in overlays.
Steve Fungd3b1f5f2015-03-29 21:47:24 -0700756# $1 - File to load.
Albert Chaulk5d190f72013-07-12 11:42:18 -0700757load_board_specific_script() {
Steve Fung88897cc2015-03-25 13:38:38 -0700758 local file=$1 overlay
759 [[ $# -ne 1 ]] && die "load_board_specific_script requires exactly 1 param"
760 for overlay in ${BOARD_OVERLAY}; do
Albert Chaulk5d190f72013-07-12 11:42:18 -0700761 local setup_sh="${overlay}/scripts/${file}"
762 if [[ -e ${setup_sh} ]]; then
763 source "${setup_sh}"
764 fi
765 done
766}
767
Chris Sosafd2cdec2011-03-24 16:06:59 -0700768# Reinterprets path from outside the chroot for use inside.
769# Returns "" if "" given.
770# $1 - The path to reinterpret.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400771reinterpret_path_for_chroot() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500772 if [[ ${INSIDE_CHROOT} -ne 1 ]]; then
773 if [[ -z $1 ]]; then
Chris Sosafd2cdec2011-03-24 16:06:59 -0700774 echo ""
775 else
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500776 local path_abs_path=$(readlink -f "$1")
Chris Sosafd2cdec2011-03-24 16:06:59 -0700777 local gclient_root_abs_path=$(readlink -f "${GCLIENT_ROOT}")
778
779 # Strip the repository root from the path.
780 local relative_path=$(echo ${path_abs_path} \
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400781 | sed "s:${gclient_root_abs_path}/::")
Chris Sosafd2cdec2011-03-24 16:06:59 -0700782
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500783 if [[ ${relative_path} == "${path_abs_path}" ]]; then
784 die "Error reinterpreting path. Path $1 is not within source tree."
Chris Sosafd2cdec2011-03-24 16:06:59 -0700785 fi
786
787 # Prepend the chroot repository path.
Chris Sosaed2ff4b2013-08-30 12:58:41 -0700788 echo "/mnt/host/source/${relative_path}"
Chris Sosafd2cdec2011-03-24 16:06:59 -0700789 fi
790 else
791 # Path is already inside the chroot :).
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500792 echo "$1"
Chris Sosafd2cdec2011-03-24 16:06:59 -0700793 fi
794}
Gabe Black83d8b822011-08-01 17:50:09 -0700795
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400796switch_to_strict_mode() {
Brian Harring7f175a52012-03-02 05:37:00 -0800797 # Set up strict execution mode; note that the trap
798 # must follow switch_to_strict_mode, else it will have no effect.
799 set -e
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400800 trap 'die_err_trap' ERR
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500801 if [[ $# -ne 0 ]]; then
Brian Harring7f175a52012-03-02 05:37:00 -0800802 set "$@"
803 fi
804}
805
806# TODO: Re-enable this once shflags is set -e safe.
807#switch_to_strict_mode