blob: a0599f0402e0cb9e234cc1fedf64d37cd7e90472 [file] [log] [blame]
Mandeep Singh Bainese8c337d2011-03-24 21:03:491#!/bin/bash
2
3# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6#
7# Simple wrapper script to build a cros_workon package incrementally.
8# You must already be cros_workon'ing the package in question.
9
10# --- BEGIN COMMON.SH BOILERPLATE ---
11# Load common CrOS utilities. Inside the chroot this file is installed in
12# /usr/lib/crosutils. Outside the chroot we find it relative to the script's
13# location.
14find_common_sh() {
15 local common_paths=(/usr/lib/crosutils "$(dirname "$(readlink -f "$0")")/..")
16 local path
17
18 SCRIPT_ROOT=
19 for path in "${common_paths[@]}"; do
20 if [ -r "${path}/common.sh" ]; then
21 SCRIPT_ROOT=${path}
22 break
23 fi
24 done
25}
26
27find_common_sh
28. "${SCRIPT_ROOT}/common.sh" || (echo "Unable to load common.sh" && exit 1)
29# --- END COMMON.SH BOILERPLATE ---
30
31# Script must be run inside the chroot.
32restart_in_chroot_if_needed "$@"
33
34get_default_board
35
36DEFINE_string board "${DEFAULT_BOARD}" \
37 "Board for which to build the package."
38DEFINE_boolean test "${FLAGS_FALSE}" \
39 "Compile and run tests as well."
40DEFINE_boolean reconf "${FLAGS_FALSE}" \
41 "Re-run configure and prepare steps."
42DEFINE_boolean install "${FLAGS_FALSE}" \
43 "Incrementally build and install your package."
44DEFINE_boolean scrub "${FLAGS_FALSE}" \
45 "Blow away all in-tree files not managed by git."
46
47set -e
48# Parse command line.
49FLAGS "$@" || exit 1
50eval set -- "${FLAGS_ARGV}"
51
52if [ $# -lt 1 ]; then
53 echo "Usage: ${0} [OPTIONS] <package (read: ebuild) basename>"
54 exit 1
55fi
56
57if [ -n "${FLAGS_board}" ]; then
58 BOARD_DIR=/build/"${FLAGS_board}"
59 EBUILDCMD=ebuild-"${FLAGS_board}"
60 EMERGECMD=emerge-"${FLAGS_board}"
61 EQUERYCMD=equery-"${FLAGS_board}"
62 BOARD_STR="${FLAGS_board}"
63 BOARD_KEYWORD="$(portageq-${FLAGS_board} envvar ARCH)"
64fi
65
66unstable_suffix="9999"
67workon_name="${1}-${unstable_suffix}"
68pkgfile=
69workpath=
70
71if ! pkgfile=$("${EQUERYCMD}" which "${workon_name}" 2> /dev/null); then
72 if ACCEPT_KEYWORDS="~${BOARD_KEYWORD}" "${EQUERYCMD}" which "${workon_name}" \
73 > /dev/null 2>&1; then
74 die "run 'cros_workon --board ${BOARD_STR} start ${1}' first!" 1>&2
75 fi
76 die "error looking up package $1"
77fi
78
79if [ "${FLAGS_scrub}" = "${FLAGS_TRUE}" ]; then
80 warn "--scrub will destroy ALL FILES unknown to git!"
81 read -p "Are you sure you want to do this? [y|N]" resp
82 if egrep -qi "^y(es)?$" <(echo -n "${resp}"); then
83 eval $(${EBUILDCMD} $(${EQUERYCMD} which ${workon_name}) info)
84 srcdir=$(readlink -m ${CROS_WORKON_SRCDIR})
85 trunkdir=$(readlink -m ${CHROOT_TRUNK_DIR})
86 project_path=${srcdir#${trunkdir}/}
87 if ! (cd "${GCLIENT_ROOT}/${project_path}" && git clean -xf); then
88 die "Could not scrub source directory"
89 fi
90 else
91 info "Not scrubbing; exiting gracefully"
92 fi
93 exit 0
94fi
95
96workpath=$(\
97 echo "${pkgfile}" | \
98 awk -F '/' '{ print $(NF-2) "/" $(NF-1) }')-"${unstable_suffix}"
99
100emerge_features=
101to_do="compile"
102if [ "${FLAGS_test}" = "${FLAGS_TRUE}" ]; then
103 to_do="test"
104 emerge_features="test"
105fi
106if [ "${FLAGS_install}" = "${FLAGS_TRUE}" ]; then
107 SANDBOX_WRITE=~/trunk CROS_WORKON_INPLACE=1 \
108 FEATURES="${emerge_features} ${FEATURES}" "${EMERGECMD}" "${1}"
109 exit $?
110fi
111
112clean=
113if [ "${FLAGS_reconf}" = "${FLAGS_TRUE}" ]; then
114 clean="clean"
115else
116 rm -f "/build/${BOARD_STR}/tmp/portage/${workpath}/.compiled"
117fi
118SANDBOX_WRITE=~/trunk CROS_WORKON_INPLACE=1 \
119 "${EBUILDCMD}" "${pkgfile}" ${clean} "${to_do}"