blob: 2a3c5d25a5cc66e7e24601746efc6e6cc2169dae [file] [log] [blame]
rspangler@google.comd74220d2009-10-09 20:56:14 +00001# Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
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 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
80# Mirrors and build suites come in 3 flavors
81# EXT - external source used to build local package repository
82# DEV - development chroot, from local package repository
83# IMG - bootable image, from local package repository
84
85# Build suites
86DEFAULT_EXT_SUITE=${CHROMEOS_EXT_SUITE:-"chromeos_dev"}
87DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"chromeos_dev"}
88DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"chromeos"}
89
90# Package repositories (mirrors)
91DEFAULT_EXT_MIRROR=${CHROMEOS_EXT_MIRROR:-"http://build.chromium.org/buildbot/packages"}
92DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"file://$GCLIENT_ROOT/repo/apt"}
tedbo4f44d9e2010-01-08 17:26:11 -080093DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"copy:///home/$USER/trunk/repo/apt"}
rspangler@google.comd74220d2009-10-09 20:56:14 +000094
95# Default location for chroot
96DEFAULT_CHROOT_DIR=${CHROMEOS_CHROOT_DIR:-"$GCLIENT_ROOT/chroot"}
97
98# All output files from build should go under $DEFAULT_BUILD_ROOT, so that
99# they don't pollute the source directory.
100DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"$SRC_ROOT/build"}
101
102# Detect whether we're inside a chroot or not
103if [ -e /etc/debian_chroot ]
104then
105 INSIDE_CHROOT=1
106else
107 INSIDE_CHROOT=0
108fi
109
110# Directory locations inside the dev chroot
111CHROOT_TRUNK_DIR="/home/$USER/trunk"
112
113# -----------------------------------------------------------------------------
114# Functions
115
116# Make a package
117function make_pkg_common {
118 # Positional parameters from calling script. :? means "fail if unset".
119 set -e
120 PKG_BASE=${1:?}
121 shift
122 set +e
tedbo4f44d9e2010-01-08 17:26:11 -0800123
rspangler@google.comd74220d2009-10-09 20:56:14 +0000124 # All packages are built in the chroot
125 assert_inside_chroot
126
127 # Command line options
128 DEFINE_string build_root "$DEFAULT_BUILD_ROOT" "Root of build output"
129
130 # Parse command line and update positional args
131 FLAGS "$@" || exit 1
132 eval set -- "${FLAGS_ARGV}"
133
134 # Die on any errors
135 set -e
136
137 # Make output dir
138 OUT_DIR="$FLAGS_build_root/x86/local_packages"
139 mkdir -p "$OUT_DIR"
140
141 # Remove previous package from output dir
142 rm -f "$OUT_DIR"/${PKG_BASE}_*.deb
143
144 # Rebuild the package
145 pushd "$TOP_SCRIPT_DIR"
146 rm -f ../${PKG_BASE}_*.deb
147 dpkg-buildpackage -b -tc -us -uc -j$NUM_JOBS
148 mv ../${PKG_BASE}_*.deb "$OUT_DIR"
149 rm ../${PKG_BASE}_*.changes
150 popd
151}
152
153# Fail unless we're inside the chroot. This guards against messing up your
154# workstation.
155function assert_inside_chroot {
156 if [ $INSIDE_CHROOT -ne 1 ]
157 then
158 echo "This script must be run inside the chroot. Run this first:"
159 echo " $SCRIPTS_DIR/enter_chroot.sh"
160 exit 1
161 fi
162}
163
164# Fail if we're inside the chroot. This guards against creating or entering
165# nested chroots, among other potential problems.
166function assert_outside_chroot {
167 if [ $INSIDE_CHROOT -ne 0 ]
168 then
169 echo "This script must be run outside the chroot."
170 exit 1
171 fi
172}
173
derat@google.com86dcc8e2009-11-21 19:49:49 +0000174function assert_not_root_user {
175 if [ `id -u` = 0 ]; then
176 echo "This script must be run as a non-root user."
177 exit 1
178 fi
179}
180
rspangler@google.comd74220d2009-10-09 20:56:14 +0000181# Install a package if it's not already installed
182function install_if_missing {
183 # Positional parameters from calling script. :? means "fail if unset".
184 PKG_NAME=${1:?}
185 shift
186
187 if [ -z `which $PKG_NAME` ]
188 then
189 echo "Can't find $PKG_NAME; attempting to install it."
190 sudo apt-get --yes --force-yes install $PKG_NAME
191 fi
192}
Steve VanDeBogart8174ba02010-01-15 19:45:30 -0800193
194# Returns true if the input file is whitelisted.
195#
196# $1 - The file to check
197is_whitelisted() {
198 local file=$1
199 local whitelist="$FLAGS_whitelist"
200 test -f "$whitelist" || (echo "Whitelist file missing ($whitelist)" && exit 1)
201
202 local checksum=$(md5sum "$file" | awk '{ print $1 }')
203 local count=$(sed -e "s/#.*$//" "${whitelist}" | grep -c "$checksum" \
204 || /bin/true)
205 test $count -ne 0
206}