cros_setup_toolchains: fix splitdebug behaviour

* Pass FEATURES via crossdev option in order to record it in persistent
configuration
* Make use of crossdev's reconfigure option to fix configuration of
pre-existing targets

BUG=chromium-os:31348
TEST=update toolchains, see success

Change-Id: I38334744d83836a642cbc872625fcb16b6d7f8aa
Reviewed-on: https://gerrit.chromium.org/gerrit/23946
Tested-by: Zdenek Behan <zbehan@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Commit-Ready: Zdenek Behan <zbehan@chromium.org>
diff --git a/scripts/cros_setup_toolchains.py b/scripts/cros_setup_toolchains.py
index 9ee530e..b7c1264 100644
--- a/scripts/cros_setup_toolchains.py
+++ b/scripts/cros_setup_toolchains.py
@@ -396,19 +396,15 @@
 
 
 # Main functions performing the actual update steps.
-def InitializeCrossdevTargets(targets, usepkg):
+def UpdateCrossdevTargets(targets, usepkg, config_only=False):
   """Calls crossdev to initialize a cross target.
   args:
     targets - the list of targets to initialize using crossdev
     usepkg - copies the commandline opts
   """
-  print 'The following targets need to be re-initialized:'
-  print targets
-
-  extra_env = { 'FEATURES' : 'splitdebug' }
   for target in targets:
-    cmd = ['crossdev', '--show-fail-log',
-           '-t', target]
+    cmd = ['crossdev', '--show-fail-log', '-t', target]
+    cmd.extend(['--env', 'FEATURES=splitdebug'])
     # Pick stable by default, and override as necessary.
     cmd.extend(['-P', '--oneshot'])
     if usepkg:
@@ -427,7 +423,13 @@
       # The first of the desired versions is the "primary" one.
       version = GetDesiredPackageVersions(target, pkg)[0]
       cmd.extend(['--%s' % pkg, version])
-    cros_build_lib.RunCommand(cmd, extra_env=extra_env)
+
+    if config_only:
+      # In this case we want to just quietly reinit
+      cmd.append('--init-target')
+      cros_build_lib.RunCommand(cmd, print_cmd=False, redirect_stdout=True)
+    else:
+      cros_build_lib.RunCommand(cmd)
 
 
 def UpdateTargets(targets, usepkg):
@@ -582,7 +584,12 @@
     crossdev_targets = \
         [t for t in targets if not TargetIsInitialized(t)]
     if crossdev_targets:
-      InitializeCrossdevTargets(crossdev_targets, usepkg)
+      print 'The following targets need to be re-initialized:'
+      print crossdev_targets
+      UpdateCrossdevTargets(crossdev_targets, usepkg)
+    # Those that were not initialized may need a config update.
+    reconfig_targets = targets.difference(set(crossdev_targets))
+    UpdateCrossdevTargets(reconfig_targets, usepkg, config_only=True)
 
   # We want host updated.
   targets.add('host')