cros_workon_make: move here
To help consolidate, move this script out of platform/dev/host/.
We're largely turning down that directory, and this script is
better here with the other remaining build shell scripts.
BUG=None
TEST=None
Change-Id: I734ef610e508a1f8ca3e1e362ae631d832b6902e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosutils/+/2574666
Reviewed-by: Alex Klein <saklein@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Nicolas Boichat <drinkcat@chromium.org>
diff --git a/cros_workon_make b/cros_workon_make
new file mode 100755
index 0000000..9a469ae
--- /dev/null
+++ b/cros_workon_make
@@ -0,0 +1,147 @@
+#!/bin/bash
+
+# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+#
+# Simple wrapper script to build a cros_workon package incrementally.
+# You must already be cros_workon'ing the package in question.
+
+. /usr/share/misc/shflags || exit 1
+
+GCLIENT_ROOT="/mnt/host/source"
+DEFAULT_BOARD="$(cat "${GCLIENT_ROOT}"/src/scripts/.default_board 2>/dev/null)"
+
+info() { echo "INFO: $*"; }
+warn() { echo "WARN: $*"; }
+error() { echo "ERROR: $*"; }
+die() { error "$@"; exit 1; }
+
+DEFINE_string board "${DEFAULT_BOARD}" \
+ "Board for which to build the package."
+DEFINE_boolean test "${FLAGS_FALSE}" \
+ "Compile and run tests as well."
+DEFINE_boolean reconf "${FLAGS_FALSE}" \
+ "Re-run configure and prepare steps."
+DEFINE_boolean install "${FLAGS_FALSE}" \
+ "Incrementally build and install your package."
+DEFINE_boolean scrub "${FLAGS_FALSE}" \
+ "Blow away all in-tree files not managed by git."
+
+set -e
+# Parse command line.
+FLAGS "$@" || exit 1
+eval set -- "${FLAGS_ARGV}"
+
+if [ $# -lt 1 ]; then
+ echo "Usage: ${0} [OPTIONS] <package (read: ebuild) basename> [target args]"
+ exit 1
+fi
+
+if [ -z "${FLAGS_board}" ]; then
+ die "--board is required"
+fi
+
+if [ -n "${FLAGS_board}" ]; then
+ EBUILDCMD=ebuild-"${FLAGS_board}"
+ EMERGECMD=emerge-"${FLAGS_board}"
+ EQUERYCMD=equery-"${FLAGS_board}"
+ BOARD="${FLAGS_board}"
+fi
+
+pkg="${1}"
+shift
+if [ "${pkg}" = "." ]; then
+ if ! pkg=$(git config workon.pkg); then
+ die "workon.pkg not set in git config for this project"
+ fi
+fi
+
+unstable_suffix="9999"
+workon_name="${pkg}-${unstable_suffix}"
+pkgfile=
+
+# Find the ebuild file, ensure the caller is workon'ing the package.
+if ! pkgfile=$("${EQUERYCMD}" which "${workon_name}" 2> /dev/null); then
+ BOARD_KEYWORD="$(portageq-${FLAGS_board} envvar ARCH)"
+ if ACCEPT_KEYWORDS="~${BOARD_KEYWORD}" "${EQUERYCMD}" which "${workon_name}" \
+ > /dev/null 2>&1; then
+ die "run 'cros_workon --board ${BOARD} start ${pkg}' first!" 1>&2
+ fi
+ die "error looking up package ${pkg}"
+fi
+
+if [ "${FLAGS_scrub}" = "${FLAGS_TRUE}" ]; then
+ warn "--scrub will destroy ALL FILES unknown to git!"
+ read -p "Are you sure you want to do this? [y|N]" resp
+ if egrep -qi "^y(es)?$" <(echo -n "${resp}"); then
+ eval $(${EBUILDCMD} $(${EQUERYCMD} which ${workon_name}) info)
+ srcdir=$(readlink -m ${CROS_WORKON_SRCDIR})
+ project_path=${srcdir#${GCLIENT_ROOT}/}
+ if ! (cd "${GCLIENT_ROOT}/${project_path}" && git clean -dxf); then
+ die "Could not scrub source directory"
+ fi
+ else
+ info "Not scrubbing; exiting gracefully"
+ fi
+ exit 0
+fi
+
+# Find the portage work directory for this package.
+workpath=$(\
+ echo "${pkgfile}" | \
+ awk -F '/' '{ print $(NF-2) "/" $(NF-1) }')-"${unstable_suffix}"
+workpath="/build/${BOARD}/tmp/portage/${workpath}"
+
+# Export vars that the ebuild env needs from us.
+export SANDBOX_WRITE=~/trunk
+export CROS_WORKON_INPLACE=1
+export CROS_WORKON_MAKE_COMPILE_ARGS="$*"
+
+# The ebuild commands we run rely on portage automatically running earlier
+# phases for us. Append in case there is something already in the env.
+FEATURES+=" -noauto"
+export FEATURES
+
+# Vars that we want to pass through for the user.
+PASS_THROUGH_VARS=(
+ # cros-workon.eclass vars.
+ CROS_WORKON_MAKE_COMPILE_ARGS
+ # Common test vars.
+ GTEST_ARGS
+ # Platform eclass vars.
+ P2_TEST_FILTER
+ P2_VMODULE
+)
+
+# Determine if we're going to do tests, set up commands appropriately.
+to_do="compile"
+if [ "${FLAGS_test}" = "${FLAGS_TRUE}" ]; then
+ to_do="test"
+ FEATURES+=" test"
+ rm -f "${workpath}/.tested"
+fi
+
+workdir="${workpath}/work/${workon_name}"
+if [ ! -h "${workdir}" ]; then
+ warn "Cleaning up stale workdir: ${workdir}"
+ FLAGS_reconf="${FLAGS_TRUE}" # To force symlinking in the user's src dir.
+fi
+
+if [ "${FLAGS_install}" = "${FLAGS_TRUE}" ]; then
+ exec "${EMERGECMD}" --nodeps "${pkg}"
+fi
+
+clean=
+if [ "${FLAGS_reconf}" = "${FLAGS_TRUE}" ]; then
+ clean="clean"
+else
+ rm -f "${workpath}/.compiled"
+ envf="${workpath}/temp/environment"
+ for v in ${PASS_THROUGH_VARS[@]}; do
+ # We delete it independently in case the var wasn't set initially.
+ sed -i -e "/^declare .. ${v}=/d" "${envf}"
+ printf 'declare -x %s="%s"\n' "${v}" "${!v}" >> "${envf}"
+ done
+fi
+exec "${EBUILDCMD}" "${pkgfile}" ${clean} "${to_do}"