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 ( |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 18 | "bytes" |
| 19 | "fmt" |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 20 | "reflect" |
| 21 | |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 22 | "github.com/google/blueprint" |
| 23 | "github.com/google/blueprint/proptools" |
| 24 | ) |
| 25 | |
Colin Cross | c99deeb | 2016-04-11 15:06:20 -0700 | [diff] [blame] | 26 | type defaultsDependencyTag struct { |
| 27 | blueprint.BaseDependencyTag |
| 28 | } |
| 29 | |
| 30 | var DefaultsDepTag defaultsDependencyTag |
| 31 | |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 32 | type defaultsProperties struct { |
| 33 | Defaults []string |
| 34 | } |
| 35 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 36 | type DefaultableModuleBase struct { |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 37 | defaultsProperties defaultsProperties |
| 38 | defaultableProperties []interface{} |
| 39 | defaultableVariableProperties interface{} |
Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 40 | |
| 41 | // The optional hook to call after any defaults have been applied. |
| 42 | hook DefaultableHook |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 45 | func (d *DefaultableModuleBase) defaults() *defaultsProperties { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 46 | return &d.defaultsProperties |
| 47 | } |
| 48 | |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 49 | func (d *DefaultableModuleBase) setProperties(props []interface{}, variableProperties interface{}) { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 50 | d.defaultableProperties = props |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 51 | d.defaultableVariableProperties = variableProperties |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 54 | func (d *DefaultableModuleBase) SetDefaultableHook(hook DefaultableHook) { |
| 55 | d.hook = hook |
| 56 | } |
| 57 | |
| 58 | func (d *DefaultableModuleBase) callHookIfAvailable(ctx DefaultableHookContext) { |
| 59 | if d.hook != nil { |
| 60 | d.hook(ctx) |
| 61 | } |
| 62 | } |
| 63 | |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 64 | // 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] | 65 | type Defaultable interface { |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 66 | // Get a pointer to the struct containing the Defaults property. |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 67 | defaults() *defaultsProperties |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 68 | |
| 69 | // Set the property structures into which defaults will be added. |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 70 | setProperties(props []interface{}, variableProperties interface{}) |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 71 | |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 72 | // Apply defaults from the supplied DefaultsModule to the property structures supplied to |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 73 | // setProperties(...). |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 74 | applyDefaults(TopDownMutatorContext, []DefaultsModule) |
| 75 | |
| 76 | applySingleDefaultsWithTracker(EarlyModuleContext, DefaultsModule, defaultsTrackerFunc) |
Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 77 | |
| 78 | // Set the hook to be called after any defaults have been applied. |
| 79 | // |
| 80 | // Should be used in preference to a AddLoadHook when the behavior of the load |
| 81 | // hook is dependent on properties supplied in the Android.bp file. |
| 82 | SetDefaultableHook(hook DefaultableHook) |
| 83 | |
| 84 | // Call the hook if specified. |
| 85 | callHookIfAvailable(context DefaultableHookContext) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 88 | type DefaultableModule interface { |
| 89 | Module |
| 90 | Defaultable |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 93 | var _ Defaultable = (*DefaultableModuleBase)(nil) |
| 94 | |
| 95 | func InitDefaultableModule(module DefaultableModule) { |
Usta | fe201fe | 2021-12-06 15:13:36 -0500 | [diff] [blame] | 96 | if module.base().module == nil { |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 97 | panic("InitAndroidModule must be called before InitDefaultableModule") |
| 98 | } |
Liz Kammer | 416201d | 2021-12-15 13:18:42 -0500 | [diff] [blame] | 99 | |
Usta | fe201fe | 2021-12-06 15:13:36 -0500 | [diff] [blame] | 100 | module.setProperties(module.GetProperties(), module.base().variableProperties) |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 101 | |
| 102 | module.AddProperties(module.defaults()) |
| 103 | } |
| 104 | |
Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 105 | // A restricted subset of context methods, similar to LoadHookContext. |
| 106 | type DefaultableHookContext interface { |
| 107 | EarlyModuleContext |
| 108 | |
| 109 | CreateModule(ModuleFactory, ...interface{}) Module |
Colin Cross | 18f840c | 2021-05-20 17:56:54 -0700 | [diff] [blame] | 110 | AddMissingDependencies(missingDeps []string) |
Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | type DefaultableHook func(ctx DefaultableHookContext) |
| 114 | |
Paul Duffin | 95d53b5 | 2019-07-24 13:45:05 +0100 | [diff] [blame] | 115 | // The Defaults_visibility property. |
| 116 | type DefaultsVisibilityProperties struct { |
| 117 | |
| 118 | // Controls the visibility of the defaults module itself. |
| 119 | Defaults_visibility []string |
| 120 | } |
| 121 | |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 122 | // AdditionalDefaultsProperties contains properties of defaults modules which |
| 123 | // can have other defaults applied. |
| 124 | type AdditionalDefaultsProperties struct { |
| 125 | |
| 126 | // The list of properties set by the default whose values must not be changed by any module that |
| 127 | // applies these defaults. It is an error if a property is not supported by the defaults module or |
| 128 | // has not been set to a non-zero value. If this contains "*" then that must be the only entry in |
| 129 | // which case all properties that are set on this defaults will be protected (except the |
| 130 | // protected_properties and visibility. |
| 131 | Protected_properties []string |
| 132 | } |
| 133 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 134 | type DefaultsModuleBase struct { |
| 135 | DefaultableModuleBase |
Liz Kammer | 416201d | 2021-12-15 13:18:42 -0500 | [diff] [blame] | 136 | |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 137 | defaultsProperties AdditionalDefaultsProperties |
| 138 | |
Liz Kammer | 416201d | 2021-12-15 13:18:42 -0500 | [diff] [blame] | 139 | // Included to support setting bazel_module.label for multiple Soong modules to the same Bazel |
| 140 | // target. This is primarily useful for modules that were architecture specific and instead are |
| 141 | // handled in Bazel as a select(). |
| 142 | BazelModuleBase |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Martin Stjernholm | ebd757d | 2019-05-24 11:00:30 +0100 | [diff] [blame] | 145 | // The common pattern for defaults modules is to register separate instances of |
| 146 | // the xxxProperties structs in the AddProperties calls, rather than reusing the |
| 147 | // ones inherited from Module. |
| 148 | // |
| 149 | // The effect is that e.g. myDefaultsModuleInstance.base().xxxProperties won't |
| 150 | // contain the values that have been set for the defaults module. Rather, to |
| 151 | // retrieve the values it is necessary to iterate over properties(). E.g. to get |
| 152 | // the commonProperties instance that have the real values: |
| 153 | // |
| 154 | // d := myModule.(Defaults) |
| 155 | // for _, props := range d.properties() { |
| 156 | // if cp, ok := props.(*commonProperties); ok { |
| 157 | // ... access property values in cp ... |
| 158 | // } |
| 159 | // } |
| 160 | // |
| 161 | // The rationale is that the properties on a defaults module apply to the |
| 162 | // defaultable modules using it, not to the defaults module itself. E.g. setting |
| 163 | // the "enabled" property false makes inheriting modules disabled by default, |
| 164 | // rather than disabling the defaults module itself. |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 165 | type Defaults interface { |
Colin Cross | e7b0713 | 2016-07-27 10:15:06 -0700 | [diff] [blame] | 166 | Defaultable |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 167 | |
| 168 | // Although this function is unused it is actually needed to ensure that only modules that embed |
| 169 | // DefaultsModuleBase will type-assert to the Defaults interface. |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 170 | isDefaults() bool |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 171 | |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 172 | // additionalDefaultableProperties returns additional properties provided by the defaults which |
| 173 | // can themselves have defaults applied. |
| 174 | additionalDefaultableProperties() []interface{} |
| 175 | |
| 176 | // protectedProperties returns the names of the properties whose values cannot be changed by a |
| 177 | // module that applies these defaults. |
| 178 | protectedProperties() []string |
| 179 | |
| 180 | // setProtectedProperties sets the names of the properties whose values cannot be changed by a |
| 181 | // module that applies these defaults. |
| 182 | setProtectedProperties(protectedProperties []string) |
| 183 | |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 184 | // Get the structures containing the properties for which defaults can be provided. |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 185 | properties() []interface{} |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 186 | |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 187 | productVariableProperties() interface{} |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 190 | func (d *DefaultsModuleBase) isDefaults() bool { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 191 | return true |
| 192 | } |
| 193 | |
Paul Duffin | e62432f | 2019-07-24 12:51:21 +0100 | [diff] [blame] | 194 | type DefaultsModule interface { |
| 195 | Module |
| 196 | Defaults |
Liz Kammer | 416201d | 2021-12-15 13:18:42 -0500 | [diff] [blame] | 197 | Bazelable |
Paul Duffin | e62432f | 2019-07-24 12:51:21 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 200 | func (d *DefaultsModuleBase) additionalDefaultableProperties() []interface{} { |
| 201 | return []interface{}{&d.defaultsProperties} |
| 202 | } |
| 203 | |
| 204 | func (d *DefaultsModuleBase) protectedProperties() []string { |
| 205 | return d.defaultsProperties.Protected_properties |
| 206 | } |
| 207 | |
| 208 | func (d *DefaultsModuleBase) setProtectedProperties(protectedProperties []string) { |
| 209 | d.defaultsProperties.Protected_properties = protectedProperties |
| 210 | } |
| 211 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 212 | func (d *DefaultsModuleBase) properties() []interface{} { |
Colin Cross | e7b0713 | 2016-07-27 10:15:06 -0700 | [diff] [blame] | 213 | return d.defaultableProperties |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 216 | func (d *DefaultsModuleBase) productVariableProperties() interface{} { |
| 217 | return d.defaultableVariableProperties |
| 218 | } |
| 219 | |
Liz Kammer | 416201d | 2021-12-15 13:18:42 -0500 | [diff] [blame] | 220 | func (d *DefaultsModuleBase) GenerateAndroidBuildActions(ctx ModuleContext) {} |
Colin Cross | 5903762 | 2019-06-10 13:12:56 -0700 | [diff] [blame] | 221 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 222 | // ConvertWithBp2build to fulfill Bazelable interface; however, at this time defaults module are |
| 223 | // *NOT* converted with bp2build |
| 224 | func (defaultable *DefaultsModuleBase) ConvertWithBp2build(ctx TopDownMutatorContext) {} |
| 225 | |
Paul Duffin | e62432f | 2019-07-24 12:51:21 +0100 | [diff] [blame] | 226 | func InitDefaultsModule(module DefaultsModule) { |
Paul Duffin | 3f98d14 | 2020-09-02 13:28:25 +0100 | [diff] [blame] | 227 | commonProperties := &commonProperties{} |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 228 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 229 | module.AddProperties( |
Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 230 | &hostAndDeviceProperties{}, |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 231 | commonProperties, |
Paul Duffin | ed87513 | 2020-09-02 13:08:57 +0100 | [diff] [blame] | 232 | &ApexProperties{}, |
| 233 | &distProperties{}) |
Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 234 | |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 235 | // Additional properties of defaults modules that can themselves have |
| 236 | // defaults applied. |
| 237 | module.AddProperties(module.additionalDefaultableProperties()...) |
| 238 | |
Liz Kammer | 416201d | 2021-12-15 13:18:42 -0500 | [diff] [blame] | 239 | // Bazel module must be initialized _before_ Defaults to be included in cc_defaults module. |
| 240 | InitBazelModule(module) |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 241 | initAndroidModuleBase(module) |
| 242 | initProductVariableModule(module) |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 243 | initArchModule(module) |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 244 | InitDefaultableModule(module) |
Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 245 | |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 246 | // Add properties that will not have defaults applied to them. |
Colin Cross | 08d6f8f | 2020-11-19 02:33:19 +0000 | [diff] [blame] | 247 | base := module.base() |
Paul Duffin | 3f98d14 | 2020-09-02 13:28:25 +0100 | [diff] [blame] | 248 | defaultsVisibility := &DefaultsVisibilityProperties{} |
Paul Duffin | 95d53b5 | 2019-07-24 13:45:05 +0100 | [diff] [blame] | 249 | module.AddProperties(&base.nameProperties, defaultsVisibility) |
Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 250 | |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 251 | // 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] | 252 | // Instead it is stored in a separate instance of commonProperties created above so clear the |
| 253 | // existing list of properties. |
| 254 | clearVisibilityProperties(module) |
| 255 | |
| 256 | // The defaults_visibility property controls the visibility of a defaults module so it must be |
| 257 | // set as the primary property, which also adds it to the list. |
| 258 | setPrimaryVisibilityProperty(module, "defaults_visibility", &defaultsVisibility.Defaults_visibility) |
| 259 | |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 260 | // 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] | 261 | // its checking phase and parsing phase so add it to the list as a normal property. |
| 262 | AddVisibilityProperty(module, "visibility", &commonProperties.Visibility) |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 263 | |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 264 | // The applicable licenses property for defaults is 'licenses'. |
| 265 | setPrimaryLicensesProperty(module, "licenses", &commonProperties.Licenses) |
| 266 | |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 267 | AddLoadHook(module, func(ctx LoadHookContext) { |
| 268 | |
| 269 | protectedProperties := module.protectedProperties() |
| 270 | if len(protectedProperties) == 0 { |
| 271 | return |
| 272 | } |
| 273 | |
| 274 | propertiesAvailable := map[string]struct{}{} |
| 275 | propertiesSet := map[string]struct{}{} |
| 276 | |
| 277 | // A defaults tracker which will keep track of which properties have been set on this module. |
| 278 | collector := func(defaults DefaultsModule, property string, dstValue interface{}, srcValue interface{}) bool { |
| 279 | value := reflect.ValueOf(dstValue) |
| 280 | propertiesAvailable[property] = struct{}{} |
| 281 | if !value.IsZero() { |
| 282 | propertiesSet[property] = struct{}{} |
| 283 | } |
| 284 | // Skip all the properties so that there are no changes to the defaults. |
| 285 | return false |
| 286 | } |
| 287 | |
| 288 | // Try and apply this module's defaults to itself, so that the properties can be collected but |
| 289 | // skip all the properties so it doesn't actually do anything. |
| 290 | module.applySingleDefaultsWithTracker(ctx, module, collector) |
| 291 | |
| 292 | if InList("*", protectedProperties) { |
| 293 | if len(protectedProperties) != 1 { |
| 294 | ctx.PropertyErrorf("protected_properties", `if specified then "*" must be the only property listed`) |
| 295 | return |
| 296 | } |
| 297 | |
| 298 | // Do not automatically protect the protected_properties property. |
| 299 | delete(propertiesSet, "protected_properties") |
| 300 | |
| 301 | // Or the visibility property. |
| 302 | delete(propertiesSet, "visibility") |
| 303 | |
| 304 | // Replace the "*" with the names of all the properties that have been set. |
| 305 | protectedProperties = SortedStringKeys(propertiesSet) |
| 306 | module.setProtectedProperties(protectedProperties) |
| 307 | } else { |
| 308 | for _, property := range protectedProperties { |
| 309 | if _, ok := propertiesAvailable[property]; !ok { |
| 310 | ctx.PropertyErrorf(property, "property is not supported by this module type %q", |
| 311 | ctx.ModuleType()) |
| 312 | } else if _, ok := propertiesSet[property]; !ok { |
| 313 | ctx.PropertyErrorf(property, "is not set; protected properties must be explicitly set") |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | }) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 320 | var _ Defaults = (*DefaultsModuleBase)(nil) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 321 | |
Jingwen Chen | 84817de | 2021-11-17 10:57:35 +0000 | [diff] [blame] | 322 | // applyNamespacedVariableDefaults only runs in bp2build mode for |
| 323 | // defaultable/defaults modules. Its purpose is to merge namespaced product |
| 324 | // variable props from defaults deps, even if those defaults are custom module |
| 325 | // types created from soong_config_module_type, e.g. one that's wrapping a |
| 326 | // cc_defaults or java_defaults. |
| 327 | func applyNamespacedVariableDefaults(defaultDep Defaults, ctx TopDownMutatorContext) { |
| 328 | var dep, b Bazelable |
| 329 | |
| 330 | dep, ok := defaultDep.(Bazelable) |
| 331 | if !ok { |
| 332 | if depMod, ok := defaultDep.(Module); ok { |
| 333 | // Track that this dependency hasn't been converted to bp2build yet. |
| 334 | ctx.AddUnconvertedBp2buildDep(depMod.Name()) |
| 335 | return |
| 336 | } else { |
| 337 | panic("Expected default dep to be a Module.") |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | b, ok = ctx.Module().(Bazelable) |
| 342 | if !ok { |
| 343 | return |
| 344 | } |
| 345 | |
| 346 | // namespacedVariableProps is a map from namespaces (e.g. acme, android, |
| 347 | // vendor_foo) to a slice of soong_config_variable struct pointers, |
| 348 | // containing properties for that particular module. |
| 349 | src := dep.namespacedVariableProps() |
| 350 | dst := b.namespacedVariableProps() |
| 351 | if dst == nil { |
| 352 | dst = make(namespacedVariableProperties) |
| 353 | } |
| 354 | |
| 355 | // Propagate all soong_config_variable structs from the dep. We'll merge the |
| 356 | // actual property values later in variable.go. |
| 357 | for namespace := range src { |
| 358 | if dst[namespace] == nil { |
| 359 | dst[namespace] = []interface{}{} |
| 360 | } |
| 361 | for _, i := range src[namespace] { |
| 362 | dst[namespace] = append(dst[namespace], i) |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | b.setNamespacedVariableProps(dst) |
| 367 | } |
| 368 | |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 369 | // defaultValueInfo contains information about each default value that applies to a protected |
| 370 | // property. |
| 371 | type defaultValueInfo struct { |
| 372 | // The DefaultsModule providing the value, which may be defined on that module or applied as a |
| 373 | // default from other modules. |
| 374 | module Module |
| 375 | |
| 376 | // The default value, as returned by getComparableValue |
| 377 | defaultValue reflect.Value |
| 378 | } |
| 379 | |
| 380 | // protectedPropertyInfo contains information about each property that has to be protected when |
| 381 | // applying defaults. |
| 382 | type protectedPropertyInfo struct { |
| 383 | // True if the property was set on the module to which defaults are applied, this is an error. |
| 384 | propertySet bool |
| 385 | |
| 386 | // The original value of the property on the module, as returned by getComparableValue. |
| 387 | originalValue reflect.Value |
| 388 | |
| 389 | // A list of defaults for the property that are being applied. |
| 390 | defaultValues []defaultValueInfo |
| 391 | } |
| 392 | |
| 393 | // getComparableValue takes a reflect.Value that may be a pointer to another value and returns a |
| 394 | // reflect.Value to the underlying data or the original if was not a pointer or was nil. The |
| 395 | // returned values can then be compared for equality. |
| 396 | func getComparableValue(value reflect.Value) reflect.Value { |
| 397 | if value.IsZero() { |
| 398 | return value |
| 399 | } |
| 400 | for value.Kind() == reflect.Ptr { |
| 401 | value = value.Elem() |
| 402 | } |
| 403 | return value |
| 404 | } |
| 405 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 406 | func (defaultable *DefaultableModuleBase) applyDefaults(ctx TopDownMutatorContext, |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 407 | defaultsList []DefaultsModule) { |
| 408 | |
| 409 | // Collate information on all the properties protected by each of the default modules applied |
| 410 | // to this module. |
| 411 | allProtectedProperties := map[string]*protectedPropertyInfo{} |
| 412 | for _, defaults := range defaultsList { |
| 413 | for _, property := range defaults.protectedProperties() { |
| 414 | info := allProtectedProperties[property] |
| 415 | if info == nil { |
| 416 | info = &protectedPropertyInfo{} |
| 417 | allProtectedProperties[property] = info |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | // If there are any protected properties then collate information about attempts to change them. |
| 423 | var protectedPropertyInfoCollector defaultsTrackerFunc |
| 424 | if len(allProtectedProperties) > 0 { |
| 425 | protectedPropertyInfoCollector = func(defaults DefaultsModule, property string, |
| 426 | dstValue interface{}, srcValue interface{}) bool { |
| 427 | |
| 428 | // If the property is not protected then return immediately. |
| 429 | info := allProtectedProperties[property] |
| 430 | if info == nil { |
| 431 | return true |
| 432 | } |
| 433 | |
| 434 | currentValue := reflect.ValueOf(dstValue) |
| 435 | if info.defaultValues == nil { |
| 436 | info.propertySet = !currentValue.IsZero() |
| 437 | info.originalValue = getComparableValue(currentValue) |
| 438 | } |
| 439 | |
| 440 | defaultValue := reflect.ValueOf(srcValue) |
| 441 | if !defaultValue.IsZero() { |
| 442 | info.defaultValues = append(info.defaultValues, |
| 443 | defaultValueInfo{defaults, getComparableValue(defaultValue)}) |
| 444 | } |
| 445 | |
| 446 | return true |
| 447 | } |
| 448 | } |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 449 | |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 450 | for _, defaults := range defaultsList { |
Jingwen Chen | 84817de | 2021-11-17 10:57:35 +0000 | [diff] [blame] | 451 | if ctx.Config().runningAsBp2Build { |
| 452 | applyNamespacedVariableDefaults(defaults, ctx) |
| 453 | } |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 454 | |
| 455 | defaultable.applySingleDefaultsWithTracker(ctx, defaults, protectedPropertyInfoCollector) |
| 456 | } |
| 457 | |
| 458 | // Check the status of any protected properties. |
| 459 | for property, info := range allProtectedProperties { |
| 460 | if len(info.defaultValues) == 0 { |
| 461 | // No defaults were applied to the protected properties. Possibly because this module type |
| 462 | // does not support any of them. |
| 463 | continue |
| 464 | } |
| 465 | |
| 466 | // Check to make sure that there are no conflicts between the defaults. |
| 467 | conflictingDefaults := false |
| 468 | previousDefaultValue := reflect.ValueOf(false) |
| 469 | for _, defaultInfo := range info.defaultValues { |
| 470 | defaultValue := defaultInfo.defaultValue |
| 471 | if previousDefaultValue.IsZero() { |
| 472 | previousDefaultValue = defaultValue |
| 473 | } else if !reflect.DeepEqual(previousDefaultValue.Interface(), defaultValue.Interface()) { |
| 474 | conflictingDefaults = true |
| 475 | break |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 476 | } |
| 477 | } |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 478 | |
| 479 | if conflictingDefaults { |
| 480 | var buf bytes.Buffer |
| 481 | for _, defaultInfo := range info.defaultValues { |
| 482 | buf.WriteString(fmt.Sprintf("\n defaults module %q provides value %#v", |
| 483 | ctx.OtherModuleName(defaultInfo.module), defaultInfo.defaultValue)) |
| 484 | } |
| 485 | result := buf.String() |
| 486 | ctx.ModuleErrorf("has conflicting default values for protected property %q:%s", property, result) |
| 487 | continue |
| 488 | } |
| 489 | |
| 490 | // Now check to see whether there the current module tried to override/append to the defaults. |
| 491 | if info.propertySet { |
| 492 | originalValue := info.originalValue |
| 493 | // Just compare against the first defaults. |
| 494 | defaultValue := info.defaultValues[0].defaultValue |
| 495 | defaults := info.defaultValues[0].module |
| 496 | |
| 497 | if originalValue.Kind() == reflect.Slice { |
| 498 | ctx.ModuleErrorf("attempts to append %q to protected property %q's value of %q defined in module %q", |
| 499 | originalValue, |
| 500 | property, |
| 501 | defaultValue, |
| 502 | ctx.OtherModuleName(defaults)) |
| 503 | } else { |
| 504 | same := reflect.DeepEqual(originalValue.Interface(), defaultValue.Interface()) |
| 505 | message := "" |
| 506 | if same { |
| 507 | message = fmt.Sprintf(" with a matching value (%#v) so this property can simply be removed.", originalValue) |
| 508 | } else { |
| 509 | message = fmt.Sprintf(" with a different value (override %#v with %#v) so removing the property may necessitate other changes.", defaultValue, originalValue) |
| 510 | } |
| 511 | ctx.ModuleErrorf("attempts to override protected property %q defined in module %q%s", |
| 512 | property, |
| 513 | ctx.OtherModuleName(defaults), message) |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | func (defaultable *DefaultableModuleBase) applySingleDefaultsWithTracker(ctx EarlyModuleContext, defaults DefaultsModule, tracker defaultsTrackerFunc) { |
| 520 | for _, prop := range defaultable.defaultableProperties { |
| 521 | var err error |
| 522 | if prop == defaultable.defaultableVariableProperties { |
| 523 | err = defaultable.applyDefaultVariableProperties(defaults, prop, tracker) |
| 524 | } else { |
| 525 | err = defaultable.applyDefaultProperties(defaults, prop, tracker) |
| 526 | } |
| 527 | if err != nil { |
| 528 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 529 | ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 530 | } else { |
| 531 | panic(err) |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | // defaultsTrackerFunc is the type of a function that can be used to track how defaults are applied. |
| 538 | type defaultsTrackerFunc func(defaults DefaultsModule, property string, |
| 539 | dstValue interface{}, srcValue interface{}) bool |
| 540 | |
| 541 | // filterForTracker wraps a defaultsTrackerFunc in a proptools.ExtendPropertyFilterFunc |
| 542 | func filterForTracker(defaults DefaultsModule, tracker defaultsTrackerFunc) proptools.ExtendPropertyFilterFunc { |
| 543 | if tracker == nil { |
| 544 | return nil |
| 545 | } |
| 546 | return func(property string, |
| 547 | dstField, srcField reflect.StructField, |
| 548 | dstValue, srcValue interface{}) (bool, error) { |
| 549 | |
| 550 | apply := tracker(defaults, property, dstValue, srcValue) |
| 551 | return apply, nil |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
| 555 | // Product variable properties need special handling, the type of the filtered product variable |
| 556 | // property struct may not be identical between the defaults module and the defaultable module. |
| 557 | // Use PrependMatchingProperties to apply whichever properties match. |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 558 | func (defaultable *DefaultableModuleBase) applyDefaultVariableProperties(defaults DefaultsModule, |
| 559 | defaultableProp interface{}, tracker defaultsTrackerFunc) error { |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 560 | if defaultableProp == nil { |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 561 | return nil |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | defaultsProp := defaults.productVariableProperties() |
| 565 | if defaultsProp == nil { |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 566 | return nil |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | dst := []interface{}{ |
| 570 | defaultableProp, |
| 571 | // Put an empty copy of the src properties into dst so that properties in src that are not in dst |
| 572 | // don't cause a "failed to find property to extend" error. |
| 573 | proptools.CloneEmptyProperties(reflect.ValueOf(defaultsProp)).Interface(), |
| 574 | } |
| 575 | |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 576 | filter := filterForTracker(defaults, tracker) |
| 577 | |
| 578 | return proptools.PrependMatchingProperties(dst, defaultsProp, filter) |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 579 | } |
| 580 | |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 581 | func (defaultable *DefaultableModuleBase) applyDefaultProperties(defaults DefaultsModule, |
| 582 | defaultableProp interface{}, checker defaultsTrackerFunc) error { |
| 583 | |
| 584 | filter := filterForTracker(defaults, checker) |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 585 | |
| 586 | for _, def := range defaults.properties() { |
| 587 | if proptools.TypeEqual(defaultableProp, def) { |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 588 | err := proptools.PrependProperties(defaultableProp, def, filter) |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 589 | if err != nil { |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 590 | return err |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | } |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 594 | |
| 595 | return nil |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 596 | } |
| 597 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 598 | func RegisterDefaultsPreArchMutators(ctx RegisterMutatorsContext) { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 599 | ctx.BottomUp("defaults_deps", defaultsDepsMutator).Parallel() |
| 600 | ctx.TopDown("defaults", defaultsMutator).Parallel() |
| 601 | } |
| 602 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 603 | func defaultsDepsMutator(ctx BottomUpMutatorContext) { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 604 | if defaultable, ok := ctx.Module().(Defaultable); ok { |
Colin Cross | c99deeb | 2016-04-11 15:06:20 -0700 | [diff] [blame] | 605 | ctx.AddDependency(ctx.Module(), DefaultsDepTag, defaultable.defaults().Defaults...) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 609 | func defaultsMutator(ctx TopDownMutatorContext) { |
Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 610 | if defaultable, ok := ctx.Module().(Defaultable); ok { |
| 611 | if len(defaultable.defaults().Defaults) > 0 { |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 612 | var defaultsList []DefaultsModule |
Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 613 | seen := make(map[Defaults]bool) |
Colin Cross | a1ce2a0 | 2018-06-20 15:19:39 -0700 | [diff] [blame] | 614 | |
Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 615 | ctx.WalkDeps(func(module, parent Module) bool { |
| 616 | if ctx.OtherModuleDependencyTag(module) == DefaultsDepTag { |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame^] | 617 | if defaults, ok := module.(DefaultsModule); ok { |
Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 618 | if !seen[defaults] { |
| 619 | seen[defaults] = true |
| 620 | defaultsList = append(defaultsList, defaults) |
| 621 | return len(defaults.defaults().Defaults) > 0 |
| 622 | } |
| 623 | } else { |
| 624 | ctx.PropertyErrorf("defaults", "module %s is not an defaults module", |
| 625 | ctx.OtherModuleName(module)) |
Colin Cross | a1ce2a0 | 2018-06-20 15:19:39 -0700 | [diff] [blame] | 626 | } |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 627 | } |
Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 628 | return false |
| 629 | }) |
| 630 | defaultable.applyDefaults(ctx, defaultsList) |
| 631 | } |
| 632 | |
| 633 | defaultable.callHookIfAvailable(ctx) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 634 | } |
| 635 | } |