Mike Frysinger | a263b4d | 2020-12-04 16:34:10 -0500 | [diff] [blame] | 1 | #!/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 | |
| 12 | GCLIENT_ROOT="/mnt/host/source" |
| 13 | DEFAULT_BOARD="$(cat "${GCLIENT_ROOT}"/src/scripts/.default_board 2>/dev/null)" |
| 14 | |
| 15 | info() { echo "INFO: $*"; } |
| 16 | warn() { echo "WARN: $*"; } |
| 17 | error() { echo "ERROR: $*"; } |
| 18 | die() { error "$@"; exit 1; } |
| 19 | |
| 20 | DEFINE_string board "${DEFAULT_BOARD}" \ |
| 21 | "Board for which to build the package." |
| 22 | DEFINE_boolean test "${FLAGS_FALSE}" \ |
| 23 | "Compile and run tests as well." |
| 24 | DEFINE_boolean reconf "${FLAGS_FALSE}" \ |
| 25 | "Re-run configure and prepare steps." |
| 26 | DEFINE_boolean install "${FLAGS_FALSE}" \ |
| 27 | "Incrementally build and install your package." |
| 28 | DEFINE_boolean scrub "${FLAGS_FALSE}" \ |
| 29 | "Blow away all in-tree files not managed by git." |
| 30 | |
| 31 | set -e |
| 32 | # Parse command line. |
| 33 | FLAGS "$@" || exit 1 |
| 34 | eval set -- "${FLAGS_ARGV}" |
| 35 | |
| 36 | if [ $# -lt 1 ]; then |
| 37 | echo "Usage: ${0} [OPTIONS] <package (read: ebuild) basename> [target args]" |
| 38 | exit 1 |
| 39 | fi |
| 40 | |
| 41 | if [ -z "${FLAGS_board}" ]; then |
| 42 | die "--board is required" |
| 43 | fi |
| 44 | |
| 45 | if [ -n "${FLAGS_board}" ]; then |
| 46 | EBUILDCMD=ebuild-"${FLAGS_board}" |
| 47 | EMERGECMD=emerge-"${FLAGS_board}" |
| 48 | EQUERYCMD=equery-"${FLAGS_board}" |
| 49 | BOARD="${FLAGS_board}" |
| 50 | fi |
| 51 | |
| 52 | pkg="${1}" |
| 53 | shift |
| 54 | if [ "${pkg}" = "." ]; then |
| 55 | if ! pkg=$(git config workon.pkg); then |
| 56 | die "workon.pkg not set in git config for this project" |
| 57 | fi |
| 58 | fi |
| 59 | |
| 60 | unstable_suffix="9999" |
| 61 | workon_name="${pkg}-${unstable_suffix}" |
| 62 | pkgfile= |
| 63 | |
| 64 | # Find the ebuild file, ensure the caller is workon'ing the package. |
| 65 | if ! 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}" |
| 72 | fi |
| 73 | |
| 74 | if [ "${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 |
| 88 | fi |
| 89 | |
| 90 | # Find the portage work directory for this package. |
| 91 | workpath=$(\ |
| 92 | echo "${pkgfile}" | \ |
| 93 | awk -F '/' '{ print $(NF-2) "/" $(NF-1) }')-"${unstable_suffix}" |
| 94 | workpath="/build/${BOARD}/tmp/portage/${workpath}" |
| 95 | |
| 96 | # Export vars that the ebuild env needs from us. |
| 97 | export SANDBOX_WRITE=~/trunk |
| 98 | export CROS_WORKON_INPLACE=1 |
| 99 | export 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. |
| 103 | FEATURES+=" -noauto" |
| 104 | export FEATURES |
| 105 | |
| 106 | # Vars that we want to pass through for the user. |
| 107 | PASS_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. |
| 118 | to_do="compile" |
| 119 | if [ "${FLAGS_test}" = "${FLAGS_TRUE}" ]; then |
| 120 | to_do="test" |
| 121 | FEATURES+=" test" |
| 122 | rm -f "${workpath}/.tested" |
| 123 | fi |
| 124 | |
| 125 | workdir="${workpath}/work/${workon_name}" |
| 126 | if [ ! -h "${workdir}" ]; then |
| 127 | warn "Cleaning up stale workdir: ${workdir}" |
| 128 | FLAGS_reconf="${FLAGS_TRUE}" # To force symlinking in the user's src dir. |
| 129 | fi |
| 130 | |
| 131 | if [ "${FLAGS_install}" = "${FLAGS_TRUE}" ]; then |
| 132 | exec "${EMERGECMD}" --nodeps "${pkg}" |
| 133 | fi |
| 134 | |
| 135 | clean= |
| 136 | if [ "${FLAGS_reconf}" = "${FLAGS_TRUE}" ]; then |
| 137 | clean="clean" |
| 138 | else |
| 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 |
| 146 | fi |
| 147 | exec "${EBUILDCMD}" "${pkgfile}" ${clean} "${to_do}" |