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 | |
David McMahon | b7eb3a2 | 2010-02-09 14:07:40 -0800 | [diff] [blame] | 119 | # Check to ensure not running old scripts |
| 120 | V_REVERSE='[7m' |
| 121 | V_VIDOFF='[m' |
| 122 | case "$(basename $0)" in |
| 123 | build_image.sh|build_platform_packages.sh|customize_rootfs.sh|make_chroot.sh) |
| 124 | echo |
| 125 | echo "$V_REVERSE============================================================" |
| 126 | echo "=========================== WARNING ======================" |
| 127 | echo "============================================================$V_VIDOFF" |
| 128 | echo |
| 129 | echo "RUNNING OLD BUILD SYSTEM SCRIPTS. RUN THE PORTAGE-BASED BUILD HERE:" |
| 130 | echo "http://www.chromium.org/chromium-os/building-chromium-os/portage-based-build" |
| 131 | echo |
| 132 | if [ "$USER" != "chrome-bot" ] |
| 133 | then |
| 134 | read -n1 -p "Press any key to continue using the OLD build system..." |
| 135 | echo |
| 136 | echo |
| 137 | fi |
| 138 | ;; |
| 139 | esac |
| 140 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 141 | # ----------------------------------------------------------------------------- |
| 142 | # Functions |
| 143 | |
| 144 | # Make a package |
| 145 | function make_pkg_common { |
| 146 | # Positional parameters from calling script. :? means "fail if unset". |
| 147 | set -e |
| 148 | PKG_BASE=${1:?} |
| 149 | shift |
| 150 | set +e |
tedbo | 4f44d9e | 2010-01-08 17:26:11 -0800 | [diff] [blame] | 151 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 152 | # All packages are built in the chroot |
| 153 | assert_inside_chroot |
| 154 | |
| 155 | # Command line options |
| 156 | DEFINE_string build_root "$DEFAULT_BUILD_ROOT" "Root of build output" |
| 157 | |
| 158 | # Parse command line and update positional args |
| 159 | FLAGS "$@" || exit 1 |
| 160 | eval set -- "${FLAGS_ARGV}" |
| 161 | |
| 162 | # Die on any errors |
| 163 | set -e |
| 164 | |
| 165 | # Make output dir |
| 166 | OUT_DIR="$FLAGS_build_root/x86/local_packages" |
| 167 | mkdir -p "$OUT_DIR" |
| 168 | |
| 169 | # Remove previous package from output dir |
| 170 | rm -f "$OUT_DIR"/${PKG_BASE}_*.deb |
| 171 | |
| 172 | # Rebuild the package |
| 173 | pushd "$TOP_SCRIPT_DIR" |
| 174 | rm -f ../${PKG_BASE}_*.deb |
| 175 | dpkg-buildpackage -b -tc -us -uc -j$NUM_JOBS |
| 176 | mv ../${PKG_BASE}_*.deb "$OUT_DIR" |
| 177 | rm ../${PKG_BASE}_*.changes |
| 178 | popd |
| 179 | } |
| 180 | |
| 181 | # Fail unless we're inside the chroot. This guards against messing up your |
| 182 | # workstation. |
| 183 | function assert_inside_chroot { |
| 184 | if [ $INSIDE_CHROOT -ne 1 ] |
| 185 | then |
| 186 | echo "This script must be run inside the chroot. Run this first:" |
| 187 | echo " $SCRIPTS_DIR/enter_chroot.sh" |
| 188 | exit 1 |
| 189 | fi |
| 190 | } |
| 191 | |
| 192 | # Fail if we're inside the chroot. This guards against creating or entering |
| 193 | # nested chroots, among other potential problems. |
| 194 | function assert_outside_chroot { |
| 195 | if [ $INSIDE_CHROOT -ne 0 ] |
| 196 | then |
| 197 | echo "This script must be run outside the chroot." |
| 198 | exit 1 |
| 199 | fi |
| 200 | } |
| 201 | |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 202 | function assert_not_root_user { |
| 203 | if [ `id -u` = 0 ]; then |
| 204 | echo "This script must be run as a non-root user." |
| 205 | exit 1 |
| 206 | fi |
| 207 | } |
| 208 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 209 | # Install a package if it's not already installed |
| 210 | function install_if_missing { |
| 211 | # Positional parameters from calling script. :? means "fail if unset". |
| 212 | PKG_NAME=${1:?} |
| 213 | shift |
| 214 | |
| 215 | if [ -z `which $PKG_NAME` ] |
| 216 | then |
| 217 | echo "Can't find $PKG_NAME; attempting to install it." |
| 218 | sudo apt-get --yes --force-yes install $PKG_NAME |
| 219 | fi |
| 220 | } |
Steve VanDeBogart | 8174ba0 | 2010-01-15 19:45:30 -0800 | [diff] [blame] | 221 | |
| 222 | # Returns true if the input file is whitelisted. |
| 223 | # |
| 224 | # $1 - The file to check |
| 225 | is_whitelisted() { |
David McMahon | b7eb3a2 | 2010-02-09 14:07:40 -0800 | [diff] [blame] | 226 | local file=$1 |
Steve VanDeBogart | 8174ba0 | 2010-01-15 19:45:30 -0800 | [diff] [blame] | 227 | local whitelist="$FLAGS_whitelist" |
| 228 | test -f "$whitelist" || (echo "Whitelist file missing ($whitelist)" && exit 1) |
| 229 | |
| 230 | local checksum=$(md5sum "$file" | awk '{ print $1 }') |
| 231 | local count=$(sed -e "s/#.*$//" "${whitelist}" | grep -c "$checksum" \ |
| 232 | || /bin/true) |
| 233 | test $count -ne 0 |
| 234 | } |
robotboy | 2ea03ac | 2010-02-11 15:30:55 -0800 | [diff] [blame] | 235 | |
| 236 | V_RED="\e[31m" |
| 237 | V_YELLOW="\e[33m" |
| 238 | |
| 239 | function warn { |
| 240 | echo -e "${V_YELLOW}Warning..: $1${V_VIDOFF}" |
| 241 | } |
| 242 | |
| 243 | function error { |
| 244 | echo -e "${V_RED}Error....: $1${V_VIDOFF}" |
| 245 | } |