Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 1 | // 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 Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "github.com/google/blueprint" |
| 19 | "github.com/google/blueprint/proptools" |
| 20 | ) |
| 21 | |
Colin Cross | c99deeb | 2016-04-11 15:06:20 -0700 | [diff] [blame] | 22 | type defaultsDependencyTag struct { |
| 23 | blueprint.BaseDependencyTag |
| 24 | } |
| 25 | |
| 26 | var DefaultsDepTag defaultsDependencyTag |
| 27 | |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 28 | type defaultsProperties struct { |
| 29 | Defaults []string |
| 30 | } |
| 31 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 32 | type DefaultableModuleBase struct { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 33 | defaultsProperties defaultsProperties |
| 34 | defaultableProperties []interface{} |
| 35 | } |
| 36 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 37 | func (d *DefaultableModuleBase) defaults() *defaultsProperties { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 38 | return &d.defaultsProperties |
| 39 | } |
| 40 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 41 | func (d *DefaultableModuleBase) setProperties(props []interface{}) { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 42 | d.defaultableProperties = props |
| 43 | } |
| 44 | |
| 45 | type Defaultable interface { |
| 46 | defaults() *defaultsProperties |
| 47 | setProperties([]interface{}) |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 48 | applyDefaults(TopDownMutatorContext, []Defaults) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 51 | type DefaultableModule interface { |
| 52 | Module |
| 53 | Defaultable |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 56 | var _ Defaultable = (*DefaultableModuleBase)(nil) |
| 57 | |
| 58 | func InitDefaultableModule(module DefaultableModule) { |
| 59 | module.(Defaultable).setProperties(module.(Module).GetProperties()) |
| 60 | |
| 61 | module.AddProperties(module.defaults()) |
| 62 | } |
| 63 | |
| 64 | type DefaultsModuleBase struct { |
| 65 | DefaultableModuleBase |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 66 | defaultProperties []interface{} |
| 67 | } |
| 68 | |
| 69 | type Defaults interface { |
Colin Cross | e7b0713 | 2016-07-27 10:15:06 -0700 | [diff] [blame] | 70 | Defaultable |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 71 | isDefaults() bool |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 72 | properties() []interface{} |
| 73 | } |
| 74 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 75 | func (d *DefaultsModuleBase) isDefaults() bool { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 76 | return true |
| 77 | } |
| 78 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 79 | func (d *DefaultsModuleBase) properties() []interface{} { |
Colin Cross | e7b0713 | 2016-07-27 10:15:06 -0700 | [diff] [blame] | 80 | return d.defaultableProperties |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 83 | func InitDefaultsModule(module DefaultableModule) { |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 84 | module.AddProperties( |
Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 85 | &hostAndDeviceProperties{}, |
| 86 | &commonProperties{}, |
| 87 | &variableProperties{}) |
| 88 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 89 | InitArchModule(module) |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 90 | InitDefaultableModule(module) |
Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 91 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 92 | module.AddProperties(&module.base().nameProperties) |
Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 93 | |
| 94 | module.base().module = module |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 97 | var _ Defaults = (*DefaultsModuleBase)(nil) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 98 | |
Colin Cross | 1f44a3a | 2017-07-07 14:33:33 -0700 | [diff] [blame] | 99 | func (defaultable *DefaultableModuleBase) applyDefaults(ctx TopDownMutatorContext, |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 100 | defaultsList []Defaults) { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 101 | |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 102 | for _, defaults := range defaultsList { |
| 103 | for _, prop := range defaultable.defaultableProperties { |
| 104 | for _, def := range defaults.properties() { |
| 105 | if proptools.TypeEqual(prop, def) { |
| 106 | err := proptools.PrependProperties(prop, def, nil) |
| 107 | if err != nil { |
| 108 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 109 | ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 110 | } else { |
| 111 | panic(err) |
| 112 | } |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 120 | func RegisterDefaultsPreArchMutators(ctx RegisterMutatorsContext) { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 121 | ctx.BottomUp("defaults_deps", defaultsDepsMutator).Parallel() |
| 122 | ctx.TopDown("defaults", defaultsMutator).Parallel() |
| 123 | } |
| 124 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 125 | func defaultsDepsMutator(ctx BottomUpMutatorContext) { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 126 | if defaultable, ok := ctx.Module().(Defaultable); ok { |
Colin Cross | c99deeb | 2016-04-11 15:06:20 -0700 | [diff] [blame] | 127 | ctx.AddDependency(ctx.Module(), DefaultsDepTag, defaultable.defaults().Defaults...) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 131 | func defaultsMutator(ctx TopDownMutatorContext) { |
| 132 | if defaultable, ok := ctx.Module().(Defaultable); ok && len(defaultable.defaults().Defaults) > 0 { |
| 133 | var defaultsList []Defaults |
Colin Cross | a1ce2a0 | 2018-06-20 15:19:39 -0700 | [diff] [blame] | 134 | seen := make(map[Defaults]bool) |
| 135 | |
Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 136 | ctx.WalkDeps(func(module, parent Module) bool { |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 137 | if ctx.OtherModuleDependencyTag(module) == DefaultsDepTag { |
| 138 | if defaults, ok := module.(Defaults); ok { |
Colin Cross | a1ce2a0 | 2018-06-20 15:19:39 -0700 | [diff] [blame] | 139 | if !seen[defaults] { |
| 140 | seen[defaults] = true |
| 141 | defaultsList = append(defaultsList, defaults) |
| 142 | return len(defaults.defaults().Defaults) > 0 |
| 143 | } |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 144 | } else { |
| 145 | ctx.PropertyErrorf("defaults", "module %s is not an defaults module", |
| 146 | ctx.OtherModuleName(module)) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 147 | } |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 148 | } |
| 149 | return false |
| 150 | }) |
| 151 | defaultable.applyDefaults(ctx, defaultsList) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 152 | } |
| 153 | } |