blob: 2ad3b5fa59c66a402eda32f0d86ae3149f41cf82 [file] [log] [blame]
Colin Cross178a5092016-09-13 13:42:32 -07001// 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 (
Sam Delmerico286bf262022-03-09 20:46:37 +000018 "fmt"
19 "path"
Colin Cross31a738b2019-12-30 18:45:15 -080020 "reflect"
Sam Delmerico286bf262022-03-09 20:46:37 +000021 "runtime"
Colin Cross31a738b2019-12-30 18:45:15 -080022
Colin Cross178a5092016-09-13 13:42:32 -070023 "github.com/google/blueprint"
Colin Cross31a738b2019-12-30 18:45:15 -080024 "github.com/google/blueprint/proptools"
Colin Cross178a5092016-09-13 13:42:32 -070025)
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 {
Colin Cross1184b642019-12-30 18:43:07 -080035 EarlyModuleContext
36
Colin Cross178a5092016-09-13 13:42:32 -070037 AppendProperties(...interface{})
38 PrependProperties(...interface{})
Colin Crosse003c4a2019-09-25 12:58:36 -070039 CreateModule(ModuleFactory, ...interface{}) Module
Colin Cross9d34f352019-11-22 16:03:51 -080040
41 registerScopedModuleType(name string, factory blueprint.ModuleFactory)
42 moduleFactories() map[string]blueprint.ModuleFactory
Colin Cross178a5092016-09-13 13:42:32 -070043}
44
Paul Duffinafa9fa12020-04-29 16:47:28 +010045// 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 Cross178a5092016-09-13 13:42:32 -070051func AddLoadHook(m blueprint.Module, hook func(LoadHookContext)) {
Colin Cross31a738b2019-12-30 18:45:15 -080052 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 Cross178a5092016-09-13 13:42:32 -070059}
60
Colin Cross31a738b2019-12-30 18:45:15 -080061type loadHookContext struct {
62 earlyModuleContext
63 bp blueprint.LoadHookContext
64 module Module
65}
66
Colin Cross9d34f352019-11-22 16:03:51 -080067func (l *loadHookContext) moduleFactories() map[string]blueprint.ModuleFactory {
68 return l.bp.ModuleFactories()
69}
70
Ustaef3676c2021-11-23 12:31:55 -050071func (l *loadHookContext) appendPrependHelper(props []interface{},
72 extendFn func([]interface{}, interface{}, proptools.ExtendPropertyFilterFunc) error) {
Colin Cross31a738b2019-12-30 18:45:15 -080073 for _, p := range props {
Usta851a3272022-01-05 23:42:33 -050074 err := extendFn(l.Module().base().GetProperties(), p, nil)
Colin Cross31a738b2019-12-30 18:45:15 -080075 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 Cross178a5092016-09-13 13:42:32 -070080 }
81 }
82 }
83}
Ustaef3676c2021-11-23 12:31:55 -050084func (l *loadHookContext) AppendProperties(props ...interface{}) {
85 l.appendPrependHelper(props, proptools.AppendMatchingProperties)
86}
Colin Cross178a5092016-09-13 13:42:32 -070087
Colin Cross31a738b2019-12-30 18:45:15 -080088func (l *loadHookContext) PrependProperties(props ...interface{}) {
Ustaef3676c2021-11-23 12:31:55 -050089 l.appendPrependHelper(props, proptools.PrependMatchingProperties)
Colin Cross31a738b2019-12-30 18:45:15 -080090}
91
Liz Kammerf31c9002022-04-26 09:08:55 -040092func (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}
Sam Delmerico286bf262022-03-09 20:46:37 +0000103
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 Kammerf31c9002022-04-26 09:08:55 -0400113 typeName = typeName + "_" + ext
Sam Delmerico286bf262022-03-09 20:46:37 +0000114
Liz Kammerf31c9002022-04-26 09:08:55 -0400115 module := ctx.createModule(ModuleFactoryAdaptor(factory), typeName, append(inherited, props...)...).(Module)
Colin Cross31a738b2019-12-30 18:45:15 -0800116
Liz Kammerf31c9002022-04-26 09:08:55 -0400117 if ctx.Module().base().variableProperties != nil && module.base().variableProperties != nil {
118 src := ctx.Module().base().variableProperties
Colin Cross31a738b2019-12-30 18:45:15 -0800119 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 Cross43e789d2020-01-28 09:46:50 -0800123 proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(),
Colin Cross31a738b2019-12-30 18:45:15 -0800124 }
125 err := proptools.AppendMatchingProperties(dst, src, nil)
126 if err != nil {
127 panic(err)
128 }
129 }
130
131 return module
132}
133
Liz Kammerf31c9002022-04-26 09:08:55 -0400134func (l *loadHookContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
135 return createModule(l, factory, "_loadHookModule", props...)
136}
137
Colin Cross9d34f352019-11-22 16:03:51 -0800138func (l *loadHookContext) registerScopedModuleType(name string, factory blueprint.ModuleFactory) {
139 l.bp.RegisterScopedModuleType(name, factory)
140}
141
Colin Cross178a5092016-09-13 13:42:32 -0700142type InstallHookContext interface {
143 ModuleContext
David Srbecky07656412020-06-04 01:26:16 +0100144 SrcPath() Path
Colin Cross70dda7e2019-10-01 22:05:35 -0700145 Path() InstallPath
Colin Cross178a5092016-09-13 13:42:32 -0700146 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
David Srbecky07656412020-06-04 01:26:16 +0100159 srcPath Path
Colin Cross70dda7e2019-10-01 22:05:35 -0700160 path InstallPath
Colin Cross178a5092016-09-13 13:42:32 -0700161 symlink bool
162}
163
David Srbecky07656412020-06-04 01:26:16 +0100164var _ InstallHookContext = &installHookContext{}
165
166func (x *installHookContext) SrcPath() Path {
167 return x.srcPath
168}
169
Colin Cross70dda7e2019-10-01 22:05:35 -0700170func (x *installHookContext) Path() InstallPath {
Colin Cross178a5092016-09-13 13:42:32 -0700171 return x.path
172}
173
174func (x *installHookContext) Symlink() bool {
175 return x.symlink
176}
177
David Srbecky07656412020-06-04 01:26:16 +0100178func (x *hooks) runInstallHooks(ctx ModuleContext, srcPath Path, path InstallPath, symlink bool) {
Colin Cross178a5092016-09-13 13:42:32 -0700179 if len(x.install) > 0 {
180 mctx := &installHookContext{
181 ModuleContext: ctx,
David Srbecky07656412020-06-04 01:26:16 +0100182 srcPath: srcPath,
Colin Cross178a5092016-09-13 13:42:32 -0700183 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 {
Colin Cross178a5092016-09-13 13:42:32 -0700196 install []func(InstallHookContext)
197}