build_stages: pass the list of packages to build to build_packages

build_packages automatically determines a list of packages to build if
none is given. This applies to most of the configs in cbuidlbot.

This CL moves the logic into cbuildbot so that we explicitly pass a
list of packages to build_packages. This way, we also know what
packages to extract the dependencies for later.

BUG=chromium:422639
TEST=`cbuildbot --remote link-paladin`

Change-Id: I025c771f66cfc57a2fc0cf390e93dc373952d4d8
Reviewed-on: https://chromium-review.googlesource.com/225781
Tested-by: Yu-Ju Hong <yjhong@chromium.org>
Reviewed-by: David James <davidjames@chromium.org>
Commit-Queue: Yu-Ju Hong <yjhong@chromium.org>
diff --git a/cbuildbot/stages/build_stages.py b/cbuildbot/stages/build_stages.py
index 6f4f28b..f825297 100644
--- a/cbuildbot/stages/build_stages.py
+++ b/cbuildbot/stages/build_stages.py
@@ -237,18 +237,43 @@
                             constants.CHROME_CP,
                             extra_env=self._portage_extra_env)
 
+  def GetListOfPackagesToBuild(self):
+    """Returns a list of packages to build."""
+    if self._run.config.packages:
+      # If the list of packages is set in the config, use it.
+      return self._run.config.packages
+
+    # TODO: the logic below is duplicated from the build_packages
+    # script. Once we switch to `cros build`, we should consolidate
+    # the logic in a shared location.
+    packages = ['virtual/target-os']
+    # Build Dev packages by default.
+    packages += ['virtual/target-os-dev']
+    # Build test packages by default.
+    packages += ['virtual/target-os-test']
+    # Build factory packages by default.
+    packages += ['chromeos-base/chromeos-installshim',
+                 'chromeos-base/chromeos-factory',
+                 'chromeos-base/chromeos-hwid',
+                 'chromeos-base/autotest-factory-install']
+    if self._run.ShouldBuildAutotest():
+      packages += ['chromeos-base/autotest-all']
+
+    return packages
+
   def PerformStage(self):
     # If we have rietveld patches, always compile Chrome from source.
     noworkon = not self._run.options.rietveld_patches
 
     self.VerifyChromeBinpkg()
+    packages = self.GetListOfPackagesToBuild()
 
     commands.Build(self._build_root,
                    self._current_board,
                    build_autotest=self._run.ShouldBuildAutotest(),
                    usepkg=self._run.config.usepkg_build_packages,
                    chrome_binhost_only=self._run.config.chrome_binhost_only,
-                   packages=self._run.config.packages,
+                   packages=packages,
                    skip_chroot_upgrade=True,
                    chrome_root=self._run.options.chrome_root,
                    noworkon=noworkon,