chroot_version_hooks.d: Address unit_test breakage in CQ from caches.

We need to make sure all the packages that are rebuilt because of the
protobuf uprev actually are rebuilt rather than relying on cached copies
of generated files.

BUG=chromium:932757
TEST=unit_test stage passes in CQ.

Change-Id: I5b4f4a44e1fcfb0e2f0a669261e04b8b1a9d06b1
Reviewed-on: https://chromium-review.googlesource.com/c/1476151
Tested-by: Allen Webb <allenwebb@google.com>
Reviewed-by: Manoj Gupta <manojgupta@chromium.org>
Trybot-Ready: Allen Webb <allenwebb@google.com>
diff --git a/chroot_version_hooks.d/162_post_protobuf_upgrade b/chroot_version_hooks.d/162_post_protobuf_upgrade
new file mode 100644
index 0000000..16f93ee
--- /dev/null
+++ b/chroot_version_hooks.d/162_post_protobuf_upgrade
@@ -0,0 +1,31 @@
+# Copyright 2019 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.
+
+# Clean local portage caches. This is necessary if there are
+# cros_workon packages.
+sudo rm -rf /var/cache/portage/* /build/*/var/cache/portage/*
+
+# Clean board packages.
+for board_root in /build/* ; do
+  board=${board_root##*/}
+  [[ "${board}" == "bin" || "${board}" == "dev" || \
+     "${board}" == "proc" ]] && continue
+
+  # Find packages using  either protobuf or grpc.
+  PKGS=( $(qdepends-${board} -qCN -Q dev-libs/protobuf && \
+         qdepends-${board} -qCN -Q net-libs/grpc | sort -u))
+  if [[ ${#PKGS[@]} -eq 0 ]]; then
+    continue
+  fi
+
+  echo "Cleaning packages using protobuf or grpc from ${board}"
+  PKGDIR=$(portageq-${board} envvar PKGDIR)
+  cd "${PKGDIR}"
+  # Remove existing binary packages.
+  for pkg in "${PKGS[@]}"; do
+    sudo rm -f "${pkg}"-[0-9]*.tbz2
+  done
+  # Unmerge all packages using either protobuf or grpc.
+  qmerge-${board} -Uqy "${PKGS[@]}"
+done