Use binary packages in cros_generate_sysroot.

cros_generate_sysroot currently rewrites the sysroot itself, breaking
any scripts that need to build images from the sysroot. This is
aggravated by the fact that cros_generate_sysroot builds with the wrong
use flags. We have fixed this problem by updating updating
cros_generate_sysroot to pass in the right use flags and to use
--usepkgonly.

BUG=chromium:218085
TEST=All unit tests, all release trybots.
TEST=Compile built tarball with cros chrome-sdk.

Change-Id: I6661e5ba6b0b18cd6292d0ac48fd428a232fadbf
Reviewed-on: https://gerrit.chromium.org/gerrit/63152
Tested-by: David James <davidjames@chromium.org>
Reviewed-by: David James <davidjames@chromium.org>
Commit-Queue: David James <davidjames@chromium.org>
diff --git a/scripts/cros_generate_sysroot.py b/scripts/cros_generate_sysroot.py
index 9a38c48..556de9d 100644
--- a/scripts/cros_generate_sysroot.py
+++ b/scripts/cros_generate_sysroot.py
@@ -57,6 +57,14 @@
     """
     self.sysroot = sysroot
     self.options = options
+    self.extra_env = {'ROOT': self.sysroot, 'USE': os.environ.get('USE', '')}
+
+  def _Emerge(self, *args, **kwargs):
+    """Emerge the given packages using parallel_emerge."""
+    cmd = [self.PARALLEL_EMERGE, '--board=%s' % self.options.board,
+           '--usepkgonly', '--noreplace'] + list(args)
+    kwargs.setdefault('extra_env', self.extra_env)
+    cros_build_lib.SudoRunCommand(cmd, **kwargs)
 
   def _InstallToolchain(self):
     cros_build_lib.RunCommand(
@@ -65,16 +73,24 @@
          self.options.board])
 
   def _InstallKernelHeaders(self):
-    cros_build_lib.SudoRunCommand(
-        [self.PARALLEL_EMERGE, '--board=%s' % self.options.board,
-         '--root-deps=rdeps', '--getbinpkg', '--usepkg',
-         '--root=%s' % self.sysroot, 'sys-kernel/linux-headers'])
+    self._Emerge('sys-kernel/linux-headers')
 
   def _InstallBuildDependencies(self):
-    cros_build_lib.SudoRunCommand(
-        [self.PARALLEL_EMERGE, '--board=%s' % self.options.board,
-         '--root=%s' % self.sysroot, '--usepkg', '--onlydeps',
-         '--usepkg-exclude=%s' % self.options.package, self.options.package])
+    # Calculate buildtime deps that are not runtime deps.
+    raw_sysroot = '/build/%s' % (self.options.board,)
+    cmd = ['qdepends', '-q', '-C', self.options.package]
+    output = cros_build_lib.RunCommandCaptureOutput(
+        cmd, extra_env={'ROOT': raw_sysroot}).output
+
+    if output.count('\n') > 1:
+      raise AssertionError('Too many packages matched given package pattern')
+
+    # qdepend outputs "package: deps", so only grab the deps.
+    atoms = output.partition(':')[2].split()
+
+    # Install the buildtime deps.
+    if atoms:
+      self._Emerge(*atoms)
 
   def _CreateTarball(self):
     target = os.path.join(self.options.out_dir, self.options.out_file)