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 |
Jihoon Kang | 222874d | 2024-10-23 23:38:05 +0000 | [diff] [blame^] | 40 | CreateModuleInDirectory(ModuleFactory, string, ...interface{}) Module |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 41 | |
| 42 | registerScopedModuleType(name string, factory blueprint.ModuleFactory) |
| 43 | moduleFactories() map[string]blueprint.ModuleFactory |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 46 | // Add a hook that will be called once the module has been loaded, i.e. its |
| 47 | // properties have been initialized from the Android.bp file. |
| 48 | // |
| 49 | // Consider using SetDefaultableHook to register a hook for any module that implements |
| 50 | // DefaultableModule as the hook is called after any defaults have been applied to the |
| 51 | // module which could reduce duplication and make it easier to use. |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 52 | func AddLoadHook(m blueprint.Module, hook func(LoadHookContext)) { |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 53 | blueprint.AddLoadHook(m, func(ctx blueprint.LoadHookContext) { |
| 54 | actx := &loadHookContext{ |
| 55 | earlyModuleContext: m.(Module).base().earlyModuleContextFactory(ctx), |
| 56 | bp: ctx, |
| 57 | } |
| 58 | hook(actx) |
| 59 | }) |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 62 | type loadHookContext struct { |
| 63 | earlyModuleContext |
| 64 | bp blueprint.LoadHookContext |
| 65 | module Module |
| 66 | } |
| 67 | |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 68 | func (l *loadHookContext) moduleFactories() map[string]blueprint.ModuleFactory { |
| 69 | return l.bp.ModuleFactories() |
| 70 | } |
| 71 | |
Usta | ef3676c | 2021-11-23 12:31:55 -0500 | [diff] [blame] | 72 | func (l *loadHookContext) appendPrependHelper(props []interface{}, |
| 73 | extendFn func([]interface{}, interface{}, proptools.ExtendPropertyFilterFunc) error) { |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 74 | for _, p := range props { |
Usta | 851a327 | 2022-01-05 23:42:33 -0500 | [diff] [blame] | 75 | err := extendFn(l.Module().base().GetProperties(), p, nil) |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 76 | if err != nil { |
| 77 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 78 | l.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 79 | } else { |
| 80 | panic(err) |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
Usta | ef3676c | 2021-11-23 12:31:55 -0500 | [diff] [blame] | 85 | func (l *loadHookContext) AppendProperties(props ...interface{}) { |
| 86 | l.appendPrependHelper(props, proptools.AppendMatchingProperties) |
| 87 | } |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 88 | |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 89 | func (l *loadHookContext) PrependProperties(props ...interface{}) { |
Usta | ef3676c | 2021-11-23 12:31:55 -0500 | [diff] [blame] | 90 | l.appendPrependHelper(props, proptools.PrependMatchingProperties) |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 93 | func (l *loadHookContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module { |
| 94 | return l.bp.CreateModule(factory, name, props...) |
| 95 | } |
| 96 | |
Jihoon Kang | 222874d | 2024-10-23 23:38:05 +0000 | [diff] [blame^] | 97 | func (l *loadHookContext) createModuleInDirectory(factory blueprint.ModuleFactory, name, moduleDir string, props ...interface{}) blueprint.Module { |
| 98 | return l.bp.CreateModuleInDirectory(factory, name, moduleDir, props...) |
| 99 | } |
| 100 | |
| 101 | type specifyDirectory struct { |
| 102 | specified bool |
| 103 | directory string |
| 104 | } |
| 105 | |
| 106 | func doesNotSpecifyDirectory() specifyDirectory { |
| 107 | return specifyDirectory{ |
| 108 | specified: false, |
| 109 | directory: "", |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func specifiesDirectory(directory string) specifyDirectory { |
| 114 | return specifyDirectory{ |
| 115 | specified: true, |
| 116 | directory: directory, |
| 117 | } |
| 118 | } |
| 119 | |
Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 120 | type createModuleContext interface { |
| 121 | Module() Module |
Cole Faust | b1ccc2f | 2024-09-27 11:21:53 -0700 | [diff] [blame] | 122 | HasMutatorFinished(mutatorName string) bool |
Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 123 | createModule(blueprint.ModuleFactory, string, ...interface{}) blueprint.Module |
Jihoon Kang | 222874d | 2024-10-23 23:38:05 +0000 | [diff] [blame^] | 124 | createModuleInDirectory(blueprint.ModuleFactory, string, string, ...interface{}) blueprint.Module |
Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 125 | } |
| 126 | |
Jihoon Kang | 222874d | 2024-10-23 23:38:05 +0000 | [diff] [blame^] | 127 | func createModule(ctx createModuleContext, factory ModuleFactory, ext string, specifyDirectory specifyDirectory, props ...interface{}) Module { |
Cole Faust | b1ccc2f | 2024-09-27 11:21:53 -0700 | [diff] [blame] | 128 | if ctx.HasMutatorFinished("defaults") { |
| 129 | // Creating modules late is oftentimes problematic, because they don't have earlier |
| 130 | // mutators run on them. Prevent making modules after the defaults mutator has run. |
| 131 | panic("Cannot create a module after the defaults mutator has finished") |
| 132 | } |
| 133 | |
Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 134 | inherited := []interface{}{&ctx.Module().base().commonProperties} |
Sam Delmerico | 286bf26 | 2022-03-09 20:46:37 +0000 | [diff] [blame] | 135 | |
| 136 | var typeName string |
| 137 | if typeNameLookup, ok := ModuleTypeByFactory()[reflect.ValueOf(factory)]; ok { |
| 138 | typeName = typeNameLookup |
| 139 | } else { |
| 140 | factoryPtr := reflect.ValueOf(factory).Pointer() |
| 141 | factoryFunc := runtime.FuncForPC(factoryPtr) |
| 142 | filePath, _ := factoryFunc.FileLine(factoryPtr) |
| 143 | typeName = fmt.Sprintf("%s_%s", path.Base(filePath), factoryFunc.Name()) |
| 144 | } |
Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 145 | typeName = typeName + "_" + ext |
Sam Delmerico | 286bf26 | 2022-03-09 20:46:37 +0000 | [diff] [blame] | 146 | |
Jihoon Kang | 222874d | 2024-10-23 23:38:05 +0000 | [diff] [blame^] | 147 | var module Module |
| 148 | if specifyDirectory.specified { |
| 149 | module = ctx.createModuleInDirectory(ModuleFactoryAdaptor(factory), typeName, specifyDirectory.directory, append(inherited, props...)...).(Module) |
| 150 | } else { |
| 151 | module = ctx.createModule(ModuleFactoryAdaptor(factory), typeName, append(inherited, props...)...).(Module) |
| 152 | } |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 153 | |
Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 154 | if ctx.Module().base().variableProperties != nil && module.base().variableProperties != nil { |
| 155 | src := ctx.Module().base().variableProperties |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 156 | dst := []interface{}{ |
| 157 | module.base().variableProperties, |
| 158 | // Put an empty copy of the src properties into dst so that properties in src that are not in dst |
| 159 | // don't cause a "failed to find property to extend" error. |
Colin Cross | 43e789d | 2020-01-28 09:46:50 -0800 | [diff] [blame] | 160 | proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(), |
Colin Cross | 31a738b | 2019-12-30 18:45:15 -0800 | [diff] [blame] | 161 | } |
| 162 | err := proptools.AppendMatchingProperties(dst, src, nil) |
| 163 | if err != nil { |
| 164 | panic(err) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return module |
| 169 | } |
| 170 | |
Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 171 | func (l *loadHookContext) CreateModule(factory ModuleFactory, props ...interface{}) Module { |
Jihoon Kang | 222874d | 2024-10-23 23:38:05 +0000 | [diff] [blame^] | 172 | return createModule(l, factory, "_loadHookModule", doesNotSpecifyDirectory(), props...) |
| 173 | } |
| 174 | |
| 175 | func (l *loadHookContext) CreateModuleInDirectory(factory ModuleFactory, directory string, props ...interface{}) Module { |
| 176 | return createModule(l, factory, "_loadHookModule", specifiesDirectory(directory), props...) |
Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 179 | func (l *loadHookContext) registerScopedModuleType(name string, factory blueprint.ModuleFactory) { |
| 180 | l.bp.RegisterScopedModuleType(name, factory) |
| 181 | } |
| 182 | |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 183 | type InstallHookContext interface { |
| 184 | ModuleContext |
David Srbecky | 0765641 | 2020-06-04 01:26:16 +0100 | [diff] [blame] | 185 | SrcPath() Path |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 186 | Path() InstallPath |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 187 | Symlink() bool |
| 188 | } |
| 189 | |
| 190 | // Install hooks are run after a module creates a rule to install a file or symlink. |
| 191 | // The installed path is available from InstallHookContext.Path(), and |
| 192 | // InstallHookContext.Symlink() will be true if it was a symlink. |
| 193 | func AddInstallHook(m blueprint.Module, hook func(InstallHookContext)) { |
| 194 | h := &m.(Module).base().hooks |
| 195 | h.install = append(h.install, hook) |
| 196 | } |
| 197 | |
| 198 | type installHookContext struct { |
| 199 | ModuleContext |
David Srbecky | 0765641 | 2020-06-04 01:26:16 +0100 | [diff] [blame] | 200 | srcPath Path |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 201 | path InstallPath |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 202 | symlink bool |
| 203 | } |
| 204 | |
David Srbecky | 0765641 | 2020-06-04 01:26:16 +0100 | [diff] [blame] | 205 | var _ InstallHookContext = &installHookContext{} |
| 206 | |
| 207 | func (x *installHookContext) SrcPath() Path { |
| 208 | return x.srcPath |
| 209 | } |
| 210 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 211 | func (x *installHookContext) Path() InstallPath { |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 212 | return x.path |
| 213 | } |
| 214 | |
| 215 | func (x *installHookContext) Symlink() bool { |
| 216 | return x.symlink |
| 217 | } |
| 218 | |
David Srbecky | 0765641 | 2020-06-04 01:26:16 +0100 | [diff] [blame] | 219 | func (x *hooks) runInstallHooks(ctx ModuleContext, srcPath Path, path InstallPath, symlink bool) { |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 220 | if len(x.install) > 0 { |
| 221 | mctx := &installHookContext{ |
| 222 | ModuleContext: ctx, |
David Srbecky | 0765641 | 2020-06-04 01:26:16 +0100 | [diff] [blame] | 223 | srcPath: srcPath, |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 224 | path: path, |
| 225 | symlink: symlink, |
| 226 | } |
| 227 | for _, x := range x.install { |
| 228 | x(mctx) |
| 229 | if mctx.Failed() { |
| 230 | return |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | type hooks struct { |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 237 | install []func(InstallHookContext) |
| 238 | } |