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 | |
| 32 | type DefaultableModule struct { |
| 33 | defaultsProperties defaultsProperties |
| 34 | defaultableProperties []interface{} |
| 35 | } |
| 36 | |
| 37 | func (d *DefaultableModule) defaults() *defaultsProperties { |
| 38 | return &d.defaultsProperties |
| 39 | } |
| 40 | |
| 41 | func (d *DefaultableModule) setProperties(props []interface{}) { |
| 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 | |
| 51 | var _ Defaultable = (*DefaultableModule)(nil) |
| 52 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame^] | 53 | func InitDefaultableModule(module Module, d Defaultable) { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 54 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame^] | 55 | d.setProperties(module.GetProperties()) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 56 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame^] | 57 | module.AddProperties(d.defaults()) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | type DefaultsModule struct { |
Colin Cross | e7b0713 | 2016-07-27 10:15:06 -0700 | [diff] [blame] | 61 | DefaultableModule |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 62 | defaultProperties []interface{} |
| 63 | } |
| 64 | |
| 65 | type Defaults interface { |
Colin Cross | e7b0713 | 2016-07-27 10:15:06 -0700 | [diff] [blame] | 66 | Defaultable |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 67 | isDefaults() bool |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 68 | properties() []interface{} |
| 69 | } |
| 70 | |
| 71 | func (d *DefaultsModule) isDefaults() bool { |
| 72 | return true |
| 73 | } |
| 74 | |
| 75 | func (d *DefaultsModule) properties() []interface{} { |
Colin Cross | e7b0713 | 2016-07-27 10:15:06 -0700 | [diff] [blame] | 76 | return d.defaultableProperties |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame^] | 79 | func InitDefaultsModule(module Module, d Defaults) { |
| 80 | module.AddProperties( |
Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 81 | &hostAndDeviceProperties{}, |
| 82 | &commonProperties{}, |
| 83 | &variableProperties{}) |
| 84 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame^] | 85 | InitArchModule(module) |
| 86 | InitDefaultableModule(module, d) |
Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 87 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame^] | 88 | module.AddProperties(&module.base().nameProperties) |
Colin Cross | fc75458 | 2016-05-17 16:34:16 -0700 | [diff] [blame] | 89 | |
| 90 | module.base().module = module |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | var _ Defaults = (*DefaultsModule)(nil) |
| 94 | |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 95 | func (defaultable *DefaultableModule) applyDefaults(ctx TopDownMutatorContext, |
| 96 | defaultsList []Defaults) { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 97 | |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 98 | for _, defaults := range defaultsList { |
| 99 | for _, prop := range defaultable.defaultableProperties { |
| 100 | for _, def := range defaults.properties() { |
| 101 | if proptools.TypeEqual(prop, def) { |
| 102 | err := proptools.PrependProperties(prop, def, nil) |
| 103 | if err != nil { |
| 104 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 105 | ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 106 | } else { |
| 107 | panic(err) |
| 108 | } |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 116 | func defaultsDepsMutator(ctx BottomUpMutatorContext) { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 117 | if defaultable, ok := ctx.Module().(Defaultable); ok { |
Colin Cross | c99deeb | 2016-04-11 15:06:20 -0700 | [diff] [blame] | 118 | ctx.AddDependency(ctx.Module(), DefaultsDepTag, defaultable.defaults().Defaults...) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 122 | func defaultsMutator(ctx TopDownMutatorContext) { |
| 123 | if defaultable, ok := ctx.Module().(Defaultable); ok && len(defaultable.defaults().Defaults) > 0 { |
| 124 | var defaultsList []Defaults |
| 125 | ctx.WalkDeps(func(module, parent blueprint.Module) bool { |
| 126 | if ctx.OtherModuleDependencyTag(module) == DefaultsDepTag { |
| 127 | if defaults, ok := module.(Defaults); ok { |
| 128 | defaultsList = append(defaultsList, defaults) |
| 129 | return len(defaults.defaults().Defaults) > 0 |
| 130 | } else { |
| 131 | ctx.PropertyErrorf("defaults", "module %s is not an defaults module", |
| 132 | ctx.OtherModuleName(module)) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 133 | } |
Colin Cross | 1317701 | 2016-08-09 12:00:45 -0700 | [diff] [blame] | 134 | } |
| 135 | return false |
| 136 | }) |
| 137 | defaultable.applyDefaults(ctx, defaultsList) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 138 | } |
| 139 | } |