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 15package android 16 17import ( 18 "fmt" 19 "path" 20 "reflect" 21 "runtime" 22 23 "github.com/google/blueprint" 24 "github.com/google/blueprint/proptools" 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. 34type LoadHookContext interface { 35 EarlyModuleContext 36 37 AppendProperties(...interface{}) 38 PrependProperties(...interface{}) 39 CreateModule(ModuleFactory, ...interface{}) Module 40 41 registerScopedModuleType(name string, factory blueprint.ModuleFactory) 42 moduleFactories() map[string]blueprint.ModuleFactory 43} 44 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. 51func AddLoadHook(m blueprint.Module, hook func(LoadHookContext)) { 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 }) 59} 60 61type loadHookContext struct { 62 earlyModuleContext 63 bp blueprint.LoadHookContext 64 module Module 65} 66 67func (l *loadHookContext) moduleFactories() map[string]blueprint.ModuleFactory { 68 return l.bp.ModuleFactories() 69} 70 71func (l *loadHookContext) appendPrependHelper(props []interface{}, 72 extendFn func([]interface{}, interface{}, proptools.ExtendPropertyFilterFunc) error) { 73 for _, p := range props { 74 err := extendFn(l.Module().base().GetProperties(), p, nil) 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) 80 } 81 } 82 } 83} 84func (l *loadHookContext) AppendProperties(props ...interface{}) { 85 l.appendPrependHelper(props, proptools.AppendMatchingProperties) 86} 87 88func (l *loadHookContext) PrependProperties(props ...interface{}) { 89 l.appendPrependHelper(props, proptools.PrependMatchingProperties) 90} 91 92func (l *loadHookContext) CreateModule(factory ModuleFactory, props ...interface{}) Module { 93 inherited := []interface{}{&l.Module().base().commonProperties} 94 95 var typeName string 96 if typeNameLookup, ok := ModuleTypeByFactory()[reflect.ValueOf(factory)]; ok { 97 typeName = typeNameLookup 98 } else { 99 factoryPtr := reflect.ValueOf(factory).Pointer() 100 factoryFunc := runtime.FuncForPC(factoryPtr) 101 filePath, _ := factoryFunc.FileLine(factoryPtr) 102 typeName = fmt.Sprintf("%s_%s", path.Base(filePath), factoryFunc.Name()) 103 } 104 typeName = typeName + "_loadHookModule" 105 106 module := l.bp.CreateModule(ModuleFactoryAdaptor(factory), typeName, append(inherited, props...)...).(Module) 107 108 if l.Module().base().variableProperties != nil && module.base().variableProperties != nil { 109 src := l.Module().base().variableProperties 110 dst := []interface{}{ 111 module.base().variableProperties, 112 // Put an empty copy of the src properties into dst so that properties in src that are not in dst 113 // don't cause a "failed to find property to extend" error. 114 proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(), 115 } 116 err := proptools.AppendMatchingProperties(dst, src, nil) 117 if err != nil { 118 panic(err) 119 } 120 } 121 122 return module 123} 124 125func (l *loadHookContext) registerScopedModuleType(name string, factory blueprint.ModuleFactory) { 126 l.bp.RegisterScopedModuleType(name, factory) 127} 128 129type InstallHookContext interface { 130 ModuleContext 131 SrcPath() Path 132 Path() InstallPath 133 Symlink() bool 134} 135 136// Install hooks are run after a module creates a rule to install a file or symlink. 137// The installed path is available from InstallHookContext.Path(), and 138// InstallHookContext.Symlink() will be true if it was a symlink. 139func AddInstallHook(m blueprint.Module, hook func(InstallHookContext)) { 140 h := &m.(Module).base().hooks 141 h.install = append(h.install, hook) 142} 143 144type installHookContext struct { 145 ModuleContext 146 srcPath Path 147 path InstallPath 148 symlink bool 149} 150 151var _ InstallHookContext = &installHookContext{} 152 153func (x *installHookContext) SrcPath() Path { 154 return x.srcPath 155} 156 157func (x *installHookContext) Path() InstallPath { 158 return x.path 159} 160 161func (x *installHookContext) Symlink() bool { 162 return x.symlink 163} 164 165func (x *hooks) runInstallHooks(ctx ModuleContext, srcPath Path, path InstallPath, symlink bool) { 166 if len(x.install) > 0 { 167 mctx := &installHookContext{ 168 ModuleContext: ctx, 169 srcPath: srcPath, 170 path: path, 171 symlink: symlink, 172 } 173 for _, x := range x.install { 174 x(mctx) 175 if mctx.Failed() { 176 return 177 } 178 } 179 } 180} 181 182type hooks struct { 183 install []func(InstallHookContext) 184} 185