Revert of Instrumented libraries: Remove dependence on GYP_DEFINES (patchset #2 id:60001 of https://codereview.chromium.org/2775913002/ )

Reason for revert:
Reverting now that all of the third_party changes have been reverted

Original issue's description:
> Instrumented libraries: Remove dependence on GYP_DEFINES
>
> This CL ports download_binaries.py and unpack_binaries.py from using
> GYP_DEFINES to gn args.  Previously, there was a gotcha step where you
> had to run 'gclient sync' with the right GYP_DEFINES to actually download
> the prebuilt instrumented libraries.  This CL removes that step.
>
> CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.linux:linux_chromium_msan_rel_ng;master.tryserver.chromium.linux:linux_chromium_chromeos_msan_rel_ng
> R=eugenis@chromium.org
> BUG=705072
>
> Review-Url: https://codereview.chromium.org/2775913002
> Cr-Commit-Position: refs/heads/master@{#459630}
> Committed: https://chromium.googlesource.com/chromium/src/+/5adb8c047852670be6c9d42114f3a17d44b6c56e

TBR=eugenis@chromium.org,dpranke@chromium.org,machenbach@chromium.org,thomasanderson@google.com
# Not skipping CQ checks because original CL landed more than 1 days ago.
BUG=705072

Review-Url: https://codereview.chromium.org/2792343003
Cr-Original-Commit-Position: refs/heads/master@{#461871}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: aa441f50eecca13f2d3490a89705cb00cc0d5920
diff --git a/BUILD.gn b/BUILD.gn
index 4d1e910..9d7636a 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -57,27 +57,18 @@
     }
   }
 
-  action("download_prebuilt_instrumented_libraries") {
-    tarfile_name = "$archive_prefix-$instrumented_libraries_platform.tgz"
-    shafile = "binaries/$tarfile_name.sha1"
-    tarfile = "$target_out_dir/$tarfile_name"
-    inputs = [ shafile ]
-    outputs = [ tarfile ]
-    script = "scripts/download_binaries.py"
-    args = [ rebase_path(shafile), rebase_path(tarfile) ]
-  }
-
+  # TODO(GYP): scripts/download_binaries.py uses GYP_DEFINES to decide whether
+  # to download the archives extracted here.
   # Note: This requires a clobber whenever Ubuntu version changes.
   action("extract_prebuilt_instrumented_libraries") {
-    deps = [ ":download_prebuilt_instrumented_libraries" ]
     visibility = [ ":prebuilt" ]
     script = "scripts/unpack_binaries.py"
     depfile = "$target_out_dir/$archive_prefix.d"
-    tarfile = get_target_outputs(":download_prebuilt_instrumented_libraries")
     args = [
-      rebase_path(tarfile[0]),
-      rebase_path("$target_out_dir/$archive_prefix.txt"),
+      archive_prefix,
+      rebase_path("binaries"),
       rebase_path(root_out_dir + "/instrumented_libraries_prebuilt"),
+      rebase_path(target_out_dir, root_out_dir),
     ]
     outputs = [
       "$target_out_dir/$archive_prefix.txt",
diff --git a/scripts/download_binaries.py b/scripts/download_binaries.py
index fa339cc..b7e87b2 100755
--- a/scripts/download_binaries.py
+++ b/scripts/download_binaries.py
@@ -3,34 +3,69 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-"""Downloads pre-built sanitizer-instrumented third-party libraries from GCS.
-This script should only be run from gn.
-"""
+"""Downloads pre-built sanitizer-instrumented third-party libraries from GCS."""
 
+import os
+import re
 import subprocess
 import sys
 
 
-def main(args):
-  if not sys.platform.startswith('linux'):
-    raise Exception("Prebuilt instrumented libraries require Linux.")
+SCRIPT_PATH = os.path.abspath(__file__)
+BASE_PATH = os.path.normpath(os.path.join(SCRIPT_PATH, *(4 * ['..'])))
 
-  sha1file = args[0]
-  tarfile = args[1]
+# This defaults to the consuming project's base directory, e.g. 'src' in
+# chromium.
+BASE_DIR = os.path.basename(BASE_PATH)
+
+
+def get_ubuntu_release():
+  supported_releases = ['trusty']
+  release = subprocess.check_output(['lsb_release', '-cs']).strip()
+  if release not in supported_releases:
+    raise Exception("Supported Ubuntu versions: %s", str(supported_releases))
+  return release
+
+
+def get_configuration(gyp_defines):
+  if re.search(r'\b(msan)=1', gyp_defines):
+    if 'msan_track_origins=0' in gyp_defines:
+      return 'msan-no-origins'
+    if 'msan_track_origins=2' in gyp_defines:
+      return 'msan-chained-origins'
+    if 'msan_track_origins=' not in gyp_defines:
+      # NB: must be the same as the default value in common.gypi
+      return 'msan-chained-origins'
+  raise Exception(
+      "Prebuilt instrumented libraries not available for your configuration.")
+
+
+def get_archive_name(gyp_defines):
+  return "%s-%s.tgz" % (get_configuration(gyp_defines), get_ubuntu_release())
+
+
+def main(args):
+  gyp_defines = os.environ.get('GYP_DEFINES', '')
+  if not 'use_prebuilt_instrumented_libraries=1' in gyp_defines:
+    return 0
+
+  if not sys.platform.startswith('linux'):
+    raise Exception("'use_prebuilt_instrumented_libraries=1' requires Linux.")
+
+  archive_name = get_archive_name(gyp_defines)
+  sha1file = '%s.sha1' % archive_name
+  target_directory = os.path.join(
+      BASE_DIR, 'third_party', 'instrumented_libraries', 'binaries')
 
   subprocess.check_call([
       'download_from_google_storage',
       '--no_resume',
       '--no_auth',
       '--bucket', 'chromium-instrumented-libraries',
-      '-s', sha1file, '-o', tarfile])
+      '-s', sha1file], cwd=target_directory)
 
   return 0
 
 
 if __name__ == '__main__':
-  # TODO(thomasanderson): Remove this once all third_party DEPS
-  # entires for this script are removed.
-  if (len(sys.argv) == 1):
-    sys.exit(0)
   sys.exit(main(sys.argv[1:]))
diff --git a/scripts/unpack_binaries.py b/scripts/unpack_binaries.py
index 2f335bc..3e17790 100755
--- a/scripts/unpack_binaries.py
+++ b/scripts/unpack_binaries.py
@@ -3,27 +3,38 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-"""Unpacks pre-built sanitizer-instrumented third-party libraries.
-This script should only be run by gn.
-"""
+"""Unpacks pre-built sanitizer-instrumented third-party libraries."""
 
 import os
 import subprocess
 import shutil
 import sys
 
+import download_binaries
 
-def main(archive, stamp_file, target_dir):
+
+def get_archive_name(archive_prefix):
+  return '%s-%s.tgz' % (archive_prefix, download_binaries.get_ubuntu_release())
+
+
+def main(archive_prefix, archive_dir, target_dir, stamp_dir=None):
   shutil.rmtree(target_dir, ignore_errors=True)
 
   os.mkdir(target_dir)
   subprocess.check_call([
       'tar',
       '-zxf',
-      archive,
+      os.path.join(archive_dir, get_archive_name(archive_prefix)),
       '-C',
       target_dir])
+  stamp_file = os.path.join(stamp_dir or target_dir, '%s.txt' % archive_prefix)
   open(stamp_file, 'w').close()
+
+  if stamp_dir:
+    with open(os.path.join(stamp_dir, '%s.d' % archive_prefix), 'w') as f:
+      f.write('%s: %s' % (
+          stamp_file, os.path.join(archive_dir,
+                                   get_archive_name(archive_prefix))))
   return 0