blob: 8b121f6d9214d87ab9d37d6957cfcb891ddafa90 [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 }
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
Liz Kammer416201d2021-12-15 13:18:42 -0500120
121 // Included to support setting bazel_module.label for multiple Soong modules to the same Bazel
122 // target. This is primarily useful for modules that were architecture specific and instead are
123 // handled in Bazel as a select().
124 BazelModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -0800125}
126
Martin Stjernholmebd757d2019-05-24 11:00:30 +0100127// The common pattern for defaults modules is to register separate instances of
128// the xxxProperties structs in the AddProperties calls, rather than reusing the
129// ones inherited from Module.
130//
131// The effect is that e.g. myDefaultsModuleInstance.base().xxxProperties won't
132// contain the values that have been set for the defaults module. Rather, to
133// retrieve the values it is necessary to iterate over properties(). E.g. to get
134// the commonProperties instance that have the real values:
135//
136// d := myModule.(Defaults)
137// for _, props := range d.properties() {
138// if cp, ok := props.(*commonProperties); ok {
139// ... access property values in cp ...
140// }
141// }
142//
143// The rationale is that the properties on a defaults module apply to the
144// defaultable modules using it, not to the defaults module itself. E.g. setting
145// the "enabled" property false makes inheriting modules disabled by default,
146// rather than disabling the defaults module itself.
Colin Crosscfad1192015-11-02 16:43:11 -0800147type Defaults interface {
Colin Crosse7b07132016-07-27 10:15:06 -0700148 Defaultable
Paul Duffin7df7fb02019-07-24 12:26:14 +0100149
150 // Although this function is unused it is actually needed to ensure that only modules that embed
151 // DefaultsModuleBase will type-assert to the Defaults interface.
Colin Crosscfad1192015-11-02 16:43:11 -0800152 isDefaults() bool
Paul Duffin7df7fb02019-07-24 12:26:14 +0100153
154 // Get the structures containing the properties for which defaults can be provided.
Colin Crosscfad1192015-11-02 16:43:11 -0800155 properties() []interface{}
Paul Duffin63c6e182019-07-24 14:24:38 +0100156
Colin Crosseabaedd2020-02-06 17:01:55 -0800157 productVariableProperties() interface{}
Colin Crosscfad1192015-11-02 16:43:11 -0800158}
159
Colin Cross1f44a3a2017-07-07 14:33:33 -0700160func (d *DefaultsModuleBase) isDefaults() bool {
Colin Crosscfad1192015-11-02 16:43:11 -0800161 return true
162}
163
Paul Duffine62432f2019-07-24 12:51:21 +0100164type DefaultsModule interface {
165 Module
166 Defaults
Liz Kammer416201d2021-12-15 13:18:42 -0500167 Bazelable
Paul Duffine62432f2019-07-24 12:51:21 +0100168}
169
Colin Cross1f44a3a2017-07-07 14:33:33 -0700170func (d *DefaultsModuleBase) properties() []interface{} {
Colin Crosse7b07132016-07-27 10:15:06 -0700171 return d.defaultableProperties
Colin Crosscfad1192015-11-02 16:43:11 -0800172}
173
Colin Crosseabaedd2020-02-06 17:01:55 -0800174func (d *DefaultsModuleBase) productVariableProperties() interface{} {
175 return d.defaultableVariableProperties
176}
177
Liz Kammer416201d2021-12-15 13:18:42 -0500178func (d *DefaultsModuleBase) GenerateAndroidBuildActions(ctx ModuleContext) {}
Colin Cross59037622019-06-10 13:12:56 -0700179
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400180// ConvertWithBp2build to fulfill Bazelable interface; however, at this time defaults module are
181// *NOT* converted with bp2build
182func (defaultable *DefaultsModuleBase) ConvertWithBp2build(ctx TopDownMutatorContext) {}
183
Paul Duffine62432f2019-07-24 12:51:21 +0100184func InitDefaultsModule(module DefaultsModule) {
Paul Duffin3f98d142020-09-02 13:28:25 +0100185 commonProperties := &commonProperties{}
Paul Duffin63c6e182019-07-24 14:24:38 +0100186
Colin Cross36242852017-06-23 15:06:31 -0700187 module.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700188 &hostAndDeviceProperties{},
Paul Duffin63c6e182019-07-24 14:24:38 +0100189 commonProperties,
Paul Duffined875132020-09-02 13:08:57 +0100190 &ApexProperties{},
191 &distProperties{})
Colin Crossfc754582016-05-17 16:34:16 -0700192
Liz Kammer416201d2021-12-15 13:18:42 -0500193 // Bazel module must be initialized _before_ Defaults to be included in cc_defaults module.
194 InitBazelModule(module)
Colin Crosseabaedd2020-02-06 17:01:55 -0800195 initAndroidModuleBase(module)
196 initProductVariableModule(module)
Colin Crossa6845402020-11-16 15:08:19 -0800197 initArchModule(module)
Colin Cross1f44a3a2017-07-07 14:33:33 -0700198 InitDefaultableModule(module)
Colin Crossfc754582016-05-17 16:34:16 -0700199
Paul Duffin7df7fb02019-07-24 12:26:14 +0100200 // Add properties that will not have defaults applied to them.
Colin Cross08d6f8f2020-11-19 02:33:19 +0000201 base := module.base()
Paul Duffin3f98d142020-09-02 13:28:25 +0100202 defaultsVisibility := &DefaultsVisibilityProperties{}
Paul Duffin95d53b52019-07-24 13:45:05 +0100203 module.AddProperties(&base.nameProperties, defaultsVisibility)
Colin Crossfc754582016-05-17 16:34:16 -0700204
Paul Duffin63c6e182019-07-24 14:24:38 +0100205 // Unlike non-defaults modules the visibility property is not stored in m.base().commonProperties.
Paul Duffin5ec73ec2020-05-01 17:52:01 +0100206 // Instead it is stored in a separate instance of commonProperties created above so clear the
207 // existing list of properties.
208 clearVisibilityProperties(module)
209
210 // The defaults_visibility property controls the visibility of a defaults module so it must be
211 // set as the primary property, which also adds it to the list.
212 setPrimaryVisibilityProperty(module, "defaults_visibility", &defaultsVisibility.Defaults_visibility)
213
Paul Duffin63c6e182019-07-24 14:24:38 +0100214 // The visibility property needs to be checked (but not parsed) by the visibility module during
Paul Duffin5ec73ec2020-05-01 17:52:01 +0100215 // its checking phase and parsing phase so add it to the list as a normal property.
216 AddVisibilityProperty(module, "visibility", &commonProperties.Visibility)
Paul Duffin63c6e182019-07-24 14:24:38 +0100217
Bob Badour37af0462021-01-07 03:34:31 +0000218 // The applicable licenses property for defaults is 'licenses'.
219 setPrimaryLicensesProperty(module, "licenses", &commonProperties.Licenses)
220
Colin Crosscfad1192015-11-02 16:43:11 -0800221}
222
Colin Cross1f44a3a2017-07-07 14:33:33 -0700223var _ Defaults = (*DefaultsModuleBase)(nil)
Colin Crosscfad1192015-11-02 16:43:11 -0800224
Jingwen Chen84817de2021-11-17 10:57:35 +0000225// applyNamespacedVariableDefaults only runs in bp2build mode for
226// defaultable/defaults modules. Its purpose is to merge namespaced product
227// variable props from defaults deps, even if those defaults are custom module
228// types created from soong_config_module_type, e.g. one that's wrapping a
229// cc_defaults or java_defaults.
230func applyNamespacedVariableDefaults(defaultDep Defaults, ctx TopDownMutatorContext) {
231 var dep, b Bazelable
232
233 dep, ok := defaultDep.(Bazelable)
234 if !ok {
235 if depMod, ok := defaultDep.(Module); ok {
236 // Track that this dependency hasn't been converted to bp2build yet.
237 ctx.AddUnconvertedBp2buildDep(depMod.Name())
238 return
239 } else {
240 panic("Expected default dep to be a Module.")
241 }
242 }
243
244 b, ok = ctx.Module().(Bazelable)
245 if !ok {
246 return
247 }
248
249 // namespacedVariableProps is a map from namespaces (e.g. acme, android,
250 // vendor_foo) to a slice of soong_config_variable struct pointers,
251 // containing properties for that particular module.
252 src := dep.namespacedVariableProps()
253 dst := b.namespacedVariableProps()
254 if dst == nil {
255 dst = make(namespacedVariableProperties)
256 }
257
258 // Propagate all soong_config_variable structs from the dep. We'll merge the
259 // actual property values later in variable.go.
260 for namespace := range src {
261 if dst[namespace] == nil {
262 dst[namespace] = []interface{}{}
263 }
264 for _, i := range src[namespace] {
265 dst[namespace] = append(dst[namespace], i)
266 }
267 }
268
269 b.setNamespacedVariableProps(dst)
270}
271
Colin Cross1f44a3a2017-07-07 14:33:33 -0700272func (defaultable *DefaultableModuleBase) applyDefaults(ctx TopDownMutatorContext,
Colin Cross13177012016-08-09 12:00:45 -0700273 defaultsList []Defaults) {
Colin Crosscfad1192015-11-02 16:43:11 -0800274
Colin Cross13177012016-08-09 12:00:45 -0700275 for _, defaults := range defaultsList {
Jingwen Chen84817de2021-11-17 10:57:35 +0000276 if ctx.Config().runningAsBp2Build {
277 applyNamespacedVariableDefaults(defaults, ctx)
278 }
Colin Cross13177012016-08-09 12:00:45 -0700279 for _, prop := range defaultable.defaultableProperties {
Colin Crosseabaedd2020-02-06 17:01:55 -0800280 if prop == defaultable.defaultableVariableProperties {
281 defaultable.applyDefaultVariableProperties(ctx, defaults, prop)
282 } else {
283 defaultable.applyDefaultProperties(ctx, defaults, prop)
284 }
285 }
286 }
287}
288
289// Product variable properties need special handling, the type of the filtered product variable
290// property struct may not be identical between the defaults module and the defaultable module.
291// Use PrependMatchingProperties to apply whichever properties match.
292func (defaultable *DefaultableModuleBase) applyDefaultVariableProperties(ctx TopDownMutatorContext,
293 defaults Defaults, defaultableProp interface{}) {
294 if defaultableProp == nil {
295 return
296 }
297
298 defaultsProp := defaults.productVariableProperties()
299 if defaultsProp == nil {
300 return
301 }
302
303 dst := []interface{}{
304 defaultableProp,
305 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
306 // don't cause a "failed to find property to extend" error.
307 proptools.CloneEmptyProperties(reflect.ValueOf(defaultsProp)).Interface(),
308 }
309
310 err := proptools.PrependMatchingProperties(dst, defaultsProp, nil)
311 if err != nil {
312 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
313 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
314 } else {
315 panic(err)
316 }
317 }
318}
319
320func (defaultable *DefaultableModuleBase) applyDefaultProperties(ctx TopDownMutatorContext,
321 defaults Defaults, defaultableProp interface{}) {
322
323 for _, def := range defaults.properties() {
324 if proptools.TypeEqual(defaultableProp, def) {
325 err := proptools.PrependProperties(defaultableProp, def, nil)
326 if err != nil {
327 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
328 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
329 } else {
330 panic(err)
Colin Crosscfad1192015-11-02 16:43:11 -0800331 }
332 }
333 }
334 }
335}
336
Colin Cross89536d42017-07-07 14:35:50 -0700337func RegisterDefaultsPreArchMutators(ctx RegisterMutatorsContext) {
Colin Crosscec81712017-07-13 14:43:27 -0700338 ctx.BottomUp("defaults_deps", defaultsDepsMutator).Parallel()
339 ctx.TopDown("defaults", defaultsMutator).Parallel()
340}
341
Colin Cross635c3b02016-05-18 15:37:25 -0700342func defaultsDepsMutator(ctx BottomUpMutatorContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800343 if defaultable, ok := ctx.Module().(Defaultable); ok {
Colin Crossc99deeb2016-04-11 15:06:20 -0700344 ctx.AddDependency(ctx.Module(), DefaultsDepTag, defaultable.defaults().Defaults...)
Colin Crosscfad1192015-11-02 16:43:11 -0800345 }
346}
347
Colin Cross13177012016-08-09 12:00:45 -0700348func defaultsMutator(ctx TopDownMutatorContext) {
Paul Duffinafa9fa12020-04-29 16:47:28 +0100349 if defaultable, ok := ctx.Module().(Defaultable); ok {
350 if len(defaultable.defaults().Defaults) > 0 {
351 var defaultsList []Defaults
352 seen := make(map[Defaults]bool)
Colin Crossa1ce2a02018-06-20 15:19:39 -0700353
Paul Duffinafa9fa12020-04-29 16:47:28 +0100354 ctx.WalkDeps(func(module, parent Module) bool {
355 if ctx.OtherModuleDependencyTag(module) == DefaultsDepTag {
356 if defaults, ok := module.(Defaults); ok {
357 if !seen[defaults] {
358 seen[defaults] = true
359 defaultsList = append(defaultsList, defaults)
360 return len(defaults.defaults().Defaults) > 0
361 }
362 } else {
363 ctx.PropertyErrorf("defaults", "module %s is not an defaults module",
364 ctx.OtherModuleName(module))
Colin Crossa1ce2a02018-06-20 15:19:39 -0700365 }
Colin Crosscfad1192015-11-02 16:43:11 -0800366 }
Paul Duffinafa9fa12020-04-29 16:47:28 +0100367 return false
368 })
369 defaultable.applyDefaults(ctx, defaultsList)
370 }
371
372 defaultable.callHookIfAvailable(ctx)
Colin Crosscfad1192015-11-02 16:43:11 -0800373 }
374}