Run eclean by default to clear out binary packages in case of a revert.

This adds eclean early in make_chroot, update_chroot, and
build_packages to clear out binary packages that correspond to ebuilds
that have been removed or reverted.

BUG=chromium:1202194
TEST=CQ passes

Change-Id: Ie519aa547bb63743375d4fd9b83d1b11f0ef11ff
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosutils/+/2848595
Tested-by: Allen Webb <allenwebb@google.com>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Allen Webb <allenwebb@google.com>
diff --git a/sdk_lib/make_chroot.sh b/sdk_lib/make_chroot.sh
index 95e6328..6f9888a 100755
--- a/sdk_lib/make_chroot.sh
+++ b/sdk_lib/make_chroot.sh
@@ -43,6 +43,7 @@
   "Use the stage3 located on this path."
 DEFINE_string cache_dir "" "Directory to store caches within."
 DEFINE_boolean useimage $FLAGS_FALSE "Mount the chroot on a loopback image."
+DEFINE_boolean eclean "${FLAGS_TRUE}" "Run eclean to delete old binpkgs."
 
 # Parse command line flags.
 FLAGS_HELP="usage: $SCRIPT_NAME [flags]"
@@ -558,6 +559,14 @@
 # Add version of stage3 for update checks.
 echo "STAGE3=${FLAGS_stage3_path}" > "${CHROOT_STATE}"
 
+# Clean out any stale binpkgs that might be in a warm cache. This is done
+# immediately after unpacking the tarball in case ebuilds have been removed
+# (e.g. from a revert).
+if [[ "${FLAGS_eclean}" -eq "${FLAGS_TRUE}" ]]; then
+  info "Cleaning stale binpkgs"
+  early_enter_chroot eclean packages
+fi
+
 # Switch SDK python to Python 3 by default.
 early_enter_chroot eselect python update
 
@@ -608,7 +617,7 @@
 # Update chroot.
 # Skip toolchain update because it already happened above, and the chroot is
 # not ready to emerge all cross toolchains.
-UPDATE_ARGS=( --skip_toolchain_update )
+UPDATE_ARGS=( --skip_toolchain_update --noeclean )
 if [[ "${FLAGS_usepkg}" == "${FLAGS_TRUE}" ]]; then
   UPDATE_ARGS+=( --usepkg )
 else