Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 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 | |
| 16 | import argparse |
| 17 | import hashlib |
| 18 | import logging |
| 19 | import os |
| 20 | import shutil |
Primiano Tucci | 0825bc8 | 2017-09-28 18:50:23 +0100 | [diff] [blame] | 21 | import subprocess |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 22 | import sys |
| 23 | import urllib |
| 24 | import zipfile |
| 25 | |
Primiano Tucci | d775045 | 2017-09-29 14:38:51 +0100 | [diff] [blame] | 26 | from collections import namedtuple |
| 27 | |
Primiano Tucci | 636ede1 | 2017-10-30 16:03:07 +0000 | [diff] [blame] | 28 | # When adding a new git dependency here please also add a corresponding entry in |
| 29 | # .travis.yml under the "cache:" section. |
| 30 | |
Primiano Tucci | b60d4b0 | 2017-11-10 11:03:00 +0000 | [diff] [blame] | 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 | |
Primiano Tucci | d775045 | 2017-09-29 14:38:51 +0100 | [diff] [blame] | 43 | # Dependencies required to build code on the host or when targeting desktop OS. |
| 44 | BUILD_DEPS_HOST = [ |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 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 | |
Primiano Tucci | 104bd6d | 2017-10-12 00:10:24 +0200 | [diff] [blame] | 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 | |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 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 | |
Primiano Tucci | d775045 | 2017-09-29 14:38:51 +0100 | [diff] [blame] | 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 | |
Primiano Tucci | b60d4b0 | 2017-11-10 11:03:00 +0000 | [diff] [blame] | 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. |
Primiano Tucci | d775045 | 2017-09-29 14:38:51 +0100 | [diff] [blame] | 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 | ), |
Primiano Tucci | 7278dea | 2017-10-31 11:50:32 +0000 | [diff] [blame] | 113 | ('buildtools/libunwind', |
| 114 | 'https://chromium.googlesource.com/external/llvm.org/libunwind.git', |
| 115 | '41f982e5887185b904a456e20dfcd58e6be6cc19', |
| 116 | 'all' |
| 117 | ), |
Hector Dearman | 88a1011 | 2017-10-12 11:07:10 +0100 | [diff] [blame] | 118 | |
Primiano Tucci | b60d4b0 | 2017-11-10 11:03:00 +0000 | [diff] [blame] | 119 | # Keep the revision in sync with Chrome's CLANG_REVISION in |
| 120 | # tools/clang/scripts/update.py. |
| 121 | ('buildtools/clang.tgz', |
Florian Mayer | 4b3b13b | 2018-01-19 14:06:18 +0000 | [diff] [blame] | 122 | 'https://commondatastorage.googleapis.com/chromium-browser-clang/Linux_x64/clang-321529-1.tgz', |
| 123 | '7ca04034ac7e4a956b0084a8bc604bacab94af26', |
Primiano Tucci | b60d4b0 | 2017-11-10 11:03:00 +0000 | [diff] [blame] | 124 | 'linux2' |
| 125 | ), |
| 126 | |
Hector Dearman | 88a1011 | 2017-10-12 11:07:10 +0100 | [diff] [blame] | 127 | # Benchmarking tool. |
| 128 | ('buildtools/benchmark.zip', |
| 129 | 'https://github.com/google/benchmark/archive/v1.2.0.zip', |
| 130 | '553b6bfa59c63f15c54cfd8c5122b0f7f84469af', |
| 131 | 'all' |
| 132 | ), |
Primiano Tucci | d775045 | 2017-09-29 14:38:51 +0100 | [diff] [blame] | 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 |
| 139 | BUILD_DEPS_ANDROID = [ |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 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 | ), |
Primiano Tucci | d775045 | 2017-09-29 14:38:51 +0100 | [diff] [blame] | 151 | ] |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 152 | |
Primiano Tucci | d775045 | 2017-09-29 14:38:51 +0100 | [diff] [blame] | 153 | # Dependencies required to run Android tests. |
| 154 | TEST_DEPS_ANDROID = [ |
Lalit Maganti | 367fcd5 | 2018-02-05 16:06:13 +0000 | [diff] [blame] | 155 | # Android emulator images. |
| 156 | ('buildtools/aosp-arm.zip', |
| 157 | 'https://storage.googleapis.com/perfetto/aosp-02022018-arm.zip', |
| 158 | 'a480d5e7d3ca888b0a58fe15ce76b1791537429a', |
Primiano Tucci | d775045 | 2017-09-29 14:38:51 +0100 | [diff] [blame] | 159 | 'linux2' |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 160 | ), |
| 161 | |
Primiano Tucci | d775045 | 2017-09-29 14:38:51 +0100 | [diff] [blame] | 162 | # platform-tools.zip contains adb binaries. |
| 163 | ('buildtools/android_sdk/platform-tools.zip', |
| 164 | 'https://dl.google.com/android/repository/platform-tools_r26.0.0-darwin.zip', |
| 165 | 'e75b6137dc444f777eb02f44a6d9819b3aabff82', |
| 166 | 'darwin' |
| 167 | ), |
| 168 | ('buildtools/android_sdk/platform-tools.zip', |
| 169 | 'https://dl.google.com/android/repository/platform-tools_r26.0.0-linux.zip', |
| 170 | '00de8a6631405b617c10f68cd11ff2e1cd528e23', |
| 171 | 'linux2' |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 172 | ), |
Primiano Tucci | 0825bc8 | 2017-09-28 18:50:23 +0100 | [diff] [blame] | 173 | |
Lalit Maganti | 367fcd5 | 2018-02-05 16:06:13 +0000 | [diff] [blame] | 174 | # Android emulator binaries. |
| 175 | ('buildtools/emulator', |
| 176 | 'https://android.googlesource.com/platform/prebuilts/android-emulator.git', |
| 177 | '4b260028dc27bc92c39bee9129cb2ba839970956', |
| 178 | 'all' |
Primiano Tucci | 0825bc8 | 2017-09-28 18:50:23 +0100 | [diff] [blame] | 179 | ), |
Primiano Tucci | d775045 | 2017-09-29 14:38:51 +0100 | [diff] [blame] | 180 | ] |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 181 | |
| 182 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 183 | |
| 184 | |
| 185 | def ReadFile(path): |
| 186 | if not os.path.exists(path): |
| 187 | return None |
| 188 | with open(path) as f: |
| 189 | return f.read().strip() |
| 190 | |
| 191 | |
Primiano Tucci | 0825bc8 | 2017-09-28 18:50:23 +0100 | [diff] [blame] | 192 | def MkdirRecursive(path): |
| 193 | # Works with both relative and absolute paths |
| 194 | cwd = '/' if path.startswith('/') else ROOT_DIR |
| 195 | for part in path.split('/'): |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 196 | cwd = os.path.join(cwd, part) |
| 197 | if not os.path.exists(cwd): |
| 198 | os.makedirs(cwd) |
| 199 | else: |
| 200 | assert(os.path.isdir(cwd)) |
| 201 | |
| 202 | |
| 203 | def HashLocalFile(path): |
| 204 | if not os.path.exists(path): |
| 205 | return None |
| 206 | with open(path, 'rb') as f: |
| 207 | return hashlib.sha1(f.read()).hexdigest() |
| 208 | |
| 209 | |
| 210 | def ExtractZipfilePreservePermissions(zf, info, path): |
| 211 | zf.extract(info.filename, path=path) |
| 212 | target_path = os.path.join(path, info.filename) |
| 213 | min_acls = 0o755 if info.filename.endswith('/') else 0o644 |
| 214 | os.chmod(target_path, (info.external_attr >> 16L) | min_acls) |
| 215 | |
| 216 | |
Primiano Tucci | 0825bc8 | 2017-09-28 18:50:23 +0100 | [diff] [blame] | 217 | def IsGitRepoCheckoutOutAtRevision(path, revision): |
| 218 | return ReadFile(os.path.join(path, '.git', 'HEAD')) == revision |
| 219 | |
| 220 | |
| 221 | def CheckoutGitRepo(path, git_url, revision): |
| 222 | if IsGitRepoCheckoutOutAtRevision(path, revision): |
| 223 | return |
| 224 | if os.path.exists(path): |
| 225 | shutil.rmtree(path) |
| 226 | MkdirRecursive(path) |
| 227 | logging.info('Fetching %s @ %s into %s', git_url, revision, path) |
Lalit Maganti | 367fcd5 | 2018-02-05 16:06:13 +0000 | [diff] [blame] | 228 | subprocess.check_call(['git', 'init', path], cwd=path) |
| 229 | subprocess.check_call( |
| 230 | ['git', 'fetch', '--depth', '1', git_url, revision], cwd=path) |
Primiano Tucci | 0825bc8 | 2017-09-28 18:50:23 +0100 | [diff] [blame] | 231 | subprocess.check_call(['git', 'checkout', revision, '--quiet'], cwd=path) |
| 232 | assert(IsGitRepoCheckoutOutAtRevision(path, revision)) |
| 233 | |
| 234 | |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 235 | def Main(): |
| 236 | parser = argparse.ArgumentParser() |
Primiano Tucci | d775045 | 2017-09-29 14:38:51 +0100 | [diff] [blame] | 237 | parser.add_argument('--no-android', action='store_true') |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 238 | args = parser.parse_args() |
Primiano Tucci | d775045 | 2017-09-29 14:38:51 +0100 | [diff] [blame] | 239 | deps = BUILD_DEPS_HOST |
| 240 | if not args.no_android: |
| 241 | deps += BUILD_DEPS_ANDROID + TEST_DEPS_ANDROID |
| 242 | for rel_path, url, expected_sha1, platform in deps: |
Lalit Maganti | 367fcd5 | 2018-02-05 16:06:13 +0000 | [diff] [blame] | 243 | if (platform != 'all' and platform != sys.platform): |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 244 | continue |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 245 | local_path = os.path.join(ROOT_DIR, rel_path) |
Primiano Tucci | 0825bc8 | 2017-09-28 18:50:23 +0100 | [diff] [blame] | 246 | if url.endswith('.git'): |
| 247 | CheckoutGitRepo(local_path, url, expected_sha1) |
| 248 | continue |
Primiano Tucci | b60d4b0 | 2017-11-10 11:03:00 +0000 | [diff] [blame] | 249 | is_zip = local_path.endswith('.zip') or local_path.endswith('.tgz') |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 250 | zip_target_dir = local_path[:-4] if is_zip else None |
| 251 | zip_dir_stamp = os.path.join(zip_target_dir, '.stamp') if is_zip else None |
| 252 | |
| 253 | if ((not is_zip and HashLocalFile(local_path) == expected_sha1) or |
| 254 | (is_zip and ReadFile(zip_dir_stamp) == expected_sha1)): |
| 255 | continue |
| 256 | MkdirRecursive(os.path.dirname(rel_path)) |
| 257 | if HashLocalFile(local_path) != expected_sha1: |
| 258 | download_path = local_path + '.tmp' |
| 259 | logging.info('Downloading %s from %s', local_path, url) |
| 260 | urllib.urlretrieve(url, download_path) |
| 261 | os.chmod(download_path, 0o755) |
| 262 | if (HashLocalFile(download_path) != expected_sha1): |
| 263 | os.remove(download_path) |
| 264 | logging.fatal('SHA1 mismatch for %s', download_path) |
| 265 | return 1 |
| 266 | os.rename(download_path, local_path) |
| 267 | assert(HashLocalFile(local_path) == expected_sha1) |
| 268 | |
| 269 | if is_zip: |
| 270 | logging.info('Extracting %s into %s' % (local_path, zip_target_dir)) |
| 271 | assert(os.path.commonprefix((ROOT_DIR, zip_target_dir)) == ROOT_DIR) |
| 272 | if os.path.exists(zip_target_dir): |
| 273 | logging.info('Deleting stale dir %s' % zip_target_dir) |
| 274 | shutil.rmtree(zip_target_dir) |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 275 | |
Primiano Tucci | b60d4b0 | 2017-11-10 11:03:00 +0000 | [diff] [blame] | 276 | # Decompress the archive. |
| 277 | if local_path.endswith('.tgz'): |
| 278 | MkdirRecursive(zip_target_dir) |
| 279 | subprocess.check_call(['tar', '-zxf', local_path], cwd=zip_target_dir) |
| 280 | elif local_path.endswith('.zip'): |
| 281 | with zipfile.ZipFile(local_path, 'r') as zf: |
| 282 | for info in zf.infolist(): |
| 283 | ExtractZipfilePreservePermissions(zf, info, zip_target_dir) |
| 284 | |
| 285 | # If the zip contains one root folder, rebase one level up moving all |
| 286 | # its sub files and folders inside |target_dir|. |
| 287 | subdir = os.listdir(zip_target_dir) |
| 288 | if len(subdir) == 1: |
| 289 | subdir = os.path.join(zip_target_dir, subdir[0]) |
| 290 | if os.path.isdir(subdir): |
| 291 | for subf in os.listdir(subdir): |
| 292 | shutil.move(os.path.join(subdir,subf), zip_target_dir) |
| 293 | os.rmdir(subdir) |
| 294 | |
| 295 | # Create stamp and remove the archive. |
| 296 | with open(zip_dir_stamp, 'w') as stamp_file: |
| 297 | stamp_file.write(expected_sha1) |
| 298 | os.remove(local_path) |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 299 | |
| 300 | |
| 301 | if __name__ == '__main__': |
| 302 | logging.basicConfig(level=logging.INFO) |
| 303 | sys.exit(Main()) |