blob: be1e33bd12f7617f745b0547bd96cbeb8f27b613 [file] [log] [blame]
Primiano Tucciae2879e2017-09-27 11:02:09 +09001#!/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
Primiano Tucci0825bc82017-09-28 18:50:23 +010021import subprocess
Primiano Tucciae2879e2017-09-27 11:02:09 +090022import sys
23import urllib
24import zipfile
25
Primiano Tuccid7750452017-09-29 14:38:51 +010026from collections import namedtuple
27
28# Dependencies required to build code on the host or when targeting desktop OS.
29BUILD_DEPS_HOST = [
Primiano Tucciae2879e2017-09-27 11:02:09 +090030 # GN
31 ('buildtools/mac/gn',
32 'https://storage.googleapis.com/chromium-gn/c2c934d4dda1f470a6511b1015dda9a9fb1ce50b',
33 'c2c934d4dda1f470a6511b1015dda9a9fb1ce50b',
34 'darwin'
35 ),
36 ('buildtools/linux64/gn',
37 'https://storage.googleapis.com/chromium-gn/b53fa13e950948c6f9a062189b76b34a9610281f',
38 'b53fa13e950948c6f9a062189b76b34a9610281f',
39 'linux2'
40 ),
41
Primiano Tucci104bd6d2017-10-12 00:10:24 +020042 # clang-format
43 ('buildtools/mac/clang-format',
44 'https://storage.googleapis.com/chromium-clang-format/0679b295e2ce2fce7919d1e8d003e497475f24a3',
45 '0679b295e2ce2fce7919d1e8d003e497475f24a3',
46 'darwin'
47 ),
48 ('buildtools/linux64/clang-format',
49 'https://storage.googleapis.com/chromium-clang-format/5349d1954e17f6ccafb6e6663b0f13cdb2bb33c8',
50 '5349d1954e17f6ccafb6e6663b0f13cdb2bb33c8',
51 'linux2'
52 ),
53 # Keep the SHA1 in sync with |clang_format_rev| in chromium //buildtools/DEPS.
54 ('buildtools/clang_format/script',
55 'https://chromium.googlesource.com/chromium/llvm-project/cfe/tools/clang-format.git',
56 '0653eee0c81ea04715c635dd0885e8096ff6ba6d',
57 'all'
58 ),
59
Primiano Tucciae2879e2017-09-27 11:02:09 +090060 # Ninja
61 ('buildtools/mac/ninja',
62 'https://storage.googleapis.com/fuchsia-build/fuchsia/ninja/mac/a1db595e824c50cf565fbf0af2437fd91b7babf4',
63 'a1db595e824c50cf565fbf0af2437fd91b7babf4',
64 'darwin'
65 ),
66 ('buildtools/linux64/ninja',
67 'https://storage.googleapis.com/fuchsia-build/fuchsia/ninja/linux64/d35b36c84a09f7e38b25947cafada10e8bf835bc',
68 'd35b36c84a09f7e38b25947cafada10e8bf835bc',
69 'linux2'
70 ),
71
Primiano Tuccid7750452017-09-29 14:38:51 +010072 # Keep in sync with Android's //external/googletest/README.version.
73 ('buildtools/googletest.zip',
74 'https://github.com/google/googletest/archive/ff07a5de0e81580547f1685e101194ed1a4fcd56.zip',
75 'c7edec7d7e6db1fc37a20710de9c4d89e3a3893b',
76 'all'
77 ),
78
79 # Keep in sync with Android's //external/protobuf/README.version.
80 ('buildtools/protobuf.zip',
81 'https://github.com/google/protobuf/releases/download/v3.0.0-beta-3/protobuf-cpp-3.0.0-beta-3.zip',
82 '3caec60aa9d8eefc8c3c3201b6b8ca19935edb89',
83 'all'
84 ),
85
86 # libc++ and libc++abi, for clang msan that require rebuilding the C++ lib
87 # from sources. Keep the SHA1s in sync with Chrome's src/buildtools/DEPS.
88 ('buildtools/libcxx',
89 'https://chromium.googlesource.com/chromium/llvm-project/libcxx.git',
90 '3a07dd740be63878167a0ea19fe81869954badd7',
91 'all'
92 ),
93 ('buildtools/libcxxabi',
94 'https://chromium.googlesource.com/chromium/llvm-project/libcxxabi.git',
95 '4072e8fd76febee37f60aeda76d6d9f5e3791daa',
96 'all'
97 ),
98]
99
100# Dependencies required to build Android code.
101# URLs and SHA1s taken from:
102# - https://dl.google.com/android/repository/repository-11.xml
103# - https://dl.google.com/android/repository/sys-img/android/sys-img.xml
104BUILD_DEPS_ANDROID = [
Primiano Tucciae2879e2017-09-27 11:02:09 +0900105 # Android NDK
106 ('buildtools/ndk.zip',
107 'https://dl.google.com/android/repository/android-ndk-r15c-darwin-x86_64.zip',
108 'ea4b5d76475db84745aa8828000d009625fc1f98',
109 'darwin'
110 ),
111 ('buildtools/ndk.zip',
112 'https://dl.google.com/android/repository/android-ndk-r15c-linux-x86_64.zip',
113 '0bf02d4e8b85fd770fd7b9b2cdec57f9441f27a2',
114 'linux2'
115 ),
Primiano Tuccid7750452017-09-29 14:38:51 +0100116]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900117
Primiano Tuccid7750452017-09-29 14:38:51 +0100118# Dependencies required to run Android tests.
119TEST_DEPS_ANDROID = [
120 # tools.zip contains the emulator binaries.
121 ('buildtools/android_sdk/tools.zip',
122 'https://dl.google.com/android/repository/tools_r25.2.5-macosx.zip',
123 'd2168d963ac5b616e3d3ddaf21511d084baf3659',
124 'darwin'
125 ),
126 ('buildtools/android_sdk/tools.zip',
127 'https://dl.google.com/android/repository/tools_r25.2.5-linux.zip',
128 '72df3aa1988c0a9003ccdfd7a13a7b8bd0f47fc1',
129 'linux2'
Primiano Tucciae2879e2017-09-27 11:02:09 +0900130 ),
131
Primiano Tuccid7750452017-09-29 14:38:51 +0100132 # platform-tools.zip contains adb binaries.
133 ('buildtools/android_sdk/platform-tools.zip',
134 'https://dl.google.com/android/repository/platform-tools_r26.0.0-darwin.zip',
135 'e75b6137dc444f777eb02f44a6d9819b3aabff82',
136 'darwin'
137 ),
138 ('buildtools/android_sdk/platform-tools.zip',
139 'https://dl.google.com/android/repository/platform-tools_r26.0.0-linux.zip',
140 '00de8a6631405b617c10f68cd11ff2e1cd528e23',
141 'linux2'
Primiano Tucciae2879e2017-09-27 11:02:09 +0900142 ),
Primiano Tucci0825bc82017-09-28 18:50:23 +0100143
Primiano Tuccid7750452017-09-29 14:38:51 +0100144 # Android emulator images.
145 ('buildtools/android_sdk/system-images/android-24/default/armeabi-v7a.zip',
146 'https://dl.google.com/android/repository/sys-img/android/armeabi-v7a-24_r07.zip',
147 '3454546b4eed2d6c3dd06d47757d6da9f4176033',
Primiano Tucci0825bc82017-09-28 18:50:23 +0100148 'all'
149 ),
Primiano Tuccid7750452017-09-29 14:38:51 +0100150 ('buildtools/android_sdk/system-images/android-24/default/arm64-v8a.zip',
151 'https://dl.google.com/android/repository/sys-img/android/arm64-v8a-24_r07.zip',
152 'e8ab2e49e4efe4b064232b33b5eeaded61437d7f',
Primiano Tucci0825bc82017-09-28 18:50:23 +0100153 'all'
154 ),
Primiano Tuccid7750452017-09-29 14:38:51 +0100155]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900156
157ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
158
159
160def ReadFile(path):
161 if not os.path.exists(path):
162 return None
163 with open(path) as f:
164 return f.read().strip()
165
166
Primiano Tucci0825bc82017-09-28 18:50:23 +0100167def MkdirRecursive(path):
168 # Works with both relative and absolute paths
169 cwd = '/' if path.startswith('/') else ROOT_DIR
170 for part in path.split('/'):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900171 cwd = os.path.join(cwd, part)
172 if not os.path.exists(cwd):
173 os.makedirs(cwd)
174 else:
175 assert(os.path.isdir(cwd))
176
177
178def HashLocalFile(path):
179 if not os.path.exists(path):
180 return None
181 with open(path, 'rb') as f:
182 return hashlib.sha1(f.read()).hexdigest()
183
184
185def ExtractZipfilePreservePermissions(zf, info, path):
186 zf.extract(info.filename, path=path)
187 target_path = os.path.join(path, info.filename)
188 min_acls = 0o755 if info.filename.endswith('/') else 0o644
189 os.chmod(target_path, (info.external_attr >> 16L) | min_acls)
190
191
Primiano Tucci0825bc82017-09-28 18:50:23 +0100192def IsGitRepoCheckoutOutAtRevision(path, revision):
193 return ReadFile(os.path.join(path, '.git', 'HEAD')) == revision
194
195
196def CheckoutGitRepo(path, git_url, revision):
197 if IsGitRepoCheckoutOutAtRevision(path, revision):
198 return
199 if os.path.exists(path):
200 shutil.rmtree(path)
201 MkdirRecursive(path)
202 logging.info('Fetching %s @ %s into %s', git_url, revision, path)
203 subprocess.check_call(['git', 'clone', git_url, path], cwd=path)
204 subprocess.check_call(['git', 'checkout', revision, '--quiet'], cwd=path)
205 assert(IsGitRepoCheckoutOutAtRevision(path, revision))
206
207
Primiano Tucciae2879e2017-09-27 11:02:09 +0900208def Main():
209 parser = argparse.ArgumentParser()
Primiano Tuccid7750452017-09-29 14:38:51 +0100210 parser.add_argument('--no-android', action='store_true')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900211 args = parser.parse_args()
Primiano Tuccid7750452017-09-29 14:38:51 +0100212 deps = BUILD_DEPS_HOST
213 if not args.no_android:
214 deps += BUILD_DEPS_ANDROID + TEST_DEPS_ANDROID
215 for rel_path, url, expected_sha1, platform in deps:
Primiano Tucciae2879e2017-09-27 11:02:09 +0900216 if platform != 'all' and platform != sys.platform:
217 continue
Primiano Tucciae2879e2017-09-27 11:02:09 +0900218 local_path = os.path.join(ROOT_DIR, rel_path)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100219 if url.endswith('.git'):
220 CheckoutGitRepo(local_path, url, expected_sha1)
221 continue
Primiano Tucciae2879e2017-09-27 11:02:09 +0900222 is_zip = local_path.lower().endswith('.zip')
223 zip_target_dir = local_path[:-4] if is_zip else None
224 zip_dir_stamp = os.path.join(zip_target_dir, '.stamp') if is_zip else None
225
226 if ((not is_zip and HashLocalFile(local_path) == expected_sha1) or
227 (is_zip and ReadFile(zip_dir_stamp) == expected_sha1)):
228 continue
229 MkdirRecursive(os.path.dirname(rel_path))
230 if HashLocalFile(local_path) != expected_sha1:
231 download_path = local_path + '.tmp'
232 logging.info('Downloading %s from %s', local_path, url)
233 urllib.urlretrieve(url, download_path)
234 os.chmod(download_path, 0o755)
235 if (HashLocalFile(download_path) != expected_sha1):
236 os.remove(download_path)
237 logging.fatal('SHA1 mismatch for %s', download_path)
238 return 1
239 os.rename(download_path, local_path)
240 assert(HashLocalFile(local_path) == expected_sha1)
241
242 if is_zip:
243 logging.info('Extracting %s into %s' % (local_path, zip_target_dir))
244 assert(os.path.commonprefix((ROOT_DIR, zip_target_dir)) == ROOT_DIR)
245 if os.path.exists(zip_target_dir):
246 logging.info('Deleting stale dir %s' % zip_target_dir)
247 shutil.rmtree(zip_target_dir)
248 with zipfile.ZipFile(local_path, 'r') as zf:
249 for info in zf.infolist():
250 ExtractZipfilePreservePermissions(zf, info, zip_target_dir)
251
252 # If the zip contains one root folder, rebase one level up moving all
253 # its sub files and folders inside |target_dir|.
254 subdir = os.listdir(zip_target_dir)
255 if len(subdir) == 1:
256 subdir = os.path.join(zip_target_dir, subdir[0])
257 if os.path.isdir(subdir):
258 for subf in os.listdir(subdir):
259 shutil.move(os.path.join(subdir,subf), zip_target_dir)
260 os.rmdir(subdir)
261 with open(zip_dir_stamp, 'w') as stamp_file:
262 stamp_file.write(expected_sha1)
263 os.remove(local_path)
264
265
266if __name__ == '__main__':
267 logging.basicConfig(level=logging.INFO)
268 sys.exit(Main())