6 """This script is used to download prebuilt clang binaries."""
8 from __future__
import division
9 from __future__
import print_function
22 from urllib.error
import HTTPError, URLError
23 from urllib.request
import urlopen
25 from urllib2
import urlopen, HTTPError, URLError
31 CLANG_REVISION =
'llvmorg-14-init-6722-g0fbd3aad'
32 CLANG_SUB_REVISION = 2
34 PACKAGE_VERSION =
'%s-%s' % (CLANG_REVISION, CLANG_SUB_REVISION)
37 THIS_DIR = os.path.abspath(os.path.dirname(__file__))
38 LLVM_BUILD_DIR = os.path.join(THIS_DIR,
'llvm-build')
39 STAMP_FILE = os.path.join(LLVM_BUILD_DIR,
'cr_build_revision')
42 CDS_URL = os.environ.get(
'CDS_CLANG_BUCKET_OVERRIDE',
43 'https://commondatastorage.googleapis.com/chromium-browser-clang')
47 """Download url into output_file."""
55 sys.stdout.write(
'Downloading %s ' % url)
57 response = urlopen(url)
58 total_size =
int(response.headers.get(
'Content-Length').strip())
62 chunk = response.read(CHUNK_SIZE)
65 output_file.write(chunk)
66 bytes_done +=
len(chunk)
67 num_dots = TOTAL_DOTS * bytes_done // total_size
68 sys.stdout.write(
'.' * (num_dots - dots_printed))
70 dots_printed = num_dots
71 if bytes_done != total_size:
72 raise URLError(
"only got %d of %d bytes" % (bytes_done, total_size))
76 sys.stdout.write(
'\n')
78 if num_retries == 0
or isinstance(e, HTTPError)
and e.code == 404:
81 print(
'Retrying in %d s ...' % retry_wait_s)
82 time.sleep(retry_wait_s)
87 if not os.path.exists(path):
88 print(
"Creating directory %s" % path)
93 with tempfile.TemporaryFile()
as f:
97 tarfile.open(mode=
'r:gz', fileobj=f).extractall(path=output_dir)
101 """Return the contents of the stamp file, or '' if it doesn't exist."""
103 with open(path,
'r')
as f:
104 return f.read().rstrip()
110 """Write s to the stamp file."""
112 with open(path,
'w')
as f:
119 def ChmodAndRetry(func, path, _):
121 if not os.access(path, os.W_OK):
122 os.chmod(path, stat.S_IWUSR)
126 shutil.rmtree(dir, onerror=ChmodAndRetry)
130 """Copy a file from src to dst."""
131 print(
"Copying %s to %s" % (src, dst))
132 shutil.copy(src, dst)
136 cds_file =
"clang-%s.tgz" % PACKAGE_VERSION
137 if sys.platform ==
'win32' or sys.platform ==
'cygwin':
138 cds_full_url = CDS_URL +
'/Win/' + cds_file
139 elif sys.platform.startswith(
'linux'):
140 cds_full_url = CDS_URL +
'/Linux_x64/' + cds_file
144 print(
'Updating Clang to %s...' % PACKAGE_VERSION)
147 print(
'Clang is already up to date.')
153 print(
'Downloading prebuilt clang')
154 if os.path.exists(LLVM_BUILD_DIR):
158 print(
'clang %s unpacked' % PACKAGE_VERSION)
162 print(
'Failed to download prebuilt clang %s' % cds_file)
171 if __name__ ==
'__main__':