1#!/usr/bin/env python3 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 19import argparse 20import glob 21import logging 22import os 23import re 24import subprocess 25import sys 26 27# Global Keys 28# All paths are relative to install_dir: prebuilts/vndk/v{version} 29ROOT_BP_PATH = 'Android.bp' 30COMMON_DIR_NAME = 'common' 31COMMON_DIR_PATH = COMMON_DIR_NAME 32COMMON_BP_PATH = os.path.join(COMMON_DIR_PATH, 'Android.bp') 33CONFIG_DIR_PATH_PATTERN = '*/configs' 34MANIFEST_FILE_NAME = 'manifest.xml' 35MODULE_PATHS_FILE_NAME = 'module_paths.txt' 36NOTICE_FILES_DIR_NAME = 'NOTICE_FILES' 37NOTICE_FILES_DIR_PATH = os.path.join(COMMON_DIR_PATH, NOTICE_FILES_DIR_NAME) 38BINDER32 = 'binder32' 39MINIMUM_VNDK_VERSION = 28 40SOONG_INTERMEDIATES_DIR = 'out/soong/.intermediates' 41GENERATED_HEADERS_DIR = 'generated-headers' 42 43def 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 51def check_call(cmd): 52 logging.debug('Running `{}`'.format(' '.join(cmd))) 53 subprocess.check_call(cmd) 54 55 56def check_output(cmd): 57 logging.debug('Running `{}`'.format(' '.join(cmd))) 58 output = subprocess.check_output(cmd, encoding='utf-8') 59 logging.debug('Output: `{}`'.format(output)) 60 return output 61 62 63def 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 76def 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 85def join_realpath(root, *args): 86 return os.path.realpath(os.path.join(root, *args)) 87 88 89def _get_dir_from_env(env_var, default): 90 return os.path.realpath(os.getenv(env_var, default)) 91 92 93def get_out_dir(android_build_top): 94 return _get_dir_from_env('OUT_DIR', join_realpath(android_build_top, 95 'out')) 96 97 98def get_dist_dir(out_dir): 99 return _get_dir_from_env('DIST_DIR', join_realpath(out_dir, 'dist')) 100 101 102def get_snapshot_archs(install_dir): 103 """Returns a list of VNDK snapshot arch flavors under install_dir. 104 105 Args: 106 install_dir: string, absolute path of prebuilts/vndk/v{version} 107 """ 108 archs = [] 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: 112 archs.append(basename) 113 return sorted(archs) 114 115 116def prebuilt_arch_from_path(path): 117 """Extracts arch of prebuilts from path relative to install_dir. 118 119 Args: 120 path: string, path relative to prebuilts/vndk/v{version} 121 122 Returns: 123 string, arch of prebuilt (e.g., 'arm' or 'arm64' or 'x86' or 'x86_64') 124 """ 125 return path.split('/')[1].split('-')[1] 126 127 128def snapshot_arch_from_path(path): 129 """Extracts VNDK snapshot arch from path relative to install_dir. 130 131 Args: 132 path: string, path relative to prebuilts/vndk/v{version} 133 134 Returns: 135 string, VNDK snapshot arch (e.g. 'arm64') 136 """ 137 return path.split('/')[0] 138 139 140def 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 155 156 157def 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 ] 171 check_call(cmd) 172 173 174def 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 """ 184 ab_tool_path = '/google/bin/releases/android/ab/ab.par' 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] 194