blob: 78ae481c190e9c7f98294474dd703b927c4ac585 [file] [log] [blame]
Colin Cross463a90e2015-06-17 14:20:06 -07001// Copyright 2015 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
Colin Cross798bfce2016-10-12 14:28:16 -070015package android
Colin Cross463a90e2015-06-17 14:20:06 -070016
Colin Cross4498afc2016-10-13 14:18:27 -070017import (
Colin Cross4498afc2016-10-13 14:18:27 -070018 "github.com/google/blueprint"
19)
Colin Cross463a90e2015-06-17 14:20:06 -070020
21type moduleType struct {
22 name string
23 factory blueprint.ModuleFactory
24}
25
26var moduleTypes []moduleType
27
28type singleton struct {
29 name string
30 factory blueprint.SingletonFactory
31}
32
33var singletons []singleton
Colin Cross5a79c832017-11-07 13:35:38 -080034var preSingletons []singleton
Colin Cross463a90e2015-06-17 14:20:06 -070035
Colin Cross6362e272015-10-29 15:25:03 -070036type mutator struct {
37 name string
38 bottomUpMutator blueprint.BottomUpMutator
39 topDownMutator blueprint.TopDownMutator
Colin Crosse8a67a72016-08-07 21:17:54 -070040 parallel bool
Colin Cross463a90e2015-06-17 14:20:06 -070041}
42
Colin Crosse8a67a72016-08-07 21:17:54 -070043var mutators []*mutator
Colin Cross463a90e2015-06-17 14:20:06 -070044
Colin Cross36242852017-06-23 15:06:31 -070045type ModuleFactory func() Module
46
Colin Cross0875c522017-11-28 17:34:01 -080047// ModuleFactoryAdaptor wraps a ModuleFactory into a blueprint.ModuleFactory by converting a Module
Colin Cross36242852017-06-23 15:06:31 -070048// into a blueprint.Module and a list of property structs
49func ModuleFactoryAdaptor(factory ModuleFactory) blueprint.ModuleFactory {
50 return func() (blueprint.Module, []interface{}) {
51 module := factory()
52 return module, module.GetProperties()
53 }
54}
55
Colin Cross0875c522017-11-28 17:34:01 -080056type SingletonFactory func() Singleton
57
58// SingletonFactoryAdaptor wraps a SingletonFactory into a blueprint.SingletonFactory by converting
59// a Singleton into a blueprint.Singleton
60func SingletonFactoryAdaptor(factory SingletonFactory) blueprint.SingletonFactory {
61 return func() blueprint.Singleton {
62 singleton := factory()
63 return singletonAdaptor{singleton}
64 }
65}
66
Colin Cross36242852017-06-23 15:06:31 -070067func RegisterModuleType(name string, factory ModuleFactory) {
68 moduleTypes = append(moduleTypes, moduleType{name, ModuleFactoryAdaptor(factory)})
Colin Cross463a90e2015-06-17 14:20:06 -070069}
70
Colin Cross0875c522017-11-28 17:34:01 -080071func RegisterSingletonType(name string, factory SingletonFactory) {
72 singletons = append(singletons, singleton{name, SingletonFactoryAdaptor(factory)})
Colin Cross463a90e2015-06-17 14:20:06 -070073}
74
Colin Cross0875c522017-11-28 17:34:01 -080075func RegisterPreSingletonType(name string, factory SingletonFactory) {
76 preSingletons = append(preSingletons, singleton{name, SingletonFactoryAdaptor(factory)})
Colin Cross5a79c832017-11-07 13:35:38 -080077}
78
Colin Crosscec81712017-07-13 14:43:27 -070079type Context struct {
80 *blueprint.Context
81}
Colin Cross463a90e2015-06-17 14:20:06 -070082
Colin Crosscec81712017-07-13 14:43:27 -070083func NewContext() *Context {
84 return &Context{blueprint.NewContext()}
85}
86
87func (ctx *Context) Register() {
Colin Cross5a79c832017-11-07 13:35:38 -080088 for _, t := range preSingletons {
89 ctx.RegisterPreSingletonType(t.name, t.factory)
90 }
91
Colin Cross463a90e2015-06-17 14:20:06 -070092 for _, t := range moduleTypes {
93 ctx.RegisterModuleType(t.name, t.factory)
94 }
95
96 for _, t := range singletons {
97 ctx.RegisterSingletonType(t.name, t.factory)
98 }
99
Colin Crosscec81712017-07-13 14:43:27 -0700100 registerMutators(ctx.Context, preArch, preDeps, postDeps)
Colin Cross1e676be2016-10-12 14:38:15 -0700101
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800102 ctx.RegisterSingletonType("env", EnvSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -0700103}