Luca Farsi | 040fabe | 2024-05-22 17:21:47 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2024, The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | from abc import ABC |
Luca Farsi | 70a53bd | 2024-08-07 17:29:16 -0700 | [diff] [blame^] | 17 | from typing import Self |
| 18 | import argparse |
| 19 | import functools |
Luca Farsi | 040fabe | 2024-05-22 17:21:47 -0700 | [diff] [blame] | 20 | |
| 21 | |
| 22 | class OptimizedBuildTarget(ABC): |
| 23 | """A representation of an optimized build target. |
| 24 | |
| 25 | This class will determine what targets to build given a given build_cotext and |
| 26 | will have a packaging function to generate any necessary output zips for the |
| 27 | build. |
| 28 | """ |
| 29 | |
Luca Farsi | 70a53bd | 2024-08-07 17:29:16 -0700 | [diff] [blame^] | 30 | def __init__( |
| 31 | self, |
| 32 | target: str, |
| 33 | build_context: dict[str, any], |
| 34 | args: argparse.Namespace, |
| 35 | ): |
| 36 | self.target = target |
Luca Farsi | 040fabe | 2024-05-22 17:21:47 -0700 | [diff] [blame] | 37 | self.build_context = build_context |
| 38 | self.args = args |
| 39 | |
Luca Farsi | 70a53bd | 2024-08-07 17:29:16 -0700 | [diff] [blame^] | 40 | def get_build_targets(self) -> set[str]: |
| 41 | features = self.build_context.get('enabledBuildFeatures', []) |
| 42 | if self.get_enabled_flag() in features: |
| 43 | return self.get_build_targets_impl() |
| 44 | return {self.target} |
Luca Farsi | 040fabe | 2024-05-22 17:21:47 -0700 | [diff] [blame] | 45 | |
| 46 | def package_outputs(self): |
Luca Farsi | 70a53bd | 2024-08-07 17:29:16 -0700 | [diff] [blame^] | 47 | features = self.build_context.get('enabledBuildFeatures', []) |
| 48 | if self.get_enabled_flag() in features: |
| 49 | return self.package_outputs_impl() |
| 50 | |
| 51 | def package_outputs_impl(self): |
| 52 | raise NotImplementedError( |
| 53 | f'package_outputs_impl not implemented in {type(self).__name__}' |
| 54 | ) |
| 55 | |
| 56 | def get_enabled_flag(self): |
| 57 | raise NotImplementedError( |
| 58 | f'get_enabled_flag not implemented in {type(self).__name__}' |
| 59 | ) |
| 60 | |
| 61 | def get_build_targets_impl(self) -> set[str]: |
| 62 | raise NotImplementedError( |
| 63 | f'get_build_targets_impl not implemented in {type(self).__name__}' |
| 64 | ) |
Luca Farsi | 040fabe | 2024-05-22 17:21:47 -0700 | [diff] [blame] | 65 | |
| 66 | |
| 67 | class NullOptimizer(OptimizedBuildTarget): |
| 68 | """No-op target optimizer. |
| 69 | |
| 70 | This will simply build the same target it was given and do nothing for the |
| 71 | packaging step. |
| 72 | """ |
| 73 | |
| 74 | def __init__(self, target): |
| 75 | self.target = target |
| 76 | |
| 77 | def get_build_targets(self): |
| 78 | return {self.target} |
| 79 | |
| 80 | def package_outputs(self): |
| 81 | pass |
| 82 | |
| 83 | |
Luca Farsi | 70a53bd | 2024-08-07 17:29:16 -0700 | [diff] [blame^] | 84 | class GeneralTestsOptimizer(OptimizedBuildTarget): |
| 85 | """general-tests optimizer |
Luca Farsi | 040fabe | 2024-05-22 17:21:47 -0700 | [diff] [blame] | 86 | |
Luca Farsi | 70a53bd | 2024-08-07 17:29:16 -0700 | [diff] [blame^] | 87 | TODO(b/358215235): Implement |
| 88 | |
| 89 | This optimizer reads in the list of changed files from the file located in |
| 90 | env[CHANGE_INFO] and uses this list alongside the normal TEST MAPPING logic to |
| 91 | determine what test mapping modules will run for the given changes. It then |
| 92 | builds those modules and packages them in the same way general-tests.zip is |
| 93 | normally built. |
| 94 | """ |
| 95 | |
| 96 | def get_enabled_flag(self): |
| 97 | return 'general-tests-optimized' |
| 98 | |
| 99 | @classmethod |
| 100 | def get_optimized_targets(cls) -> dict[str, OptimizedBuildTarget]: |
| 101 | return {'general-tests': functools.partial(cls)} |
Luca Farsi | 040fabe | 2024-05-22 17:21:47 -0700 | [diff] [blame] | 102 | |
| 103 | |
Luca Farsi | 70a53bd | 2024-08-07 17:29:16 -0700 | [diff] [blame^] | 104 | OPTIMIZED_BUILD_TARGETS = {} |
| 105 | OPTIMIZED_BUILD_TARGETS.update(GeneralTestsOptimizer.get_optimized_targets()) |