| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved. | 
|  | 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 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 16 |  | 
|  | 17 | import ( | 
| Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 18 | "reflect" | 
|  | 19 |  | 
| Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame] | 20 | "android/soong/ui/metrics/bp2build_metrics_proto" | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 21 | "github.com/google/blueprint" | 
|  | 22 | "github.com/google/blueprint/proptools" | 
|  | 23 | ) | 
|  | 24 |  | 
| Colin Cross | c99deeb | 2016-04-11 15:06:20 -0700 | [diff] [blame] | 25 | type defaultsDependencyTag struct { | 
|  | 26 | blueprint.BaseDependencyTag | 
|  | 27 | } | 
|  | 28 |  | 
|  | 29 | var DefaultsDepTag defaultsDependencyTag | 
|  | 30 |  | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 31 | type defaultsProperties struct { | 
|  | 32 | Defaults []string | 
|  | 33 | } | 
|  | 34 |  | 
| Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 35 | type DefaultableModuleBase struct { | 
| Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 36 | defaultsProperties            defaultsProperties | 
|  | 37 | defaultableProperties         []interface{} | 
|  | 38 | defaultableVariableProperties interface{} | 
| Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 39 |  | 
|  | 40 | // The optional hook to call after any defaults have been applied. | 
|  | 41 | hook DefaultableHook | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 42 | } | 
|  | 43 |  | 
| Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 44 | func (d *DefaultableModuleBase) defaults() *defaultsProperties { | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 45 | return &d.defaultsProperties | 
|  | 46 | } | 
|  | 47 |  | 
| Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 48 | func (d *DefaultableModuleBase) setProperties(props []interface{}, variableProperties interface{}) { | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 49 | d.defaultableProperties = props | 
| Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 50 | d.defaultableVariableProperties = variableProperties | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 51 | } | 
|  | 52 |  | 
| Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 53 | func (d *DefaultableModuleBase) SetDefaultableHook(hook DefaultableHook) { | 
|  | 54 | d.hook = hook | 
|  | 55 | } | 
|  | 56 |  | 
| Jihoon Kang | d48abd5 | 2023-02-02 22:32:31 +0000 | [diff] [blame] | 57 | func (d *DefaultableModuleBase) CallHookIfAvailable(ctx DefaultableHookContext) { | 
| Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 58 | if d.hook != nil { | 
|  | 59 | d.hook(ctx) | 
|  | 60 | } | 
|  | 61 | } | 
|  | 62 |  | 
| Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 63 | // Interface that must be supported by any module to which defaults can be applied. | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 64 | type Defaultable interface { | 
| Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 65 | // Get a pointer to the struct containing the Defaults property. | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 66 | defaults() *defaultsProperties | 
| Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 67 |  | 
|  | 68 | // Set the property structures into which defaults will be added. | 
| Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 69 | setProperties(props []interface{}, variableProperties interface{}) | 
| Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 70 |  | 
| Liz Kammer | eeeb952 | 2023-03-10 18:53:41 -0500 | [diff] [blame] | 71 | // Apply defaults from the supplied Defaults to the property structures supplied to | 
| Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 72 | // setProperties(...). | 
| Liz Kammer | eeeb952 | 2023-03-10 18:53:41 -0500 | [diff] [blame] | 73 | applyDefaults(TopDownMutatorContext, []Defaults) | 
| Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 74 |  | 
|  | 75 | // Set the hook to be called after any defaults have been applied. | 
|  | 76 | // | 
|  | 77 | // Should be used in preference to a AddLoadHook when the behavior of the load | 
|  | 78 | // hook is dependent on properties supplied in the Android.bp file. | 
|  | 79 | SetDefaultableHook(hook DefaultableHook) | 
|  | 80 |  | 
|  | 81 | // Call the hook if specified. | 
| Jihoon Kang | d48abd5 | 2023-02-02 22:32:31 +0000 | [diff] [blame] | 82 | CallHookIfAvailable(context DefaultableHookContext) | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
| Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 85 | type DefaultableModule interface { | 
|  | 86 | Module | 
|  | 87 | Defaultable | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 88 | } | 
|  | 89 |  | 
| Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 90 | var _ Defaultable = (*DefaultableModuleBase)(nil) | 
|  | 91 |  | 
|  | 92 | func InitDefaultableModule(module DefaultableModule) { | 
| Usta | fe201fe | 2021-12-06 15:13:36 -0500 | [diff] [blame] | 93 | if module.base().module == nil { | 
| Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 94 | panic("InitAndroidModule must be called before InitDefaultableModule") | 
|  | 95 | } | 
| Liz Kammer | 416201d | 2021-12-15 13:18:42 -0500 | [diff] [blame] | 96 |  | 
| Usta | fe201fe | 2021-12-06 15:13:36 -0500 | [diff] [blame] | 97 | module.setProperties(module.GetProperties(), module.base().variableProperties) | 
| Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 98 |  | 
|  | 99 | module.AddProperties(module.defaults()) | 
|  | 100 | } | 
|  | 101 |  | 
| Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 102 | // A restricted subset of context methods, similar to LoadHookContext. | 
|  | 103 | type DefaultableHookContext interface { | 
|  | 104 | EarlyModuleContext | 
|  | 105 |  | 
|  | 106 | CreateModule(ModuleFactory, ...interface{}) Module | 
| Colin Cross | 18f840c | 2021-05-20 17:56:54 -0700 | [diff] [blame] | 107 | AddMissingDependencies(missingDeps []string) | 
| Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 108 | } | 
|  | 109 |  | 
|  | 110 | type DefaultableHook func(ctx DefaultableHookContext) | 
|  | 111 |  | 
| Paul Duffin | 95d53b5 | 2019-07-24 13:45:05 +0100 | [diff] [blame] | 112 | // The Defaults_visibility property. | 
|  | 113 | type DefaultsVisibilityProperties struct { | 
|  | 114 |  | 
|  | 115 | // Controls the visibility of the defaults module itself. | 
|  | 116 | Defaults_visibility []string | 
|  | 117 | } | 
|  | 118 |  | 
| Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 119 | type DefaultsModuleBase struct { | 
|  | 120 | DefaultableModuleBase | 
| Liz Kammer | 416201d | 2021-12-15 13:18:42 -0500 | [diff] [blame] | 121 |  | 
|  | 122 | // Included to support setting bazel_module.label for multiple Soong modules to the same Bazel | 
|  | 123 | // target. This is primarily useful for modules that were architecture specific and instead are | 
|  | 124 | // handled in Bazel as a select(). | 
|  | 125 | BazelModuleBase | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 126 | } | 
|  | 127 |  | 
| Martin Stjernholm | ebd757d | 2019-05-24 11:00:30 +0100 | [diff] [blame] | 128 | // The common pattern for defaults modules is to register separate instances of | 
|  | 129 | // the xxxProperties structs in the AddProperties calls, rather than reusing the | 
|  | 130 | // ones inherited from Module. | 
|  | 131 | // | 
|  | 132 | // The effect is that e.g. myDefaultsModuleInstance.base().xxxProperties won't | 
|  | 133 | // contain the values that have been set for the defaults module. Rather, to | 
|  | 134 | // retrieve the values it is necessary to iterate over properties(). E.g. to get | 
|  | 135 | // the commonProperties instance that have the real values: | 
|  | 136 | // | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 137 | //	d := myModule.(Defaults) | 
|  | 138 | //	for _, props := range d.properties() { | 
|  | 139 | //	  if cp, ok := props.(*commonProperties); ok { | 
|  | 140 | //	    ... access property values in cp ... | 
|  | 141 | //	  } | 
|  | 142 | //	} | 
| Martin Stjernholm | ebd757d | 2019-05-24 11:00:30 +0100 | [diff] [blame] | 143 | // | 
|  | 144 | // The rationale is that the properties on a defaults module apply to the | 
|  | 145 | // defaultable modules using it, not to the defaults module itself. E.g. setting | 
|  | 146 | // the "enabled" property false makes inheriting modules disabled by default, | 
|  | 147 | // rather than disabling the defaults module itself. | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 148 | type Defaults interface { | 
| Colin Cross | e7b0713 | 2016-07-27 10:15:06 -0700 | [diff] [blame] | 149 | Defaultable | 
| Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 150 |  | 
|  | 151 | // Although this function is unused it is actually needed to ensure that only modules that embed | 
|  | 152 | // DefaultsModuleBase will type-assert to the Defaults interface. | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 153 | isDefaults() bool | 
| Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 154 |  | 
|  | 155 | // Get the structures containing the properties for which defaults can be provided. | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 156 | properties() []interface{} | 
| Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 157 |  | 
| Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 158 | productVariableProperties() interface{} | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 159 | } | 
|  | 160 |  | 
| Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 161 | func (d *DefaultsModuleBase) isDefaults() bool { | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 162 | return true | 
|  | 163 | } | 
|  | 164 |  | 
| Paul Duffin | e62432f | 2019-07-24 12:51:21 +0100 | [diff] [blame] | 165 | type DefaultsModule interface { | 
|  | 166 | Module | 
|  | 167 | Defaults | 
| Liz Kammer | 416201d | 2021-12-15 13:18:42 -0500 | [diff] [blame] | 168 | Bazelable | 
| Paul Duffin | e62432f | 2019-07-24 12:51:21 +0100 | [diff] [blame] | 169 | } | 
|  | 170 |  | 
| Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 171 | func (d *DefaultsModuleBase) properties() []interface{} { | 
| Colin Cross | e7b0713 | 2016-07-27 10:15:06 -0700 | [diff] [blame] | 172 | return d.defaultableProperties | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 173 | } | 
|  | 174 |  | 
| Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 175 | func (d *DefaultsModuleBase) productVariableProperties() interface{} { | 
|  | 176 | return d.defaultableVariableProperties | 
|  | 177 | } | 
|  | 178 |  | 
| Liz Kammer | 416201d | 2021-12-15 13:18:42 -0500 | [diff] [blame] | 179 | func (d *DefaultsModuleBase) GenerateAndroidBuildActions(ctx ModuleContext) {} | 
| Colin Cross | 5903762 | 2019-06-10 13:12:56 -0700 | [diff] [blame] | 180 |  | 
| Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 181 | // ConvertWithBp2build to fulfill Bazelable interface; however, at this time defaults module are | 
|  | 182 | // *NOT* converted with bp2build | 
| Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 183 | func (defaultable *DefaultsModuleBase) ConvertWithBp2build(ctx Bp2buildMutatorContext) { | 
| Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame] | 184 | // Defaults types are never convertible. | 
|  | 185 | ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "") | 
|  | 186 | } | 
| Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 187 |  | 
| Paul Duffin | e62432f | 2019-07-24 12:51:21 +0100 | [diff] [blame] | 188 | func InitDefaultsModule(module DefaultsModule) { | 
| Paul Duffin | 3f98d14 | 2020-09-02 13:28:25 +0100 | [diff] [blame] | 189 | commonProperties := &commonProperties{} | 
| Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 190 |  | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 191 | module.AddProperties( | 
| Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 192 | &hostAndDeviceProperties{}, | 
| Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 193 | commonProperties, | 
| Paul Duffin | ed87513 | 2020-09-02 13:08:57 +0100 | [diff] [blame] | 194 | &ApexProperties{}, | 
|  | 195 | &distProperties{}) | 
| Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 196 |  | 
| Liz Kammer | 416201d | 2021-12-15 13:18:42 -0500 | [diff] [blame] | 197 | // Bazel module must be initialized _before_ Defaults to be included in cc_defaults module. | 
|  | 198 | InitBazelModule(module) | 
| Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 199 | initAndroidModuleBase(module) | 
|  | 200 | initProductVariableModule(module) | 
| Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 201 | initArchModule(module) | 
| Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 202 | InitDefaultableModule(module) | 
| Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 203 |  | 
| Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 204 | // Add properties that will not have defaults applied to them. | 
| Colin Cross | 08d6f8f | 2020-11-19 02:33:19 +0000 | [diff] [blame] | 205 | base := module.base() | 
| Paul Duffin | 3f98d14 | 2020-09-02 13:28:25 +0100 | [diff] [blame] | 206 | defaultsVisibility := &DefaultsVisibilityProperties{} | 
| Paul Duffin | 95d53b5 | 2019-07-24 13:45:05 +0100 | [diff] [blame] | 207 | module.AddProperties(&base.nameProperties, defaultsVisibility) | 
| Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 208 |  | 
| Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 209 | // Unlike non-defaults modules the visibility property is not stored in m.base().commonProperties. | 
| Paul Duffin | 5ec73ec | 2020-05-01 17:52:01 +0100 | [diff] [blame] | 210 | // Instead it is stored in a separate instance of commonProperties created above so clear the | 
|  | 211 | // existing list of properties. | 
|  | 212 | clearVisibilityProperties(module) | 
|  | 213 |  | 
|  | 214 | // The defaults_visibility property controls the visibility of a defaults module so it must be | 
|  | 215 | // set as the primary property, which also adds it to the list. | 
|  | 216 | setPrimaryVisibilityProperty(module, "defaults_visibility", &defaultsVisibility.Defaults_visibility) | 
|  | 217 |  | 
| Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 218 | // The visibility property needs to be checked (but not parsed) by the visibility module during | 
| Paul Duffin | 5ec73ec | 2020-05-01 17:52:01 +0100 | [diff] [blame] | 219 | // its checking phase and parsing phase so add it to the list as a normal property. | 
|  | 220 | AddVisibilityProperty(module, "visibility", &commonProperties.Visibility) | 
| Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 221 |  | 
| Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 222 | // The applicable licenses property for defaults is 'licenses'. | 
|  | 223 | setPrimaryLicensesProperty(module, "licenses", &commonProperties.Licenses) | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 224 | } | 
|  | 225 |  | 
| Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 226 | var _ Defaults = (*DefaultsModuleBase)(nil) | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 227 |  | 
| Jingwen Chen | 84817de | 2021-11-17 10:57:35 +0000 | [diff] [blame] | 228 | // applyNamespacedVariableDefaults only runs in bp2build mode for | 
|  | 229 | // defaultable/defaults modules. Its purpose is to merge namespaced product | 
|  | 230 | // variable props from defaults deps, even if those defaults are custom module | 
|  | 231 | // types created from soong_config_module_type, e.g. one that's wrapping a | 
|  | 232 | // cc_defaults or java_defaults. | 
|  | 233 | func applyNamespacedVariableDefaults(defaultDep Defaults, ctx TopDownMutatorContext) { | 
|  | 234 | var dep, b Bazelable | 
|  | 235 |  | 
|  | 236 | dep, ok := defaultDep.(Bazelable) | 
|  | 237 | if !ok { | 
|  | 238 | if depMod, ok := defaultDep.(Module); ok { | 
|  | 239 | // Track that this dependency hasn't been converted to bp2build yet. | 
|  | 240 | ctx.AddUnconvertedBp2buildDep(depMod.Name()) | 
|  | 241 | return | 
|  | 242 | } else { | 
|  | 243 | panic("Expected default dep to be a Module.") | 
|  | 244 | } | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | b, ok = ctx.Module().(Bazelable) | 
|  | 248 | if !ok { | 
|  | 249 | return | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | // namespacedVariableProps is a map from namespaces (e.g. acme, android, | 
|  | 253 | // vendor_foo) to a slice of soong_config_variable struct pointers, | 
|  | 254 | // containing properties for that particular module. | 
|  | 255 | src := dep.namespacedVariableProps() | 
|  | 256 | dst := b.namespacedVariableProps() | 
|  | 257 | if dst == nil { | 
|  | 258 | dst = make(namespacedVariableProperties) | 
|  | 259 | } | 
|  | 260 |  | 
|  | 261 | // Propagate all soong_config_variable structs from the dep. We'll merge the | 
|  | 262 | // actual property values later in variable.go. | 
|  | 263 | for namespace := range src { | 
|  | 264 | if dst[namespace] == nil { | 
|  | 265 | dst[namespace] = []interface{}{} | 
|  | 266 | } | 
|  | 267 | for _, i := range src[namespace] { | 
|  | 268 | dst[namespace] = append(dst[namespace], i) | 
|  | 269 | } | 
|  | 270 | } | 
|  | 271 |  | 
|  | 272 | b.setNamespacedVariableProps(dst) | 
|  | 273 | } | 
|  | 274 |  | 
| Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 275 | func (defaultable *DefaultableModuleBase) applyDefaults(ctx TopDownMutatorContext, | 
| Liz Kammer | eeeb952 | 2023-03-10 18:53:41 -0500 | [diff] [blame] | 276 | defaultsList []Defaults) { | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 277 |  | 
| Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 278 | for _, defaults := range defaultsList { | 
| Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 279 | if ctx.Config().BuildMode == Bp2build { | 
| Jingwen Chen | 84817de | 2021-11-17 10:57:35 +0000 | [diff] [blame] | 280 | applyNamespacedVariableDefaults(defaults, ctx) | 
|  | 281 | } | 
| Liz Kammer | eeeb952 | 2023-03-10 18:53:41 -0500 | [diff] [blame] | 282 | for _, prop := range defaultable.defaultableProperties { | 
|  | 283 | if prop == defaultable.defaultableVariableProperties { | 
|  | 284 | defaultable.applyDefaultVariableProperties(ctx, defaults, prop) | 
| Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame] | 285 | } else { | 
| Liz Kammer | eeeb952 | 2023-03-10 18:53:41 -0500 | [diff] [blame] | 286 | defaultable.applyDefaultProperties(ctx, defaults, prop) | 
| Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame] | 287 | } | 
|  | 288 | } | 
|  | 289 | } | 
|  | 290 | } | 
|  | 291 |  | 
| Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 292 | // Product variable properties need special handling, the type of the filtered product variable | 
|  | 293 | // property struct may not be identical between the defaults module and the defaultable module. | 
|  | 294 | // Use PrependMatchingProperties to apply whichever properties match. | 
| Liz Kammer | eeeb952 | 2023-03-10 18:53:41 -0500 | [diff] [blame] | 295 | func (defaultable *DefaultableModuleBase) applyDefaultVariableProperties(ctx TopDownMutatorContext, | 
|  | 296 | defaults Defaults, defaultableProp interface{}) { | 
| Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 297 | if defaultableProp == nil { | 
| Liz Kammer | eeeb952 | 2023-03-10 18:53:41 -0500 | [diff] [blame] | 298 | return | 
| Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 299 | } | 
|  | 300 |  | 
|  | 301 | defaultsProp := defaults.productVariableProperties() | 
|  | 302 | if defaultsProp == nil { | 
| Liz Kammer | eeeb952 | 2023-03-10 18:53:41 -0500 | [diff] [blame] | 303 | return | 
| Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 304 | } | 
|  | 305 |  | 
|  | 306 | dst := []interface{}{ | 
|  | 307 | defaultableProp, | 
|  | 308 | // Put an empty copy of the src properties into dst so that properties in src that are not in dst | 
|  | 309 | // don't cause a "failed to find property to extend" error. | 
|  | 310 | proptools.CloneEmptyProperties(reflect.ValueOf(defaultsProp)).Interface(), | 
|  | 311 | } | 
|  | 312 |  | 
| Liz Kammer | eeeb952 | 2023-03-10 18:53:41 -0500 | [diff] [blame] | 313 | err := proptools.PrependMatchingProperties(dst, defaultsProp, nil) | 
|  | 314 | if err != nil { | 
|  | 315 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { | 
|  | 316 | ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) | 
|  | 317 | } else { | 
|  | 318 | panic(err) | 
|  | 319 | } | 
|  | 320 | } | 
| Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 321 | } | 
|  | 322 |  | 
| Liz Kammer | eeeb952 | 2023-03-10 18:53:41 -0500 | [diff] [blame] | 323 | func (defaultable *DefaultableModuleBase) applyDefaultProperties(ctx TopDownMutatorContext, | 
|  | 324 | defaults Defaults, defaultableProp interface{}) { | 
| Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 325 |  | 
|  | 326 | for _, def := range defaults.properties() { | 
|  | 327 | if proptools.TypeEqual(defaultableProp, def) { | 
| Liz Kammer | eeeb952 | 2023-03-10 18:53:41 -0500 | [diff] [blame] | 328 | err := proptools.PrependProperties(defaultableProp, def, nil) | 
| Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 329 | if err != nil { | 
| Liz Kammer | eeeb952 | 2023-03-10 18:53:41 -0500 | [diff] [blame] | 330 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { | 
|  | 331 | ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) | 
|  | 332 | } else { | 
|  | 333 | panic(err) | 
|  | 334 | } | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 335 | } | 
|  | 336 | } | 
|  | 337 | } | 
|  | 338 | } | 
|  | 339 |  | 
| Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 340 | func RegisterDefaultsPreArchMutators(ctx RegisterMutatorsContext) { | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 341 | ctx.BottomUp("defaults_deps", defaultsDepsMutator).Parallel() | 
|  | 342 | ctx.TopDown("defaults", defaultsMutator).Parallel() | 
|  | 343 | } | 
|  | 344 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 345 | func defaultsDepsMutator(ctx BottomUpMutatorContext) { | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 346 | if defaultable, ok := ctx.Module().(Defaultable); ok { | 
| Colin Cross | c99deeb | 2016-04-11 15:06:20 -0700 | [diff] [blame] | 347 | ctx.AddDependency(ctx.Module(), DefaultsDepTag, defaultable.defaults().Defaults...) | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 348 | } | 
|  | 349 | } | 
|  | 350 |  | 
| Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 351 | func defaultsMutator(ctx TopDownMutatorContext) { | 
| Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 352 | if defaultable, ok := ctx.Module().(Defaultable); ok { | 
|  | 353 | if len(defaultable.defaults().Defaults) > 0 { | 
| Liz Kammer | eeeb952 | 2023-03-10 18:53:41 -0500 | [diff] [blame] | 354 | var defaultsList []Defaults | 
| Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 355 | seen := make(map[Defaults]bool) | 
| Colin Cross | a1ce2a0 | 2018-06-20 15:19:39 -0700 | [diff] [blame] | 356 |  | 
| Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 357 | ctx.WalkDeps(func(module, parent Module) bool { | 
|  | 358 | if ctx.OtherModuleDependencyTag(module) == DefaultsDepTag { | 
| Liz Kammer | eeeb952 | 2023-03-10 18:53:41 -0500 | [diff] [blame] | 359 | if defaults, ok := module.(Defaults); ok { | 
| Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 360 | if !seen[defaults] { | 
|  | 361 | seen[defaults] = true | 
|  | 362 | defaultsList = append(defaultsList, defaults) | 
|  | 363 | return len(defaults.defaults().Defaults) > 0 | 
|  | 364 | } | 
|  | 365 | } else { | 
|  | 366 | ctx.PropertyErrorf("defaults", "module %s is not an defaults module", | 
|  | 367 | ctx.OtherModuleName(module)) | 
| Colin Cross | a1ce2a0 | 2018-06-20 15:19:39 -0700 | [diff] [blame] | 368 | } | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 369 | } | 
| Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 370 | return false | 
|  | 371 | }) | 
|  | 372 | defaultable.applyDefaults(ctx, defaultsList) | 
|  | 373 | } | 
|  | 374 |  | 
| Jihoon Kang | d48abd5 | 2023-02-02 22:32:31 +0000 | [diff] [blame] | 375 | defaultable.CallHookIfAvailable(ctx) | 
| Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 376 | } | 
|  | 377 | } |