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 ( |
Sam Delmerico | 286bf26 | 2022-03-09 20:46:37 +0000 | [diff] [blame] | 18 | "fmt" |
| 19 | "path" |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 20 | "reflect" |
Sam Delmerico | 286bf26 | 2022-03-09 20:46:37 +0000 | [diff] [blame] | 21 | "runtime" |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 22 | |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint" |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | // This file implements hooks that external module types can use to inject logic into existing |
| 28 | // module types. Each hook takes an interface as a parameter so that new methods can be added |
| 29 | // to the interface without breaking existing module types. |
| 30 | |
| 31 | // Load hooks are run after the module's properties have been filled from the blueprint file, but |
| 32 | // before the module has been split into architecture variants, and before defaults modules have |
| 33 | // been applied. |
| 34 | type LoadHookContext interface { |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 35 | EarlyModuleContext |
| 36 | |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 37 | AppendProperties(...interface{}) |
| 38 | PrependProperties(...interface{}) |
Colin Cross | e003c4a | 2019-09-25 12:58:36 -0700 | [diff] [blame] | 39 | CreateModule(ModuleFactory, ...interface{}) Module |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 40 | |
| 41 | registerScopedModuleType(name string, factory blueprint.ModuleFactory) |
| 42 | moduleFactories() map[string]blueprint.ModuleFactory |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 45 | // Add a hook that will be called once the module has been loaded, i.e. its |
| 46 | // properties have been initialized from the Android.bp file. |
| 47 | // |
| 48 | // Consider using SetDefaultableHook to register a hook for any module that implements |
| 49 | // DefaultableModule as the hook is called after any defaults have been applied to the |
| 50 | // module which could reduce duplication and make it easier to use. |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 51 | func AddLoadHook(m blueprint.Module, hook func(LoadHookContext)) { |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 52 | blueprint.AddLoadHook(m, func(ctx blueprint.LoadHookContext) { |
| 53 | actx := &loadHookContext{ |
| 54 | earlyModuleContext: m.(Module).base().earlyModuleContextFactory(ctx), |
| 55 | bp: ctx, |
| 56 | } |
| 57 | hook(actx) |
| 58 | }) |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 61 | type loadHookContext struct { |
| 62 | earlyModuleContext |
| 63 | bp blueprint.LoadHookContext |
| 64 | module Module |
| 65 | } |
| 66 | |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 67 | func (l *loadHookContext) moduleFactories() map[string]blueprint.ModuleFactory { |
| 68 | return l.bp.ModuleFactories() |
| 69 | } |
| 70 | |
Usta | ef3676c | 2021-11-23 12:31:55 -0500 | [diff] [blame] | 71 | func (l *loadHookContext) appendPrependHelper(props []interface{}, |
| 72 | extendFn func([]interface{}, interface{}, proptools.ExtendPropertyFilterFunc) error) { |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 73 | for _, p := range props { |
Usta | 851a327 | 2022-01-05 23:42:33 -0500 | [diff] [blame] | 74 | err := extendFn(l.Module().base().GetProperties(), p, nil) |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 75 | if err != nil { |
| 76 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 77 | l.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 78 | } else { |
| 79 | panic(err) |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
Usta | ef3676c | 2021-11-23 12:31:55 -0500 | [diff] [blame] | 84 | func (l *loadHookContext) AppendProperties(props ...interface{}) { |
| 85 | l.appendPrependHelper(props, proptools.AppendMatchingProperties) |
| 86 | } |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 87 | |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 88 | func (l *loadHookContext) PrependProperties(props ...interface{}) { |
Usta | ef3676c | 2021-11-23 12:31:55 -0500 | [diff] [blame] | 89 | l.appendPrependHelper(props, proptools.PrependMatchingProperties) |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 92 | func (l *loadHookContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module { |
| 93 | return l.bp.CreateModule(factory, name, props...) |
| 94 | } |
| 95 | |
| 96 | type createModuleContext interface { |
| 97 | Module() Module |
| 98 | createModule(blueprint.ModuleFactory, string, ...interface{}) blueprint.Module |
| 99 | } |
| 100 | |
| 101 | func createModule(ctx createModuleContext, factory ModuleFactory, ext string, props ...interface{}) Module { |
| 102 | inherited := []interface{}{&ctx.Module().base().commonProperties} |
Sam Delmerico | 286bf26 | 2022-03-09 20:46:37 +0000 | [diff] [blame] | 103 | |
| 104 | var typeName string |
| 105 | if typeNameLookup, ok := ModuleTypeByFactory()[reflect.ValueOf(factory)]; ok { |
| 106 | typeName = typeNameLookup |
| 107 | } else { |
| 108 | factoryPtr := reflect.ValueOf(factory).Pointer() |
| 109 | factoryFunc := runtime.FuncForPC(factoryPtr) |
| 110 | filePath, _ := factoryFunc.FileLine(factoryPtr) |
| 111 | typeName = fmt.Sprintf("%s_%s", path.Base(filePath), factoryFunc.Name()) |
| 112 | } |
Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 113 | typeName = typeName + "_" + ext |
Sam Delmerico | 286bf26 | 2022-03-09 20:46:37 +0000 | [diff] [blame] | 114 | |
Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 115 | module := ctx.createModule(ModuleFactoryAdaptor(factory), typeName, append(inherited, props...)...).(Module) |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 116 | |
Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 117 | if ctx.Module().base().variableProperties != nil && module.base().variableProperties != nil { |
| 118 | src := ctx.Module().base().variableProperties |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 119 | dst := []interface{}{ |
| 120 | module.base().variableProperties, |
| 121 | // Put an empty copy of the src properties into dst so that properties in src that are not in dst |
| 122 | // don't cause a "failed to find property to extend" error. |
Colin Cross | 43e789d | 2020-01-28 09:46:50 -0800 | [diff] [blame] | 123 | proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(), |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 124 | } |
| 125 | err := proptools.AppendMatchingProperties(dst, src, nil) |
| 126 | if err != nil { |
| 127 | panic(err) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return module |
| 132 | } |
| 133 | |
Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 134 | func (l *loadHookContext) CreateModule(factory ModuleFactory, props ...interface{}) Module { |
| 135 | return createModule(l, factory, "_loadHookModule", props...) |
| 136 | } |
| 137 | |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 138 | func (l *loadHookContext) registerScopedModuleType(name string, factory blueprint.ModuleFactory) { |
| 139 | l.bp.RegisterScopedModuleType(name, factory) |
| 140 | } |
| 141 | |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 142 | type InstallHookContext interface { |
| 143 | ModuleContext |
David Srbecky | 0765641 | 2020-06-04 01:26:16 +0100 | [diff] [blame] | 144 | SrcPath() Path |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 145 | Path() InstallPath |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 146 | Symlink() bool |
| 147 | } |
| 148 | |
| 149 | // Install hooks are run after a module creates a rule to install a file or symlink. |
| 150 | // The installed path is available from InstallHookContext.Path(), and |
| 151 | // InstallHookContext.Symlink() will be true if it was a symlink. |
| 152 | func AddInstallHook(m blueprint.Module, hook func(InstallHookContext)) { |
| 153 | h := &m.(Module).base().hooks |
| 154 | h.install = append(h.install, hook) |
| 155 | } |
| 156 | |
| 157 | type installHookContext struct { |
| 158 | ModuleContext |
David Srbecky | 0765641 | 2020-06-04 01:26:16 +0100 | [diff] [blame] | 159 | srcPath Path |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 160 | path InstallPath |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 161 | symlink bool |
| 162 | } |
| 163 | |
David Srbecky | 0765641 | 2020-06-04 01:26:16 +0100 | [diff] [blame] | 164 | var _ InstallHookContext = &installHookContext{} |
| 165 | |
| 166 | func (x *installHookContext) SrcPath() Path { |
| 167 | return x.srcPath |
| 168 | } |
| 169 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 170 | func (x *installHookContext) Path() InstallPath { |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 171 | return x.path |
| 172 | } |
| 173 | |
| 174 | func (x *installHookContext) Symlink() bool { |
| 175 | return x.symlink |
| 176 | } |
| 177 | |
David Srbecky | 0765641 | 2020-06-04 01:26:16 +0100 | [diff] [blame] | 178 | func (x *hooks) runInstallHooks(ctx ModuleContext, srcPath Path, path InstallPath, symlink bool) { |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 179 | if len(x.install) > 0 { |
| 180 | mctx := &installHookContext{ |
| 181 | ModuleContext: ctx, |
David Srbecky | 0765641 | 2020-06-04 01:26:16 +0100 | [diff] [blame] | 182 | srcPath: srcPath, |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 183 | path: path, |
| 184 | symlink: symlink, |
| 185 | } |
| 186 | for _, x := range x.install { |
| 187 | x(mctx) |
| 188 | if mctx.Failed() { |
| 189 | return |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | type hooks struct { |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 196 | install []func(InstallHookContext) |
| 197 | } |