blob: 8a529c7420880c5afabdded4851e93ca951d7c35 [file] [log] [blame]
Luca Farsi040fabe2024-05-22 17:21:47 -07001#
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
16from abc import ABC
Luca Farsi70a53bd2024-08-07 17:29:16 -070017from typing import Self
18import argparse
19import functools
Luca Farsi040fabe2024-05-22 17:21:47 -070020
21
22class 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 Farsi70a53bd2024-08-07 17:29:16 -070030 def __init__(
31 self,
32 target: str,
33 build_context: dict[str, any],
34 args: argparse.Namespace,
35 ):
36 self.target = target
Luca Farsi040fabe2024-05-22 17:21:47 -070037 self.build_context = build_context
38 self.args = args
39
Luca Farsi70a53bd2024-08-07 17:29:16 -070040 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 Farsi040fabe2024-05-22 17:21:47 -070045
46 def package_outputs(self):
Luca Farsi70a53bd2024-08-07 17:29:16 -070047 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 Farsi040fabe2024-05-22 17:21:47 -070065
66
67class 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 Farsi70a53bd2024-08-07 17:29:16 -070084class GeneralTestsOptimizer(OptimizedBuildTarget):
85 """general-tests optimizer
Luca Farsi040fabe2024-05-22 17:21:47 -070086
Luca Farsi70a53bd2024-08-07 17:29:16 -070087 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 Farsi040fabe2024-05-22 17:21:47 -0700102
103
Luca Farsi70a53bd2024-08-07 17:29:16 -0700104OPTIMIZED_BUILD_TARGETS = {}
105OPTIMIZED_BUILD_TARGETS.update(GeneralTestsOptimizer.get_optimized_targets())