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"""Installs VNDK snapshot under prebuilts/vndk/v{version}.""" 18 19import argparse 20import glob 21import logging 22import os 23import re 24import shutil 25import subprocess 26import sys 27import tempfile 28import textwrap 29 30import utils 31 32from check_gpl_license import GPLChecker 33from gen_buildfiles import GenBuildFile 34 35ANDROID_BUILD_TOP = utils.get_android_build_top() 36PREBUILTS_VNDK_DIR = utils.join_realpath(ANDROID_BUILD_TOP, 'prebuilts/vndk') 37 38 39def start_branch(build): 40 branch_name = 'update-' + (build or 'local') 41 logging.info('Creating branch {branch} in {dir}'.format( 42 branch=branch_name, dir=os.getcwd())) 43 utils.check_call(['repo', 'start', branch_name, '.']) 44 45 46def remove_old_snapshot(install_dir): 47 logging.info('Removing any old files in {}'.format(install_dir)) 48 for file in glob.glob('{}/*'.format(install_dir)): 49 try: 50 if os.path.isfile(file): 51 os.unlink(file) 52 elif os.path.isdir(file): 53 shutil.rmtree(file) 54 except Exception as error: 55 logging.error('Error: {}'.format(error)) 56 sys.exit(1) 57 58 59def install_snapshot(branch, build, local_dir, install_dir, temp_artifact_dir): 60 """Installs VNDK snapshot build artifacts to prebuilts/vndk/v{version}. 61 62 1) Fetch build artifacts from Android Build server or from local_dir 63 2) Unzip build artifacts 64 65 Args: 66 branch: string or None, branch name of build artifacts 67 build: string or None, build number of build artifacts 68 local_dir: string or None, local dir to pull artifacts from 69 install_dir: string, directory to install VNDK snapshot 70 temp_artifact_dir: string, temp directory to hold build artifacts fetched 71 from Android Build server. For 'local' option, is set to None. 72 """ 73 artifact_pattern = 'android-vndk-*.zip' 74 75 if branch and build: 76 artifact_dir = temp_artifact_dir 77 os.chdir(temp_artifact_dir) 78 logging.info('Fetching {pattern} from {branch} (bid: {build})'.format( 79 pattern=artifact_pattern, branch=branch, build=build)) 80 utils.fetch_artifact(branch, build, artifact_pattern) 81 82 manifest_pattern = 'manifest_{}.xml'.format(build) 83 logging.info('Fetching {file} from {branch} (bid: {build})'.format( 84 file=manifest_pattern, branch=branch, build=build)) 85 utils.fetch_artifact(branch, build, manifest_pattern, 86 utils.MANIFEST_FILE_NAME) 87 88 os.chdir(install_dir) 89 elif local_dir: 90 logging.info('Fetching local VNDK snapshot from {}'.format(local_dir)) 91 artifact_dir = local_dir 92 93 artifacts = glob.glob(os.path.join(artifact_dir, artifact_pattern)) 94 for artifact in artifacts: 95 logging.info('Unzipping VNDK snapshot: {}'.format(artifact)) 96 utils.check_call(['unzip', '-qn', artifact, '-d', install_dir]) 97 98 # rename {install_dir}/{arch}/include/out/soong/.intermediates 99 for soong_intermediates_dir in glob.glob(install_dir + '/*/include/' + utils.SOONG_INTERMEDIATES_DIR): 100 generated_headers_dir = soong_intermediates_dir.replace( 101 utils.SOONG_INTERMEDIATES_DIR, 102 utils.GENERATED_HEADERS_DIR 103 ) 104 os.rename(soong_intermediates_dir, generated_headers_dir) 105 106def gather_notice_files(install_dir): 107 """Gathers all NOTICE files to a common NOTICE_FILES directory.""" 108 109 common_notices_dir = utils.NOTICE_FILES_DIR_PATH 110 logging.info('Creating {} directory to gather all NOTICE files...'.format( 111 common_notices_dir)) 112 os.makedirs(common_notices_dir) 113 for arch in utils.get_snapshot_archs(install_dir): 114 notices_dir_per_arch = os.path.join(arch, utils.NOTICE_FILES_DIR_NAME) 115 if os.path.isdir(notices_dir_per_arch): 116 for notice_file in glob.glob( 117 '{}/*.txt'.format(notices_dir_per_arch)): 118 if not os.path.isfile( 119 os.path.join(common_notices_dir, 120 os.path.basename(notice_file))): 121 shutil.copy(notice_file, common_notices_dir) 122 shutil.rmtree(notices_dir_per_arch) 123 124 125def post_processe_files_if_needed(vndk_version): 126 """Renames vndkcore.libraries.txt and vndksp.libraries.txt 127 files to have version suffix. 128 Create empty vndkproduct.libraries.txt file if not exist. 129 130 Args: 131 vndk_version: int, version of VNDK snapshot 132 """ 133 def add_version_suffix(file_name): 134 logging.info('Rename {} to have version suffix'.format(file_name)) 135 target_files = glob.glob( 136 os.path.join(utils.CONFIG_DIR_PATH_PATTERN, file_name)) 137 for target_file in target_files: 138 name, ext = os.path.splitext(target_file) 139 os.rename(target_file, name + '.' + str(vndk_version) + ext) 140 def create_empty_file_if_not_exist(file_name): 141 target_dirs = glob.glob(utils.CONFIG_DIR_PATH_PATTERN) 142 for dir in target_dirs: 143 path = os.path.join(dir, file_name) 144 if os.path.isfile(path): 145 continue 146 logging.info('Creating empty file: {}'.format(path)) 147 open(path, 'a').close() 148 149 files_to_add_version_suffix = ('vndkcore.libraries.txt', 150 'vndkprivate.libraries.txt') 151 files_to_create_if_not_exist = ('vndkproduct.libraries.txt',) 152 for file_to_rename in files_to_add_version_suffix: 153 add_version_suffix(file_to_rename) 154 for file_to_create in files_to_create_if_not_exist: 155 create_empty_file_if_not_exist(file_to_create) 156 157 158def update_buildfiles(buildfile_generator): 159 logging.info('Generating root Android.bp file...') 160 buildfile_generator.generate_root_android_bp() 161 162 logging.info('Generating common/Android.bp file...') 163 buildfile_generator.generate_common_android_bp() 164 165 logging.info('Generating Android.bp files...') 166 buildfile_generator.generate_android_bp() 167 168def copy_owners(root_dir, install_dir): 169 path = os.path.dirname(__file__) 170 shutil.copy(os.path.join(root_dir, path, 'OWNERS'), install_dir) 171 172def check_gpl_license(license_checker): 173 try: 174 license_checker.check_gpl_projects() 175 except ValueError as error: 176 logging.error('***CANNOT INSTALL VNDK SNAPSHOT***: {}'.format(error)) 177 raise 178 179 180def commit(branch, build, version): 181 logging.info('Making commit...') 182 utils.check_call(['git', 'add', '.']) 183 message = textwrap.dedent("""\ 184 Update VNDK snapshot v{version} to build {build}. 185 186 Taken from branch {branch}.""").format( 187 version=version, branch=branch, build=build) 188 utils.check_call(['git', 'commit', '-m', message]) 189 190 191def get_args(): 192 parser = argparse.ArgumentParser() 193 parser.add_argument( 194 'vndk_version', 195 type=utils.vndk_version_int, 196 help='VNDK snapshot version to install, e.g. "{}".'.format( 197 utils.MINIMUM_VNDK_VERSION)) 198 parser.add_argument('-b', '--branch', help='Branch to pull build from.') 199 parser.add_argument('--build', help='Build number to pull.') 200 parser.add_argument( 201 '--local', 202 help=('Fetch local VNDK snapshot artifacts from specified local ' 203 'directory instead of Android Build server. ' 204 'Example: --local=/path/to/local/dir')) 205 parser.add_argument( 206 '--use-current-branch', 207 action='store_true', 208 help='Perform the update in the current branch. Do not repo start.') 209 parser.add_argument( 210 '--remote', 211 default='aosp', 212 help=('Remote name to fetch and check if the revision of VNDK snapshot ' 213 'is included in the source to conform GPL license. default=aosp')) 214 parser.add_argument( 215 '-v', 216 '--verbose', 217 action='count', 218 default=0, 219 help='Increase output verbosity, e.g. "-v", "-vv".') 220 return parser.parse_args() 221 222 223def main(): 224 """Program entry point.""" 225 args = get_args() 226 227 local = None 228 if args.local: 229 local = os.path.abspath(os.path.expanduser(args.local)) 230 231 if local: 232 if args.build or args.branch: 233 raise ValueError( 234 'When --local option is set, --branch or --build cannot be ' 235 'specified.') 236 elif not os.path.isdir(local): 237 raise RuntimeError( 238 'The specified local directory, {}, does not exist.'.format( 239 local)) 240 else: 241 if not (args.build and args.branch): 242 raise ValueError( 243 'Please provide both --branch and --build or set --local ' 244 'option.') 245 246 vndk_version = args.vndk_version 247 248 install_dir = os.path.join(PREBUILTS_VNDK_DIR, 'v{}'.format(vndk_version)) 249 if not os.path.isdir(install_dir): 250 raise RuntimeError( 251 'The directory for VNDK snapshot version {ver} does not exist.\n' 252 'Please request a new git project for prebuilts/vndk/v{ver} ' 253 'before installing new snapshot.'.format(ver=vndk_version)) 254 255 utils.set_logging_config(args.verbose) 256 root_dir = os.getcwd() 257 os.chdir(install_dir) 258 259 if not args.use_current_branch: 260 start_branch(args.build) 261 262 remove_old_snapshot(install_dir) 263 os.makedirs(utils.COMMON_DIR_PATH) 264 265 temp_artifact_dir = None 266 if not local: 267 temp_artifact_dir = tempfile.mkdtemp() 268 269 try: 270 install_snapshot(args.branch, args.build, local, install_dir, 271 temp_artifact_dir) 272 gather_notice_files(install_dir) 273 post_processe_files_if_needed(vndk_version) 274 275 buildfile_generator = GenBuildFile(install_dir, vndk_version) 276 update_buildfiles(buildfile_generator) 277 278 copy_owners(root_dir, install_dir) 279 280 if not local: 281 license_checker = GPLChecker(install_dir, ANDROID_BUILD_TOP, 282 temp_artifact_dir, args.remote) 283 check_gpl_license(license_checker) 284 logging.info( 285 'Successfully updated VNDK snapshot v{}'.format(vndk_version)) 286 except Exception as error: 287 logging.error('FAILED TO INSTALL SNAPSHOT: {}'.format(error)) 288 raise 289 finally: 290 if temp_artifact_dir: 291 logging.info( 292 'Deleting temp_artifact_dir: {}'.format(temp_artifact_dir)) 293 shutil.rmtree(temp_artifact_dir) 294 295 if not local: 296 commit(args.branch, args.build, vndk_version) 297 logging.info( 298 'Successfully created commit for VNDK snapshot v{}'.format( 299 vndk_version)) 300 301 logging.info('Done.') 302 303 304if __name__ == '__main__': 305 main() 306