blob: 7eb03f46c3559dd51e432e3413d37eac0496ec84 [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=
34V_REVERSE=
35V_VIDOFF=
36
Steve Fungff6345d2014-02-04 16:14:55 -080037if [[ -t 1 ]] && tput colors >&/dev/null; then
Mike Frysinger669b28b2012-02-07 18:01:00 -050038 # order matters: we want VIDOFF last so that when we trace with `set -x`,
39 # our terminal doesn't bleed colors as bash dumps the values of vars.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050040 V_BOLD_RED=$(tput bold; tput setaf 1)
41 V_BOLD_GREEN=$(tput bold; tput setaf 2)
42 V_BOLD_YELLOW=$(tput bold; tput setaf 3)
43 V_REVERSE=$(tput rev)
44 V_VIDOFF=$(tput sgr0)
Mike Frysinger669b28b2012-02-07 18:01:00 -050045fi
46
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050047# Turn on bash debug support if available for backtraces.
48shopt -s extdebug 2>/dev/null
Brian Harring7f175a52012-03-02 05:37:00 -080049
Marc Herbertff9fa312015-01-16 17:18:53 -080050# Output a backtrace. Optional parameter allows hiding the last
51# frame(s) so functions like "die()" can hide their additional
52# frame(s) if they wish.
53dump_trace() {
54 # Default = 0 hidden frames: show everything except dump_trace
55 # frame itself.
56 local hidden_frames=${1:-0}
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050057 local j n p func src line args
58 p=${#BASH_ARGV[@]}
Marc Herbertff9fa312015-01-16 17:18:53 -080059
Marc Herbertf51cf8f2015-01-20 13:56:00 -080060 error "$(date)"
Marc Herbertee1fcbb2015-05-28 10:37:46 -070061 error "$(ps f -o pgid,ppid,pid,etime,cputime,%cpu,command)"
Marc Herbertf51cf8f2015-01-20 13:56:00 -080062
Marc Herbertff9fa312015-01-16 17:18:53 -080063 # Frame 0 is ourselves so it's always suppressed / does not count.
64 for (( n = ${#FUNCNAME[@]}; n > hidden_frames; --n )); do
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050065 func=${FUNCNAME[${n} - 1]}
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050066 line=${BASH_LINENO[${n} - 1]}
67 args=
68 if [[ -z ${BASH_ARGC[${n} -1]} ]]; then
69 args='(args unknown, no debug available)'
Brian Harring7f175a52012-03-02 05:37:00 -080070 else
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050071 for (( j = 0; j < ${BASH_ARGC[${n} -1]}; ++j )); do
72 args="${args:+${args} }'${BASH_ARGV[$(( p - j - 1 ))]}'"
73 done
74 ! (( p -= ${BASH_ARGC[${n} - 1]} ))
Brian Harring7f175a52012-03-02 05:37:00 -080075 fi
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050076 if [[ ${n} == ${#FUNCNAME[@]} ]]; then
Marc Herbertee1fcbb2015-05-28 10:37:46 -070077 error "Arguments of $$: ${0##/*} ${args}"
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050078 error "Backtrace: (most recent call is last)"
79 else
Marc Herbert2e8c5902015-01-15 20:17:31 -080080 src=${BASH_SOURCE[${n}]##*/}
Marc Herbertfa6cbe72015-01-20 14:09:09 -080081 curr_func=${FUNCNAME[${n}]}
82 error "$(printf ' %s:%s:%s(), called: %s %s ' \
83 "${src}" "${line}" "${curr_func}" "${func}" "${args}")"
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050084 fi
85 done
86}
Brian Harring7f175a52012-03-02 05:37:00 -080087
Mike Frysinger669b28b2012-02-07 18:01:00 -050088# Declare these asap so that code below can safely assume they exist.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040089_message() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050090 local prefix=$1
Brian Harring7f175a52012-03-02 05:37:00 -080091 shift
Mike Frysingerbaae8eb2013-01-15 01:34:26 -050092 if [[ $# -eq 0 ]]; then
93 echo -e "${prefix}${CROS_LOG_PREFIX:-""}:${V_VIDOFF}" >&2
Brian Harring7f175a52012-03-02 05:37:00 -080094 return
95 fi
96 (
97 # Handle newlines in the message, prefixing each chunk correctly.
98 # Do this in a subshell to avoid having to track IFS/set -f state.
99 IFS="
100"
101 set +f
102 set -- $*
103 IFS=' '
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500104 if [[ $# -eq 0 ]]; then
Brian Harring7f175a52012-03-02 05:37:00 -0800105 # Empty line was requested.
106 set -- ''
107 fi
108 for line in "$@"; do
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500109 echo -e "${prefix}${CROS_LOG_PREFIX:-}: ${line}${V_VIDOFF}" >&2
Brian Harring7f175a52012-03-02 05:37:00 -0800110 done
111 )
112}
113
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400114info() {
Brian Harring7f175a52012-03-02 05:37:00 -0800115 _message "${V_BOLD_GREEN}INFO " "$*"
Mike Frysinger669b28b2012-02-07 18:01:00 -0500116}
117
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400118warn() {
Brian Harring7f175a52012-03-02 05:37:00 -0800119 _message "${V_BOLD_YELLOW}WARNING " "$*"
Mike Frysinger669b28b2012-02-07 18:01:00 -0500120}
121
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400122error() {
Brian Harring7f175a52012-03-02 05:37:00 -0800123 _message "${V_BOLD_RED}ERROR " "$*"
Mike Frysinger669b28b2012-02-07 18:01:00 -0500124}
125
Brian Harring7f175a52012-03-02 05:37:00 -0800126
127# For all die functions, they must explicitly force set +eu;
Alex Deymof9235952015-01-15 11:15:09 -0800128# no reason to have them cause their own crash if we're in the middle
Brian Harring7f175a52012-03-02 05:37:00 -0800129# of reporting an error condition then exiting.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400130die_err_trap() {
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400131 local result=${1:-$?}
132 local command=${2:-${BASH_COMMAND:-command unknown}}
Brian Harring7f175a52012-03-02 05:37:00 -0800133 set +e +u
134
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400135 if [[ ${result} == "0" ]]; then
136 # Let callers simplify by setting us as an EXIT trap handler.
137 return 0
138 fi
139
Brian Harring7f175a52012-03-02 05:37:00 -0800140 # Per the message, bash misreports 127 as 1 during err trap sometimes.
141 # Note this fact to ensure users don't place too much faith in the
142 # exit code in that case.
143 set -- "Command '${command}' exited with nonzero code: ${result}"
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500144 if [[ ${result} -eq 1 ]] && [[ -z $(type -t ${command}) ]]; then
145 set -- "$@" \
146 '(Note bash sometimes misreports "command not found" as exit code 1 '\
Brian Harring7f175a52012-03-02 05:37:00 -0800147'instead of 127)'
Brian Harring7f175a52012-03-02 05:37:00 -0800148 fi
Marc Herbertff9fa312015-01-16 17:18:53 -0800149 dump_trace 1
Brian Harring7f175a52012-03-02 05:37:00 -0800150 error
151 error "Command failed:"
152 DIE_PREFIX=' '
153 die_notrace "$@"
154}
155
156# Exit this script due to a failure, outputting a backtrace in the process.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400157die() {
Brian Harring7f175a52012-03-02 05:37:00 -0800158 set +e +u
Marc Herbertff9fa312015-01-16 17:18:53 -0800159 dump_trace 1
Brian Harring7f175a52012-03-02 05:37:00 -0800160 error
161 error "Error was:"
162 DIE_PREFIX=' '
163 die_notrace "$@"
164}
165
166# Exit this script w/out a backtrace.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400167die_notrace() {
Brian Harring7f175a52012-03-02 05:37:00 -0800168 set +e +u
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500169 if [[ $# -eq 0 ]]; then
Brian Harring7f175a52012-03-02 05:37:00 -0800170 set -- '(no error message given)'
171 fi
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500172 local line
Brian Harring7f175a52012-03-02 05:37:00 -0800173 for line in "$@"; do
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500174 error "${DIE_PREFIX}${line}"
Brian Harring7f175a52012-03-02 05:37:00 -0800175 done
Mike Frysinger669b28b2012-02-07 18:01:00 -0500176 exit 1
177}
178
David Jamesbf13ea82013-05-08 14:17:25 -0700179# Check for a single string in a list of space-separated strings.
180# e.g. has "foo" "foo bar baz" is true, but has "f" "foo bar baz" is not.
181has() { [[ " ${*:2} " == *" $1 "* ]]; }
182
Brian Harring9086a3f2013-01-14 01:43:16 -0800183# Directory locations inside the dev chroot; try the new default,
184# falling back to user specific paths if the upgrade has yet to
185# happen.
186_user="${USER}"
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500187[[ ${USER} == "root" ]] && _user="${SUDO_USER}"
Brian Harring9086a3f2013-01-14 01:43:16 -0800188_CHROOT_TRUNK_DIRS=( "/home/${_user}/trunk" /mnt/host/source )
189_DEPOT_TOOLS_DIRS=( "/home/${_user}/depot_tools" /mnt/host/depot_tools )
190unset _user
191
192_process_mount_pt() {
193 # Given 4 arguments; the root path, the variable to set,
194 # the old location, and the new; finally, forcing the upgrade is doable
195 # via if a 5th arg is provided.
196 # This will then try to migrate the old to new if we can do so right now
197 # (else leaving symlinks in place w/in the new), and will set $1 to the
198 # new location.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500199 local base=${1:-/} var=$2 old=$3 new=$4 force=${5:-false}
200 local _sudo=$([[ ${USER} != "root" ]] && echo sudo)
201 local val=${new}
Mike Frysinger2d9a97d2015-02-25 01:38:56 -0500202 if ${force} || [[ -L ${base}/${new} ]] || [[ ! -e ${base}/${new} ]]; then
Brian Harring9086a3f2013-01-14 01:43:16 -0800203 # Ok, it's either a symlink or this is the first run. Upgrade if we can-
204 # specifically, if we're outside the chroot and we can rmdir the old.
205 # If we cannot rmdir the old, that's due to a mount being bound to that
206 # point (even if we can't see it, it's there)- thus fallback to adding
207 # compat links.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500208 if ${force} || ( [[ ${INSIDE_CHROOT} -eq 0 ]] && \
209 ${_sudo} rmdir "${base}/${old}" 2>/dev/null ); then
Brian Harring9086a3f2013-01-14 01:43:16 -0800210 ${_sudo} rm -f "${base}/${new}" || :
211 ${_sudo} mkdir -p "${base}/${new}" "$(dirname "${base}/${old}" )"
212 ${_sudo} ln -s "${new}" "${base}/${old}"
213 else
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500214 if [[ ! -L ${base}/${new} ]]; then
Brian Harring9086a3f2013-01-14 01:43:16 -0800215 # We can't do the upgrade right now; install compatibility links.
216 ${_sudo} mkdir -p "$(dirname "${base}/${new}")" "${base}/${old}"
217 ${_sudo} ln -s "${old}" "${base}/${new}"
218 fi
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500219 val=${old}
Brian Harring9086a3f2013-01-14 01:43:16 -0800220 fi
221 fi
222 eval "${var}=\"${val}\""
223}
224
225set_chroot_trunk_dir() {
226 # This takes two optional arguments; the first being the path to the chroot
227 # base; this is only used by enter_chroot. The second argument is whether
228 # or not to force the new pathways; this is only used by make_chroot. Passing
229 # a non-null value for $2 forces the new paths.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500230 if [[ ${INSIDE_CHROOT} -eq 0 ]] && [[ -z ${1-} ]]; then
Brian Harring9086a3f2013-01-14 01:43:16 -0800231 # Can't do the upgrade, thus skip trying to do so.
232 CHROOT_TRUNK_DIR="${_CHROOT_TRUNK_DIRS[1]}"
233 DEPOT_TOOLS_DIR="${_DEPOT_TOOLS_DIRS[1]}"
234 return
235 fi
Alex Deymo76277de2015-01-22 11:02:13 -0800236 _process_mount_pt "${1:-}" CHROOT_TRUNK_DIR "${_CHROOT_TRUNK_DIRS[@]}" \
237 ${2:+true}
238 _process_mount_pt "${1:-}" DEPOT_TOOLS_DIR "${_DEPOT_TOOLS_DIRS[@]}" \
239 ${2:+true}
Brian Harring9086a3f2013-01-14 01:43:16 -0800240}
241
242set_chroot_trunk_dir
243
Anton Staaf30acb0b2011-01-26 16:00:20 -0800244# Construct a list of possible locations for the source tree. This list is
245# based on various environment variables and globals that may have been set
246# by the calling script.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400247get_gclient_root_list() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500248 if [[ ${INSIDE_CHROOT} -eq 1 ]]; then
Brian Harring2499bfb2012-12-18 16:23:31 -0800249 echo "${CHROOT_TRUNK_DIR}"
Anton Staaf30acb0b2011-01-26 16:00:20 -0800250 fi
251
Alex Deymo135faa12015-01-21 11:20:13 -0800252 if [[ -n ${COMMON_SH:-} ]]; then echo "$(dirname "${COMMON_SH}")/../.."; fi
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500253 if [[ -n ${BASH_SOURCE} ]]; then echo "$(dirname "${BASH_SOURCE}")/../.."; fi
Anton Staaf30acb0b2011-01-26 16:00:20 -0800254}
255
256# Based on the list of possible source locations we set GCLIENT_ROOT if it is
257# not already defined by looking for a src directory in each seach path
258# location. If we do not find a valid looking root we error out.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400259get_gclient_root() {
Alex Deymo135faa12015-01-21 11:20:13 -0800260 if [[ -n ${GCLIENT_ROOT:-} ]]; then
Anton Staaf30acb0b2011-01-26 16:00:20 -0800261 return
262 fi
263
264 for path in $(get_gclient_root_list); do
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500265 if [[ -d ${path}/src ]]; then
Anton Staaf30acb0b2011-01-26 16:00:20 -0800266 GCLIENT_ROOT=${path}
267 break
268 fi
269 done
270
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500271 if [[ -z ${GCLIENT_ROOT} ]]; then
Anton Staaf30acb0b2011-01-26 16:00:20 -0800272 # Using dash or sh, we don't know where we are. $0 refers to the calling
273 # script, not ourselves, so that doesn't help us.
274 echo "Unable to determine location for common.sh. If you are sourcing"
275 echo "common.sh from a script run via dash or sh, you must do it in the"
276 echo "following way:"
277 echo ' COMMON_SH="$(dirname "$0")/../../scripts/common.sh"'
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500278 echo ' . "${COMMON_SH}"'
Anton Staaf30acb0b2011-01-26 16:00:20 -0800279 echo "where the first line is the relative path from your script to"
280 echo "common.sh."
281 exit 1
282 fi
283}
284
David James76764882012-10-24 19:46:29 -0700285# Populate the ENVIRONMENT_WHITELIST array.
286load_environment_whitelist() {
287 set -f
288 ENVIRONMENT_WHITELIST=(
289 $("${GCLIENT_ROOT}/chromite/scripts/cros_env_whitelist")
290 )
291 set +f
292}
293
Anton Staaf30acb0b2011-01-26 16:00:20 -0800294# Find root of source tree
295get_gclient_root
296
rspangler@google.comd74220d2009-10-09 20:56:14 +0000297# Canonicalize the directories for the root dir and the calling script.
298# readlink is part of coreutils and should be present even in a bare chroot.
tedbo4f44d9e2010-01-08 17:26:11 -0800299# This is better than just using
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500300# FOO="$(cd ${FOO} ; pwd)"
tedbo4f44d9e2010-01-08 17:26:11 -0800301# since that leaves symbolic links intact.
rspangler@google.comd74220d2009-10-09 20:56:14 +0000302# Note that 'realpath' is equivalent to 'readlink -f'.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500303SCRIPT_LOCATION=$(readlink -f "${SCRIPT_LOCATION}")
304GCLIENT_ROOT=$(readlink -f "${GCLIENT_ROOT}")
rspangler@google.comd74220d2009-10-09 20:56:14 +0000305
306# Other directories should always be pathed down from GCLIENT_ROOT.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500307SRC_ROOT="${GCLIENT_ROOT}/src"
308SRC_INTERNAL="${GCLIENT_ROOT}/src-internal"
309SCRIPTS_DIR="${SRC_ROOT}/scripts"
310BUILD_LIBRARY_DIR="${SCRIPTS_DIR}/build_library"
Mike Frysinger66accd22017-09-13 03:50:30 -0400311CHROMITE_BIN="${GCLIENT_ROOT}/chromite/bin"
Alex Klein999443c2018-10-17 12:02:05 -0600312IMAGES_DIR="${GCLIENT_ROOT}/src/build/images"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000313
314# Load developer's custom settings. Default location is in scripts dir,
315# since that's available both inside and outside the chroot. By convention,
316# settings from this file are variables starting with 'CHROMEOS_'
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500317: ${CHROMEOS_DEV_SETTINGS:=${SCRIPTS_DIR}/.chromeos_dev}
318if [[ -f ${CHROMEOS_DEV_SETTINGS} ]]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000319 # Turn on exit-on-error during custom settings processing
Greg Spencer798d75f2011-02-01 22:04:49 -0800320 SAVE_OPTS=$(set +o)
Brian Harring7f175a52012-03-02 05:37:00 -0800321 switch_to_strict_mode
rspangler@google.comd74220d2009-10-09 20:56:14 +0000322
323 # Read settings
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500324 . "${CHROMEOS_DEV_SETTINGS}"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000325
326 # Restore previous state of exit-on-error
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500327 eval "${SAVE_OPTS}"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000328fi
329
330# Load shflags
Zdenek Behan07d24222011-11-02 00:46:25 +0000331# NOTE: This code snippet is in particular used by the au-generator (which
332# stores shflags in ./lib/shflags/) and should not be touched.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500333if [[ -f ${SCRIPTS_DIR}/lib/shflags/shflags ]]; then
Mike Frysinger77c674b2012-02-07 18:05:07 -0500334 . "${SCRIPTS_DIR}/lib/shflags/shflags" || die "Couldn't find shflags"
Zdenek Behan07d24222011-11-02 00:46:25 +0000335else
336 . ./lib/shflags/shflags || die "Couldn't find shflags"
337fi
rspangler@google.comd74220d2009-10-09 20:56:14 +0000338
Bill Richardson10d27c22010-01-20 13:38:50 -0800339# Our local mirror
340DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000341
Bill Richardson10d27c22010-01-20 13:38:50 -0800342# Upstream mirrors and build suites come in 2 flavors
343# DEV - development chroot, used to build the chromeos image
344# IMG - bootable image, to run on actual hardware
rspangler@google.comd74220d2009-10-09 20:56:14 +0000345
Bill Richardson10d27c22010-01-20 13:38:50 -0800346DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
347DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"}
348
349DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
350DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000351
352# Default location for chroot
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500353DEFAULT_CHROOT_DIR=${CHROMEOS_CHROOT_DIR:-"${GCLIENT_ROOT}/chroot"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000354
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500355# All output files from build should go under ${DEFAULT_BUILD_ROOT}, so that
rspangler@google.comd74220d2009-10-09 20:56:14 +0000356# they don't pollute the source directory.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500357DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"${SRC_ROOT}/build"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000358
Chris Ching4bc95a12016-11-22 13:44:13 -0700359# Default location for event files
360DEFAULT_EVENT_DIR=${DEFAULT_EVENT_DIR:-"${DEFAULT_BUILD_ROOT}/events"}
361
362# Default event file. Format is YYYYDD.HHMM.json
363DEFAULT_EVENT_FILE=${DEFAULT_EVENT_FILE:-"${DEFAULT_EVENT_DIR}/$(date +%Y%m%d.%H%M.).json"}
364
David McMahon49302942010-02-18 16:55:35 -0800365# Set up a global ALL_BOARDS value
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500366if [[ -d ${SRC_ROOT}/overlays ]]; then
367 ALL_BOARDS=$(cd "${SRC_ROOT}/overlays"; \
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400368 ls -1d overlay-* 2>&- | sed 's,overlay-,,g')
David Rochberg3b910702010-12-02 10:45:21 -0500369fi
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500370# Normalize whitespace.
371ALL_BOARDS=$(echo ${ALL_BOARDS})
Mike Frysingerc17a4932012-03-12 17:07:40 -0400372
373# Sets the default board variable for calling script.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500374if [[ -f ${GCLIENT_ROOT}/src/scripts/.default_board ]]; then
375 DEFAULT_BOARD=$(<"${GCLIENT_ROOT}/src/scripts/.default_board")
Mike Frysingerc17a4932012-03-12 17:07:40 -0400376 # Check for user typos like whitespace.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500377 if [[ -n ${DEFAULT_BOARD//[a-zA-Z0-9-_]} ]]; then
Mike Frysingerc17a4932012-03-12 17:07:40 -0400378 die ".default_board: invalid name detected; please fix:" \
379 "'${DEFAULT_BOARD}'"
380 fi
381fi
382# Stub to get people to upgrade.
383get_default_board() {
384 warn "please upgrade your script, and make sure to run build_packages"
385}
David McMahon49302942010-02-18 16:55:35 -0800386
David Jamesff072012010-11-30 13:22:05 -0800387# Enable --fast by default.
Greg Spencer798d75f2011-02-01 22:04:49 -0800388DEFAULT_FAST=${FLAGS_TRUE}
David James03668642010-07-28 17:08:29 -0700389
Chris Sosab0f57322011-10-25 03:07:23 +0000390# Directory to store built images. Should be set by sourcing script when used.
391BUILD_DIR=
Simon Glass142ca062011-02-09 13:39:43 -0800392
Mike Frysinger34808e52017-01-11 21:14:08 -0500393# Path to the verified boot directory where we get signing related keys/scripts.
394VBOOT_DIR="${CHROOT_TRUNK_DIR}/src/platform/vboot_reference"
395VBOOT_TESTKEYS_DIR="${VBOOT_DIR}/tests/testkeys"
Mike Frysinger40cea032017-03-12 19:48:20 -0400396# We load these from the chroot rather than directly from the vboot source repo
397# so we work correctly even in a minilayout.
398VBOOT_DEVKEYS_DIR="/usr/share/vboot/devkeys"
399VBOOT_SIGNING_DIR="/usr/share/vboot/bin"
Mike Frysinger34808e52017-01-11 21:14:08 -0500400
Simon Glass142ca062011-02-09 13:39:43 -0800401# Standard filenames
Chris Sosab0f57322011-10-25 03:07:23 +0000402CHROMEOS_BASE_IMAGE_NAME="chromiumos_base_image.bin"
Simon Glass142ca062011-02-09 13:39:43 -0800403CHROMEOS_IMAGE_NAME="chromiumos_image.bin"
Chris Sosab0f57322011-10-25 03:07:23 +0000404CHROMEOS_DEVELOPER_IMAGE_NAME="chromiumos_image.bin"
Gilad Arnold08366272012-02-08 10:46:26 -0800405CHROMEOS_RECOVERY_IMAGE_NAME="recovery_image.bin"
Simon Glass142ca062011-02-09 13:39:43 -0800406CHROMEOS_TEST_IMAGE_NAME="chromiumos_test_image.bin"
Chris Sosab0f57322011-10-25 03:07:23 +0000407CHROMEOS_FACTORY_INSTALL_SHIM_NAME="factory_install_shim.bin"
Bertrand SIMONNETb17cd572015-04-01 10:27:37 -0700408SYSROOT_SETTINGS_FILE="/var/cache/edb/chromeos"
Simon Glass142ca062011-02-09 13:39:43 -0800409
Rahul Chaudhry306f6882015-05-13 15:14:13 -0700410# Install mask for portage ebuilds. Used by build_image and gmergefs.
Chris Masoned11ce172010-11-09 14:22:08 -0800411# TODO: Is /usr/local/autotest-chrome still used by anyone?
Hung-Te Lind32c59f2012-01-19 19:54:01 +0800412COMMON_INSTALL_MASK="
Daniel Erate82f07c2010-12-21 13:39:22 -0800413 *.a
Alex Deymo1856c4f2014-09-24 08:15:34 -0700414 *.c
415 *.cc
Rahul Chaudhry306f6882015-05-13 15:14:13 -0700416 *.go
Daniel Erate82f07c2010-12-21 13:39:22 -0800417 *.la
Rene Bolldorf32228222012-07-21 14:18:22 -0400418 *.h
Brian Norrisef0880d2016-05-19 12:39:56 -0700419 *.hh
Rene Bolldorf32228222012-07-21 14:18:22 -0400420 *.hpp
Kevin Cernekee6ab57a92015-06-25 15:43:06 -0700421 *.h++
Nam T. Nguyenaa15f142015-05-15 14:27:50 -0700422 *.hxx
Mike Frysinger97c6ac92014-06-16 00:06:37 -0400423 */.keep*
Shuhei Takahashic62d3072019-01-29 18:04:35 +0900424 /build/libexec/tast
425 /build/share/tast
Daniel Erate82f07c2010-12-21 13:39:22 -0800426 /etc/init.d
427 /etc/runlevels
Qijiang Fan6834cba2018-04-04 16:41:56 +0900428 /etc/selinux/intermediates
Hung-Te Lin76272be2012-08-07 19:10:09 +0800429 /firmware
Mike Frysinger785f09d2013-09-25 22:11:21 -0400430 /lib/modules/*/vdso
Daniel Erate82f07c2010-12-21 13:39:22 -0800431 /lib/rc
Tomasz Figa435f0e52017-01-06 14:28:06 +0900432 /opt/google/containers/android/vendor/lib*/pkgconfig
Benjamin Gordonffcb2b12017-10-24 13:02:46 -0600433 /opt/google/containers/android/build
Nam T. Nguyenf1de1a72015-05-27 08:37:46 -0700434 /usr/bin/*-config
Daniel Erate82f07c2010-12-21 13:39:22 -0800435 /usr/bin/Xnest
436 /usr/bin/Xvfb
Manoj Gupta019843b2017-08-17 13:28:18 -0700437 /usr/include/c++
Mike Frysinger10c56472013-03-01 00:58:10 -0500438 /usr/include/nspr/*
Yunlian Jianged7cd2d2018-10-27 13:07:02 -0700439 /usr/include/rpcsvc/*.x
Bing Xue18e37582019-01-17 17:04:54 -0800440 /usr/include/tensorflow
Yufeng Wang6dea68d2019-05-13 11:52:51 -0700441 /usr/include/boost
Mike Frysinger10c56472013-03-01 00:58:10 -0500442 /usr/include/X11/*
Daniel Erate82f07c2010-12-21 13:39:22 -0800443 /usr/lib/debug
Rahul Chaudhry306f6882015-05-13 15:14:13 -0700444 /usr/lib/gopath
Mike Frysinger27b5edb2012-08-08 18:52:06 -0400445 /usr/lib*/pkgconfig
Daniel Erate82f07c2010-12-21 13:39:22 -0800446 /usr/local/autotest-chrome
447 /usr/man
448 /usr/share/aclocal
Brian Norris0f3c4e22016-06-21 12:23:18 -0700449 /usr/share/cups/drv
Daniel Erate82f07c2010-12-21 13:39:22 -0800450 /usr/share/doc
451 /usr/share/gettext
452 /usr/share/gtk-2.0
453 /usr/share/gtk-doc
454 /usr/share/info
455 /usr/share/man
Brian Norris0f3c4e22016-06-21 12:23:18 -0700456 /usr/share/ppd
Daniel Erate82f07c2010-12-21 13:39:22 -0800457 /usr/share/openrc
458 /usr/share/pkgconfig
Ryan Harrisonb8bd5942013-02-02 13:39:43 -0500459 /usr/share/profiling
Daniel Erate82f07c2010-12-21 13:39:22 -0800460 /usr/share/readline
Chris Wolfed13775f2011-07-26 16:34:38 -0400461 /usr/src
Daniel Erate82f07c2010-12-21 13:39:22 -0800462 "
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700463
Hung-Te Lind32c59f2012-01-19 19:54:01 +0800464# Mask for base, dev, and test images (build_image, build_image --test)
465DEFAULT_INSTALL_MASK="
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500466 ${COMMON_INSTALL_MASK}
Don Garrettac1cd0d2013-07-18 18:04:06 -0700467 /boot/config-*
468 /boot/System.map-*
Aviv Kesheta1838ba2013-07-23 10:58:23 -0700469 /usr/local/build/autotest
Olof Johansson1222b2e2012-08-08 15:27:35 -0700470 /lib/modules/*/build
471 /lib/modules/*/source
Kees Cook74f163b2012-12-18 15:46:51 -0800472 test_*.ko
Hung-Te Lind32c59f2012-01-19 19:54:01 +0800473 "
474
Chris Sosac9422fa2012-03-05 15:58:07 -0800475# Mask for factory install shim (build_image factory_install)
Hung-Te Lind32c59f2012-01-19 19:54:01 +0800476FACTORY_SHIM_INSTALL_MASK="
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500477 ${DEFAULT_INSTALL_MASK}
Cheng-Han Yangc924c2c2018-11-02 17:02:29 +0800478 /opt/google/chrome
479 /opt/google/containers
Vic Yang982556a2012-12-11 11:41:54 +0800480 /usr/lib64/dri
Hung-Te Lin7f721ec2017-07-18 15:14:48 +0800481 /usr/lib/dri
Daniel Erate82f07c2010-12-21 13:39:22 -0800482 /usr/share/X11
Hung-Te Lin7f721ec2017-07-18 15:14:48 +0800483 /usr/share/chromeos-assets/[^i]*
484 /usr/share/chromeos-assets/i[^m]*
Daniel Erate82f07c2010-12-21 13:39:22 -0800485 /usr/share/fonts
Daniel Erate82f07c2010-12-21 13:39:22 -0800486 /usr/share/locale
Daniel Erate82f07c2010-12-21 13:39:22 -0800487 /usr/share/mime
Vic Yang982556a2012-12-11 11:41:54 +0800488 /usr/share/oem
Daniel Erate82f07c2010-12-21 13:39:22 -0800489 /usr/share/sounds
490 /usr/share/tts
491 /usr/share/zoneinfo
492 "
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800493
Sabin Floarese790ea72016-11-21 14:56:41 +0200494# Mask for images without systemd.
495SYSTEMD_INSTALL_MASK="
Chris Morin83226ab2018-12-07 19:53:48 -0800496 /lib/systemd/network
497 /usr/lib/systemd/system
Sabin Floarese790ea72016-11-21 14:56:41 +0200498"
499
rspangler@google.comd74220d2009-10-09 20:56:14 +0000500# -----------------------------------------------------------------------------
501# Functions
502
Don Garrett640a0582010-05-04 16:54:28 -0700503# Enter a chroot and restart the current script if needed
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400504restart_in_chroot_if_needed() {
David Rochberg3b910702010-12-02 10:45:21 -0500505 # NB: Pass in ARGV: restart_in_chroot_if_needed "$@"
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500506 if [[ ${INSIDE_CHROOT} -ne 1 ]]; then
Chris Sosafd2cdec2011-03-24 16:06:59 -0700507 # Get inside_chroot path for script.
508 local chroot_path="$(reinterpret_path_for_chroot "$0")"
Mike Frysinger66accd22017-09-13 03:50:30 -0400509 exec "${CHROMITE_BIN}/cros_sdk" -- "${chroot_path}" "$@"
Don Garrett640a0582010-05-04 16:54:28 -0700510 fi
511}
512
rspangler@google.comd74220d2009-10-09 20:56:14 +0000513# Fail unless we're inside the chroot. This guards against messing up your
514# workstation.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400515assert_inside_chroot() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500516 if [[ ${INSIDE_CHROOT} -ne 1 ]]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000517 echo "This script must be run inside the chroot. Run this first:"
Zdenek Behan2811c162011-08-13 00:47:38 +0200518 echo " cros_sdk"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000519 exit 1
520 fi
521}
522
523# Fail if we're inside the chroot. This guards against creating or entering
524# nested chroots, among other potential problems.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400525assert_outside_chroot() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500526 if [[ ${INSIDE_CHROOT} -ne 0 ]]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000527 echo "This script must be run outside the chroot."
528 exit 1
529 fi
530}
531
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400532assert_not_root_user() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500533 if [[ ${UID:-$(id -u)} == 0 ]]; then
derat@google.com86dcc8e2009-11-21 19:49:49 +0000534 echo "This script must be run as a non-root user."
535 exit 1
536 fi
537}
538
David James76764882012-10-24 19:46:29 -0700539assert_root_user() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500540 if [[ ${UID:-$(id -u)} != 0 ]] || [[ ${SUDO_USER:-root} == "root" ]]; then
David James76764882012-10-24 19:46:29 -0700541 die_notrace "This script must be run using sudo from a non-root user."
542 fi
543}
544
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700545# Removes single quotes around parameter
546# Arguments:
547# $1 - string which optionally has surrounding quotes
548# Returns:
549# None, but prints the string without quotes.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400550remove_quotes() {
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700551 echo "$1" | sed -e "s/^'//; s/'$//"
552}
tedbo373c3902010-04-12 10:52:40 -0700553
554# Writes stdin to the given file name as root using sudo in overwrite mode.
555#
556# $1 - The output file name.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400557sudo_clobber() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500558 sudo tee "$1" >/dev/null
tedbo373c3902010-04-12 10:52:40 -0700559}
560
561# Writes stdin to the given file name as root using sudo in append mode.
562#
563# $1 - The output file name.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400564sudo_append() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500565 sudo tee -a "$1" >/dev/null
tedbo373c3902010-04-12 10:52:40 -0700566}
robotboy98912212010-04-12 14:08:14 -0700567
Mike Frysinger286b5922011-09-28 11:59:53 -0400568# Execute multiple commands in a single sudo. Generally will speed things
569# up by avoiding multiple calls to `sudo`. If any commands fail, we will
570# call die with the failing command. We can handle a max of ~100 commands,
571# but hopefully no one will ever try that many at once.
572#
573# $@ - The commands to execute, one per arg.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400574sudo_multi() {
Mike Frysinger286b5922011-09-28 11:59:53 -0400575 local i cmds
576
577 # Construct the shell code to execute. It'll be of the form:
578 # ... && ( ( command ) || exit <command index> ) && ...
579 # This way we know which command exited. The exit status of
580 # the underlying command is lost, but we never cared about it
581 # in the first place (other than it is non zero), so oh well.
582 for (( i = 1; i <= $#; ++i )); do
583 cmds+=" && ( ( ${!i} ) || exit $(( i + 10 )) )"
584 done
585
586 # Execute our constructed shell code.
587 sudo -- sh -c ":${cmds[*]}" && i=0 || i=$?
588
589 # See if this failed, and if so, print out the failing command.
590 if [[ $i -gt 10 ]]; then
591 : $(( i -= 10 ))
592 die "sudo_multi failed: ${!i}"
593 elif [[ $i -ne 0 ]]; then
594 die "sudo_multi failed for unknown reason $i"
595 fi
596}
597
Chris Masonebbccc242014-02-08 16:23:53 -0800598# Clears out stale shadow-utils locks in the given target root.
599sudo_clear_shadow_locks() {
600 info "Clearing shadow utils lockfiles under $1"
601 sudo rm -f "$1/etc/"{passwd,group,shadow,gshadow}.lock*
602}
603
David James76764882012-10-24 19:46:29 -0700604# Writes stdin to the given file name as the sudo user in overwrite mode.
605#
606# $@ - The output file names.
607user_clobber() {
608 install -m644 -o ${SUDO_UID} -g ${SUDO_GID} /dev/stdin "$@"
609}
610
David James22dc2ba2012-11-29 15:46:58 -0800611# Copies the specified file owned by the user to the specified location.
612# If the copy fails as root (e.g. due to root_squash and NFS), retry the copy
613# with the user's account before failing.
614user_cp() {
615 cp -p "$@" 2>/dev/null || sudo -u ${SUDO_USER} -- cp -p "$@"
616}
617
David James76764882012-10-24 19:46:29 -0700618# Appends stdin to the given file name as the sudo user.
619#
620# $1 - The output file name.
621user_append() {
622 cat >> "$1"
623 chown ${SUDO_UID}:${SUDO_GID} "$1"
624}
625
626# Create the specified directory, along with parents, as the sudo user.
627#
628# $@ - The directories to create.
629user_mkdir() {
630 install -o ${SUDO_UID} -g ${SUDO_GID} -d "$@"
631}
632
633# Create the specified symlink as the sudo user.
634#
635# $1 - Link target
636# $2 - Link name
637user_symlink() {
638 ln -sfT "$1" "$2"
639 chown -h ${SUDO_UID}:${SUDO_GID} "$2"
640}
641
Mike Frysinger1aa61242011-09-15 17:46:44 -0400642# Locate all mounts below a specified directory.
643#
644# $1 - The root tree.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400645sub_mounts() {
Mike Frysinger1aa61242011-09-15 17:46:44 -0400646 # Assume that `mount` outputs a list of mount points in the order
647 # that things were mounted (since it always has and hopefully always
648 # will). As such, we have to unmount in reverse order to cleanly
649 # unmount submounts (think /dev/pts and /dev).
Alex Deymo7ba56be2015-02-12 14:28:06 -0800650 awk -v path="$1" -v len="${#1}" \
Benjamin Gordon1d5afed2017-06-14 16:35:32 -0600651 '(substr($2, 1, len+1) == path ||
652 substr($2, 1, len+1) == (path "/")) { print $2 }' /proc/mounts | \
Zdenek Behan52970f42012-08-30 22:36:40 +0200653 tac | \
654 sed -e 's/\\040(deleted)$//'
655 # Hack(zbehan): If a bind mount's source is mysteriously removed,
656 # we'd end up with an orphaned mount with the above string in its name.
657 # It can only be seen through /proc/mounts and will stick around even
658 # when it should be gone already. crosbug.com/31250
Mike Frysinger1aa61242011-09-15 17:46:44 -0400659}
660
robotboy98912212010-04-12 14:08:14 -0700661# Unmounts a directory, if the unmount fails, warn, and then lazily unmount.
662#
663# $1 - The path to unmount.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400664safe_umount_tree() {
Alex Deymo7ba56be2015-02-12 14:28:06 -0800665 local mount_point="$1"
robotboy98912212010-04-12 14:08:14 -0700666
Alex Deymo7ba56be2015-02-12 14:28:06 -0800667 local mounts=( $(sub_mounts "${mount_point}") )
668
669 # Silently return if the mount_point was already unmounted.
670 if [[ ${#mounts[@]} -eq 0 ]]; then
Mike Frysingere8aec372011-09-21 00:03:22 -0400671 return 0
672 fi
673
Mike Frysinger1aa61242011-09-15 17:46:44 -0400674 # First try to unmount in one shot to speed things up.
Chirantan Ekbote30823e92016-06-29 11:39:03 -0700675 if LC_ALL=C safe_umount -d "${mounts[@]}"; then
676 return 0
David Jamesd9b67982012-10-26 09:46:50 -0700677 fi
678
679 # Well that didn't work, so lazy unmount remaining ones.
Alex Deymo7ba56be2015-02-12 14:28:06 -0800680 warn "Failed to unmount ${mounts[@]}, these are the processes using the" \
681 "mount points."
682 sudo fuser -vm "${mount_point}" || true
683
Mike Frysinger1aa61242011-09-15 17:46:44 -0400684 warn "Doing a lazy unmount"
Alex Deymo7ba56be2015-02-12 14:28:06 -0800685 if ! safe_umount -d -l "${mounts[@]}"; then
686 mounts=( $(sub_mounts "${mount_point}") )
687 die "Failed to lazily unmount ${mounts[@]}"
robotboy98912212010-04-12 14:08:14 -0700688 fi
689}
Chris Sosa702618f2010-05-14 12:52:32 -0700690
Brian Harringece65e02012-08-30 13:42:21 -0700691
David James76764882012-10-24 19:46:29 -0700692# Run umount as root.
Brian Harringece65e02012-08-30 13:42:21 -0700693safe_umount() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500694 $([[ ${UID:-$(id -u)} != 0 ]] && echo sudo) umount "$@"
Brian Harringece65e02012-08-30 13:42:21 -0700695}
696
Gabe Black0a68b202014-10-08 22:09:10 -0700697# Run a command with sudo in a way that still preferentially uses files
698# from au-generator.zip, but still finds things in /sbin and /usr/sbin when
699# not using au-generator.zip.
700au_generator_sudo() {
701 # When searching for env, the unmodified path is used and env is potentially
702 # found somewhere out in the system. env itself sees the modified PATH
703 # and will find the command where we're telling it to. Running the command
704 # directly without env will escape our constructed PATH.
705 sudo -E PATH="${PATH}:/sbin:/usr/sbin" env "$@"
706}
707
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700708# Setup a loopback device for a file and scan for partitions, with retries.
709#
710# $1 - The file to back the new loopback device.
711# $2-$N - Additional arguments to pass to losetup.
712loopback_partscan() {
713 local lb_dev image="$1"
714 shift
Gabe Black01ba4de2015-03-11 16:34:56 -0700715 lb_dev=$(au_generator_sudo losetup --show -f "$@" "${image}")
716 # Ignore problems deleting existing partitions. There shouldn't be any
717 # which will upset partx, but that's actually ok.
718 au_generator_sudo partx -d "${lb_dev}" 2>/dev/null || true
719 au_generator_sudo partx -a "${lb_dev}"
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700720
Gabe Black7bb6c5a2015-03-16 20:23:06 -0700721 echo "${lb_dev}"
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700722}
723
724# Detach a loopback device set up earlier.
725#
Gabe Black01ba4de2015-03-11 16:34:56 -0700726# $1 - The loop device to detach.
727# $2-$N - Additional arguments to pass to losetup.
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700728loopback_detach() {
Mike Frysingeraf64ed52015-04-06 13:20:39 -0400729 # Retry the deletes before we detach. crbug.com/469259
730 local i
731 for (( i = 0; i < 10; i++ )); do
732 if au_generator_sudo partx -d "$1"; then
733 break
734 fi
735 warn "Sleeping & retrying ..."
736 sync
737 sleep 1
738 done
Gabe Black0a68b202014-10-08 22:09:10 -0700739 au_generator_sudo losetup --detach "$@"
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700740}
741
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500742# Fixes symlinks that are incorrectly prefixed with the build root $1
Chris Sosad4455022010-05-20 10:14:06 -0700743# rather than the real running root '/'.
744# TODO(sosa) - Merge setup - cleanup below with this method.
745fix_broken_symlinks() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500746 local build_root=$1
Chris Sosad4455022010-05-20 10:14:06 -0700747 local symlinks=$(find "${build_root}/usr/local" -lname "${build_root}/*")
Greg Spencer798d75f2011-02-01 22:04:49 -0800748 local symlink
Chris Sosad4455022010-05-20 10:14:06 -0700749 for symlink in ${symlinks}; do
750 echo "Fixing ${symlink}"
751 local target=$(ls -l "${symlink}" | cut -f 2 -d '>')
752 # Trim spaces from target (bashism).
753 target=${target/ /}
754 # Make new target (removes rootfs prefix).
755 new_target=$(echo ${target} | sed "s#${build_root}##")
756
757 echo "Fixing symlink ${symlink}"
758 sudo unlink "${symlink}"
759 sudo ln -sf "${new_target}" "${symlink}"
760 done
761}
762
Chris Sosa702618f2010-05-14 12:52:32 -0700763# Sets up symlinks for the developer root. It is necessary to symlink
764# usr and local since the developer root is mounted at /usr/local and
765# applications expect to be installed under /usr/local/bin, etc.
766# This avoids packages installing into /usr/local/usr/local/bin.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500767# $1 specifies the symlink target for the developer root.
768# $2 specifies the symlink target for the var directory.
769# $3 specifies the location of the stateful partition.
Chris Sosa702618f2010-05-14 12:52:32 -0700770setup_symlinks_on_root() {
771 # Give args better names.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500772 local dev_image_target=$1
773 local var_target=$2
774 local dev_image_root="$3/dev_image"
Chris Sosa702618f2010-05-14 12:52:32 -0700775
Mike Frysinger9e155382019-07-12 21:11:00 -0400776 # Make sure the dev_image dir itself exists.
777 if [[ ! -d "${dev_image_root}" ]]; then
778 sudo mkdir "${dev_image_root}"
779 fi
780
Chris Sosa702618f2010-05-14 12:52:32 -0700781 # If our var target is actually the standard var, we are cleaning up the
782 # symlinks (could also check for /usr/local for the dev_image_target).
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500783 if [[ ${var_target} == "/var" ]]; then
Chris Sosa702618f2010-05-14 12:52:32 -0700784 echo "Cleaning up /usr/local symlinks for ${dev_image_root}"
785 else
786 echo "Setting up symlinks for /usr/local for ${dev_image_root}"
787 fi
788
789 # Set up symlinks that should point to ${dev_image_target}.
Greg Spencer798d75f2011-02-01 22:04:49 -0800790 local path
Chris Sosa702618f2010-05-14 12:52:32 -0700791 for path in usr local; do
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500792 if [[ -h ${dev_image_root}/${path} ]]; then
Chris Sosa702618f2010-05-14 12:52:32 -0700793 sudo unlink "${dev_image_root}/${path}"
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500794 elif [[ -e ${dev_image_root}/${path} ]]; then
Chris Sosa702618f2010-05-14 12:52:32 -0700795 die "${dev_image_root}/${path} should be a symlink if exists"
796 fi
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400797 sudo ln -s "${dev_image_target}" "${dev_image_root}/${path}"
Chris Sosa702618f2010-05-14 12:52:32 -0700798 done
799
800 # Setup var symlink.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500801 if [[ -h ${dev_image_root}/var ]]; then
Chris Sosa702618f2010-05-14 12:52:32 -0700802 sudo unlink "${dev_image_root}/var"
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500803 elif [[ -e ${dev_image_root}/var ]]; then
Chris Sosa702618f2010-05-14 12:52:32 -0700804 die "${dev_image_root}/var should be a symlink if it exists"
805 fi
806
807 sudo ln -s "${var_target}" "${dev_image_root}/var"
808}
Nick Sandersd2509272010-06-16 03:50:04 -0700809
Will Drewry55b42c92010-10-20 15:44:11 -0500810# These two helpers clobber the ro compat value in our root filesystem.
811#
812# When the system is built with --enable_rootfs_verification, bit-precise
813# integrity checking is performed. That precision poses a usability issue on
814# systems that automount partitions with recognizable filesystems, such as
815# ext2/3/4. When the filesystem is mounted 'rw', ext2 metadata will be
816# automatically updated even if no other writes are performed to the
817# filesystem. In addition, ext2+ does not support a "read-only" flag for a
818# given filesystem. That said, forward and backward compatibility of
819# filesystem features are supported by tracking if a new feature breaks r/w or
820# just write compatibility. We abuse the read-only compatibility flag[1] in
821# the filesystem header by setting the high order byte (le) to FF. This tells
822# the kernel that features R24-R31 are all enabled. Since those features are
823# undefined on all ext-based filesystem, all standard kernels will refuse to
824# mount the filesystem as read-write -- only read-only[2].
825#
826# [1] 32-bit flag we are modifying:
Aaron Gable5dde8482013-09-27 18:35:57 -0700827# https://chromium.googlesource.com/chromiumos/third_party/kernel.git/+/master/include/linux/ext2_fs.h#l417
Will Drewry55b42c92010-10-20 15:44:11 -0500828# [2] Mount behavior is enforced here:
Aaron Gable5dde8482013-09-27 18:35:57 -0700829# https://chromium.googlesource.com/chromiumos/third_party/kernel.git/+/master/ext2/super.c#l857
Will Drewry55b42c92010-10-20 15:44:11 -0500830#
831# N.B., if the high order feature bits are used in the future, we will need to
832# revisit this technique.
833disable_rw_mount() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500834 local rootfs=$1
Will Drewry55b42c92010-10-20 15:44:11 -0500835 local offset="${2-0}" # in bytes
Will Drewry9b7cb512010-10-20 18:11:24 -0500836 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
Alex Deymof6a527d2015-01-12 18:30:58 -0800837 is_ext_filesystem "${rootfs}" "${offset}" || return 0
Alex Deymo41226312015-03-09 15:35:24 -0700838 is_ext2_rw_mount_enabled "${rootfs}" "${offset}" || return 0
839
840 make_block_device_rw "${rootfs}"
Will Drewry9b7cb512010-10-20 18:11:24 -0500841 printf '\377' |
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500842 sudo dd of="${rootfs}" seek=$((offset + ro_compat_offset)) \
Alex Deymo41226312015-03-09 15:35:24 -0700843 conv=notrunc count=1 bs=1 status=none
LaMont Jones70743742019-04-26 10:14:33 -0600844 # Force all of the file writes to complete, in case it's necessary for
845 # crbug.com/954188
846 sync
Will Drewry55b42c92010-10-20 15:44:11 -0500847}
848
849enable_rw_mount() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500850 local rootfs=$1
Will Drewry55b42c92010-10-20 15:44:11 -0500851 local offset="${2-0}"
Will Drewry9b7cb512010-10-20 18:11:24 -0500852 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
Alex Deymof6a527d2015-01-12 18:30:58 -0800853 is_ext_filesystem "${rootfs}" "${offset}" || return 0
Alex Deymo41226312015-03-09 15:35:24 -0700854 is_ext2_rw_mount_enabled "${rootfs}" "${offset}" && return 0
855
856 make_block_device_rw "${rootfs}"
Will Drewry9b7cb512010-10-20 18:11:24 -0500857 printf '\000' |
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500858 sudo dd of="${rootfs}" seek=$((offset + ro_compat_offset)) \
Alex Deymo41226312015-03-09 15:35:24 -0700859 conv=notrunc count=1 bs=1 status=none
LaMont Jones70743742019-04-26 10:14:33 -0600860 # Force all of the file writes to complete, in case it's necessary for
861 # crbug.com/954188
862 sync
Alex Deymo41226312015-03-09 15:35:24 -0700863}
864
865is_ext2_rw_mount_enabled() {
866 local rootfs=$1
867 local offset="${2-0}"
868 local ro_compat_offset=$((0x464 + 3)) # Get 'highest' byte
869 local ro_compat_flag=$(sudo dd if="${rootfs}" \
870 skip=$((offset + ro_compat_offset)) bs=1 count=1 status=none \
871 2>/dev/null | hexdump -e '1 "%.2x"')
872 test "${ro_compat_flag}" = "00"
Will Drewry55b42c92010-10-20 15:44:11 -0500873}
874
Alex Deymof9235952015-01-15 11:15:09 -0800875# Returns whether the passed rootfs is an extended filesystem by checking the
Alex Deymof6a527d2015-01-12 18:30:58 -0800876# ext2 s_magic field in the superblock.
877is_ext_filesystem() {
878 local rootfs=$1
879 local offset="${2-0}"
880 local ext_magic_offset=$((0x400 + 56))
Alex Deymo41226312015-03-09 15:35:24 -0700881 local ext_magic=$(sudo dd if="${rootfs}" \
882 skip=$((offset + ext_magic_offset)) bs=1 count=2 2>/dev/null |
883 hexdump -e '1/2 "%.4x"')
Alex Deymof6a527d2015-01-12 18:30:58 -0800884 test "${ext_magic}" = "ef53"
885}
886
Alex Deymo41226312015-03-09 15:35:24 -0700887# If the passed argument is a block device, ensure it is writtable and make it
888# writtable if not.
889make_block_device_rw() {
890 local block_dev="$1"
891 [[ -b "${block_dev}" ]] || return 0
892 if [[ $(sudo blockdev --getro "${block_dev}") == "1" ]]; then
893 sudo blockdev --setrw "${block_dev}"
894 fi
895}
896
Nick Sandersd2509272010-06-16 03:50:04 -0700897# Get current timestamp. Assumes common.sh runs at startup.
898start_time=$(date +%s)
899
Matt Tennant298f61a2012-06-25 21:54:33 -0700900# Get time elapsed since start_time in seconds.
901get_elapsed_seconds() {
Greg Spencer798d75f2011-02-01 22:04:49 -0800902 local end_time=$(date +%s)
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500903 local elapsed_seconds=$(( end_time - start_time ))
Matt Tennant298f61a2012-06-25 21:54:33 -0700904 echo ${elapsed_seconds}
905}
906
907# Print time elapsed since start_time.
908print_time_elapsed() {
909 # Optional first arg to specify elapsed_seconds. If not given, will
910 # recalculate elapsed time to now. Optional second arg to specify
911 # command name associated with elapsed time.
912 local elapsed_seconds=${1:-$(get_elapsed_seconds)}
913 local cmd_base=${2:-}
914
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500915 local minutes=$(( elapsed_seconds / 60 ))
916 local seconds=$(( elapsed_seconds % 60 ))
Matt Tennant298f61a2012-06-25 21:54:33 -0700917
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500918 if [[ -n ${cmd_base} ]]; then
Matt Tennant298f61a2012-06-25 21:54:33 -0700919 info "Elapsed time (${cmd_base}): ${minutes}m${seconds}s"
920 else
921 info "Elapsed time: ${minutes}m${seconds}s"
922 fi
923}
924
Matt Tennant298f61a2012-06-25 21:54:33 -0700925command_completed() {
926 # Call print_elapsed_time regardless.
927 local run_time=$(get_elapsed_seconds)
Mike Frysinger3131d412019-03-02 14:09:37 -0500928 local cmd_base=$(basename "$0")
Matt Tennant298f61a2012-06-25 21:54:33 -0700929 print_time_elapsed ${run_time} ${cmd_base}
Nick Sandersd2509272010-06-16 03:50:04 -0700930}
Doug Anderson0c9e88d2010-10-19 14:49:39 -0700931
Bertrand SIMONNETb17cd572015-04-01 10:27:37 -0700932# Load a single variable from a bash file.
933# $1 - Path to the file.
934# $2 - Variable to get.
935get_variable() {
936 local filepath=$1
937 local variable=$2
938 local lockfile="${filepath}.lock"
939
940 if [[ -e "${filepath}" ]]; then
941 userowned_file "${lockfile}"
Bertrand SIMONNET48b6e782015-03-26 16:04:07 -0700942 (
Bertrand SIMONNETb17cd572015-04-01 10:27:37 -0700943 flock 201
944 . "${filepath}"
Bertrand SIMONNET48b6e782015-03-26 16:04:07 -0700945 if [[ "${!variable+set}" == "set" ]]; then
946 echo "${!variable}"
Bertrand SIMONNET48b6e782015-03-26 16:04:07 -0700947 fi
Bertrand SIMONNETb17cd572015-04-01 10:27:37 -0700948 ) 201>"${lockfile}"
949 fi
950}
951
Bertrand SIMONNETb17cd572015-04-01 10:27:37 -0700952# Creates a user owned file.
953# $1 - Path to the file.
954userowned_file() {
955 local filepath=$1
956
957 if [[ ! -w "${filepath}" ]]; then
958 cmds=(
959 "mkdir -p '$(dirname "${filepath}")'"
960 "touch '${filepath}'"
961 "chown ${USER} '${filepath}'"
Bertrand SIMONNET48b6e782015-03-26 16:04:07 -0700962 )
Bertrand SIMONNETb17cd572015-04-01 10:27:37 -0700963 sudo_multi "${cmds[@]}"
Bertrand SIMONNET48b6e782015-03-26 16:04:07 -0700964 fi
965}
966
Albert Chaulk5d190f72013-07-12 11:42:18 -0700967# Load configuration files that allow board-specific overrides of default
968# functionality to be specified in overlays.
Steve Fungd3b1f5f2015-03-29 21:47:24 -0700969# $1 - File to load.
Albert Chaulk5d190f72013-07-12 11:42:18 -0700970load_board_specific_script() {
Steve Fung88897cc2015-03-25 13:38:38 -0700971 local file=$1 overlay
972 [[ $# -ne 1 ]] && die "load_board_specific_script requires exactly 1 param"
973 for overlay in ${BOARD_OVERLAY}; do
Albert Chaulk5d190f72013-07-12 11:42:18 -0700974 local setup_sh="${overlay}/scripts/${file}"
975 if [[ -e ${setup_sh} ]]; then
976 source "${setup_sh}"
977 fi
978 done
979}
980
Chris Sosafd2cdec2011-03-24 16:06:59 -0700981# Reinterprets path from outside the chroot for use inside.
982# Returns "" if "" given.
983# $1 - The path to reinterpret.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400984reinterpret_path_for_chroot() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500985 if [[ ${INSIDE_CHROOT} -ne 1 ]]; then
986 if [[ -z $1 ]]; then
Chris Sosafd2cdec2011-03-24 16:06:59 -0700987 echo ""
988 else
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500989 local path_abs_path=$(readlink -f "$1")
Chris Sosafd2cdec2011-03-24 16:06:59 -0700990 local gclient_root_abs_path=$(readlink -f "${GCLIENT_ROOT}")
991
992 # Strip the repository root from the path.
993 local relative_path=$(echo ${path_abs_path} \
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400994 | sed "s:${gclient_root_abs_path}/::")
Chris Sosafd2cdec2011-03-24 16:06:59 -0700995
Mike Frysingerbaae8eb2013-01-15 01:34:26 -0500996 if [[ ${relative_path} == "${path_abs_path}" ]]; then
997 die "Error reinterpreting path. Path $1 is not within source tree."
Chris Sosafd2cdec2011-03-24 16:06:59 -0700998 fi
999
1000 # Prepend the chroot repository path.
Chris Sosaed2ff4b2013-08-30 12:58:41 -07001001 echo "/mnt/host/source/${relative_path}"
Chris Sosafd2cdec2011-03-24 16:06:59 -07001002 fi
1003 else
1004 # Path is already inside the chroot :).
Mike Frysingerbaae8eb2013-01-15 01:34:26 -05001005 echo "$1"
Chris Sosafd2cdec2011-03-24 16:06:59 -07001006 fi
1007}
Gabe Black83d8b822011-08-01 17:50:09 -07001008
Mike Frysinger6b1abb22012-05-11 13:44:06 -04001009emerge_custom_kernel() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -05001010 local install_root=$1
David Jamesdee866c2012-03-15 14:53:19 -07001011 local root=/build/${FLAGS_board}
David James0ea96e42011-08-03 11:53:50 -07001012 local tmp_pkgdir=${root}/custom-packages
1013
1014 # Clean up any leftover state in custom directories.
Mike Frysingera1a06ab2011-08-10 11:40:30 -04001015 sudo rm -rf "${tmp_pkgdir}"
David James0ea96e42011-08-03 11:53:50 -07001016
Aviv Keshet14373912014-06-27 14:34:06 +00001017 # Update chromeos-initramfs to contain the latest binaries from the build
1018 # tree. This is basically just packaging up already-built binaries from
1019 # ${root}. We are careful not to muck with the existing prebuilts so that
1020 # prebuilts can be uploaded in parallel.
1021 # TODO(davidjames): Implement ABI deps so that chromeos-initramfs will be
1022 # rebuilt automatically when its dependencies change.
1023 sudo -E PKGDIR="${tmp_pkgdir}" ${EMERGE_BOARD_CMD} -1 \
1024 chromeos-base/chromeos-initramfs || die "Cannot emerge chromeos-initramfs"
1025
David James0ea96e42011-08-03 11:53:50 -07001026 # Verify all dependencies of the kernel are installed. This should be a
1027 # no-op, but it's good to check in case a developer didn't run
Mike Frysinger0957a182012-03-21 23:17:14 -04001028 # build_packages. We need the expand_virtual call to workaround a bug
1029 # in portage where it only installs the virtual pkg.
1030 local kernel=$(portageq-${FLAGS_board} expand_virtual ${root} \
1031 virtual/linux-sources)
Mike Frysingerbaae8eb2013-01-15 01:34:26 -05001032 sudo -E PKGDIR="${tmp_pkgdir}" ${EMERGE_BOARD_CMD} --onlydeps \
David James0ea96e42011-08-03 11:53:50 -07001033 ${kernel} || die "Cannot emerge kernel dependencies"
1034
1035 # Build the kernel. This uses the standard root so that we can pick up the
1036 # initramfs from there. But we don't actually install the kernel to the
1037 # standard root, because that'll muck up the kernel debug symbols there,
1038 # which we want to upload in parallel.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -05001039 sudo -E PKGDIR="${tmp_pkgdir}" ${EMERGE_BOARD_CMD} --buildpkgonly \
David James0ea96e42011-08-03 11:53:50 -07001040 ${kernel} || die "Cannot emerge kernel"
1041
1042 # Install the custom kernel to the provided install root.
Mike Frysingerbaae8eb2013-01-15 01:34:26 -05001043 sudo -E PKGDIR="${tmp_pkgdir}" ${EMERGE_BOARD_CMD} --usepkgonly \
David James0ea96e42011-08-03 11:53:50 -07001044 --root=${install_root} ${kernel} || die "Cannot emerge kernel to root"
1045}
Brian Harringfeb04f72012-02-03 21:22:50 -08001046
Mike Frysinger6b1abb22012-05-11 13:44:06 -04001047enable_strict_sudo() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -05001048 if [[ -z ${CROS_SUDO_KEEP_ALIVE} ]]; then
Brian Harringfeb04f72012-02-03 21:22:50 -08001049 echo "$0 was somehow invoked in a way that the sudo keep alive could"
1050 echo "not be found. Failing due to this. See crosbug.com/18393."
1051 exit 126
1052 fi
Mike Frysingerbaae8eb2013-01-15 01:34:26 -05001053 sudo() {
1054 $(type -P sudo) -n "$@"
Brian Harringfeb04f72012-02-03 21:22:50 -08001055 }
1056}
Gilad Arnold207a7c72012-02-09 10:19:16 -08001057
David James855afb72012-03-14 20:04:59 -07001058# Display --help if requested. This is used to hide options from help
1059# that are not intended for developer use.
1060#
1061# How to use:
1062# 1) Declare the options that you want to appear in help.
1063# 2) Call this function.
1064# 3) Declare the options that you don't want to appear in help.
1065#
1066# See build_packages for example usage.
Mike Frysinger6b1abb22012-05-11 13:44:06 -04001067show_help_if_requested() {
Mike Frysingerbaae8eb2013-01-15 01:34:26 -05001068 local opt
David James855afb72012-03-14 20:04:59 -07001069 for opt in "$@"; do
Mike Frysingerbaae8eb2013-01-15 01:34:26 -05001070 if [[ ${opt} == "-h" || ${opt} == "--help" ]]; then
David James855afb72012-03-14 20:04:59 -07001071 flags_help
1072 exit 0
1073 fi
1074 done
1075}
Brian Harring7f175a52012-03-02 05:37:00 -08001076
Mike Frysinger6b1abb22012-05-11 13:44:06 -04001077switch_to_strict_mode() {
Brian Harring7f175a52012-03-02 05:37:00 -08001078 # Set up strict execution mode; note that the trap
1079 # must follow switch_to_strict_mode, else it will have no effect.
1080 set -e
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -04001081 trap 'die_err_trap' ERR
Mike Frysingerbaae8eb2013-01-15 01:34:26 -05001082 if [[ $# -ne 0 ]]; then
Brian Harring7f175a52012-03-02 05:37:00 -08001083 set "$@"
1084 fi
1085}
1086
1087# TODO: Re-enable this once shflags is set -e safe.
1088#switch_to_strict_mode