blob: 2cfd77aae4dfcea501707b86c9ec6a34d7dc2c1d [file] [log] [blame]
Colin Crosscfad1192015-11-02 16:43:11 -08001// 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 Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Crosscfad1192015-11-02 16:43:11 -080016
17import (
18 "github.com/google/blueprint"
19 "github.com/google/blueprint/proptools"
20)
21
Colin Crossc99deeb2016-04-11 15:06:20 -070022type defaultsDependencyTag struct {
23 blueprint.BaseDependencyTag
24}
25
26var DefaultsDepTag defaultsDependencyTag
27
Colin Crosscfad1192015-11-02 16:43:11 -080028type defaultsProperties struct {
29 Defaults []string
30}
31
Colin Cross1f44a3a2017-07-07 14:33:33 -070032type DefaultableModuleBase struct {
Colin Crosscfad1192015-11-02 16:43:11 -080033 defaultsProperties defaultsProperties
34 defaultableProperties []interface{}
35}
36
Colin Cross1f44a3a2017-07-07 14:33:33 -070037func (d *DefaultableModuleBase) defaults() *defaultsProperties {
Colin Crosscfad1192015-11-02 16:43:11 -080038 return &d.defaultsProperties
39}
40
Colin Cross1f44a3a2017-07-07 14:33:33 -070041func (d *DefaultableModuleBase) setProperties(props []interface{}) {
Colin Crosscfad1192015-11-02 16:43:11 -080042 d.defaultableProperties = props
43}
44
Paul Duffin7df7fb02019-07-24 12:26:14 +010045// Interface that must be supported by any module to which defaults can be applied.
Colin Crosscfad1192015-11-02 16:43:11 -080046type Defaultable interface {
Paul Duffin7df7fb02019-07-24 12:26:14 +010047 // Get a pointer to the struct containing the Defaults property.
Colin Crosscfad1192015-11-02 16:43:11 -080048 defaults() *defaultsProperties
Paul Duffin7df7fb02019-07-24 12:26:14 +010049
50 // Set the property structures into which defaults will be added.
Colin Crosscfad1192015-11-02 16:43:11 -080051 setProperties([]interface{})
Paul Duffin7df7fb02019-07-24 12:26:14 +010052
53 // Apply defaults from the supplied Defaults to the property structures supplied to
54 // setProperties(...).
Colin Cross13177012016-08-09 12:00:45 -070055 applyDefaults(TopDownMutatorContext, []Defaults)
Colin Crosscfad1192015-11-02 16:43:11 -080056}
57
Colin Cross1f44a3a2017-07-07 14:33:33 -070058type DefaultableModule interface {
59 Module
60 Defaultable
Colin Crosscfad1192015-11-02 16:43:11 -080061}
62
Colin Cross1f44a3a2017-07-07 14:33:33 -070063var _ Defaultable = (*DefaultableModuleBase)(nil)
64
65func InitDefaultableModule(module DefaultableModule) {
Paul Duffin7df7fb02019-07-24 12:26:14 +010066 module.setProperties(module.(Module).GetProperties())
Colin Cross1f44a3a2017-07-07 14:33:33 -070067
68 module.AddProperties(module.defaults())
69}
70
71type DefaultsModuleBase struct {
72 DefaultableModuleBase
Paul Duffin63c6e182019-07-24 14:24:38 +010073
74 // Container for defaults of the common properties
75 commonProperties commonProperties
Colin Crosscfad1192015-11-02 16:43:11 -080076}
77
Martin Stjernholmebd757d2019-05-24 11:00:30 +010078// The common pattern for defaults modules is to register separate instances of
79// the xxxProperties structs in the AddProperties calls, rather than reusing the
80// ones inherited from Module.
81//
82// The effect is that e.g. myDefaultsModuleInstance.base().xxxProperties won't
83// contain the values that have been set for the defaults module. Rather, to
84// retrieve the values it is necessary to iterate over properties(). E.g. to get
85// the commonProperties instance that have the real values:
86//
87// d := myModule.(Defaults)
88// for _, props := range d.properties() {
89// if cp, ok := props.(*commonProperties); ok {
90// ... access property values in cp ...
91// }
92// }
93//
94// The rationale is that the properties on a defaults module apply to the
95// defaultable modules using it, not to the defaults module itself. E.g. setting
96// the "enabled" property false makes inheriting modules disabled by default,
97// rather than disabling the defaults module itself.
Colin Crosscfad1192015-11-02 16:43:11 -080098type Defaults interface {
Colin Crosse7b07132016-07-27 10:15:06 -070099 Defaultable
Paul Duffin7df7fb02019-07-24 12:26:14 +0100100
101 // Although this function is unused it is actually needed to ensure that only modules that embed
102 // DefaultsModuleBase will type-assert to the Defaults interface.
Colin Crosscfad1192015-11-02 16:43:11 -0800103 isDefaults() bool
Paul Duffin7df7fb02019-07-24 12:26:14 +0100104
105 // Get the structures containing the properties for which defaults can be provided.
Colin Crosscfad1192015-11-02 16:43:11 -0800106 properties() []interface{}
Paul Duffin63c6e182019-07-24 14:24:38 +0100107
108 // Return the defaults common properties.
109 common() *commonProperties
Colin Crosscfad1192015-11-02 16:43:11 -0800110}
111
Colin Cross1f44a3a2017-07-07 14:33:33 -0700112func (d *DefaultsModuleBase) isDefaults() bool {
Colin Crosscfad1192015-11-02 16:43:11 -0800113 return true
114}
115
Paul Duffine62432f2019-07-24 12:51:21 +0100116type DefaultsModule interface {
117 Module
118 Defaults
119}
120
Colin Cross1f44a3a2017-07-07 14:33:33 -0700121func (d *DefaultsModuleBase) properties() []interface{} {
Colin Crosse7b07132016-07-27 10:15:06 -0700122 return d.defaultableProperties
Colin Crosscfad1192015-11-02 16:43:11 -0800123}
124
Paul Duffin63c6e182019-07-24 14:24:38 +0100125func (d *DefaultsModuleBase) common() *commonProperties {
126 return &d.commonProperties
127}
128
Colin Cross59037622019-06-10 13:12:56 -0700129func (d *DefaultsModuleBase) GenerateAndroidBuildActions(ctx ModuleContext) {
130}
131
Paul Duffine62432f2019-07-24 12:51:21 +0100132func InitDefaultsModule(module DefaultsModule) {
Paul Duffin63c6e182019-07-24 14:24:38 +0100133 commonProperties := module.common()
134
Colin Cross36242852017-06-23 15:06:31 -0700135 module.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700136 &hostAndDeviceProperties{},
Paul Duffin63c6e182019-07-24 14:24:38 +0100137 commonProperties,
Colin Crossfc754582016-05-17 16:34:16 -0700138 &variableProperties{})
139
Colin Cross36242852017-06-23 15:06:31 -0700140 InitArchModule(module)
Colin Cross1f44a3a2017-07-07 14:33:33 -0700141 InitDefaultableModule(module)
Colin Crossfc754582016-05-17 16:34:16 -0700142
Paul Duffin7df7fb02019-07-24 12:26:14 +0100143 // Add properties that will not have defaults applied to them.
Paul Duffin63c6e182019-07-24 14:24:38 +0100144 base := module.base()
145 module.AddProperties(&base.nameProperties)
Colin Crossfc754582016-05-17 16:34:16 -0700146
Paul Duffin63c6e182019-07-24 14:24:38 +0100147 // There is currently no way to control the visibility of a defaults module, i.e. there is no
148 // primary visibility property.
149 base.primaryVisibilityProperty = nil
150
151 // Unlike non-defaults modules the visibility property is not stored in m.base().commonProperties.
152 // Instead it is stored in a separate instance of commonProperties created above so use that.
153 // The visibility property needs to be checked (but not parsed) by the visibility module during
154 // its checking phase and parsing phase.
155 base.visibilityPropertyInfo = []visibilityProperty{
156 newVisibilityProperty("visibility", &commonProperties.Visibility),
157 }
158
159 base.module = module
Colin Crosscfad1192015-11-02 16:43:11 -0800160}
161
Colin Cross1f44a3a2017-07-07 14:33:33 -0700162var _ Defaults = (*DefaultsModuleBase)(nil)
Colin Crosscfad1192015-11-02 16:43:11 -0800163
Colin Cross1f44a3a2017-07-07 14:33:33 -0700164func (defaultable *DefaultableModuleBase) applyDefaults(ctx TopDownMutatorContext,
Colin Cross13177012016-08-09 12:00:45 -0700165 defaultsList []Defaults) {
Colin Crosscfad1192015-11-02 16:43:11 -0800166
Colin Cross13177012016-08-09 12:00:45 -0700167 for _, defaults := range defaultsList {
168 for _, prop := range defaultable.defaultableProperties {
169 for _, def := range defaults.properties() {
170 if proptools.TypeEqual(prop, def) {
171 err := proptools.PrependProperties(prop, def, nil)
172 if err != nil {
173 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
174 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
175 } else {
176 panic(err)
177 }
Colin Crosscfad1192015-11-02 16:43:11 -0800178 }
179 }
180 }
181 }
182 }
183}
184
Colin Cross89536d42017-07-07 14:35:50 -0700185func RegisterDefaultsPreArchMutators(ctx RegisterMutatorsContext) {
Colin Crosscec81712017-07-13 14:43:27 -0700186 ctx.BottomUp("defaults_deps", defaultsDepsMutator).Parallel()
187 ctx.TopDown("defaults", defaultsMutator).Parallel()
188}
189
Colin Cross635c3b02016-05-18 15:37:25 -0700190func defaultsDepsMutator(ctx BottomUpMutatorContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800191 if defaultable, ok := ctx.Module().(Defaultable); ok {
Colin Crossc99deeb2016-04-11 15:06:20 -0700192 ctx.AddDependency(ctx.Module(), DefaultsDepTag, defaultable.defaults().Defaults...)
Colin Crosscfad1192015-11-02 16:43:11 -0800193 }
194}
195
Colin Cross13177012016-08-09 12:00:45 -0700196func defaultsMutator(ctx TopDownMutatorContext) {
197 if defaultable, ok := ctx.Module().(Defaultable); ok && len(defaultable.defaults().Defaults) > 0 {
198 var defaultsList []Defaults
Colin Crossa1ce2a02018-06-20 15:19:39 -0700199 seen := make(map[Defaults]bool)
200
Colin Crossd11fcda2017-10-23 17:59:01 -0700201 ctx.WalkDeps(func(module, parent Module) bool {
Colin Cross13177012016-08-09 12:00:45 -0700202 if ctx.OtherModuleDependencyTag(module) == DefaultsDepTag {
203 if defaults, ok := module.(Defaults); ok {
Colin Crossa1ce2a02018-06-20 15:19:39 -0700204 if !seen[defaults] {
205 seen[defaults] = true
206 defaultsList = append(defaultsList, defaults)
207 return len(defaults.defaults().Defaults) > 0
208 }
Colin Cross13177012016-08-09 12:00:45 -0700209 } else {
210 ctx.PropertyErrorf("defaults", "module %s is not an defaults module",
211 ctx.OtherModuleName(module))
Colin Crosscfad1192015-11-02 16:43:11 -0800212 }
Colin Cross13177012016-08-09 12:00:45 -0700213 }
214 return false
215 })
216 defaultable.applyDefaults(ctx, defaultsList)
Colin Crosscfad1192015-11-02 16:43:11 -0800217 }
218}