blob: 9f4e5b620f6eaa1e3a1d5cf66873e22aa864a7f0 [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
Colin Cross31a738b2019-12-30 18:45:15 -080062type loadHookContext struct {
63 earlyModuleContext
64 bp blueprint.LoadHookContext
65 module Module
66}
67
Colin Cross9d34f352019-11-22 16:03:51 -080068func (l *loadHookContext) moduleFactories() map[string]blueprint.ModuleFactory {
69 return l.bp.ModuleFactories()
70}
71
Ustaef3676c2021-11-23 12:31:55 -050072func (l *loadHookContext) appendPrependHelper(props []interface{},
73 extendFn func([]interface{}, interface{}, proptools.ExtendPropertyFilterFunc) error) {
Colin Cross31a738b2019-12-30 18:45:15 -080074 for _, p := range props {
Usta851a3272022-01-05 23:42:33 -050075 err := extendFn(l.Module().base().GetProperties(), p, nil)
Colin Cross31a738b2019-12-30 18:45:15 -080076 if err != nil {
77 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
78 l.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
79 } else {
80 panic(err)
Colin Cross178a5092016-09-13 13:42:32 -070081 }
82 }
83 }
84}
Ustaef3676c2021-11-23 12:31:55 -050085func (l *loadHookContext) AppendProperties(props ...interface{}) {
86 l.appendPrependHelper(props, proptools.AppendMatchingProperties)
87}
Colin Cross178a5092016-09-13 13:42:32 -070088
Colin Cross31a738b2019-12-30 18:45:15 -080089func (l *loadHookContext) PrependProperties(props ...interface{}) {
Ustaef3676c2021-11-23 12:31:55 -050090 l.appendPrependHelper(props, proptools.PrependMatchingProperties)
Colin Cross31a738b2019-12-30 18:45:15 -080091}
92
Liz Kammerf31c9002022-04-26 09:08:55 -040093func (l *loadHookContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module {
94 return l.bp.CreateModule(factory, name, props...)
95}
96
Jihoon Kang222874d2024-10-23 23:38:05 +000097func (l *loadHookContext) createModuleInDirectory(factory blueprint.ModuleFactory, name, moduleDir string, props ...interface{}) blueprint.Module {
98 return l.bp.CreateModuleInDirectory(factory, name, moduleDir, props...)
99}
100
101type specifyDirectory struct {
102 specified bool
103 directory string
104}
105
106func doesNotSpecifyDirectory() specifyDirectory {
107 return specifyDirectory{
108 specified: false,
109 directory: "",
110 }
111}
112
113func specifiesDirectory(directory string) specifyDirectory {
114 return specifyDirectory{
115 specified: true,
116 directory: directory,
117 }
118}
119
Liz Kammerf31c9002022-04-26 09:08:55 -0400120type createModuleContext interface {
121 Module() Module
Cole Faustb1ccc2f2024-09-27 11:21:53 -0700122 HasMutatorFinished(mutatorName string) bool
Liz Kammerf31c9002022-04-26 09:08:55 -0400123 createModule(blueprint.ModuleFactory, string, ...interface{}) blueprint.Module
Jihoon Kang222874d2024-10-23 23:38:05 +0000124 createModuleInDirectory(blueprint.ModuleFactory, string, string, ...interface{}) blueprint.Module
Liz Kammerf31c9002022-04-26 09:08:55 -0400125}
126
Jihoon Kang222874d2024-10-23 23:38:05 +0000127func createModule(ctx createModuleContext, factory ModuleFactory, ext string, specifyDirectory specifyDirectory, props ...interface{}) Module {
Cole Faustb1ccc2f2024-09-27 11:21:53 -0700128 if ctx.HasMutatorFinished("defaults") {
129 // Creating modules late is oftentimes problematic, because they don't have earlier
130 // mutators run on them. Prevent making modules after the defaults mutator has run.
131 panic("Cannot create a module after the defaults mutator has finished")
132 }
133
Liz Kammerf31c9002022-04-26 09:08:55 -0400134 inherited := []interface{}{&ctx.Module().base().commonProperties}
Sam Delmerico286bf262022-03-09 20:46:37 +0000135
136 var typeName string
137 if typeNameLookup, ok := ModuleTypeByFactory()[reflect.ValueOf(factory)]; ok {
138 typeName = typeNameLookup
139 } else {
140 factoryPtr := reflect.ValueOf(factory).Pointer()
141 factoryFunc := runtime.FuncForPC(factoryPtr)
142 filePath, _ := factoryFunc.FileLine(factoryPtr)
143 typeName = fmt.Sprintf("%s_%s", path.Base(filePath), factoryFunc.Name())
144 }
Liz Kammerf31c9002022-04-26 09:08:55 -0400145 typeName = typeName + "_" + ext
Sam Delmerico286bf262022-03-09 20:46:37 +0000146
Jihoon Kang222874d2024-10-23 23:38:05 +0000147 var module Module
148 if specifyDirectory.specified {
149 module = ctx.createModuleInDirectory(ModuleFactoryAdaptor(factory), typeName, specifyDirectory.directory, append(inherited, props...)...).(Module)
150 } else {
151 module = ctx.createModule(ModuleFactoryAdaptor(factory), typeName, append(inherited, props...)...).(Module)
152 }
Colin Cross31a738b2019-12-30 18:45:15 -0800153
Liz Kammerf31c9002022-04-26 09:08:55 -0400154 if ctx.Module().base().variableProperties != nil && module.base().variableProperties != nil {
155 src := ctx.Module().base().variableProperties
Colin Cross31a738b2019-12-30 18:45:15 -0800156 dst := []interface{}{
157 module.base().variableProperties,
158 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
159 // don't cause a "failed to find property to extend" error.
Colin Cross43e789d2020-01-28 09:46:50 -0800160 proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(),
Colin Cross31a738b2019-12-30 18:45:15 -0800161 }
162 err := proptools.AppendMatchingProperties(dst, src, nil)
163 if err != nil {
164 panic(err)
165 }
166 }
167
168 return module
169}
170
Liz Kammerf31c9002022-04-26 09:08:55 -0400171func (l *loadHookContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
Jihoon Kang222874d2024-10-23 23:38:05 +0000172 return createModule(l, factory, "_loadHookModule", doesNotSpecifyDirectory(), props...)
173}
174
175func (l *loadHookContext) CreateModuleInDirectory(factory ModuleFactory, directory string, props ...interface{}) Module {
176 return createModule(l, factory, "_loadHookModule", specifiesDirectory(directory), props...)
Liz Kammerf31c9002022-04-26 09:08:55 -0400177}
178
Colin Cross9d34f352019-11-22 16:03:51 -0800179func (l *loadHookContext) registerScopedModuleType(name string, factory blueprint.ModuleFactory) {
180 l.bp.RegisterScopedModuleType(name, factory)
181}
182
Colin Cross178a5092016-09-13 13:42:32 -0700183type InstallHookContext interface {
184 ModuleContext
David Srbecky07656412020-06-04 01:26:16 +0100185 SrcPath() Path
Colin Cross70dda7e2019-10-01 22:05:35 -0700186 Path() InstallPath
Colin Cross178a5092016-09-13 13:42:32 -0700187 Symlink() bool
188}
189
190// Install hooks are run after a module creates a rule to install a file or symlink.
191// The installed path is available from InstallHookContext.Path(), and
192// InstallHookContext.Symlink() will be true if it was a symlink.
193func AddInstallHook(m blueprint.Module, hook func(InstallHookContext)) {
194 h := &m.(Module).base().hooks
195 h.install = append(h.install, hook)
196}
197
198type installHookContext struct {
199 ModuleContext
David Srbecky07656412020-06-04 01:26:16 +0100200 srcPath Path
Colin Cross70dda7e2019-10-01 22:05:35 -0700201 path InstallPath
Colin Cross178a5092016-09-13 13:42:32 -0700202 symlink bool
203}
204
David Srbecky07656412020-06-04 01:26:16 +0100205var _ InstallHookContext = &installHookContext{}
206
207func (x *installHookContext) SrcPath() Path {
208 return x.srcPath
209}
210
Colin Cross70dda7e2019-10-01 22:05:35 -0700211func (x *installHookContext) Path() InstallPath {
Colin Cross178a5092016-09-13 13:42:32 -0700212 return x.path
213}
214
215func (x *installHookContext) Symlink() bool {
216 return x.symlink
217}
218
David Srbecky07656412020-06-04 01:26:16 +0100219func (x *hooks) runInstallHooks(ctx ModuleContext, srcPath Path, path InstallPath, symlink bool) {
Colin Cross178a5092016-09-13 13:42:32 -0700220 if len(x.install) > 0 {
221 mctx := &installHookContext{
222 ModuleContext: ctx,
David Srbecky07656412020-06-04 01:26:16 +0100223 srcPath: srcPath,
Colin Cross178a5092016-09-13 13:42:32 -0700224 path: path,
225 symlink: symlink,
226 }
227 for _, x := range x.install {
228 x(mctx)
229 if mctx.Failed() {
230 return
231 }
232 }
233 }
234}
235
236type hooks struct {
Colin Cross178a5092016-09-13 13:42:32 -0700237 install []func(InstallHookContext)
238}