blob: c0a2fc68f105b37ee5394dab884a7acaf326f3e4 [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 (
Colin Crosseabaedd2020-02-06 17:01:55 -080018 "reflect"
19
Colin Crosscfad1192015-11-02 16:43:11 -080020 "github.com/google/blueprint"
21 "github.com/google/blueprint/proptools"
22)
23
Colin Crossc99deeb2016-04-11 15:06:20 -070024type defaultsDependencyTag struct {
25 blueprint.BaseDependencyTag
26}
27
28var DefaultsDepTag defaultsDependencyTag
29
Colin Crosscfad1192015-11-02 16:43:11 -080030type defaultsProperties struct {
Inseob Kimf11bb2d2024-07-23 13:29:48 +090031 Defaults proptools.Configurable[[]string]
Colin Crosscfad1192015-11-02 16:43:11 -080032}
33
Colin Cross1f44a3a2017-07-07 14:33:33 -070034type DefaultableModuleBase struct {
Colin Crosseabaedd2020-02-06 17:01:55 -080035 defaultsProperties defaultsProperties
36 defaultableProperties []interface{}
37 defaultableVariableProperties interface{}
Paul Duffinafa9fa12020-04-29 16:47:28 +010038
39 // The optional hook to call after any defaults have been applied.
40 hook DefaultableHook
Colin Crosscfad1192015-11-02 16:43:11 -080041}
42
Colin Cross1f44a3a2017-07-07 14:33:33 -070043func (d *DefaultableModuleBase) defaults() *defaultsProperties {
Colin Crosscfad1192015-11-02 16:43:11 -080044 return &d.defaultsProperties
45}
46
Colin Crosseabaedd2020-02-06 17:01:55 -080047func (d *DefaultableModuleBase) setProperties(props []interface{}, variableProperties interface{}) {
Colin Crosscfad1192015-11-02 16:43:11 -080048 d.defaultableProperties = props
Colin Crosseabaedd2020-02-06 17:01:55 -080049 d.defaultableVariableProperties = variableProperties
Colin Crosscfad1192015-11-02 16:43:11 -080050}
51
Paul Duffinafa9fa12020-04-29 16:47:28 +010052func (d *DefaultableModuleBase) SetDefaultableHook(hook DefaultableHook) {
53 d.hook = hook
54}
55
Jihoon Kangd48abd52023-02-02 22:32:31 +000056func (d *DefaultableModuleBase) CallHookIfAvailable(ctx DefaultableHookContext) {
Paul Duffinafa9fa12020-04-29 16:47:28 +010057 if d.hook != nil {
58 d.hook(ctx)
59 }
60}
61
Paul Duffin7df7fb02019-07-24 12:26:14 +010062// Interface that must be supported by any module to which defaults can be applied.
Colin Crosscfad1192015-11-02 16:43:11 -080063type Defaultable interface {
Paul Duffin7df7fb02019-07-24 12:26:14 +010064 // Get a pointer to the struct containing the Defaults property.
Colin Crosscfad1192015-11-02 16:43:11 -080065 defaults() *defaultsProperties
Paul Duffin7df7fb02019-07-24 12:26:14 +010066
67 // Set the property structures into which defaults will be added.
Colin Crosseabaedd2020-02-06 17:01:55 -080068 setProperties(props []interface{}, variableProperties interface{})
Paul Duffin7df7fb02019-07-24 12:26:14 +010069
Liz Kammereeeb9522023-03-10 18:53:41 -050070 // Apply defaults from the supplied Defaults to the property structures supplied to
Paul Duffin7df7fb02019-07-24 12:26:14 +010071 // setProperties(...).
Liz Kammereeeb9522023-03-10 18:53:41 -050072 applyDefaults(TopDownMutatorContext, []Defaults)
Paul Duffinafa9fa12020-04-29 16:47:28 +010073
74 // Set the hook to be called after any defaults have been applied.
75 //
76 // Should be used in preference to a AddLoadHook when the behavior of the load
77 // hook is dependent on properties supplied in the Android.bp file.
78 SetDefaultableHook(hook DefaultableHook)
79
80 // Call the hook if specified.
Jihoon Kangd48abd52023-02-02 22:32:31 +000081 CallHookIfAvailable(context DefaultableHookContext)
Colin Crosscfad1192015-11-02 16:43:11 -080082}
83
Colin Cross1f44a3a2017-07-07 14:33:33 -070084type DefaultableModule interface {
85 Module
86 Defaultable
Colin Crosscfad1192015-11-02 16:43:11 -080087}
88
Colin Cross1f44a3a2017-07-07 14:33:33 -070089var _ Defaultable = (*DefaultableModuleBase)(nil)
90
91func InitDefaultableModule(module DefaultableModule) {
Ustafe201fe2021-12-06 15:13:36 -050092 if module.base().module == nil {
Colin Crosseabaedd2020-02-06 17:01:55 -080093 panic("InitAndroidModule must be called before InitDefaultableModule")
94 }
Liz Kammer416201d2021-12-15 13:18:42 -050095
Ustafe201fe2021-12-06 15:13:36 -050096 module.setProperties(module.GetProperties(), module.base().variableProperties)
Colin Cross1f44a3a2017-07-07 14:33:33 -070097
98 module.AddProperties(module.defaults())
99}
100
Paul Duffinafa9fa12020-04-29 16:47:28 +0100101// A restricted subset of context methods, similar to LoadHookContext.
102type DefaultableHookContext interface {
103 EarlyModuleContext
104
105 CreateModule(ModuleFactory, ...interface{}) Module
Colin Cross18f840c2021-05-20 17:56:54 -0700106 AddMissingDependencies(missingDeps []string)
Paul Duffinafa9fa12020-04-29 16:47:28 +0100107}
108
109type DefaultableHook func(ctx DefaultableHookContext)
110
Paul Duffin95d53b52019-07-24 13:45:05 +0100111// The Defaults_visibility property.
112type DefaultsVisibilityProperties struct {
113
114 // Controls the visibility of the defaults module itself.
115 Defaults_visibility []string
116}
117
Colin Cross1f44a3a2017-07-07 14:33:33 -0700118type DefaultsModuleBase struct {
119 DefaultableModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -0800120}
121
Martin Stjernholmebd757d2019-05-24 11:00:30 +0100122// The common pattern for defaults modules is to register separate instances of
123// the xxxProperties structs in the AddProperties calls, rather than reusing the
124// ones inherited from Module.
125//
126// The effect is that e.g. myDefaultsModuleInstance.base().xxxProperties won't
127// contain the values that have been set for the defaults module. Rather, to
128// retrieve the values it is necessary to iterate over properties(). E.g. to get
129// the commonProperties instance that have the real values:
130//
Colin Crossd079e0b2022-08-16 10:27:33 -0700131// d := myModule.(Defaults)
132// for _, props := range d.properties() {
133// if cp, ok := props.(*commonProperties); ok {
134// ... access property values in cp ...
135// }
136// }
Martin Stjernholmebd757d2019-05-24 11:00:30 +0100137//
138// The rationale is that the properties on a defaults module apply to the
139// defaultable modules using it, not to the defaults module itself. E.g. setting
140// the "enabled" property false makes inheriting modules disabled by default,
141// rather than disabling the defaults module itself.
Colin Crosscfad1192015-11-02 16:43:11 -0800142type Defaults interface {
Colin Crosse7b07132016-07-27 10:15:06 -0700143 Defaultable
Paul Duffin7df7fb02019-07-24 12:26:14 +0100144
145 // Although this function is unused it is actually needed to ensure that only modules that embed
146 // DefaultsModuleBase will type-assert to the Defaults interface.
Colin Crosscfad1192015-11-02 16:43:11 -0800147 isDefaults() bool
Paul Duffin7df7fb02019-07-24 12:26:14 +0100148
149 // Get the structures containing the properties for which defaults can be provided.
Colin Crosscfad1192015-11-02 16:43:11 -0800150 properties() []interface{}
Paul Duffin63c6e182019-07-24 14:24:38 +0100151
Colin Crosseabaedd2020-02-06 17:01:55 -0800152 productVariableProperties() interface{}
Colin Crosscfad1192015-11-02 16:43:11 -0800153}
154
Colin Cross1f44a3a2017-07-07 14:33:33 -0700155func (d *DefaultsModuleBase) isDefaults() bool {
Colin Crosscfad1192015-11-02 16:43:11 -0800156 return true
157}
158
Paul Duffine62432f2019-07-24 12:51:21 +0100159type DefaultsModule interface {
160 Module
161 Defaults
162}
163
Colin Cross1f44a3a2017-07-07 14:33:33 -0700164func (d *DefaultsModuleBase) properties() []interface{} {
Colin Crosse7b07132016-07-27 10:15:06 -0700165 return d.defaultableProperties
Colin Crosscfad1192015-11-02 16:43:11 -0800166}
167
Colin Crosseabaedd2020-02-06 17:01:55 -0800168func (d *DefaultsModuleBase) productVariableProperties() interface{} {
169 return d.defaultableVariableProperties
170}
171
Liz Kammer416201d2021-12-15 13:18:42 -0500172func (d *DefaultsModuleBase) GenerateAndroidBuildActions(ctx ModuleContext) {}
Colin Cross59037622019-06-10 13:12:56 -0700173
Paul Duffine62432f2019-07-24 12:51:21 +0100174func InitDefaultsModule(module DefaultsModule) {
Paul Duffin3f98d142020-09-02 13:28:25 +0100175 commonProperties := &commonProperties{}
Paul Duffin63c6e182019-07-24 14:24:38 +0100176
Colin Cross36242852017-06-23 15:06:31 -0700177 module.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700178 &hostAndDeviceProperties{},
Paul Duffin63c6e182019-07-24 14:24:38 +0100179 commonProperties,
Paul Duffined875132020-09-02 13:08:57 +0100180 &ApexProperties{},
181 &distProperties{})
Colin Crossfc754582016-05-17 16:34:16 -0700182
Colin Crosseabaedd2020-02-06 17:01:55 -0800183 initAndroidModuleBase(module)
184 initProductVariableModule(module)
Colin Crossa6845402020-11-16 15:08:19 -0800185 initArchModule(module)
Colin Cross1f44a3a2017-07-07 14:33:33 -0700186 InitDefaultableModule(module)
Colin Crossfc754582016-05-17 16:34:16 -0700187
Paul Duffin7df7fb02019-07-24 12:26:14 +0100188 // Add properties that will not have defaults applied to them.
Colin Cross08d6f8f2020-11-19 02:33:19 +0000189 base := module.base()
Paul Duffin3f98d142020-09-02 13:28:25 +0100190 defaultsVisibility := &DefaultsVisibilityProperties{}
Paul Duffin95d53b52019-07-24 13:45:05 +0100191 module.AddProperties(&base.nameProperties, defaultsVisibility)
Colin Crossfc754582016-05-17 16:34:16 -0700192
Paul Duffin63c6e182019-07-24 14:24:38 +0100193 // Unlike non-defaults modules the visibility property is not stored in m.base().commonProperties.
Paul Duffin5ec73ec2020-05-01 17:52:01 +0100194 // Instead it is stored in a separate instance of commonProperties created above so clear the
195 // existing list of properties.
196 clearVisibilityProperties(module)
197
198 // The defaults_visibility property controls the visibility of a defaults module so it must be
199 // set as the primary property, which also adds it to the list.
200 setPrimaryVisibilityProperty(module, "defaults_visibility", &defaultsVisibility.Defaults_visibility)
201
Paul Duffin63c6e182019-07-24 14:24:38 +0100202 // The visibility property needs to be checked (but not parsed) by the visibility module during
Paul Duffin5ec73ec2020-05-01 17:52:01 +0100203 // its checking phase and parsing phase so add it to the list as a normal property.
204 AddVisibilityProperty(module, "visibility", &commonProperties.Visibility)
Paul Duffin63c6e182019-07-24 14:24:38 +0100205
Bob Badour37af0462021-01-07 03:34:31 +0000206 // The applicable licenses property for defaults is 'licenses'.
207 setPrimaryLicensesProperty(module, "licenses", &commonProperties.Licenses)
Colin Crosscfad1192015-11-02 16:43:11 -0800208}
209
Colin Cross1f44a3a2017-07-07 14:33:33 -0700210var _ Defaults = (*DefaultsModuleBase)(nil)
Colin Crosscfad1192015-11-02 16:43:11 -0800211
Colin Cross1f44a3a2017-07-07 14:33:33 -0700212func (defaultable *DefaultableModuleBase) applyDefaults(ctx TopDownMutatorContext,
Liz Kammereeeb9522023-03-10 18:53:41 -0500213 defaultsList []Defaults) {
Colin Crosscfad1192015-11-02 16:43:11 -0800214
Colin Cross13177012016-08-09 12:00:45 -0700215 for _, defaults := range defaultsList {
Liz Kammereeeb9522023-03-10 18:53:41 -0500216 for _, prop := range defaultable.defaultableProperties {
217 if prop == defaultable.defaultableVariableProperties {
218 defaultable.applyDefaultVariableProperties(ctx, defaults, prop)
Paul Duffin79996272022-05-04 11:39:52 +0000219 } else {
Liz Kammereeeb9522023-03-10 18:53:41 -0500220 defaultable.applyDefaultProperties(ctx, defaults, prop)
Paul Duffin79996272022-05-04 11:39:52 +0000221 }
222 }
223 }
224}
225
Colin Crosseabaedd2020-02-06 17:01:55 -0800226// Product variable properties need special handling, the type of the filtered product variable
227// property struct may not be identical between the defaults module and the defaultable module.
228// Use PrependMatchingProperties to apply whichever properties match.
Liz Kammereeeb9522023-03-10 18:53:41 -0500229func (defaultable *DefaultableModuleBase) applyDefaultVariableProperties(ctx TopDownMutatorContext,
230 defaults Defaults, defaultableProp interface{}) {
Colin Crosseabaedd2020-02-06 17:01:55 -0800231 if defaultableProp == nil {
Liz Kammereeeb9522023-03-10 18:53:41 -0500232 return
Colin Crosseabaedd2020-02-06 17:01:55 -0800233 }
234
235 defaultsProp := defaults.productVariableProperties()
236 if defaultsProp == nil {
Liz Kammereeeb9522023-03-10 18:53:41 -0500237 return
Colin Crosseabaedd2020-02-06 17:01:55 -0800238 }
239
240 dst := []interface{}{
241 defaultableProp,
242 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
243 // don't cause a "failed to find property to extend" error.
244 proptools.CloneEmptyProperties(reflect.ValueOf(defaultsProp)).Interface(),
245 }
246
Liz Kammereeeb9522023-03-10 18:53:41 -0500247 err := proptools.PrependMatchingProperties(dst, defaultsProp, 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)
253 }
254 }
Colin Crosseabaedd2020-02-06 17:01:55 -0800255}
256
Liz Kammereeeb9522023-03-10 18:53:41 -0500257func (defaultable *DefaultableModuleBase) applyDefaultProperties(ctx TopDownMutatorContext,
258 defaults Defaults, defaultableProp interface{}) {
Colin Crosseabaedd2020-02-06 17:01:55 -0800259
260 for _, def := range defaults.properties() {
261 if proptools.TypeEqual(defaultableProp, def) {
Liz Kammereeeb9522023-03-10 18:53:41 -0500262 err := proptools.PrependProperties(defaultableProp, def, nil)
Colin Crosseabaedd2020-02-06 17:01:55 -0800263 if err != nil {
Liz Kammereeeb9522023-03-10 18:53:41 -0500264 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
265 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
266 } else {
267 panic(err)
268 }
Colin Crosscfad1192015-11-02 16:43:11 -0800269 }
270 }
271 }
272}
273
Colin Cross89536d42017-07-07 14:35:50 -0700274func RegisterDefaultsPreArchMutators(ctx RegisterMutatorsContext) {
Colin Crosscec81712017-07-13 14:43:27 -0700275 ctx.BottomUp("defaults_deps", defaultsDepsMutator).Parallel()
276 ctx.TopDown("defaults", defaultsMutator).Parallel()
277}
278
Colin Cross635c3b02016-05-18 15:37:25 -0700279func defaultsDepsMutator(ctx BottomUpMutatorContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800280 if defaultable, ok := ctx.Module().(Defaultable); ok {
Cole Faustf53b6b12024-07-29 12:24:25 -0700281 ctx.AddDependency(ctx.Module(), DefaultsDepTag, defaultable.defaults().Defaults.GetOrDefault(ctx, nil)...)
Colin Crosscfad1192015-11-02 16:43:11 -0800282 }
283}
284
Colin Cross13177012016-08-09 12:00:45 -0700285func defaultsMutator(ctx TopDownMutatorContext) {
Paul Duffinafa9fa12020-04-29 16:47:28 +0100286 if defaultable, ok := ctx.Module().(Defaultable); ok {
Cole Faustf53b6b12024-07-29 12:24:25 -0700287 defaults := defaultable.defaults().Defaults.GetOrDefault(ctx, nil)
Inseob Kimf11bb2d2024-07-23 13:29:48 +0900288 if len(defaults) > 0 {
Liz Kammereeeb9522023-03-10 18:53:41 -0500289 var defaultsList []Defaults
Paul Duffinafa9fa12020-04-29 16:47:28 +0100290 seen := make(map[Defaults]bool)
Colin Crossa1ce2a02018-06-20 15:19:39 -0700291
Paul Duffinafa9fa12020-04-29 16:47:28 +0100292 ctx.WalkDeps(func(module, parent Module) bool {
293 if ctx.OtherModuleDependencyTag(module) == DefaultsDepTag {
Liz Kammereeeb9522023-03-10 18:53:41 -0500294 if defaults, ok := module.(Defaults); ok {
Paul Duffinafa9fa12020-04-29 16:47:28 +0100295 if !seen[defaults] {
296 seen[defaults] = true
297 defaultsList = append(defaultsList, defaults)
Cole Faustf53b6b12024-07-29 12:24:25 -0700298 return len(defaults.defaults().Defaults.GetOrDefault(ctx, nil)) > 0
Paul Duffinafa9fa12020-04-29 16:47:28 +0100299 }
300 } else {
301 ctx.PropertyErrorf("defaults", "module %s is not an defaults module",
302 ctx.OtherModuleName(module))
Colin Crossa1ce2a02018-06-20 15:19:39 -0700303 }
Colin Crosscfad1192015-11-02 16:43:11 -0800304 }
Paul Duffinafa9fa12020-04-29 16:47:28 +0100305 return false
306 })
307 defaultable.applyDefaults(ctx, defaultsList)
308 }
309
Jihoon Kangd48abd52023-02-02 22:32:31 +0000310 defaultable.CallHookIfAvailable(ctx)
Colin Crosscfad1192015-11-02 16:43:11 -0800311 }
312}