blob: 04ba69ec60f0129824dae5370ba348d536eac150 [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 Cross178a5092016-09-13 13:42:32 -070037}
38
Colin Cross178a5092016-09-13 13:42:32 -070039func AddLoadHook(m blueprint.Module, hook func(LoadHookContext)) {
Colin Cross31a738b2019-12-30 18:45:15 -080040 blueprint.AddLoadHook(m, func(ctx blueprint.LoadHookContext) {
41 actx := &loadHookContext{
42 earlyModuleContext: m.(Module).base().earlyModuleContextFactory(ctx),
43 bp: ctx,
44 }
45 hook(actx)
46 })
Colin Cross178a5092016-09-13 13:42:32 -070047}
48
Colin Cross31a738b2019-12-30 18:45:15 -080049type loadHookContext struct {
50 earlyModuleContext
51 bp blueprint.LoadHookContext
52 module Module
53}
54
55func (l *loadHookContext) AppendProperties(props ...interface{}) {
56 for _, p := range props {
57 err := proptools.AppendMatchingProperties(l.Module().base().customizableProperties,
58 p, nil)
59 if err != nil {
60 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
61 l.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
62 } else {
63 panic(err)
Colin Cross178a5092016-09-13 13:42:32 -070064 }
65 }
66 }
67}
68
Colin Cross31a738b2019-12-30 18:45:15 -080069func (l *loadHookContext) PrependProperties(props ...interface{}) {
70 for _, p := range props {
71 err := proptools.PrependMatchingProperties(l.Module().base().customizableProperties,
72 p, nil)
73 if err != nil {
74 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
75 l.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
76 } else {
77 panic(err)
78 }
79 }
80 }
81}
82
83func (l *loadHookContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
84 inherited := []interface{}{&l.Module().base().commonProperties}
85 module := l.bp.CreateModule(ModuleFactoryAdaptor(factory), append(inherited, props...)...).(Module)
86
87 if l.Module().base().variableProperties != nil && module.base().variableProperties != nil {
88 src := l.Module().base().variableProperties
89 dst := []interface{}{
90 module.base().variableProperties,
91 // Put an empty copy of the src properties into dst so that properties in src that are not in dst
92 // don't cause a "failed to find property to extend" error.
93 proptools.CloneEmptyProperties(reflect.ValueOf(src).Elem()).Interface(),
94 }
95 err := proptools.AppendMatchingProperties(dst, src, nil)
96 if err != nil {
97 panic(err)
98 }
99 }
100
101 return module
102}
103
Colin Cross178a5092016-09-13 13:42:32 -0700104type InstallHookContext interface {
105 ModuleContext
Colin Cross70dda7e2019-10-01 22:05:35 -0700106 Path() InstallPath
Colin Cross178a5092016-09-13 13:42:32 -0700107 Symlink() bool
108}
109
110// Install hooks are run after a module creates a rule to install a file or symlink.
111// The installed path is available from InstallHookContext.Path(), and
112// InstallHookContext.Symlink() will be true if it was a symlink.
113func AddInstallHook(m blueprint.Module, hook func(InstallHookContext)) {
114 h := &m.(Module).base().hooks
115 h.install = append(h.install, hook)
116}
117
118type installHookContext struct {
119 ModuleContext
Colin Cross70dda7e2019-10-01 22:05:35 -0700120 path InstallPath
Colin Cross178a5092016-09-13 13:42:32 -0700121 symlink bool
122}
123
Colin Cross70dda7e2019-10-01 22:05:35 -0700124func (x *installHookContext) Path() InstallPath {
Colin Cross178a5092016-09-13 13:42:32 -0700125 return x.path
126}
127
128func (x *installHookContext) Symlink() bool {
129 return x.symlink
130}
131
Colin Cross70dda7e2019-10-01 22:05:35 -0700132func (x *hooks) runInstallHooks(ctx ModuleContext, path InstallPath, symlink bool) {
Colin Cross178a5092016-09-13 13:42:32 -0700133 if len(x.install) > 0 {
134 mctx := &installHookContext{
135 ModuleContext: ctx,
136 path: path,
137 symlink: symlink,
138 }
139 for _, x := range x.install {
140 x(mctx)
141 if mctx.Failed() {
142 return
143 }
144 }
145 }
146}
147
148type hooks struct {
Colin Cross178a5092016-09-13 13:42:32 -0700149 install []func(InstallHookContext)
150}