Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 1 | // Copyright 2016 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame^] | 18 | "reflect" |
| 19 | |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 20 | "github.com/google/blueprint" |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame^] | 21 | "github.com/google/blueprint/proptools" |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | // This file implements hooks that external module types can use to inject logic into existing |
| 25 | // module types. Each hook takes an interface as a parameter so that new methods can be added |
| 26 | // to the interface without breaking existing module types. |
| 27 | |
| 28 | // Load hooks are run after the module's properties have been filled from the blueprint file, but |
| 29 | // before the module has been split into architecture variants, and before defaults modules have |
| 30 | // been applied. |
| 31 | type LoadHookContext interface { |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 32 | EarlyModuleContext |
| 33 | |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 34 | AppendProperties(...interface{}) |
| 35 | PrependProperties(...interface{}) |
Colin Cross | e003c4a | 2019-09-25 12:58:36 -0700 | [diff] [blame] | 36 | CreateModule(ModuleFactory, ...interface{}) Module |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 39 | func AddLoadHook(m blueprint.Module, hook func(LoadHookContext)) { |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame^] | 40 | blueprint.AddLoadHook(m, func(ctx blueprint.LoadHookContext) { |
| 41 | actx := &loadHookContext{ |
| 42 | earlyModuleContext: m.(Module).base().earlyModuleContextFactory(ctx), |
| 43 | bp: ctx, |
| 44 | } |
| 45 | hook(actx) |
| 46 | }) |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame^] | 49 | type loadHookContext struct { |
| 50 | earlyModuleContext |
| 51 | bp blueprint.LoadHookContext |
| 52 | module Module |
| 53 | } |
| 54 | |
| 55 | func (l *loadHookContext) AppendProperties(props ...interface{}) { |
| 56 | for _, p := range props { |
| 57 | err := proptools.AppendMatchingProperties(l.Module().base().customizableProperties, |
| 58 | p, nil) |
| 59 | if err != nil { |
| 60 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 61 | l.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 62 | } else { |
| 63 | panic(err) |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame^] | 69 | func (l *loadHookContext) PrependProperties(props ...interface{}) { |
| 70 | for _, p := range props { |
| 71 | err := proptools.PrependMatchingProperties(l.Module().base().customizableProperties, |
| 72 | p, nil) |
| 73 | if err != nil { |
| 74 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 75 | l.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 76 | } else { |
| 77 | panic(err) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func (l *loadHookContext) CreateModule(factory ModuleFactory, props ...interface{}) Module { |
| 84 | inherited := []interface{}{&l.Module().base().commonProperties} |
| 85 | module := l.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module) |
| 86 | |
| 87 | if l.Module().base().variableProperties != nil && module.base().variableProperties != nil { |
| 88 | src := l.Module().base().variableProperties |
| 89 | dst := []interface{}{ |
| 90 | module.base().variableProperties, |
| 91 | // Put an empty copy of the src properties into dst so that properties in src that are not in dst |
| 92 | // don't cause a "failed to find property to extend" error. |
| 93 | proptools.CloneEmptyProperties(reflect.ValueOf(src).Elem()).Interface(), |
| 94 | } |
| 95 | err := proptools.AppendMatchingProperties(dst, src, nil) |
| 96 | if err != nil { |
| 97 | panic(err) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return module |
| 102 | } |
| 103 | |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 104 | type InstallHookContext interface { |
| 105 | ModuleContext |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 106 | Path() InstallPath |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 107 | Symlink() bool |
| 108 | } |
| 109 | |
| 110 | // Install hooks are run after a module creates a rule to install a file or symlink. |
| 111 | // The installed path is available from InstallHookContext.Path(), and |
| 112 | // InstallHookContext.Symlink() will be true if it was a symlink. |
| 113 | func AddInstallHook(m blueprint.Module, hook func(InstallHookContext)) { |
| 114 | h := &m.(Module).base().hooks |
| 115 | h.install = append(h.install, hook) |
| 116 | } |
| 117 | |
| 118 | type installHookContext struct { |
| 119 | ModuleContext |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 120 | path InstallPath |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 121 | symlink bool |
| 122 | } |
| 123 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 124 | func (x *installHookContext) Path() InstallPath { |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 125 | return x.path |
| 126 | } |
| 127 | |
| 128 | func (x *installHookContext) Symlink() bool { |
| 129 | return x.symlink |
| 130 | } |
| 131 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 132 | func (x *hooks) runInstallHooks(ctx ModuleContext, path InstallPath, symlink bool) { |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 133 | if len(x.install) > 0 { |
| 134 | mctx := &installHookContext{ |
| 135 | ModuleContext: ctx, |
| 136 | path: path, |
| 137 | symlink: symlink, |
| 138 | } |
| 139 | for _, x := range x.install { |
| 140 | x(mctx) |
| 141 | if mctx.Failed() { |
| 142 | return |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | type hooks struct { |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 149 | install []func(InstallHookContext) |
| 150 | } |