1#!/usr/bin/env python
2# Copyright 2015 the V8 project authors. All rights reserved.
3# Copyright 2015 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Script to download LLVM gold plugin from google storage."""
8
9import json
10import os
11import re
12import platform
13import shutil
14import subprocess
15import sys
16import zipfile
17
18# Bail out on windows and cygwin.
19if "win" in platform.system().lower():
20  # Python 2.7.6 hangs at the second path.insert command on windows. Works
21  # with python 2.7.8.
22  print "Gold plugin download not supported on windows."
23  sys.exit(0)
24
25SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
26CHROME_SRC = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir))
27sys.path.insert(0, os.path.join(CHROME_SRC, 'tools'))
28
29import find_depot_tools
30
31DEPOT_PATH = find_depot_tools.add_depot_tools_to_path()
32GSUTIL_PATH = os.path.join(DEPOT_PATH, 'gsutil.py')
33
34LLVM_BUILD_PATH = os.path.join(CHROME_SRC, 'third_party', 'llvm-build',
35                               'Release+Asserts')
36CLANG_UPDATE_PY = os.path.join(CHROME_SRC, 'tools', 'clang', 'scripts',
37                               'update.py')
38CLANG_REVISION = os.popen(CLANG_UPDATE_PY + ' --print-revision').read().rstrip()
39
40CLANG_BUCKET = 'gs://chromium-browser-clang/Linux_x64'
41
42GOLD_PLUGIN_PATH = os.path.join(LLVM_BUILD_PATH, 'lib', 'LLVMgold.so')
43
44sys.path.insert(0, os.path.join(CHROME_SRC, 'tools', 'clang', 'scripts'))
45
46import update
47
48def main():
49  if not re.search(r'cfi_vptr=1', os.environ.get('GYP_DEFINES', '')):
50    # Bailout if this is not a cfi build.
51    print 'Skipping gold plugin download for non-cfi build.'
52    return 0
53  if (os.path.exists(GOLD_PLUGIN_PATH) and
54      update.ReadStampFile().strip() == update.PACKAGE_VERSION):
55    # Bailout if clang is up-to-date. This requires the script to be run before
56    # the clang update step! I.e. afterwards clang would always be up-to-date.
57    print 'Skipping gold plugin download. File present and clang up to date.'
58    return 0
59
60  # Make sure this works on empty checkouts (i.e. clang not downloaded yet).
61  if not os.path.exists(LLVM_BUILD_PATH):
62    os.makedirs(LLVM_BUILD_PATH)
63
64  targz_name = 'llvmgold-%s.tgz' % CLANG_REVISION
65  remote_path = '%s/%s' % (CLANG_BUCKET, targz_name)
66
67  os.chdir(LLVM_BUILD_PATH)
68
69  # TODO(pcc): Fix gsutil.py cp url file < /dev/null 2>&0
70  # (currently aborts with exit code 1,
71  # https://github.com/GoogleCloudPlatform/gsutil/issues/289) or change the
72  # stdin->stderr redirect in update.py to do something else (crbug.com/494442).
73  subprocess.check_call(['python', GSUTIL_PATH,
74                         'cp', remote_path, targz_name],
75                        stderr=open('/dev/null', 'w'))
76  subprocess.check_call(['tar', 'xzf', targz_name])
77  os.remove(targz_name)
78  return 0
79
80if __name__ == '__main__':
81  sys.exit(main())
82