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 | |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 20 | "github.com/google/blueprint" |
| 21 | "github.com/google/blueprint/proptools" |
| 22 | ) |
| 23 | |
Colin Cross | c99deeb | 2016-04-11 15:06:20 -0700 | [diff] [blame] | 24 | type defaultsDependencyTag struct { |
| 25 | blueprint.BaseDependencyTag |
| 26 | } |
| 27 | |
| 28 | var DefaultsDepTag defaultsDependencyTag |
| 29 | |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 30 | type defaultsProperties struct { |
| 31 | Defaults []string |
| 32 | } |
| 33 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 34 | type DefaultableModuleBase struct { |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 35 | defaultsProperties defaultsProperties |
| 36 | defaultableProperties []interface{} |
| 37 | defaultableVariableProperties interface{} |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 40 | func (d *DefaultableModuleBase) defaults() *defaultsProperties { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 41 | return &d.defaultsProperties |
| 42 | } |
| 43 | |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 44 | func (d *DefaultableModuleBase) setProperties(props []interface{}, variableProperties interface{}) { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 45 | d.defaultableProperties = props |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 46 | d.defaultableVariableProperties = variableProperties |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 49 | // 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] | 50 | type Defaultable interface { |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 51 | // Get a pointer to the struct containing the Defaults property. |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 52 | defaults() *defaultsProperties |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 53 | |
| 54 | // Set the property structures into which defaults will be added. |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 55 | setProperties(props []interface{}, variableProperties interface{}) |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 56 | |
| 57 | // Apply defaults from the supplied Defaults to the property structures supplied to |
| 58 | // setProperties(...). |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 59 | applyDefaults(TopDownMutatorContext, []Defaults) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 62 | type DefaultableModule interface { |
| 63 | Module |
| 64 | Defaultable |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 67 | var _ Defaultable = (*DefaultableModuleBase)(nil) |
| 68 | |
| 69 | func InitDefaultableModule(module DefaultableModule) { |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 70 | if module.(Module).base().module == nil { |
| 71 | panic("InitAndroidModule must be called before InitDefaultableModule") |
| 72 | } |
| 73 | module.setProperties(module.(Module).GetProperties(), module.(Module).base().variableProperties) |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 74 | |
| 75 | module.AddProperties(module.defaults()) |
| 76 | } |
| 77 | |
Paul Duffin | 95d53b5 | 2019-07-24 13:45:05 +0100 | [diff] [blame] | 78 | // The Defaults_visibility property. |
| 79 | type DefaultsVisibilityProperties struct { |
| 80 | |
| 81 | // Controls the visibility of the defaults module itself. |
| 82 | Defaults_visibility []string |
| 83 | } |
| 84 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 85 | type DefaultsModuleBase struct { |
| 86 | DefaultableModuleBase |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 87 | |
| 88 | // Container for defaults of the common properties |
| 89 | commonProperties commonProperties |
Paul Duffin | 95d53b5 | 2019-07-24 13:45:05 +0100 | [diff] [blame] | 90 | |
| 91 | defaultsVisibilityProperties DefaultsVisibilityProperties |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Martin Stjernholm | ebd757d | 2019-05-24 11:00:30 +0100 | [diff] [blame] | 94 | // The common pattern for defaults modules is to register separate instances of |
| 95 | // the xxxProperties structs in the AddProperties calls, rather than reusing the |
| 96 | // ones inherited from Module. |
| 97 | // |
| 98 | // The effect is that e.g. myDefaultsModuleInstance.base().xxxProperties won't |
| 99 | // contain the values that have been set for the defaults module. Rather, to |
| 100 | // retrieve the values it is necessary to iterate over properties(). E.g. to get |
| 101 | // the commonProperties instance that have the real values: |
| 102 | // |
| 103 | // d := myModule.(Defaults) |
| 104 | // for _, props := range d.properties() { |
| 105 | // if cp, ok := props.(*commonProperties); ok { |
| 106 | // ... access property values in cp ... |
| 107 | // } |
| 108 | // } |
| 109 | // |
| 110 | // The rationale is that the properties on a defaults module apply to the |
| 111 | // defaultable modules using it, not to the defaults module itself. E.g. setting |
| 112 | // the "enabled" property false makes inheriting modules disabled by default, |
| 113 | // rather than disabling the defaults module itself. |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 114 | type Defaults interface { |
Colin Cross | e7b0713 | 2016-07-27 10:15:06 -0700 | [diff] [blame] | 115 | Defaultable |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 116 | |
| 117 | // Although this function is unused it is actually needed to ensure that only modules that embed |
| 118 | // DefaultsModuleBase will type-assert to the Defaults interface. |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 119 | isDefaults() bool |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 120 | |
| 121 | // Get the structures containing the properties for which defaults can be provided. |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 122 | properties() []interface{} |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 123 | |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 124 | productVariableProperties() interface{} |
| 125 | |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 126 | // Return the defaults common properties. |
| 127 | common() *commonProperties |
Paul Duffin | 95d53b5 | 2019-07-24 13:45:05 +0100 | [diff] [blame] | 128 | |
| 129 | // Return the defaults visibility properties. |
| 130 | defaultsVisibility() *DefaultsVisibilityProperties |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 133 | func (d *DefaultsModuleBase) isDefaults() bool { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 134 | return true |
| 135 | } |
| 136 | |
Paul Duffin | e62432f | 2019-07-24 12:51:21 +0100 | [diff] [blame] | 137 | type DefaultsModule interface { |
| 138 | Module |
| 139 | Defaults |
| 140 | } |
| 141 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 142 | func (d *DefaultsModuleBase) properties() []interface{} { |
Colin Cross | e7b0713 | 2016-07-27 10:15:06 -0700 | [diff] [blame] | 143 | return d.defaultableProperties |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 146 | func (d *DefaultsModuleBase) productVariableProperties() interface{} { |
| 147 | return d.defaultableVariableProperties |
| 148 | } |
| 149 | |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 150 | func (d *DefaultsModuleBase) common() *commonProperties { |
| 151 | return &d.commonProperties |
| 152 | } |
| 153 | |
Paul Duffin | 95d53b5 | 2019-07-24 13:45:05 +0100 | [diff] [blame] | 154 | func (d *DefaultsModuleBase) defaultsVisibility() *DefaultsVisibilityProperties { |
| 155 | return &d.defaultsVisibilityProperties |
| 156 | } |
| 157 | |
Colin Cross | 5903762 | 2019-06-10 13:12:56 -0700 | [diff] [blame] | 158 | func (d *DefaultsModuleBase) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 159 | } |
| 160 | |
Paul Duffin | e62432f | 2019-07-24 12:51:21 +0100 | [diff] [blame] | 161 | func InitDefaultsModule(module DefaultsModule) { |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 162 | commonProperties := module.common() |
| 163 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 164 | module.AddProperties( |
Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 165 | &hostAndDeviceProperties{}, |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 166 | commonProperties, |
Jiyong Park | 3814f4d | 2019-12-02 13:08:53 +0900 | [diff] [blame] | 167 | &ApexProperties{}) |
Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 168 | |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 169 | initAndroidModuleBase(module) |
| 170 | initProductVariableModule(module) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 171 | InitArchModule(module) |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 172 | InitDefaultableModule(module) |
Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 173 | |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 174 | // Add properties that will not have defaults applied to them. |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 175 | base := module.base() |
Paul Duffin | 95d53b5 | 2019-07-24 13:45:05 +0100 | [diff] [blame] | 176 | defaultsVisibility := module.defaultsVisibility() |
| 177 | module.AddProperties(&base.nameProperties, defaultsVisibility) |
Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 178 | |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 179 | // 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] | 180 | // Instead it is stored in a separate instance of commonProperties created above so clear the |
| 181 | // existing list of properties. |
| 182 | clearVisibilityProperties(module) |
| 183 | |
| 184 | // The defaults_visibility property controls the visibility of a defaults module so it must be |
| 185 | // set as the primary property, which also adds it to the list. |
| 186 | setPrimaryVisibilityProperty(module, "defaults_visibility", &defaultsVisibility.Defaults_visibility) |
| 187 | |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 188 | // 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] | 189 | // its checking phase and parsing phase so add it to the list as a normal property. |
| 190 | AddVisibilityProperty(module, "visibility", &commonProperties.Visibility) |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 191 | |
| 192 | base.module = module |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 195 | var _ Defaults = (*DefaultsModuleBase)(nil) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 196 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 197 | func (defaultable *DefaultableModuleBase) applyDefaults(ctx TopDownMutatorContext, |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 198 | defaultsList []Defaults) { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 199 | |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 200 | for _, defaults := range defaultsList { |
| 201 | for _, prop := range defaultable.defaultableProperties { |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 202 | if prop == defaultable.defaultableVariableProperties { |
| 203 | defaultable.applyDefaultVariableProperties(ctx, defaults, prop) |
| 204 | } else { |
| 205 | defaultable.applyDefaultProperties(ctx, defaults, prop) |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // Product variable properties need special handling, the type of the filtered product variable |
| 212 | // property struct may not be identical between the defaults module and the defaultable module. |
| 213 | // Use PrependMatchingProperties to apply whichever properties match. |
| 214 | func (defaultable *DefaultableModuleBase) applyDefaultVariableProperties(ctx TopDownMutatorContext, |
| 215 | defaults Defaults, defaultableProp interface{}) { |
| 216 | if defaultableProp == nil { |
| 217 | return |
| 218 | } |
| 219 | |
| 220 | defaultsProp := defaults.productVariableProperties() |
| 221 | if defaultsProp == nil { |
| 222 | return |
| 223 | } |
| 224 | |
| 225 | dst := []interface{}{ |
| 226 | defaultableProp, |
| 227 | // Put an empty copy of the src properties into dst so that properties in src that are not in dst |
| 228 | // don't cause a "failed to find property to extend" error. |
| 229 | proptools.CloneEmptyProperties(reflect.ValueOf(defaultsProp)).Interface(), |
| 230 | } |
| 231 | |
| 232 | err := proptools.PrependMatchingProperties(dst, defaultsProp, nil) |
| 233 | if err != nil { |
| 234 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 235 | ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 236 | } else { |
| 237 | panic(err) |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | func (defaultable *DefaultableModuleBase) applyDefaultProperties(ctx TopDownMutatorContext, |
| 243 | defaults Defaults, defaultableProp interface{}) { |
| 244 | |
| 245 | for _, def := range defaults.properties() { |
| 246 | if proptools.TypeEqual(defaultableProp, def) { |
| 247 | err := proptools.PrependProperties(defaultableProp, def, nil) |
| 248 | if err != nil { |
| 249 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 250 | ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 251 | } else { |
| 252 | panic(err) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 259 | func RegisterDefaultsPreArchMutators(ctx RegisterMutatorsContext) { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 260 | ctx.BottomUp("defaults_deps", defaultsDepsMutator).Parallel() |
| 261 | ctx.TopDown("defaults", defaultsMutator).Parallel() |
| 262 | } |
| 263 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 264 | func defaultsDepsMutator(ctx BottomUpMutatorContext) { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 265 | if defaultable, ok := ctx.Module().(Defaultable); ok { |
Colin Cross | c99deeb | 2016-04-11 15:06:20 -0700 | [diff] [blame] | 266 | ctx.AddDependency(ctx.Module(), DefaultsDepTag, defaultable.defaults().Defaults...) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 270 | func defaultsMutator(ctx TopDownMutatorContext) { |
| 271 | if defaultable, ok := ctx.Module().(Defaultable); ok && len(defaultable.defaults().Defaults) > 0 { |
| 272 | var defaultsList []Defaults |
Colin Cross | a1ce2a0 | 2018-06-20 15:19:39 -0700 | [diff] [blame] | 273 | seen := make(map[Defaults]bool) |
| 274 | |
Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 275 | ctx.WalkDeps(func(module, parent Module) bool { |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 276 | if ctx.OtherModuleDependencyTag(module) == DefaultsDepTag { |
| 277 | if defaults, ok := module.(Defaults); ok { |
Colin Cross | a1ce2a0 | 2018-06-20 15:19:39 -0700 | [diff] [blame] | 278 | if !seen[defaults] { |
| 279 | seen[defaults] = true |
| 280 | defaultsList = append(defaultsList, defaults) |
| 281 | return len(defaults.defaults().Defaults) > 0 |
| 282 | } |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 283 | } else { |
| 284 | ctx.PropertyErrorf("defaults", "module %s is not an defaults module", |
| 285 | ctx.OtherModuleName(module)) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 286 | } |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 287 | } |
| 288 | return false |
| 289 | }) |
| 290 | defaultable.applyDefaults(ctx, defaultsList) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 291 | } |
| 292 | } |