blob: d2b351d04be53b518bc4d03c15bc55831025ad17 [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 {
31 Defaults []string
32}
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
56func (d *DefaultableModuleBase) callHookIfAvailable(ctx DefaultableHookContext) {
57 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
70 // Apply defaults from the supplied Defaults to the property structures supplied to
71 // setProperties(...).
Colin Cross13177012016-08-09 12:00:45 -070072 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.
81 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 }
Ustafe201fe2021-12-06 15:13:36 -050095 module.setProperties(module.GetProperties(), module.base().variableProperties)
Colin Cross1f44a3a2017-07-07 14:33:33 -070096
97 module.AddProperties(module.defaults())
Liz Kammerdbd48092020-09-21 22:24:17 +000098
99 module.base().customizableProperties = module.GetProperties()
Colin Cross1f44a3a2017-07-07 14:33:33 -0700100}
101
Paul Duffinafa9fa12020-04-29 16:47:28 +0100102// A restricted subset of context methods, similar to LoadHookContext.
103type DefaultableHookContext interface {
104 EarlyModuleContext
105
106 CreateModule(ModuleFactory, ...interface{}) Module
Colin Cross18f840c2021-05-20 17:56:54 -0700107 AddMissingDependencies(missingDeps []string)
Paul Duffinafa9fa12020-04-29 16:47:28 +0100108}
109
110type DefaultableHook func(ctx DefaultableHookContext)
111
Paul Duffin95d53b52019-07-24 13:45:05 +0100112// The Defaults_visibility property.
113type DefaultsVisibilityProperties struct {
114
115 // Controls the visibility of the defaults module itself.
116 Defaults_visibility []string
117}
118
Colin Cross1f44a3a2017-07-07 14:33:33 -0700119type DefaultsModuleBase struct {
120 DefaultableModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -0800121}
122
Martin Stjernholmebd757d2019-05-24 11:00:30 +0100123// The common pattern for defaults modules is to register separate instances of
124// the xxxProperties structs in the AddProperties calls, rather than reusing the
125// ones inherited from Module.
126//
127// The effect is that e.g. myDefaultsModuleInstance.base().xxxProperties won't
128// contain the values that have been set for the defaults module. Rather, to
129// retrieve the values it is necessary to iterate over properties(). E.g. to get
130// the commonProperties instance that have the real values:
131//
132// d := myModule.(Defaults)
133// for _, props := range d.properties() {
134// if cp, ok := props.(*commonProperties); ok {
135// ... access property values in cp ...
136// }
137// }
138//
139// The rationale is that the properties on a defaults module apply to the
140// defaultable modules using it, not to the defaults module itself. E.g. setting
141// the "enabled" property false makes inheriting modules disabled by default,
142// rather than disabling the defaults module itself.
Colin Crosscfad1192015-11-02 16:43:11 -0800143type Defaults interface {
Colin Crosse7b07132016-07-27 10:15:06 -0700144 Defaultable
Paul Duffin7df7fb02019-07-24 12:26:14 +0100145
146 // Although this function is unused it is actually needed to ensure that only modules that embed
147 // DefaultsModuleBase will type-assert to the Defaults interface.
Colin Crosscfad1192015-11-02 16:43:11 -0800148 isDefaults() bool
Paul Duffin7df7fb02019-07-24 12:26:14 +0100149
150 // Get the structures containing the properties for which defaults can be provided.
Colin Crosscfad1192015-11-02 16:43:11 -0800151 properties() []interface{}
Paul Duffin63c6e182019-07-24 14:24:38 +0100152
Colin Crosseabaedd2020-02-06 17:01:55 -0800153 productVariableProperties() interface{}
Colin Crosscfad1192015-11-02 16:43:11 -0800154}
155
Colin Cross1f44a3a2017-07-07 14:33:33 -0700156func (d *DefaultsModuleBase) isDefaults() bool {
Colin Crosscfad1192015-11-02 16:43:11 -0800157 return true
158}
159
Paul Duffine62432f2019-07-24 12:51:21 +0100160type DefaultsModule interface {
161 Module
162 Defaults
163}
164
Colin Cross1f44a3a2017-07-07 14:33:33 -0700165func (d *DefaultsModuleBase) properties() []interface{} {
Colin Crosse7b07132016-07-27 10:15:06 -0700166 return d.defaultableProperties
Colin Crosscfad1192015-11-02 16:43:11 -0800167}
168
Colin Crosseabaedd2020-02-06 17:01:55 -0800169func (d *DefaultsModuleBase) productVariableProperties() interface{} {
170 return d.defaultableVariableProperties
171}
172
Colin Cross59037622019-06-10 13:12:56 -0700173func (d *DefaultsModuleBase) GenerateAndroidBuildActions(ctx ModuleContext) {
174}
175
Paul Duffine62432f2019-07-24 12:51:21 +0100176func InitDefaultsModule(module DefaultsModule) {
Paul Duffin3f98d142020-09-02 13:28:25 +0100177 commonProperties := &commonProperties{}
Paul Duffin63c6e182019-07-24 14:24:38 +0100178
Colin Cross36242852017-06-23 15:06:31 -0700179 module.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700180 &hostAndDeviceProperties{},
Paul Duffin63c6e182019-07-24 14:24:38 +0100181 commonProperties,
Paul Duffined875132020-09-02 13:08:57 +0100182 &ApexProperties{},
183 &distProperties{})
Colin Crossfc754582016-05-17 16:34:16 -0700184
Colin Crosseabaedd2020-02-06 17:01:55 -0800185 initAndroidModuleBase(module)
186 initProductVariableModule(module)
Colin Crossa6845402020-11-16 15:08:19 -0800187 initArchModule(module)
Colin Cross1f44a3a2017-07-07 14:33:33 -0700188 InitDefaultableModule(module)
Colin Crossfc754582016-05-17 16:34:16 -0700189
Paul Duffin7df7fb02019-07-24 12:26:14 +0100190 // Add properties that will not have defaults applied to them.
Colin Cross08d6f8f2020-11-19 02:33:19 +0000191 base := module.base()
Paul Duffin3f98d142020-09-02 13:28:25 +0100192 defaultsVisibility := &DefaultsVisibilityProperties{}
Paul Duffin95d53b52019-07-24 13:45:05 +0100193 module.AddProperties(&base.nameProperties, defaultsVisibility)
Colin Crossfc754582016-05-17 16:34:16 -0700194
Paul Duffin63c6e182019-07-24 14:24:38 +0100195 // Unlike non-defaults modules the visibility property is not stored in m.base().commonProperties.
Paul Duffin5ec73ec2020-05-01 17:52:01 +0100196 // Instead it is stored in a separate instance of commonProperties created above so clear the
197 // existing list of properties.
198 clearVisibilityProperties(module)
199
200 // The defaults_visibility property controls the visibility of a defaults module so it must be
201 // set as the primary property, which also adds it to the list.
202 setPrimaryVisibilityProperty(module, "defaults_visibility", &defaultsVisibility.Defaults_visibility)
203
Paul Duffin63c6e182019-07-24 14:24:38 +0100204 // The visibility property needs to be checked (but not parsed) by the visibility module during
Paul Duffin5ec73ec2020-05-01 17:52:01 +0100205 // its checking phase and parsing phase so add it to the list as a normal property.
206 AddVisibilityProperty(module, "visibility", &commonProperties.Visibility)
Paul Duffin63c6e182019-07-24 14:24:38 +0100207
Bob Badour37af0462021-01-07 03:34:31 +0000208 // The applicable licenses property for defaults is 'licenses'.
209 setPrimaryLicensesProperty(module, "licenses", &commonProperties.Licenses)
210
Paul Duffin63c6e182019-07-24 14:24:38 +0100211 base.module = module
Colin Crosscfad1192015-11-02 16:43:11 -0800212}
213
Colin Cross1f44a3a2017-07-07 14:33:33 -0700214var _ Defaults = (*DefaultsModuleBase)(nil)
Colin Crosscfad1192015-11-02 16:43:11 -0800215
Jingwen Chen84817de2021-11-17 10:57:35 +0000216// applyNamespacedVariableDefaults only runs in bp2build mode for
217// defaultable/defaults modules. Its purpose is to merge namespaced product
218// variable props from defaults deps, even if those defaults are custom module
219// types created from soong_config_module_type, e.g. one that's wrapping a
220// cc_defaults or java_defaults.
221func applyNamespacedVariableDefaults(defaultDep Defaults, ctx TopDownMutatorContext) {
222 var dep, b Bazelable
223
224 dep, ok := defaultDep.(Bazelable)
225 if !ok {
226 if depMod, ok := defaultDep.(Module); ok {
227 // Track that this dependency hasn't been converted to bp2build yet.
228 ctx.AddUnconvertedBp2buildDep(depMod.Name())
229 return
230 } else {
231 panic("Expected default dep to be a Module.")
232 }
233 }
234
235 b, ok = ctx.Module().(Bazelable)
236 if !ok {
237 return
238 }
239
240 // namespacedVariableProps is a map from namespaces (e.g. acme, android,
241 // vendor_foo) to a slice of soong_config_variable struct pointers,
242 // containing properties for that particular module.
243 src := dep.namespacedVariableProps()
244 dst := b.namespacedVariableProps()
245 if dst == nil {
246 dst = make(namespacedVariableProperties)
247 }
248
249 // Propagate all soong_config_variable structs from the dep. We'll merge the
250 // actual property values later in variable.go.
251 for namespace := range src {
252 if dst[namespace] == nil {
253 dst[namespace] = []interface{}{}
254 }
255 for _, i := range src[namespace] {
256 dst[namespace] = append(dst[namespace], i)
257 }
258 }
259
260 b.setNamespacedVariableProps(dst)
261}
262
Colin Cross1f44a3a2017-07-07 14:33:33 -0700263func (defaultable *DefaultableModuleBase) applyDefaults(ctx TopDownMutatorContext,
Colin Cross13177012016-08-09 12:00:45 -0700264 defaultsList []Defaults) {
Colin Crosscfad1192015-11-02 16:43:11 -0800265
Colin Cross13177012016-08-09 12:00:45 -0700266 for _, defaults := range defaultsList {
Jingwen Chen84817de2021-11-17 10:57:35 +0000267 if ctx.Config().runningAsBp2Build {
268 applyNamespacedVariableDefaults(defaults, ctx)
269 }
Colin Cross13177012016-08-09 12:00:45 -0700270 for _, prop := range defaultable.defaultableProperties {
Colin Crosseabaedd2020-02-06 17:01:55 -0800271 if prop == defaultable.defaultableVariableProperties {
272 defaultable.applyDefaultVariableProperties(ctx, defaults, prop)
273 } else {
274 defaultable.applyDefaultProperties(ctx, defaults, prop)
275 }
276 }
277 }
278}
279
280// Product variable properties need special handling, the type of the filtered product variable
281// property struct may not be identical between the defaults module and the defaultable module.
282// Use PrependMatchingProperties to apply whichever properties match.
283func (defaultable *DefaultableModuleBase) applyDefaultVariableProperties(ctx TopDownMutatorContext,
284 defaults Defaults, defaultableProp interface{}) {
285 if defaultableProp == nil {
286 return
287 }
288
289 defaultsProp := defaults.productVariableProperties()
290 if defaultsProp == nil {
291 return
292 }
293
294 dst := []interface{}{
295 defaultableProp,
296 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
297 // don't cause a "failed to find property to extend" error.
298 proptools.CloneEmptyProperties(reflect.ValueOf(defaultsProp)).Interface(),
299 }
300
301 err := proptools.PrependMatchingProperties(dst, defaultsProp, nil)
302 if err != nil {
303 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
304 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
305 } else {
306 panic(err)
307 }
308 }
309}
310
311func (defaultable *DefaultableModuleBase) applyDefaultProperties(ctx TopDownMutatorContext,
312 defaults Defaults, defaultableProp interface{}) {
313
314 for _, def := range defaults.properties() {
315 if proptools.TypeEqual(defaultableProp, def) {
316 err := proptools.PrependProperties(defaultableProp, def, nil)
317 if err != nil {
318 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
319 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
320 } else {
321 panic(err)
Colin Crosscfad1192015-11-02 16:43:11 -0800322 }
323 }
324 }
325 }
326}
327
Colin Cross89536d42017-07-07 14:35:50 -0700328func RegisterDefaultsPreArchMutators(ctx RegisterMutatorsContext) {
Colin Crosscec81712017-07-13 14:43:27 -0700329 ctx.BottomUp("defaults_deps", defaultsDepsMutator).Parallel()
330 ctx.TopDown("defaults", defaultsMutator).Parallel()
331}
332
Colin Cross635c3b02016-05-18 15:37:25 -0700333func defaultsDepsMutator(ctx BottomUpMutatorContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800334 if defaultable, ok := ctx.Module().(Defaultable); ok {
Colin Crossc99deeb2016-04-11 15:06:20 -0700335 ctx.AddDependency(ctx.Module(), DefaultsDepTag, defaultable.defaults().Defaults...)
Colin Crosscfad1192015-11-02 16:43:11 -0800336 }
337}
338
Colin Cross13177012016-08-09 12:00:45 -0700339func defaultsMutator(ctx TopDownMutatorContext) {
Paul Duffinafa9fa12020-04-29 16:47:28 +0100340 if defaultable, ok := ctx.Module().(Defaultable); ok {
341 if len(defaultable.defaults().Defaults) > 0 {
342 var defaultsList []Defaults
343 seen := make(map[Defaults]bool)
Colin Crossa1ce2a02018-06-20 15:19:39 -0700344
Paul Duffinafa9fa12020-04-29 16:47:28 +0100345 ctx.WalkDeps(func(module, parent Module) bool {
346 if ctx.OtherModuleDependencyTag(module) == DefaultsDepTag {
347 if defaults, ok := module.(Defaults); ok {
348 if !seen[defaults] {
349 seen[defaults] = true
350 defaultsList = append(defaultsList, defaults)
351 return len(defaults.defaults().Defaults) > 0
352 }
353 } else {
354 ctx.PropertyErrorf("defaults", "module %s is not an defaults module",
355 ctx.OtherModuleName(module))
Colin Crossa1ce2a02018-06-20 15:19:39 -0700356 }
Colin Crosscfad1192015-11-02 16:43:11 -0800357 }
Paul Duffinafa9fa12020-04-29 16:47:28 +0100358 return false
359 })
360 defaultable.applyDefaults(ctx, defaultsList)
361 }
362
363 defaultable.callHookIfAvailable(ctx)
Colin Crosscfad1192015-11-02 16:43:11 -0800364 }
365}