blob: 5258c1a6f854ddb00d2ffe5ebc0a60154cf61da8 [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
Alexey Marinichev63c42aa2009-12-21 11:42:39 -080015NUM_JOBS=`grep -c "^processor" /proc/cpuinfo`
rspangler@google.comd74220d2009-10-09 20:56:14 +000016
17# Store location of the calling script.
18TOP_SCRIPT_DIR="${TOP_SCRIPT_DIR:-$(dirname $0)}"
19
20# Find root of source tree
21if [ "x$GCLIENT_ROOT" != "x" ]
22then
23 # GCLIENT_ROOT already set, so we're done
24 true
25elif [ "x$COMMON_SH" != "x" ]
26then
27 # COMMON_SH set, so assume that's us
28 GCLIENT_ROOT="$(dirname "$COMMON_SH")/../.."
29elif [ "x$BASH_SOURCE" != "x" ]
30then
31 # Using bash, so we can find ourselves
32 GCLIENT_ROOT="$(dirname "$BASH_SOURCE")/../.."
33else
tedbo4f44d9e2010-01-08 17:26:11 -080034 # Using dash or sh, we don't know where we are. $0 refers to the calling
rspangler@google.comd74220d2009-10-09 20:56:14 +000035 # 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
44fi
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.
tedbo4f44d9e2010-01-08 17:26:11 -080048# This is better than just using
rspangler@google.comd74220d2009-10-09 20:56:14 +000049# FOO = "$(cd $FOO ; pwd)"
tedbo4f44d9e2010-01-08 17:26:11 -080050# since that leaves symbolic links intact.
rspangler@google.comd74220d2009-10-09 20:56:14 +000051# Note that 'realpath' is equivalent to 'readlink -f'.
52TOP_SCRIPT_DIR=`readlink -f $TOP_SCRIPT_DIR`
53GCLIENT_ROOT=`readlink -f $GCLIENT_ROOT`
54
55# Other directories should always be pathed down from GCLIENT_ROOT.
56SRC_ROOT="$GCLIENT_ROOT/src"
57SRC_INTERNAL="$GCLIENT_ROOT/src-internal"
58SCRIPTS_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_'
63CHROMEOS_DEV_SETTINGS="${CHROMEOS_DEV_SETTINGS:-$SCRIPTS_DIR/.chromeos_dev}"
64if [ -f $CHROMEOS_DEV_SETTINGS ]
65then
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"
75fi
76
77# Load shflags
Anush Elangovan2c7d1962010-07-13 15:24:54 -070078. "${SRC_ROOT}/scripts/lib/shflags/shflags"
rspangler@google.comd74220d2009-10-09 20:56:14 +000079
Bill Richardson10d27c22010-01-20 13:38:50 -080080# Our local mirror
81DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"}
rspangler@google.comd74220d2009-10-09 20:56:14 +000082
Bill Richardson10d27c22010-01-20 13:38:50 -080083# 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.comd74220d2009-10-09 20:56:14 +000086
Bill Richardson10d27c22010-01-20 13:38:50 -080087DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
88DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"}
89
90DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
91DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"}
rspangler@google.comd74220d2009-10-09 20:56:14 +000092
93# Default location for chroot
94DEFAULT_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.
98DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"$SRC_ROOT/build"}
99
David McMahon49302942010-02-18 16:55:35 -0800100# Set up a global ALL_BOARDS value
Sam Lefflera92ecd62010-02-23 08:25:38 -0800101ALL_BOARDS=$(cd $SRC_ROOT/overlays;ls -1d overlay-* 2>&-|sed 's,overlay-,,g')
David McMahon49302942010-02-18 16:55:35 -0800102# Strip CR
103ALL_BOARDS=$(echo $ALL_BOARDS)
104# Set a default BOARD
105#DEFAULT_BOARD=x86-generic # or...
106DEFAULT_BOARD=$(echo $ALL_BOARDS | awk '{print $NF}')
107
David James03668642010-07-28 17:08:29 -0700108# Enable --fast by default on non-official builds
109DEFAULT_FAST="${FLAGS_TRUE}"
110if [ "${CHROMEOS_OFFICIAL:-0}" = "1" ]; then
111 DEFAULT_FAST="${FLAGS_FALSE}"
112fi
113
rspangler@google.comd74220d2009-10-09 20:56:14 +0000114# Detect whether we're inside a chroot or not
115if [ -e /etc/debian_chroot ]
116then
117 INSIDE_CHROOT=1
118else
119 INSIDE_CHROOT=0
120fi
121
122# Directory locations inside the dev chroot
123CHROOT_TRUNK_DIR="/home/$USER/trunk"
124
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700125# Install make for portage ebuilds. Used by build_image and gmergefs.
Chris Sosa3d9a10b2010-04-13 15:00:46 -0700126DEFAULT_INSTALL_MASK="/usr/include /usr/man /usr/share/man /usr/share/doc \
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700127 /usr/share/gtk-doc /usr/share/gtk-2.0 /usr/lib/gtk-2.0/include \
128 /usr/share/info /usr/share/aclocal /usr/lib/gcc /usr/lib/pkgconfig \
129 /usr/share/pkgconfig /usr/share/gettext /usr/share/readline /etc/runlevels \
Amol Kher9d2a7982010-05-19 17:30:29 -0700130 /usr/share/openrc /lib/rc *.a *.la /etc/init.d /usr/lib/debug"
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700131
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800132FACTORY_INSTALL_MASK="/opt/google/chrome /opt/google/o3d /opt/netscape \
133 /usr/lib/dri /usr/lib/python2.6/test /usr/share/chewing /usr/share/fonts \
134 /usr/share/ibus-pinyin /usr/share/libhangul /usr/share/locale \
135 /usr/share/m17n /usr/share/mime /usr/share/sounds /usr/share/tts \
Amol Kher9d2a7982010-05-19 17:30:29 -0700136 /usr/share/X11 /usr/share/zoneinfo /usr/lib/debug"
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800137
David McMahonb7eb3a22010-02-09 14:07:40 -0800138# Check to ensure not running old scripts
139V_REVERSE=''
140V_VIDOFF=''
141case "$(basename $0)" in
142 build_image.sh|build_platform_packages.sh|customize_rootfs.sh|make_chroot.sh)
143 echo
144 echo "$V_REVERSE============================================================"
145 echo "=========================== WARNING ======================"
146 echo "============================================================$V_VIDOFF"
147 echo
148 echo "RUNNING OLD BUILD SYSTEM SCRIPTS. RUN THE PORTAGE-BASED BUILD HERE:"
149 echo "http://www.chromium.org/chromium-os/building-chromium-os/portage-based-build"
150 echo
151 if [ "$USER" != "chrome-bot" ]
152 then
153 read -n1 -p "Press any key to continue using the OLD build system..."
154 echo
155 echo
156 fi
157 ;;
158esac
159
rspangler@google.comd74220d2009-10-09 20:56:14 +0000160# -----------------------------------------------------------------------------
161# Functions
162
Chris Sosaacada732010-02-23 15:20:03 -0800163function setup_board_warning {
tedbo373c3902010-04-12 10:52:40 -0700164 echo
165 echo "$V_REVERSE================= WARNING ======================$V_VIDOFF"
Chris Sosaacada732010-02-23 15:20:03 -0800166 echo
167 echo "*** No default board detected in " \
168 "$GCLIENT_ROOT/src/scripts/.default_board"
169 echo "*** Either run setup_board with default flag set"
170 echo "*** or echo |board_name| > $GCLIENT_ROOT/src/scripts/.default_board"
171 echo
172}
173
174
175# Sets the default board variable for calling script
176function get_default_board {
tedbo373c3902010-04-12 10:52:40 -0700177 DEFAULT_BOARD=
178
Chris Sosaacada732010-02-23 15:20:03 -0800179 if [ -f "$GCLIENT_ROOT/src/scripts/.default_board" ] ; then
180 DEFAULT_BOARD=`cat "$GCLIENT_ROOT/src/scripts/.default_board"`
181 fi
182}
183
184
rspangler@google.comd74220d2009-10-09 20:56:14 +0000185# Make a package
186function make_pkg_common {
187 # Positional parameters from calling script. :? means "fail if unset".
188 set -e
189 PKG_BASE=${1:?}
190 shift
191 set +e
tedbo4f44d9e2010-01-08 17:26:11 -0800192
rspangler@google.comd74220d2009-10-09 20:56:14 +0000193 # All packages are built in the chroot
194 assert_inside_chroot
195
196 # Command line options
197 DEFINE_string build_root "$DEFAULT_BUILD_ROOT" "Root of build output"
198
199 # Parse command line and update positional args
200 FLAGS "$@" || exit 1
201 eval set -- "${FLAGS_ARGV}"
202
203 # Die on any errors
204 set -e
205
206 # Make output dir
207 OUT_DIR="$FLAGS_build_root/x86/local_packages"
208 mkdir -p "$OUT_DIR"
209
210 # Remove previous package from output dir
211 rm -f "$OUT_DIR"/${PKG_BASE}_*.deb
212
213 # Rebuild the package
214 pushd "$TOP_SCRIPT_DIR"
215 rm -f ../${PKG_BASE}_*.deb
216 dpkg-buildpackage -b -tc -us -uc -j$NUM_JOBS
217 mv ../${PKG_BASE}_*.deb "$OUT_DIR"
218 rm ../${PKG_BASE}_*.changes
219 popd
220}
221
Don Garrett640a0582010-05-04 16:54:28 -0700222# Enter a chroot and restart the current script if needed
223function restart_in_chroot_if_needed {
224 if [ $INSIDE_CHROOT -ne 1 ]
225 then
226 # Equivalent to enter_chroot.sh -- <current command>
227 exec $SCRIPTS_DIR/enter_chroot.sh -- \
228 $CHROOT_TRUNK_DIR/src/scripts/$(basename $0) $*
229 fi
230}
231
rspangler@google.comd74220d2009-10-09 20:56:14 +0000232# Fail unless we're inside the chroot. This guards against messing up your
233# workstation.
234function assert_inside_chroot {
235 if [ $INSIDE_CHROOT -ne 1 ]
236 then
237 echo "This script must be run inside the chroot. Run this first:"
238 echo " $SCRIPTS_DIR/enter_chroot.sh"
239 exit 1
240 fi
241}
242
243# Fail if we're inside the chroot. This guards against creating or entering
244# nested chroots, among other potential problems.
245function assert_outside_chroot {
246 if [ $INSIDE_CHROOT -ne 0 ]
247 then
248 echo "This script must be run outside the chroot."
249 exit 1
250 fi
251}
252
derat@google.com86dcc8e2009-11-21 19:49:49 +0000253function assert_not_root_user {
254 if [ `id -u` = 0 ]; then
255 echo "This script must be run as a non-root user."
256 exit 1
257 fi
258}
259
rspangler@google.comd74220d2009-10-09 20:56:14 +0000260# Install a package if it's not already installed
261function install_if_missing {
262 # Positional parameters from calling script. :? means "fail if unset".
263 PKG_NAME=${1:?}
264 shift
265
266 if [ -z `which $PKG_NAME` ]
267 then
268 echo "Can't find $PKG_NAME; attempting to install it."
269 sudo apt-get --yes --force-yes install $PKG_NAME
270 fi
271}
Steve VanDeBogart8174ba02010-01-15 19:45:30 -0800272
273# Returns true if the input file is whitelisted.
274#
275# $1 - The file to check
276is_whitelisted() {
David McMahonb7eb3a22010-02-09 14:07:40 -0800277 local file=$1
Steve VanDeBogart8174ba02010-01-15 19:45:30 -0800278 local whitelist="$FLAGS_whitelist"
279 test -f "$whitelist" || (echo "Whitelist file missing ($whitelist)" && exit 1)
280
281 local checksum=$(md5sum "$file" | awk '{ print $1 }')
282 local count=$(sed -e "s/#.*$//" "${whitelist}" | grep -c "$checksum" \
283 || /bin/true)
284 test $count -ne 0
285}
robotboy2ea03ac2010-02-11 15:30:55 -0800286
Luigi Semenzato1f82e122010-03-23 12:43:08 -0700287# Check that all arguments are flags; that is, there are no remaining arguments
288# after parsing from shflags. Allow (with a warning) a single empty-string
289# argument.
290#
291# TODO: fix buildbot so that it doesn't pass the empty-string parameter,
292# then change this function.
293#
294# Usage: check_flags_only_and_allow_null_arg "$@" && set --
295function check_flags_only_and_allow_null_arg {
296 do_shift=1
297 if [[ $# == 1 && -z "$@" ]]; then
298 echo "$0: warning: ignoring null argument" >&2
299 shift
300 do_shift=0
301 fi
302 if [[ $# -gt 0 ]]; then
303 echo "error: invalid arguments: \"$@\"" >&2
304 flags_help
305 exit 1
306 fi
307 return $do_shift
308}
309
robotboy2ea03ac2010-02-11 15:30:55 -0800310V_RED="\e[31m"
311V_YELLOW="\e[33m"
Chris Sosad9798522010-06-16 14:29:50 -0700312V_BOLD_GREEN="\e[1;32m"
Darin Petkovb2a21e72010-03-25 11:33:41 -0700313V_BOLD_RED="\e[1;31m"
314V_BOLD_YELLOW="\e[1;33m"
robotboy2ea03ac2010-02-11 15:30:55 -0800315
Chris Sosad9798522010-06-16 14:29:50 -0700316function info {
317 echo -e "${V_BOLD_GREEN}INFO : $1${V_VIDOFF}"
318}
319
robotboy2ea03ac2010-02-11 15:30:55 -0800320function warn {
Darin Petkovb2a21e72010-03-25 11:33:41 -0700321 echo -e "${V_BOLD_YELLOW}WARNING: $1${V_VIDOFF}"
robotboy2ea03ac2010-02-11 15:30:55 -0800322}
323
324function error {
Darin Petkovb2a21e72010-03-25 11:33:41 -0700325 echo -e "${V_BOLD_RED}ERROR : $1${V_VIDOFF}"
robotboy2ea03ac2010-02-11 15:30:55 -0800326}
David James546747b2010-03-23 15:19:43 -0700327
328function die {
329 error "$1"
330 exit 1
331}
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700332
333# Retry an emerge command according to $FLAGS_retries
David Jamesfd7403a2010-07-09 12:00:17 -0700334# The $EMERGE_JOBS flags will only be added the first time the command is run
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700335function eretry () {
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700336 local i=
337 for i in $(seq $FLAGS_retries); do
338 echo Retrying $*
David Jamesfd7403a2010-07-09 12:00:17 -0700339 $* $EMERGE_JOBS && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700340 done
David Jamesfd7403a2010-07-09 12:00:17 -0700341 $* && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700342 return 1
343}
344
345# Removes single quotes around parameter
346# Arguments:
347# $1 - string which optionally has surrounding quotes
348# Returns:
349# None, but prints the string without quotes.
350function remove_quotes() {
351 echo "$1" | sed -e "s/^'//; s/'$//"
352}
tedbo373c3902010-04-12 10:52:40 -0700353
354# Writes stdin to the given file name as root using sudo in overwrite mode.
355#
356# $1 - The output file name.
357function sudo_clobber() {
358 sudo tee "$1" > /dev/null
359}
360
361# Writes stdin to the given file name as root using sudo in append mode.
362#
363# $1 - The output file name.
364function sudo_append() {
365 sudo tee -a "$1" > /dev/null
366}
robotboy98912212010-04-12 14:08:14 -0700367
368# Unmounts a directory, if the unmount fails, warn, and then lazily unmount.
369#
370# $1 - The path to unmount.
371function safe_umount {
372 path=${1:?}
373 shift
374
375 if ! sudo umount -d "${path}"; then
376 warn "Failed to unmount ${path}"
377 warn "Doing a lazy unmount"
378
379 sudo umount -d -l "${path}" || die "Failed to lazily unmount ${path}"
380 fi
381}
Chris Sosa702618f2010-05-14 12:52:32 -0700382
Chris Sosad4455022010-05-20 10:14:06 -0700383# Fixes symlinks that are incorrectly prefixed with the build root ${1}
384# rather than the real running root '/'.
385# TODO(sosa) - Merge setup - cleanup below with this method.
386fix_broken_symlinks() {
387 local build_root="${1}"
388 local symlinks=$(find "${build_root}/usr/local" -lname "${build_root}/*")
389 for symlink in ${symlinks}; do
390 echo "Fixing ${symlink}"
391 local target=$(ls -l "${symlink}" | cut -f 2 -d '>')
392 # Trim spaces from target (bashism).
393 target=${target/ /}
394 # Make new target (removes rootfs prefix).
395 new_target=$(echo ${target} | sed "s#${build_root}##")
396
397 echo "Fixing symlink ${symlink}"
398 sudo unlink "${symlink}"
399 sudo ln -sf "${new_target}" "${symlink}"
400 done
401}
402
Chris Sosa702618f2010-05-14 12:52:32 -0700403# Sets up symlinks for the developer root. It is necessary to symlink
404# usr and local since the developer root is mounted at /usr/local and
405# applications expect to be installed under /usr/local/bin, etc.
406# This avoids packages installing into /usr/local/usr/local/bin.
407# ${1} specifies the symlink target for the developer root.
408# ${2} specifies the symlink target for the var directory.
409# ${3} specifies the location of the stateful partition.
410setup_symlinks_on_root() {
411 # Give args better names.
412 local dev_image_target=${1}
413 local var_target=${2}
414 local dev_image_root="${3}/dev_image"
415
416 # If our var target is actually the standard var, we are cleaning up the
417 # symlinks (could also check for /usr/local for the dev_image_target).
418 if [ ${var_target} = "/var" ]; then
419 echo "Cleaning up /usr/local symlinks for ${dev_image_root}"
420 else
421 echo "Setting up symlinks for /usr/local for ${dev_image_root}"
422 fi
423
424 # Set up symlinks that should point to ${dev_image_target}.
425 for path in usr local; do
426 if [ -h "${dev_image_root}/${path}" ]; then
427 sudo unlink "${dev_image_root}/${path}"
428 elif [ -e "${dev_image_root}/${path}" ]; then
429 die "${dev_image_root}/${path} should be a symlink if exists"
430 fi
431 sudo ln -s ${dev_image_target} "${dev_image_root}/${path}"
432 done
433
434 # Setup var symlink.
435 if [ -h "${dev_image_root}/var" ]; then
436 sudo unlink "${dev_image_root}/var"
437 elif [ -e "${dev_image_root}/var" ]; then
438 die "${dev_image_root}/var should be a symlink if it exists"
439 fi
440
441 sudo ln -s "${var_target}" "${dev_image_root}/var"
442}
Nick Sandersd2509272010-06-16 03:50:04 -0700443
444# Get current timestamp. Assumes common.sh runs at startup.
445start_time=$(date +%s)
446
447# Print time elsapsed since start_time.
448print_time_elapsed() {
449 end_time=$(date +%s)
450 elapsed_seconds="$(( $end_time - $start_time ))"
451 minutes="$(( $elapsed_seconds / 60 ))"
452 seconds="$(( $elapsed_seconds % 60 ))"
453 echo "Elapsed time: ${minutes}:${seconds}"
454}