Luca Farsi | 5717d6f | 2023-12-28 15:09:28 -0800 | [diff] [blame] | 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 | |
Luca Farsi | db13644 | 2024-03-26 10:55:21 -0700 | [diff] [blame] | 15 | """Build script for the CI `test_suites` target.""" |
Luca Farsi | 5717d6f | 2023-12-28 15:09:28 -0800 | [diff] [blame] | 16 | |
| 17 | import argparse |
Luca Farsi | db13644 | 2024-03-26 10:55:21 -0700 | [diff] [blame] | 18 | import logging |
Luca Farsi | 5717d6f | 2023-12-28 15:09:28 -0800 | [diff] [blame] | 19 | import os |
| 20 | import pathlib |
Luca Farsi | 5717d6f | 2023-12-28 15:09:28 -0800 | [diff] [blame] | 21 | import subprocess |
| 22 | import sys |
Luca Farsi | 5717d6f | 2023-12-28 15:09:28 -0800 | [diff] [blame] | 23 | |
| 24 | |
Luca Farsi | db13644 | 2024-03-26 10:55:21 -0700 | [diff] [blame] | 25 | class Error(Exception): |
| 26 | |
| 27 | def __init__(self, message): |
| 28 | super().__init__(message) |
Luca Farsi | 5717d6f | 2023-12-28 15:09:28 -0800 | [diff] [blame] | 29 | |
| 30 | |
Luca Farsi | db13644 | 2024-03-26 10:55:21 -0700 | [diff] [blame] | 31 | class 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 | |
| 38 | REQUIRED_ENV_VARS = frozenset(['TARGET_PRODUCT', 'TARGET_RELEASE', 'TOP']) |
| 39 | SOONG_UI_EXE_REL_PATH = 'build/soong/soong_ui.bash' |
| 40 | |
| 41 | |
| 42 | def get_top() -> pathlib.Path: |
| 43 | return pathlib.Path(os.environ['TOP']) |
| 44 | |
| 45 | |
| 46 | def 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 Farsi | 5717d6f | 2023-12-28 15:09:28 -0800 | [diff] [blame] | 55 | args = parse_args(argv) |
Luca Farsi | db13644 | 2024-03-26 10:55:21 -0700 | [diff] [blame] | 56 | check_required_env() |
Luca Farsi | 5717d6f | 2023-12-28 15:09:28 -0800 | [diff] [blame] | 57 | |
Luca Farsi | db13644 | 2024-03-26 10:55:21 -0700 | [diff] [blame] | 58 | try: |
Luca Farsi | 2dc1701 | 2024-03-19 16:47:54 -0700 | [diff] [blame] | 59 | build_everything(args) |
Luca Farsi | db13644 | 2024-03-26 10:55:21 -0700 | [diff] [blame] | 60 | 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 | |
| 67 | def 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 Farsi | 5717d6f | 2023-12-28 15:09:28 -0800 | [diff] [blame] | 80 | |
| 81 | |
| 82 | def parse_args(argv): |
| 83 | argparser = argparse.ArgumentParser() |
Luca Farsi | db13644 | 2024-03-26 10:55:21 -0700 | [diff] [blame] | 84 | |
Luca Farsi | 5717d6f | 2023-12-28 15:09:28 -0800 | [diff] [blame] | 85 | argparser.add_argument( |
| 86 | 'extra_targets', nargs='*', help='Extra test suites to build.' |
| 87 | ) |
Luca Farsi | 5717d6f | 2023-12-28 15:09:28 -0800 | [diff] [blame] | 88 | |
Luca Farsi | db13644 | 2024-03-26 10:55:21 -0700 | [diff] [blame] | 89 | return argparser.parse_args(argv) |
Luca Farsi | 11767d5 | 2024-03-07 13:33:57 -0800 | [diff] [blame] | 90 | |
| 91 | |
Luca Farsi | 2dc1701 | 2024-03-19 16:47:54 -0700 | [diff] [blame] | 92 | def build_everything(args: argparse.Namespace): |
Luca Farsi | db13644 | 2024-03-26 10:55:21 -0700 | [diff] [blame] | 93 | """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 Farsi | 2dc1701 | 2024-03-19 16:47:54 -0700 | [diff] [blame] | 101 | build_command = base_build_command(args, args.extra_targets) |
Luca Farsi | 5717d6f | 2023-12-28 15:09:28 -0800 | [diff] [blame] | 102 | |
Luca Farsi | db13644 | 2024-03-26 10:55:21 -0700 | [diff] [blame] | 103 | try: |
| 104 | run_command(build_command) |
| 105 | except subprocess.CalledProcessError as e: |
| 106 | raise BuildFailureError(e.returncode) from e |
Luca Farsi | 5717d6f | 2023-12-28 15:09:28 -0800 | [diff] [blame] | 107 | |
| 108 | |
Luca Farsi | c2ba38a | 2024-03-21 15:38:02 -0700 | [diff] [blame] | 109 | def base_build_command( |
| 110 | args: argparse.Namespace, extra_targets: set[str] |
Luca Farsi | db13644 | 2024-03-26 10:55:21 -0700 | [diff] [blame] | 111 | ) -> list[str]: |
| 112 | |
Luca Farsi | 5717d6f | 2023-12-28 15:09:28 -0800 | [diff] [blame] | 113 | build_command = [] |
Luca Farsi | db13644 | 2024-03-26 10:55:21 -0700 | [diff] [blame] | 114 | build_command.append(get_top().joinpath(SOONG_UI_EXE_REL_PATH)) |
Luca Farsi | 5717d6f | 2023-12-28 15:09:28 -0800 | [diff] [blame] | 115 | build_command.append('--make-mode') |
Luca Farsi | 11767d5 | 2024-03-07 13:33:57 -0800 | [diff] [blame] | 116 | build_command.extend(extra_targets) |
Luca Farsi | 5717d6f | 2023-12-28 15:09:28 -0800 | [diff] [blame] | 117 | |
| 118 | return build_command |
| 119 | |
| 120 | |
Luca Farsi | db13644 | 2024-03-26 10:55:21 -0700 | [diff] [blame] | 121 | def run_command(args: list[str], stdout=None): |
| 122 | subprocess.run(args=args, check=True, stdout=stdout) |
Luca Farsi | 5717d6f | 2023-12-28 15:09:28 -0800 | [diff] [blame] | 123 | |
| 124 | |
Luca Farsi | 2dc1701 | 2024-03-19 16:47:54 -0700 | [diff] [blame] | 125 | def main(argv): |
Luca Farsi | db13644 | 2024-03-26 10:55:21 -0700 | [diff] [blame] | 126 | sys.exit(build_test_suites(argv)) |