1 # Copyright (c) 2015, Google Inc. 2 # 3 # Permission to use, copy, modify, and/or distribute this software for any 4 # purpose with or without fee is hereby granted, provided that the above 5 # copyright notice and this permission notice appear in all copies. 6 # 7 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 10 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 12 # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 13 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 15 import os.path 16 import shutil 17 import sys 18 import tarfile 19 import tempfile 20 import urllib 21 22 # CLANG_REVISION and CLANG_SUB_REVISION determine the build of clang 23 # to use. These should be synced with tools/clang/scripts/update.py in 24 # Chromium. 25 CLANG_REVISION = "233105" 26 CLANG_SUB_REVISION = "1" 27 28 PACKAGE_VERSION = "%s-%s" % (CLANG_REVISION, CLANG_SUB_REVISION) 29 LLVM_BUILD_DIR = os.path.join(os.path.dirname(__file__), "llvm-build") 30 STAMP_FILE = os.path.join(LLVM_BUILD_DIR, "cr_build_revision") 31 32 CDS_URL = "https://commondatastorage.googleapis.com/chromium-browser-clang" 33 34 def DownloadFile(url, path): 35 """DownloadFile fetches |url| to |path|.""" 36 last_progress = [0] 37 def report(a, b, c): 38 progress = int(a * b * 100.0 / c) 39 if progress != last_progress[0]: 40 print >> sys.stderr, "Downloading... %d%%" % progress 41 last_progress[0] = progress 42 urllib.urlretrieve(url, path, reporthook=report) 43 44 def main(args): 45 # For now, only download clang on Linux. 46 if not sys.platform.startswith("linux"): 47 return 0 48 49 if os.path.exists(STAMP_FILE): 50 with open(STAMP_FILE) as f: 51 if f.read().strip() == PACKAGE_VERSION: 52 print >> sys.stderr, "Clang already at %s" % (PACKAGE_VERSION,) 53 return 0 54 55 if os.path.exists(LLVM_BUILD_DIR): 56 shutil.rmtree(LLVM_BUILD_DIR) 57 58 print >> sys.stderr, "Downloading Clang %s" % (PACKAGE_VERSION,) 59 cds_full_url = "%s/Linux_x64/clang-%s.tgz" % (CDS_URL, PACKAGE_VERSION) 60 with tempfile.NamedTemporaryFile() as temp: 61 DownloadFile(cds_full_url, temp.name) 62 with tarfile.open(temp.name, "r:gz") as tar_file: 63 tar_file.extractall(LLVM_BUILD_DIR) 64 65 with open(STAMP_FILE, "wb") as stamp_file: 66 stamp_file.write(PACKAGE_VERSION) 67 68 return 0 69 70 if __name__ == "__main__": 71 sys.exit(main(sys.argv[1:])) 72