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 ( |
| 18 | "github.com/google/blueprint" |
| 19 | "github.com/google/blueprint/proptools" |
| 20 | ) |
| 21 | |
Colin Cross | c99deeb | 2016-04-11 15:06:20 -0700 | [diff] [blame] | 22 | type defaultsDependencyTag struct { |
| 23 | blueprint.BaseDependencyTag |
| 24 | } |
| 25 | |
| 26 | var DefaultsDepTag defaultsDependencyTag |
| 27 | |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 28 | type defaultsProperties struct { |
| 29 | Defaults []string |
| 30 | } |
| 31 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 32 | type DefaultableModuleBase struct { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 33 | defaultsProperties defaultsProperties |
| 34 | defaultableProperties []interface{} |
| 35 | } |
| 36 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 37 | func (d *DefaultableModuleBase) defaults() *defaultsProperties { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 38 | return &d.defaultsProperties |
| 39 | } |
| 40 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 41 | func (d *DefaultableModuleBase) setProperties(props []interface{}) { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 42 | d.defaultableProperties = props |
| 43 | } |
| 44 | |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 45 | // 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] | 46 | type Defaultable interface { |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 47 | // Get a pointer to the struct containing the Defaults property. |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 48 | defaults() *defaultsProperties |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 49 | |
| 50 | // Set the property structures into which defaults will be added. |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 51 | setProperties([]interface{}) |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 52 | |
| 53 | // Apply defaults from the supplied Defaults to the property structures supplied to |
| 54 | // setProperties(...). |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 55 | applyDefaults(TopDownMutatorContext, []Defaults) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 58 | type DefaultableModule interface { |
| 59 | Module |
| 60 | Defaultable |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 63 | var _ Defaultable = (*DefaultableModuleBase)(nil) |
| 64 | |
| 65 | func InitDefaultableModule(module DefaultableModule) { |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 66 | module.setProperties(module.(Module).GetProperties()) |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 67 | |
| 68 | module.AddProperties(module.defaults()) |
| 69 | } |
| 70 | |
Paul Duffin | 95d53b5 | 2019-07-24 13:45:05 +0100 | [diff] [blame^] | 71 | // The Defaults_visibility property. |
| 72 | type DefaultsVisibilityProperties struct { |
| 73 | |
| 74 | // Controls the visibility of the defaults module itself. |
| 75 | Defaults_visibility []string |
| 76 | } |
| 77 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 78 | type DefaultsModuleBase struct { |
| 79 | DefaultableModuleBase |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 80 | |
| 81 | // Container for defaults of the common properties |
| 82 | commonProperties commonProperties |
Paul Duffin | 95d53b5 | 2019-07-24 13:45:05 +0100 | [diff] [blame^] | 83 | |
| 84 | defaultsVisibilityProperties DefaultsVisibilityProperties |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Martin Stjernholm | ebd757d | 2019-05-24 11:00:30 +0100 | [diff] [blame] | 87 | // The common pattern for defaults modules is to register separate instances of |
| 88 | // the xxxProperties structs in the AddProperties calls, rather than reusing the |
| 89 | // ones inherited from Module. |
| 90 | // |
| 91 | // The effect is that e.g. myDefaultsModuleInstance.base().xxxProperties won't |
| 92 | // contain the values that have been set for the defaults module. Rather, to |
| 93 | // retrieve the values it is necessary to iterate over properties(). E.g. to get |
| 94 | // the commonProperties instance that have the real values: |
| 95 | // |
| 96 | // d := myModule.(Defaults) |
| 97 | // for _, props := range d.properties() { |
| 98 | // if cp, ok := props.(*commonProperties); ok { |
| 99 | // ... access property values in cp ... |
| 100 | // } |
| 101 | // } |
| 102 | // |
| 103 | // The rationale is that the properties on a defaults module apply to the |
| 104 | // defaultable modules using it, not to the defaults module itself. E.g. setting |
| 105 | // the "enabled" property false makes inheriting modules disabled by default, |
| 106 | // rather than disabling the defaults module itself. |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 107 | type Defaults interface { |
Colin Cross | e7b0713 | 2016-07-27 10:15:06 -0700 | [diff] [blame] | 108 | Defaultable |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 109 | |
| 110 | // Although this function is unused it is actually needed to ensure that only modules that embed |
| 111 | // DefaultsModuleBase will type-assert to the Defaults interface. |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 112 | isDefaults() bool |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 113 | |
| 114 | // Get the structures containing the properties for which defaults can be provided. |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 115 | properties() []interface{} |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 116 | |
| 117 | // Return the defaults common properties. |
| 118 | common() *commonProperties |
Paul Duffin | 95d53b5 | 2019-07-24 13:45:05 +0100 | [diff] [blame^] | 119 | |
| 120 | // Return the defaults visibility properties. |
| 121 | defaultsVisibility() *DefaultsVisibilityProperties |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 124 | func (d *DefaultsModuleBase) isDefaults() bool { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 125 | return true |
| 126 | } |
| 127 | |
Paul Duffin | e62432f | 2019-07-24 12:51:21 +0100 | [diff] [blame] | 128 | type DefaultsModule interface { |
| 129 | Module |
| 130 | Defaults |
| 131 | } |
| 132 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 133 | func (d *DefaultsModuleBase) properties() []interface{} { |
Colin Cross | e7b0713 | 2016-07-27 10:15:06 -0700 | [diff] [blame] | 134 | return d.defaultableProperties |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 137 | func (d *DefaultsModuleBase) common() *commonProperties { |
| 138 | return &d.commonProperties |
| 139 | } |
| 140 | |
Paul Duffin | 95d53b5 | 2019-07-24 13:45:05 +0100 | [diff] [blame^] | 141 | func (d *DefaultsModuleBase) defaultsVisibility() *DefaultsVisibilityProperties { |
| 142 | return &d.defaultsVisibilityProperties |
| 143 | } |
| 144 | |
Colin Cross | 5903762 | 2019-06-10 13:12:56 -0700 | [diff] [blame] | 145 | func (d *DefaultsModuleBase) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 146 | } |
| 147 | |
Paul Duffin | e62432f | 2019-07-24 12:51:21 +0100 | [diff] [blame] | 148 | func InitDefaultsModule(module DefaultsModule) { |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 149 | commonProperties := module.common() |
| 150 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 151 | module.AddProperties( |
Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 152 | &hostAndDeviceProperties{}, |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 153 | commonProperties, |
Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 154 | &variableProperties{}) |
| 155 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 156 | InitArchModule(module) |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 157 | InitDefaultableModule(module) |
Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 158 | |
Paul Duffin | 7df7fb0 | 2019-07-24 12:26:14 +0100 | [diff] [blame] | 159 | // Add properties that will not have defaults applied to them. |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 160 | base := module.base() |
Paul Duffin | 95d53b5 | 2019-07-24 13:45:05 +0100 | [diff] [blame^] | 161 | defaultsVisibility := module.defaultsVisibility() |
| 162 | module.AddProperties(&base.nameProperties, defaultsVisibility) |
Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 163 | |
Paul Duffin | 95d53b5 | 2019-07-24 13:45:05 +0100 | [diff] [blame^] | 164 | // The defaults_visibility property controls the visibility of a defaults module. |
| 165 | base.primaryVisibilityProperty = |
| 166 | newVisibilityProperty("defaults_visibility", &defaultsVisibility.Defaults_visibility) |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 167 | |
| 168 | // Unlike non-defaults modules the visibility property is not stored in m.base().commonProperties. |
| 169 | // Instead it is stored in a separate instance of commonProperties created above so use that. |
| 170 | // The visibility property needs to be checked (but not parsed) by the visibility module during |
| 171 | // its checking phase and parsing phase. |
| 172 | base.visibilityPropertyInfo = []visibilityProperty{ |
Paul Duffin | 95d53b5 | 2019-07-24 13:45:05 +0100 | [diff] [blame^] | 173 | base.primaryVisibilityProperty, |
Paul Duffin | 63c6e18 | 2019-07-24 14:24:38 +0100 | [diff] [blame] | 174 | newVisibilityProperty("visibility", &commonProperties.Visibility), |
| 175 | } |
| 176 | |
| 177 | base.module = module |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 180 | var _ Defaults = (*DefaultsModuleBase)(nil) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 181 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 182 | func (defaultable *DefaultableModuleBase) applyDefaults(ctx TopDownMutatorContext, |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 183 | defaultsList []Defaults) { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 184 | |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 185 | for _, defaults := range defaultsList { |
| 186 | for _, prop := range defaultable.defaultableProperties { |
| 187 | for _, def := range defaults.properties() { |
| 188 | if proptools.TypeEqual(prop, def) { |
| 189 | err := proptools.PrependProperties(prop, def, nil) |
| 190 | if err != nil { |
| 191 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 192 | ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 193 | } else { |
| 194 | panic(err) |
| 195 | } |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 203 | func RegisterDefaultsPreArchMutators(ctx RegisterMutatorsContext) { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 204 | ctx.BottomUp("defaults_deps", defaultsDepsMutator).Parallel() |
| 205 | ctx.TopDown("defaults", defaultsMutator).Parallel() |
| 206 | } |
| 207 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 208 | func defaultsDepsMutator(ctx BottomUpMutatorContext) { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 209 | if defaultable, ok := ctx.Module().(Defaultable); ok { |
Colin Cross | c99deeb | 2016-04-11 15:06:20 -0700 | [diff] [blame] | 210 | ctx.AddDependency(ctx.Module(), DefaultsDepTag, defaultable.defaults().Defaults...) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 214 | func defaultsMutator(ctx TopDownMutatorContext) { |
| 215 | if defaultable, ok := ctx.Module().(Defaultable); ok && len(defaultable.defaults().Defaults) > 0 { |
| 216 | var defaultsList []Defaults |
Colin Cross | a1ce2a0 | 2018-06-20 15:19:39 -0700 | [diff] [blame] | 217 | seen := make(map[Defaults]bool) |
| 218 | |
Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 219 | ctx.WalkDeps(func(module, parent Module) bool { |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 220 | if ctx.OtherModuleDependencyTag(module) == DefaultsDepTag { |
| 221 | if defaults, ok := module.(Defaults); ok { |
Colin Cross | a1ce2a0 | 2018-06-20 15:19:39 -0700 | [diff] [blame] | 222 | if !seen[defaults] { |
| 223 | seen[defaults] = true |
| 224 | defaultsList = append(defaultsList, defaults) |
| 225 | return len(defaults.defaults().Defaults) > 0 |
| 226 | } |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 227 | } else { |
| 228 | ctx.PropertyErrorf("defaults", "module %s is not an defaults module", |
| 229 | ctx.OtherModuleName(module)) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 230 | } |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 231 | } |
| 232 | return false |
| 233 | }) |
| 234 | defaultable.applyDefaults(ctx, defaultsList) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 235 | } |
| 236 | } |