blob: bd2fa5e755605ce74e70f4b196eafd762c6384fc [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
Cole Faustb1ccc2f2024-09-27 11:21:53 -070098 HasMutatorFinished(mutatorName string) bool
Liz Kammerf31c9002022-04-26 09:08:55 -040099 createModule(blueprint.ModuleFactory, string, ...interface{}) blueprint.Module
100}
101
102func createModule(ctx createModuleContext, factory ModuleFactory, ext string, props ...interface{}) Module {
Cole Faustb1ccc2f2024-09-27 11:21:53 -0700103 if ctx.HasMutatorFinished("defaults") {
104 // Creating modules late is oftentimes problematic, because they don't have earlier
105 // mutators run on them. Prevent making modules after the defaults mutator has run.
106 panic("Cannot create a module after the defaults mutator has finished")
107 }
108
Liz Kammerf31c9002022-04-26 09:08:55 -0400109 inherited := []interface{}{&ctx.Module().base().commonProperties}
Sam Delmerico286bf262022-03-09 20:46:37 +0000110
111 var typeName string
112 if typeNameLookup, ok := ModuleTypeByFactory()[reflect.ValueOf(factory)]; ok {
113 typeName = typeNameLookup
114 } else {
115 factoryPtr := reflect.ValueOf(factory).Pointer()
116 factoryFunc := runtime.FuncForPC(factoryPtr)
117 filePath, _ := factoryFunc.FileLine(factoryPtr)
118 typeName = fmt.Sprintf("%s_%s", path.Base(filePath), factoryFunc.Name())
119 }
Liz Kammerf31c9002022-04-26 09:08:55 -0400120 typeName = typeName + "_" + ext
Sam Delmerico286bf262022-03-09 20:46:37 +0000121
Liz Kammerf31c9002022-04-26 09:08:55 -0400122 module := ctx.createModule(ModuleFactoryAdaptor(factory), typeName, append(inherited, props...)...).(Module)
Colin Cross31a738b2019-12-30 18:45:15 -0800123
Liz Kammerf31c9002022-04-26 09:08:55 -0400124 if ctx.Module().base().variableProperties != nil && module.base().variableProperties != nil {
125 src := ctx.Module().base().variableProperties
Colin Cross31a738b2019-12-30 18:45:15 -0800126 dst := []interface{}{
127 module.base().variableProperties,
128 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
129 // don't cause a "failed to find property to extend" error.
Colin Cross43e789d2020-01-28 09:46:50 -0800130 proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(),
Colin Cross31a738b2019-12-30 18:45:15 -0800131 }
132 err := proptools.AppendMatchingProperties(dst, src, nil)
133 if err != nil {
134 panic(err)
135 }
136 }
137
138 return module
139}
140
Liz Kammerf31c9002022-04-26 09:08:55 -0400141func (l *loadHookContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
142 return createModule(l, factory, "_loadHookModule", props...)
143}
144
Colin Cross9d34f352019-11-22 16:03:51 -0800145func (l *loadHookContext) registerScopedModuleType(name string, factory blueprint.ModuleFactory) {
146 l.bp.RegisterScopedModuleType(name, factory)
147}
148
Colin Cross178a5092016-09-13 13:42:32 -0700149type InstallHookContext interface {
150 ModuleContext
David Srbecky07656412020-06-04 01:26:16 +0100151 SrcPath() Path
Colin Cross70dda7e2019-10-01 22:05:35 -0700152 Path() InstallPath
Colin Cross178a5092016-09-13 13:42:32 -0700153 Symlink() bool
154}
155
156// Install hooks are run after a module creates a rule to install a file or symlink.
157// The installed path is available from InstallHookContext.Path(), and
158// InstallHookContext.Symlink() will be true if it was a symlink.
159func AddInstallHook(m blueprint.Module, hook func(InstallHookContext)) {
160 h := &m.(Module).base().hooks
161 h.install = append(h.install, hook)
162}
163
164type installHookContext struct {
165 ModuleContext
David Srbecky07656412020-06-04 01:26:16 +0100166 srcPath Path
Colin Cross70dda7e2019-10-01 22:05:35 -0700167 path InstallPath
Colin Cross178a5092016-09-13 13:42:32 -0700168 symlink bool
169}
170
David Srbecky07656412020-06-04 01:26:16 +0100171var _ InstallHookContext = &installHookContext{}
172
173func (x *installHookContext) SrcPath() Path {
174 return x.srcPath
175}
176
Colin Cross70dda7e2019-10-01 22:05:35 -0700177func (x *installHookContext) Path() InstallPath {
Colin Cross178a5092016-09-13 13:42:32 -0700178 return x.path
179}
180
181func (x *installHookContext) Symlink() bool {
182 return x.symlink
183}
184
David Srbecky07656412020-06-04 01:26:16 +0100185func (x *hooks) runInstallHooks(ctx ModuleContext, srcPath Path, path InstallPath, symlink bool) {
Colin Cross178a5092016-09-13 13:42:32 -0700186 if len(x.install) > 0 {
187 mctx := &installHookContext{
188 ModuleContext: ctx,
David Srbecky07656412020-06-04 01:26:16 +0100189 srcPath: srcPath,
Colin Cross178a5092016-09-13 13:42:32 -0700190 path: path,
191 symlink: symlink,
192 }
193 for _, x := range x.install {
194 x(mctx)
195 if mctx.Failed() {
196 return
197 }
198 }
199 }
200}
201
202type hooks struct {
Colin Cross178a5092016-09-13 13:42:32 -0700203 install []func(InstallHookContext)
204}