David McMahon | 4930294 | 2010-02-18 16:55:35 -0800 | [diff] [blame] | 1 | # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | # Common constants for build scripts |
| 6 | # This must evaluate properly for both /bin/bash and /bin/sh |
| 7 | |
| 8 | # All scripts should die on error unless commands are specifically excepted |
| 9 | # by prefixing with '!' or surrounded by 'set +e' / 'set -e'. |
| 10 | # TODO: Re-enable this once shflags is less prone to dying. |
| 11 | #set -e |
| 12 | |
| 13 | # The number of jobs to pass to tools that can run in parallel (such as make |
| 14 | # and dpkg-buildpackage |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 15 | NUM_JOBS=$(grep -c "^processor" /proc/cpuinfo) |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 16 | |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 17 | # True if we have the 'pv' utility - also set up COMMON_PV_CAT for convenience |
| 18 | COMMON_PV_OK=1 |
| 19 | COMMON_PV_CAT=pv |
| 20 | pv -V >/dev/null 2>&1 || COMMON_PV_OK=0 |
| 21 | if [ $COMMON_PV_OK -eq 0 ]; then |
| 22 | COMMON_PV_CAT=cat |
| 23 | fi |
| 24 | |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 25 | # Make sure we have the location and name of the calling script, using |
| 26 | # the current value if it is already set. |
| 27 | SCRIPT_LOCATION=${SCRIPT_LOCATION:-$(dirname "$(readlink -f "$0")")} |
| 28 | SCRIPT_NAME=${SCRIPT_NAME:-$(basename "$0")} |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 29 | |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 30 | # Detect whether we're inside a chroot or not |
| 31 | if [ -e /etc/debian_chroot ] |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 32 | then |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 33 | INSIDE_CHROOT=1 |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 34 | else |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 35 | INSIDE_CHROOT=0 |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 36 | fi |
| 37 | |
Mike Frysinger | 669b28b | 2012-02-07 18:01:00 -0500 | [diff] [blame] | 38 | # Determine and set up variables needed for fancy color output (if supported). |
| 39 | V_BOLD_RED= |
| 40 | V_BOLD_GREEN= |
| 41 | V_BOLD_YELLOW= |
| 42 | V_REVERSE= |
| 43 | V_VIDOFF= |
| 44 | |
| 45 | if tput colors >/dev/null 2>&1; then |
| 46 | # order matters: we want VIDOFF last so that when we trace with `set -x`, |
| 47 | # our terminal doesn't bleed colors as bash dumps the values of vars. |
| 48 | V_BOLD_RED="$(tput bold; tput setaf 1)" |
| 49 | V_BOLD_GREEN="$(tput bold; tput setaf 2)" |
| 50 | V_BOLD_YELLOW="$(tput bold; tput setaf 3)" |
| 51 | V_REVERSE="$(tput rev)" |
| 52 | V_VIDOFF="$(tput sgr0)" |
| 53 | fi |
| 54 | |
| 55 | # Declare these asap so that code below can safely assume they exist. |
| 56 | function info { |
| 57 | echo -e >&2 "${V_BOLD_GREEN}INFO ${CROS_LOG_PREFIX:-""}: $@${V_VIDOFF}" |
| 58 | } |
| 59 | |
| 60 | function warn { |
| 61 | echo -e >&2 "${V_BOLD_YELLOW}WARNING ${CROS_LOG_PREFIX:-""}: $@${V_VIDOFF}" |
| 62 | } |
| 63 | |
| 64 | function error { |
| 65 | echo -e >&2 "${V_BOLD_RED}ERROR ${CROS_LOG_PREFIX:-""}: $@${V_VIDOFF}" |
| 66 | } |
| 67 | |
| 68 | function die { |
| 69 | error "$@" |
| 70 | exit 1 |
| 71 | } |
| 72 | |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 73 | # Construct a list of possible locations for the source tree. This list is |
| 74 | # based on various environment variables and globals that may have been set |
| 75 | # by the calling script. |
| 76 | function get_gclient_root_list() { |
| 77 | if [ $INSIDE_CHROOT -eq 1 ]; then |
| 78 | echo "/home/${USER}/trunk" |
| 79 | |
| 80 | if [ -n "${SUDO_USER}" ]; then echo "/home/${SUDO_USER}/trunk"; fi |
| 81 | fi |
| 82 | |
| 83 | if [ -n "${COMMON_SH}" ]; then echo "$(dirname "$COMMON_SH")/../.."; fi |
| 84 | if [ -n "${BASH_SOURCE}" ]; then echo "$(dirname "$BASH_SOURCE")/../.."; fi |
| 85 | } |
| 86 | |
| 87 | # Based on the list of possible source locations we set GCLIENT_ROOT if it is |
| 88 | # not already defined by looking for a src directory in each seach path |
| 89 | # location. If we do not find a valid looking root we error out. |
| 90 | function get_gclient_root() { |
| 91 | if [ -n "${GCLIENT_ROOT}" ]; then |
| 92 | return |
| 93 | fi |
| 94 | |
| 95 | for path in $(get_gclient_root_list); do |
| 96 | if [ -d "${path}/src" ]; then |
| 97 | GCLIENT_ROOT=${path} |
| 98 | break |
| 99 | fi |
| 100 | done |
| 101 | |
| 102 | if [ -z "${GCLIENT_ROOT}" ]; then |
| 103 | # Using dash or sh, we don't know where we are. $0 refers to the calling |
| 104 | # script, not ourselves, so that doesn't help us. |
| 105 | echo "Unable to determine location for common.sh. If you are sourcing" |
| 106 | echo "common.sh from a script run via dash or sh, you must do it in the" |
| 107 | echo "following way:" |
| 108 | echo ' COMMON_SH="$(dirname "$0")/../../scripts/common.sh"' |
| 109 | echo ' . "$COMMON_SH"' |
| 110 | echo "where the first line is the relative path from your script to" |
| 111 | echo "common.sh." |
| 112 | exit 1 |
| 113 | fi |
| 114 | } |
| 115 | |
| 116 | # Find root of source tree |
| 117 | get_gclient_root |
| 118 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 119 | # Canonicalize the directories for the root dir and the calling script. |
| 120 | # readlink is part of coreutils and should be present even in a bare chroot. |
tedbo | 4f44d9e | 2010-01-08 17:26:11 -0800 | [diff] [blame] | 121 | # This is better than just using |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 122 | # FOO = "$(cd $FOO ; pwd)" |
tedbo | 4f44d9e | 2010-01-08 17:26:11 -0800 | [diff] [blame] | 123 | # since that leaves symbolic links intact. |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 124 | # Note that 'realpath' is equivalent to 'readlink -f'. |
Mike Frysinger | a1a06ab | 2011-08-10 11:40:30 -0400 | [diff] [blame] | 125 | SCRIPT_LOCATION=$(readlink -f "$SCRIPT_LOCATION") |
| 126 | GCLIENT_ROOT=$(readlink -f "$GCLIENT_ROOT") |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 127 | |
| 128 | # Other directories should always be pathed down from GCLIENT_ROOT. |
| 129 | SRC_ROOT="$GCLIENT_ROOT/src" |
| 130 | SRC_INTERNAL="$GCLIENT_ROOT/src-internal" |
| 131 | SCRIPTS_DIR="$SRC_ROOT/scripts" |
| 132 | |
| 133 | # Load developer's custom settings. Default location is in scripts dir, |
| 134 | # since that's available both inside and outside the chroot. By convention, |
| 135 | # settings from this file are variables starting with 'CHROMEOS_' |
| 136 | CHROMEOS_DEV_SETTINGS="${CHROMEOS_DEV_SETTINGS:-$SCRIPTS_DIR/.chromeos_dev}" |
Mike Frysinger | a1a06ab | 2011-08-10 11:40:30 -0400 | [diff] [blame] | 137 | if [ -f "$CHROMEOS_DEV_SETTINGS" ]; then |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 138 | # Turn on exit-on-error during custom settings processing |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 139 | SAVE_OPTS=$(set +o) |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 140 | set -e |
| 141 | |
| 142 | # Read settings |
Mike Frysinger | a1a06ab | 2011-08-10 11:40:30 -0400 | [diff] [blame] | 143 | . "$CHROMEOS_DEV_SETTINGS" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 144 | |
| 145 | # Restore previous state of exit-on-error |
| 146 | eval "$SAVE_OPTS" |
| 147 | fi |
| 148 | |
| 149 | # Load shflags |
Zdenek Behan | 07d2422 | 2011-11-02 00:46:25 +0000 | [diff] [blame] | 150 | # NOTE: This code snippet is in particular used by the au-generator (which |
| 151 | # stores shflags in ./lib/shflags/) and should not be touched. |
| 152 | if [ -f "${SCRIPTS_DIR}/lib/shflags/shflags" ]; then |
Mike Frysinger | 77c674b | 2012-02-07 18:05:07 -0500 | [diff] [blame] | 153 | . "${SCRIPTS_DIR}/lib/shflags/shflags" || die "Couldn't find shflags" |
Zdenek Behan | 07d2422 | 2011-11-02 00:46:25 +0000 | [diff] [blame] | 154 | else |
| 155 | . ./lib/shflags/shflags || die "Couldn't find shflags" |
| 156 | fi |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 157 | |
Bill Richardson | 10d27c2 | 2010-01-20 13:38:50 -0800 | [diff] [blame] | 158 | # Our local mirror |
| 159 | DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"} |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 160 | |
Bill Richardson | 10d27c2 | 2010-01-20 13:38:50 -0800 | [diff] [blame] | 161 | # Upstream mirrors and build suites come in 2 flavors |
| 162 | # DEV - development chroot, used to build the chromeos image |
| 163 | # IMG - bootable image, to run on actual hardware |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 164 | |
Bill Richardson | 10d27c2 | 2010-01-20 13:38:50 -0800 | [diff] [blame] | 165 | DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"} |
| 166 | DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"} |
| 167 | |
| 168 | DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"} |
| 169 | DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"} |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 170 | |
| 171 | # Default location for chroot |
| 172 | DEFAULT_CHROOT_DIR=${CHROMEOS_CHROOT_DIR:-"$GCLIENT_ROOT/chroot"} |
| 173 | |
| 174 | # All output files from build should go under $DEFAULT_BUILD_ROOT, so that |
| 175 | # they don't pollute the source directory. |
| 176 | DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"$SRC_ROOT/build"} |
| 177 | |
David McMahon | 4930294 | 2010-02-18 16:55:35 -0800 | [diff] [blame] | 178 | # Set up a global ALL_BOARDS value |
Mike Frysinger | a1a06ab | 2011-08-10 11:40:30 -0400 | [diff] [blame] | 179 | if [ -d "$SRC_ROOT/overlays" ]; then |
| 180 | ALL_BOARDS=$(cd "$SRC_ROOT/overlays"; \ |
| 181 | ls -1d overlay-* 2>&- | sed 's,overlay-,,g') |
David Rochberg | 3b91070 | 2010-12-02 10:45:21 -0500 | [diff] [blame] | 182 | fi |
David McMahon | 4930294 | 2010-02-18 16:55:35 -0800 | [diff] [blame] | 183 | # Strip CR |
| 184 | ALL_BOARDS=$(echo $ALL_BOARDS) |
| 185 | # Set a default BOARD |
| 186 | #DEFAULT_BOARD=x86-generic # or... |
| 187 | DEFAULT_BOARD=$(echo $ALL_BOARDS | awk '{print $NF}') |
| 188 | |
David James | ff07201 | 2010-11-30 13:22:05 -0800 | [diff] [blame] | 189 | # Enable --fast by default. |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 190 | DEFAULT_FAST=${FLAGS_TRUE} |
David James | 0366864 | 2010-07-28 17:08:29 -0700 | [diff] [blame] | 191 | |
Chris Sosa | b0f5732 | 2011-10-25 03:07:23 +0000 | [diff] [blame] | 192 | # Directory to store built images. Should be set by sourcing script when used. |
| 193 | BUILD_DIR= |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 194 | |
| 195 | # Standard filenames |
Chris Sosa | b0f5732 | 2011-10-25 03:07:23 +0000 | [diff] [blame] | 196 | CHROMEOS_BASE_IMAGE_NAME="chromiumos_base_image.bin" |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 197 | CHROMEOS_IMAGE_NAME="chromiumos_image.bin" |
Chris Sosa | b0f5732 | 2011-10-25 03:07:23 +0000 | [diff] [blame] | 198 | CHROMEOS_DEVELOPER_IMAGE_NAME="chromiumos_image.bin" |
Gilad Arnold | 0836627 | 2012-02-08 10:46:26 -0800 | [diff] [blame] | 199 | CHROMEOS_RECOVERY_IMAGE_NAME="recovery_image.bin" |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 200 | CHROMEOS_TEST_IMAGE_NAME="chromiumos_test_image.bin" |
Chris Sosa | b885b80 | 2011-02-16 15:33:11 -0800 | [diff] [blame] | 201 | CHROMEOS_FACTORY_TEST_IMAGE_NAME="chromiumos_factory_image.bin" |
Chris Sosa | b0f5732 | 2011-10-25 03:07:23 +0000 | [diff] [blame] | 202 | CHROMEOS_FACTORY_INSTALL_SHIM_NAME="factory_install_shim.bin" |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 203 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 204 | # Directory locations inside the dev chroot |
| 205 | CHROOT_TRUNK_DIR="/home/$USER/trunk" |
| 206 | |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 207 | # Install make for portage ebuilds. Used by build_image and gmergefs. |
Chris Masone | d11ce17 | 2010-11-09 14:22:08 -0800 | [diff] [blame] | 208 | # TODO: Is /usr/local/autotest-chrome still used by anyone? |
Hung-Te Lin | d32c59f | 2012-01-19 19:54:01 +0800 | [diff] [blame] | 209 | COMMON_INSTALL_MASK=" |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 210 | *.a |
| 211 | *.la |
| 212 | /etc/init.d |
| 213 | /etc/runlevels |
| 214 | /lib/rc |
| 215 | /usr/bin/Xnest |
| 216 | /usr/bin/Xvfb |
| 217 | /usr/include |
| 218 | /usr/lib/debug |
| 219 | /usr/lib/gcc |
| 220 | /usr/lib/gtk-2.0/include |
| 221 | /usr/lib/pkgconfig |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 222 | /usr/local/autotest-chrome |
| 223 | /usr/man |
| 224 | /usr/share/aclocal |
| 225 | /usr/share/doc |
| 226 | /usr/share/gettext |
| 227 | /usr/share/gtk-2.0 |
| 228 | /usr/share/gtk-doc |
| 229 | /usr/share/info |
| 230 | /usr/share/man |
| 231 | /usr/share/openrc |
| 232 | /usr/share/pkgconfig |
| 233 | /usr/share/readline |
Chris Wolfe | d13775f | 2011-07-26 16:34:38 -0400 | [diff] [blame] | 234 | /usr/src |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 235 | " |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 236 | |
Hung-Te Lin | d32c59f | 2012-01-19 19:54:01 +0800 | [diff] [blame] | 237 | # Mask for base, dev, and test images (build_image, build_image --test) |
| 238 | DEFAULT_INSTALL_MASK=" |
| 239 | $COMMON_INSTALL_MASK |
| 240 | /usr/local/autotest |
| 241 | " |
| 242 | |
| 243 | # Mask for factory test image (build_image --factory) |
| 244 | FACTORY_TEST_INSTALL_MASK=" |
| 245 | $COMMON_INSTALL_MASK |
| 246 | */.svn |
| 247 | */CVS |
| 248 | /usr/local/autotest/[^c]* |
| 249 | /usr/local/autotest/conmux |
| 250 | /usr/local/autotest/client/deps/chrome_test |
| 251 | /usr/local/autotest/client/deps/piglit |
| 252 | /usr/local/autotest/client/deps/pyauto_dep |
| 253 | /usr/local/autotest/client/deps/realtimecomm_* |
| 254 | /usr/local/autotest/client/site_tests/desktopui_PageCyclerTests |
| 255 | /usr/local/autotest/client/site_tests/graphics_WebGLConformance |
| 256 | /usr/local/autotest/client/site_tests/platform_ToolchainOptions |
| 257 | /usr/local/autotest/client/site_tests/realtimecomm_GTalk* |
| 258 | " |
| 259 | |
| 260 | # Mask for factory install shim (build_image --factory_install) |
| 261 | FACTORY_SHIM_INSTALL_MASK=" |
| 262 | $DEFAULT_INSTALL_MASK |
| 263 | /opt/[^g]* |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 264 | /opt/google/chrome |
| 265 | /opt/google/o3d |
| 266 | /opt/google/talkplugin |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 267 | /usr/lib/dri |
| 268 | /usr/lib/python2.6/test |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 269 | /usr/local/autotest-pkgs |
| 270 | /usr/share/X11 |
| 271 | /usr/share/chewing |
| 272 | /usr/share/fonts |
| 273 | /usr/share/ibus-pinyin |
| 274 | /usr/share/libhangul |
| 275 | /usr/share/locale |
| 276 | /usr/share/m17n |
| 277 | /usr/share/mime |
| 278 | /usr/share/sounds |
| 279 | /usr/share/tts |
| 280 | /usr/share/zoneinfo |
| 281 | " |
Tom Wai-Hong Tam | f87a367 | 2010-05-17 16:06:33 +0800 | [diff] [blame] | 282 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 283 | # ----------------------------------------------------------------------------- |
| 284 | # Functions |
| 285 | |
Chris Sosa | acada73 | 2010-02-23 15:20:03 -0800 | [diff] [blame] | 286 | function setup_board_warning { |
tedbo | 373c390 | 2010-04-12 10:52:40 -0700 | [diff] [blame] | 287 | echo |
| 288 | echo "$V_REVERSE================= WARNING ======================$V_VIDOFF" |
Chris Sosa | acada73 | 2010-02-23 15:20:03 -0800 | [diff] [blame] | 289 | echo |
| 290 | echo "*** No default board detected in " \ |
| 291 | "$GCLIENT_ROOT/src/scripts/.default_board" |
| 292 | echo "*** Either run setup_board with default flag set" |
| 293 | echo "*** or echo |board_name| > $GCLIENT_ROOT/src/scripts/.default_board" |
| 294 | echo |
| 295 | } |
| 296 | |
| 297 | |
| 298 | # Sets the default board variable for calling script |
| 299 | function get_default_board { |
tedbo | 373c390 | 2010-04-12 10:52:40 -0700 | [diff] [blame] | 300 | DEFAULT_BOARD= |
| 301 | |
Chris Sosa | acada73 | 2010-02-23 15:20:03 -0800 | [diff] [blame] | 302 | if [ -f "$GCLIENT_ROOT/src/scripts/.default_board" ] ; then |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 303 | DEFAULT_BOARD=$(cat "$GCLIENT_ROOT/src/scripts/.default_board") |
Mike Frysinger | bc36d04 | 2011-12-19 16:01:09 -0500 | [diff] [blame] | 304 | # Check for user typos like whitespace. |
| 305 | if [[ -n ${DEFAULT_BOARD//[a-zA-Z0-9-_]} ]] ; then |
| 306 | die ".default_board: invalid name detected; please fix:" \ |
| 307 | "'${DEFAULT_BOARD}'" |
| 308 | fi |
Chris Sosa | acada73 | 2010-02-23 15:20:03 -0800 | [diff] [blame] | 309 | fi |
| 310 | } |
| 311 | |
| 312 | |
Don Garrett | 640a058 | 2010-05-04 16:54:28 -0700 | [diff] [blame] | 313 | # Enter a chroot and restart the current script if needed |
| 314 | function restart_in_chroot_if_needed { |
David Rochberg | 3b91070 | 2010-12-02 10:45:21 -0500 | [diff] [blame] | 315 | # NB: Pass in ARGV: restart_in_chroot_if_needed "$@" |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 316 | if [ $INSIDE_CHROOT -ne 1 ]; then |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 317 | # Get inside_chroot path for script. |
| 318 | local chroot_path="$(reinterpret_path_for_chroot "$0")" |
Zdenek Behan | 2811c16 | 2011-08-13 00:47:38 +0200 | [diff] [blame] | 319 | exec $GCLIENT_ROOT/chromite/bin/cros_sdk -- "$chroot_path" "$@" |
Don Garrett | 640a058 | 2010-05-04 16:54:28 -0700 | [diff] [blame] | 320 | fi |
| 321 | } |
| 322 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 323 | # Fail unless we're inside the chroot. This guards against messing up your |
| 324 | # workstation. |
| 325 | function assert_inside_chroot { |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 326 | if [ $INSIDE_CHROOT -ne 1 ]; then |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 327 | echo "This script must be run inside the chroot. Run this first:" |
Zdenek Behan | 2811c16 | 2011-08-13 00:47:38 +0200 | [diff] [blame] | 328 | echo " cros_sdk" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 329 | exit 1 |
| 330 | fi |
| 331 | } |
| 332 | |
| 333 | # Fail if we're inside the chroot. This guards against creating or entering |
| 334 | # nested chroots, among other potential problems. |
| 335 | function assert_outside_chroot { |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 336 | if [ $INSIDE_CHROOT -ne 0 ]; then |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 337 | echo "This script must be run outside the chroot." |
| 338 | exit 1 |
| 339 | fi |
| 340 | } |
| 341 | |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 342 | function assert_not_root_user { |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 343 | if [ $(id -u) = 0 ]; then |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 344 | echo "This script must be run as a non-root user." |
| 345 | exit 1 |
| 346 | fi |
| 347 | } |
| 348 | |
Luigi Semenzato | 1f82e12 | 2010-03-23 12:43:08 -0700 | [diff] [blame] | 349 | # Check that all arguments are flags; that is, there are no remaining arguments |
| 350 | # after parsing from shflags. Allow (with a warning) a single empty-string |
| 351 | # argument. |
| 352 | # |
| 353 | # TODO: fix buildbot so that it doesn't pass the empty-string parameter, |
| 354 | # then change this function. |
| 355 | # |
| 356 | # Usage: check_flags_only_and_allow_null_arg "$@" && set -- |
| 357 | function check_flags_only_and_allow_null_arg { |
| 358 | do_shift=1 |
| 359 | if [[ $# == 1 && -z "$@" ]]; then |
| 360 | echo "$0: warning: ignoring null argument" >&2 |
| 361 | shift |
| 362 | do_shift=0 |
| 363 | fi |
| 364 | if [[ $# -gt 0 ]]; then |
| 365 | echo "error: invalid arguments: \"$@\"" >&2 |
| 366 | flags_help |
| 367 | exit 1 |
| 368 | fi |
| 369 | return $do_shift |
| 370 | } |
| 371 | |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 372 | # Retry an emerge command according to $FLAGS_retries |
David James | fd7403a | 2010-07-09 12:00:17 -0700 | [diff] [blame] | 373 | # The $EMERGE_JOBS flags will only be added the first time the command is run |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 374 | function eretry () { |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 375 | local i |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 376 | for i in $(seq $FLAGS_retries); do |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 377 | echo "Retrying $@" |
| 378 | "$@" $EMERGE_JOBS && return 0 |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 379 | done |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 380 | "$@" && return 0 |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 381 | return 1 |
| 382 | } |
| 383 | |
| 384 | # Removes single quotes around parameter |
| 385 | # Arguments: |
| 386 | # $1 - string which optionally has surrounding quotes |
| 387 | # Returns: |
| 388 | # None, but prints the string without quotes. |
| 389 | function remove_quotes() { |
| 390 | echo "$1" | sed -e "s/^'//; s/'$//" |
| 391 | } |
tedbo | 373c390 | 2010-04-12 10:52:40 -0700 | [diff] [blame] | 392 | |
| 393 | # Writes stdin to the given file name as root using sudo in overwrite mode. |
| 394 | # |
| 395 | # $1 - The output file name. |
| 396 | function sudo_clobber() { |
| 397 | sudo tee "$1" > /dev/null |
| 398 | } |
| 399 | |
| 400 | # Writes stdin to the given file name as root using sudo in append mode. |
| 401 | # |
| 402 | # $1 - The output file name. |
| 403 | function sudo_append() { |
| 404 | sudo tee -a "$1" > /dev/null |
| 405 | } |
robotboy | 9891221 | 2010-04-12 14:08:14 -0700 | [diff] [blame] | 406 | |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 407 | # Execute multiple commands in a single sudo. Generally will speed things |
| 408 | # up by avoiding multiple calls to `sudo`. If any commands fail, we will |
| 409 | # call die with the failing command. We can handle a max of ~100 commands, |
| 410 | # but hopefully no one will ever try that many at once. |
| 411 | # |
| 412 | # $@ - The commands to execute, one per arg. |
| 413 | function sudo_multi() { |
| 414 | local i cmds |
| 415 | |
| 416 | # Construct the shell code to execute. It'll be of the form: |
| 417 | # ... && ( ( command ) || exit <command index> ) && ... |
| 418 | # This way we know which command exited. The exit status of |
| 419 | # the underlying command is lost, but we never cared about it |
| 420 | # in the first place (other than it is non zero), so oh well. |
| 421 | for (( i = 1; i <= $#; ++i )); do |
| 422 | cmds+=" && ( ( ${!i} ) || exit $(( i + 10 )) )" |
| 423 | done |
| 424 | |
| 425 | # Execute our constructed shell code. |
| 426 | sudo -- sh -c ":${cmds[*]}" && i=0 || i=$? |
| 427 | |
| 428 | # See if this failed, and if so, print out the failing command. |
| 429 | if [[ $i -gt 10 ]]; then |
| 430 | : $(( i -= 10 )) |
| 431 | die "sudo_multi failed: ${!i}" |
| 432 | elif [[ $i -ne 0 ]]; then |
| 433 | die "sudo_multi failed for unknown reason $i" |
| 434 | fi |
| 435 | } |
| 436 | |
Mike Frysinger | 1aa6124 | 2011-09-15 17:46:44 -0400 | [diff] [blame] | 437 | # Locate all mounts below a specified directory. |
| 438 | # |
| 439 | # $1 - The root tree. |
| 440 | function sub_mounts() { |
| 441 | # Assume that `mount` outputs a list of mount points in the order |
| 442 | # that things were mounted (since it always has and hopefully always |
| 443 | # will). As such, we have to unmount in reverse order to cleanly |
| 444 | # unmount submounts (think /dev/pts and /dev). |
| 445 | mount | \ |
Mike Frysinger | b2e897b | 2011-09-22 17:04:35 -0400 | [diff] [blame] | 446 | awk -v path="$1" -v len="${#1}" \ |
| 447 | '(substr($3, 1, len) == path) { print $3 }' | \ |
Mike Frysinger | 1aa6124 | 2011-09-15 17:46:44 -0400 | [diff] [blame] | 448 | tac |
| 449 | } |
| 450 | |
robotboy | 9891221 | 2010-04-12 14:08:14 -0700 | [diff] [blame] | 451 | # Unmounts a directory, if the unmount fails, warn, and then lazily unmount. |
| 452 | # |
| 453 | # $1 - The path to unmount. |
Mike Frysinger | 1aa6124 | 2011-09-15 17:46:44 -0400 | [diff] [blame] | 454 | function safe_umount_tree { |
| 455 | local mounts=$(sub_mounts "$1") |
robotboy | 9891221 | 2010-04-12 14:08:14 -0700 | [diff] [blame] | 456 | |
Mike Frysinger | e8aec37 | 2011-09-21 00:03:22 -0400 | [diff] [blame] | 457 | # Hmm, this shouldn't normally happen, but anything is possible. |
| 458 | if [ -z "${mounts}" ] ; then |
| 459 | return 0 |
| 460 | fi |
| 461 | |
Mike Frysinger | 1aa6124 | 2011-09-15 17:46:44 -0400 | [diff] [blame] | 462 | # First try to unmount in one shot to speed things up. |
| 463 | if sudo umount -d ${mounts}; then |
| 464 | return 0 |
| 465 | fi |
robotboy | 9891221 | 2010-04-12 14:08:14 -0700 | [diff] [blame] | 466 | |
Mike Frysinger | 1aa6124 | 2011-09-15 17:46:44 -0400 | [diff] [blame] | 467 | # Well that didn't work, so lazy unmount remaining ones. |
| 468 | mounts=$(sub_mounts "$1") |
| 469 | warn "Failed to unmount ${mounts}" |
| 470 | warn "Doing a lazy unmount" |
| 471 | if ! sudo umount -d -l ${mounts}; then |
| 472 | mounts=$(sub_mounts "$1") |
| 473 | die "Failed to lazily unmount ${mounts}" |
robotboy | 9891221 | 2010-04-12 14:08:14 -0700 | [diff] [blame] | 474 | fi |
| 475 | } |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 476 | |
Chris Sosa | d445502 | 2010-05-20 10:14:06 -0700 | [diff] [blame] | 477 | # Fixes symlinks that are incorrectly prefixed with the build root ${1} |
| 478 | # rather than the real running root '/'. |
| 479 | # TODO(sosa) - Merge setup - cleanup below with this method. |
| 480 | fix_broken_symlinks() { |
| 481 | local build_root="${1}" |
| 482 | local symlinks=$(find "${build_root}/usr/local" -lname "${build_root}/*") |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 483 | local symlink |
Chris Sosa | d445502 | 2010-05-20 10:14:06 -0700 | [diff] [blame] | 484 | for symlink in ${symlinks}; do |
| 485 | echo "Fixing ${symlink}" |
| 486 | local target=$(ls -l "${symlink}" | cut -f 2 -d '>') |
| 487 | # Trim spaces from target (bashism). |
| 488 | target=${target/ /} |
| 489 | # Make new target (removes rootfs prefix). |
| 490 | new_target=$(echo ${target} | sed "s#${build_root}##") |
| 491 | |
| 492 | echo "Fixing symlink ${symlink}" |
| 493 | sudo unlink "${symlink}" |
| 494 | sudo ln -sf "${new_target}" "${symlink}" |
| 495 | done |
| 496 | } |
| 497 | |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 498 | # Sets up symlinks for the developer root. It is necessary to symlink |
| 499 | # usr and local since the developer root is mounted at /usr/local and |
| 500 | # applications expect to be installed under /usr/local/bin, etc. |
| 501 | # This avoids packages installing into /usr/local/usr/local/bin. |
| 502 | # ${1} specifies the symlink target for the developer root. |
| 503 | # ${2} specifies the symlink target for the var directory. |
| 504 | # ${3} specifies the location of the stateful partition. |
| 505 | setup_symlinks_on_root() { |
| 506 | # Give args better names. |
| 507 | local dev_image_target=${1} |
| 508 | local var_target=${2} |
| 509 | local dev_image_root="${3}/dev_image" |
| 510 | |
| 511 | # If our var target is actually the standard var, we are cleaning up the |
| 512 | # symlinks (could also check for /usr/local for the dev_image_target). |
| 513 | if [ ${var_target} = "/var" ]; then |
| 514 | echo "Cleaning up /usr/local symlinks for ${dev_image_root}" |
| 515 | else |
| 516 | echo "Setting up symlinks for /usr/local for ${dev_image_root}" |
| 517 | fi |
| 518 | |
| 519 | # Set up symlinks that should point to ${dev_image_target}. |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 520 | local path |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 521 | for path in usr local; do |
| 522 | if [ -h "${dev_image_root}/${path}" ]; then |
| 523 | sudo unlink "${dev_image_root}/${path}" |
| 524 | elif [ -e "${dev_image_root}/${path}" ]; then |
| 525 | die "${dev_image_root}/${path} should be a symlink if exists" |
| 526 | fi |
Mike Frysinger | a1a06ab | 2011-08-10 11:40:30 -0400 | [diff] [blame] | 527 | sudo ln -s "${dev_image_target}" "${dev_image_root}/${path}" |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 528 | done |
| 529 | |
| 530 | # Setup var symlink. |
| 531 | if [ -h "${dev_image_root}/var" ]; then |
| 532 | sudo unlink "${dev_image_root}/var" |
| 533 | elif [ -e "${dev_image_root}/var" ]; then |
| 534 | die "${dev_image_root}/var should be a symlink if it exists" |
| 535 | fi |
| 536 | |
| 537 | sudo ln -s "${var_target}" "${dev_image_root}/var" |
| 538 | } |
Nick Sanders | d250927 | 2010-06-16 03:50:04 -0700 | [diff] [blame] | 539 | |
Will Drewry | 55b42c9 | 2010-10-20 15:44:11 -0500 | [diff] [blame] | 540 | # These two helpers clobber the ro compat value in our root filesystem. |
| 541 | # |
| 542 | # When the system is built with --enable_rootfs_verification, bit-precise |
| 543 | # integrity checking is performed. That precision poses a usability issue on |
| 544 | # systems that automount partitions with recognizable filesystems, such as |
| 545 | # ext2/3/4. When the filesystem is mounted 'rw', ext2 metadata will be |
| 546 | # automatically updated even if no other writes are performed to the |
| 547 | # filesystem. In addition, ext2+ does not support a "read-only" flag for a |
| 548 | # given filesystem. That said, forward and backward compatibility of |
| 549 | # filesystem features are supported by tracking if a new feature breaks r/w or |
| 550 | # just write compatibility. We abuse the read-only compatibility flag[1] in |
| 551 | # the filesystem header by setting the high order byte (le) to FF. This tells |
| 552 | # the kernel that features R24-R31 are all enabled. Since those features are |
| 553 | # undefined on all ext-based filesystem, all standard kernels will refuse to |
| 554 | # mount the filesystem as read-write -- only read-only[2]. |
| 555 | # |
| 556 | # [1] 32-bit flag we are modifying: |
| 557 | # http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=include/linux/ext2_fs.h#l417 |
| 558 | # [2] Mount behavior is enforced here: |
| 559 | # http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=fs/ext2/super.c#l857 |
| 560 | # |
| 561 | # N.B., if the high order feature bits are used in the future, we will need to |
| 562 | # revisit this technique. |
| 563 | disable_rw_mount() { |
| 564 | local rootfs="$1" |
| 565 | local offset="${2-0}" # in bytes |
Will Drewry | 9b7cb51 | 2010-10-20 18:11:24 -0500 | [diff] [blame] | 566 | local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte |
| 567 | printf '\377' | |
Will Drewry | 55b42c9 | 2010-10-20 15:44:11 -0500 | [diff] [blame] | 568 | sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \ |
| 569 | conv=notrunc count=1 bs=1 |
| 570 | } |
| 571 | |
| 572 | enable_rw_mount() { |
| 573 | local rootfs="$1" |
| 574 | local offset="${2-0}" |
Will Drewry | 9b7cb51 | 2010-10-20 18:11:24 -0500 | [diff] [blame] | 575 | local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte |
| 576 | printf '\000' | |
Will Drewry | 55b42c9 | 2010-10-20 15:44:11 -0500 | [diff] [blame] | 577 | sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \ |
| 578 | conv=notrunc count=1 bs=1 |
| 579 | } |
| 580 | |
Nick Sanders | d250927 | 2010-06-16 03:50:04 -0700 | [diff] [blame] | 581 | # Get current timestamp. Assumes common.sh runs at startup. |
| 582 | start_time=$(date +%s) |
| 583 | |
| 584 | # Print time elsapsed since start_time. |
| 585 | print_time_elapsed() { |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 586 | local end_time=$(date +%s) |
| 587 | local elapsed_seconds=$(($end_time - $start_time)) |
| 588 | local minutes=$(($elapsed_seconds / 60)) |
| 589 | local seconds=$(($elapsed_seconds % 60)) |
Olof Johansson | 6d49138 | 2010-08-09 16:05:50 -0500 | [diff] [blame] | 590 | echo "Elapsed time: ${minutes}m${seconds}s" |
Nick Sanders | d250927 | 2010-06-16 03:50:04 -0700 | [diff] [blame] | 591 | } |
Doug Anderson | 0c9e88d | 2010-10-19 14:49:39 -0700 | [diff] [blame] | 592 | |
Anton Staaf | 9bcd841 | 2011-01-24 15:27:14 -0800 | [diff] [blame] | 593 | # The board and variant command line options can be used in a number of ways |
| 594 | # to specify the board and variant. The board can encode both pieces of |
| 595 | # information separated by underscores. Or the variant can be passed using |
| 596 | # the separate variant option. This function extracts the canonical board and |
| 597 | # variant information and provides it in the BOARD, VARIANT and BOARD_VARIANT |
| 598 | # variables. |
| 599 | get_board_and_variant() { |
| 600 | local flags_board="${1}" |
| 601 | local flags_variant="${2}" |
| 602 | |
| 603 | BOARD=$(echo "$flags_board" | cut -d '_' -f 1) |
| 604 | VARIANT=${flags_variant:-$(echo "$flags_board" | cut -s -d '_' -f 2)} |
| 605 | |
| 606 | if [ -n "$VARIANT" ]; then |
| 607 | BOARD_VARIANT="${BOARD}_${VARIANT}" |
| 608 | else |
| 609 | BOARD_VARIANT="${BOARD}" |
| 610 | fi |
| 611 | } |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 612 | |
| 613 | # This function converts a chromiumos image into a test image, either |
| 614 | # in place or by copying to a new test image filename first. It honors |
| 615 | # the following flags (see mod_image_for_test.sh) |
| 616 | # |
| 617 | # --factory |
| 618 | # --factory_install |
| 619 | # --force_copy |
| 620 | # |
| 621 | # On entry, pass the directory containing the image, and the image filename |
| 622 | # On exit, it puts the pathname of the resulting test image into |
| 623 | # CHROMEOS_RETURN_VAL |
| 624 | # (yes this is ugly, but perhaps less ugly than the alternatives) |
| 625 | # |
| 626 | # Usage: |
| 627 | # SRC_IMAGE=$(prepare_test_image "directory" "imagefile") |
| 628 | prepare_test_image() { |
| 629 | # If we're asked to modify the image for test, then let's make a copy and |
| 630 | # modify that instead. |
| 631 | # Check for manufacturing image. |
| 632 | local args |
| 633 | |
| 634 | if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then |
| 635 | args="--factory" |
| 636 | fi |
| 637 | |
| 638 | # Check for install shim. |
| 639 | if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]; then |
| 640 | args="--factory_install" |
| 641 | fi |
| 642 | |
| 643 | # Check for forcing copy of image |
| 644 | if [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ]; then |
| 645 | args="${args} --force_copy" |
| 646 | fi |
| 647 | |
| 648 | # Modify the image for test, creating a new test image |
| 649 | "${SCRIPTS_DIR}/mod_image_for_test.sh" --board=${FLAGS_board} \ |
| 650 | --image="$1/$2" --noinplace ${args} |
| 651 | |
| 652 | # From now on we use the just-created test image |
Simon Glass | 6e448ae | 2011-03-03 11:20:54 -0800 | [diff] [blame] | 653 | if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then |
| 654 | CHROMEOS_RETURN_VAL="$1/${CHROMEOS_FACTORY_TEST_IMAGE_NAME}" |
| 655 | else |
| 656 | CHROMEOS_RETURN_VAL="$1/${CHROMEOS_TEST_IMAGE_NAME}" |
| 657 | fi |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 658 | } |
Anton Staaf | 6f5262d | 2011-03-02 09:35:54 -0800 | [diff] [blame] | 659 | |
| 660 | # Check that the specified file exists. If the file path is empty or the file |
| 661 | # doesn't exist on the filesystem generate useful error messages. Otherwise |
| 662 | # show the user the name and path of the file that will be used. The padding |
| 663 | # parameter can be used to tabulate multiple name:path pairs. For example: |
| 664 | # |
| 665 | # check_for_file "really long name" "...:" "file.foo" |
| 666 | # check_for_file "short name" ".........:" "another.bar" |
| 667 | # |
| 668 | # Results in the following output: |
| 669 | # |
| 670 | # Using really long name...: file.foo |
| 671 | # Using short name.........: another.bar |
| 672 | # |
| 673 | # If tabulation is not required then passing "" for padding generates the |
| 674 | # output "Using <name> <path>" |
| 675 | check_for_file() { |
| 676 | local name=$1 |
| 677 | local padding=$2 |
| 678 | local path=$3 |
| 679 | |
| 680 | if [ -z "${path}" ]; then |
| 681 | die "No ${name} file specified." |
| 682 | fi |
| 683 | |
| 684 | if [ ! -e "${path}" ]; then |
| 685 | die "No ${name} file found at: ${path}" |
| 686 | else |
| 687 | info "Using ${name}${padding} ${path}" |
| 688 | fi |
| 689 | } |
| 690 | |
| 691 | # Check that the specified tool exists. If it does not exist in the PATH |
| 692 | # generate a useful error message indicating how to install the ebuild |
| 693 | # that contains the required tool. |
| 694 | check_for_tool() { |
| 695 | local tool=$1 |
| 696 | local ebuild=$2 |
| 697 | |
| 698 | if ! which "${tool}" >/dev/null ; then |
| 699 | error "The ${tool} utility was not found in your path. Run the following" |
| 700 | error "command in your chroot to install it: sudo -E emerge ${ebuild}" |
| 701 | exit 1 |
| 702 | fi |
| 703 | } |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 704 | |
| 705 | # Reinterprets path from outside the chroot for use inside. |
| 706 | # Returns "" if "" given. |
| 707 | # $1 - The path to reinterpret. |
| 708 | function reinterpret_path_for_chroot() { |
| 709 | if [ $INSIDE_CHROOT -ne 1 ]; then |
| 710 | if [ -z "${1}" ]; then |
| 711 | echo "" |
| 712 | else |
| 713 | local path_abs_path=$(readlink -f "${1}") |
| 714 | local gclient_root_abs_path=$(readlink -f "${GCLIENT_ROOT}") |
| 715 | |
| 716 | # Strip the repository root from the path. |
| 717 | local relative_path=$(echo ${path_abs_path} \ |
Mike Frysinger | a1a06ab | 2011-08-10 11:40:30 -0400 | [diff] [blame] | 718 | | sed "s:${gclient_root_abs_path}/::") |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 719 | |
| 720 | if [ "${relative_path}" = "${path_abs_path}" ]; then |
| 721 | die "Error reinterpreting path. Path ${1} is not within source tree." |
| 722 | fi |
| 723 | |
| 724 | # Prepend the chroot repository path. |
| 725 | echo "/home/${USER}/trunk/${relative_path}" |
| 726 | fi |
| 727 | else |
| 728 | # Path is already inside the chroot :). |
| 729 | echo "${1}" |
| 730 | fi |
| 731 | } |
Gabe Black | 83d8b82 | 2011-08-01 17:50:09 -0700 | [diff] [blame] | 732 | |
David James | 0ea96e4 | 2011-08-03 11:53:50 -0700 | [diff] [blame] | 733 | function emerge_custom_kernel() { |
| 734 | local install_root="$1" |
| 735 | local root=${FLAGS_build_root}/${FLAGS_board} |
| 736 | local tmp_pkgdir=${root}/custom-packages |
| 737 | |
| 738 | # Clean up any leftover state in custom directories. |
Mike Frysinger | a1a06ab | 2011-08-10 11:40:30 -0400 | [diff] [blame] | 739 | sudo rm -rf "${tmp_pkgdir}" |
David James | 0ea96e4 | 2011-08-03 11:53:50 -0700 | [diff] [blame] | 740 | |
| 741 | # Update chromeos-initramfs to contain the latest binaries from the build |
| 742 | # tree. This is basically just packaging up already-built binaries from |
| 743 | # $root. We are careful not to muck with the existing prebuilts so that |
| 744 | # prebuilts can be uploaded in parallel. |
| 745 | # TODO(davidjames): Implement ABI deps so that chromeos-initramfs will be |
| 746 | # rebuilt automatically when its dependencies change. |
Mike Frysinger | a1a06ab | 2011-08-10 11:40:30 -0400 | [diff] [blame] | 747 | sudo -E PKGDIR="${tmp_pkgdir}" $EMERGE_BOARD_CMD -1 \ |
David James | 0ea96e4 | 2011-08-03 11:53:50 -0700 | [diff] [blame] | 748 | chromeos-base/chromeos-initramfs || die "Cannot emerge chromeos-initramfs" |
| 749 | |
| 750 | # Verify all dependencies of the kernel are installed. This should be a |
| 751 | # no-op, but it's good to check in case a developer didn't run |
| 752 | # build_packages. |
| 753 | local kernel=$(portageq-${FLAGS_board} expand_virtual ${root} virtual/kernel) |
Mike Frysinger | a1a06ab | 2011-08-10 11:40:30 -0400 | [diff] [blame] | 754 | sudo -E PKGDIR="${tmp_pkgdir}" $EMERGE_BOARD_CMD --onlydeps \ |
David James | 0ea96e4 | 2011-08-03 11:53:50 -0700 | [diff] [blame] | 755 | ${kernel} || die "Cannot emerge kernel dependencies" |
| 756 | |
| 757 | # Build the kernel. This uses the standard root so that we can pick up the |
| 758 | # initramfs from there. But we don't actually install the kernel to the |
| 759 | # standard root, because that'll muck up the kernel debug symbols there, |
| 760 | # which we want to upload in parallel. |
Mike Frysinger | a1a06ab | 2011-08-10 11:40:30 -0400 | [diff] [blame] | 761 | sudo -E PKGDIR="${tmp_pkgdir}" $EMERGE_BOARD_CMD --buildpkgonly \ |
David James | 0ea96e4 | 2011-08-03 11:53:50 -0700 | [diff] [blame] | 762 | ${kernel} || die "Cannot emerge kernel" |
| 763 | |
| 764 | # Install the custom kernel to the provided install root. |
Mike Frysinger | a1a06ab | 2011-08-10 11:40:30 -0400 | [diff] [blame] | 765 | sudo -E PKGDIR="${tmp_pkgdir}" $EMERGE_BOARD_CMD --usepkgonly \ |
David James | 0ea96e4 | 2011-08-03 11:53:50 -0700 | [diff] [blame] | 766 | --root=${install_root} ${kernel} || die "Cannot emerge kernel to root" |
| 767 | } |
Brian Harring | feb04f7 | 2012-02-03 21:22:50 -0800 | [diff] [blame] | 768 | |
| 769 | function enable_strict_sudo { |
| 770 | if [ -z "$CROS_SUDO_KEEP_ALIVE" ]; then |
| 771 | echo "$0 was somehow invoked in a way that the sudo keep alive could" |
| 772 | echo "not be found. Failing due to this. See crosbug.com/18393." |
| 773 | exit 126 |
| 774 | fi |
| 775 | function sudo { |
| 776 | `type -P sudo` -n "$@" |
| 777 | } |
| 778 | } |
Gilad Arnold | 207a7c7 | 2012-02-09 10:19:16 -0800 | [diff] [blame^] | 779 | |
| 780 | # Selection menu with a default option: this is similar to bash's select |
| 781 | # built-in, only that in case of an empty selection it'll return the default |
| 782 | # choice. Like select, it uses PS3 as the prompt. |
| 783 | # |
| 784 | # $1: name of variable to be assigned the selected value; it better not be of |
| 785 | # the form choose_foo to avoid conflict with local variables. |
| 786 | # $2: default value to return in case of an empty user entry. |
| 787 | # $3: value to return in case of an invalid choice. |
| 788 | # $...: options for selection. |
| 789 | # |
| 790 | # Usage example: |
| 791 | # |
| 792 | # PS3="Select one [1]: " |
| 793 | # choose reply "foo" "ERROR" "foo" "bar" "foobar" |
| 794 | # |
| 795 | # This will present the following menu and prompt: |
| 796 | # |
| 797 | # 1) foo |
| 798 | # 2) bar |
| 799 | # 3) foobar |
| 800 | # Select one [1]: |
| 801 | # |
| 802 | # The return value will be stored in a variable named 'reply'. If the input is |
| 803 | # 1, 2 or 3, the return value will be "foo", "bar" or "foobar", respectively. |
| 804 | # If it is empty (i.e. the user clicked Enter) it will be "foo". Anything else |
| 805 | # will return "ERROR". |
| 806 | function choose() { |
| 807 | typeset -i choose_i=1 |
| 808 | |
| 809 | # Retrieve output variable name and default return value. |
| 810 | local choose_reply=$1 |
| 811 | local choose_default="$2" |
| 812 | local choose_invalid="$3" |
| 813 | shift 3 |
| 814 | |
| 815 | # Select a return value |
| 816 | unset REPLY |
| 817 | if [ $# -gt 0 ]; then |
| 818 | # Actual options provided, present a menu and prompt for a choice. |
| 819 | local choose_opt |
| 820 | for choose_opt in "$@"; do |
| 821 | echo "$choose_i) $choose_opt" |
| 822 | choose_i=choose_i+1 |
| 823 | done |
| 824 | read -p "$PS3" |
| 825 | fi |
| 826 | # Filter out strings containing non-digits. |
| 827 | if [ "${REPLY}" != "${REPLY%%[!0-9]*}" ]; then |
| 828 | REPLY=0 |
| 829 | fi |
| 830 | choose_i="${REPLY}" |
| 831 | |
| 832 | if [ $choose_i -ge 1 -a $choose_i -le $# ]; then |
| 833 | # Valid choice, return the corresponding value. |
| 834 | eval ${choose_reply}="${!choose_i}" |
| 835 | elif [ -z "${REPLY}" ]; then |
| 836 | # Empty choice, return default value. |
| 837 | eval ${choose_reply}="${choose_default}" |
| 838 | else |
| 839 | # Invalid choice, return corresponding value. |
| 840 | eval ${choose_reply}="${choose_invalid}" |
| 841 | fi |
| 842 | } |