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 | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 37 | |
| 38 | registerScopedModuleType(name string, factory blueprint.ModuleFactory) |
| 39 | moduleFactories() map[string]blueprint.ModuleFactory |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 42 | func AddLoadHook(m blueprint.Module, hook func(LoadHookContext)) { |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 43 | blueprint.AddLoadHook(m, func(ctx blueprint.LoadHookContext) { |
| 44 | actx := &loadHookContext{ |
| 45 | earlyModuleContext: m.(Module).base().earlyModuleContextFactory(ctx), |
| 46 | bp: ctx, |
| 47 | } |
| 48 | hook(actx) |
| 49 | }) |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 52 | type loadHookContext struct { |
| 53 | earlyModuleContext |
| 54 | bp blueprint.LoadHookContext |
| 55 | module Module |
| 56 | } |
| 57 | |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 58 | func (l *loadHookContext) moduleFactories() map[string]blueprint.ModuleFactory { |
| 59 | return l.bp.ModuleFactories() |
| 60 | } |
| 61 | |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 62 | func (l *loadHookContext) AppendProperties(props ...interface{}) { |
| 63 | for _, p := range props { |
| 64 | err := proptools.AppendMatchingProperties(l.Module().base().customizableProperties, |
| 65 | p, nil) |
| 66 | if err != nil { |
| 67 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 68 | l.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 69 | } else { |
| 70 | panic(err) |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 76 | func (l *loadHookContext) PrependProperties(props ...interface{}) { |
| 77 | for _, p := range props { |
| 78 | err := proptools.PrependMatchingProperties(l.Module().base().customizableProperties, |
| 79 | p, nil) |
| 80 | if err != nil { |
| 81 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 82 | l.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 83 | } else { |
| 84 | panic(err) |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func (l *loadHookContext) CreateModule(factory ModuleFactory, props ...interface{}) Module { |
| 91 | inherited := []interface{}{&l.Module().base().commonProperties} |
| 92 | module := l.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module) |
| 93 | |
| 94 | if l.Module().base().variableProperties != nil && module.base().variableProperties != nil { |
| 95 | src := l.Module().base().variableProperties |
| 96 | dst := []interface{}{ |
| 97 | module.base().variableProperties, |
| 98 | // Put an empty copy of the src properties into dst so that properties in src that are not in dst |
| 99 | // don't cause a "failed to find property to extend" error. |
| 100 | proptools.CloneEmptyProperties(reflect.ValueOf(src).Elem()).Interface(), |
| 101 | } |
| 102 | err := proptools.AppendMatchingProperties(dst, src, nil) |
| 103 | if err != nil { |
| 104 | panic(err) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return module |
| 109 | } |
| 110 | |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 111 | func (l *loadHookContext) registerScopedModuleType(name string, factory blueprint.ModuleFactory) { |
| 112 | l.bp.RegisterScopedModuleType(name, factory) |
| 113 | } |
| 114 | |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 115 | type InstallHookContext interface { |
| 116 | ModuleContext |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 117 | Path() InstallPath |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 118 | Symlink() bool |
| 119 | } |
| 120 | |
| 121 | // Install hooks are run after a module creates a rule to install a file or symlink. |
| 122 | // The installed path is available from InstallHookContext.Path(), and |
| 123 | // InstallHookContext.Symlink() will be true if it was a symlink. |
| 124 | func AddInstallHook(m blueprint.Module, hook func(InstallHookContext)) { |
| 125 | h := &m.(Module).base().hooks |
| 126 | h.install = append(h.install, hook) |
| 127 | } |
| 128 | |
| 129 | type installHookContext struct { |
| 130 | ModuleContext |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 131 | path InstallPath |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 132 | symlink bool |
| 133 | } |
| 134 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 135 | func (x *installHookContext) Path() InstallPath { |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 136 | return x.path |
| 137 | } |
| 138 | |
| 139 | func (x *installHookContext) Symlink() bool { |
| 140 | return x.symlink |
| 141 | } |
| 142 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 143 | func (x *hooks) runInstallHooks(ctx ModuleContext, path InstallPath, symlink bool) { |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 144 | if len(x.install) > 0 { |
| 145 | mctx := &installHookContext{ |
| 146 | ModuleContext: ctx, |
| 147 | path: path, |
| 148 | symlink: symlink, |
| 149 | } |
| 150 | for _, x := range x.install { |
| 151 | x(mctx) |
| 152 | if mctx.Failed() { |
| 153 | return |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | type hooks struct { |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 160 | install []func(InstallHookContext) |
| 161 | } |