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 |
Alexey Marinichev | 63c42aa | 2009-12-21 11:42:39 -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 | |
| 17 | # Store location of the calling script. |
| 18 | TOP_SCRIPT_DIR="${TOP_SCRIPT_DIR:-$(dirname $0)}" |
| 19 | |
| 20 | # Find root of source tree |
| 21 | if [ "x$GCLIENT_ROOT" != "x" ] |
| 22 | then |
| 23 | # GCLIENT_ROOT already set, so we're done |
| 24 | true |
| 25 | elif [ "x$COMMON_SH" != "x" ] |
| 26 | then |
| 27 | # COMMON_SH set, so assume that's us |
| 28 | GCLIENT_ROOT="$(dirname "$COMMON_SH")/../.." |
| 29 | elif [ "x$BASH_SOURCE" != "x" ] |
| 30 | then |
| 31 | # Using bash, so we can find ourselves |
| 32 | GCLIENT_ROOT="$(dirname "$BASH_SOURCE")/../.." |
| 33 | else |
tedbo | 4f44d9e | 2010-01-08 17:26:11 -0800 | [diff] [blame] | 34 | # Using dash or sh, we don't know where we are. $0 refers to the calling |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 35 | # script, not ourselves, so that doesn't help us. |
| 36 | echo "Unable to determine location for common.sh. If you are sourcing" |
| 37 | echo "common.sh from a script run via dash or sh, you must do it in the" |
| 38 | echo "following way:" |
| 39 | echo ' COMMON_SH="$(dirname "$0")/../../scripts/common.sh"' |
| 40 | echo ' . "$COMMON_SH"' |
| 41 | echo "where the first line is the relative path from your script to" |
| 42 | echo "common.sh." |
| 43 | exit 1 |
| 44 | fi |
| 45 | |
| 46 | # Canonicalize the directories for the root dir and the calling script. |
| 47 | # 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] | 48 | # This is better than just using |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 49 | # FOO = "$(cd $FOO ; pwd)" |
tedbo | 4f44d9e | 2010-01-08 17:26:11 -0800 | [diff] [blame] | 50 | # since that leaves symbolic links intact. |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 51 | # Note that 'realpath' is equivalent to 'readlink -f'. |
| 52 | TOP_SCRIPT_DIR=`readlink -f $TOP_SCRIPT_DIR` |
| 53 | GCLIENT_ROOT=`readlink -f $GCLIENT_ROOT` |
| 54 | |
| 55 | # Other directories should always be pathed down from GCLIENT_ROOT. |
| 56 | SRC_ROOT="$GCLIENT_ROOT/src" |
| 57 | SRC_INTERNAL="$GCLIENT_ROOT/src-internal" |
| 58 | SCRIPTS_DIR="$SRC_ROOT/scripts" |
| 59 | |
| 60 | # Load developer's custom settings. Default location is in scripts dir, |
| 61 | # since that's available both inside and outside the chroot. By convention, |
| 62 | # settings from this file are variables starting with 'CHROMEOS_' |
| 63 | CHROMEOS_DEV_SETTINGS="${CHROMEOS_DEV_SETTINGS:-$SCRIPTS_DIR/.chromeos_dev}" |
| 64 | if [ -f $CHROMEOS_DEV_SETTINGS ] |
| 65 | then |
| 66 | # Turn on exit-on-error during custom settings processing |
| 67 | SAVE_OPTS=`set +o` |
| 68 | set -e |
| 69 | |
| 70 | # Read settings |
| 71 | . $CHROMEOS_DEV_SETTINGS |
| 72 | |
| 73 | # Restore previous state of exit-on-error |
| 74 | eval "$SAVE_OPTS" |
| 75 | fi |
| 76 | |
| 77 | # Load shflags |
| 78 | . "$SRC_ROOT/third_party/shflags/files/src/shflags" |
| 79 | |
Bill Richardson | 10d27c2 | 2010-01-20 13:38:50 -0800 | [diff] [blame] | 80 | # Our local mirror |
| 81 | DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"} |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 82 | |
Bill Richardson | 10d27c2 | 2010-01-20 13:38:50 -0800 | [diff] [blame] | 83 | # Upstream mirrors and build suites come in 2 flavors |
| 84 | # DEV - development chroot, used to build the chromeos image |
| 85 | # IMG - bootable image, to run on actual hardware |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 86 | |
Bill Richardson | 10d27c2 | 2010-01-20 13:38:50 -0800 | [diff] [blame] | 87 | DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"} |
| 88 | DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"} |
| 89 | |
| 90 | DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"} |
| 91 | DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"} |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 92 | |
| 93 | # Default location for chroot |
| 94 | DEFAULT_CHROOT_DIR=${CHROMEOS_CHROOT_DIR:-"$GCLIENT_ROOT/chroot"} |
| 95 | |
| 96 | # All output files from build should go under $DEFAULT_BUILD_ROOT, so that |
| 97 | # they don't pollute the source directory. |
| 98 | DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"$SRC_ROOT/build"} |
| 99 | |
David McMahon | 4930294 | 2010-02-18 16:55:35 -0800 | [diff] [blame] | 100 | # Set up a global ALL_BOARDS value |
Sam Leffler | a92ecd6 | 2010-02-23 08:25:38 -0800 | [diff] [blame] | 101 | ALL_BOARDS=$(cd $SRC_ROOT/overlays;ls -1d overlay-* 2>&-|sed 's,overlay-,,g') |
David McMahon | 4930294 | 2010-02-18 16:55:35 -0800 | [diff] [blame] | 102 | # Strip CR |
| 103 | ALL_BOARDS=$(echo $ALL_BOARDS) |
| 104 | # Set a default BOARD |
| 105 | #DEFAULT_BOARD=x86-generic # or... |
| 106 | DEFAULT_BOARD=$(echo $ALL_BOARDS | awk '{print $NF}') |
| 107 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 108 | # Detect whether we're inside a chroot or not |
| 109 | if [ -e /etc/debian_chroot ] |
| 110 | then |
| 111 | INSIDE_CHROOT=1 |
| 112 | else |
| 113 | INSIDE_CHROOT=0 |
| 114 | fi |
| 115 | |
| 116 | # Directory locations inside the dev chroot |
| 117 | CHROOT_TRUNK_DIR="/home/$USER/trunk" |
| 118 | |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 119 | # Install make for portage ebuilds. Used by build_image and gmergefs. |
Chris Sosa | 3d9a10b | 2010-04-13 15:00:46 -0700 | [diff] [blame] | 120 | DEFAULT_INSTALL_MASK="/usr/include /usr/man /usr/share/man /usr/share/doc \ |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 121 | /usr/share/gtk-doc /usr/share/gtk-2.0 /usr/lib/gtk-2.0/include \ |
| 122 | /usr/share/info /usr/share/aclocal /usr/lib/gcc /usr/lib/pkgconfig \ |
| 123 | /usr/share/pkgconfig /usr/share/gettext /usr/share/readline /etc/runlevels \ |
Amol Kher | 9d2a798 | 2010-05-19 17:30:29 -0700 | [diff] [blame] | 124 | /usr/share/openrc /lib/rc *.a *.la /etc/init.d /usr/lib/debug" |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 125 | |
Tom Wai-Hong Tam | f87a367 | 2010-05-17 16:06:33 +0800 | [diff] [blame] | 126 | FACTORY_INSTALL_MASK="/opt/google/chrome /opt/google/o3d /opt/netscape \ |
| 127 | /usr/lib/dri /usr/lib/python2.6/test /usr/share/chewing /usr/share/fonts \ |
| 128 | /usr/share/ibus-pinyin /usr/share/libhangul /usr/share/locale \ |
| 129 | /usr/share/m17n /usr/share/mime /usr/share/sounds /usr/share/tts \ |
Amol Kher | 9d2a798 | 2010-05-19 17:30:29 -0700 | [diff] [blame] | 130 | /usr/share/X11 /usr/share/zoneinfo /usr/lib/debug" |
Tom Wai-Hong Tam | f87a367 | 2010-05-17 16:06:33 +0800 | [diff] [blame] | 131 | |
David McMahon | b7eb3a2 | 2010-02-09 14:07:40 -0800 | [diff] [blame] | 132 | # Check to ensure not running old scripts |
| 133 | V_REVERSE='[7m' |
| 134 | V_VIDOFF='[m' |
| 135 | case "$(basename $0)" in |
| 136 | build_image.sh|build_platform_packages.sh|customize_rootfs.sh|make_chroot.sh) |
| 137 | echo |
| 138 | echo "$V_REVERSE============================================================" |
| 139 | echo "=========================== WARNING ======================" |
| 140 | echo "============================================================$V_VIDOFF" |
| 141 | echo |
| 142 | echo "RUNNING OLD BUILD SYSTEM SCRIPTS. RUN THE PORTAGE-BASED BUILD HERE:" |
| 143 | echo "http://www.chromium.org/chromium-os/building-chromium-os/portage-based-build" |
| 144 | echo |
| 145 | if [ "$USER" != "chrome-bot" ] |
| 146 | then |
| 147 | read -n1 -p "Press any key to continue using the OLD build system..." |
| 148 | echo |
| 149 | echo |
| 150 | fi |
| 151 | ;; |
| 152 | esac |
| 153 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 154 | # ----------------------------------------------------------------------------- |
| 155 | # Functions |
| 156 | |
Chris Sosa | acada73 | 2010-02-23 15:20:03 -0800 | [diff] [blame] | 157 | function setup_board_warning { |
tedbo | 373c390 | 2010-04-12 10:52:40 -0700 | [diff] [blame] | 158 | echo |
| 159 | echo "$V_REVERSE================= WARNING ======================$V_VIDOFF" |
Chris Sosa | acada73 | 2010-02-23 15:20:03 -0800 | [diff] [blame] | 160 | echo |
| 161 | echo "*** No default board detected in " \ |
| 162 | "$GCLIENT_ROOT/src/scripts/.default_board" |
| 163 | echo "*** Either run setup_board with default flag set" |
| 164 | echo "*** or echo |board_name| > $GCLIENT_ROOT/src/scripts/.default_board" |
| 165 | echo |
| 166 | } |
| 167 | |
| 168 | |
| 169 | # Sets the default board variable for calling script |
| 170 | function get_default_board { |
tedbo | 373c390 | 2010-04-12 10:52:40 -0700 | [diff] [blame] | 171 | DEFAULT_BOARD= |
| 172 | |
Chris Sosa | acada73 | 2010-02-23 15:20:03 -0800 | [diff] [blame] | 173 | if [ -f "$GCLIENT_ROOT/src/scripts/.default_board" ] ; then |
| 174 | DEFAULT_BOARD=`cat "$GCLIENT_ROOT/src/scripts/.default_board"` |
| 175 | fi |
| 176 | } |
| 177 | |
| 178 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 179 | # Make a package |
| 180 | function make_pkg_common { |
| 181 | # Positional parameters from calling script. :? means "fail if unset". |
| 182 | set -e |
| 183 | PKG_BASE=${1:?} |
| 184 | shift |
| 185 | set +e |
tedbo | 4f44d9e | 2010-01-08 17:26:11 -0800 | [diff] [blame] | 186 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 187 | # All packages are built in the chroot |
| 188 | assert_inside_chroot |
| 189 | |
| 190 | # Command line options |
| 191 | DEFINE_string build_root "$DEFAULT_BUILD_ROOT" "Root of build output" |
| 192 | |
| 193 | # Parse command line and update positional args |
| 194 | FLAGS "$@" || exit 1 |
| 195 | eval set -- "${FLAGS_ARGV}" |
| 196 | |
| 197 | # Die on any errors |
| 198 | set -e |
| 199 | |
| 200 | # Make output dir |
| 201 | OUT_DIR="$FLAGS_build_root/x86/local_packages" |
| 202 | mkdir -p "$OUT_DIR" |
| 203 | |
| 204 | # Remove previous package from output dir |
| 205 | rm -f "$OUT_DIR"/${PKG_BASE}_*.deb |
| 206 | |
| 207 | # Rebuild the package |
| 208 | pushd "$TOP_SCRIPT_DIR" |
| 209 | rm -f ../${PKG_BASE}_*.deb |
| 210 | dpkg-buildpackage -b -tc -us -uc -j$NUM_JOBS |
| 211 | mv ../${PKG_BASE}_*.deb "$OUT_DIR" |
| 212 | rm ../${PKG_BASE}_*.changes |
| 213 | popd |
| 214 | } |
| 215 | |
Don Garrett | 640a058 | 2010-05-04 16:54:28 -0700 | [diff] [blame] | 216 | # Enter a chroot and restart the current script if needed |
| 217 | function restart_in_chroot_if_needed { |
| 218 | if [ $INSIDE_CHROOT -ne 1 ] |
| 219 | then |
| 220 | # Equivalent to enter_chroot.sh -- <current command> |
| 221 | exec $SCRIPTS_DIR/enter_chroot.sh -- \ |
| 222 | $CHROOT_TRUNK_DIR/src/scripts/$(basename $0) $* |
| 223 | fi |
| 224 | } |
| 225 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 226 | # Fail unless we're inside the chroot. This guards against messing up your |
| 227 | # workstation. |
| 228 | function assert_inside_chroot { |
| 229 | if [ $INSIDE_CHROOT -ne 1 ] |
| 230 | then |
| 231 | echo "This script must be run inside the chroot. Run this first:" |
| 232 | echo " $SCRIPTS_DIR/enter_chroot.sh" |
| 233 | exit 1 |
| 234 | fi |
| 235 | } |
| 236 | |
| 237 | # Fail if we're inside the chroot. This guards against creating or entering |
| 238 | # nested chroots, among other potential problems. |
| 239 | function assert_outside_chroot { |
| 240 | if [ $INSIDE_CHROOT -ne 0 ] |
| 241 | then |
| 242 | echo "This script must be run outside the chroot." |
| 243 | exit 1 |
| 244 | fi |
| 245 | } |
| 246 | |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 247 | function assert_not_root_user { |
| 248 | if [ `id -u` = 0 ]; then |
| 249 | echo "This script must be run as a non-root user." |
| 250 | exit 1 |
| 251 | fi |
| 252 | } |
| 253 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 254 | # Install a package if it's not already installed |
| 255 | function install_if_missing { |
| 256 | # Positional parameters from calling script. :? means "fail if unset". |
| 257 | PKG_NAME=${1:?} |
| 258 | shift |
| 259 | |
| 260 | if [ -z `which $PKG_NAME` ] |
| 261 | then |
| 262 | echo "Can't find $PKG_NAME; attempting to install it." |
| 263 | sudo apt-get --yes --force-yes install $PKG_NAME |
| 264 | fi |
| 265 | } |
Steve VanDeBogart | 8174ba0 | 2010-01-15 19:45:30 -0800 | [diff] [blame] | 266 | |
| 267 | # Returns true if the input file is whitelisted. |
| 268 | # |
| 269 | # $1 - The file to check |
| 270 | is_whitelisted() { |
David McMahon | b7eb3a2 | 2010-02-09 14:07:40 -0800 | [diff] [blame] | 271 | local file=$1 |
Steve VanDeBogart | 8174ba0 | 2010-01-15 19:45:30 -0800 | [diff] [blame] | 272 | local whitelist="$FLAGS_whitelist" |
| 273 | test -f "$whitelist" || (echo "Whitelist file missing ($whitelist)" && exit 1) |
| 274 | |
| 275 | local checksum=$(md5sum "$file" | awk '{ print $1 }') |
| 276 | local count=$(sed -e "s/#.*$//" "${whitelist}" | grep -c "$checksum" \ |
| 277 | || /bin/true) |
| 278 | test $count -ne 0 |
| 279 | } |
robotboy | 2ea03ac | 2010-02-11 15:30:55 -0800 | [diff] [blame] | 280 | |
Luigi Semenzato | 1f82e12 | 2010-03-23 12:43:08 -0700 | [diff] [blame] | 281 | # Check that all arguments are flags; that is, there are no remaining arguments |
| 282 | # after parsing from shflags. Allow (with a warning) a single empty-string |
| 283 | # argument. |
| 284 | # |
| 285 | # TODO: fix buildbot so that it doesn't pass the empty-string parameter, |
| 286 | # then change this function. |
| 287 | # |
| 288 | # Usage: check_flags_only_and_allow_null_arg "$@" && set -- |
| 289 | function check_flags_only_and_allow_null_arg { |
| 290 | do_shift=1 |
| 291 | if [[ $# == 1 && -z "$@" ]]; then |
| 292 | echo "$0: warning: ignoring null argument" >&2 |
| 293 | shift |
| 294 | do_shift=0 |
| 295 | fi |
| 296 | if [[ $# -gt 0 ]]; then |
| 297 | echo "error: invalid arguments: \"$@\"" >&2 |
| 298 | flags_help |
| 299 | exit 1 |
| 300 | fi |
| 301 | return $do_shift |
| 302 | } |
| 303 | |
robotboy | 2ea03ac | 2010-02-11 15:30:55 -0800 | [diff] [blame] | 304 | V_RED="\e[31m" |
| 305 | V_YELLOW="\e[33m" |
Darin Petkov | b2a21e7 | 2010-03-25 11:33:41 -0700 | [diff] [blame] | 306 | V_BOLD_RED="\e[1;31m" |
| 307 | V_BOLD_YELLOW="\e[1;33m" |
robotboy | 2ea03ac | 2010-02-11 15:30:55 -0800 | [diff] [blame] | 308 | |
| 309 | function warn { |
Darin Petkov | b2a21e7 | 2010-03-25 11:33:41 -0700 | [diff] [blame] | 310 | echo -e "${V_BOLD_YELLOW}WARNING: $1${V_VIDOFF}" |
robotboy | 2ea03ac | 2010-02-11 15:30:55 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | function error { |
Darin Petkov | b2a21e7 | 2010-03-25 11:33:41 -0700 | [diff] [blame] | 314 | echo -e "${V_BOLD_RED}ERROR : $1${V_VIDOFF}" |
robotboy | 2ea03ac | 2010-02-11 15:30:55 -0800 | [diff] [blame] | 315 | } |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 316 | |
| 317 | function die { |
| 318 | error "$1" |
| 319 | exit 1 |
| 320 | } |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 321 | |
| 322 | # Retry an emerge command according to $FLAGS_retries |
| 323 | # The $EMERGE_JOBS flags will only be added the first time the command is run |
| 324 | function eretry () { |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 325 | local i= |
| 326 | for i in $(seq $FLAGS_retries); do |
| 327 | echo Retrying $* |
Nasser Grainawi | cb5f3eb | 2010-04-21 08:46:49 -0600 | [diff] [blame] | 328 | $* $EMERGE_JOBS && return 0 |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 329 | done |
Nasser Grainawi | cb5f3eb | 2010-04-21 08:46:49 -0600 | [diff] [blame] | 330 | $* && return 0 |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 331 | return 1 |
| 332 | } |
| 333 | |
| 334 | # Removes single quotes around parameter |
| 335 | # Arguments: |
| 336 | # $1 - string which optionally has surrounding quotes |
| 337 | # Returns: |
| 338 | # None, but prints the string without quotes. |
| 339 | function remove_quotes() { |
| 340 | echo "$1" | sed -e "s/^'//; s/'$//" |
| 341 | } |
tedbo | 373c390 | 2010-04-12 10:52:40 -0700 | [diff] [blame] | 342 | |
| 343 | # Writes stdin to the given file name as root using sudo in overwrite mode. |
| 344 | # |
| 345 | # $1 - The output file name. |
| 346 | function sudo_clobber() { |
| 347 | sudo tee "$1" > /dev/null |
| 348 | } |
| 349 | |
| 350 | # Writes stdin to the given file name as root using sudo in append mode. |
| 351 | # |
| 352 | # $1 - The output file name. |
| 353 | function sudo_append() { |
| 354 | sudo tee -a "$1" > /dev/null |
| 355 | } |
robotboy | 9891221 | 2010-04-12 14:08:14 -0700 | [diff] [blame] | 356 | |
| 357 | # Unmounts a directory, if the unmount fails, warn, and then lazily unmount. |
| 358 | # |
| 359 | # $1 - The path to unmount. |
| 360 | function safe_umount { |
| 361 | path=${1:?} |
| 362 | shift |
| 363 | |
| 364 | if ! sudo umount -d "${path}"; then |
| 365 | warn "Failed to unmount ${path}" |
| 366 | warn "Doing a lazy unmount" |
| 367 | |
| 368 | sudo umount -d -l "${path}" || die "Failed to lazily unmount ${path}" |
| 369 | fi |
| 370 | } |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 371 | |
Chris Sosa | d445502 | 2010-05-20 10:14:06 -0700 | [diff] [blame] | 372 | # Fixes symlinks that are incorrectly prefixed with the build root ${1} |
| 373 | # rather than the real running root '/'. |
| 374 | # TODO(sosa) - Merge setup - cleanup below with this method. |
| 375 | fix_broken_symlinks() { |
| 376 | local build_root="${1}" |
| 377 | local symlinks=$(find "${build_root}/usr/local" -lname "${build_root}/*") |
| 378 | for symlink in ${symlinks}; do |
| 379 | echo "Fixing ${symlink}" |
| 380 | local target=$(ls -l "${symlink}" | cut -f 2 -d '>') |
| 381 | # Trim spaces from target (bashism). |
| 382 | target=${target/ /} |
| 383 | # Make new target (removes rootfs prefix). |
| 384 | new_target=$(echo ${target} | sed "s#${build_root}##") |
| 385 | |
| 386 | echo "Fixing symlink ${symlink}" |
| 387 | sudo unlink "${symlink}" |
| 388 | sudo ln -sf "${new_target}" "${symlink}" |
| 389 | done |
| 390 | } |
| 391 | |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 392 | # Sets up symlinks for the developer root. It is necessary to symlink |
| 393 | # usr and local since the developer root is mounted at /usr/local and |
| 394 | # applications expect to be installed under /usr/local/bin, etc. |
| 395 | # This avoids packages installing into /usr/local/usr/local/bin. |
| 396 | # ${1} specifies the symlink target for the developer root. |
| 397 | # ${2} specifies the symlink target for the var directory. |
| 398 | # ${3} specifies the location of the stateful partition. |
| 399 | setup_symlinks_on_root() { |
| 400 | # Give args better names. |
| 401 | local dev_image_target=${1} |
| 402 | local var_target=${2} |
| 403 | local dev_image_root="${3}/dev_image" |
| 404 | |
| 405 | # If our var target is actually the standard var, we are cleaning up the |
| 406 | # symlinks (could also check for /usr/local for the dev_image_target). |
| 407 | if [ ${var_target} = "/var" ]; then |
| 408 | echo "Cleaning up /usr/local symlinks for ${dev_image_root}" |
| 409 | else |
| 410 | echo "Setting up symlinks for /usr/local for ${dev_image_root}" |
| 411 | fi |
| 412 | |
| 413 | # Set up symlinks that should point to ${dev_image_target}. |
| 414 | for path in usr local; do |
| 415 | if [ -h "${dev_image_root}/${path}" ]; then |
| 416 | sudo unlink "${dev_image_root}/${path}" |
| 417 | elif [ -e "${dev_image_root}/${path}" ]; then |
| 418 | die "${dev_image_root}/${path} should be a symlink if exists" |
| 419 | fi |
| 420 | sudo ln -s ${dev_image_target} "${dev_image_root}/${path}" |
| 421 | done |
| 422 | |
| 423 | # Setup var symlink. |
| 424 | if [ -h "${dev_image_root}/var" ]; then |
| 425 | sudo unlink "${dev_image_root}/var" |
| 426 | elif [ -e "${dev_image_root}/var" ]; then |
| 427 | die "${dev_image_root}/var should be a symlink if it exists" |
| 428 | fi |
| 429 | |
| 430 | sudo ln -s "${var_target}" "${dev_image_root}/var" |
| 431 | } |
Nick Sanders | d250927 | 2010-06-16 03:50:04 -0700 | [diff] [blame^] | 432 | |
| 433 | # Get current timestamp. Assumes common.sh runs at startup. |
| 434 | start_time=$(date +%s) |
| 435 | |
| 436 | # Print time elsapsed since start_time. |
| 437 | print_time_elapsed() { |
| 438 | end_time=$(date +%s) |
| 439 | elapsed_seconds="$(( $end_time - $start_time ))" |
| 440 | minutes="$(( $elapsed_seconds / 60 ))" |
| 441 | seconds="$(( $elapsed_seconds % 60 ))" |
| 442 | echo "Elapsed time: ${minutes}:${seconds}" |
| 443 | } |