blob: 6e1f88c36cb4141b942262aa82e2fdc3a16a3974 [file] [log] [blame]
Luca Farsi5717d6f2023-12-28 15:09:28 -08001# 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
Luca Farsidb136442024-03-26 10:55:21 -070015"""Build script for the CI `test_suites` target."""
Luca Farsi5717d6f2023-12-28 15:09:28 -080016
17import argparse
Luca Farsi040fabe2024-05-22 17:21:47 -070018from dataclasses import dataclass
19import json
Luca Farsidb136442024-03-26 10:55:21 -070020import logging
Luca Farsi5717d6f2023-12-28 15:09:28 -080021import os
22import pathlib
Luca Farsi5717d6f2023-12-28 15:09:28 -080023import subprocess
24import sys
Luca Farsi040fabe2024-05-22 17:21:47 -070025from typing import Callable
26import optimized_targets
27
28
29REQUIRED_ENV_VARS = frozenset(['TARGET_PRODUCT', 'TARGET_RELEASE', 'TOP'])
30SOONG_UI_EXE_REL_PATH = 'build/soong/soong_ui.bash'
Luca Farsi5717d6f2023-12-28 15:09:28 -080031
32
Luca Farsidb136442024-03-26 10:55:21 -070033class Error(Exception):
34
35 def __init__(self, message):
36 super().__init__(message)
Luca Farsi5717d6f2023-12-28 15:09:28 -080037
38
Luca Farsidb136442024-03-26 10:55:21 -070039class BuildFailureError(Error):
40
41 def __init__(self, return_code):
42 super().__init__(f'Build command failed with return code: f{return_code}')
43 self.return_code = return_code
44
45
Luca Farsi040fabe2024-05-22 17:21:47 -070046class BuildPlanner:
47 """Class in charge of determining how to optimize build targets.
48
49 Given the build context and targets to build it will determine a final list of
50 targets to build along with getting a set of packaging functions to package up
51 any output zip files needed by the build.
52 """
53
54 def __init__(
55 self,
56 build_context: dict[str, any],
57 args: argparse.Namespace,
58 target_optimizations: dict[str, optimized_targets.OptimizedBuildTarget],
59 ):
60 self.build_context = build_context
61 self.args = args
62 self.target_optimizations = target_optimizations
63
64 def create_build_plan(self):
65
66 if 'optimized_build' not in self.build_context['enabled_build_features']:
67 return BuildPlan(set(self.args.extra_targets), set())
68
69 build_targets = set()
70 packaging_functions = set()
71 for target in self.args.extra_targets:
72 target_optimizer_getter = self.target_optimizations.get(target, None)
73 if not target_optimizer_getter:
74 build_targets.add(target)
75 continue
76
77 target_optimizer = target_optimizer_getter(
78 target, self.build_context, self.args
79 )
80 build_targets.update(target_optimizer.get_build_targets())
81 packaging_functions.add(target_optimizer.package_outputs)
82
83 return BuildPlan(build_targets, packaging_functions)
Luca Farsidb136442024-03-26 10:55:21 -070084
85
Luca Farsi040fabe2024-05-22 17:21:47 -070086@dataclass(frozen=True)
87class BuildPlan:
88 build_targets: set[str]
89 packaging_functions: set[Callable[..., None]]
Luca Farsidb136442024-03-26 10:55:21 -070090
91
92def build_test_suites(argv: list[str]) -> int:
Luca Farsi040fabe2024-05-22 17:21:47 -070093 """Builds all test suites passed in, optimizing based on the build_context content.
Luca Farsidb136442024-03-26 10:55:21 -070094
95 Args:
96 argv: The command line arguments passed in.
97
98 Returns:
99 The exit code of the build.
100 """
Luca Farsi5717d6f2023-12-28 15:09:28 -0800101 args = parse_args(argv)
Luca Farsidb136442024-03-26 10:55:21 -0700102 check_required_env()
Luca Farsi040fabe2024-05-22 17:21:47 -0700103 build_context = load_build_context()
104 build_planner = BuildPlanner(
105 build_context, args, optimized_targets.OPTIMIZED_BUILD_TARGETS
106 )
107 build_plan = build_planner.create_build_plan()
Luca Farsi5717d6f2023-12-28 15:09:28 -0800108
Luca Farsidb136442024-03-26 10:55:21 -0700109 try:
Luca Farsi040fabe2024-05-22 17:21:47 -0700110 execute_build_plan(build_plan)
Luca Farsidb136442024-03-26 10:55:21 -0700111 except BuildFailureError as e:
112 logging.error('Build command failed! Check build_log for details.')
113 return e.return_code
114
115 return 0
116
117
Luca Farsi040fabe2024-05-22 17:21:47 -0700118def parse_args(argv: list[str]) -> argparse.Namespace:
119 argparser = argparse.ArgumentParser()
120
121 argparser.add_argument(
122 'extra_targets', nargs='*', help='Extra test suites to build.'
123 )
124
125 return argparser.parse_args(argv)
126
127
Luca Farsidb136442024-03-26 10:55:21 -0700128def check_required_env():
129 """Check for required env vars.
130
131 Raises:
132 RuntimeError: If any required env vars are not found.
133 """
134 missing_env_vars = sorted(v for v in REQUIRED_ENV_VARS if v not in os.environ)
135
136 if not missing_env_vars:
137 return
138
139 t = ','.join(missing_env_vars)
140 raise Error(f'Missing required environment variables: {t}')
Luca Farsi5717d6f2023-12-28 15:09:28 -0800141
142
Luca Farsi040fabe2024-05-22 17:21:47 -0700143def load_build_context():
144 build_context_path = pathlib.Path(os.environ.get('BUILD_CONTEXT', ''))
145 if build_context_path.is_file():
146 try:
147 with open(build_context_path, 'r') as f:
148 return json.load(f)
149 except json.decoder.JSONDecodeError as e:
150 raise Error(f'Failed to load JSON file: {build_context_path}')
Luca Farsidb136442024-03-26 10:55:21 -0700151
Luca Farsi040fabe2024-05-22 17:21:47 -0700152 logging.info('No BUILD_CONTEXT found, skipping optimizations.')
153 return empty_build_context()
Luca Farsi11767d52024-03-07 13:33:57 -0800154
155
Luca Farsi040fabe2024-05-22 17:21:47 -0700156def empty_build_context():
157 return {'enabled_build_features': []}
Luca Farsidb136442024-03-26 10:55:21 -0700158
Luca Farsidb136442024-03-26 10:55:21 -0700159
Luca Farsi040fabe2024-05-22 17:21:47 -0700160def execute_build_plan(build_plan: BuildPlan):
161 build_command = []
162 build_command.append(get_top().joinpath(SOONG_UI_EXE_REL_PATH))
163 build_command.append('--make-mode')
164 build_command.extend(build_plan.build_targets)
Luca Farsi5717d6f2023-12-28 15:09:28 -0800165
Luca Farsidb136442024-03-26 10:55:21 -0700166 try:
167 run_command(build_command)
168 except subprocess.CalledProcessError as e:
169 raise BuildFailureError(e.returncode) from e
Luca Farsi5717d6f2023-12-28 15:09:28 -0800170
Luca Farsi040fabe2024-05-22 17:21:47 -0700171 for packaging_function in build_plan.packaging_functions:
172 packaging_function()
Luca Farsi5717d6f2023-12-28 15:09:28 -0800173
Luca Farsidb136442024-03-26 10:55:21 -0700174
Luca Farsi040fabe2024-05-22 17:21:47 -0700175def get_top() -> pathlib.Path:
176 return pathlib.Path(os.environ['TOP'])
Luca Farsi5717d6f2023-12-28 15:09:28 -0800177
178
Luca Farsidb136442024-03-26 10:55:21 -0700179def run_command(args: list[str], stdout=None):
180 subprocess.run(args=args, check=True, stdout=stdout)
Luca Farsi5717d6f2023-12-28 15:09:28 -0800181
182
Luca Farsi2dc17012024-03-19 16:47:54 -0700183def main(argv):
Luca Farsidb136442024-03-26 10:55:21 -0700184 sys.exit(build_test_suites(argv))