cros_setup_toolchains: add a post-cross-compiler set of packages

This is cherry-pick of following CL
https://chromium-review.googlesource.com/699064 with rust removed
and virtual/target-sdk-post-cross added.

BUG=b:162254118
TEST=precqs pass
CQ-DEPEND=CL:2478667

Change-Id: I71326e906723cc49e3d08efee98443e329756eb1
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/2478985
Tested-by: Patryk Duda <patrykd@google.com>
Commit-Queue: Patrick Georgi <pgeorgi@chromium.org>
Reviewed-by: Jett Rink <jettrink@chromium.org>
diff --git a/scripts/cros_setup_toolchains.py b/scripts/cros_setup_toolchains.py
index 2f3ba82..361e8d2 100644
--- a/scripts/cros_setup_toolchains.py
+++ b/scripts/cros_setup_toolchains.py
@@ -73,6 +73,21 @@
     'sys-libs/glibc',
 )
 
+# These packages are also installed into the host SDK.  However, they require
+# the cross-compilers to be installed first (because they need them to actually
+# build), so we have to delay their installation.
+HOST_POST_CROSS_PACKAGES = (
+    'virtual/target-sdk-post-cross',
+)
+
+# New packages that we're in the process of adding to the SDK.  Since the SDK
+# bot hasn't had a chance to run yet, there are no binary packages available,
+# so we have to list them here and wait.  Once it completes, entries here can
+# be removed so they'll end up on bots & dev's systems.
+NEW_PACKAGES = (
+    'virtual/target-sdk-post-cross',
+)
+
 # Enable the Go compiler for these targets.
 TARGET_GO_ENABLED = (
     'x86_64-cros-linux-gnu',
@@ -150,13 +165,17 @@
 
     val = cls._CACHE.setdefault(CACHE_ATTR, {})
     if not target in val:
-      if target == 'host':
+      if target.startswith('host'):
         conf = {
             'crosspkgs': [],
             'target': toolchain.GetHostTuple(),
         }
+        if target == 'host':
+          packages_list = HOST_PACKAGES
+        else:
+          packages_list = HOST_POST_CROSS_PACKAGES
         manual_pkgs = dict((pkg, cat) for cat, pkg in
-                           [x.split('/') for x in HOST_PACKAGES])
+                           [x.split('/') for x in packages_list])
       else:
         # Build the crossdev command.
         cmd = ['crossdev', '--show-target-cfg', '--ex-gdb']
@@ -290,7 +309,7 @@
   """Returns a package name for the given target."""
   conf = Crossdev.GetConfig(target)
   # Portage category:
-  if target == 'host' or package in Crossdev.MANUAL_PKGS:
+  if target.startswith('host') or package in Crossdev.MANUAL_PKGS:
     category = conf[package + '_category']
   else:
     category = conf['category']
@@ -517,6 +536,9 @@
       current = GetInstalledPackageVersions(pkg, root=root)
       desired = GetDesiredPackageVersions(target, package)
       desired_num = VersionListToNumeric(target, package, desired, False)
+      if pkg in NEW_PACKAGES and usepkg:
+        # Skip this binary package (for now).
+        continue
       mergemap[pkg] = set(desired_num).difference(current)
 
   packages = []
@@ -602,6 +624,11 @@
   """
   for package in ['gcc', 'binutils']:
     for target in targets:
+      # See if this package is part of this target.
+      if package not in GetTargetPackages(target):
+        logging.debug('%s: %s is not used', target, package)
+        continue
+
       # Pick the first version in the numbered list as the selected one.
       desired = GetDesiredPackageVersions(target, package)
       desired_num = VersionListToNumeric(target, package, desired, True,
@@ -610,7 +637,7 @@
       # *-config does not play revisions, strip them, keep just PV.
       desired = portage.versions.pkgsplit('%s-%s' % (package, desired))[1]
 
-      if target == 'host':
+      if target.startswith('host'):
         # *-config is the only tool treating host identically (by tuple).
         target = toolchain.GetHostTuple()
 
@@ -705,6 +732,9 @@
     # Those that were not initialized may need a config update.
     Crossdev.UpdateTargets(reconfig_targets, usepkg, config_only=True)
 
+    # Since we have cross-compilers now, we can update these packages.
+    targets['host-post-cross'] = {}
+
   # We want host updated.
   targets['host'] = {}