blob: e8cd81b602330d2ddf2e29a2b152f2d14ba97406 [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 (
Colin Cross31a738b2019-12-30 18:45:15 -080018 "reflect"
19
Colin Cross178a5092016-09-13 13:42:32 -070020 "github.com/google/blueprint"
Colin Cross31a738b2019-12-30 18:45:15 -080021 "github.com/google/blueprint/proptools"
Colin Cross178a5092016-09-13 13:42:32 -070022)
23
24// This file implements hooks that external module types can use to inject logic into existing
25// module types. Each hook takes an interface as a parameter so that new methods can be added
26// to the interface without breaking existing module types.
27
28// Load hooks are run after the module's properties have been filled from the blueprint file, but
29// before the module has been split into architecture variants, and before defaults modules have
30// been applied.
31type LoadHookContext interface {
Colin Cross1184b642019-12-30 18:43:07 -080032 EarlyModuleContext
33
Colin Cross178a5092016-09-13 13:42:32 -070034 AppendProperties(...interface{})
35 PrependProperties(...interface{})
Colin Crosse003c4a2019-09-25 12:58:36 -070036 CreateModule(ModuleFactory, ...interface{}) Module
Colin Cross9d34f352019-11-22 16:03:51 -080037
38 registerScopedModuleType(name string, factory blueprint.ModuleFactory)
39 moduleFactories() map[string]blueprint.ModuleFactory
Colin Cross178a5092016-09-13 13:42:32 -070040}
41
Colin Cross178a5092016-09-13 13:42:32 -070042func AddLoadHook(m blueprint.Module, hook func(LoadHookContext)) {
Colin Cross31a738b2019-12-30 18:45:15 -080043 blueprint.AddLoadHook(m, func(ctx blueprint.LoadHookContext) {
44 actx := &loadHookContext{
45 earlyModuleContext: m.(Module).base().earlyModuleContextFactory(ctx),
46 bp: ctx,
47 }
48 hook(actx)
49 })
Colin Cross178a5092016-09-13 13:42:32 -070050}
51
Colin Cross31a738b2019-12-30 18:45:15 -080052type loadHookContext struct {
53 earlyModuleContext
54 bp blueprint.LoadHookContext
55 module Module
56}
57
Colin Cross9d34f352019-11-22 16:03:51 -080058func (l *loadHookContext) moduleFactories() map[string]blueprint.ModuleFactory {
59 return l.bp.ModuleFactories()
60}
61
Colin Cross31a738b2019-12-30 18:45:15 -080062func (l *loadHookContext) AppendProperties(props ...interface{}) {
63 for _, p := range props {
64 err := proptools.AppendMatchingProperties(l.Module().base().customizableProperties,
65 p, nil)
66 if err != nil {
67 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
68 l.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
69 } else {
70 panic(err)
Colin Cross178a5092016-09-13 13:42:32 -070071 }
72 }
73 }
74}
75
Colin Cross31a738b2019-12-30 18:45:15 -080076func (l *loadHookContext) PrependProperties(props ...interface{}) {
77 for _, p := range props {
78 err := proptools.PrependMatchingProperties(l.Module().base().customizableProperties,
79 p, nil)
80 if err != nil {
81 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
82 l.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
83 } else {
84 panic(err)
85 }
86 }
87 }
88}
89
90func (l *loadHookContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
91 inherited := []interface{}{&l.Module().base().commonProperties}
92 module := l.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module)
93
94 if l.Module().base().variableProperties != nil && module.base().variableProperties != nil {
95 src := l.Module().base().variableProperties
96 dst := []interface{}{
97 module.base().variableProperties,
98 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
99 // don't cause a "failed to find property to extend" error.
100 proptools.CloneEmptyProperties(reflect.ValueOf(src).Elem()).Interface(),
101 }
102 err := proptools.AppendMatchingProperties(dst, src, nil)
103 if err != nil {
104 panic(err)
105 }
106 }
107
108 return module
109}
110
Colin Cross9d34f352019-11-22 16:03:51 -0800111func (l *loadHookContext) registerScopedModuleType(name string, factory blueprint.ModuleFactory) {
112 l.bp.RegisterScopedModuleType(name, factory)
113}
114
Colin Cross178a5092016-09-13 13:42:32 -0700115type InstallHookContext interface {
116 ModuleContext
Colin Cross70dda7e2019-10-01 22:05:35 -0700117 Path() InstallPath
Colin Cross178a5092016-09-13 13:42:32 -0700118 Symlink() bool
119}
120
121// Install hooks are run after a module creates a rule to install a file or symlink.
122// The installed path is available from InstallHookContext.Path(), and
123// InstallHookContext.Symlink() will be true if it was a symlink.
124func AddInstallHook(m blueprint.Module, hook func(InstallHookContext)) {
125 h := &m.(Module).base().hooks
126 h.install = append(h.install, hook)
127}
128
129type installHookContext struct {
130 ModuleContext
Colin Cross70dda7e2019-10-01 22:05:35 -0700131 path InstallPath
Colin Cross178a5092016-09-13 13:42:32 -0700132 symlink bool
133}
134
Colin Cross70dda7e2019-10-01 22:05:35 -0700135func (x *installHookContext) Path() InstallPath {
Colin Cross178a5092016-09-13 13:42:32 -0700136 return x.path
137}
138
139func (x *installHookContext) Symlink() bool {
140 return x.symlink
141}
142
Colin Cross70dda7e2019-10-01 22:05:35 -0700143func (x *hooks) runInstallHooks(ctx ModuleContext, path InstallPath, symlink bool) {
Colin Cross178a5092016-09-13 13:42:32 -0700144 if len(x.install) > 0 {
145 mctx := &installHookContext{
146 ModuleContext: ctx,
147 path: path,
148 symlink: symlink,
149 }
150 for _, x := range x.install {
151 x(mctx)
152 if mctx.Failed() {
153 return
154 }
155 }
156 }
157}
158
159type hooks struct {
Colin Cross178a5092016-09-13 13:42:32 -0700160 install []func(InstallHookContext)
161}