blob: 69c7d53d80199b207eb43278fa88e166d6f9697f [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
42 # Ninja
43 ('buildtools/mac/ninja',
44 'https://storage.googleapis.com/fuchsia-build/fuchsia/ninja/mac/a1db595e824c50cf565fbf0af2437fd91b7babf4',
45 'a1db595e824c50cf565fbf0af2437fd91b7babf4',
46 'darwin'
47 ),
48 ('buildtools/linux64/ninja',
49 'https://storage.googleapis.com/fuchsia-build/fuchsia/ninja/linux64/d35b36c84a09f7e38b25947cafada10e8bf835bc',
50 'd35b36c84a09f7e38b25947cafada10e8bf835bc',
51 'linux2'
52 ),
53
Primiano Tuccid7750452017-09-29 14:38:51 +010054 # Keep in sync with Android's //external/googletest/README.version.
55 ('buildtools/googletest.zip',
56 'https://github.com/google/googletest/archive/ff07a5de0e81580547f1685e101194ed1a4fcd56.zip',
57 'c7edec7d7e6db1fc37a20710de9c4d89e3a3893b',
58 'all'
59 ),
60
61 # Keep in sync with Android's //external/protobuf/README.version.
62 ('buildtools/protobuf.zip',
63 'https://github.com/google/protobuf/releases/download/v3.0.0-beta-3/protobuf-cpp-3.0.0-beta-3.zip',
64 '3caec60aa9d8eefc8c3c3201b6b8ca19935edb89',
65 'all'
66 ),
67
68 # libc++ and libc++abi, for clang msan that require rebuilding the C++ lib
69 # from sources. Keep the SHA1s in sync with Chrome's src/buildtools/DEPS.
70 ('buildtools/libcxx',
71 'https://chromium.googlesource.com/chromium/llvm-project/libcxx.git',
72 '3a07dd740be63878167a0ea19fe81869954badd7',
73 'all'
74 ),
75 ('buildtools/libcxxabi',
76 'https://chromium.googlesource.com/chromium/llvm-project/libcxxabi.git',
77 '4072e8fd76febee37f60aeda76d6d9f5e3791daa',
78 'all'
79 ),
80]
81
82# Dependencies required to build Android code.
83# URLs and SHA1s taken from:
84# - https://dl.google.com/android/repository/repository-11.xml
85# - https://dl.google.com/android/repository/sys-img/android/sys-img.xml
86BUILD_DEPS_ANDROID = [
Primiano Tucciae2879e2017-09-27 11:02:09 +090087 # Android NDK
88 ('buildtools/ndk.zip',
89 'https://dl.google.com/android/repository/android-ndk-r15c-darwin-x86_64.zip',
90 'ea4b5d76475db84745aa8828000d009625fc1f98',
91 'darwin'
92 ),
93 ('buildtools/ndk.zip',
94 'https://dl.google.com/android/repository/android-ndk-r15c-linux-x86_64.zip',
95 '0bf02d4e8b85fd770fd7b9b2cdec57f9441f27a2',
96 'linux2'
97 ),
Primiano Tuccid7750452017-09-29 14:38:51 +010098]
Primiano Tucciae2879e2017-09-27 11:02:09 +090099
Primiano Tuccid7750452017-09-29 14:38:51 +0100100# Dependencies required to run Android tests.
101TEST_DEPS_ANDROID = [
102 # tools.zip contains the emulator binaries.
103 ('buildtools/android_sdk/tools.zip',
104 'https://dl.google.com/android/repository/tools_r25.2.5-macosx.zip',
105 'd2168d963ac5b616e3d3ddaf21511d084baf3659',
106 'darwin'
107 ),
108 ('buildtools/android_sdk/tools.zip',
109 'https://dl.google.com/android/repository/tools_r25.2.5-linux.zip',
110 '72df3aa1988c0a9003ccdfd7a13a7b8bd0f47fc1',
111 'linux2'
Primiano Tucciae2879e2017-09-27 11:02:09 +0900112 ),
113
Primiano Tuccid7750452017-09-29 14:38:51 +0100114 # platform-tools.zip contains adb binaries.
115 ('buildtools/android_sdk/platform-tools.zip',
116 'https://dl.google.com/android/repository/platform-tools_r26.0.0-darwin.zip',
117 'e75b6137dc444f777eb02f44a6d9819b3aabff82',
118 'darwin'
119 ),
120 ('buildtools/android_sdk/platform-tools.zip',
121 'https://dl.google.com/android/repository/platform-tools_r26.0.0-linux.zip',
122 '00de8a6631405b617c10f68cd11ff2e1cd528e23',
123 'linux2'
Primiano Tucciae2879e2017-09-27 11:02:09 +0900124 ),
Primiano Tucci0825bc82017-09-28 18:50:23 +0100125
Primiano Tuccid7750452017-09-29 14:38:51 +0100126 # Android emulator images.
127 ('buildtools/android_sdk/system-images/android-24/default/armeabi-v7a.zip',
128 'https://dl.google.com/android/repository/sys-img/android/armeabi-v7a-24_r07.zip',
129 '3454546b4eed2d6c3dd06d47757d6da9f4176033',
Primiano Tucci0825bc82017-09-28 18:50:23 +0100130 'all'
131 ),
Primiano Tuccid7750452017-09-29 14:38:51 +0100132 ('buildtools/android_sdk/system-images/android-24/default/arm64-v8a.zip',
133 'https://dl.google.com/android/repository/sys-img/android/arm64-v8a-24_r07.zip',
134 'e8ab2e49e4efe4b064232b33b5eeaded61437d7f',
Primiano Tucci0825bc82017-09-28 18:50:23 +0100135 'all'
136 ),
Primiano Tuccid7750452017-09-29 14:38:51 +0100137]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900138
139ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
140
141
142def ReadFile(path):
143 if not os.path.exists(path):
144 return None
145 with open(path) as f:
146 return f.read().strip()
147
148
Primiano Tucci0825bc82017-09-28 18:50:23 +0100149def MkdirRecursive(path):
150 # Works with both relative and absolute paths
151 cwd = '/' if path.startswith('/') else ROOT_DIR
152 for part in path.split('/'):
Primiano Tucciae2879e2017-09-27 11:02:09 +0900153 cwd = os.path.join(cwd, part)
154 if not os.path.exists(cwd):
155 os.makedirs(cwd)
156 else:
157 assert(os.path.isdir(cwd))
158
159
160def HashLocalFile(path):
161 if not os.path.exists(path):
162 return None
163 with open(path, 'rb') as f:
164 return hashlib.sha1(f.read()).hexdigest()
165
166
167def ExtractZipfilePreservePermissions(zf, info, path):
168 zf.extract(info.filename, path=path)
169 target_path = os.path.join(path, info.filename)
170 min_acls = 0o755 if info.filename.endswith('/') else 0o644
171 os.chmod(target_path, (info.external_attr >> 16L) | min_acls)
172
173
Primiano Tucci0825bc82017-09-28 18:50:23 +0100174def IsGitRepoCheckoutOutAtRevision(path, revision):
175 return ReadFile(os.path.join(path, '.git', 'HEAD')) == revision
176
177
178def CheckoutGitRepo(path, git_url, revision):
179 if IsGitRepoCheckoutOutAtRevision(path, revision):
180 return
181 if os.path.exists(path):
182 shutil.rmtree(path)
183 MkdirRecursive(path)
184 logging.info('Fetching %s @ %s into %s', git_url, revision, path)
185 subprocess.check_call(['git', 'clone', git_url, path], cwd=path)
186 subprocess.check_call(['git', 'checkout', revision, '--quiet'], cwd=path)
187 assert(IsGitRepoCheckoutOutAtRevision(path, revision))
188
189
Primiano Tucciae2879e2017-09-27 11:02:09 +0900190def Main():
191 parser = argparse.ArgumentParser()
Primiano Tuccid7750452017-09-29 14:38:51 +0100192 parser.add_argument('--no-android', action='store_true')
Primiano Tucciae2879e2017-09-27 11:02:09 +0900193 args = parser.parse_args()
Primiano Tuccid7750452017-09-29 14:38:51 +0100194 deps = BUILD_DEPS_HOST
195 if not args.no_android:
196 deps += BUILD_DEPS_ANDROID + TEST_DEPS_ANDROID
197 for rel_path, url, expected_sha1, platform in deps:
Primiano Tucciae2879e2017-09-27 11:02:09 +0900198 if platform != 'all' and platform != sys.platform:
199 continue
Primiano Tucciae2879e2017-09-27 11:02:09 +0900200 local_path = os.path.join(ROOT_DIR, rel_path)
Primiano Tucci0825bc82017-09-28 18:50:23 +0100201 if url.endswith('.git'):
202 CheckoutGitRepo(local_path, url, expected_sha1)
203 continue
Primiano Tucciae2879e2017-09-27 11:02:09 +0900204 is_zip = local_path.lower().endswith('.zip')
205 zip_target_dir = local_path[:-4] if is_zip else None
206 zip_dir_stamp = os.path.join(zip_target_dir, '.stamp') if is_zip else None
207
208 if ((not is_zip and HashLocalFile(local_path) == expected_sha1) or
209 (is_zip and ReadFile(zip_dir_stamp) == expected_sha1)):
210 continue
211 MkdirRecursive(os.path.dirname(rel_path))
212 if HashLocalFile(local_path) != expected_sha1:
213 download_path = local_path + '.tmp'
214 logging.info('Downloading %s from %s', local_path, url)
215 urllib.urlretrieve(url, download_path)
216 os.chmod(download_path, 0o755)
217 if (HashLocalFile(download_path) != expected_sha1):
218 os.remove(download_path)
219 logging.fatal('SHA1 mismatch for %s', download_path)
220 return 1
221 os.rename(download_path, local_path)
222 assert(HashLocalFile(local_path) == expected_sha1)
223
224 if is_zip:
225 logging.info('Extracting %s into %s' % (local_path, zip_target_dir))
226 assert(os.path.commonprefix((ROOT_DIR, zip_target_dir)) == ROOT_DIR)
227 if os.path.exists(zip_target_dir):
228 logging.info('Deleting stale dir %s' % zip_target_dir)
229 shutil.rmtree(zip_target_dir)
230 with zipfile.ZipFile(local_path, 'r') as zf:
231 for info in zf.infolist():
232 ExtractZipfilePreservePermissions(zf, info, zip_target_dir)
233
234 # If the zip contains one root folder, rebase one level up moving all
235 # its sub files and folders inside |target_dir|.
236 subdir = os.listdir(zip_target_dir)
237 if len(subdir) == 1:
238 subdir = os.path.join(zip_target_dir, subdir[0])
239 if os.path.isdir(subdir):
240 for subf in os.listdir(subdir):
241 shutil.move(os.path.join(subdir,subf), zip_target_dir)
242 os.rmdir(subdir)
243 with open(zip_dir_stamp, 'w') as stamp_file:
244 stamp_file.write(expected_sha1)
245 os.remove(local_path)
246
247
248if __name__ == '__main__':
249 logging.basicConfig(level=logging.INFO)
250 sys.exit(Main())