install-build-deps revision 7a40e4d530f2764dd4a3f9a1e6d517d0654fb82d
1#!/usr/bin/env python
2# Copyright (C) 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import argparse
17import hashlib
18import logging
19import os
20import shutil
21import subprocess
22import sys
23import urllib
24import zipfile
25
26from collections import namedtuple
27
28# When adding a new git dependency here please also add a corresponding entry in
29# .travis.yml under the "cache:" section.
30
31# The format for the deps below is the following:
32# (target_folder, source_url, sha1, target_platform)
33# |source_url| can be either a git repo or a http url.
34# If a git repo, |sha1| is the committish that will be checked out.
35# If a http url, |sha1| is the shasum of the original file.
36# If the url is a .zip or .tgz file it will be automatically deflated under
37# |target_folder|, taking care of stripping the root folder if it's a single
38# root (to avoid ending up with buildtools/protobuf/protobuf-1.2.3/... and have
39# instead just buildtools/protobuf).
40# |target_platform| is either 'darwin', 'linux2' or 'all' and applies the dep
41# only on the given platform (ask python why linux2 and not just linux).
42
43# Dependencies required to build code on the host or when targeting desktop OS.
44BUILD_DEPS_HOST = [
45  # GN
46  ('buildtools/mac/gn',
47   'https://storage.googleapis.com/chromium-gn/c2c934d4dda1f470a6511b1015dda9a9fb1ce50b',
48   'c2c934d4dda1f470a6511b1015dda9a9fb1ce50b',
49   'darwin'
50  ),
51  ('buildtools/linux64/gn',
52   'https://storage.googleapis.com/chromium-gn/b53fa13e950948c6f9a062189b76b34a9610281f',
53   'b53fa13e950948c6f9a062189b76b34a9610281f',
54   'linux2'
55  ),
56
57  # clang-format
58  ('buildtools/mac/clang-format',
59   'https://storage.googleapis.com/chromium-clang-format/0679b295e2ce2fce7919d1e8d003e497475f24a3',
60   '0679b295e2ce2fce7919d1e8d003e497475f24a3',
61   'darwin'
62  ),
63  ('buildtools/linux64/clang-format',
64   'https://storage.googleapis.com/chromium-clang-format/5349d1954e17f6ccafb6e6663b0f13cdb2bb33c8',
65   '5349d1954e17f6ccafb6e6663b0f13cdb2bb33c8',
66   'linux2'
67  ),
68  # Keep the SHA1 in sync with |clang_format_rev| in chromium //buildtools/DEPS.
69  ('buildtools/clang_format/script',
70   'https://chromium.googlesource.com/chromium/llvm-project/cfe/tools/clang-format.git',
71   '0653eee0c81ea04715c635dd0885e8096ff6ba6d',
72   'all'
73  ),
74
75  # Ninja
76  ('buildtools/mac/ninja',
77   'https://storage.googleapis.com/fuchsia-build/fuchsia/ninja/mac/a1db595e824c50cf565fbf0af2437fd91b7babf4',
78   'a1db595e824c50cf565fbf0af2437fd91b7babf4',
79   'darwin'
80  ),
81  ('buildtools/linux64/ninja',
82   'https://storage.googleapis.com/fuchsia-build/fuchsia/ninja/linux64/d35b36c84a09f7e38b25947cafada10e8bf835bc',
83   'd35b36c84a09f7e38b25947cafada10e8bf835bc',
84   'linux2'
85  ),
86
87  # Keep in sync with Android's //external/googletest/README.version.
88  ('buildtools/googletest.zip',
89   'https://github.com/google/googletest/archive/ff07a5de0e81580547f1685e101194ed1a4fcd56.zip',
90   'c7edec7d7e6db1fc37a20710de9c4d89e3a3893b',
91   'all'
92  ),
93
94  # Keep in sync with Android's //external/protobuf/README.version.
95  ('buildtools/protobuf.zip',
96   'https://github.com/google/protobuf/releases/download/v3.0.0-beta-3/protobuf-cpp-3.0.0-beta-3.zip',
97   '3caec60aa9d8eefc8c3c3201b6b8ca19935edb89',
98   'all'
99  ),
100
101  # libc++, libc++abi and libunwind for Linux where we need to rebuild the C++
102  # lib from sources. Keep the SHA1s in sync with Chrome's src/buildtools/DEPS.
103  ('buildtools/libcxx',
104   'https://chromium.googlesource.com/chromium/llvm-project/libcxx.git',
105   '3a07dd740be63878167a0ea19fe81869954badd7',
106   'all'
107  ),
108  ('buildtools/libcxxabi',
109   'https://chromium.googlesource.com/chromium/llvm-project/libcxxabi.git',
110   '4072e8fd76febee37f60aeda76d6d9f5e3791daa',
111   'all'
112  ),
113  ('buildtools/libunwind',
114   'https://chromium.googlesource.com/external/llvm.org/libunwind.git',
115   '41f982e5887185b904a456e20dfcd58e6be6cc19',
116   'all'
117  ),
118
119  # Keep the revision in sync with Chrome's CLANG_REVISION in
120  # tools/clang/scripts/update.py.
121  ('buildtools/clang.tgz',
122   'https://commondatastorage.googleapis.com/chromium-browser-clang/Linux_x64/clang-315613-1.tgz',
123   '09f63ace1ce25a3e5d36e760026f4ad4619e42de',
124   'linux2'
125  ),
126
127  # Benchmarking tool.
128  ('buildtools/benchmark.zip',
129   'https://github.com/google/benchmark/archive/v1.2.0.zip',
130   '553b6bfa59c63f15c54cfd8c5122b0f7f84469af',
131   'all'
132  ),
133]
134
135# Dependencies required to build Android code.
136# URLs and SHA1s taken from:
137# - https://dl.google.com/android/repository/repository-11.xml
138# - https://dl.google.com/android/repository/sys-img/android/sys-img.xml
139BUILD_DEPS_ANDROID = [
140  # Android NDK
141  ('buildtools/ndk.zip',
142   'https://dl.google.com/android/repository/android-ndk-r15c-darwin-x86_64.zip',
143   'ea4b5d76475db84745aa8828000d009625fc1f98',
144   'darwin'
145  ),
146  ('buildtools/ndk.zip',
147   'https://dl.google.com/android/repository/android-ndk-r15c-linux-x86_64.zip',
148   '0bf02d4e8b85fd770fd7b9b2cdec57f9441f27a2',
149   'linux2'
150  ),
151]
152
153# Dependencies required to run Android tests.
154TEST_DEPS_ANDROID = [
155  # tools.zip contains the emulator binaries.
156  ('buildtools/android_sdk/tools.zip',
157   'https://dl.google.com/android/repository/tools_r25.2.5-macosx.zip',
158   'd2168d963ac5b616e3d3ddaf21511d084baf3659',
159   'darwin'
160  ),
161  ('buildtools/android_sdk/tools.zip',
162   'https://dl.google.com/android/repository/tools_r25.2.5-linux.zip',
163   '72df3aa1988c0a9003ccdfd7a13a7b8bd0f47fc1',
164   'linux2'
165  ),
166
167  # platform-tools.zip contains adb binaries.
168  ('buildtools/android_sdk/platform-tools.zip',
169   'https://dl.google.com/android/repository/platform-tools_r26.0.0-darwin.zip',
170   'e75b6137dc444f777eb02f44a6d9819b3aabff82',
171   'darwin'
172  ),
173  ('buildtools/android_sdk/platform-tools.zip',
174   'https://dl.google.com/android/repository/platform-tools_r26.0.0-linux.zip',
175   '00de8a6631405b617c10f68cd11ff2e1cd528e23',
176   'linux2'
177  ),
178
179  # Android emulator images.
180  ('buildtools/android_sdk/system-images/android-24/default/armeabi-v7a.zip',
181   'https://dl.google.com/android/repository/sys-img/android/armeabi-v7a-24_r07.zip',
182   '3454546b4eed2d6c3dd06d47757d6da9f4176033',
183   'all'
184  ),
185  ('buildtools/android_sdk/system-images/android-24/default/arm64-v8a.zip',
186   'https://dl.google.com/android/repository/sys-img/android/arm64-v8a-24_r07.zip',
187   'e8ab2e49e4efe4b064232b33b5eeaded61437d7f',
188   'all'
189  ),
190]
191
192ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
193
194
195def ReadFile(path):
196  if not os.path.exists(path):
197    return None
198  with open(path) as f:
199      return f.read().strip()
200
201
202def MkdirRecursive(path):
203  # Works with both relative and absolute paths
204  cwd = '/' if path.startswith('/') else ROOT_DIR
205  for part in path.split('/'):
206    cwd = os.path.join(cwd, part)
207    if not os.path.exists(cwd):
208      os.makedirs(cwd)
209    else:
210      assert(os.path.isdir(cwd))
211
212
213def HashLocalFile(path):
214  if not os.path.exists(path):
215    return None
216  with open(path, 'rb') as f:
217    return hashlib.sha1(f.read()).hexdigest()
218
219
220def ExtractZipfilePreservePermissions(zf, info, path):
221  zf.extract(info.filename, path=path)
222  target_path = os.path.join(path, info.filename)
223  min_acls = 0o755 if info.filename.endswith('/') else 0o644
224  os.chmod(target_path, (info.external_attr >> 16L) | min_acls)
225
226
227def IsGitRepoCheckoutOutAtRevision(path, revision):
228  return ReadFile(os.path.join(path, '.git', 'HEAD')) == revision
229
230
231def CheckoutGitRepo(path, git_url, revision):
232  if IsGitRepoCheckoutOutAtRevision(path, revision):
233    return
234  if os.path.exists(path):
235    shutil.rmtree(path)
236  MkdirRecursive(path)
237  logging.info('Fetching %s @ %s into %s', git_url, revision, path)
238  subprocess.check_call(['git', 'clone', git_url, path], cwd=path)
239  subprocess.check_call(['git', 'checkout', revision, '--quiet'], cwd=path)
240  assert(IsGitRepoCheckoutOutAtRevision(path, revision))
241
242
243def Main():
244  parser = argparse.ArgumentParser()
245  parser.add_argument('--no-android', action='store_true')
246  args = parser.parse_args()
247  deps = BUILD_DEPS_HOST
248  if not args.no_android:
249    deps += BUILD_DEPS_ANDROID + TEST_DEPS_ANDROID
250  for rel_path, url, expected_sha1, platform in deps:
251    if platform != 'all' and platform != sys.platform:
252      continue
253    local_path = os.path.join(ROOT_DIR, rel_path)
254    if url.endswith('.git'):
255      CheckoutGitRepo(local_path, url, expected_sha1)
256      continue
257    is_zip = local_path.endswith('.zip') or local_path.endswith('.tgz')
258    zip_target_dir = local_path[:-4] if is_zip else None
259    zip_dir_stamp = os.path.join(zip_target_dir, '.stamp') if is_zip else None
260
261    if ((not is_zip and HashLocalFile(local_path) == expected_sha1) or
262        (is_zip and ReadFile(zip_dir_stamp) == expected_sha1)):
263      continue
264    MkdirRecursive(os.path.dirname(rel_path))
265    if HashLocalFile(local_path) != expected_sha1:
266      download_path = local_path + '.tmp'
267      logging.info('Downloading %s from %s', local_path, url)
268      urllib.urlretrieve(url, download_path)
269      os.chmod(download_path, 0o755)
270      if (HashLocalFile(download_path) != expected_sha1):
271        os.remove(download_path)
272        logging.fatal('SHA1 mismatch for %s', download_path)
273        return 1
274      os.rename(download_path, local_path)
275    assert(HashLocalFile(local_path) == expected_sha1)
276
277    if is_zip:
278      logging.info('Extracting %s into %s' % (local_path, zip_target_dir))
279      assert(os.path.commonprefix((ROOT_DIR, zip_target_dir)) == ROOT_DIR)
280      if os.path.exists(zip_target_dir):
281        logging.info('Deleting stale dir %s' % zip_target_dir)
282        shutil.rmtree(zip_target_dir)
283
284      # Decompress the archive.
285      if local_path.endswith('.tgz'):
286        MkdirRecursive(zip_target_dir)
287        subprocess.check_call(['tar', '-zxf', local_path], cwd=zip_target_dir)
288      elif local_path.endswith('.zip'):
289        with zipfile.ZipFile(local_path, 'r') as zf:
290          for info in zf.infolist():
291            ExtractZipfilePreservePermissions(zf, info, zip_target_dir)
292
293      # If the zip contains one root folder, rebase one level up moving all
294      # its sub files and folders inside |target_dir|.
295      subdir = os.listdir(zip_target_dir)
296      if len(subdir) == 1:
297        subdir = os.path.join(zip_target_dir, subdir[0])
298        if os.path.isdir(subdir):
299          for subf in os.listdir(subdir):
300            shutil.move(os.path.join(subdir,subf), zip_target_dir)
301          os.rmdir(subdir)
302
303      # Create stamp and remove the archive.
304      with open(zip_dir_stamp, 'w') as stamp_file:
305        stamp_file.write(expected_sha1)
306      os.remove(local_path)
307
308
309if __name__ == '__main__':
310  logging.basicConfig(level=logging.INFO)
311  sys.exit(Main())
312