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
15import os.path
16import shutil
17import sys
18import tarfile
19import tempfile
20import 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.
25CLANG_REVISION = "233105"
26CLANG_SUB_REVISION = "1"
27
28PACKAGE_VERSION = "%s-%s" % (CLANG_REVISION, CLANG_SUB_REVISION)
29LLVM_BUILD_DIR = os.path.join(os.path.dirname(__file__), "llvm-build")
30STAMP_FILE = os.path.join(LLVM_BUILD_DIR, "cr_build_revision")
31
32CDS_URL = "https://commondatastorage.googleapis.com/chromium-browser-clang"
33
34def 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
44def 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
70if __name__ == "__main__":
71  sys.exit(main(sys.argv[1:]))
72