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/build_packages b/build_packages
index beee931..e82b663 100755
--- a/build_packages
+++ b/build_packages
@@ -67,6 +67,8 @@
 # not needed for the typical developer workflow.
 DEFINE_string accept_licenses "" \
   "Licenses to append to the accept list."
+DEFINE_boolean eclean "${FLAGS_TRUE}" \
+  "Run eclean to delete old binpkgs."
 DEFINE_integer jobs -1 \
   "How many packages to build in parallel at maximum."
 DEFINE_boolean norebuild "${FLAGS_FALSE}" \
@@ -290,6 +292,13 @@
 # in parallel.
 info_run "${EMERGE_CMD[@]}" --regen --quiet
 
+# Clean out any stale binpkgs we've accumulated. This is done immediately after
+# regenerating the cache in case ebuilds have been removed (e.g. from a revert).
+if [[ "${FLAGS_eclean}" -eq "${FLAGS_TRUE}" ]]; then
+  info "Cleaning stale binpkgs"
+  info_run "eclean-${FLAGS_board}" packages
+fi
+
 # Verify that all packages can be emerged from scratch, without any
 # backtracking. Only print the output if this step fails.
 info "Checking package dependencies are correct: ${PACKAGES[*]}"
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
diff --git a/update_chroot b/update_chroot
index dc47dce..84152ea 100755
--- a/update_chroot
+++ b/update_chroot
@@ -31,8 +31,7 @@
   "Don't update the toolchains."
 DEFINE_string toolchain_boards "" \
   "Extra toolchains to setup for the specified boards."
-DEFINE_boolean eclean ${FLAGS_TRUE} \
-  "Run eclean to delete old binpkgs after updating the chroot."
+DEFINE_boolean eclean "${FLAGS_TRUE}" "Run eclean to delete old binpkgs."
 
 # Parse command line flags
 FLAGS "$@" || exit 1
@@ -96,6 +95,13 @@
 # in parallel.
 info_run "${EMERGE_CMD[@]}" --regen --quiet
 
+# Clean out any stale binpkgs we've accumulated. This is done immediately after
+# regenerating the cache in case ebuilds have been removed (e.g. from a revert).
+if [[ "${FLAGS_eclean}" -eq "${FLAGS_TRUE}" ]]; then
+  info "Cleaning stale binpkgs"
+  info_run sudo eclean packages
+fi
+
 info "Updating the SDK"
 
 EMERGE_FLAGS=( -uNv --with-bdeps=y )
@@ -148,9 +154,10 @@
 # If the user still has old perl modules installed, update them.
 "${SCRIPTS_DIR}/build_library/perl_rebuild.sh"
 
-# Finally clean out any stale binpkgs we've accumulated.
-if [[ ${FLAGS_eclean} -eq ${FLAGS_TRUE} ]]; then
-  info "Cleaning stale binpkgs"
+# Deep clean any stale binpkgs. This includes any binary packages that do not
+# correspond to a currently installed package (different versions are kept).
+if [[ "${FLAGS_eclean}" -eq "${FLAGS_TRUE}" ]]; then
+  info "Deep cleaning stale binpkgs"
   info_run sudo eclean -d packages
 fi