blob: 29ed50e095d3984dbc2f314f5368cde4c685d463 [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 Farsidb136442024-03-26 10:55:21 -070018import logging
Luca Farsi5717d6f2023-12-28 15:09:28 -080019import os
20import pathlib
Luca Farsi5717d6f2023-12-28 15:09:28 -080021import subprocess
22import sys
Luca Farsi5717d6f2023-12-28 15:09:28 -080023
24
Luca Farsidb136442024-03-26 10:55:21 -070025class Error(Exception):
26
27 def __init__(self, message):
28 super().__init__(message)
Luca Farsi5717d6f2023-12-28 15:09:28 -080029
30
Luca Farsidb136442024-03-26 10:55:21 -070031class BuildFailureError(Error):
32
33 def __init__(self, return_code):
34 super().__init__(f'Build command failed with return code: f{return_code}')
35 self.return_code = return_code
36
37
38REQUIRED_ENV_VARS = frozenset(['TARGET_PRODUCT', 'TARGET_RELEASE', 'TOP'])
39SOONG_UI_EXE_REL_PATH = 'build/soong/soong_ui.bash'
40
41
42def get_top() -> pathlib.Path:
43 return pathlib.Path(os.environ['TOP'])
44
45
46def build_test_suites(argv: list[str]) -> int:
47 """Builds the general-tests and any other test suites passed in.
48
49 Args:
50 argv: The command line arguments passed in.
51
52 Returns:
53 The exit code of the build.
54 """
Luca Farsi5717d6f2023-12-28 15:09:28 -080055 args = parse_args(argv)
Luca Farsidb136442024-03-26 10:55:21 -070056 check_required_env()
Luca Farsi5717d6f2023-12-28 15:09:28 -080057
Luca Farsidb136442024-03-26 10:55:21 -070058 try:
Luca Farsi2dc17012024-03-19 16:47:54 -070059 build_everything(args)
Luca Farsidb136442024-03-26 10:55:21 -070060 except BuildFailureError as e:
61 logging.error('Build command failed! Check build_log for details.')
62 return e.return_code
63
64 return 0
65
66
67def check_required_env():
68 """Check for required env vars.
69
70 Raises:
71 RuntimeError: If any required env vars are not found.
72 """
73 missing_env_vars = sorted(v for v in REQUIRED_ENV_VARS if v not in os.environ)
74
75 if not missing_env_vars:
76 return
77
78 t = ','.join(missing_env_vars)
79 raise Error(f'Missing required environment variables: {t}')
Luca Farsi5717d6f2023-12-28 15:09:28 -080080
81
82def parse_args(argv):
83 argparser = argparse.ArgumentParser()
Luca Farsidb136442024-03-26 10:55:21 -070084
Luca Farsi5717d6f2023-12-28 15:09:28 -080085 argparser.add_argument(
86 'extra_targets', nargs='*', help='Extra test suites to build.'
87 )
Luca Farsi5717d6f2023-12-28 15:09:28 -080088
Luca Farsidb136442024-03-26 10:55:21 -070089 return argparser.parse_args(argv)
Luca Farsi11767d52024-03-07 13:33:57 -080090
91
Luca Farsi2dc17012024-03-19 16:47:54 -070092def build_everything(args: argparse.Namespace):
Luca Farsidb136442024-03-26 10:55:21 -070093 """Builds all tests (regardless of whether they are needed).
94
95 Args:
96 args: The parsed arguments.
97
98 Raises:
99 BuildFailure: If the build command fails.
100 """
Luca Farsi2dc17012024-03-19 16:47:54 -0700101 build_command = base_build_command(args, args.extra_targets)
Luca Farsi5717d6f2023-12-28 15:09:28 -0800102
Luca Farsidb136442024-03-26 10:55:21 -0700103 try:
104 run_command(build_command)
105 except subprocess.CalledProcessError as e:
106 raise BuildFailureError(e.returncode) from e
Luca Farsi5717d6f2023-12-28 15:09:28 -0800107
108
Luca Farsic2ba38a2024-03-21 15:38:02 -0700109def base_build_command(
110 args: argparse.Namespace, extra_targets: set[str]
Luca Farsidb136442024-03-26 10:55:21 -0700111) -> list[str]:
112
Luca Farsi5717d6f2023-12-28 15:09:28 -0800113 build_command = []
Luca Farsidb136442024-03-26 10:55:21 -0700114 build_command.append(get_top().joinpath(SOONG_UI_EXE_REL_PATH))
Luca Farsi5717d6f2023-12-28 15:09:28 -0800115 build_command.append('--make-mode')
Luca Farsi11767d52024-03-07 13:33:57 -0800116 build_command.extend(extra_targets)
Luca Farsi5717d6f2023-12-28 15:09:28 -0800117
118 return build_command
119
120
Luca Farsidb136442024-03-26 10:55:21 -0700121def run_command(args: list[str], stdout=None):
122 subprocess.run(args=args, check=True, stdout=stdout)
Luca Farsi5717d6f2023-12-28 15:09:28 -0800123
124
Luca Farsi2dc17012024-03-19 16:47:54 -0700125def main(argv):
Luca Farsidb136442024-03-26 10:55:21 -0700126 sys.exit(build_test_suites(argv))