blob: 2a000cebeec671ac15d214e0978ebbee85c20a0b [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 (
18 "github.com/google/blueprint"
Colin Cross178a5092016-09-13 13:42:32 -070019)
20
21// This file implements hooks that external module types can use to inject logic into existing
22// module types. Each hook takes an interface as a parameter so that new methods can be added
23// to the interface without breaking existing module types.
24
25// Load hooks are run after the module's properties have been filled from the blueprint file, but
26// before the module has been split into architecture variants, and before defaults modules have
27// been applied.
28type LoadHookContext interface {
Colin Cross1184b642019-12-30 18:43:07 -080029 EarlyModuleContext
30
Colin Cross178a5092016-09-13 13:42:32 -070031 AppendProperties(...interface{})
32 PrependProperties(...interface{})
Colin Crosse003c4a2019-09-25 12:58:36 -070033 CreateModule(ModuleFactory, ...interface{}) Module
Colin Cross178a5092016-09-13 13:42:32 -070034}
35
Colin Cross178a5092016-09-13 13:42:32 -070036func AddLoadHook(m blueprint.Module, hook func(LoadHookContext)) {
37 h := &m.(Module).base().hooks
38 h.load = append(h.load, hook)
39}
40
Colin Cross519917d2017-11-02 16:35:56 -070041func (x *hooks) runLoadHooks(ctx LoadHookContext, m *ModuleBase) {
Colin Cross178a5092016-09-13 13:42:32 -070042 if len(x.load) > 0 {
Colin Cross178a5092016-09-13 13:42:32 -070043 for _, x := range x.load {
Colin Cross519917d2017-11-02 16:35:56 -070044 x(ctx)
45 if ctx.Failed() {
Colin Cross178a5092016-09-13 13:42:32 -070046 return
47 }
48 }
49 }
50}
51
Colin Cross178a5092016-09-13 13:42:32 -070052type InstallHookContext interface {
53 ModuleContext
Colin Cross70dda7e2019-10-01 22:05:35 -070054 Path() InstallPath
Colin Cross178a5092016-09-13 13:42:32 -070055 Symlink() bool
56}
57
58// Install hooks are run after a module creates a rule to install a file or symlink.
59// The installed path is available from InstallHookContext.Path(), and
60// InstallHookContext.Symlink() will be true if it was a symlink.
61func AddInstallHook(m blueprint.Module, hook func(InstallHookContext)) {
62 h := &m.(Module).base().hooks
63 h.install = append(h.install, hook)
64}
65
66type installHookContext struct {
67 ModuleContext
Colin Cross70dda7e2019-10-01 22:05:35 -070068 path InstallPath
Colin Cross178a5092016-09-13 13:42:32 -070069 symlink bool
70}
71
Colin Cross70dda7e2019-10-01 22:05:35 -070072func (x *installHookContext) Path() InstallPath {
Colin Cross178a5092016-09-13 13:42:32 -070073 return x.path
74}
75
76func (x *installHookContext) Symlink() bool {
77 return x.symlink
78}
79
Colin Cross70dda7e2019-10-01 22:05:35 -070080func (x *hooks) runInstallHooks(ctx ModuleContext, path InstallPath, symlink bool) {
Colin Cross178a5092016-09-13 13:42:32 -070081 if len(x.install) > 0 {
82 mctx := &installHookContext{
83 ModuleContext: ctx,
84 path: path,
85 symlink: symlink,
86 }
87 for _, x := range x.install {
88 x(mctx)
89 if mctx.Failed() {
90 return
91 }
92 }
93 }
94}
95
96type hooks struct {
97 load []func(LoadHookContext)
Colin Cross178a5092016-09-13 13:42:32 -070098 install []func(InstallHookContext)
99}
100
Colin Crossf8b860a2019-04-16 14:43:28 -0700101func registerLoadHookMutator(ctx RegisterMutatorsContext) {
102 ctx.TopDown("load_hooks", LoadHookMutator).Parallel()
103}
104
Inseob Kimc0907f12019-02-08 21:00:45 +0900105func LoadHookMutator(ctx TopDownMutatorContext) {
Colin Cross178a5092016-09-13 13:42:32 -0700106 if m, ok := ctx.Module().(Module); ok {
Colin Cross9a362232019-07-01 15:32:45 -0700107 m.base().commonProperties.DebugName = ctx.ModuleName()
108
Colin Cross25de6c32019-06-06 14:29:25 -0700109 // Cast through *topDownMutatorContext because AppendProperties is implemented
110 // on *topDownMutatorContext but not exposed through TopDownMutatorContext
111 var loadHookCtx LoadHookContext = ctx.(*topDownMutatorContext)
Colin Cross519917d2017-11-02 16:35:56 -0700112 m.base().hooks.runLoadHooks(loadHookCtx, m.base())
Colin Cross178a5092016-09-13 13:42:32 -0700113 }
114}