blob: accb468aaf15fe79ec422adc0b3aaf0d7aa98cfd [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
78. "$SRC_ROOT/third_party/shflags/files/src/shflags"
79
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
rspangler@google.comd74220d2009-10-09 20:56:14 +0000108# Detect whether we're inside a chroot or not
109if [ -e /etc/debian_chroot ]
110then
111 INSIDE_CHROOT=1
112else
113 INSIDE_CHROOT=0
114fi
115
116# Directory locations inside the dev chroot
117CHROOT_TRUNK_DIR="/home/$USER/trunk"
118
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700119# Install make for portage ebuilds. Used by build_image and gmergefs.
Chris Sosa3d9a10b2010-04-13 15:00:46 -0700120DEFAULT_INSTALL_MASK="/usr/include /usr/man /usr/share/man /usr/share/doc \
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700121 /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 \
124 /usr/share/openrc /lib/rc *.a *.la"
125
David McMahonb7eb3a22010-02-09 14:07:40 -0800126# Check to ensure not running old scripts
127V_REVERSE=''
128V_VIDOFF=''
129case "$(basename $0)" in
130 build_image.sh|build_platform_packages.sh|customize_rootfs.sh|make_chroot.sh)
131 echo
132 echo "$V_REVERSE============================================================"
133 echo "=========================== WARNING ======================"
134 echo "============================================================$V_VIDOFF"
135 echo
136 echo "RUNNING OLD BUILD SYSTEM SCRIPTS. RUN THE PORTAGE-BASED BUILD HERE:"
137 echo "http://www.chromium.org/chromium-os/building-chromium-os/portage-based-build"
138 echo
139 if [ "$USER" != "chrome-bot" ]
140 then
141 read -n1 -p "Press any key to continue using the OLD build system..."
142 echo
143 echo
144 fi
145 ;;
146esac
147
rspangler@google.comd74220d2009-10-09 20:56:14 +0000148# -----------------------------------------------------------------------------
149# Functions
150
Chris Sosaacada732010-02-23 15:20:03 -0800151function setup_board_warning {
tedbo373c3902010-04-12 10:52:40 -0700152 echo
153 echo "$V_REVERSE================= WARNING ======================$V_VIDOFF"
Chris Sosaacada732010-02-23 15:20:03 -0800154 echo
155 echo "*** No default board detected in " \
156 "$GCLIENT_ROOT/src/scripts/.default_board"
157 echo "*** Either run setup_board with default flag set"
158 echo "*** or echo |board_name| > $GCLIENT_ROOT/src/scripts/.default_board"
159 echo
160}
161
162
163# Sets the default board variable for calling script
164function get_default_board {
tedbo373c3902010-04-12 10:52:40 -0700165 DEFAULT_BOARD=
166
Chris Sosaacada732010-02-23 15:20:03 -0800167 if [ -f "$GCLIENT_ROOT/src/scripts/.default_board" ] ; then
168 DEFAULT_BOARD=`cat "$GCLIENT_ROOT/src/scripts/.default_board"`
169 fi
170}
171
172
rspangler@google.comd74220d2009-10-09 20:56:14 +0000173# Make a package
174function make_pkg_common {
175 # Positional parameters from calling script. :? means "fail if unset".
176 set -e
177 PKG_BASE=${1:?}
178 shift
179 set +e
tedbo4f44d9e2010-01-08 17:26:11 -0800180
rspangler@google.comd74220d2009-10-09 20:56:14 +0000181 # All packages are built in the chroot
182 assert_inside_chroot
183
184 # Command line options
185 DEFINE_string build_root "$DEFAULT_BUILD_ROOT" "Root of build output"
186
187 # Parse command line and update positional args
188 FLAGS "$@" || exit 1
189 eval set -- "${FLAGS_ARGV}"
190
191 # Die on any errors
192 set -e
193
194 # Make output dir
195 OUT_DIR="$FLAGS_build_root/x86/local_packages"
196 mkdir -p "$OUT_DIR"
197
198 # Remove previous package from output dir
199 rm -f "$OUT_DIR"/${PKG_BASE}_*.deb
200
201 # Rebuild the package
202 pushd "$TOP_SCRIPT_DIR"
203 rm -f ../${PKG_BASE}_*.deb
204 dpkg-buildpackage -b -tc -us -uc -j$NUM_JOBS
205 mv ../${PKG_BASE}_*.deb "$OUT_DIR"
206 rm ../${PKG_BASE}_*.changes
207 popd
208}
209
Don Garrett640a0582010-05-04 16:54:28 -0700210# Enter a chroot and restart the current script if needed
211function restart_in_chroot_if_needed {
212 if [ $INSIDE_CHROOT -ne 1 ]
213 then
214 # Equivalent to enter_chroot.sh -- <current command>
215 exec $SCRIPTS_DIR/enter_chroot.sh -- \
216 $CHROOT_TRUNK_DIR/src/scripts/$(basename $0) $*
217 fi
218}
219
rspangler@google.comd74220d2009-10-09 20:56:14 +0000220# Fail unless we're inside the chroot. This guards against messing up your
221# workstation.
222function assert_inside_chroot {
223 if [ $INSIDE_CHROOT -ne 1 ]
224 then
225 echo "This script must be run inside the chroot. Run this first:"
226 echo " $SCRIPTS_DIR/enter_chroot.sh"
227 exit 1
228 fi
229}
230
231# Fail if we're inside the chroot. This guards against creating or entering
232# nested chroots, among other potential problems.
233function assert_outside_chroot {
234 if [ $INSIDE_CHROOT -ne 0 ]
235 then
236 echo "This script must be run outside the chroot."
237 exit 1
238 fi
239}
240
derat@google.com86dcc8e2009-11-21 19:49:49 +0000241function assert_not_root_user {
242 if [ `id -u` = 0 ]; then
243 echo "This script must be run as a non-root user."
244 exit 1
245 fi
246}
247
rspangler@google.comd74220d2009-10-09 20:56:14 +0000248# Install a package if it's not already installed
249function install_if_missing {
250 # Positional parameters from calling script. :? means "fail if unset".
251 PKG_NAME=${1:?}
252 shift
253
254 if [ -z `which $PKG_NAME` ]
255 then
256 echo "Can't find $PKG_NAME; attempting to install it."
257 sudo apt-get --yes --force-yes install $PKG_NAME
258 fi
259}
Steve VanDeBogart8174ba02010-01-15 19:45:30 -0800260
261# Returns true if the input file is whitelisted.
262#
263# $1 - The file to check
264is_whitelisted() {
David McMahonb7eb3a22010-02-09 14:07:40 -0800265 local file=$1
Steve VanDeBogart8174ba02010-01-15 19:45:30 -0800266 local whitelist="$FLAGS_whitelist"
267 test -f "$whitelist" || (echo "Whitelist file missing ($whitelist)" && exit 1)
268
269 local checksum=$(md5sum "$file" | awk '{ print $1 }')
270 local count=$(sed -e "s/#.*$//" "${whitelist}" | grep -c "$checksum" \
271 || /bin/true)
272 test $count -ne 0
273}
robotboy2ea03ac2010-02-11 15:30:55 -0800274
Luigi Semenzato1f82e122010-03-23 12:43:08 -0700275# Check that all arguments are flags; that is, there are no remaining arguments
276# after parsing from shflags. Allow (with a warning) a single empty-string
277# argument.
278#
279# TODO: fix buildbot so that it doesn't pass the empty-string parameter,
280# then change this function.
281#
282# Usage: check_flags_only_and_allow_null_arg "$@" && set --
283function check_flags_only_and_allow_null_arg {
284 do_shift=1
285 if [[ $# == 1 && -z "$@" ]]; then
286 echo "$0: warning: ignoring null argument" >&2
287 shift
288 do_shift=0
289 fi
290 if [[ $# -gt 0 ]]; then
291 echo "error: invalid arguments: \"$@\"" >&2
292 flags_help
293 exit 1
294 fi
295 return $do_shift
296}
297
robotboy2ea03ac2010-02-11 15:30:55 -0800298V_RED="\e[31m"
299V_YELLOW="\e[33m"
Darin Petkovb2a21e72010-03-25 11:33:41 -0700300V_BOLD_RED="\e[1;31m"
301V_BOLD_YELLOW="\e[1;33m"
robotboy2ea03ac2010-02-11 15:30:55 -0800302
303function warn {
Darin Petkovb2a21e72010-03-25 11:33:41 -0700304 echo -e "${V_BOLD_YELLOW}WARNING: $1${V_VIDOFF}"
robotboy2ea03ac2010-02-11 15:30:55 -0800305}
306
307function error {
Darin Petkovb2a21e72010-03-25 11:33:41 -0700308 echo -e "${V_BOLD_RED}ERROR : $1${V_VIDOFF}"
robotboy2ea03ac2010-02-11 15:30:55 -0800309}
David James546747b2010-03-23 15:19:43 -0700310
311function die {
312 error "$1"
313 exit 1
314}
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700315
316# Retry an emerge command according to $FLAGS_retries
317# The $EMERGE_JOBS flags will only be added the first time the command is run
318function eretry () {
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700319 local i=
320 for i in $(seq $FLAGS_retries); do
321 echo Retrying $*
Nasser Grainawicb5f3eb2010-04-21 08:46:49 -0600322 $* $EMERGE_JOBS && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700323 done
Nasser Grainawicb5f3eb2010-04-21 08:46:49 -0600324 $* && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700325 return 1
326}
327
328# Removes single quotes around parameter
329# Arguments:
330# $1 - string which optionally has surrounding quotes
331# Returns:
332# None, but prints the string without quotes.
333function remove_quotes() {
334 echo "$1" | sed -e "s/^'//; s/'$//"
335}
tedbo373c3902010-04-12 10:52:40 -0700336
337# Writes stdin to the given file name as root using sudo in overwrite mode.
338#
339# $1 - The output file name.
340function sudo_clobber() {
341 sudo tee "$1" > /dev/null
342}
343
344# Writes stdin to the given file name as root using sudo in append mode.
345#
346# $1 - The output file name.
347function sudo_append() {
348 sudo tee -a "$1" > /dev/null
349}
robotboy98912212010-04-12 14:08:14 -0700350
351# Unmounts a directory, if the unmount fails, warn, and then lazily unmount.
352#
353# $1 - The path to unmount.
354function safe_umount {
355 path=${1:?}
356 shift
357
358 if ! sudo umount -d "${path}"; then
359 warn "Failed to unmount ${path}"
360 warn "Doing a lazy unmount"
361
362 sudo umount -d -l "${path}" || die "Failed to lazily unmount ${path}"
363 fi
364}