Inseob Kim | b462b9c | 2020-08-11 15:38:20 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Jae Shin | 5233fe1 | 2017-12-18 22:29:48 +0900 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2017 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | """Utility functions for VNDK snapshot.""" |
| 18 | |
Justin Yun | ef95d09 | 2020-11-30 10:19:43 +0900 | [diff] [blame] | 19 | import argparse |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 20 | import glob |
Jae Shin | 9407521 | 2018-06-14 14:54:34 +0900 | [diff] [blame] | 21 | import logging |
Jae Shin | 5233fe1 | 2017-12-18 22:29:48 +0900 | [diff] [blame] | 22 | import os |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 23 | import re |
Jae Shin | 25d3abf | 2018-04-18 18:00:26 +0900 | [diff] [blame] | 24 | import subprocess |
Jae Shin | 5233fe1 | 2017-12-18 22:29:48 +0900 | [diff] [blame] | 25 | import sys |
| 26 | |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 27 | # Global Keys |
| 28 | # All paths are relative to install_dir: prebuilts/vndk/v{version} |
Jaewoong Jung | 6a5aaca | 2019-01-17 15:41:06 -0800 | [diff] [blame] | 29 | ROOT_BP_PATH = 'Android.bp' |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 30 | COMMON_DIR_NAME = 'common' |
| 31 | COMMON_DIR_PATH = COMMON_DIR_NAME |
Jaewoong Jung | 6fbb9d2 | 2018-11-28 09:25:22 -0800 | [diff] [blame] | 32 | COMMON_BP_PATH = os.path.join(COMMON_DIR_PATH, 'Android.bp') |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 33 | CONFIG_DIR_PATH_PATTERN = '*/configs' |
| 34 | MANIFEST_FILE_NAME = 'manifest.xml' |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 35 | MODULE_PATHS_FILE_NAME = 'module_paths.txt' |
| 36 | NOTICE_FILES_DIR_NAME = 'NOTICE_FILES' |
| 37 | NOTICE_FILES_DIR_PATH = os.path.join(COMMON_DIR_PATH, NOTICE_FILES_DIR_NAME) |
Jae Shin | af0c003 | 2018-06-21 11:54:05 +0900 | [diff] [blame] | 38 | BINDER32 = 'binder32' |
Justin Yun | ef95d09 | 2020-11-30 10:19:43 +0900 | [diff] [blame] | 39 | MINIMUM_VNDK_VERSION = 28 |
Inseob Kim | 6547080 | 2021-09-24 13:17:06 +0900 | [diff] [blame] | 40 | SOONG_INTERMEDIATES_DIR = 'out/soong/.intermediates' |
| 41 | GENERATED_HEADERS_DIR = 'generated-headers' |
Jae Shin | 5233fe1 | 2017-12-18 22:29:48 +0900 | [diff] [blame] | 42 | |
Jae Shin | 9407521 | 2018-06-14 14:54:34 +0900 | [diff] [blame] | 43 | def set_logging_config(verbose_level): |
| 44 | verbose_map = (logging.WARNING, logging.INFO, logging.DEBUG) |
| 45 | verbosity = min(verbose_level, 2) |
| 46 | logging.basicConfig( |
| 47 | format='%(levelname)-8s [%(filename)s:%(lineno)d] %(message)s', |
| 48 | level=verbose_map[verbosity]) |
| 49 | |
| 50 | |
Jae Shin | bd82ebb | 2018-06-21 14:13:46 +0900 | [diff] [blame] | 51 | def check_call(cmd): |
| 52 | logging.debug('Running `{}`'.format(' '.join(cmd))) |
Jae Shin | 9407521 | 2018-06-14 14:54:34 +0900 | [diff] [blame] | 53 | subprocess.check_call(cmd) |
| 54 | |
| 55 | |
Jae Shin | bd82ebb | 2018-06-21 14:13:46 +0900 | [diff] [blame] | 56 | def check_output(cmd): |
| 57 | logging.debug('Running `{}`'.format(' '.join(cmd))) |
Inseob Kim | b462b9c | 2020-08-11 15:38:20 +0900 | [diff] [blame] | 58 | output = subprocess.check_output(cmd, encoding='utf-8') |
Jae Shin | bd82ebb | 2018-06-21 14:13:46 +0900 | [diff] [blame] | 59 | logging.debug('Output: `{}`'.format(output)) |
Jae Shin | ce3bb75 | 2018-06-18 12:40:53 +0900 | [diff] [blame] | 60 | return output |
| 61 | |
| 62 | |
Justin Yun | ef95d09 | 2020-11-30 10:19:43 +0900 | [diff] [blame] | 63 | def vndk_version_int(vndk_version): |
| 64 | """Used for a type keyword argument in the argparse. |
| 65 | It checks if vndk version is in the supported versions. |
| 66 | """ |
| 67 | version_int = int(vndk_version) |
| 68 | if version_int < MINIMUM_VNDK_VERSION: |
| 69 | raise argparse.ArgumentTypeError( |
| 70 | 'The VNDK version {input} is not supported. ' |
| 71 | 'It must be no smaller than {min_vndk}.'.format( |
| 72 | input=version_int, min_vndk=MINIMUM_VNDK_VERSION)) |
| 73 | return version_int |
| 74 | |
| 75 | |
Jae Shin | 5233fe1 | 2017-12-18 22:29:48 +0900 | [diff] [blame] | 76 | def get_android_build_top(): |
| 77 | ANDROID_BUILD_TOP = os.getenv('ANDROID_BUILD_TOP') |
| 78 | if not ANDROID_BUILD_TOP: |
| 79 | print('Error: Missing ANDROID_BUILD_TOP env variable. Please run ' |
| 80 | '\'. build/envsetup.sh; lunch <build target>\'. Exiting script.') |
| 81 | sys.exit(1) |
| 82 | return ANDROID_BUILD_TOP |
| 83 | |
| 84 | |
| 85 | def join_realpath(root, *args): |
| 86 | return os.path.realpath(os.path.join(root, *args)) |
| 87 | |
| 88 | |
| 89 | def _get_dir_from_env(env_var, default): |
| 90 | return os.path.realpath(os.getenv(env_var, default)) |
| 91 | |
| 92 | |
| 93 | def get_out_dir(android_build_top): |
| 94 | return _get_dir_from_env('OUT_DIR', join_realpath(android_build_top, |
| 95 | 'out')) |
| 96 | |
| 97 | |
| 98 | def get_dist_dir(out_dir): |
| 99 | return _get_dir_from_env('DIST_DIR', join_realpath(out_dir, 'dist')) |
| 100 | |
| 101 | |
Jae Shin | af0c003 | 2018-06-21 11:54:05 +0900 | [diff] [blame] | 102 | def get_snapshot_archs(install_dir): |
| 103 | """Returns a list of VNDK snapshot arch flavors under install_dir. |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 104 | |
| 105 | Args: |
| 106 | install_dir: string, absolute path of prebuilts/vndk/v{version} |
| 107 | """ |
Jae Shin | af0c003 | 2018-06-21 11:54:05 +0900 | [diff] [blame] | 108 | archs = [] |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 109 | for file in glob.glob('{}/*'.format(install_dir)): |
| 110 | basename = os.path.basename(file) |
| 111 | if os.path.isdir(file) and basename != COMMON_DIR_NAME: |
Jae Shin | af0c003 | 2018-06-21 11:54:05 +0900 | [diff] [blame] | 112 | archs.append(basename) |
Yo Chiang | f2480f8 | 2020-06-10 14:52:37 +0800 | [diff] [blame] | 113 | return sorted(archs) |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 114 | |
| 115 | |
Jae Shin | af0c003 | 2018-06-21 11:54:05 +0900 | [diff] [blame] | 116 | def prebuilt_arch_from_path(path): |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 117 | """Extracts arch of prebuilts from path relative to install_dir. |
Jae Shin | 5233fe1 | 2017-12-18 22:29:48 +0900 | [diff] [blame] | 118 | |
| 119 | Args: |
| 120 | path: string, path relative to prebuilts/vndk/v{version} |
| 121 | |
| 122 | Returns: |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 123 | string, arch of prebuilt (e.g., 'arm' or 'arm64' or 'x86' or 'x86_64') |
Jae Shin | 5233fe1 | 2017-12-18 22:29:48 +0900 | [diff] [blame] | 124 | """ |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 125 | return path.split('/')[1].split('-')[1] |
| 126 | |
| 127 | |
Jae Shin | af0c003 | 2018-06-21 11:54:05 +0900 | [diff] [blame] | 128 | def snapshot_arch_from_path(path): |
| 129 | """Extracts VNDK snapshot arch from path relative to install_dir. |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 130 | |
| 131 | Args: |
| 132 | path: string, path relative to prebuilts/vndk/v{version} |
| 133 | |
| 134 | Returns: |
Jae Shin | af0c003 | 2018-06-21 11:54:05 +0900 | [diff] [blame] | 135 | string, VNDK snapshot arch (e.g. 'arm64') |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 136 | """ |
| 137 | return path.split('/')[0] |
Jae Shin | 5233fe1 | 2017-12-18 22:29:48 +0900 | [diff] [blame] | 138 | |
| 139 | |
| 140 | def find(path, names): |
| 141 | """Returns a list of files in a directory that match the given names. |
| 142 | |
| 143 | Args: |
| 144 | path: string, absolute path of directory from which to find files |
| 145 | names: list of strings, names of the files to find |
| 146 | """ |
| 147 | found = [] |
| 148 | for root, _, files in os.walk(path): |
| 149 | for file_name in sorted(files): |
| 150 | if file_name in names: |
| 151 | abspath = os.path.abspath(os.path.join(root, file_name)) |
| 152 | rel_to_root = abspath.replace(os.path.abspath(path), '') |
| 153 | found.append(rel_to_root[1:]) # strip leading / |
| 154 | return found |
Jae Shin | 25d3abf | 2018-04-18 18:00:26 +0900 | [diff] [blame] | 155 | |
| 156 | |
| 157 | def fetch_artifact(branch, build, pattern, destination='.'): |
| 158 | """Fetches build artifacts from Android Build server. |
| 159 | |
| 160 | Args: |
| 161 | branch: string, branch to pull build artifacts from |
| 162 | build: string, build number to pull build artifacts from |
| 163 | pattern: string, pattern of build artifact file name |
| 164 | destination: string, destination to pull build artifact to |
| 165 | """ |
| 166 | fetch_artifact_path = '/google/data/ro/projects/android/fetch_artifact' |
| 167 | cmd = [ |
| 168 | fetch_artifact_path, '--branch', branch, '--target=vndk', '--bid', |
| 169 | build, pattern, destination |
| 170 | ] |
Jae Shin | bd82ebb | 2018-06-21 14:13:46 +0900 | [diff] [blame] | 171 | check_call(cmd) |
Justin Yun | 9d2e85e | 2022-11-25 15:18:11 +0900 | [diff] [blame] | 172 | |
| 173 | |
| 174 | def get_latest_vndk_bid(branch): |
| 175 | """Get the build id of the latest green build of the vndk target |
| 176 | from the Android Build Server. |
| 177 | |
| 178 | Args: |
| 179 | branch: string, branch to pull build artifacts from |
| 180 | |
| 181 | Returns: |
| 182 | string: bid of the latest green build |
| 183 | """ |
Justin Yun | d7cd02f | 2023-06-19 17:56:26 +0900 | [diff] [blame] | 184 | ab_tool_path = '/google/bin/releases/android/ab/ab.par' |
Justin Yun | 9d2e85e | 2022-11-25 15:18:11 +0900 | [diff] [blame] | 185 | cmd = [ |
| 186 | ab_tool_path, 'lkgb', '--branch', branch, '--target', 'vndk' |
| 187 | ] |
| 188 | |
| 189 | # output will be 'branch target build_id status successful' |
| 190 | output = check_output(cmd).strip() |
| 191 | |
| 192 | # return build_id from the output |
| 193 | return output.split()[2] |