Support disabling ccache for new wrapper.

The new wrapper is a binary, so we can't modify it.
This replaces the version of the wrapper that uses
ccache with the version of the wrapper that does not
use ccache.

Ran the script to build standalone toolchain:
sudo $(which cros_setup_toolchains) --create-packages \
  --output-dir /tmp/toolchain-pkgs -t x86_64-cros-linux-gnu

Verified that the new wrapper has ccache disabled by default.
Verified that a toolchain with the old wrapper has ccache
disabled by default.
Verified that the wrappers in the chroot are not changed
when building a standline toolchain.

BUG=chromium:773875
TEST=tested locally with new and old wrappers

Change-Id: Idca6884754d8c3dd47a65a3ca888e5a605b6dc67
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/1753552
Tested-by: Tobias Bosch <tbosch@google.com>
Commit-Queue: Tobias Bosch <tbosch@google.com>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Manoj Gupta <manojgupta@chromium.org>
diff --git a/scripts/cros_setup_toolchains.py b/scripts/cros_setup_toolchains.py
index cf160dc..7262b5a 100644
--- a/scripts/cros_setup_toolchains.py
+++ b/scripts/cros_setup_toolchains.py
@@ -13,6 +13,7 @@
 import json
 import os
 import re
+import shutil
 
 from chromite.lib import constants
 from chromite.lib import commandline
@@ -1155,8 +1156,26 @@
 def _ProcessSysrootWrappers(_target, output_dir, srcpath):
   """Remove chroot-specific things from our sysroot wrappers"""
   # Disable ccache since we know it won't work outside of chroot.
+
+  # Update the new go wrapper.
+  # Use the version of the wrapper that does not use ccache.
   for sysroot_wrapper in glob.glob(os.path.join(
-      output_dir + srcpath, 'sysroot_wrapper*')):
+      output_dir + srcpath, 'sysroot_wrapper*.ccache')):
+    # Can't update the wrapper in place to not affect the chroot,
+    # but only the extracted toolchain.
+    os.unlink(sysroot_wrapper)
+    shutil.copy(sysroot_wrapper[:-6] + 'noccache', sysroot_wrapper)
+
+  # Update the old python wrapper
+  # TODO(crbug/773875): Remove this logic once the go wrapper
+  # is rolled out.
+  old_wrapper_paths = [os.path.join(output_dir + srcpath,
+                                    'sysroot_wrapper'),
+                       os.path.join(output_dir + srcpath,
+                                    'sysroot_wrapper.hardened')]
+  for sysroot_wrapper in old_wrapper_paths:
+    if not os.path.exists(sysroot_wrapper):
+      continue
     contents = osutils.ReadFile(sysroot_wrapper).splitlines()
 
     # In order to optimize startup time in the chroot we run python a little
@@ -1170,7 +1189,8 @@
         assert 'True' in line
         contents[num] = line.replace('True', 'False')
         break
-    # Can't update the wrapper in place since it's a hardlink to a file in /.
+    # Can't update the wrapper in place to not affect the chroot,
+    # but only the extracted toolchain.
     os.unlink(sysroot_wrapper)
     osutils.WriteFile(sysroot_wrapper, '\n'.join(contents))
     os.chmod(sysroot_wrapper, 0o755)