1# Copyright 2024, The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""Build script for the CI `test_suites` target.""" 16 17import argparse 18from dataclasses import dataclass 19from collections import defaultdict 20import json 21import logging 22import os 23import pathlib 24import re 25import subprocess 26import sys 27from typing import Callable 28from build_context import BuildContext 29import optimized_targets 30import metrics_agent 31import test_discovery_agent 32 33 34REQUIRED_ENV_VARS = frozenset(['TARGET_PRODUCT', 'TARGET_RELEASE', 'TOP', 'DIST_DIR']) 35SOONG_UI_EXE_REL_PATH = 'build/soong/soong_ui.bash' 36LOG_PATH = 'logs/build_test_suites.log' 37# Currently, this prevents the removal of those tags when they exist. In the future we likely 38# want the script to supply 'dist directly 39REQUIRED_BUILD_TARGETS = frozenset(['dist', 'droid', 'checkbuild']) 40 41 42class Error(Exception): 43 44 def __init__(self, message): 45 super().__init__(message) 46 47 48class BuildFailureError(Error): 49 50 def __init__(self, return_code): 51 super().__init__(f'Build command failed with return code: f{return_code}') 52 self.return_code = return_code 53 54 55class BuildPlanner: 56 """Class in charge of determining how to optimize build targets. 57 58 Given the build context and targets to build it will determine a final list of 59 targets to build along with getting a set of packaging functions to package up 60 any output zip files needed by the build. 61 """ 62 63 def __init__( 64 self, 65 build_context: BuildContext, 66 args: argparse.Namespace, 67 target_optimizations: dict[str, optimized_targets.OptimizedBuildTarget], 68 ): 69 self.build_context = build_context 70 self.args = args 71 self.target_optimizations = target_optimizations 72 self.target_to_test_infos = defaultdict(list) 73 74 def create_build_plan(self): 75 76 if 'optimized_build' not in self.build_context.enabled_build_features: 77 return BuildPlan(set(self.args.extra_targets), set()) 78 79 if not self.build_context.test_infos: 80 logging.warning('Build context has no test infos, skipping optimizations.') 81 for target in self.args.extra_targets: 82 get_metrics_agent().report_unoptimized_target(target, 'BUILD_CONTEXT has no test infos.') 83 return BuildPlan(set(self.args.extra_targets), set()) 84 85 build_targets = set() 86 packaging_commands_getters = [] 87 # In order to roll optimizations out differently between test suites and 88 # device builds, we have separate flags. 89 enable_discovery = (('test_suites_zip_test_discovery' 90 in self.build_context.enabled_build_features 91 and not self.args.device_build 92 ) or ( 93 'device_zip_test_discovery' 94 in self.build_context.enabled_build_features 95 and self.args.device_build 96 )) and not self.args.test_discovery_info_mode 97 logging.info(f'Discovery mode is enabled= {enable_discovery}') 98 preliminary_build_targets = self._collect_preliminary_build_targets(enable_discovery) 99 100 for target in preliminary_build_targets: 101 target_optimizer_getter = self.target_optimizations.get(target, None) 102 if not target_optimizer_getter: 103 build_targets.add(target) 104 continue 105 106 target_optimizer = target_optimizer_getter( 107 target, self.build_context, self.args, self.target_to_test_infos[target] 108 ) 109 build_targets.update(target_optimizer.get_build_targets()) 110 packaging_commands_getters.append( 111 target_optimizer.get_package_outputs_commands 112 ) 113 114 return BuildPlan(build_targets, packaging_commands_getters) 115 116 def _collect_preliminary_build_targets(self, enable_discovery: bool): 117 build_targets = set() 118 try: 119 test_discovery_zip_regexes = self._get_test_discovery_zip_regexes() 120 logging.info(f'Discovered test discovery regexes: {test_discovery_zip_regexes}') 121 except test_discovery_agent.TestDiscoveryError as e: 122 optimization_rationale = e.message 123 logging.warning(f'Unable to perform test discovery: {optimization_rationale}') 124 125 for target in self.args.extra_targets: 126 get_metrics_agent().report_unoptimized_target(target, optimization_rationale) 127 return self._legacy_collect_preliminary_build_targets() 128 129 for target in self.args.extra_targets: 130 if target in REQUIRED_BUILD_TARGETS: 131 build_targets.add(target) 132 get_metrics_agent().report_unoptimized_target(target, 'Required build target.') 133 continue 134 # If nothing is discovered without error, that means nothing is needed. 135 if not test_discovery_zip_regexes: 136 get_metrics_agent().report_optimized_target(target) 137 continue 138 139 regex = r'\b(%s.*)\b' % re.escape(target) 140 for opt in test_discovery_zip_regexes: 141 try: 142 if re.search(regex, opt): 143 get_metrics_agent().report_unoptimized_target(target, 'Test artifact used.') 144 build_targets.add(target) 145 # proceed to next target evaluation 146 break 147 get_metrics_agent().report_optimized_target(target) 148 except Exception as e: 149 # In case of exception report as unoptimized 150 build_targets.add(target) 151 get_metrics_agent().report_unoptimized_target(target, f'Error in parsing test discovery output for {target}: {repr(e)}') 152 logging.error(f'unable to parse test discovery output: {repr(e)}') 153 break 154 # If discovery is not enabled, return the original list 155 if not enable_discovery: 156 return self._legacy_collect_preliminary_build_targets() 157 158 return build_targets 159 160 def _legacy_collect_preliminary_build_targets(self): 161 build_targets = set() 162 for target in self.args.extra_targets: 163 if self._unused_target_exclusion_enabled( 164 target 165 ) and not self.build_context.build_target_used(target): 166 continue 167 168 build_targets.add(target) 169 return build_targets 170 171 def _unused_target_exclusion_enabled(self, target: str) -> bool: 172 return ( 173 f'{target}_unused_exclusion' 174 in self.build_context.enabled_build_features 175 ) 176 177 def _get_test_discovery_zip_regexes(self) -> set[str]: 178 build_target_regexes = set() 179 for test_info in self.build_context.test_infos: 180 tf_command = self._build_tf_command(test_info) 181 discovery_agent = test_discovery_agent.TestDiscoveryAgent(tradefed_args=tf_command) 182 for regex in discovery_agent.discover_test_zip_regexes(): 183 for target in self.args.extra_targets: 184 target_regex = r'\b(%s.*)\b' % re.escape(target) 185 if re.search(target_regex, regex): 186 self.target_to_test_infos[target].append(test_info) 187 build_target_regexes.add(regex) 188 return build_target_regexes 189 190 191 def _build_tf_command(self, test_info) -> list[str]: 192 command = [test_info.command] 193 for extra_option in test_info.extra_options: 194 if not extra_option.get('key'): 195 continue 196 arg_key = '--' + extra_option.get('key') 197 if arg_key == '--build-id': 198 command.append(arg_key) 199 command.append(os.environ.get('BUILD_NUMBER')) 200 continue 201 if extra_option.get('values'): 202 for value in extra_option.get('values'): 203 command.append(arg_key) 204 command.append(value) 205 else: 206 command.append(arg_key) 207 208 return command 209 210@dataclass(frozen=True) 211class BuildPlan: 212 build_targets: set[str] 213 packaging_commands_getters: list[Callable[[], list[list[str]]]] 214 215 216def build_test_suites(argv: list[str]) -> int: 217 """Builds all test suites passed in, optimizing based on the build_context content. 218 219 Args: 220 argv: The command line arguments passed in. 221 222 Returns: 223 The exit code of the build. 224 """ 225 get_metrics_agent().analysis_start() 226 try: 227 args = parse_args(argv) 228 check_required_env() 229 build_context = BuildContext(load_build_context()) 230 build_planner = BuildPlanner( 231 build_context, args, optimized_targets.OPTIMIZED_BUILD_TARGETS 232 ) 233 build_plan = build_planner.create_build_plan() 234 except: 235 raise 236 finally: 237 get_metrics_agent().analysis_end() 238 239 try: 240 execute_build_plan(build_plan) 241 except BuildFailureError as e: 242 logging.error('Build command failed! Check build_log for details.') 243 return e.return_code 244 finally: 245 get_metrics_agent().end_reporting() 246 247 return 0 248 249 250def parse_args(argv: list[str]) -> argparse.Namespace: 251 argparser = argparse.ArgumentParser() 252 253 argparser.add_argument( 254 'extra_targets', nargs='*', help='Extra test suites to build.' 255 ) 256 argparser.add_argument( 257 '--device-build', 258 action='store_true', 259 help='Flag to indicate running a device build.', 260 ) 261 argparser.add_argument( 262 '--test_discovery_info_mode', 263 action='store_true', 264 help='Flag to enable running test discovery in info only mode.', 265 ) 266 267 return argparser.parse_args(argv) 268 269 270def check_required_env(): 271 """Check for required env vars. 272 273 Raises: 274 RuntimeError: If any required env vars are not found. 275 """ 276 missing_env_vars = sorted(v for v in REQUIRED_ENV_VARS if v not in os.environ) 277 278 if not missing_env_vars: 279 return 280 281 t = ','.join(missing_env_vars) 282 raise Error(f'Missing required environment variables: {t}') 283 284 285def load_build_context(): 286 build_context_path = pathlib.Path(os.environ.get('BUILD_CONTEXT', '')) 287 if build_context_path.is_file(): 288 try: 289 with open(build_context_path, 'r') as f: 290 return json.load(f) 291 except json.decoder.JSONDecodeError as e: 292 raise Error(f'Failed to load JSON file: {build_context_path}') 293 294 logging.info('No BUILD_CONTEXT found, skipping optimizations.') 295 return empty_build_context() 296 297 298def empty_build_context(): 299 return {'enabledBuildFeatures': []} 300 301 302def execute_build_plan(build_plan: BuildPlan): 303 build_command = [] 304 build_command.append(get_top().joinpath(SOONG_UI_EXE_REL_PATH)) 305 build_command.append('--make-mode') 306 build_command.extend(build_plan.build_targets) 307 logging.info(f'Running build command: {build_command}') 308 try: 309 run_command(build_command) 310 except subprocess.CalledProcessError as e: 311 raise BuildFailureError(e.returncode) from e 312 313 get_metrics_agent().packaging_start() 314 try: 315 for packaging_commands_getter in build_plan.packaging_commands_getters: 316 for packaging_command in packaging_commands_getter(): 317 run_command(packaging_command) 318 except subprocess.CalledProcessError as e: 319 raise BuildFailureError(e.returncode) from e 320 finally: 321 get_metrics_agent().packaging_end() 322 323 324def get_top() -> pathlib.Path: 325 return pathlib.Path(os.environ['TOP']) 326 327 328def run_command(args: list[str], stdout=None): 329 subprocess.run(args=args, check=True, stdout=stdout) 330 331 332def get_metrics_agent(): 333 return metrics_agent.MetricsAgent.instance() 334 335 336def main(argv): 337 dist_dir = os.environ.get('DIST_DIR') 338 if dist_dir: 339 log_file = pathlib.Path(dist_dir) / LOG_PATH 340 logging.basicConfig( 341 level=logging.DEBUG, 342 format='%(asctime)s %(levelname)s %(message)s', 343 filename=log_file, 344 ) 345 sys.exit(build_test_suites(argv)) 346 347 348if __name__ == '__main__': 349 main(sys.argv[1:]) 350