blob: 9a469aea8a8860cee7ac7a46bdb005a29eea28bb [file] [log] [blame]
Mike Frysingera263b4d2020-12-04 16:34:10 -05001#!/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. /usr/share/misc/shflags || exit 1
11
12GCLIENT_ROOT="/mnt/host/source"
13DEFAULT_BOARD="$(cat "${GCLIENT_ROOT}"/src/scripts/.default_board 2>/dev/null)"
14
15info() { echo "INFO: $*"; }
16warn() { echo "WARN: $*"; }
17error() { echo "ERROR: $*"; }
18die() { error "$@"; exit 1; }
19
20DEFINE_string board "${DEFAULT_BOARD}" \
21 "Board for which to build the package."
22DEFINE_boolean test "${FLAGS_FALSE}" \
23 "Compile and run tests as well."
24DEFINE_boolean reconf "${FLAGS_FALSE}" \
25 "Re-run configure and prepare steps."
26DEFINE_boolean install "${FLAGS_FALSE}" \
27 "Incrementally build and install your package."
28DEFINE_boolean scrub "${FLAGS_FALSE}" \
29 "Blow away all in-tree files not managed by git."
30
31set -e
32# Parse command line.
33FLAGS "$@" || exit 1
34eval set -- "${FLAGS_ARGV}"
35
36if [ $# -lt 1 ]; then
37 echo "Usage: ${0} [OPTIONS] <package (read: ebuild) basename> [target args]"
38 exit 1
39fi
40
41if [ -z "${FLAGS_board}" ]; then
42 die "--board is required"
43fi
44
45if [ -n "${FLAGS_board}" ]; then
46 EBUILDCMD=ebuild-"${FLAGS_board}"
47 EMERGECMD=emerge-"${FLAGS_board}"
48 EQUERYCMD=equery-"${FLAGS_board}"
49 BOARD="${FLAGS_board}"
50fi
51
52pkg="${1}"
53shift
54if [ "${pkg}" = "." ]; then
55 if ! pkg=$(git config workon.pkg); then
56 die "workon.pkg not set in git config for this project"
57 fi
58fi
59
60unstable_suffix="9999"
61workon_name="${pkg}-${unstable_suffix}"
62pkgfile=
63
64# Find the ebuild file, ensure the caller is workon'ing the package.
65if ! pkgfile=$("${EQUERYCMD}" which "${workon_name}" 2> /dev/null); then
66 BOARD_KEYWORD="$(portageq-${FLAGS_board} envvar ARCH)"
67 if ACCEPT_KEYWORDS="~${BOARD_KEYWORD}" "${EQUERYCMD}" which "${workon_name}" \
68 > /dev/null 2>&1; then
69 die "run 'cros_workon --board ${BOARD} start ${pkg}' first!" 1>&2
70 fi
71 die "error looking up package ${pkg}"
72fi
73
74if [ "${FLAGS_scrub}" = "${FLAGS_TRUE}" ]; then
75 warn "--scrub will destroy ALL FILES unknown to git!"
76 read -p "Are you sure you want to do this? [y|N]" resp
77 if egrep -qi "^y(es)?$" <(echo -n "${resp}"); then
78 eval $(${EBUILDCMD} $(${EQUERYCMD} which ${workon_name}) info)
79 srcdir=$(readlink -m ${CROS_WORKON_SRCDIR})
80 project_path=${srcdir#${GCLIENT_ROOT}/}
81 if ! (cd "${GCLIENT_ROOT}/${project_path}" && git clean -dxf); then
82 die "Could not scrub source directory"
83 fi
84 else
85 info "Not scrubbing; exiting gracefully"
86 fi
87 exit 0
88fi
89
90# Find the portage work directory for this package.
91workpath=$(\
92 echo "${pkgfile}" | \
93 awk -F '/' '{ print $(NF-2) "/" $(NF-1) }')-"${unstable_suffix}"
94workpath="/build/${BOARD}/tmp/portage/${workpath}"
95
96# Export vars that the ebuild env needs from us.
97export SANDBOX_WRITE=~/trunk
98export CROS_WORKON_INPLACE=1
99export CROS_WORKON_MAKE_COMPILE_ARGS="$*"
100
101# The ebuild commands we run rely on portage automatically running earlier
102# phases for us. Append in case there is something already in the env.
103FEATURES+=" -noauto"
104export FEATURES
105
106# Vars that we want to pass through for the user.
107PASS_THROUGH_VARS=(
108 # cros-workon.eclass vars.
109 CROS_WORKON_MAKE_COMPILE_ARGS
110 # Common test vars.
111 GTEST_ARGS
112 # Platform eclass vars.
113 P2_TEST_FILTER
114 P2_VMODULE
115)
116
117# Determine if we're going to do tests, set up commands appropriately.
118to_do="compile"
119if [ "${FLAGS_test}" = "${FLAGS_TRUE}" ]; then
120 to_do="test"
121 FEATURES+=" test"
122 rm -f "${workpath}/.tested"
123fi
124
125workdir="${workpath}/work/${workon_name}"
126if [ ! -h "${workdir}" ]; then
127 warn "Cleaning up stale workdir: ${workdir}"
128 FLAGS_reconf="${FLAGS_TRUE}" # To force symlinking in the user's src dir.
129fi
130
131if [ "${FLAGS_install}" = "${FLAGS_TRUE}" ]; then
132 exec "${EMERGECMD}" --nodeps "${pkg}"
133fi
134
135clean=
136if [ "${FLAGS_reconf}" = "${FLAGS_TRUE}" ]; then
137 clean="clean"
138else
139 rm -f "${workpath}/.compiled"
140 envf="${workpath}/temp/environment"
141 for v in ${PASS_THROUGH_VARS[@]}; do
142 # We delete it independently in case the var wasn't set initially.
143 sed -i -e "/^declare .. ${v}=/d" "${envf}"
144 printf 'declare -x %s="%s"\n' "${v}" "${!v}" >> "${envf}"
145 done
146fi
147exec "${EBUILDCMD}" "${pkgfile}" ${clean} "${to_do}"