blob: 19745fedbbbc0e35f409bbd5f82281dcb8d7e81e [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
Colin Cross7089c272019-01-25 22:43:35 -080023 factory ModuleFactory
Colin Cross463a90e2015-06-17 14:20:06 -070024}
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 Cross36242852017-06-23 15:06:31 -070043type ModuleFactory func() Module
44
Colin Cross0875c522017-11-28 17:34:01 -080045// ModuleFactoryAdaptor wraps a ModuleFactory into a blueprint.ModuleFactory by converting a Module
Colin Cross36242852017-06-23 15:06:31 -070046// into a blueprint.Module and a list of property structs
47func ModuleFactoryAdaptor(factory ModuleFactory) blueprint.ModuleFactory {
48 return func() (blueprint.Module, []interface{}) {
49 module := factory()
50 return module, module.GetProperties()
51 }
52}
53
Colin Cross0875c522017-11-28 17:34:01 -080054type SingletonFactory func() Singleton
55
56// SingletonFactoryAdaptor wraps a SingletonFactory into a blueprint.SingletonFactory by converting
57// a Singleton into a blueprint.Singleton
58func SingletonFactoryAdaptor(factory SingletonFactory) blueprint.SingletonFactory {
59 return func() blueprint.Singleton {
60 singleton := factory()
61 return singletonAdaptor{singleton}
62 }
63}
64
Colin Cross36242852017-06-23 15:06:31 -070065func RegisterModuleType(name string, factory ModuleFactory) {
Colin Cross7089c272019-01-25 22:43:35 -080066 moduleTypes = append(moduleTypes, moduleType{name, factory})
Colin Cross463a90e2015-06-17 14:20:06 -070067}
68
Colin Cross0875c522017-11-28 17:34:01 -080069func RegisterSingletonType(name string, factory SingletonFactory) {
70 singletons = append(singletons, singleton{name, SingletonFactoryAdaptor(factory)})
Colin Cross463a90e2015-06-17 14:20:06 -070071}
72
Colin Cross0875c522017-11-28 17:34:01 -080073func RegisterPreSingletonType(name string, factory SingletonFactory) {
74 preSingletons = append(preSingletons, singleton{name, SingletonFactoryAdaptor(factory)})
Colin Cross5a79c832017-11-07 13:35:38 -080075}
76
Colin Crosscec81712017-07-13 14:43:27 -070077type Context struct {
78 *blueprint.Context
79}
Colin Cross463a90e2015-06-17 14:20:06 -070080
Colin Crosscec81712017-07-13 14:43:27 -070081func NewContext() *Context {
82 return &Context{blueprint.NewContext()}
83}
84
85func (ctx *Context) Register() {
Colin Cross5a79c832017-11-07 13:35:38 -080086 for _, t := range preSingletons {
87 ctx.RegisterPreSingletonType(t.name, t.factory)
88 }
89
Colin Cross463a90e2015-06-17 14:20:06 -070090 for _, t := range moduleTypes {
Colin Cross7089c272019-01-25 22:43:35 -080091 ctx.RegisterModuleType(t.name, ModuleFactoryAdaptor(t.factory))
Colin Cross463a90e2015-06-17 14:20:06 -070092 }
93
94 for _, t := range singletons {
95 ctx.RegisterSingletonType(t.name, t.factory)
96 }
97
Colin Crosscec81712017-07-13 14:43:27 -070098 registerMutators(ctx.Context, preArch, preDeps, postDeps)
Colin Cross1e676be2016-10-12 14:38:15 -070099
Colin Cross580d2ce2019-02-09 22:59:32 -0800100 // Register makevars after other singletons so they can export values through makevars
101 ctx.RegisterSingletonType("makevars", SingletonFactoryAdaptor(makeVarsSingletonFunc))
102
103 // Register env last so that it can track all used environment variables
Colin Cross54855dd2017-11-28 23:55:23 -0800104 ctx.RegisterSingletonType("env", SingletonFactoryAdaptor(EnvSingleton))
Colin Cross463a90e2015-06-17 14:20:06 -0700105}
Colin Cross7089c272019-01-25 22:43:35 -0800106
107func ModuleTypeFactories() map[string]ModuleFactory {
108 ret := make(map[string]ModuleFactory)
109 for _, t := range moduleTypes {
110 ret[t.name] = t.factory
111 }
112 return ret
113}