Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 1 | # Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file |
| 2 | # for details. All rights reserved. Use of this source code is governed by a |
| 3 | # BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | # Different utility functions used accross scripts |
| 6 | |
| 7 | import hashlib |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 8 | import json |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 9 | import os |
Tamas Kenez | 82efeb5 | 2017-06-12 13:56:22 +0200 | [diff] [blame] | 10 | import re |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 11 | import shutil |
| 12 | import subprocess |
| 13 | import sys |
Jean-Marie Henaff | 7a64eec | 2018-05-31 15:30:35 +0200 | [diff] [blame] | 14 | import tarfile |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 15 | import tempfile |
Morten Krogh-Jespersen | 16e925d | 2019-01-25 14:40:38 +0100 | [diff] [blame] | 16 | import zipfile |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 17 | |
Ian Zerny | 3709765 | 2019-04-11 13:13:27 +0200 | [diff] [blame] | 18 | import defines |
| 19 | import jdk |
| 20 | |
Christoffer Quist Adamsen | 17879c1 | 2019-01-22 16:13:54 +0100 | [diff] [blame] | 21 | ANDROID_JAR_DIR = 'third_party/android_jar/lib-v{api}' |
| 22 | ANDROID_JAR = os.path.join(ANDROID_JAR_DIR, 'android.jar') |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 23 | TOOLS_DIR = defines.TOOLS_DIR |
| 24 | REPO_ROOT = defines.REPO_ROOT |
| 25 | THIRD_PARTY = defines.THIRD_PARTY |
Christoffer Quist Adamsen | 4d9fc51 | 2022-08-11 19:59:44 +0200 | [diff] [blame] | 26 | BUNDLETOOL_JAR_DIR = os.path.join(THIRD_PARTY, 'bundletool/bundletool-1.11.0') |
| 27 | BUNDLETOOL_JAR = os.path.join(BUNDLETOOL_JAR_DIR, 'bundletool-all-1.11.0.jar') |
Morten Krogh-Jespersen | 220e570 | 2019-02-27 12:57:01 +0100 | [diff] [blame] | 28 | ANDROID_SDK = os.path.join(THIRD_PARTY, 'android_sdk') |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 29 | MEMORY_USE_TMP_FILE = 'memory_use.tmp' |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 30 | DEX_SEGMENTS_RESULT_PATTERN = re.compile('- ([^:]+): ([0-9]+)') |
Mads Ager | 12a56bc | 2017-11-27 11:51:25 +0100 | [diff] [blame] | 31 | BUILD = os.path.join(REPO_ROOT, 'build') |
Ian Zerny | 0f5fc73 | 2018-11-15 14:34:41 +0100 | [diff] [blame] | 32 | BUILD_DEPS_DIR = os.path.join(BUILD, 'deps') |
| 33 | BUILD_MAIN_DIR = os.path.join(BUILD, 'classes', 'main') |
Morten Krogh-Jespersen | 51db2b0 | 2020-11-11 12:49:26 +0100 | [diff] [blame] | 34 | BUILD_JAVA_MAIN_DIR = os.path.join(BUILD, 'classes', 'java', 'main') |
Ian Zerny | 0f5fc73 | 2018-11-15 14:34:41 +0100 | [diff] [blame] | 35 | BUILD_TEST_DIR = os.path.join(BUILD, 'classes', 'test') |
Mads Ager | 12a56bc | 2017-11-27 11:51:25 +0100 | [diff] [blame] | 36 | LIBS = os.path.join(BUILD, 'libs') |
| 37 | GENERATED_LICENSE_DIR = os.path.join(BUILD, 'generatedLicense') |
Mads Ager | a4911eb | 2017-11-22 13:19:36 +0100 | [diff] [blame] | 38 | SRC_ROOT = os.path.join(REPO_ROOT, 'src', 'main', 'java') |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 39 | TEST_ROOT = os.path.join(REPO_ROOT, 'src', 'test', 'java') |
Ian Zerny | 59dfa4c | 2019-10-25 10:34:36 +0200 | [diff] [blame] | 40 | REPO_SOURCE = 'https://r8.googlesource.com/r8' |
Søren Gjesse | dc9d8a2 | 2017-10-12 12:40:59 +0200 | [diff] [blame] | 41 | |
| 42 | D8 = 'd8' |
| 43 | R8 = 'r8' |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 44 | R8LIB = 'r8lib' |
Morten Krogh-Jespersen | e28db46 | 2019-01-09 13:32:15 +0100 | [diff] [blame] | 45 | R8LIB_NO_DEPS = 'r8LibNoDeps' |
Morten Krogh-Jespersen | 98ee89a | 2021-10-25 20:59:02 +0200 | [diff] [blame] | 46 | R8RETRACE = 'R8Retrace' |
| 47 | R8RETRACE_NO_DEPS = 'R8RetraceNoDeps' |
Mads Ager | b10c07f | 2017-11-27 13:25:52 +0100 | [diff] [blame] | 48 | R8_SRC = 'sourceJar' |
Søren Gjesse | 17fc67d | 2019-12-04 14:50:17 +0100 | [diff] [blame] | 49 | LIBRARY_DESUGAR_CONVERSIONS = 'buildLibraryDesugarConversions' |
Ian Zerny | 161ff74 | 2022-01-20 12:39:40 +0100 | [diff] [blame] | 50 | R8_TESTS_TARGET = 'TestJar' |
| 51 | R8_TESTS_DEPS_TARGET = 'RepackageTestDeps' |
| 52 | R8LIB_TESTS_TARGET = 'configureTestForR8Lib' |
| 53 | R8LIB_TESTS_DEPS_TARGET = R8_TESTS_DEPS_TARGET |
Søren Gjesse | dc9d8a2 | 2017-10-12 12:40:59 +0200 | [diff] [blame] | 54 | |
Morten Krogh-Jespersen | 51db2b0 | 2020-11-11 12:49:26 +0100 | [diff] [blame] | 55 | ALL_DEPS_JAR = os.path.join(LIBS, 'deps_all.jar') |
Rico Wind | 74fab30 | 2017-10-02 07:25:33 +0200 | [diff] [blame] | 56 | R8_JAR = os.path.join(LIBS, 'r8.jar') |
Ian Zerny | 161ff74 | 2022-01-20 12:39:40 +0100 | [diff] [blame] | 57 | R8_WITH_RELOCATED_DEPS_JAR = os.path.join(LIBS, 'r8_with_relocated_deps.jar') |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 58 | R8LIB_JAR = os.path.join(LIBS, 'r8lib.jar') |
Rico Wind | 158ef9f | 2022-05-19 11:08:30 +0200 | [diff] [blame] | 59 | R8LIB_MAP = '%s.map' % R8LIB_JAR |
Mads Ager | b10c07f | 2017-11-27 13:25:52 +0100 | [diff] [blame] | 60 | R8_SRC_JAR = os.path.join(LIBS, 'r8-src.jar') |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 61 | R8LIB_EXCLUDE_DEPS_JAR = os.path.join(LIBS, 'r8lib-exclude-deps.jar') |
Tamas Kenez | 180be09 | 2018-12-05 15:23:06 +0100 | [diff] [blame] | 62 | R8_FULL_EXCLUDE_DEPS_JAR = os.path.join(LIBS, 'r8-full-exclude-deps.jar') |
Morten Krogh-Jespersen | 98ee89a | 2021-10-25 20:59:02 +0200 | [diff] [blame] | 63 | R8RETRACE_JAR = os.path.join(LIBS, 'r8retrace.jar') |
| 64 | R8RETRACE_EXCLUDE_DEPS_JAR = os.path.join(LIBS, 'r8retrace-exclude-deps.jar') |
Ian Zerny | 161ff74 | 2022-01-20 12:39:40 +0100 | [diff] [blame] | 65 | R8_TESTS_JAR = os.path.join(LIBS, 'r8tests.jar') |
| 66 | R8LIB_TESTS_JAR = os.path.join(LIBS, 'r8libtestdeps-cf.jar') |
| 67 | R8_TESTS_DEPS_JAR = os.path.join(LIBS, 'test_deps_all.jar') |
| 68 | R8LIB_TESTS_DEPS_JAR = R8_TESTS_DEPS_JAR |
Mads Ager | 12a56bc | 2017-11-27 11:51:25 +0100 | [diff] [blame] | 69 | MAVEN_ZIP = os.path.join(LIBS, 'r8.zip') |
Rico Wind | 8fc8bfa | 2019-03-22 09:57:36 +0100 | [diff] [blame] | 70 | MAVEN_ZIP_LIB = os.path.join(LIBS, 'r8lib.zip') |
Søren Gjesse | e18fa6e | 2022-06-24 15:14:53 +0200 | [diff] [blame] | 71 | LIBRARY_DESUGAR_CONVERSIONS_LEGACY_ZIP = os.path.join(LIBS, 'library_desugar_conversions_legacy.jar') |
Clément Béra | 00aedde | 2022-06-22 12:48:35 +0200 | [diff] [blame] | 72 | LIBRARY_DESUGAR_CONVERSIONS_ZIP = os.path.join(LIBS, 'library_desugar_conversions.jar') |
Søren Gjesse | 17fc67d | 2019-12-04 14:50:17 +0100 | [diff] [blame] | 73 | |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 74 | DESUGAR_CONFIGURATION = os.path.join( |
Søren Gjesse | 927a92e | 2019-12-04 15:18:06 +0100 | [diff] [blame] | 75 | 'src', 'library_desugar', 'desugar_jdk_libs.json') |
Søren Gjesse | ee086b2 | 2020-10-30 11:46:39 +0100 | [diff] [blame] | 76 | DESUGAR_IMPLEMENTATION = os.path.join( |
Søren Gjesse | 3dc207b | 2021-02-15 09:45:30 +0100 | [diff] [blame] | 77 | 'third_party', 'openjdk', 'desugar_jdk_libs', 'desugar_jdk_libs.jar') |
Søren Gjesse | 705a3b1 | 2022-03-17 11:37:30 +0100 | [diff] [blame] | 78 | DESUGAR_CONFIGURATION_JDK11_LEGACY = os.path.join( |
| 79 | 'src', 'library_desugar', 'jdk11', 'desugar_jdk_libs_legacy.json') |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 80 | DESUGAR_CONFIGURATION_JDK11_MINIMAL = os.path.join( |
| 81 | 'src', 'library_desugar', 'jdk11', 'desugar_jdk_libs_minimal.json') |
| 82 | DESUGAR_CONFIGURATION_JDK11 = os.path.join( |
| 83 | 'src', 'library_desugar', 'jdk11', 'desugar_jdk_libs.json') |
| 84 | DESUGAR_CONFIGURATION_JDK11_NIO = os.path.join( |
| 85 | 'src', 'library_desugar', 'jdk11', 'desugar_jdk_libs_nio.json') |
Søren Gjesse | 705a3b1 | 2022-03-17 11:37:30 +0100 | [diff] [blame] | 86 | DESUGAR_IMPLEMENTATION_JDK11 = os.path.join( |
| 87 | 'third_party', 'openjdk', 'desugar_jdk_libs_11', 'desugar_jdk_libs.jar') |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 88 | DESUGAR_CONFIGURATION_MAVEN_ZIP = os.path.join( |
| 89 | LIBS, 'desugar_jdk_libs_configuration.zip') |
Søren Gjesse | e18fa6e | 2022-06-24 15:14:53 +0200 | [diff] [blame] | 90 | DESUGAR_CONFIGURATION_JDK11_LEGACY_MAVEN_ZIP = os.path.join( |
| 91 | LIBS, 'desugar_jdk_libs_configuration_jdk11_legacy.zip') |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 92 | DESUGAR_CONFIGURATION_JDK11_MINIMAL_MAVEN_ZIP = os.path.join( |
| 93 | LIBS, 'desugar_jdk_libs_configuration_jdk11_minimal.zip') |
| 94 | DESUGAR_CONFIGURATION_JDK11_MAVEN_ZIP = os.path.join( |
| 95 | LIBS, 'desugar_jdk_libs_configuration_jdk11.zip') |
| 96 | DESUGAR_CONFIGURATION_JDK11_NIO_MAVEN_ZIP = os.path.join( |
| 97 | LIBS, 'desugar_jdk_libs_configuration_jdk11_nio.zip') |
Mads Ager | 12a56bc | 2017-11-27 11:51:25 +0100 | [diff] [blame] | 98 | GENERATED_LICENSE = os.path.join(GENERATED_LICENSE_DIR, 'LICENSE') |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 99 | RT_JAR = os.path.join(REPO_ROOT, 'third_party/openjdk/openjdk-rt-1.8/rt.jar') |
Mathias Rav | b46dc00 | 2018-06-06 09:37:11 +0200 | [diff] [blame] | 100 | R8LIB_KEEP_RULES = os.path.join(REPO_ROOT, 'src/main/keep.txt') |
Morten Krogh-Jespersen | 480784d | 2019-02-05 08:10:46 +0100 | [diff] [blame] | 101 | CF_SEGMENTS_TOOL = os.path.join(THIRD_PARTY, 'cf_segments') |
Morten Krogh-Jespersen | 38c7ca0 | 2019-02-04 10:39:57 +0100 | [diff] [blame] | 102 | PINNED_R8_JAR = os.path.join(REPO_ROOT, 'third_party/r8/r8.jar') |
| 103 | PINNED_PGR8_JAR = os.path.join(REPO_ROOT, 'third_party/r8/r8-pg6.0.1.jar') |
Ian Zerny | fbb1f7a | 2019-05-02 14:34:13 +0200 | [diff] [blame] | 104 | SAMPLE_LIBRARIES_SHA_FILE = os.path.join( |
| 105 | THIRD_PARTY, 'sample_libraries.tar.gz.sha1') |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 106 | OPENSOURCE_DUMPS_DIR = os.path.join(THIRD_PARTY, 'opensource-apps') |
Morten Krogh-Jespersen | 8622274 | 2021-03-02 11:13:33 +0100 | [diff] [blame] | 107 | INTERNAL_DUMPS_DIR = os.path.join(THIRD_PARTY, 'internal-apps') |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 108 | BAZEL_SHA_FILE = os.path.join(THIRD_PARTY, 'bazel.tar.gz.sha1') |
| 109 | BAZEL_TOOL = os.path.join(THIRD_PARTY, 'bazel') |
Søren Gjesse | 699f636 | 2019-10-09 14:56:33 +0200 | [diff] [blame] | 110 | JAVA8_SHA_FILE = os.path.join(THIRD_PARTY, 'openjdk', 'jdk8', 'linux-x86.tar.gz.sha1') |
Søren Gjesse | ef19577 | 2021-03-11 16:04:42 +0100 | [diff] [blame] | 111 | JAVA11_SHA_FILE = os.path.join(THIRD_PARTY, 'openjdk', 'jdk-11', 'linux.tar.gz.sha1') |
Søren Gjesse | eddc661 | 2022-09-02 15:38:39 +0200 | [diff] [blame] | 112 | DESUGAR_JDK_LIBS_11_SHA_FILE = os.path.join(THIRD_PARTY, 'openjdk', 'desugar_jdk_libs_11.tar.gz.sha1') |
Christoffer Quist Adamsen | 1ca046c | 2021-02-21 11:25:16 +0100 | [diff] [blame] | 113 | IGNORE_WARNINGS_RULES = os.path.join(REPO_ROOT, 'src', 'test', 'ignorewarnings.rules') |
Morten Krogh-Jespersen | 38c7ca0 | 2019-02-04 10:39:57 +0100 | [diff] [blame] | 114 | |
Morten Krogh-Jespersen | 220e570 | 2019-02-27 12:57:01 +0100 | [diff] [blame] | 115 | ANDROID_HOME_ENVIROMENT_NAME = "ANDROID_HOME" |
| 116 | ANDROID_TOOLS_VERSION_ENVIRONMENT_NAME = "ANDROID_TOOLS_VERSION" |
Morten Krogh-Jespersen | c8efedd | 2019-01-28 11:36:17 +0100 | [diff] [blame] | 117 | USER_HOME = os.path.expanduser('~') |
Morten Krogh-Jespersen | 220e570 | 2019-02-27 12:57:01 +0100 | [diff] [blame] | 118 | |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 119 | R8_TEST_RESULTS_BUCKET = 'r8-test-results' |
Rico Wind | 635b2de | 2022-04-25 10:35:14 +0200 | [diff] [blame] | 120 | R8_INTERNAL_TEST_RESULTS_BUCKET = 'r8-internal-test-results' |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 121 | |
| 122 | def archive_file(name, gs_dir, src_file): |
| 123 | gs_file = '%s/%s' % (gs_dir, name) |
Rico Wind | 0342428 | 2022-04-26 15:09:51 +0200 | [diff] [blame] | 124 | upload_file_to_cloud_storage(src_file, gs_file) |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 125 | |
| 126 | def archive_value(name, gs_dir, value): |
| 127 | with TempDir() as temp: |
| 128 | tempfile = os.path.join(temp, name); |
| 129 | with open(tempfile, 'w') as f: |
| 130 | f.write(str(value)) |
| 131 | archive_file(name, gs_dir, tempfile) |
| 132 | |
Christoffer Quist Adamsen | 4d38d03 | 2021-04-20 12:31:31 +0200 | [diff] [blame] | 133 | def find_cloud_storage_file_from_options(name, options, orElse=None): |
| 134 | # Import archive on-demand since archive depends on utils. |
| 135 | from archive import GetUploadDestination |
| 136 | hash_or_version = find_hash_or_version_from_options(options) |
| 137 | if not hash_or_version: |
| 138 | return orElse |
| 139 | is_hash = options.commit_hash is not None |
| 140 | download_path = GetUploadDestination(hash_or_version, name, is_hash) |
| 141 | if file_exists_on_cloud_storage(download_path): |
| 142 | out = tempfile.NamedTemporaryFile().name |
| 143 | download_file_from_cloud_storage(download_path, out) |
| 144 | return out |
| 145 | else: |
| 146 | raise Exception('Could not find file {} from hash/version: {}.' |
| 147 | .format(name, hash_or_version)) |
| 148 | |
| 149 | def find_r8_jar_from_options(options): |
| 150 | return find_cloud_storage_file_from_options('r8.jar', options) |
| 151 | |
| 152 | def find_r8_lib_jar_from_options(options): |
| 153 | return find_cloud_storage_file_from_options('r8lib.jar', options) |
| 154 | |
| 155 | def find_hash_or_version_from_options(options): |
| 156 | if options.tag: |
| 157 | return find_hash_or_version_from_tag(options.tag) |
| 158 | else: |
| 159 | return options.commit_hash or options.version |
| 160 | |
| 161 | def find_hash_or_version_from_tag(tag_or_hash): |
Rico Wind | fd18637 | 2022-02-28 08:55:48 +0100 | [diff] [blame] | 162 | info = subprocess.check_output([ |
Christoffer Quist Adamsen | 4d38d03 | 2021-04-20 12:31:31 +0200 | [diff] [blame] | 163 | 'git', |
| 164 | 'show', |
| 165 | tag_or_hash, |
| 166 | '-s', |
Rico Wind | fd18637 | 2022-02-28 08:55:48 +0100 | [diff] [blame] | 167 | '--format=oneline']).decode('utf-8').splitlines()[-1].split() |
Christoffer Quist Adamsen | 4d38d03 | 2021-04-20 12:31:31 +0200 | [diff] [blame] | 168 | # The info should be on the following form [hash,"Version",version] |
| 169 | if len(info) == 3 and len(info[0]) == 40 and info[1] == "Version": |
| 170 | return info[2] |
| 171 | return None |
| 172 | |
Morten Krogh-Jespersen | 220e570 | 2019-02-27 12:57:01 +0100 | [diff] [blame] | 173 | def getAndroidHome(): |
| 174 | return os.environ.get( |
| 175 | ANDROID_HOME_ENVIROMENT_NAME, os.path.join(USER_HOME, 'Android', 'Sdk')) |
| 176 | |
| 177 | def getAndroidBuildTools(): |
Christoffer Quist Adamsen | 8c803b4 | 2022-05-31 10:36:17 +0200 | [diff] [blame] | 178 | if ANDROID_TOOLS_VERSION_ENVIRONMENT_NAME in os.environ: |
| 179 | version = os.environ.get(ANDROID_TOOLS_VERSION_ENVIRONMENT_NAME) |
| 180 | build_tools_dir = os.path.join(getAndroidHome(), 'build-tools', version) |
| 181 | assert os.path.exists(build_tools_dir) |
| 182 | return build_tools_dir |
| 183 | else: |
| 184 | versions = ['30.0.3', '30.0.2', '30.0.1', '30.0.0'] |
| 185 | for version in versions: |
| 186 | build_tools_dir = os.path.join(getAndroidHome(), 'build-tools', version) |
| 187 | if os.path.exists(build_tools_dir): |
| 188 | return build_tools_dir |
| 189 | raise Exception('Unable to find Android build-tools') |
Morten Krogh-Jespersen | c8efedd | 2019-01-28 11:36:17 +0100 | [diff] [blame] | 190 | |
Christoffer Quist Adamsen | 5c9ded1 | 2021-01-14 14:29:37 +0100 | [diff] [blame] | 191 | def is_python3(): |
| 192 | return sys.version_info.major == 3 |
| 193 | |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 194 | def Print(s, quiet=False): |
| 195 | if quiet: |
| 196 | return |
| 197 | print(s) |
| 198 | |
| 199 | def Warn(message): |
| 200 | CRED = '\033[91m' |
| 201 | CEND = '\033[0m' |
| 202 | print(CRED + message + CEND) |
| 203 | |
| 204 | def PrintCmd(cmd, env=None, quiet=False): |
| 205 | if quiet: |
| 206 | return |
| 207 | if type(cmd) is list: |
| 208 | cmd = ' '.join(cmd) |
| 209 | if env: |
| 210 | env = ' '.join(['{}=\"{}\"'.format(x, y) for x, y in env.iteritems()]) |
| 211 | print('Running: {} {}'.format(env, cmd)) |
| 212 | else: |
| 213 | print('Running: {}'.format(cmd)) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 214 | # I know this will hit os on windows eventually if we don't do this. |
| 215 | sys.stdout.flush() |
| 216 | |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 217 | class ProgressLogger(object): |
| 218 | CLEAR_LINE = '\033[K' |
| 219 | UP = '\033[F' |
| 220 | |
| 221 | def __init__(self, quiet=False): |
| 222 | self._count = 0 |
| 223 | self._has_printed = False |
| 224 | self._quiet = quiet |
| 225 | |
| 226 | def log(self, text): |
Christoffer Quist Adamsen | 7607ebe | 2022-06-28 11:52:46 +0200 | [diff] [blame] | 227 | if len(text.strip()) == 0: |
| 228 | return |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 229 | if self._quiet: |
| 230 | if self._has_printed: |
| 231 | sys.stdout.write(ProgressLogger.UP + ProgressLogger.CLEAR_LINE) |
| 232 | if len(text) > 140: |
| 233 | text = text[0:140] + '...' |
| 234 | print(text) |
| 235 | self._has_printed = True |
| 236 | |
| 237 | def done(self): |
| 238 | if self._quiet and self._has_printed: |
| 239 | sys.stdout.write(ProgressLogger.UP + ProgressLogger.CLEAR_LINE) |
| 240 | print('') |
| 241 | sys.stdout.write(ProgressLogger.UP) |
| 242 | |
Morten Krogh-Jespersen | 7cdd3a7 | 2019-03-13 14:58:25 +0100 | [diff] [blame] | 243 | def RunCmd(cmd, env_vars=None, quiet=False, fail=True, logging=True): |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 244 | PrintCmd(cmd, env=env_vars, quiet=quiet) |
| 245 | env = os.environ.copy() |
| 246 | if env_vars: |
| 247 | env.update(env_vars) |
| 248 | process = subprocess.Popen( |
| 249 | cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 250 | stdout = [] |
Morten Krogh-Jespersen | 7cdd3a7 | 2019-03-13 14:58:25 +0100 | [diff] [blame] | 251 | logger = ProgressLogger(quiet=quiet) if logging else None |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 252 | failed = False |
| 253 | while True: |
Rico Wind | 744ba75 | 2021-01-22 06:24:49 +0100 | [diff] [blame] | 254 | line = process.stdout.readline().decode('utf-8') |
| 255 | if line != '': |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 256 | stripped = line.rstrip() |
| 257 | stdout.append(stripped) |
Morten Krogh-Jespersen | 7cdd3a7 | 2019-03-13 14:58:25 +0100 | [diff] [blame] | 258 | if logger: |
| 259 | logger.log(stripped) |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 260 | # TODO(christofferqa): r8 should fail with non-zero exit code. |
Morten Krogh-Jespersen | 121c47b | 2019-01-25 09:57:21 +0100 | [diff] [blame] | 261 | if ('AssertionError:' in stripped |
| 262 | or 'CompilationError:' in stripped |
| 263 | or 'CompilationFailedException:' in stripped |
Morten Krogh-Jespersen | 5d02a6b | 2019-10-29 14:48:56 +0100 | [diff] [blame] | 264 | or 'Compilation failed' in stripped |
| 265 | or 'FAILURE:' in stripped |
| 266 | or 'org.gradle.api.ProjectConfigurationException' in stripped |
| 267 | or 'BUILD FAILED' in stripped): |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 268 | failed = True |
| 269 | else: |
Morten Krogh-Jespersen | 7cdd3a7 | 2019-03-13 14:58:25 +0100 | [diff] [blame] | 270 | if logger: |
| 271 | logger.done() |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 272 | exit_code = process.poll() |
| 273 | if exit_code or failed: |
| 274 | for line in stdout: |
| 275 | Warn(line) |
Christoffer Quist Adamsen | 7cf4c56 | 2019-03-07 10:57:33 +0100 | [diff] [blame] | 276 | if fail: |
| 277 | raise subprocess.CalledProcessError( |
| 278 | exit_code or -1, cmd, output='\n'.join(stdout)) |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 279 | return stdout |
| 280 | |
Christoffer Quist Adamsen | 7cf4c56 | 2019-03-07 10:57:33 +0100 | [diff] [blame] | 281 | def RunGradlew( |
| 282 | args, clean=True, stacktrace=True, use_daemon=False, env_vars=None, |
Morten Krogh-Jespersen | 7cdd3a7 | 2019-03-13 14:58:25 +0100 | [diff] [blame] | 283 | quiet=False, fail=True, logging=True): |
Christoffer Quist Adamsen | 7cf4c56 | 2019-03-07 10:57:33 +0100 | [diff] [blame] | 284 | cmd = ['./gradlew'] |
| 285 | if clean: |
| 286 | assert 'clean' not in args |
| 287 | cmd.append('clean') |
| 288 | if stacktrace: |
| 289 | assert '--stacktrace' not in args |
| 290 | cmd.append('--stacktrace') |
| 291 | if not use_daemon: |
| 292 | assert '--no-daemon' not in args |
| 293 | cmd.append('--no-daemon') |
| 294 | cmd.extend(args) |
Morten Krogh-Jespersen | 7cdd3a7 | 2019-03-13 14:58:25 +0100 | [diff] [blame] | 295 | return RunCmd(cmd, env_vars=env_vars, quiet=quiet, fail=fail, logging=logging) |
Christoffer Quist Adamsen | 7cf4c56 | 2019-03-07 10:57:33 +0100 | [diff] [blame] | 296 | |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 297 | def IsWindows(): |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 298 | return defines.IsWindows() |
Ian Zerny | 5fffb0a | 2019-02-11 13:54:22 +0100 | [diff] [blame] | 299 | |
| 300 | def IsLinux(): |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 301 | return defines.IsLinux() |
Ian Zerny | 5fffb0a | 2019-02-11 13:54:22 +0100 | [diff] [blame] | 302 | |
| 303 | def IsOsX(): |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 304 | return defines.IsOsX() |
Ian Zerny | 5fffb0a | 2019-02-11 13:54:22 +0100 | [diff] [blame] | 305 | |
| 306 | def EnsureDepFromGoogleCloudStorage(dep, tgz, sha1, msg): |
| 307 | if not os.path.exists(dep) or os.path.getmtime(tgz) < os.path.getmtime(sha1): |
| 308 | DownloadFromGoogleCloudStorage(sha1) |
| 309 | # Update the mtime of the tar file to make sure we do not run again unless |
| 310 | # there is an update. |
| 311 | os.utime(tgz, None) |
| 312 | else: |
Rico Wind | 3d369b4 | 2021-01-12 10:26:24 +0100 | [diff] [blame] | 313 | print('Ensure cloud dependency:', msg, 'present') |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 314 | |
Jean-Marie Henaff | e4e36d1 | 2018-04-05 10:33:50 +0200 | [diff] [blame] | 315 | def DownloadFromX20(sha1_file): |
| 316 | download_script = os.path.join(REPO_ROOT, 'tools', 'download_from_x20.py') |
| 317 | cmd = [download_script, sha1_file] |
| 318 | PrintCmd(cmd) |
| 319 | subprocess.check_call(cmd) |
| 320 | |
Rico Wind | 5959392 | 2021-03-03 09:12:36 +0100 | [diff] [blame] | 321 | def DownloadFromGoogleCloudStorage(sha1_file, bucket='r8-deps', auth=False, |
| 322 | quiet=False): |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 323 | suffix = '.bat' if IsWindows() else '' |
| 324 | download_script = 'download_from_google_storage%s' % suffix |
Rico Wind | 533e3ce | 2019-04-04 10:26:12 +0200 | [diff] [blame] | 325 | cmd = [download_script] |
| 326 | if not auth: |
| 327 | cmd.append('-n') |
| 328 | cmd.extend(['-b', bucket, '-u', '-s', sha1_file]) |
Rico Wind | 5959392 | 2021-03-03 09:12:36 +0100 | [diff] [blame] | 329 | if not quiet: |
| 330 | PrintCmd(cmd) |
| 331 | subprocess.check_call(cmd) |
| 332 | else: |
| 333 | subprocess.check_output(cmd) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 334 | |
| 335 | def get_sha1(filename): |
| 336 | sha1 = hashlib.sha1() |
| 337 | with open(filename, 'rb') as f: |
| 338 | while True: |
| 339 | chunk = f.read(1024*1024) |
| 340 | if not chunk: |
| 341 | break |
| 342 | sha1.update(chunk) |
| 343 | return sha1.hexdigest() |
| 344 | |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 345 | def is_main(): |
Rico Wind | fd18637 | 2022-02-28 08:55:48 +0100 | [diff] [blame] | 346 | remotes = subprocess.check_output(['git', 'branch', '-r', '--contains', |
| 347 | 'HEAD']).decode('utf-8') |
Rico Wind | 1b52acf | 2021-03-21 12:36:55 +0100 | [diff] [blame] | 348 | return 'origin/main' in remotes |
Rico Wind | 1b09c56 | 2019-01-17 08:53:09 +0100 | [diff] [blame] | 349 | |
Jean-Marie Henaff | 7a64eec | 2018-05-31 15:30:35 +0200 | [diff] [blame] | 350 | def get_HEAD_sha1(): |
Morten Krogh-Jespersen | e0ce6a3 | 2019-02-07 11:44:45 +0100 | [diff] [blame] | 351 | return get_HEAD_sha1_for_checkout(REPO_ROOT) |
| 352 | |
| 353 | def get_HEAD_sha1_for_checkout(checkout): |
Jean-Marie Henaff | 7a64eec | 2018-05-31 15:30:35 +0200 | [diff] [blame] | 354 | cmd = ['git', 'rev-parse', 'HEAD'] |
| 355 | PrintCmd(cmd) |
Morten Krogh-Jespersen | e0ce6a3 | 2019-02-07 11:44:45 +0100 | [diff] [blame] | 356 | with ChangedWorkingDirectory(checkout): |
Christoffer Quist Adamsen | 50930e4 | 2021-01-20 11:55:12 +0100 | [diff] [blame] | 357 | return subprocess.check_output(cmd).decode('utf-8').strip() |
Jean-Marie Henaff | 7a64eec | 2018-05-31 15:30:35 +0200 | [diff] [blame] | 358 | |
Tamas Kenez | 971eec6 | 2017-05-24 11:08:40 +0200 | [diff] [blame] | 359 | def makedirs_if_needed(path): |
| 360 | try: |
| 361 | os.makedirs(path) |
| 362 | except OSError: |
| 363 | if not os.path.isdir(path): |
| 364 | raise |
| 365 | |
Rico Wind | ef7420c | 2021-10-14 13:16:01 +0200 | [diff] [blame] | 366 | def get_gsutil(): |
| 367 | return 'gsutil.py' if os.name != 'nt' else 'gsutil.py.bat' |
| 368 | |
Rico Wind | 0342428 | 2022-04-26 15:09:51 +0200 | [diff] [blame] | 369 | def upload_dir_to_cloud_storage(directory, destination, is_html=False): |
Rico Wind | a94f01c | 2017-06-27 10:32:34 +0200 | [diff] [blame] | 370 | # Upload and make the content encoding right for viewing directly |
Rico Wind | ef7420c | 2021-10-14 13:16:01 +0200 | [diff] [blame] | 371 | cmd = [get_gsutil(), '-m', 'cp'] |
Rico Wind | d0d88cf | 2018-02-09 09:46:11 +0100 | [diff] [blame] | 372 | if is_html: |
| 373 | cmd += ['-z', 'html'] |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 374 | cmd += ['-R', directory, destination] |
Rico Wind | a94f01c | 2017-06-27 10:32:34 +0200 | [diff] [blame] | 375 | PrintCmd(cmd) |
| 376 | subprocess.check_call(cmd) |
| 377 | |
Rico Wind | 0342428 | 2022-04-26 15:09:51 +0200 | [diff] [blame] | 378 | def upload_file_to_cloud_storage(source, destination): |
Rico Wind | ef7420c | 2021-10-14 13:16:01 +0200 | [diff] [blame] | 379 | cmd = [get_gsutil(), 'cp'] |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 380 | cmd += [source, destination] |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 381 | PrintCmd(cmd) |
| 382 | subprocess.check_call(cmd) |
| 383 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 384 | def delete_file_from_cloud_storage(destination): |
Rico Wind | ef7420c | 2021-10-14 13:16:01 +0200 | [diff] [blame] | 385 | cmd = [get_gsutil(), 'rm', destination] |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 386 | PrintCmd(cmd) |
| 387 | subprocess.check_call(cmd) |
| 388 | |
Rico Wind | 4fd2dda | 2018-09-26 17:41:45 +0200 | [diff] [blame] | 389 | def ls_files_on_cloud_storage(destination): |
Rico Wind | ef7420c | 2021-10-14 13:16:01 +0200 | [diff] [blame] | 390 | cmd = [get_gsutil(), 'ls', destination] |
Rico Wind | 4fd2dda | 2018-09-26 17:41:45 +0200 | [diff] [blame] | 391 | PrintCmd(cmd) |
Rico Wind | fd18637 | 2022-02-28 08:55:48 +0100 | [diff] [blame] | 392 | return subprocess.check_output(cmd).decode('utf-8') |
Rico Wind | 4fd2dda | 2018-09-26 17:41:45 +0200 | [diff] [blame] | 393 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 394 | def cat_file_on_cloud_storage(destination, ignore_errors=False): |
Rico Wind | ef7420c | 2021-10-14 13:16:01 +0200 | [diff] [blame] | 395 | cmd = [get_gsutil(), 'cat', destination] |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 396 | PrintCmd(cmd) |
| 397 | try: |
Rico Wind | 8dadb31 | 2022-02-28 08:40:20 +0100 | [diff] [blame] | 398 | return subprocess.check_output(cmd).decode('utf-8').strip() |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 399 | except subprocess.CalledProcessError as e: |
| 400 | if ignore_errors: |
| 401 | return '' |
| 402 | else: |
| 403 | raise e |
| 404 | |
| 405 | def file_exists_on_cloud_storage(destination): |
Rico Wind | ef7420c | 2021-10-14 13:16:01 +0200 | [diff] [blame] | 406 | cmd = [get_gsutil(), 'ls', destination] |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 407 | PrintCmd(cmd) |
| 408 | return subprocess.call(cmd) == 0 |
| 409 | |
Christoffer Quist Adamsen | 870fa46 | 2020-12-15 10:50:54 +0100 | [diff] [blame] | 410 | def download_file_from_cloud_storage(source, destination, quiet=False): |
Rico Wind | ef7420c | 2021-10-14 13:16:01 +0200 | [diff] [blame] | 411 | cmd = [get_gsutil(), 'cp', source, destination] |
Christoffer Quist Adamsen | 870fa46 | 2020-12-15 10:50:54 +0100 | [diff] [blame] | 412 | PrintCmd(cmd, quiet=quiet) |
Jean-Marie Henaff | 7a64eec | 2018-05-31 15:30:35 +0200 | [diff] [blame] | 413 | subprocess.check_call(cmd) |
| 414 | |
Morten Krogh-Jespersen | 220e570 | 2019-02-27 12:57:01 +0100 | [diff] [blame] | 415 | def create_archive(name, sources=None): |
| 416 | if not sources: |
| 417 | sources = [name] |
Jean-Marie Henaff | 7a64eec | 2018-05-31 15:30:35 +0200 | [diff] [blame] | 418 | tarname = '%s.tar.gz' % name |
| 419 | with tarfile.open(tarname, 'w:gz') as tar: |
Morten Krogh-Jespersen | 5409086 | 2019-02-19 11:31:10 +0100 | [diff] [blame] | 420 | for source in sources: |
| 421 | tar.add(source) |
Jean-Marie Henaff | 7a64eec | 2018-05-31 15:30:35 +0200 | [diff] [blame] | 422 | return tarname |
| 423 | |
| 424 | def extract_dir(filename): |
| 425 | return filename[0:len(filename) - len('.tar.gz')] |
| 426 | |
| 427 | def unpack_archive(filename): |
| 428 | dest_dir = extract_dir(filename) |
| 429 | if os.path.exists(dest_dir): |
Rico Wind | 3d369b4 | 2021-01-12 10:26:24 +0100 | [diff] [blame] | 430 | print('Deleting existing dir %s' % dest_dir) |
Jean-Marie Henaff | 7a64eec | 2018-05-31 15:30:35 +0200 | [diff] [blame] | 431 | shutil.rmtree(dest_dir) |
| 432 | dirname = os.path.dirname(os.path.abspath(filename)) |
| 433 | with tarfile.open(filename, 'r:gz') as tar: |
| 434 | tar.extractall(path=dirname) |
| 435 | |
Morten Krogh-Jespersen | ec3047b | 2020-08-18 13:09:06 +0200 | [diff] [blame] | 436 | def check_gcert(): |
Ian Zerny | 86c4cfd | 2022-01-17 13:43:37 +0100 | [diff] [blame] | 437 | status = subprocess.call(['gcertstatus']) |
| 438 | if status != 0: |
| 439 | subprocess.check_call(['gcert']) |
Morten Krogh-Jespersen | 5409086 | 2019-02-19 11:31:10 +0100 | [diff] [blame] | 440 | |
Rico Wind | 1a29c4f | 2018-01-25 08:43:08 +0100 | [diff] [blame] | 441 | # Note that gcs is eventually consistent with regards to list operations. |
| 442 | # This is not a problem in our case, but don't ever use this method |
| 443 | # for synchronization. |
| 444 | def cloud_storage_exists(destination): |
Rico Wind | ef7420c | 2021-10-14 13:16:01 +0200 | [diff] [blame] | 445 | cmd = [get_gsutil(), 'ls', destination] |
Rico Wind | 1a29c4f | 2018-01-25 08:43:08 +0100 | [diff] [blame] | 446 | PrintCmd(cmd) |
| 447 | exit_code = subprocess.call(cmd) |
| 448 | return exit_code == 0 |
| 449 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 450 | class TempDir(object): |
Søren Gjesse | cbeae78 | 2019-05-21 14:14:25 +0200 | [diff] [blame] | 451 | def __init__(self, prefix='', delete=True): |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 452 | self._temp_dir = None |
| 453 | self._prefix = prefix |
Søren Gjesse | cbeae78 | 2019-05-21 14:14:25 +0200 | [diff] [blame] | 454 | self._delete = delete |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 455 | |
| 456 | def __enter__(self): |
| 457 | self._temp_dir = tempfile.mkdtemp(self._prefix) |
| 458 | return self._temp_dir |
| 459 | |
| 460 | def __exit__(self, *_): |
Søren Gjesse | cbeae78 | 2019-05-21 14:14:25 +0200 | [diff] [blame] | 461 | if self._delete: |
| 462 | shutil.rmtree(self._temp_dir, ignore_errors=True) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 463 | |
| 464 | class ChangedWorkingDirectory(object): |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 465 | def __init__(self, working_directory, quiet=False): |
| 466 | self._quiet = quiet |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 467 | self._working_directory = working_directory |
| 468 | |
| 469 | def __enter__(self): |
| 470 | self._old_cwd = os.getcwd() |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 471 | if not self._quiet: |
Rico Wind | 3d369b4 | 2021-01-12 10:26:24 +0100 | [diff] [blame] | 472 | print('Enter directory:', self._working_directory) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 473 | os.chdir(self._working_directory) |
| 474 | |
| 475 | def __exit__(self, *_): |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 476 | if not self._quiet: |
Rico Wind | 3d369b4 | 2021-01-12 10:26:24 +0100 | [diff] [blame] | 477 | print('Enter directory:', self._old_cwd) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 478 | os.chdir(self._old_cwd) |
Tamas Kenez | 82efeb5 | 2017-06-12 13:56:22 +0200 | [diff] [blame] | 479 | |
| 480 | # Reading Android CTS test_result.xml |
| 481 | |
| 482 | class CtsModule(object): |
| 483 | def __init__(self, module_name): |
| 484 | self.name = module_name |
| 485 | |
| 486 | class CtsTestCase(object): |
| 487 | def __init__(self, test_case_name): |
| 488 | self.name = test_case_name |
| 489 | |
| 490 | class CtsTest(object): |
| 491 | def __init__(self, test_name, outcome): |
| 492 | self.name = test_name |
| 493 | self.outcome = outcome |
| 494 | |
| 495 | # Generator yielding CtsModule, CtsTestCase or CtsTest from |
| 496 | # reading through a CTS test_result.xml file. |
| 497 | def read_cts_test_result(file_xml): |
| 498 | re_module = re.compile('<Module name="([^"]*)"') |
| 499 | re_test_case = re.compile('<TestCase name="([^"]*)"') |
| 500 | re_test = re.compile('<Test result="(pass|fail)" name="([^"]*)"') |
| 501 | with open(file_xml) as f: |
| 502 | for line in f: |
| 503 | m = re_module.search(line) |
| 504 | if m: |
| 505 | yield CtsModule(m.groups()[0]) |
| 506 | continue |
| 507 | m = re_test_case.search(line) |
| 508 | if m: |
| 509 | yield CtsTestCase(m.groups()[0]) |
| 510 | continue |
| 511 | m = re_test.search(line) |
| 512 | if m: |
| 513 | outcome = m.groups()[0] |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 514 | assert outcome in ['fail', 'pass'] |
Tamas Kenez | 82efeb5 | 2017-06-12 13:56:22 +0200 | [diff] [blame] | 515 | yield CtsTest(m.groups()[1], outcome == 'pass') |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 516 | |
| 517 | def grep_memoryuse(logfile): |
| 518 | re_vmhwm = re.compile('^VmHWM:[ \t]*([0-9]+)[ \t]*([a-zA-Z]*)') |
| 519 | result = None |
| 520 | with open(logfile) as f: |
| 521 | for line in f: |
| 522 | m = re_vmhwm.search(line) |
| 523 | if m: |
| 524 | groups = m.groups() |
| 525 | s = len(groups) |
| 526 | if s >= 1: |
| 527 | result = int(groups[0]) |
| 528 | if s >= 2: |
| 529 | unit = groups[1] |
| 530 | if unit == 'kB': |
| 531 | result *= 1024 |
| 532 | elif unit != '': |
| 533 | raise Exception('Unrecognized unit in memory usage log: {}' |
| 534 | .format(unit)) |
| 535 | if result is None: |
| 536 | raise Exception('No memory usage found in log: {}'.format(logfile)) |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 537 | return result |
| 538 | |
| 539 | # Return a dictionary: {segment_name -> segments_size} |
| 540 | def getDexSegmentSizes(dex_files): |
| 541 | assert len(dex_files) > 0 |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 542 | cmd = [jdk.GetJavaExecutable(), '-jar', R8_JAR, 'dexsegments'] |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 543 | cmd.extend(dex_files) |
| 544 | PrintCmd(cmd) |
Morten Krogh-Jespersen | f54dd78 | 2021-02-09 09:06:08 +0100 | [diff] [blame] | 545 | output = subprocess.check_output(cmd).decode('utf-8') |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 546 | |
| 547 | matches = DEX_SEGMENTS_RESULT_PATTERN.findall(output) |
| 548 | |
| 549 | if matches is None or len(matches) == 0: |
| 550 | raise Exception('DexSegments failed to return any output for' \ |
| 551 | ' these files: {}'.format(dex_files)) |
| 552 | |
| 553 | result = {} |
| 554 | |
| 555 | for match in matches: |
| 556 | result[match[0]] = int(match[1]) |
| 557 | |
| 558 | return result |
| 559 | |
Morten Krogh-Jespersen | 38c7ca0 | 2019-02-04 10:39:57 +0100 | [diff] [blame] | 560 | # Return a dictionary: {segment_name -> segments_size} |
| 561 | def getCfSegmentSizes(cfFile): |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 562 | cmd = [jdk.GetJavaExecutable(), |
Morten Krogh-Jespersen | 38c7ca0 | 2019-02-04 10:39:57 +0100 | [diff] [blame] | 563 | '-cp', |
Morten Krogh-Jespersen | 480784d | 2019-02-05 08:10:46 +0100 | [diff] [blame] | 564 | CF_SEGMENTS_TOOL, |
Morten Krogh-Jespersen | 38c7ca0 | 2019-02-04 10:39:57 +0100 | [diff] [blame] | 565 | 'com.android.tools.r8.cf_segments.MeasureLib', |
| 566 | cfFile] |
| 567 | PrintCmd(cmd) |
Rico Wind | fd18637 | 2022-02-28 08:55:48 +0100 | [diff] [blame] | 568 | output = subprocess.check_output(cmd).decode('utf-8') |
Morten Krogh-Jespersen | 38c7ca0 | 2019-02-04 10:39:57 +0100 | [diff] [blame] | 569 | |
| 570 | matches = DEX_SEGMENTS_RESULT_PATTERN.findall(output) |
| 571 | |
| 572 | if matches is None or len(matches) == 0: |
| 573 | raise Exception('CfSegments failed to return any output for' \ |
| 574 | ' the file: ' + cfFile) |
| 575 | |
| 576 | result = {} |
| 577 | |
| 578 | for match in matches: |
| 579 | result[match[0]] = int(match[1]) |
| 580 | |
| 581 | return result |
| 582 | |
Søren Gjesse | 1c115b5 | 2019-08-14 12:43:57 +0200 | [diff] [blame] | 583 | def get_maven_path(artifact, version): |
| 584 | return os.path.join('com', 'android', 'tools', artifact, version) |
Rico Wind | c0b1638 | 2018-05-17 13:23:43 +0200 | [diff] [blame] | 585 | |
Morten Krogh-Jespersen | 38c7ca0 | 2019-02-04 10:39:57 +0100 | [diff] [blame] | 586 | def print_cfsegments(prefix, cf_files): |
| 587 | for cf_file in cf_files: |
| 588 | for segment_name, size in getCfSegmentSizes(cf_file).items(): |
| 589 | print('{}-{}(CodeSize): {}' |
| 590 | .format(prefix, segment_name, size)) |
| 591 | |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 592 | def print_dexsegments(prefix, dex_files): |
| 593 | for segment_name, size in getDexSegmentSizes(dex_files).items(): |
| 594 | print('{}-{}(CodeSize): {}' |
| 595 | .format(prefix, segment_name, size)) |
Tamas Kenez | 2cf47cf | 2017-07-25 10:22:52 +0200 | [diff] [blame] | 596 | |
Mads Ager | bc7b2ce | 2018-02-05 11:28:47 +0100 | [diff] [blame] | 597 | # Ensure that we are not benchmarking with a google jvm. |
Tamas Kenez | 2cf47cf | 2017-07-25 10:22:52 +0200 | [diff] [blame] | 598 | def check_java_version(): |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 599 | cmd= [jdk.GetJavaExecutable(), '-version'] |
Rico Wind | fd18637 | 2022-02-28 08:55:48 +0100 | [diff] [blame] | 600 | output = subprocess.check_output(cmd, stderr = subprocess.STDOUT).decode('utf-8') |
| 601 | m = re.search('openjdk version "([^"]*)"', output) |
Tamas Kenez | 2cf47cf | 2017-07-25 10:22:52 +0200 | [diff] [blame] | 602 | if m is None: |
| 603 | raise Exception("Can't check java version: no version string in output" |
| 604 | " of 'java -version': '{}'".format(output)) |
| 605 | version = m.groups(0)[0] |
Mads Ager | bc7b2ce | 2018-02-05 11:28:47 +0100 | [diff] [blame] | 606 | m = re.search('google', version) |
| 607 | if m is not None: |
| 608 | raise Exception("Do not use google JVM for benchmarking: " + version) |
Tamas Kenez | 0cad51c | 2017-08-21 14:42:01 +0200 | [diff] [blame] | 609 | |
Christoffer Quist Adamsen | 17879c1 | 2019-01-22 16:13:54 +0100 | [diff] [blame] | 610 | def get_android_jar_dir(api): |
| 611 | return os.path.join(REPO_ROOT, ANDROID_JAR_DIR.format(api=api)) |
| 612 | |
Rico Wind | 9d70f61 | 2018-08-31 09:17:43 +0200 | [diff] [blame] | 613 | def get_android_jar(api): |
| 614 | return os.path.join(REPO_ROOT, ANDROID_JAR.format(api=api)) |
Rico Wind | da6836e | 2018-12-07 12:32:03 +0100 | [diff] [blame] | 615 | |
Christoffer Quist Adamsen | 17879c1 | 2019-01-22 16:13:54 +0100 | [diff] [blame] | 616 | def get_android_optional_jars(api): |
| 617 | android_optional_jars_dir = os.path.join(get_android_jar_dir(api), 'optional') |
| 618 | android_optional_jars = [ |
| 619 | os.path.join(android_optional_jars_dir, 'android.test.base.jar'), |
| 620 | os.path.join(android_optional_jars_dir, 'android.test.mock.jar'), |
| 621 | os.path.join(android_optional_jars_dir, 'android.test.runner.jar'), |
| 622 | os.path.join(android_optional_jars_dir, 'org.apache.http.legacy.jar') |
| 623 | ] |
| 624 | return [ |
| 625 | android_optional_jar for android_optional_jar in android_optional_jars |
| 626 | if os.path.isfile(android_optional_jar)] |
| 627 | |
Rico Wind | faaac01 | 2019-02-25 11:24:05 +0100 | [diff] [blame] | 628 | def is_bot(): |
Rico Wind | e11f439 | 2019-03-18 07:59:37 +0100 | [diff] [blame] | 629 | return 'SWARMING_BOT_ID' in os.environ |
Morten Krogh-Jespersen | 16e925d | 2019-01-25 14:40:38 +0100 | [diff] [blame] | 630 | |
| 631 | def uncompressed_size(path): |
| 632 | return sum(z.file_size for z in zipfile.ZipFile(path).infolist()) |
Morten Krogh-Jespersen | 0de1373 | 2019-03-01 08:56:39 +0100 | [diff] [blame] | 633 | |
| 634 | def getR8Version(path): |
| 635 | cmd = [jdk.GetJavaExecutable(), '-cp', path, 'com.android.tools.r8.R8', |
| 636 | '--version'] |
Rico Wind | fd18637 | 2022-02-28 08:55:48 +0100 | [diff] [blame] | 637 | output = subprocess.check_output(cmd, stderr = subprocess.STDOUT).decode('utf-8') |
Jinseong Jeon | a239423 | 2019-11-26 22:17:55 -0800 | [diff] [blame] | 638 | # output is of the form 'R8 <version> (with additional info)' |
| 639 | # so we split on '('; clean up tailing spaces; and strip off 'R8 '. |
| 640 | return output.split('(')[0].strip()[3:] |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 641 | |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 642 | def desugar_configuration_name_and_version(configuration, is_for_maven): |
| 643 | name = 'desugar_jdk_libs_configuration' |
Søren Gjesse | 705a3b1 | 2022-03-17 11:37:30 +0100 | [diff] [blame] | 644 | with open(configuration, 'r') as f: |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 645 | configuration_json = json.loads(f.read()) |
| 646 | configuration_format_version = \ |
| 647 | configuration_json.get('configuration_format_version') |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 648 | if (not configuration_format_version): |
| 649 | raise Exception( |
| 650 | 'No "configuration_format_version" found in ' + configuration) |
| 651 | if (configuration_format_version != 3 |
| 652 | and configuration_format_version != 5 |
| 653 | and configuration_format_version != (200 if is_for_maven else 100)): |
| 654 | raise Exception( |
| 655 | 'Unsupported "configuration_format_version" "%s" found in %s' |
| 656 | % (configuration_format_version, configuration)) |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 657 | version = configuration_json.get('version') |
| 658 | if not version: |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 659 | if configuration_format_version == (200 if is_for_maven else 100): |
| 660 | identifier = configuration_json.get('identifier') |
| 661 | if not identifier: |
| 662 | raise Exception( |
| 663 | 'No "identifier" found in ' + configuration) |
| 664 | identifier_split = identifier.split(':') |
| 665 | if (len(identifier_split) != 3): |
| 666 | raise Exception('Invalid "identifier" found in ' + configuration) |
| 667 | if (identifier_split[0] != 'com.tools.android'): |
| 668 | raise Exception('Invalid "identifier" found in ' + configuration) |
| 669 | if not identifier_split[1].startswith('desugar_jdk_libs_configuration'): |
| 670 | raise Exception('Invalid "identifier" found in ' + configuration) |
| 671 | name = identifier_split[1] |
| 672 | version = identifier_split[2] |
| 673 | else: |
| 674 | raise Exception( |
| 675 | 'No "version" found in ' + configuration) |
| 676 | else: |
| 677 | if configuration_format_version == (200 if is_for_maven else 100): |
| 678 | raise Exception( |
| 679 | 'No "version" expected in ' + configuration) |
| 680 | # Disallow prerelease, as older R8 versions cannot parse it causing hard to |
| 681 | # understand errors. |
| 682 | check_basic_semver_version(version, 'in ' + configuration, allowPrerelease = False) |
| 683 | return (name, version) |
Søren Gjesse | 1e17153 | 2019-09-03 09:44:22 +0200 | [diff] [blame] | 684 | |
Søren Gjesse | 4e5c6fe | 2019-11-05 17:17:43 +0100 | [diff] [blame] | 685 | class SemanticVersion: |
Søren Gjesse | 705a3b1 | 2022-03-17 11:37:30 +0100 | [diff] [blame] | 686 | def __init__(self, major, minor, patch, prerelease): |
Søren Gjesse | 4e5c6fe | 2019-11-05 17:17:43 +0100 | [diff] [blame] | 687 | self.major = major |
| 688 | self.minor = minor |
| 689 | self.patch = patch |
Søren Gjesse | 705a3b1 | 2022-03-17 11:37:30 +0100 | [diff] [blame] | 690 | self.prerelease = prerelease |
Søren Gjesse | 4e5c6fe | 2019-11-05 17:17:43 +0100 | [diff] [blame] | 691 | # Build metadata currently not suppported |
| 692 | |
| 693 | def larger_than(self, other): |
Søren Gjesse | 29d108a | 2022-04-07 10:49:49 +0200 | [diff] [blame] | 694 | if self.prerelease or other.prerelease: |
Søren Gjesse | 705a3b1 | 2022-03-17 11:37:30 +0100 | [diff] [blame] | 695 | raise Exception("Comparison with prerelease not implemented") |
Søren Gjesse | 4e5c6fe | 2019-11-05 17:17:43 +0100 | [diff] [blame] | 696 | if self.major > other.major: |
| 697 | return True |
| 698 | if self.major == other.major and self.minor > other.minor: |
| 699 | return True |
| 700 | if self.patch: |
| 701 | return (self.major == other.major |
| 702 | and self.minor == other.minor |
| 703 | and self.patch > other.patch) |
| 704 | else: |
| 705 | return False |
| 706 | |
| 707 | |
Søren Gjesse | 705a3b1 | 2022-03-17 11:37:30 +0100 | [diff] [blame] | 708 | # Check that the passed string is formatted as a basic semver version (x.y.z or x.y.z-prerelease |
| 709 | # depending on the value of allowPrerelease). |
| 710 | # See https://semver.org/. The regexp parts used are not all complient with what is suggested |
| 711 | # on https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string. |
| 712 | def check_basic_semver_version(version, error_context = '', components = 3, allowPrerelease = False): |
Søren Gjesse | 4e5c6fe | 2019-11-05 17:17:43 +0100 | [diff] [blame] | 713 | regexp = '^' |
| 714 | for x in range(components): |
| 715 | regexp += '([0-9]+)' |
| 716 | if x < components - 1: |
| 717 | regexp += '\\.' |
Søren Gjesse | 705a3b1 | 2022-03-17 11:37:30 +0100 | [diff] [blame] | 718 | if allowPrerelease: |
| 719 | # This part is from |
| 720 | # https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string |
| 721 | regexp += r'(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?' |
Søren Gjesse | 4e5c6fe | 2019-11-05 17:17:43 +0100 | [diff] [blame] | 722 | regexp += '$' |
| 723 | reg = re.compile(regexp) |
| 724 | match = reg.match(version) |
| 725 | if not match: |
Søren Gjesse | 1e17153 | 2019-09-03 09:44:22 +0200 | [diff] [blame] | 726 | raise Exception("Invalid version '" |
| 727 | + version |
| 728 | + "'" |
| 729 | + (' ' + error_context) if len(error_context) > 0 else '') |
Søren Gjesse | 4e5c6fe | 2019-11-05 17:17:43 +0100 | [diff] [blame] | 730 | if components == 2: |
Søren Gjesse | 705a3b1 | 2022-03-17 11:37:30 +0100 | [diff] [blame] | 731 | return SemanticVersion(int(match.group(1)), int(match.group(2)), None, None) |
| 732 | elif components == 3 and not allowPrerelease: |
Søren Gjesse | 4e5c6fe | 2019-11-05 17:17:43 +0100 | [diff] [blame] | 733 | return SemanticVersion( |
Søren Gjesse | 705a3b1 | 2022-03-17 11:37:30 +0100 | [diff] [blame] | 734 | int(match.group(1)), int(match.group(2)), int(match.group(3)), None) |
| 735 | elif components == 3 and allowPrerelease: |
| 736 | return SemanticVersion( |
| 737 | int(match.group(1)), int(match.group(2)), int(match.group(3)), match.group('prerelease')) |
Søren Gjesse | 4e5c6fe | 2019-11-05 17:17:43 +0100 | [diff] [blame] | 738 | else: |
| 739 | raise Exception('Argument "components" must be 2 or 3') |