blob: f8022d05c6cf46964014adb69bbcfa1a22ce607c [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
Jihoon Kang222874d2024-10-23 23:38:05 +000040 CreateModuleInDirectory(ModuleFactory, string, ...interface{}) Module
Colin Cross9d34f352019-11-22 16:03:51 -080041
42 registerScopedModuleType(name string, factory blueprint.ModuleFactory)
43 moduleFactories() map[string]blueprint.ModuleFactory
Colin Cross178a5092016-09-13 13:42:32 -070044}
45
Paul Duffinafa9fa12020-04-29 16:47:28 +010046// Add a hook that will be called once the module has been loaded, i.e. its
47// properties have been initialized from the Android.bp file.
48//
49// Consider using SetDefaultableHook to register a hook for any module that implements
50// DefaultableModule as the hook is called after any defaults have been applied to the
51// module which could reduce duplication and make it easier to use.
Colin Cross178a5092016-09-13 13:42:32 -070052func AddLoadHook(m blueprint.Module, hook func(LoadHookContext)) {
Colin Cross31a738b2019-12-30 18:45:15 -080053 blueprint.AddLoadHook(m, func(ctx blueprint.LoadHookContext) {
54 actx := &loadHookContext{
55 earlyModuleContext: m.(Module).base().earlyModuleContextFactory(ctx),
56 bp: ctx,
57 }
58 hook(actx)
59 })
Colin Cross178a5092016-09-13 13:42:32 -070060}
61
Spandan Dasb0aabb12024-12-04 01:29:07 +000062func AddLoadHookWithPriority(m blueprint.Module, hook func(LoadHookContext), priority int) {
63 blueprint.AddLoadHookWithPriority(m, func(ctx blueprint.LoadHookContext) {
64 actx := &loadHookContext{
65 earlyModuleContext: m.(Module).base().earlyModuleContextFactory(ctx),
66 bp: ctx,
67 }
68 hook(actx)
69 }, priority)
70}
71
Colin Cross31a738b2019-12-30 18:45:15 -080072type loadHookContext struct {
73 earlyModuleContext
74 bp blueprint.LoadHookContext
75 module Module
76}
77
Colin Cross9d34f352019-11-22 16:03:51 -080078func (l *loadHookContext) moduleFactories() map[string]blueprint.ModuleFactory {
79 return l.bp.ModuleFactories()
80}
81
Ustaef3676c2021-11-23 12:31:55 -050082func (l *loadHookContext) appendPrependHelper(props []interface{},
83 extendFn func([]interface{}, interface{}, proptools.ExtendPropertyFilterFunc) error) {
Colin Cross31a738b2019-12-30 18:45:15 -080084 for _, p := range props {
Usta851a3272022-01-05 23:42:33 -050085 err := extendFn(l.Module().base().GetProperties(), p, nil)
Colin Cross31a738b2019-12-30 18:45:15 -080086 if err != nil {
87 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
88 l.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
89 } else {
90 panic(err)
Colin Cross178a5092016-09-13 13:42:32 -070091 }
92 }
93 }
94}
Ustaef3676c2021-11-23 12:31:55 -050095func (l *loadHookContext) AppendProperties(props ...interface{}) {
96 l.appendPrependHelper(props, proptools.AppendMatchingProperties)
97}
Colin Cross178a5092016-09-13 13:42:32 -070098
Colin Cross31a738b2019-12-30 18:45:15 -080099func (l *loadHookContext) PrependProperties(props ...interface{}) {
Ustaef3676c2021-11-23 12:31:55 -0500100 l.appendPrependHelper(props, proptools.PrependMatchingProperties)
Colin Cross31a738b2019-12-30 18:45:15 -0800101}
102
Liz Kammerf31c9002022-04-26 09:08:55 -0400103func (l *loadHookContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module {
104 return l.bp.CreateModule(factory, name, props...)
105}
106
Jihoon Kang222874d2024-10-23 23:38:05 +0000107func (l *loadHookContext) createModuleInDirectory(factory blueprint.ModuleFactory, name, moduleDir string, props ...interface{}) blueprint.Module {
108 return l.bp.CreateModuleInDirectory(factory, name, moduleDir, props...)
109}
110
111type specifyDirectory struct {
112 specified bool
113 directory string
114}
115
116func doesNotSpecifyDirectory() specifyDirectory {
117 return specifyDirectory{
118 specified: false,
119 directory: "",
120 }
121}
122
123func specifiesDirectory(directory string) specifyDirectory {
124 return specifyDirectory{
125 specified: true,
126 directory: directory,
127 }
128}
129
Liz Kammerf31c9002022-04-26 09:08:55 -0400130type createModuleContext interface {
131 Module() Module
Cole Faustb1ccc2f2024-09-27 11:21:53 -0700132 HasMutatorFinished(mutatorName string) bool
Liz Kammerf31c9002022-04-26 09:08:55 -0400133 createModule(blueprint.ModuleFactory, string, ...interface{}) blueprint.Module
Jihoon Kang222874d2024-10-23 23:38:05 +0000134 createModuleInDirectory(blueprint.ModuleFactory, string, string, ...interface{}) blueprint.Module
Liz Kammerf31c9002022-04-26 09:08:55 -0400135}
136
Jihoon Kang222874d2024-10-23 23:38:05 +0000137func createModule(ctx createModuleContext, factory ModuleFactory, ext string, specifyDirectory specifyDirectory, props ...interface{}) Module {
Cole Faustb1ccc2f2024-09-27 11:21:53 -0700138 if ctx.HasMutatorFinished("defaults") {
139 // Creating modules late is oftentimes problematic, because they don't have earlier
140 // mutators run on them. Prevent making modules after the defaults mutator has run.
141 panic("Cannot create a module after the defaults mutator has finished")
142 }
143
Liz Kammerf31c9002022-04-26 09:08:55 -0400144 inherited := []interface{}{&ctx.Module().base().commonProperties}
Sam Delmerico286bf262022-03-09 20:46:37 +0000145
146 var typeName string
147 if typeNameLookup, ok := ModuleTypeByFactory()[reflect.ValueOf(factory)]; ok {
148 typeName = typeNameLookup
149 } else {
150 factoryPtr := reflect.ValueOf(factory).Pointer()
151 factoryFunc := runtime.FuncForPC(factoryPtr)
152 filePath, _ := factoryFunc.FileLine(factoryPtr)
153 typeName = fmt.Sprintf("%s_%s", path.Base(filePath), factoryFunc.Name())
154 }
Liz Kammerf31c9002022-04-26 09:08:55 -0400155 typeName = typeName + "_" + ext
Sam Delmerico286bf262022-03-09 20:46:37 +0000156
Jihoon Kang222874d2024-10-23 23:38:05 +0000157 var module Module
158 if specifyDirectory.specified {
159 module = ctx.createModuleInDirectory(ModuleFactoryAdaptor(factory), typeName, specifyDirectory.directory, append(inherited, props...)...).(Module)
160 } else {
161 module = ctx.createModule(ModuleFactoryAdaptor(factory), typeName, append(inherited, props...)...).(Module)
162 }
Colin Cross31a738b2019-12-30 18:45:15 -0800163
Liz Kammerf31c9002022-04-26 09:08:55 -0400164 if ctx.Module().base().variableProperties != nil && module.base().variableProperties != nil {
165 src := ctx.Module().base().variableProperties
Colin Cross31a738b2019-12-30 18:45:15 -0800166 dst := []interface{}{
167 module.base().variableProperties,
168 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
169 // don't cause a "failed to find property to extend" error.
Colin Cross43e789d2020-01-28 09:46:50 -0800170 proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(),
Colin Cross31a738b2019-12-30 18:45:15 -0800171 }
172 err := proptools.AppendMatchingProperties(dst, src, nil)
173 if err != nil {
174 panic(err)
175 }
176 }
177
178 return module
179}
180
Liz Kammerf31c9002022-04-26 09:08:55 -0400181func (l *loadHookContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Jihoon Kang222874d2024-10-23 23:38:05 +0000182 return createModule(l, factory, "_loadHookModule", doesNotSpecifyDirectory(), props...)
183}
184
185func (l *loadHookContext) CreateModuleInDirectory(factory ModuleFactory, directory string, props ...interface{}) Module {
186 return createModule(l, factory, "_loadHookModule", specifiesDirectory(directory), props...)
Liz Kammerf31c9002022-04-26 09:08:55 -0400187}
188
Colin Cross9d34f352019-11-22 16:03:51 -0800189func (l *loadHookContext) registerScopedModuleType(name string, factory blueprint.ModuleFactory) {
190 l.bp.RegisterScopedModuleType(name, factory)
191}
192
Colin Cross178a5092016-09-13 13:42:32 -0700193type InstallHookContext interface {
194 ModuleContext
David Srbecky07656412020-06-04 01:26:16 +0100195 SrcPath() Path
Colin Cross70dda7e2019-10-01 22:05:35 -0700196 Path() InstallPath
Colin Cross178a5092016-09-13 13:42:32 -0700197 Symlink() bool
198}
199
200// Install hooks are run after a module creates a rule to install a file or symlink.
201// The installed path is available from InstallHookContext.Path(), and
202// InstallHookContext.Symlink() will be true if it was a symlink.
203func AddInstallHook(m blueprint.Module, hook func(InstallHookContext)) {
204 h := &m.(Module).base().hooks
205 h.install = append(h.install, hook)
206}
207
208type installHookContext struct {
209 ModuleContext
David Srbecky07656412020-06-04 01:26:16 +0100210 srcPath Path
Colin Cross70dda7e2019-10-01 22:05:35 -0700211 path InstallPath
Colin Cross178a5092016-09-13 13:42:32 -0700212 symlink bool
213}
214
David Srbecky07656412020-06-04 01:26:16 +0100215var _ InstallHookContext = &installHookContext{}
216
217func (x *installHookContext) SrcPath() Path {
218 return x.srcPath
219}
220
Colin Cross70dda7e2019-10-01 22:05:35 -0700221func (x *installHookContext) Path() InstallPath {
Colin Cross178a5092016-09-13 13:42:32 -0700222 return x.path
223}
224
225func (x *installHookContext) Symlink() bool {
226 return x.symlink
227}
228
David Srbecky07656412020-06-04 01:26:16 +0100229func (x *hooks) runInstallHooks(ctx ModuleContext, srcPath Path, path InstallPath, symlink bool) {
Colin Cross178a5092016-09-13 13:42:32 -0700230 if len(x.install) > 0 {
231 mctx := &installHookContext{
232 ModuleContext: ctx,
David Srbecky07656412020-06-04 01:26:16 +0100233 srcPath: srcPath,
Colin Cross178a5092016-09-13 13:42:32 -0700234 path: path,
235 symlink: symlink,
236 }
237 for _, x := range x.install {
238 x(mctx)
239 if mctx.Failed() {
240 return
241 }
242 }
243 }
244}
245
246type hooks struct {
Colin Cross178a5092016-09-13 13:42:32 -0700247 install []func(InstallHookContext)
248}