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 | 635c3b0 | 2016-05-18 15:37:25 -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 | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame^] | 53 | func InitDefaultableModule(module Module, d Defaultable, |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 54 | props ...interface{}) (blueprint.Module, []interface{}) { |
| 55 | |
| 56 | d.setProperties(props) |
| 57 | |
| 58 | props = append(props, d.defaults()) |
| 59 | |
| 60 | return module, props |
| 61 | } |
| 62 | |
| 63 | type DefaultsModule struct { |
| 64 | defaultProperties []interface{} |
| 65 | } |
| 66 | |
| 67 | type Defaults interface { |
| 68 | isDefaults() bool |
| 69 | setProperties([]interface{}) |
| 70 | properties() []interface{} |
| 71 | } |
| 72 | |
| 73 | func (d *DefaultsModule) isDefaults() bool { |
| 74 | return true |
| 75 | } |
| 76 | |
| 77 | func (d *DefaultsModule) properties() []interface{} { |
| 78 | return d.defaultProperties |
| 79 | } |
| 80 | |
| 81 | func (d *DefaultsModule) setProperties(props []interface{}) { |
| 82 | d.defaultProperties = props |
| 83 | } |
| 84 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame^] | 85 | func InitDefaultsModule(module Module, d Defaults, props ...interface{}) (blueprint.Module, []interface{}) { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 86 | d.setProperties(props) |
| 87 | |
| 88 | return module, props |
| 89 | } |
| 90 | |
| 91 | var _ Defaults = (*DefaultsModule)(nil) |
| 92 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame^] | 93 | func (defaultable *DefaultableModule) applyDefaults(ctx TopDownMutatorContext, |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 94 | defaults Defaults) { |
| 95 | |
| 96 | for _, prop := range defaultable.defaultableProperties { |
| 97 | for _, def := range defaults.properties() { |
| 98 | if proptools.TypeEqual(prop, def) { |
| 99 | err := proptools.PrependProperties(prop, def, nil) |
| 100 | if err != nil { |
| 101 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 102 | ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 103 | } else { |
| 104 | panic(err) |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame^] | 112 | func defaultsDepsMutator(ctx BottomUpMutatorContext) { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 113 | if defaultable, ok := ctx.Module().(Defaultable); ok { |
Colin Cross | c99deeb | 2016-04-11 15:06:20 -0700 | [diff] [blame] | 114 | ctx.AddDependency(ctx.Module(), DefaultsDepTag, defaultable.defaults().Defaults...) |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame^] | 118 | func defaultsMutator(ctx TopDownMutatorContext) { |
Colin Cross | cfad119 | 2015-11-02 16:43:11 -0800 | [diff] [blame] | 119 | if defaultable, ok := ctx.Module().(Defaultable); ok { |
| 120 | for _, defaultsDep := range defaultable.defaults().Defaults { |
| 121 | ctx.VisitDirectDeps(func(m blueprint.Module) { |
| 122 | if ctx.OtherModuleName(m) == defaultsDep { |
| 123 | if defaultsModule, ok := m.(Defaults); ok { |
| 124 | defaultable.applyDefaults(ctx, defaultsModule) |
| 125 | } else { |
| 126 | ctx.PropertyErrorf("defaults", "module %s is not an defaults module", |
| 127 | ctx.OtherModuleName(m)) |
| 128 | } |
| 129 | } |
| 130 | }) |
| 131 | } |
| 132 | } |
| 133 | } |