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 | |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 38 | # Construct a list of possible locations for the source tree. This list is |
| 39 | # based on various environment variables and globals that may have been set |
| 40 | # by the calling script. |
| 41 | function get_gclient_root_list() { |
| 42 | if [ $INSIDE_CHROOT -eq 1 ]; then |
| 43 | echo "/home/${USER}/trunk" |
| 44 | |
| 45 | if [ -n "${SUDO_USER}" ]; then echo "/home/${SUDO_USER}/trunk"; fi |
| 46 | fi |
| 47 | |
| 48 | if [ -n "${COMMON_SH}" ]; then echo "$(dirname "$COMMON_SH")/../.."; fi |
| 49 | if [ -n "${BASH_SOURCE}" ]; then echo "$(dirname "$BASH_SOURCE")/../.."; fi |
| 50 | } |
| 51 | |
| 52 | # Based on the list of possible source locations we set GCLIENT_ROOT if it is |
| 53 | # not already defined by looking for a src directory in each seach path |
| 54 | # location. If we do not find a valid looking root we error out. |
| 55 | function get_gclient_root() { |
| 56 | if [ -n "${GCLIENT_ROOT}" ]; then |
| 57 | return |
| 58 | fi |
| 59 | |
| 60 | for path in $(get_gclient_root_list); do |
| 61 | if [ -d "${path}/src" ]; then |
| 62 | GCLIENT_ROOT=${path} |
| 63 | break |
| 64 | fi |
| 65 | done |
| 66 | |
| 67 | if [ -z "${GCLIENT_ROOT}" ]; then |
| 68 | # Using dash or sh, we don't know where we are. $0 refers to the calling |
| 69 | # script, not ourselves, so that doesn't help us. |
| 70 | echo "Unable to determine location for common.sh. If you are sourcing" |
| 71 | echo "common.sh from a script run via dash or sh, you must do it in the" |
| 72 | echo "following way:" |
| 73 | echo ' COMMON_SH="$(dirname "$0")/../../scripts/common.sh"' |
| 74 | echo ' . "$COMMON_SH"' |
| 75 | echo "where the first line is the relative path from your script to" |
| 76 | echo "common.sh." |
| 77 | exit 1 |
| 78 | fi |
| 79 | } |
| 80 | |
| 81 | # Find root of source tree |
| 82 | get_gclient_root |
| 83 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 84 | # Canonicalize the directories for the root dir and the calling script. |
| 85 | # 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] | 86 | # This is better than just using |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 87 | # FOO = "$(cd $FOO ; pwd)" |
tedbo | 4f44d9e | 2010-01-08 17:26:11 -0800 | [diff] [blame] | 88 | # since that leaves symbolic links intact. |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 89 | # Note that 'realpath' is equivalent to 'readlink -f'. |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 90 | SCRIPT_LOCATION=$(readlink -f $SCRIPT_LOCATION) |
| 91 | GCLIENT_ROOT=$(readlink -f $GCLIENT_ROOT) |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 92 | |
| 93 | # Other directories should always be pathed down from GCLIENT_ROOT. |
| 94 | SRC_ROOT="$GCLIENT_ROOT/src" |
| 95 | SRC_INTERNAL="$GCLIENT_ROOT/src-internal" |
| 96 | SCRIPTS_DIR="$SRC_ROOT/scripts" |
| 97 | |
| 98 | # Load developer's custom settings. Default location is in scripts dir, |
| 99 | # since that's available both inside and outside the chroot. By convention, |
| 100 | # settings from this file are variables starting with 'CHROMEOS_' |
| 101 | CHROMEOS_DEV_SETTINGS="${CHROMEOS_DEV_SETTINGS:-$SCRIPTS_DIR/.chromeos_dev}" |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 102 | if [ -f $CHROMEOS_DEV_SETTINGS ]; then |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 103 | # Turn on exit-on-error during custom settings processing |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 104 | SAVE_OPTS=$(set +o) |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 105 | set -e |
| 106 | |
| 107 | # Read settings |
| 108 | . $CHROMEOS_DEV_SETTINGS |
| 109 | |
| 110 | # Restore previous state of exit-on-error |
| 111 | eval "$SAVE_OPTS" |
| 112 | fi |
| 113 | |
| 114 | # Load shflags |
Anush Elangovan | 56de1dd | 2010-08-02 23:56:07 -0700 | [diff] [blame] | 115 | if [[ -f /usr/lib/shflags ]]; then |
| 116 | . /usr/lib/shflags |
Raja Aluri | c6e23cd | 2010-11-02 15:55:57 -0700 | [diff] [blame] | 117 | elif [ -f ./lib/shflags/shflags ]; then |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 118 | . ./lib/shflags/shflags |
Anush Elangovan | 56de1dd | 2010-08-02 23:56:07 -0700 | [diff] [blame] | 119 | else |
| 120 | . "${SRC_ROOT}/scripts/lib/shflags/shflags" |
| 121 | fi |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 122 | |
Bill Richardson | 10d27c2 | 2010-01-20 13:38:50 -0800 | [diff] [blame] | 123 | # Our local mirror |
| 124 | DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"} |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 125 | |
Bill Richardson | 10d27c2 | 2010-01-20 13:38:50 -0800 | [diff] [blame] | 126 | # Upstream mirrors and build suites come in 2 flavors |
| 127 | # DEV - development chroot, used to build the chromeos image |
| 128 | # IMG - bootable image, to run on actual hardware |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 129 | |
Bill Richardson | 10d27c2 | 2010-01-20 13:38:50 -0800 | [diff] [blame] | 130 | DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"} |
| 131 | DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"} |
| 132 | |
| 133 | DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"} |
| 134 | DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"} |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 135 | |
| 136 | # Default location for chroot |
| 137 | DEFAULT_CHROOT_DIR=${CHROMEOS_CHROOT_DIR:-"$GCLIENT_ROOT/chroot"} |
| 138 | |
| 139 | # All output files from build should go under $DEFAULT_BUILD_ROOT, so that |
| 140 | # they don't pollute the source directory. |
| 141 | DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"$SRC_ROOT/build"} |
| 142 | |
David McMahon | 4930294 | 2010-02-18 16:55:35 -0800 | [diff] [blame] | 143 | # Set up a global ALL_BOARDS value |
Raja Aluri | d44bcf9 | 2010-11-11 17:46:53 -0800 | [diff] [blame] | 144 | if [ -d $SRC_ROOT/overlays ]; then |
| 145 | ALL_BOARDS=$(cd $SRC_ROOT/overlays;ls -1d overlay-* 2>&-|sed 's,overlay-,,g') |
David Rochberg | 3b91070 | 2010-12-02 10:45:21 -0500 | [diff] [blame] | 146 | fi |
David McMahon | 4930294 | 2010-02-18 16:55:35 -0800 | [diff] [blame] | 147 | # Strip CR |
| 148 | ALL_BOARDS=$(echo $ALL_BOARDS) |
| 149 | # Set a default BOARD |
| 150 | #DEFAULT_BOARD=x86-generic # or... |
| 151 | DEFAULT_BOARD=$(echo $ALL_BOARDS | awk '{print $NF}') |
| 152 | |
David James | ff07201 | 2010-11-30 13:22:05 -0800 | [diff] [blame] | 153 | # Enable --fast by default. |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 154 | DEFAULT_FAST=${FLAGS_TRUE} |
David James | 0366864 | 2010-07-28 17:08:29 -0700 | [diff] [blame] | 155 | |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 156 | |
| 157 | # Standard filenames |
| 158 | CHROMEOS_IMAGE_NAME="chromiumos_image.bin" |
| 159 | CHROMEOS_TEST_IMAGE_NAME="chromiumos_test_image.bin" |
Chris Sosa | b885b80 | 2011-02-16 15:33:11 -0800 | [diff] [blame] | 160 | CHROMEOS_FACTORY_TEST_IMAGE_NAME="chromiumos_factory_image.bin" |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 161 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 162 | # Directory locations inside the dev chroot |
| 163 | CHROOT_TRUNK_DIR="/home/$USER/trunk" |
| 164 | |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 165 | # Install make for portage ebuilds. Used by build_image and gmergefs. |
Chris Masone | d11ce17 | 2010-11-09 14:22:08 -0800 | [diff] [blame] | 166 | # TODO: Is /usr/local/autotest-chrome still used by anyone? |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 167 | DEFAULT_INSTALL_MASK=" |
| 168 | *.a |
| 169 | *.la |
| 170 | /etc/init.d |
| 171 | /etc/runlevels |
| 172 | /lib/rc |
| 173 | /usr/bin/Xnest |
| 174 | /usr/bin/Xvfb |
| 175 | /usr/include |
| 176 | /usr/lib/debug |
| 177 | /usr/lib/gcc |
| 178 | /usr/lib/gtk-2.0/include |
| 179 | /usr/lib/pkgconfig |
| 180 | /usr/local/autotest |
| 181 | /usr/local/autotest-chrome |
| 182 | /usr/man |
| 183 | /usr/share/aclocal |
| 184 | /usr/share/doc |
| 185 | /usr/share/gettext |
| 186 | /usr/share/gtk-2.0 |
| 187 | /usr/share/gtk-doc |
| 188 | /usr/share/info |
| 189 | /usr/share/man |
| 190 | /usr/share/openrc |
| 191 | /usr/share/pkgconfig |
| 192 | /usr/share/readline |
| 193 | " |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 194 | |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 195 | FACTORY_INSTALL_MASK=" |
| 196 | /opt/Qualcomm |
| 197 | /opt/Synaptics |
| 198 | /opt/google/chrome |
| 199 | /opt/google/o3d |
| 200 | /opt/google/talkplugin |
| 201 | /opt/netscape |
| 202 | /usr/lib/debug |
| 203 | /usr/lib/dri |
| 204 | /usr/lib/python2.6/test |
| 205 | /usr/local/autotest |
| 206 | /usr/local/autotest-chrome |
| 207 | /usr/local/autotest-pkgs |
| 208 | /usr/share/X11 |
| 209 | /usr/share/chewing |
| 210 | /usr/share/fonts |
| 211 | /usr/share/ibus-pinyin |
| 212 | /usr/share/libhangul |
| 213 | /usr/share/locale |
| 214 | /usr/share/m17n |
| 215 | /usr/share/mime |
| 216 | /usr/share/sounds |
| 217 | /usr/share/tts |
| 218 | /usr/share/zoneinfo |
| 219 | " |
Tom Wai-Hong Tam | f87a367 | 2010-05-17 16:06:33 +0800 | [diff] [blame] | 220 | |
J. Richard Barnette | f502978 | 2011-07-22 09:12:52 -0700 | [diff] [blame] | 221 | V_REVERSE="$(tput rev)" |
| 222 | V_VIDOFF="$(tput sgr0)" |
| 223 | V_BOLD_RED="$(tput bold; tput setaf 1)" |
| 224 | V_BOLD_GREEN="$(tput bold; tput setaf 2)" |
| 225 | V_BOLD_YELLOW="$(tput bold; tput setaf 3)" |
David McMahon | b7eb3a2 | 2010-02-09 14:07:40 -0800 | [diff] [blame] | 226 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 227 | # ----------------------------------------------------------------------------- |
| 228 | # Functions |
| 229 | |
Chris Sosa | acada73 | 2010-02-23 15:20:03 -0800 | [diff] [blame] | 230 | function setup_board_warning { |
tedbo | 373c390 | 2010-04-12 10:52:40 -0700 | [diff] [blame] | 231 | echo |
| 232 | echo "$V_REVERSE================= WARNING ======================$V_VIDOFF" |
Chris Sosa | acada73 | 2010-02-23 15:20:03 -0800 | [diff] [blame] | 233 | echo |
| 234 | echo "*** No default board detected in " \ |
| 235 | "$GCLIENT_ROOT/src/scripts/.default_board" |
| 236 | echo "*** Either run setup_board with default flag set" |
| 237 | echo "*** or echo |board_name| > $GCLIENT_ROOT/src/scripts/.default_board" |
| 238 | echo |
| 239 | } |
| 240 | |
| 241 | |
| 242 | # Sets the default board variable for calling script |
| 243 | function get_default_board { |
tedbo | 373c390 | 2010-04-12 10:52:40 -0700 | [diff] [blame] | 244 | DEFAULT_BOARD= |
| 245 | |
Chris Sosa | acada73 | 2010-02-23 15:20:03 -0800 | [diff] [blame] | 246 | if [ -f "$GCLIENT_ROOT/src/scripts/.default_board" ] ; then |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 247 | DEFAULT_BOARD=$(cat "$GCLIENT_ROOT/src/scripts/.default_board") |
Chris Sosa | acada73 | 2010-02-23 15:20:03 -0800 | [diff] [blame] | 248 | fi |
| 249 | } |
| 250 | |
| 251 | |
Don Garrett | 640a058 | 2010-05-04 16:54:28 -0700 | [diff] [blame] | 252 | # Enter a chroot and restart the current script if needed |
| 253 | function restart_in_chroot_if_needed { |
David Rochberg | 3b91070 | 2010-12-02 10:45:21 -0500 | [diff] [blame] | 254 | # NB: Pass in ARGV: restart_in_chroot_if_needed "$@" |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 255 | if [ $INSIDE_CHROOT -ne 1 ]; then |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 256 | # Get inside_chroot path for script. |
| 257 | local chroot_path="$(reinterpret_path_for_chroot "$0")" |
Don Garrett | 640a058 | 2010-05-04 16:54:28 -0700 | [diff] [blame] | 258 | exec $SCRIPTS_DIR/enter_chroot.sh -- \ |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 259 | "$chroot_path" "$@" |
Don Garrett | 640a058 | 2010-05-04 16:54:28 -0700 | [diff] [blame] | 260 | fi |
| 261 | } |
| 262 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 263 | # Fail unless we're inside the chroot. This guards against messing up your |
| 264 | # workstation. |
| 265 | function assert_inside_chroot { |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 266 | if [ $INSIDE_CHROOT -ne 1 ]; then |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 267 | echo "This script must be run inside the chroot. Run this first:" |
| 268 | echo " $SCRIPTS_DIR/enter_chroot.sh" |
| 269 | exit 1 |
| 270 | fi |
| 271 | } |
| 272 | |
| 273 | # Fail if we're inside the chroot. This guards against creating or entering |
| 274 | # nested chroots, among other potential problems. |
| 275 | function assert_outside_chroot { |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 276 | if [ $INSIDE_CHROOT -ne 0 ]; then |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 277 | echo "This script must be run outside the chroot." |
| 278 | exit 1 |
| 279 | fi |
| 280 | } |
| 281 | |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 282 | function assert_not_root_user { |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 283 | if [ $(id -u) = 0 ]; then |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 284 | echo "This script must be run as a non-root user." |
| 285 | exit 1 |
| 286 | fi |
| 287 | } |
| 288 | |
Luigi Semenzato | 1f82e12 | 2010-03-23 12:43:08 -0700 | [diff] [blame] | 289 | # Check that all arguments are flags; that is, there are no remaining arguments |
| 290 | # after parsing from shflags. Allow (with a warning) a single empty-string |
| 291 | # argument. |
| 292 | # |
| 293 | # TODO: fix buildbot so that it doesn't pass the empty-string parameter, |
| 294 | # then change this function. |
| 295 | # |
| 296 | # Usage: check_flags_only_and_allow_null_arg "$@" && set -- |
| 297 | function check_flags_only_and_allow_null_arg { |
| 298 | do_shift=1 |
| 299 | if [[ $# == 1 && -z "$@" ]]; then |
| 300 | echo "$0: warning: ignoring null argument" >&2 |
| 301 | shift |
| 302 | do_shift=0 |
| 303 | fi |
| 304 | if [[ $# -gt 0 ]]; then |
| 305 | echo "error: invalid arguments: \"$@\"" >&2 |
| 306 | flags_help |
| 307 | exit 1 |
| 308 | fi |
| 309 | return $do_shift |
| 310 | } |
| 311 | |
Chris Sosa | d979852 | 2010-06-16 14:29:50 -0700 | [diff] [blame] | 312 | function info { |
Anton Staaf | 6adf1a3 | 2011-03-02 10:13:14 -0800 | [diff] [blame] | 313 | echo -e >&2 "${V_BOLD_GREEN}INFO ${CROS_LOG_PREFIX:-""}: $1${V_VIDOFF}" |
Will Drewry | 55b42c9 | 2010-10-20 15:44:11 -0500 | [diff] [blame] | 314 | } |
Chris Sosa | d979852 | 2010-06-16 14:29:50 -0700 | [diff] [blame] | 315 | |
robotboy | 2ea03ac | 2010-02-11 15:30:55 -0800 | [diff] [blame] | 316 | function warn { |
Anton Staaf | 6adf1a3 | 2011-03-02 10:13:14 -0800 | [diff] [blame] | 317 | echo -e >&2 "${V_BOLD_YELLOW}WARNING ${CROS_LOG_PREFIX:-""}: $1${V_VIDOFF}" |
robotboy | 2ea03ac | 2010-02-11 15:30:55 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | function error { |
Anton Staaf | 6adf1a3 | 2011-03-02 10:13:14 -0800 | [diff] [blame] | 321 | echo -e >&2 "${V_BOLD_RED}ERROR ${CROS_LOG_PREFIX:-""}: $1${V_VIDOFF}" |
robotboy | 2ea03ac | 2010-02-11 15:30:55 -0800 | [diff] [blame] | 322 | } |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 323 | |
| 324 | function die { |
| 325 | error "$1" |
| 326 | exit 1 |
| 327 | } |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 328 | |
| 329 | # Retry an emerge command according to $FLAGS_retries |
David James | fd7403a | 2010-07-09 12:00:17 -0700 | [diff] [blame] | 330 | # 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] | 331 | function eretry () { |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 332 | local i |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 333 | for i in $(seq $FLAGS_retries); do |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 334 | echo "Retrying $@" |
| 335 | "$@" $EMERGE_JOBS && return 0 |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 336 | done |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 337 | "$@" && return 0 |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 338 | return 1 |
| 339 | } |
| 340 | |
| 341 | # Removes single quotes around parameter |
| 342 | # Arguments: |
| 343 | # $1 - string which optionally has surrounding quotes |
| 344 | # Returns: |
| 345 | # None, but prints the string without quotes. |
| 346 | function remove_quotes() { |
| 347 | echo "$1" | sed -e "s/^'//; s/'$//" |
| 348 | } |
tedbo | 373c390 | 2010-04-12 10:52:40 -0700 | [diff] [blame] | 349 | |
| 350 | # Writes stdin to the given file name as root using sudo in overwrite mode. |
| 351 | # |
| 352 | # $1 - The output file name. |
| 353 | function sudo_clobber() { |
| 354 | sudo tee "$1" > /dev/null |
| 355 | } |
| 356 | |
| 357 | # Writes stdin to the given file name as root using sudo in append mode. |
| 358 | # |
| 359 | # $1 - The output file name. |
| 360 | function sudo_append() { |
| 361 | sudo tee -a "$1" > /dev/null |
| 362 | } |
robotboy | 9891221 | 2010-04-12 14:08:14 -0700 | [diff] [blame] | 363 | |
| 364 | # Unmounts a directory, if the unmount fails, warn, and then lazily unmount. |
| 365 | # |
| 366 | # $1 - The path to unmount. |
| 367 | function safe_umount { |
| 368 | path=${1:?} |
| 369 | shift |
| 370 | |
| 371 | if ! sudo umount -d "${path}"; then |
| 372 | warn "Failed to unmount ${path}" |
| 373 | warn "Doing a lazy unmount" |
| 374 | |
| 375 | sudo umount -d -l "${path}" || die "Failed to lazily unmount ${path}" |
| 376 | fi |
| 377 | } |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 378 | |
Chris Sosa | d445502 | 2010-05-20 10:14:06 -0700 | [diff] [blame] | 379 | # Fixes symlinks that are incorrectly prefixed with the build root ${1} |
| 380 | # rather than the real running root '/'. |
| 381 | # TODO(sosa) - Merge setup - cleanup below with this method. |
| 382 | fix_broken_symlinks() { |
| 383 | local build_root="${1}" |
| 384 | local symlinks=$(find "${build_root}/usr/local" -lname "${build_root}/*") |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 385 | local symlink |
Chris Sosa | d445502 | 2010-05-20 10:14:06 -0700 | [diff] [blame] | 386 | for symlink in ${symlinks}; do |
| 387 | echo "Fixing ${symlink}" |
| 388 | local target=$(ls -l "${symlink}" | cut -f 2 -d '>') |
| 389 | # Trim spaces from target (bashism). |
| 390 | target=${target/ /} |
| 391 | # Make new target (removes rootfs prefix). |
| 392 | new_target=$(echo ${target} | sed "s#${build_root}##") |
| 393 | |
| 394 | echo "Fixing symlink ${symlink}" |
| 395 | sudo unlink "${symlink}" |
| 396 | sudo ln -sf "${new_target}" "${symlink}" |
| 397 | done |
| 398 | } |
| 399 | |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 400 | # Sets up symlinks for the developer root. It is necessary to symlink |
| 401 | # usr and local since the developer root is mounted at /usr/local and |
| 402 | # applications expect to be installed under /usr/local/bin, etc. |
| 403 | # This avoids packages installing into /usr/local/usr/local/bin. |
| 404 | # ${1} specifies the symlink target for the developer root. |
| 405 | # ${2} specifies the symlink target for the var directory. |
| 406 | # ${3} specifies the location of the stateful partition. |
| 407 | setup_symlinks_on_root() { |
| 408 | # Give args better names. |
| 409 | local dev_image_target=${1} |
| 410 | local var_target=${2} |
| 411 | local dev_image_root="${3}/dev_image" |
| 412 | |
| 413 | # If our var target is actually the standard var, we are cleaning up the |
| 414 | # symlinks (could also check for /usr/local for the dev_image_target). |
| 415 | if [ ${var_target} = "/var" ]; then |
| 416 | echo "Cleaning up /usr/local symlinks for ${dev_image_root}" |
| 417 | else |
| 418 | echo "Setting up symlinks for /usr/local for ${dev_image_root}" |
| 419 | fi |
| 420 | |
| 421 | # Set up symlinks that should point to ${dev_image_target}. |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 422 | local path |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 423 | for path in usr local; do |
| 424 | if [ -h "${dev_image_root}/${path}" ]; then |
| 425 | sudo unlink "${dev_image_root}/${path}" |
| 426 | elif [ -e "${dev_image_root}/${path}" ]; then |
| 427 | die "${dev_image_root}/${path} should be a symlink if exists" |
| 428 | fi |
| 429 | sudo ln -s ${dev_image_target} "${dev_image_root}/${path}" |
| 430 | done |
| 431 | |
| 432 | # Setup var symlink. |
| 433 | if [ -h "${dev_image_root}/var" ]; then |
| 434 | sudo unlink "${dev_image_root}/var" |
| 435 | elif [ -e "${dev_image_root}/var" ]; then |
| 436 | die "${dev_image_root}/var should be a symlink if it exists" |
| 437 | fi |
| 438 | |
| 439 | sudo ln -s "${var_target}" "${dev_image_root}/var" |
| 440 | } |
Nick Sanders | d250927 | 2010-06-16 03:50:04 -0700 | [diff] [blame] | 441 | |
Will Drewry | 55b42c9 | 2010-10-20 15:44:11 -0500 | [diff] [blame] | 442 | # These two helpers clobber the ro compat value in our root filesystem. |
| 443 | # |
| 444 | # When the system is built with --enable_rootfs_verification, bit-precise |
| 445 | # integrity checking is performed. That precision poses a usability issue on |
| 446 | # systems that automount partitions with recognizable filesystems, such as |
| 447 | # ext2/3/4. When the filesystem is mounted 'rw', ext2 metadata will be |
| 448 | # automatically updated even if no other writes are performed to the |
| 449 | # filesystem. In addition, ext2+ does not support a "read-only" flag for a |
| 450 | # given filesystem. That said, forward and backward compatibility of |
| 451 | # filesystem features are supported by tracking if a new feature breaks r/w or |
| 452 | # just write compatibility. We abuse the read-only compatibility flag[1] in |
| 453 | # the filesystem header by setting the high order byte (le) to FF. This tells |
| 454 | # the kernel that features R24-R31 are all enabled. Since those features are |
| 455 | # undefined on all ext-based filesystem, all standard kernels will refuse to |
| 456 | # mount the filesystem as read-write -- only read-only[2]. |
| 457 | # |
| 458 | # [1] 32-bit flag we are modifying: |
| 459 | # http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=include/linux/ext2_fs.h#l417 |
| 460 | # [2] Mount behavior is enforced here: |
| 461 | # http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=fs/ext2/super.c#l857 |
| 462 | # |
| 463 | # N.B., if the high order feature bits are used in the future, we will need to |
| 464 | # revisit this technique. |
| 465 | disable_rw_mount() { |
| 466 | local rootfs="$1" |
| 467 | local offset="${2-0}" # in bytes |
Will Drewry | 9b7cb51 | 2010-10-20 18:11:24 -0500 | [diff] [blame] | 468 | local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte |
| 469 | printf '\377' | |
Will Drewry | 55b42c9 | 2010-10-20 15:44:11 -0500 | [diff] [blame] | 470 | sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \ |
| 471 | conv=notrunc count=1 bs=1 |
| 472 | } |
| 473 | |
| 474 | enable_rw_mount() { |
| 475 | local rootfs="$1" |
| 476 | local offset="${2-0}" |
Will Drewry | 9b7cb51 | 2010-10-20 18:11:24 -0500 | [diff] [blame] | 477 | local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte |
| 478 | printf '\000' | |
Will Drewry | 55b42c9 | 2010-10-20 15:44:11 -0500 | [diff] [blame] | 479 | sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \ |
| 480 | conv=notrunc count=1 bs=1 |
| 481 | } |
| 482 | |
Nick Sanders | d250927 | 2010-06-16 03:50:04 -0700 | [diff] [blame] | 483 | # Get current timestamp. Assumes common.sh runs at startup. |
| 484 | start_time=$(date +%s) |
| 485 | |
| 486 | # Print time elsapsed since start_time. |
| 487 | print_time_elapsed() { |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 488 | local end_time=$(date +%s) |
| 489 | local elapsed_seconds=$(($end_time - $start_time)) |
| 490 | local minutes=$(($elapsed_seconds / 60)) |
| 491 | local seconds=$(($elapsed_seconds % 60)) |
Olof Johansson | 6d49138 | 2010-08-09 16:05:50 -0500 | [diff] [blame] | 492 | echo "Elapsed time: ${minutes}m${seconds}s" |
Nick Sanders | d250927 | 2010-06-16 03:50:04 -0700 | [diff] [blame] | 493 | } |
Doug Anderson | 0c9e88d | 2010-10-19 14:49:39 -0700 | [diff] [blame] | 494 | |
Anton Staaf | 9bcd841 | 2011-01-24 15:27:14 -0800 | [diff] [blame] | 495 | # The board and variant command line options can be used in a number of ways |
| 496 | # to specify the board and variant. The board can encode both pieces of |
| 497 | # information separated by underscores. Or the variant can be passed using |
| 498 | # the separate variant option. This function extracts the canonical board and |
| 499 | # variant information and provides it in the BOARD, VARIANT and BOARD_VARIANT |
| 500 | # variables. |
| 501 | get_board_and_variant() { |
| 502 | local flags_board="${1}" |
| 503 | local flags_variant="${2}" |
| 504 | |
| 505 | BOARD=$(echo "$flags_board" | cut -d '_' -f 1) |
| 506 | VARIANT=${flags_variant:-$(echo "$flags_board" | cut -s -d '_' -f 2)} |
| 507 | |
| 508 | if [ -n "$VARIANT" ]; then |
| 509 | BOARD_VARIANT="${BOARD}_${VARIANT}" |
| 510 | else |
| 511 | BOARD_VARIANT="${BOARD}" |
| 512 | fi |
| 513 | } |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 514 | |
| 515 | # This function converts a chromiumos image into a test image, either |
| 516 | # in place or by copying to a new test image filename first. It honors |
| 517 | # the following flags (see mod_image_for_test.sh) |
| 518 | # |
| 519 | # --factory |
| 520 | # --factory_install |
| 521 | # --force_copy |
| 522 | # |
| 523 | # On entry, pass the directory containing the image, and the image filename |
| 524 | # On exit, it puts the pathname of the resulting test image into |
| 525 | # CHROMEOS_RETURN_VAL |
| 526 | # (yes this is ugly, but perhaps less ugly than the alternatives) |
| 527 | # |
| 528 | # Usage: |
| 529 | # SRC_IMAGE=$(prepare_test_image "directory" "imagefile") |
| 530 | prepare_test_image() { |
| 531 | # If we're asked to modify the image for test, then let's make a copy and |
| 532 | # modify that instead. |
| 533 | # Check for manufacturing image. |
| 534 | local args |
| 535 | |
| 536 | if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then |
| 537 | args="--factory" |
| 538 | fi |
| 539 | |
| 540 | # Check for install shim. |
| 541 | if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]; then |
| 542 | args="--factory_install" |
| 543 | fi |
| 544 | |
| 545 | # Check for forcing copy of image |
| 546 | if [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ]; then |
| 547 | args="${args} --force_copy" |
| 548 | fi |
| 549 | |
| 550 | # Modify the image for test, creating a new test image |
| 551 | "${SCRIPTS_DIR}/mod_image_for_test.sh" --board=${FLAGS_board} \ |
| 552 | --image="$1/$2" --noinplace ${args} |
| 553 | |
| 554 | # From now on we use the just-created test image |
Simon Glass | 6e448ae | 2011-03-03 11:20:54 -0800 | [diff] [blame] | 555 | if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then |
| 556 | CHROMEOS_RETURN_VAL="$1/${CHROMEOS_FACTORY_TEST_IMAGE_NAME}" |
| 557 | else |
| 558 | CHROMEOS_RETURN_VAL="$1/${CHROMEOS_TEST_IMAGE_NAME}" |
| 559 | fi |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 560 | } |
Anton Staaf | 6f5262d | 2011-03-02 09:35:54 -0800 | [diff] [blame] | 561 | |
| 562 | # Check that the specified file exists. If the file path is empty or the file |
| 563 | # doesn't exist on the filesystem generate useful error messages. Otherwise |
| 564 | # show the user the name and path of the file that will be used. The padding |
| 565 | # parameter can be used to tabulate multiple name:path pairs. For example: |
| 566 | # |
| 567 | # check_for_file "really long name" "...:" "file.foo" |
| 568 | # check_for_file "short name" ".........:" "another.bar" |
| 569 | # |
| 570 | # Results in the following output: |
| 571 | # |
| 572 | # Using really long name...: file.foo |
| 573 | # Using short name.........: another.bar |
| 574 | # |
| 575 | # If tabulation is not required then passing "" for padding generates the |
| 576 | # output "Using <name> <path>" |
| 577 | check_for_file() { |
| 578 | local name=$1 |
| 579 | local padding=$2 |
| 580 | local path=$3 |
| 581 | |
| 582 | if [ -z "${path}" ]; then |
| 583 | die "No ${name} file specified." |
| 584 | fi |
| 585 | |
| 586 | if [ ! -e "${path}" ]; then |
| 587 | die "No ${name} file found at: ${path}" |
| 588 | else |
| 589 | info "Using ${name}${padding} ${path}" |
| 590 | fi |
| 591 | } |
| 592 | |
| 593 | # Check that the specified tool exists. If it does not exist in the PATH |
| 594 | # generate a useful error message indicating how to install the ebuild |
| 595 | # that contains the required tool. |
| 596 | check_for_tool() { |
| 597 | local tool=$1 |
| 598 | local ebuild=$2 |
| 599 | |
| 600 | if ! which "${tool}" >/dev/null ; then |
| 601 | error "The ${tool} utility was not found in your path. Run the following" |
| 602 | error "command in your chroot to install it: sudo -E emerge ${ebuild}" |
| 603 | exit 1 |
| 604 | fi |
| 605 | } |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 606 | |
| 607 | # Reinterprets path from outside the chroot for use inside. |
| 608 | # Returns "" if "" given. |
| 609 | # $1 - The path to reinterpret. |
| 610 | function reinterpret_path_for_chroot() { |
| 611 | if [ $INSIDE_CHROOT -ne 1 ]; then |
| 612 | if [ -z "${1}" ]; then |
| 613 | echo "" |
| 614 | else |
| 615 | local path_abs_path=$(readlink -f "${1}") |
| 616 | local gclient_root_abs_path=$(readlink -f "${GCLIENT_ROOT}") |
| 617 | |
| 618 | # Strip the repository root from the path. |
| 619 | local relative_path=$(echo ${path_abs_path} \ |
| 620 | | sed s:${gclient_root_abs_path}/::) |
| 621 | |
| 622 | if [ "${relative_path}" = "${path_abs_path}" ]; then |
| 623 | die "Error reinterpreting path. Path ${1} is not within source tree." |
| 624 | fi |
| 625 | |
| 626 | # Prepend the chroot repository path. |
| 627 | echo "/home/${USER}/trunk/${relative_path}" |
| 628 | fi |
| 629 | else |
| 630 | # Path is already inside the chroot :). |
| 631 | echo "${1}" |
| 632 | fi |
| 633 | } |