blob: e969adc9ba23d6aefb6a15b09f535e16f7e62382 [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
45type Defaultable interface {
46 defaults() *defaultsProperties
47 setProperties([]interface{})
Colin Cross13177012016-08-09 12:00:45 -070048 applyDefaults(TopDownMutatorContext, []Defaults)
Colin Crosscfad1192015-11-02 16:43:11 -080049}
50
Colin Cross1f44a3a2017-07-07 14:33:33 -070051type DefaultableModule interface {
52 Module
53 Defaultable
Colin Crosscfad1192015-11-02 16:43:11 -080054}
55
Colin Cross1f44a3a2017-07-07 14:33:33 -070056var _ Defaultable = (*DefaultableModuleBase)(nil)
57
58func InitDefaultableModule(module DefaultableModule) {
59 module.(Defaultable).setProperties(module.(Module).GetProperties())
60
61 module.AddProperties(module.defaults())
62}
63
64type DefaultsModuleBase struct {
65 DefaultableModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -080066}
67
Martin Stjernholmebd757d2019-05-24 11:00:30 +010068// The common pattern for defaults modules is to register separate instances of
69// the xxxProperties structs in the AddProperties calls, rather than reusing the
70// ones inherited from Module.
71//
72// The effect is that e.g. myDefaultsModuleInstance.base().xxxProperties won't
73// contain the values that have been set for the defaults module. Rather, to
74// retrieve the values it is necessary to iterate over properties(). E.g. to get
75// the commonProperties instance that have the real values:
76//
77// d := myModule.(Defaults)
78// for _, props := range d.properties() {
79// if cp, ok := props.(*commonProperties); ok {
80// ... access property values in cp ...
81// }
82// }
83//
84// The rationale is that the properties on a defaults module apply to the
85// defaultable modules using it, not to the defaults module itself. E.g. setting
86// the "enabled" property false makes inheriting modules disabled by default,
87// rather than disabling the defaults module itself.
Colin Crosscfad1192015-11-02 16:43:11 -080088type Defaults interface {
Colin Crosse7b07132016-07-27 10:15:06 -070089 Defaultable
Colin Crosscfad1192015-11-02 16:43:11 -080090 isDefaults() bool
Colin Crosscfad1192015-11-02 16:43:11 -080091 properties() []interface{}
92}
93
Colin Cross1f44a3a2017-07-07 14:33:33 -070094func (d *DefaultsModuleBase) isDefaults() bool {
Colin Crosscfad1192015-11-02 16:43:11 -080095 return true
96}
97
Colin Cross1f44a3a2017-07-07 14:33:33 -070098func (d *DefaultsModuleBase) properties() []interface{} {
Colin Crosse7b07132016-07-27 10:15:06 -070099 return d.defaultableProperties
Colin Crosscfad1192015-11-02 16:43:11 -0800100}
101
Colin Cross1f44a3a2017-07-07 14:33:33 -0700102func InitDefaultsModule(module DefaultableModule) {
Colin Cross36242852017-06-23 15:06:31 -0700103 module.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700104 &hostAndDeviceProperties{},
105 &commonProperties{},
106 &variableProperties{})
107
Colin Cross36242852017-06-23 15:06:31 -0700108 InitArchModule(module)
Colin Cross1f44a3a2017-07-07 14:33:33 -0700109 InitDefaultableModule(module)
Colin Crossfc754582016-05-17 16:34:16 -0700110
Colin Cross36242852017-06-23 15:06:31 -0700111 module.AddProperties(&module.base().nameProperties)
Colin Crossfc754582016-05-17 16:34:16 -0700112
113 module.base().module = module
Colin Crosscfad1192015-11-02 16:43:11 -0800114}
115
Colin Cross1f44a3a2017-07-07 14:33:33 -0700116var _ Defaults = (*DefaultsModuleBase)(nil)
Colin Crosscfad1192015-11-02 16:43:11 -0800117
Colin Cross1f44a3a2017-07-07 14:33:33 -0700118func (defaultable *DefaultableModuleBase) applyDefaults(ctx TopDownMutatorContext,
Colin Cross13177012016-08-09 12:00:45 -0700119 defaultsList []Defaults) {
Colin Crosscfad1192015-11-02 16:43:11 -0800120
Colin Cross13177012016-08-09 12:00:45 -0700121 for _, defaults := range defaultsList {
122 for _, prop := range defaultable.defaultableProperties {
123 for _, def := range defaults.properties() {
124 if proptools.TypeEqual(prop, def) {
125 err := proptools.PrependProperties(prop, def, nil)
126 if err != nil {
127 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
128 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
129 } else {
130 panic(err)
131 }
Colin Crosscfad1192015-11-02 16:43:11 -0800132 }
133 }
134 }
135 }
136 }
137}
138
Colin Cross89536d42017-07-07 14:35:50 -0700139func RegisterDefaultsPreArchMutators(ctx RegisterMutatorsContext) {
Colin Crosscec81712017-07-13 14:43:27 -0700140 ctx.BottomUp("defaults_deps", defaultsDepsMutator).Parallel()
141 ctx.TopDown("defaults", defaultsMutator).Parallel()
142}
143
Colin Cross635c3b02016-05-18 15:37:25 -0700144func defaultsDepsMutator(ctx BottomUpMutatorContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800145 if defaultable, ok := ctx.Module().(Defaultable); ok {
Colin Crossc99deeb2016-04-11 15:06:20 -0700146 ctx.AddDependency(ctx.Module(), DefaultsDepTag, defaultable.defaults().Defaults...)
Colin Crosscfad1192015-11-02 16:43:11 -0800147 }
148}
149
Colin Cross13177012016-08-09 12:00:45 -0700150func defaultsMutator(ctx TopDownMutatorContext) {
151 if defaultable, ok := ctx.Module().(Defaultable); ok && len(defaultable.defaults().Defaults) > 0 {
152 var defaultsList []Defaults
Colin Crossa1ce2a02018-06-20 15:19:39 -0700153 seen := make(map[Defaults]bool)
154
Colin Crossd11fcda2017-10-23 17:59:01 -0700155 ctx.WalkDeps(func(module, parent Module) bool {
Colin Cross13177012016-08-09 12:00:45 -0700156 if ctx.OtherModuleDependencyTag(module) == DefaultsDepTag {
157 if defaults, ok := module.(Defaults); ok {
Colin Crossa1ce2a02018-06-20 15:19:39 -0700158 if !seen[defaults] {
159 seen[defaults] = true
160 defaultsList = append(defaultsList, defaults)
161 return len(defaults.defaults().Defaults) > 0
162 }
Colin Cross13177012016-08-09 12:00:45 -0700163 } else {
164 ctx.PropertyErrorf("defaults", "module %s is not an defaults module",
165 ctx.OtherModuleName(module))
Colin Crosscfad1192015-11-02 16:43:11 -0800166 }
Colin Cross13177012016-08-09 12:00:45 -0700167 }
168 return false
169 })
170 defaultable.applyDefaults(ctx, defaultsList)
Colin Crosscfad1192015-11-02 16:43:11 -0800171 }
172}