blob: 5e3a4a7e7a576fa8f30dc57d3151692a52a30aa9 [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
92func (l *loadHookContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
93 inherited := []interface{}{&l.Module().base().commonProperties}
Sam Delmerico286bf262022-03-09 20:46:37 +000094
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)
Colin Cross31a738b2019-12-30 18:45:15 -0800107
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.
Colin Cross43e789d2020-01-28 09:46:50 -0800114 proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(),
Colin Cross31a738b2019-12-30 18:45:15 -0800115 }
116 err := proptools.AppendMatchingProperties(dst, src, nil)
117 if err != nil {
118 panic(err)
119 }
120 }
121
122 return module
123}
124
Colin Cross9d34f352019-11-22 16:03:51 -0800125func (l *loadHookContext) registerScopedModuleType(name string, factory blueprint.ModuleFactory) {
126 l.bp.RegisterScopedModuleType(name, factory)
127}
128
Colin Cross178a5092016-09-13 13:42:32 -0700129type InstallHookContext interface {
130 ModuleContext
David Srbecky07656412020-06-04 01:26:16 +0100131 SrcPath() Path
Colin Cross70dda7e2019-10-01 22:05:35 -0700132 Path() InstallPath
Colin Cross178a5092016-09-13 13:42:32 -0700133 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
David Srbecky07656412020-06-04 01:26:16 +0100146 srcPath Path
Colin Cross70dda7e2019-10-01 22:05:35 -0700147 path InstallPath
Colin Cross178a5092016-09-13 13:42:32 -0700148 symlink bool
149}
150
David Srbecky07656412020-06-04 01:26:16 +0100151var _ InstallHookContext = &installHookContext{}
152
153func (x *installHookContext) SrcPath() Path {
154 return x.srcPath
155}
156
Colin Cross70dda7e2019-10-01 22:05:35 -0700157func (x *installHookContext) Path() InstallPath {
Colin Cross178a5092016-09-13 13:42:32 -0700158 return x.path
159}
160
161func (x *installHookContext) Symlink() bool {
162 return x.symlink
163}
164
David Srbecky07656412020-06-04 01:26:16 +0100165func (x *hooks) runInstallHooks(ctx ModuleContext, srcPath Path, path InstallPath, symlink bool) {
Colin Cross178a5092016-09-13 13:42:32 -0700166 if len(x.install) > 0 {
167 mctx := &installHookContext{
168 ModuleContext: ctx,
David Srbecky07656412020-06-04 01:26:16 +0100169 srcPath: srcPath,
Colin Cross178a5092016-09-13 13:42:32 -0700170 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 {
Colin Cross178a5092016-09-13 13:42:32 -0700183 install []func(InstallHookContext)
184}