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 ( |
| 18 | "github.com/google/blueprint" |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 19 | ) |
| 20 | |
| 21 | // This file implements hooks that external module types can use to inject logic into existing |
| 22 | // module types. Each hook takes an interface as a parameter so that new methods can be added |
| 23 | // to the interface without breaking existing module types. |
| 24 | |
| 25 | // Load hooks are run after the module's properties have been filled from the blueprint file, but |
| 26 | // before the module has been split into architecture variants, and before defaults modules have |
| 27 | // been applied. |
| 28 | type LoadHookContext interface { |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 29 | EarlyModuleContext |
| 30 | |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 31 | AppendProperties(...interface{}) |
| 32 | PrependProperties(...interface{}) |
Colin Cross | e003c4a | 2019-09-25 12:58:36 -0700 | [diff] [blame] | 33 | CreateModule(ModuleFactory, ...interface{}) Module |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 36 | func AddLoadHook(m blueprint.Module, hook func(LoadHookContext)) { |
| 37 | h := &m.(Module).base().hooks |
| 38 | h.load = append(h.load, hook) |
| 39 | } |
| 40 | |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 41 | func (x *hooks) runLoadHooks(ctx LoadHookContext, m *ModuleBase) { |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 42 | if len(x.load) > 0 { |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 43 | for _, x := range x.load { |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 44 | x(ctx) |
| 45 | if ctx.Failed() { |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 46 | return |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 52 | type InstallHookContext interface { |
| 53 | ModuleContext |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 54 | Path() InstallPath |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 55 | Symlink() bool |
| 56 | } |
| 57 | |
| 58 | // Install hooks are run after a module creates a rule to install a file or symlink. |
| 59 | // The installed path is available from InstallHookContext.Path(), and |
| 60 | // InstallHookContext.Symlink() will be true if it was a symlink. |
| 61 | func AddInstallHook(m blueprint.Module, hook func(InstallHookContext)) { |
| 62 | h := &m.(Module).base().hooks |
| 63 | h.install = append(h.install, hook) |
| 64 | } |
| 65 | |
| 66 | type installHookContext struct { |
| 67 | ModuleContext |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 68 | path InstallPath |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 69 | symlink bool |
| 70 | } |
| 71 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 72 | func (x *installHookContext) Path() InstallPath { |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 73 | return x.path |
| 74 | } |
| 75 | |
| 76 | func (x *installHookContext) Symlink() bool { |
| 77 | return x.symlink |
| 78 | } |
| 79 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 80 | func (x *hooks) runInstallHooks(ctx ModuleContext, path InstallPath, symlink bool) { |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 81 | if len(x.install) > 0 { |
| 82 | mctx := &installHookContext{ |
| 83 | ModuleContext: ctx, |
| 84 | path: path, |
| 85 | symlink: symlink, |
| 86 | } |
| 87 | for _, x := range x.install { |
| 88 | x(mctx) |
| 89 | if mctx.Failed() { |
| 90 | return |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | type hooks struct { |
| 97 | load []func(LoadHookContext) |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 98 | install []func(InstallHookContext) |
| 99 | } |
| 100 | |
Colin Cross | f8b860a | 2019-04-16 14:43:28 -0700 | [diff] [blame] | 101 | func registerLoadHookMutator(ctx RegisterMutatorsContext) { |
| 102 | ctx.TopDown("load_hooks", LoadHookMutator).Parallel() |
| 103 | } |
| 104 | |
Inseob Kim | c0907f1 | 2019-02-08 21:00:45 +0900 | [diff] [blame] | 105 | func LoadHookMutator(ctx TopDownMutatorContext) { |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 106 | if m, ok := ctx.Module().(Module); ok { |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 107 | m.base().commonProperties.DebugName = ctx.ModuleName() |
| 108 | |
Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 109 | // Cast through *topDownMutatorContext because AppendProperties is implemented |
| 110 | // on *topDownMutatorContext but not exposed through TopDownMutatorContext |
| 111 | var loadHookCtx LoadHookContext = ctx.(*topDownMutatorContext) |
Colin Cross | 519917d | 2017-11-02 16:35:56 -0700 | [diff] [blame] | 112 | m.base().hooks.runLoadHooks(loadHookCtx, m.base()) |
Colin Cross | 178a509 | 2016-09-13 13:42:32 -0700 | [diff] [blame] | 113 | } |
| 114 | } |