Optimize toolchain selection slightly.

gcc-config and binutils-config should really only be run when a new toolchain
is installed, so let's optimize cros_setup_toolchains to do that.

This CL reduces the time required to run cros_setup_toolchains from 0.39s
to 0.23s. This optimization is intended to allow us to run
cros_setup_toolchains more often to ensure the chroot is up to date.

CL:37326 implemented a similar optimization, but with a bug: We forgot
to update the configuration when crossdev installed everything. This CL
reintroduces the same optimization, but correctly.

BUG=none
TEST=Verify SDK builder still selects gold with this CL.

Change-Id: Icd79238370baf9eaff86bdae0ad96363be66086c
Reviewed-on: https://gerrit.chromium.org/gerrit/37462
Commit-Ready: David James <davidjames@chromium.org>
Reviewed-by: David James <davidjames@chromium.org>
Tested-by: David James <davidjames@chromium.org>
diff --git a/scripts/cros_setup_toolchains.py b/scripts/cros_setup_toolchains.py
index 86a882b..b7cf17b 100644
--- a/scripts/cros_setup_toolchains.py
+++ b/scripts/cros_setup_toolchains.py
@@ -453,7 +453,7 @@
 
   if not packages:
     print 'Nothing to update!'
-    return
+    return False
 
   print 'Updating packages:'
   print packages
@@ -464,6 +464,7 @@
 
   cmd.extend(packages)
   cros_build_lib.RunCommand(cmd)
+  return True
 
 
 def CleanTargets(targets):
@@ -534,14 +535,14 @@
         cros_build_lib.RunCommand(cmd, print_cmd=False)
 
 
-def UpdateToolchains(usepkg, deleteold, hostonly, targets_wanted,
-                     boards_wanted):
+def UpdateToolchains(usepkg, deleteold, hostonly, reconfig,
+                     targets_wanted, boards_wanted):
   """Performs all steps to create a synchronized toolchain enviroment.
 
   args:
     arguments correspond to the given commandline flags
   """
-  targets = {}
+  targets, crossdev_targets, reconfig_targets = {}, {}, {}
   if not hostonly:
     # For hostonly, we can skip most of the below logic, much of which won't
     # work on bare systems where this is useful.
@@ -570,8 +571,6 @@
       targets.update(GetToolchainsForBoard(board))
 
     # First check and initialize all cross targets that need to be.
-    crossdev_targets = {}
-    reconfig_targets = {}
     for target in targets:
       if TargetIsInitialized(target):
         reconfig_targets[target] = targets[target]
@@ -588,9 +587,8 @@
   targets['host'] = {}
 
   # Now update all packages.
-  UpdateTargets(targets, usepkg)
-
-  SelectActiveToolchains(targets, CONFIG_TARGET_SUFFIXES)
+  if UpdateTargets(targets, usepkg) or crossdev_targets or reconfig:
+    SelectActiveToolchains(targets, CONFIG_TARGET_SUFFIXES)
 
   if deleteold:
     CleanTargets(targets)
@@ -624,7 +622,7 @@
                     dest='board_cfg', default=None,
                     help=('Board to list toolchain tuples for'))
   parser.add_option('--reconfig', default=False, action='store_true',
-                    help=('Reload crossdev config'))
+                    help=('Reload crossdev config and reselect toolchains.'))
 
   (options, _remaining_arguments) = parser.parse_args(argv)
 
@@ -649,6 +647,6 @@
   boards = set(options.include_boards.split(',')) if options.include_boards \
       else set()
   Crossdev.Load(options.reconfig)
-  UpdateToolchains(options.usepkg, options.deleteold, options.hostonly, targets,
-                   boards)
+  UpdateToolchains(options.usepkg, options.deleteold, options.hostonly,
+                   options.reconfig, targets, boards)
   Crossdev.Save()