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
19import json
20import logging
21import os
22import pathlib
23import subprocess
24import sys
25from 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'
31
32
33class Error(Exception):
34
35  def __init__(self, message):
36    super().__init__(message)
37
38
39class 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
46class 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)
84
85
86@dataclass(frozen=True)
87class BuildPlan:
88  build_targets: set[str]
89  packaging_functions: set[Callable[..., None]]
90
91
92def build_test_suites(argv: list[str]) -> int:
93  """Builds all test suites passed in, optimizing based on the build_context content.
94
95  Args:
96    argv: The command line arguments passed in.
97
98  Returns:
99    The exit code of the build.
100  """
101  args = parse_args(argv)
102  check_required_env()
103  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()
108
109  try:
110    execute_build_plan(build_plan)
111  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
118def 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
128def 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}')
141
142
143def 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}')
151
152  logging.info('No BUILD_CONTEXT found, skipping optimizations.')
153  return empty_build_context()
154
155
156def empty_build_context():
157  return {'enabled_build_features': []}
158
159
160def 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)
165
166  try:
167    run_command(build_command)
168  except subprocess.CalledProcessError as e:
169    raise BuildFailureError(e.returncode) from e
170
171  for packaging_function in build_plan.packaging_functions:
172    packaging_function()
173
174
175def get_top() -> pathlib.Path:
176  return pathlib.Path(os.environ['TOP'])
177
178
179def run_command(args: list[str], stdout=None):
180  subprocess.run(args=args, check=True, stdout=stdout)
181
182
183def main(argv):
184  sys.exit(build_test_suites(argv))
185