blob: f2066ac72b5e5c5e51bc8bd3445133b1acb08a4f [file] [log] [blame]
David McMahon49302942010-02-18 16:55:35 -08001# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
rspangler@google.comd74220d2009-10-09 20:56:14 +00002# 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 Spencer798d75f2011-02-01 22:04:49 -080015NUM_JOBS=$(grep -c "^processor" /proc/cpuinfo)
rspangler@google.comd74220d2009-10-09 20:56:14 +000016
Simon Glass142ca062011-02-09 13:39:43 -080017# True if we have the 'pv' utility - also set up COMMON_PV_CAT for convenience
18COMMON_PV_OK=1
19COMMON_PV_CAT=pv
20pv -V >/dev/null 2>&1 || COMMON_PV_OK=0
21if [ $COMMON_PV_OK -eq 0 ]; then
22 COMMON_PV_CAT=cat
23fi
24
Greg Spencer798d75f2011-02-01 22:04:49 -080025# Make sure we have the location and name of the calling script, using
26# the current value if it is already set.
27SCRIPT_LOCATION=${SCRIPT_LOCATION:-$(dirname "$(readlink -f "$0")")}
28SCRIPT_NAME=${SCRIPT_NAME:-$(basename "$0")}
rspangler@google.comd74220d2009-10-09 20:56:14 +000029
Anton Staaf30acb0b2011-01-26 16:00:20 -080030# Detect whether we're inside a chroot or not
31if [ -e /etc/debian_chroot ]
rspangler@google.comd74220d2009-10-09 20:56:14 +000032then
Anton Staaf30acb0b2011-01-26 16:00:20 -080033 INSIDE_CHROOT=1
rspangler@google.comd74220d2009-10-09 20:56:14 +000034else
Anton Staaf30acb0b2011-01-26 16:00:20 -080035 INSIDE_CHROOT=0
rspangler@google.comd74220d2009-10-09 20:56:14 +000036fi
37
Anton Staaf30acb0b2011-01-26 16:00:20 -080038# 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.
41function 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.
55function 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
82get_gclient_root
83
rspangler@google.comd74220d2009-10-09 20:56:14 +000084# 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.
tedbo4f44d9e2010-01-08 17:26:11 -080086# This is better than just using
rspangler@google.comd74220d2009-10-09 20:56:14 +000087# FOO = "$(cd $FOO ; pwd)"
tedbo4f44d9e2010-01-08 17:26:11 -080088# since that leaves symbolic links intact.
rspangler@google.comd74220d2009-10-09 20:56:14 +000089# Note that 'realpath' is equivalent to 'readlink -f'.
Mike Frysingera1a06ab2011-08-10 11:40:30 -040090SCRIPT_LOCATION=$(readlink -f "$SCRIPT_LOCATION")
91GCLIENT_ROOT=$(readlink -f "$GCLIENT_ROOT")
rspangler@google.comd74220d2009-10-09 20:56:14 +000092
93# Other directories should always be pathed down from GCLIENT_ROOT.
94SRC_ROOT="$GCLIENT_ROOT/src"
95SRC_INTERNAL="$GCLIENT_ROOT/src-internal"
96SCRIPTS_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_'
101CHROMEOS_DEV_SETTINGS="${CHROMEOS_DEV_SETTINGS:-$SCRIPTS_DIR/.chromeos_dev}"
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400102if [ -f "$CHROMEOS_DEV_SETTINGS" ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000103 # Turn on exit-on-error during custom settings processing
Greg Spencer798d75f2011-02-01 22:04:49 -0800104 SAVE_OPTS=$(set +o)
rspangler@google.comd74220d2009-10-09 20:56:14 +0000105 set -e
106
107 # Read settings
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400108 . "$CHROMEOS_DEV_SETTINGS"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000109
110 # Restore previous state of exit-on-error
111 eval "$SAVE_OPTS"
112fi
113
114# Load shflags
Mike Frysinger9aa80dc2011-10-18 13:52:19 -0400115. "${SCRIPTS_DIR}"/lib/shflags/shflags || exit 1
rspangler@google.comd74220d2009-10-09 20:56:14 +0000116
Bill Richardson10d27c22010-01-20 13:38:50 -0800117# Our local mirror
118DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000119
Bill Richardson10d27c22010-01-20 13:38:50 -0800120# Upstream mirrors and build suites come in 2 flavors
121# DEV - development chroot, used to build the chromeos image
122# IMG - bootable image, to run on actual hardware
rspangler@google.comd74220d2009-10-09 20:56:14 +0000123
Bill Richardson10d27c22010-01-20 13:38:50 -0800124DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
125DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"}
126
127DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
128DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000129
130# Default location for chroot
131DEFAULT_CHROOT_DIR=${CHROMEOS_CHROOT_DIR:-"$GCLIENT_ROOT/chroot"}
132
133# All output files from build should go under $DEFAULT_BUILD_ROOT, so that
134# they don't pollute the source directory.
135DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"$SRC_ROOT/build"}
136
David McMahon49302942010-02-18 16:55:35 -0800137# Set up a global ALL_BOARDS value
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400138if [ -d "$SRC_ROOT/overlays" ]; then
139 ALL_BOARDS=$(cd "$SRC_ROOT/overlays"; \
140 ls -1d overlay-* 2>&- | sed 's,overlay-,,g')
David Rochberg3b910702010-12-02 10:45:21 -0500141fi
David McMahon49302942010-02-18 16:55:35 -0800142# Strip CR
143ALL_BOARDS=$(echo $ALL_BOARDS)
144# Set a default BOARD
145#DEFAULT_BOARD=x86-generic # or...
146DEFAULT_BOARD=$(echo $ALL_BOARDS | awk '{print $NF}')
147
David Jamesff072012010-11-30 13:22:05 -0800148# Enable --fast by default.
Greg Spencer798d75f2011-02-01 22:04:49 -0800149DEFAULT_FAST=${FLAGS_TRUE}
David James03668642010-07-28 17:08:29 -0700150
Simon Glass142ca062011-02-09 13:39:43 -0800151
152# Standard filenames
153CHROMEOS_IMAGE_NAME="chromiumos_image.bin"
154CHROMEOS_TEST_IMAGE_NAME="chromiumos_test_image.bin"
Chris Sosab885b802011-02-16 15:33:11 -0800155CHROMEOS_FACTORY_TEST_IMAGE_NAME="chromiumos_factory_image.bin"
Simon Glass142ca062011-02-09 13:39:43 -0800156
rspangler@google.comd74220d2009-10-09 20:56:14 +0000157# Directory locations inside the dev chroot
158CHROOT_TRUNK_DIR="/home/$USER/trunk"
159
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700160# Install make for portage ebuilds. Used by build_image and gmergefs.
Chris Masoned11ce172010-11-09 14:22:08 -0800161# TODO: Is /usr/local/autotest-chrome still used by anyone?
Daniel Erate82f07c2010-12-21 13:39:22 -0800162DEFAULT_INSTALL_MASK="
163 *.a
164 *.la
165 /etc/init.d
166 /etc/runlevels
167 /lib/rc
168 /usr/bin/Xnest
169 /usr/bin/Xvfb
170 /usr/include
171 /usr/lib/debug
172 /usr/lib/gcc
173 /usr/lib/gtk-2.0/include
174 /usr/lib/pkgconfig
175 /usr/local/autotest
176 /usr/local/autotest-chrome
177 /usr/man
178 /usr/share/aclocal
179 /usr/share/doc
180 /usr/share/gettext
181 /usr/share/gtk-2.0
182 /usr/share/gtk-doc
183 /usr/share/info
184 /usr/share/man
185 /usr/share/openrc
186 /usr/share/pkgconfig
187 /usr/share/readline
Chris Wolfed13775f2011-07-26 16:34:38 -0400188 /usr/src
Daniel Erate82f07c2010-12-21 13:39:22 -0800189 "
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700190
Daniel Erate82f07c2010-12-21 13:39:22 -0800191FACTORY_INSTALL_MASK="
192 /opt/Qualcomm
193 /opt/Synaptics
194 /opt/google/chrome
195 /opt/google/o3d
196 /opt/google/talkplugin
197 /opt/netscape
198 /usr/lib/debug
199 /usr/lib/dri
200 /usr/lib/python2.6/test
201 /usr/local/autotest
202 /usr/local/autotest-chrome
203 /usr/local/autotest-pkgs
204 /usr/share/X11
205 /usr/share/chewing
206 /usr/share/fonts
207 /usr/share/ibus-pinyin
208 /usr/share/libhangul
209 /usr/share/locale
210 /usr/share/m17n
211 /usr/share/mime
212 /usr/share/sounds
213 /usr/share/tts
214 /usr/share/zoneinfo
215 "
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800216
Gaurav Shah786ceeb2011-07-26 15:03:37 -0700217# Determine and set up variables needed for fancy color output (if supported).
Gaurav Shah786ceeb2011-07-26 15:03:37 -0700218V_BOLD_RED=
219V_BOLD_GREEN=
220V_BOLD_YELLOW=
Mike Frysingerc2474ae2011-08-11 18:27:44 -0400221V_REVERSE=
222V_VIDOFF=
Gaurav Shah786ceeb2011-07-26 15:03:37 -0700223
224if tput colors >/dev/null 2>&1; then
Mike Frysingerc2474ae2011-08-11 18:27:44 -0400225 # order matters: we want VIDOFF last so that when we trace with `set -x`,
226 # our terminal doesn't bleed colors as bash dumps the values of vars.
Gaurav Shah786ceeb2011-07-26 15:03:37 -0700227 V_BOLD_RED="$(tput bold; tput setaf 1)"
228 V_BOLD_GREEN="$(tput bold; tput setaf 2)"
229 V_BOLD_YELLOW="$(tput bold; tput setaf 3)"
Mike Frysingerc2474ae2011-08-11 18:27:44 -0400230 V_REVERSE="$(tput rev)"
231 V_VIDOFF="$(tput sgr0)"
Gaurav Shah786ceeb2011-07-26 15:03:37 -0700232fi
David McMahonb7eb3a22010-02-09 14:07:40 -0800233
rspangler@google.comd74220d2009-10-09 20:56:14 +0000234# -----------------------------------------------------------------------------
235# Functions
236
Chris Sosaacada732010-02-23 15:20:03 -0800237function setup_board_warning {
tedbo373c3902010-04-12 10:52:40 -0700238 echo
239 echo "$V_REVERSE================= WARNING ======================$V_VIDOFF"
Chris Sosaacada732010-02-23 15:20:03 -0800240 echo
241 echo "*** No default board detected in " \
242 "$GCLIENT_ROOT/src/scripts/.default_board"
243 echo "*** Either run setup_board with default flag set"
244 echo "*** or echo |board_name| > $GCLIENT_ROOT/src/scripts/.default_board"
245 echo
246}
247
248
249# Sets the default board variable for calling script
250function get_default_board {
tedbo373c3902010-04-12 10:52:40 -0700251 DEFAULT_BOARD=
252
Chris Sosaacada732010-02-23 15:20:03 -0800253 if [ -f "$GCLIENT_ROOT/src/scripts/.default_board" ] ; then
Greg Spencer798d75f2011-02-01 22:04:49 -0800254 DEFAULT_BOARD=$(cat "$GCLIENT_ROOT/src/scripts/.default_board")
Chris Sosaacada732010-02-23 15:20:03 -0800255 fi
256}
257
258
Don Garrett640a0582010-05-04 16:54:28 -0700259# Enter a chroot and restart the current script if needed
260function restart_in_chroot_if_needed {
David Rochberg3b910702010-12-02 10:45:21 -0500261 # NB: Pass in ARGV: restart_in_chroot_if_needed "$@"
Greg Spencer798d75f2011-02-01 22:04:49 -0800262 if [ $INSIDE_CHROOT -ne 1 ]; then
Chris Sosafd2cdec2011-03-24 16:06:59 -0700263 # Get inside_chroot path for script.
264 local chroot_path="$(reinterpret_path_for_chroot "$0")"
Zdenek Behan2811c162011-08-13 00:47:38 +0200265 exec $GCLIENT_ROOT/chromite/bin/cros_sdk -- "$chroot_path" "$@"
Don Garrett640a0582010-05-04 16:54:28 -0700266 fi
267}
268
rspangler@google.comd74220d2009-10-09 20:56:14 +0000269# Fail unless we're inside the chroot. This guards against messing up your
270# workstation.
271function assert_inside_chroot {
Greg Spencer798d75f2011-02-01 22:04:49 -0800272 if [ $INSIDE_CHROOT -ne 1 ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000273 echo "This script must be run inside the chroot. Run this first:"
Zdenek Behan2811c162011-08-13 00:47:38 +0200274 echo " cros_sdk"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000275 exit 1
276 fi
277}
278
279# Fail if we're inside the chroot. This guards against creating or entering
280# nested chroots, among other potential problems.
281function assert_outside_chroot {
Greg Spencer798d75f2011-02-01 22:04:49 -0800282 if [ $INSIDE_CHROOT -ne 0 ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000283 echo "This script must be run outside the chroot."
284 exit 1
285 fi
286}
287
derat@google.com86dcc8e2009-11-21 19:49:49 +0000288function assert_not_root_user {
Greg Spencer798d75f2011-02-01 22:04:49 -0800289 if [ $(id -u) = 0 ]; then
derat@google.com86dcc8e2009-11-21 19:49:49 +0000290 echo "This script must be run as a non-root user."
291 exit 1
292 fi
293}
294
Luigi Semenzato1f82e122010-03-23 12:43:08 -0700295# Check that all arguments are flags; that is, there are no remaining arguments
296# after parsing from shflags. Allow (with a warning) a single empty-string
297# argument.
298#
299# TODO: fix buildbot so that it doesn't pass the empty-string parameter,
300# then change this function.
301#
302# Usage: check_flags_only_and_allow_null_arg "$@" && set --
303function check_flags_only_and_allow_null_arg {
304 do_shift=1
305 if [[ $# == 1 && -z "$@" ]]; then
306 echo "$0: warning: ignoring null argument" >&2
307 shift
308 do_shift=0
309 fi
310 if [[ $# -gt 0 ]]; then
311 echo "error: invalid arguments: \"$@\"" >&2
312 flags_help
313 exit 1
314 fi
315 return $do_shift
316}
317
Chris Sosad9798522010-06-16 14:29:50 -0700318function info {
Anton Staaf6adf1a32011-03-02 10:13:14 -0800319 echo -e >&2 "${V_BOLD_GREEN}INFO ${CROS_LOG_PREFIX:-""}: $1${V_VIDOFF}"
Will Drewry55b42c92010-10-20 15:44:11 -0500320}
Chris Sosad9798522010-06-16 14:29:50 -0700321
robotboy2ea03ac2010-02-11 15:30:55 -0800322function warn {
Anton Staaf6adf1a32011-03-02 10:13:14 -0800323 echo -e >&2 "${V_BOLD_YELLOW}WARNING ${CROS_LOG_PREFIX:-""}: $1${V_VIDOFF}"
robotboy2ea03ac2010-02-11 15:30:55 -0800324}
325
326function error {
Anton Staaf6adf1a32011-03-02 10:13:14 -0800327 echo -e >&2 "${V_BOLD_RED}ERROR ${CROS_LOG_PREFIX:-""}: $1${V_VIDOFF}"
robotboy2ea03ac2010-02-11 15:30:55 -0800328}
David James546747b2010-03-23 15:19:43 -0700329
330function die {
331 error "$1"
332 exit 1
333}
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700334
335# Retry an emerge command according to $FLAGS_retries
David Jamesfd7403a2010-07-09 12:00:17 -0700336# The $EMERGE_JOBS flags will only be added the first time the command is run
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700337function eretry () {
Greg Spencer798d75f2011-02-01 22:04:49 -0800338 local i
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700339 for i in $(seq $FLAGS_retries); do
Greg Spencer798d75f2011-02-01 22:04:49 -0800340 echo "Retrying $@"
341 "$@" $EMERGE_JOBS && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700342 done
Greg Spencer798d75f2011-02-01 22:04:49 -0800343 "$@" && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700344 return 1
345}
346
347# Removes single quotes around parameter
348# Arguments:
349# $1 - string which optionally has surrounding quotes
350# Returns:
351# None, but prints the string without quotes.
352function remove_quotes() {
353 echo "$1" | sed -e "s/^'//; s/'$//"
354}
tedbo373c3902010-04-12 10:52:40 -0700355
356# Writes stdin to the given file name as root using sudo in overwrite mode.
357#
358# $1 - The output file name.
359function sudo_clobber() {
360 sudo tee "$1" > /dev/null
361}
362
363# Writes stdin to the given file name as root using sudo in append mode.
364#
365# $1 - The output file name.
366function sudo_append() {
367 sudo tee -a "$1" > /dev/null
368}
robotboy98912212010-04-12 14:08:14 -0700369
Mike Frysinger1aa61242011-09-15 17:46:44 -0400370# Locate all mounts below a specified directory.
371#
372# $1 - The root tree.
373function sub_mounts() {
374 # Assume that `mount` outputs a list of mount points in the order
375 # that things were mounted (since it always has and hopefully always
376 # will). As such, we have to unmount in reverse order to cleanly
377 # unmount submounts (think /dev/pts and /dev).
378 mount | \
Mike Frysingerb2e897b2011-09-22 17:04:35 -0400379 awk -v path="$1" -v len="${#1}" \
380 '(substr($3, 1, len) == path) { print $3 }' | \
Mike Frysinger1aa61242011-09-15 17:46:44 -0400381 tac
382}
383
robotboy98912212010-04-12 14:08:14 -0700384# Unmounts a directory, if the unmount fails, warn, and then lazily unmount.
385#
386# $1 - The path to unmount.
Mike Frysinger1aa61242011-09-15 17:46:44 -0400387function safe_umount_tree {
388 local mounts=$(sub_mounts "$1")
robotboy98912212010-04-12 14:08:14 -0700389
Mike Frysingere8aec372011-09-21 00:03:22 -0400390 # Hmm, this shouldn't normally happen, but anything is possible.
391 if [ -z "${mounts}" ] ; then
392 return 0
393 fi
394
Mike Frysinger1aa61242011-09-15 17:46:44 -0400395 # First try to unmount in one shot to speed things up.
396 if sudo umount -d ${mounts}; then
397 return 0
398 fi
robotboy98912212010-04-12 14:08:14 -0700399
Mike Frysinger1aa61242011-09-15 17:46:44 -0400400 # Well that didn't work, so lazy unmount remaining ones.
401 mounts=$(sub_mounts "$1")
402 warn "Failed to unmount ${mounts}"
403 warn "Doing a lazy unmount"
404 if ! sudo umount -d -l ${mounts}; then
405 mounts=$(sub_mounts "$1")
406 die "Failed to lazily unmount ${mounts}"
robotboy98912212010-04-12 14:08:14 -0700407 fi
408}
Chris Sosa702618f2010-05-14 12:52:32 -0700409
Chris Sosad4455022010-05-20 10:14:06 -0700410# Fixes symlinks that are incorrectly prefixed with the build root ${1}
411# rather than the real running root '/'.
412# TODO(sosa) - Merge setup - cleanup below with this method.
413fix_broken_symlinks() {
414 local build_root="${1}"
415 local symlinks=$(find "${build_root}/usr/local" -lname "${build_root}/*")
Greg Spencer798d75f2011-02-01 22:04:49 -0800416 local symlink
Chris Sosad4455022010-05-20 10:14:06 -0700417 for symlink in ${symlinks}; do
418 echo "Fixing ${symlink}"
419 local target=$(ls -l "${symlink}" | cut -f 2 -d '>')
420 # Trim spaces from target (bashism).
421 target=${target/ /}
422 # Make new target (removes rootfs prefix).
423 new_target=$(echo ${target} | sed "s#${build_root}##")
424
425 echo "Fixing symlink ${symlink}"
426 sudo unlink "${symlink}"
427 sudo ln -sf "${new_target}" "${symlink}"
428 done
429}
430
Chris Sosa702618f2010-05-14 12:52:32 -0700431# Sets up symlinks for the developer root. It is necessary to symlink
432# usr and local since the developer root is mounted at /usr/local and
433# applications expect to be installed under /usr/local/bin, etc.
434# This avoids packages installing into /usr/local/usr/local/bin.
435# ${1} specifies the symlink target for the developer root.
436# ${2} specifies the symlink target for the var directory.
437# ${3} specifies the location of the stateful partition.
438setup_symlinks_on_root() {
439 # Give args better names.
440 local dev_image_target=${1}
441 local var_target=${2}
442 local dev_image_root="${3}/dev_image"
443
444 # If our var target is actually the standard var, we are cleaning up the
445 # symlinks (could also check for /usr/local for the dev_image_target).
446 if [ ${var_target} = "/var" ]; then
447 echo "Cleaning up /usr/local symlinks for ${dev_image_root}"
448 else
449 echo "Setting up symlinks for /usr/local for ${dev_image_root}"
450 fi
451
452 # Set up symlinks that should point to ${dev_image_target}.
Greg Spencer798d75f2011-02-01 22:04:49 -0800453 local path
Chris Sosa702618f2010-05-14 12:52:32 -0700454 for path in usr local; do
455 if [ -h "${dev_image_root}/${path}" ]; then
456 sudo unlink "${dev_image_root}/${path}"
457 elif [ -e "${dev_image_root}/${path}" ]; then
458 die "${dev_image_root}/${path} should be a symlink if exists"
459 fi
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400460 sudo ln -s "${dev_image_target}" "${dev_image_root}/${path}"
Chris Sosa702618f2010-05-14 12:52:32 -0700461 done
462
463 # Setup var symlink.
464 if [ -h "${dev_image_root}/var" ]; then
465 sudo unlink "${dev_image_root}/var"
466 elif [ -e "${dev_image_root}/var" ]; then
467 die "${dev_image_root}/var should be a symlink if it exists"
468 fi
469
470 sudo ln -s "${var_target}" "${dev_image_root}/var"
471}
Nick Sandersd2509272010-06-16 03:50:04 -0700472
Will Drewry55b42c92010-10-20 15:44:11 -0500473# These two helpers clobber the ro compat value in our root filesystem.
474#
475# When the system is built with --enable_rootfs_verification, bit-precise
476# integrity checking is performed. That precision poses a usability issue on
477# systems that automount partitions with recognizable filesystems, such as
478# ext2/3/4. When the filesystem is mounted 'rw', ext2 metadata will be
479# automatically updated even if no other writes are performed to the
480# filesystem. In addition, ext2+ does not support a "read-only" flag for a
481# given filesystem. That said, forward and backward compatibility of
482# filesystem features are supported by tracking if a new feature breaks r/w or
483# just write compatibility. We abuse the read-only compatibility flag[1] in
484# the filesystem header by setting the high order byte (le) to FF. This tells
485# the kernel that features R24-R31 are all enabled. Since those features are
486# undefined on all ext-based filesystem, all standard kernels will refuse to
487# mount the filesystem as read-write -- only read-only[2].
488#
489# [1] 32-bit flag we are modifying:
490# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=include/linux/ext2_fs.h#l417
491# [2] Mount behavior is enforced here:
492# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=fs/ext2/super.c#l857
493#
494# N.B., if the high order feature bits are used in the future, we will need to
495# revisit this technique.
496disable_rw_mount() {
497 local rootfs="$1"
498 local offset="${2-0}" # in bytes
Will Drewry9b7cb512010-10-20 18:11:24 -0500499 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
500 printf '\377' |
Will Drewry55b42c92010-10-20 15:44:11 -0500501 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \
502 conv=notrunc count=1 bs=1
503}
504
505enable_rw_mount() {
506 local rootfs="$1"
507 local offset="${2-0}"
Will Drewry9b7cb512010-10-20 18:11:24 -0500508 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
509 printf '\000' |
Will Drewry55b42c92010-10-20 15:44:11 -0500510 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \
511 conv=notrunc count=1 bs=1
512}
513
Nick Sandersd2509272010-06-16 03:50:04 -0700514# Get current timestamp. Assumes common.sh runs at startup.
515start_time=$(date +%s)
516
517# Print time elsapsed since start_time.
518print_time_elapsed() {
Greg Spencer798d75f2011-02-01 22:04:49 -0800519 local end_time=$(date +%s)
520 local elapsed_seconds=$(($end_time - $start_time))
521 local minutes=$(($elapsed_seconds / 60))
522 local seconds=$(($elapsed_seconds % 60))
Olof Johansson6d491382010-08-09 16:05:50 -0500523 echo "Elapsed time: ${minutes}m${seconds}s"
Nick Sandersd2509272010-06-16 03:50:04 -0700524}
Doug Anderson0c9e88d2010-10-19 14:49:39 -0700525
Anton Staaf9bcd8412011-01-24 15:27:14 -0800526# The board and variant command line options can be used in a number of ways
527# to specify the board and variant. The board can encode both pieces of
528# information separated by underscores. Or the variant can be passed using
529# the separate variant option. This function extracts the canonical board and
530# variant information and provides it in the BOARD, VARIANT and BOARD_VARIANT
531# variables.
532get_board_and_variant() {
533 local flags_board="${1}"
534 local flags_variant="${2}"
535
536 BOARD=$(echo "$flags_board" | cut -d '_' -f 1)
537 VARIANT=${flags_variant:-$(echo "$flags_board" | cut -s -d '_' -f 2)}
538
539 if [ -n "$VARIANT" ]; then
540 BOARD_VARIANT="${BOARD}_${VARIANT}"
541 else
542 BOARD_VARIANT="${BOARD}"
543 fi
544}
Simon Glass142ca062011-02-09 13:39:43 -0800545
546# This function converts a chromiumos image into a test image, either
547# in place or by copying to a new test image filename first. It honors
548# the following flags (see mod_image_for_test.sh)
549#
550# --factory
551# --factory_install
552# --force_copy
553#
554# On entry, pass the directory containing the image, and the image filename
555# On exit, it puts the pathname of the resulting test image into
556# CHROMEOS_RETURN_VAL
557# (yes this is ugly, but perhaps less ugly than the alternatives)
558#
559# Usage:
560# SRC_IMAGE=$(prepare_test_image "directory" "imagefile")
561prepare_test_image() {
562 # If we're asked to modify the image for test, then let's make a copy and
563 # modify that instead.
564 # Check for manufacturing image.
565 local args
566
567 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
568 args="--factory"
569 fi
570
571 # Check for install shim.
572 if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]; then
573 args="--factory_install"
574 fi
575
576 # Check for forcing copy of image
577 if [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ]; then
578 args="${args} --force_copy"
579 fi
580
581 # Modify the image for test, creating a new test image
582 "${SCRIPTS_DIR}/mod_image_for_test.sh" --board=${FLAGS_board} \
583 --image="$1/$2" --noinplace ${args}
584
585 # From now on we use the just-created test image
Simon Glass6e448ae2011-03-03 11:20:54 -0800586 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
587 CHROMEOS_RETURN_VAL="$1/${CHROMEOS_FACTORY_TEST_IMAGE_NAME}"
588 else
589 CHROMEOS_RETURN_VAL="$1/${CHROMEOS_TEST_IMAGE_NAME}"
590 fi
Simon Glass142ca062011-02-09 13:39:43 -0800591}
Anton Staaf6f5262d2011-03-02 09:35:54 -0800592
593# Check that the specified file exists. If the file path is empty or the file
594# doesn't exist on the filesystem generate useful error messages. Otherwise
595# show the user the name and path of the file that will be used. The padding
596# parameter can be used to tabulate multiple name:path pairs. For example:
597#
598# check_for_file "really long name" "...:" "file.foo"
599# check_for_file "short name" ".........:" "another.bar"
600#
601# Results in the following output:
602#
603# Using really long name...: file.foo
604# Using short name.........: another.bar
605#
606# If tabulation is not required then passing "" for padding generates the
607# output "Using <name> <path>"
608check_for_file() {
609 local name=$1
610 local padding=$2
611 local path=$3
612
613 if [ -z "${path}" ]; then
614 die "No ${name} file specified."
615 fi
616
617 if [ ! -e "${path}" ]; then
618 die "No ${name} file found at: ${path}"
619 else
620 info "Using ${name}${padding} ${path}"
621 fi
622}
623
624# Check that the specified tool exists. If it does not exist in the PATH
625# generate a useful error message indicating how to install the ebuild
626# that contains the required tool.
627check_for_tool() {
628 local tool=$1
629 local ebuild=$2
630
631 if ! which "${tool}" >/dev/null ; then
632 error "The ${tool} utility was not found in your path. Run the following"
633 error "command in your chroot to install it: sudo -E emerge ${ebuild}"
634 exit 1
635 fi
636}
Chris Sosafd2cdec2011-03-24 16:06:59 -0700637
638# Reinterprets path from outside the chroot for use inside.
639# Returns "" if "" given.
640# $1 - The path to reinterpret.
641function reinterpret_path_for_chroot() {
642 if [ $INSIDE_CHROOT -ne 1 ]; then
643 if [ -z "${1}" ]; then
644 echo ""
645 else
646 local path_abs_path=$(readlink -f "${1}")
647 local gclient_root_abs_path=$(readlink -f "${GCLIENT_ROOT}")
648
649 # Strip the repository root from the path.
650 local relative_path=$(echo ${path_abs_path} \
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400651 | sed "s:${gclient_root_abs_path}/::")
Chris Sosafd2cdec2011-03-24 16:06:59 -0700652
653 if [ "${relative_path}" = "${path_abs_path}" ]; then
654 die "Error reinterpreting path. Path ${1} is not within source tree."
655 fi
656
657 # Prepend the chroot repository path.
658 echo "/home/${USER}/trunk/${relative_path}"
659 fi
660 else
661 # Path is already inside the chroot :).
662 echo "${1}"
663 fi
664}
Gabe Black83d8b822011-08-01 17:50:09 -0700665
David James0ea96e42011-08-03 11:53:50 -0700666function emerge_custom_kernel() {
667 local install_root="$1"
668 local root=${FLAGS_build_root}/${FLAGS_board}
669 local tmp_pkgdir=${root}/custom-packages
670
671 # Clean up any leftover state in custom directories.
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400672 sudo rm -rf "${tmp_pkgdir}"
David James0ea96e42011-08-03 11:53:50 -0700673
674 # Update chromeos-initramfs to contain the latest binaries from the build
675 # tree. This is basically just packaging up already-built binaries from
676 # $root. We are careful not to muck with the existing prebuilts so that
677 # prebuilts can be uploaded in parallel.
678 # TODO(davidjames): Implement ABI deps so that chromeos-initramfs will be
679 # rebuilt automatically when its dependencies change.
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400680 sudo -E PKGDIR="${tmp_pkgdir}" $EMERGE_BOARD_CMD -1 \
David James0ea96e42011-08-03 11:53:50 -0700681 chromeos-base/chromeos-initramfs || die "Cannot emerge chromeos-initramfs"
682
683 # Verify all dependencies of the kernel are installed. This should be a
684 # no-op, but it's good to check in case a developer didn't run
685 # build_packages.
686 local kernel=$(portageq-${FLAGS_board} expand_virtual ${root} virtual/kernel)
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400687 sudo -E PKGDIR="${tmp_pkgdir}" $EMERGE_BOARD_CMD --onlydeps \
David James0ea96e42011-08-03 11:53:50 -0700688 ${kernel} || die "Cannot emerge kernel dependencies"
689
690 # Build the kernel. This uses the standard root so that we can pick up the
691 # initramfs from there. But we don't actually install the kernel to the
692 # standard root, because that'll muck up the kernel debug symbols there,
693 # which we want to upload in parallel.
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400694 sudo -E PKGDIR="${tmp_pkgdir}" $EMERGE_BOARD_CMD --buildpkgonly \
David James0ea96e42011-08-03 11:53:50 -0700695 ${kernel} || die "Cannot emerge kernel"
696
697 # Install the custom kernel to the provided install root.
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400698 sudo -E PKGDIR="${tmp_pkgdir}" $EMERGE_BOARD_CMD --usepkgonly \
David James0ea96e42011-08-03 11:53:50 -0700699 --root=${install_root} ${kernel} || die "Cannot emerge kernel to root"
700}