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 blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module { 93 return l.bp.CreateModule(factory, name, props...) 94} 95 96type createModuleContext interface { 97 Module() Module 98 createModule(blueprint.ModuleFactory, string, ...interface{}) blueprint.Module 99} 100 101func createModule(ctx createModuleContext, factory ModuleFactory, ext string, props ...interface{}) Module { 102 inherited := []interface{}{&ctx.Module().base().commonProperties} 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 } 113 typeName = typeName + "_" + ext 114 115 module := ctx.createModule(ModuleFactoryAdaptor(factory), typeName, append(inherited, props...)...).(Module) 116 117 if ctx.Module().base().variableProperties != nil && module.base().variableProperties != nil { 118 src := ctx.Module().base().variableProperties 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. 123 proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(), 124 } 125 err := proptools.AppendMatchingProperties(dst, src, nil) 126 if err != nil { 127 panic(err) 128 } 129 } 130 131 return module 132} 133 134func (l *loadHookContext) CreateModule(factory ModuleFactory, props ...interface{}) Module { 135 return createModule(l, factory, "_loadHookModule", props...) 136} 137 138func (l *loadHookContext) registerScopedModuleType(name string, factory blueprint.ModuleFactory) { 139 l.bp.RegisterScopedModuleType(name, factory) 140} 141 142type InstallHookContext interface { 143 ModuleContext 144 SrcPath() Path 145 Path() InstallPath 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. 152func AddInstallHook(m blueprint.Module, hook func(InstallHookContext)) { 153 h := &m.(Module).base().hooks 154 h.install = append(h.install, hook) 155} 156 157type installHookContext struct { 158 ModuleContext 159 srcPath Path 160 path InstallPath 161 symlink bool 162} 163 164var _ InstallHookContext = &installHookContext{} 165 166func (x *installHookContext) SrcPath() Path { 167 return x.srcPath 168} 169 170func (x *installHookContext) Path() InstallPath { 171 return x.path 172} 173 174func (x *installHookContext) Symlink() bool { 175 return x.symlink 176} 177 178func (x *hooks) runInstallHooks(ctx ModuleContext, srcPath Path, path InstallPath, symlink bool) { 179 if len(x.install) > 0 { 180 mctx := &installHookContext{ 181 ModuleContext: ctx, 182 srcPath: srcPath, 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 195type hooks struct { 196 install []func(InstallHookContext) 197} 198