blob: 059f47718e7c90abaee873d7c1f3a42baac0977c [file] [log] [blame]
Inseob Kimb462b9c2020-08-11 15:38:20 +09001#!/usr/bin/env python3
Jae Shin5233fe12017-12-18 22:29:48 +09002#
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 Yunef95d092020-11-30 10:19:43 +090019import argparse
Jae Shin4db81a52017-12-29 13:24:54 +090020import glob
Jae Shin94075212018-06-14 14:54:34 +090021import logging
Jae Shin5233fe12017-12-18 22:29:48 +090022import os
Jae Shin4db81a52017-12-29 13:24:54 +090023import re
Jae Shin25d3abf2018-04-18 18:00:26 +090024import subprocess
Jae Shin5233fe12017-12-18 22:29:48 +090025import sys
26
Jae Shin4db81a52017-12-29 13:24:54 +090027# Global Keys
28# All paths are relative to install_dir: prebuilts/vndk/v{version}
Jaewoong Jung6a5aaca2019-01-17 15:41:06 -080029ROOT_BP_PATH = 'Android.bp'
Jae Shin4db81a52017-12-29 13:24:54 +090030COMMON_DIR_NAME = 'common'
31COMMON_DIR_PATH = COMMON_DIR_NAME
Jaewoong Jung6fbb9d22018-11-28 09:25:22 -080032COMMON_BP_PATH = os.path.join(COMMON_DIR_PATH, 'Android.bp')
Jae Shin4db81a52017-12-29 13:24:54 +090033CONFIG_DIR_PATH_PATTERN = '*/configs'
34MANIFEST_FILE_NAME = 'manifest.xml'
Jae Shin4db81a52017-12-29 13:24:54 +090035MODULE_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)
Jae Shinaf0c0032018-06-21 11:54:05 +090038BINDER32 = 'binder32'
Justin Yunef95d092020-11-30 10:19:43 +090039MINIMUM_VNDK_VERSION = 28
Inseob Kim65470802021-09-24 13:17:06 +090040SOONG_INTERMEDIATES_DIR = 'out/soong/.intermediates'
41GENERATED_HEADERS_DIR = 'generated-headers'
Jae Shin5233fe12017-12-18 22:29:48 +090042
Jae Shin94075212018-06-14 14:54:34 +090043def 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 Shinbd82ebb2018-06-21 14:13:46 +090051def check_call(cmd):
52 logging.debug('Running `{}`'.format(' '.join(cmd)))
Jae Shin94075212018-06-14 14:54:34 +090053 subprocess.check_call(cmd)
54
55
Jae Shinbd82ebb2018-06-21 14:13:46 +090056def check_output(cmd):
57 logging.debug('Running `{}`'.format(' '.join(cmd)))
Inseob Kimb462b9c2020-08-11 15:38:20 +090058 output = subprocess.check_output(cmd, encoding='utf-8')
Jae Shinbd82ebb2018-06-21 14:13:46 +090059 logging.debug('Output: `{}`'.format(output))
Jae Shince3bb752018-06-18 12:40:53 +090060 return output
61
62
Justin Yunef95d092020-11-30 10:19:43 +090063def 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 Shin5233fe12017-12-18 22:29:48 +090076def 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
Jae Shinaf0c0032018-06-21 11:54:05 +0900102def get_snapshot_archs(install_dir):
103 """Returns a list of VNDK snapshot arch flavors under install_dir.
Jae Shin4db81a52017-12-29 13:24:54 +0900104
105 Args:
106 install_dir: string, absolute path of prebuilts/vndk/v{version}
107 """
Jae Shinaf0c0032018-06-21 11:54:05 +0900108 archs = []
Jae Shin4db81a52017-12-29 13:24:54 +0900109 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 Shinaf0c0032018-06-21 11:54:05 +0900112 archs.append(basename)
Yo Chiangf2480f82020-06-10 14:52:37 +0800113 return sorted(archs)
Jae Shin4db81a52017-12-29 13:24:54 +0900114
115
Jae Shinaf0c0032018-06-21 11:54:05 +0900116def prebuilt_arch_from_path(path):
Jae Shin4db81a52017-12-29 13:24:54 +0900117 """Extracts arch of prebuilts from path relative to install_dir.
Jae Shin5233fe12017-12-18 22:29:48 +0900118
119 Args:
120 path: string, path relative to prebuilts/vndk/v{version}
121
122 Returns:
Jae Shin4db81a52017-12-29 13:24:54 +0900123 string, arch of prebuilt (e.g., 'arm' or 'arm64' or 'x86' or 'x86_64')
Jae Shin5233fe12017-12-18 22:29:48 +0900124 """
Jae Shin4db81a52017-12-29 13:24:54 +0900125 return path.split('/')[1].split('-')[1]
126
127
Jae Shinaf0c0032018-06-21 11:54:05 +0900128def snapshot_arch_from_path(path):
129 """Extracts VNDK snapshot arch from path relative to install_dir.
Jae Shin4db81a52017-12-29 13:24:54 +0900130
131 Args:
132 path: string, path relative to prebuilts/vndk/v{version}
133
134 Returns:
Jae Shinaf0c0032018-06-21 11:54:05 +0900135 string, VNDK snapshot arch (e.g. 'arm64')
Jae Shin4db81a52017-12-29 13:24:54 +0900136 """
137 return path.split('/')[0]
Jae Shin5233fe12017-12-18 22:29:48 +0900138
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
Jae Shin25d3abf2018-04-18 18:00:26 +0900155
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 ]
Jae Shinbd82ebb2018-06-21 14:13:46 +0900171 check_call(cmd)
Justin Yun9d2e85e2022-11-25 15:18:11 +0900172
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 """
Justin Yund7cd02f2023-06-19 17:56:26 +0900184 ab_tool_path = '/google/bin/releases/android/ab/ab.par'
Justin Yun9d2e85e2022-11-25 15:18:11 +0900185 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]