2016-09-13 22:42:32 +02:00
|
|
|
// Copyright 2016 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package android
|
|
|
|
|
|
|
|
import (
|
2022-03-09 21:46:37 +01:00
|
|
|
"fmt"
|
|
|
|
"path"
|
2019-12-31 03:45:15 +01:00
|
|
|
"reflect"
|
2022-03-09 21:46:37 +01:00
|
|
|
"runtime"
|
2019-12-31 03:45:15 +01:00
|
|
|
|
2016-09-13 22:42:32 +02:00
|
|
|
"github.com/google/blueprint"
|
2019-12-31 03:45:15 +01:00
|
|
|
"github.com/google/blueprint/proptools"
|
2016-09-13 22:42:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// This file implements hooks that external module types can use to inject logic into existing
|
|
|
|
// module types. Each hook takes an interface as a parameter so that new methods can be added
|
|
|
|
// to the interface without breaking existing module types.
|
|
|
|
|
|
|
|
// Load hooks are run after the module's properties have been filled from the blueprint file, but
|
|
|
|
// before the module has been split into architecture variants, and before defaults modules have
|
|
|
|
// been applied.
|
|
|
|
type LoadHookContext interface {
|
2019-12-31 03:43:07 +01:00
|
|
|
EarlyModuleContext
|
|
|
|
|
2016-09-13 22:42:32 +02:00
|
|
|
AppendProperties(...interface{})
|
|
|
|
PrependProperties(...interface{})
|
2019-09-25 21:58:36 +02:00
|
|
|
CreateModule(ModuleFactory, ...interface{}) Module
|
2019-11-23 01:03:51 +01:00
|
|
|
|
|
|
|
registerScopedModuleType(name string, factory blueprint.ModuleFactory)
|
|
|
|
moduleFactories() map[string]blueprint.ModuleFactory
|
2016-09-13 22:42:32 +02:00
|
|
|
}
|
|
|
|
|
2020-04-29 17:47:28 +02:00
|
|
|
// Add a hook that will be called once the module has been loaded, i.e. its
|
|
|
|
// properties have been initialized from the Android.bp file.
|
|
|
|
//
|
|
|
|
// Consider using SetDefaultableHook to register a hook for any module that implements
|
|
|
|
// DefaultableModule as the hook is called after any defaults have been applied to the
|
|
|
|
// module which could reduce duplication and make it easier to use.
|
2016-09-13 22:42:32 +02:00
|
|
|
func AddLoadHook(m blueprint.Module, hook func(LoadHookContext)) {
|
2019-12-31 03:45:15 +01:00
|
|
|
blueprint.AddLoadHook(m, func(ctx blueprint.LoadHookContext) {
|
|
|
|
actx := &loadHookContext{
|
|
|
|
earlyModuleContext: m.(Module).base().earlyModuleContextFactory(ctx),
|
|
|
|
bp: ctx,
|
|
|
|
}
|
|
|
|
hook(actx)
|
|
|
|
})
|
2016-09-13 22:42:32 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 03:45:15 +01:00
|
|
|
type loadHookContext struct {
|
|
|
|
earlyModuleContext
|
|
|
|
bp blueprint.LoadHookContext
|
|
|
|
module Module
|
|
|
|
}
|
|
|
|
|
2019-11-23 01:03:51 +01:00
|
|
|
func (l *loadHookContext) moduleFactories() map[string]blueprint.ModuleFactory {
|
|
|
|
return l.bp.ModuleFactories()
|
|
|
|
}
|
|
|
|
|
2021-11-23 18:31:55 +01:00
|
|
|
func (l *loadHookContext) appendPrependHelper(props []interface{},
|
|
|
|
extendFn func([]interface{}, interface{}, proptools.ExtendPropertyFilterFunc) error) {
|
2019-12-31 03:45:15 +01:00
|
|
|
for _, p := range props {
|
2022-01-06 05:42:33 +01:00
|
|
|
err := extendFn(l.Module().base().GetProperties(), p, nil)
|
2019-12-31 03:45:15 +01:00
|
|
|
if err != nil {
|
|
|
|
if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
|
|
|
|
l.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
|
|
|
|
} else {
|
|
|
|
panic(err)
|
2016-09-13 22:42:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-23 18:31:55 +01:00
|
|
|
func (l *loadHookContext) AppendProperties(props ...interface{}) {
|
|
|
|
l.appendPrependHelper(props, proptools.AppendMatchingProperties)
|
|
|
|
}
|
2016-09-13 22:42:32 +02:00
|
|
|
|
2019-12-31 03:45:15 +01:00
|
|
|
func (l *loadHookContext) PrependProperties(props ...interface{}) {
|
2021-11-23 18:31:55 +01:00
|
|
|
l.appendPrependHelper(props, proptools.PrependMatchingProperties)
|
2019-12-31 03:45:15 +01:00
|
|
|
}
|
|
|
|
|
2022-04-26 15:08:55 +02:00
|
|
|
func (l *loadHookContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module {
|
|
|
|
return l.bp.CreateModule(factory, name, props...)
|
|
|
|
}
|
|
|
|
|
|
|
|
type createModuleContext interface {
|
|
|
|
Module() Module
|
|
|
|
createModule(blueprint.ModuleFactory, string, ...interface{}) blueprint.Module
|
|
|
|
}
|
|
|
|
|
|
|
|
func createModule(ctx createModuleContext, factory ModuleFactory, ext string, props ...interface{}) Module {
|
|
|
|
inherited := []interface{}{&ctx.Module().base().commonProperties}
|
2022-03-09 21:46:37 +01:00
|
|
|
|
|
|
|
var typeName string
|
|
|
|
if typeNameLookup, ok := ModuleTypeByFactory()[reflect.ValueOf(factory)]; ok {
|
|
|
|
typeName = typeNameLookup
|
|
|
|
} else {
|
|
|
|
factoryPtr := reflect.ValueOf(factory).Pointer()
|
|
|
|
factoryFunc := runtime.FuncForPC(factoryPtr)
|
|
|
|
filePath, _ := factoryFunc.FileLine(factoryPtr)
|
|
|
|
typeName = fmt.Sprintf("%s_%s", path.Base(filePath), factoryFunc.Name())
|
|
|
|
}
|
2022-04-26 15:08:55 +02:00
|
|
|
typeName = typeName + "_" + ext
|
2022-03-09 21:46:37 +01:00
|
|
|
|
2022-04-26 15:08:55 +02:00
|
|
|
module := ctx.createModule(ModuleFactoryAdaptor(factory), typeName, append(inherited, props...)...).(Module)
|
2019-12-31 03:45:15 +01:00
|
|
|
|
2022-04-26 15:08:55 +02:00
|
|
|
if ctx.Module().base().variableProperties != nil && module.base().variableProperties != nil {
|
|
|
|
src := ctx.Module().base().variableProperties
|
2019-12-31 03:45:15 +01:00
|
|
|
dst := []interface{}{
|
|
|
|
module.base().variableProperties,
|
|
|
|
// Put an empty copy of the src properties into dst so that properties in src that are not in dst
|
|
|
|
// don't cause a "failed to find property to extend" error.
|
2020-01-28 18:46:50 +01:00
|
|
|
proptools.CloneEmptyProperties(reflect.ValueOf(src)).Interface(),
|
2019-12-31 03:45:15 +01:00
|
|
|
}
|
|
|
|
err := proptools.AppendMatchingProperties(dst, src, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return module
|
|
|
|
}
|
|
|
|
|
2022-04-26 15:08:55 +02:00
|
|
|
func (l *loadHookContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
|
|
|
|
return createModule(l, factory, "_loadHookModule", props...)
|
|
|
|
}
|
|
|
|
|
2019-11-23 01:03:51 +01:00
|
|
|
func (l *loadHookContext) registerScopedModuleType(name string, factory blueprint.ModuleFactory) {
|
|
|
|
l.bp.RegisterScopedModuleType(name, factory)
|
|
|
|
}
|
|
|
|
|
2016-09-13 22:42:32 +02:00
|
|
|
type InstallHookContext interface {
|
|
|
|
ModuleContext
|
2020-06-04 02:26:16 +02:00
|
|
|
SrcPath() Path
|
2019-10-02 07:05:35 +02:00
|
|
|
Path() InstallPath
|
2016-09-13 22:42:32 +02:00
|
|
|
Symlink() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install hooks are run after a module creates a rule to install a file or symlink.
|
|
|
|
// The installed path is available from InstallHookContext.Path(), and
|
|
|
|
// InstallHookContext.Symlink() will be true if it was a symlink.
|
|
|
|
func AddInstallHook(m blueprint.Module, hook func(InstallHookContext)) {
|
|
|
|
h := &m.(Module).base().hooks
|
|
|
|
h.install = append(h.install, hook)
|
|
|
|
}
|
|
|
|
|
|
|
|
type installHookContext struct {
|
|
|
|
ModuleContext
|
2020-06-04 02:26:16 +02:00
|
|
|
srcPath Path
|
2019-10-02 07:05:35 +02:00
|
|
|
path InstallPath
|
2016-09-13 22:42:32 +02:00
|
|
|
symlink bool
|
|
|
|
}
|
|
|
|
|
2020-06-04 02:26:16 +02:00
|
|
|
var _ InstallHookContext = &installHookContext{}
|
|
|
|
|
|
|
|
func (x *installHookContext) SrcPath() Path {
|
|
|
|
return x.srcPath
|
|
|
|
}
|
|
|
|
|
2019-10-02 07:05:35 +02:00
|
|
|
func (x *installHookContext) Path() InstallPath {
|
2016-09-13 22:42:32 +02:00
|
|
|
return x.path
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *installHookContext) Symlink() bool {
|
|
|
|
return x.symlink
|
|
|
|
}
|
|
|
|
|
2020-06-04 02:26:16 +02:00
|
|
|
func (x *hooks) runInstallHooks(ctx ModuleContext, srcPath Path, path InstallPath, symlink bool) {
|
2016-09-13 22:42:32 +02:00
|
|
|
if len(x.install) > 0 {
|
|
|
|
mctx := &installHookContext{
|
|
|
|
ModuleContext: ctx,
|
2020-06-04 02:26:16 +02:00
|
|
|
srcPath: srcPath,
|
2016-09-13 22:42:32 +02:00
|
|
|
path: path,
|
|
|
|
symlink: symlink,
|
|
|
|
}
|
|
|
|
for _, x := range x.install {
|
|
|
|
x(mctx)
|
|
|
|
if mctx.Failed() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type hooks struct {
|
|
|
|
install []func(InstallHookContext)
|
|
|
|
}
|