blob: b33ad40b145d84d2a0d7208f9821819818ee6f6d [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 (
Paul Duffin0a286832019-12-19 12:23:01 +000018 "fmt"
19
Colin Cross4498afc2016-10-13 14:18:27 -070020 "github.com/google/blueprint"
21)
Colin Cross463a90e2015-06-17 14:20:06 -070022
23type moduleType struct {
24 name string
Colin Cross7089c272019-01-25 22:43:35 -080025 factory ModuleFactory
Colin Cross463a90e2015-06-17 14:20:06 -070026}
27
28var moduleTypes []moduleType
29
30type singleton struct {
31 name string
Colin Cross06fa5882020-10-29 18:21:38 -070032 factory SingletonFactory
Colin Cross463a90e2015-06-17 14:20:06 -070033}
34
35var singletons []singleton
Colin Cross5a79c832017-11-07 13:35:38 -080036var preSingletons []singleton
Colin Cross463a90e2015-06-17 14:20:06 -070037
Colin Cross6362e272015-10-29 15:25:03 -070038type mutator struct {
39 name string
40 bottomUpMutator blueprint.BottomUpMutator
41 topDownMutator blueprint.TopDownMutator
Colin Crosse8a67a72016-08-07 21:17:54 -070042 parallel bool
Colin Cross463a90e2015-06-17 14:20:06 -070043}
44
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
Colin Cross06fa5882020-10-29 18:21:38 -070060func SingletonFactoryAdaptor(ctx *Context, factory SingletonFactory) blueprint.SingletonFactory {
Colin Cross0875c522017-11-28 17:34:01 -080061 return func() blueprint.Singleton {
62 singleton := factory()
Colin Crossed023ec2019-02-19 12:38:45 -080063 if makevars, ok := singleton.(SingletonMakeVarsProvider); ok {
Colin Cross06fa5882020-10-29 18:21:38 -070064 registerSingletonMakeVarsProvider(ctx.config, makevars)
Colin Crossed023ec2019-02-19 12:38:45 -080065 }
Colin Cross4c83e5c2019-02-25 14:54:28 -080066 return &singletonAdaptor{Singleton: singleton}
Colin Cross0875c522017-11-28 17:34:01 -080067 }
68}
69
Colin Cross36242852017-06-23 15:06:31 -070070func RegisterModuleType(name string, factory ModuleFactory) {
Colin Cross7089c272019-01-25 22:43:35 -080071 moduleTypes = append(moduleTypes, moduleType{name, factory})
Colin Cross463a90e2015-06-17 14:20:06 -070072}
73
Colin Cross0875c522017-11-28 17:34:01 -080074func RegisterSingletonType(name string, factory SingletonFactory) {
Colin Cross06fa5882020-10-29 18:21:38 -070075 singletons = append(singletons, singleton{name, factory})
Colin Cross463a90e2015-06-17 14:20:06 -070076}
77
Colin Cross0875c522017-11-28 17:34:01 -080078func RegisterPreSingletonType(name string, factory SingletonFactory) {
Colin Cross06fa5882020-10-29 18:21:38 -070079 preSingletons = append(preSingletons, singleton{name, factory})
Colin Cross5a79c832017-11-07 13:35:38 -080080}
81
Colin Crosscec81712017-07-13 14:43:27 -070082type Context struct {
83 *blueprint.Context
Colin Crossae8600b2020-10-29 17:09:13 -070084 config Config
Colin Crosscec81712017-07-13 14:43:27 -070085}
Colin Cross463a90e2015-06-17 14:20:06 -070086
Colin Crossae8600b2020-10-29 17:09:13 -070087func NewContext(config Config) *Context {
88 ctx := &Context{blueprint.NewContext(), config}
Colin Cross988414c2020-01-11 01:11:46 +000089 ctx.SetSrcDir(absSrcDir)
90 return ctx
Colin Crosscec81712017-07-13 14:43:27 -070091}
92
93func (ctx *Context) Register() {
Colin Cross5a79c832017-11-07 13:35:38 -080094 for _, t := range preSingletons {
Colin Cross06fa5882020-10-29 18:21:38 -070095 ctx.RegisterPreSingletonType(t.name, SingletonFactoryAdaptor(ctx, t.factory))
Colin Cross5a79c832017-11-07 13:35:38 -080096 }
97
Colin Cross463a90e2015-06-17 14:20:06 -070098 for _, t := range moduleTypes {
Colin Cross7089c272019-01-25 22:43:35 -080099 ctx.RegisterModuleType(t.name, ModuleFactoryAdaptor(t.factory))
Colin Cross463a90e2015-06-17 14:20:06 -0700100 }
101
102 for _, t := range singletons {
Colin Cross06fa5882020-10-29 18:21:38 -0700103 ctx.RegisterSingletonType(t.name, SingletonFactoryAdaptor(ctx, t.factory))
Colin Cross463a90e2015-06-17 14:20:06 -0700104 }
105
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000106 registerMutators(ctx.Context, preArch, preDeps, postDeps, finalDeps)
Colin Cross1e676be2016-10-12 14:38:15 -0700107
Colin Cross06fa5882020-10-29 18:21:38 -0700108 ctx.RegisterSingletonType("bazeldeps", SingletonFactoryAdaptor(ctx, BazelSingleton))
Chris Parsonsa798d962020-10-12 23:44:08 -0400109
Colin Crossc3d87d32020-06-04 13:25:17 -0700110 // Register phony just before makevars so it can write out its phony rules as Make rules
Colin Cross06fa5882020-10-29 18:21:38 -0700111 ctx.RegisterSingletonType("phony", SingletonFactoryAdaptor(ctx, phonySingletonFactory))
Colin Crossc3d87d32020-06-04 13:25:17 -0700112
Colin Cross580d2ce2019-02-09 22:59:32 -0800113 // Register makevars after other singletons so they can export values through makevars
Colin Cross06fa5882020-10-29 18:21:38 -0700114 ctx.RegisterSingletonType("makevars", SingletonFactoryAdaptor(ctx, makeVarsSingletonFunc))
Colin Cross580d2ce2019-02-09 22:59:32 -0800115
116 // Register env last so that it can track all used environment variables
Colin Cross06fa5882020-10-29 18:21:38 -0700117 ctx.RegisterSingletonType("env", SingletonFactoryAdaptor(ctx, EnvSingleton))
Colin Cross463a90e2015-06-17 14:20:06 -0700118}
Colin Cross7089c272019-01-25 22:43:35 -0800119
120func ModuleTypeFactories() map[string]ModuleFactory {
121 ret := make(map[string]ModuleFactory)
122 for _, t := range moduleTypes {
123 ret[t.name] = t.factory
124 }
125 return ret
126}
Paul Duffinf9b1da02019-12-18 19:51:55 +0000127
128// Interface for registering build components.
129//
130// Provided to allow registration of build components to be shared between the runtime
131// and test environments.
132type RegistrationContext interface {
133 RegisterModuleType(name string, factory ModuleFactory)
134 RegisterSingletonType(name string, factory SingletonFactory)
Paul Duffina48f7582019-12-19 11:25:19 +0000135 PreArchMutators(f RegisterMutatorFunc)
Paul Duffina80ef842020-01-14 12:09:36 +0000136
137 // Register pre arch mutators that are hard coded into mutator.go.
138 //
139 // Only registers mutators for testing, is a noop on the InitRegistrationContext.
140 HardCodedPreArchMutators(f RegisterMutatorFunc)
141
Paul Duffin2ccaffd2019-12-19 15:12:58 +0000142 PreDepsMutators(f RegisterMutatorFunc)
143 PostDepsMutators(f RegisterMutatorFunc)
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000144 FinalDepsMutators(f RegisterMutatorFunc)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000145}
146
147// Used to register build components from an init() method, e.g.
148//
149// init() {
150// RegisterBuildComponents(android.InitRegistrationContext)
151// }
152//
153// func RegisterBuildComponents(ctx android.RegistrationContext) {
154// ctx.RegisterModuleType(...)
155// ...
156// }
157//
158// Extracting the actual registration into a separate RegisterBuildComponents(ctx) function
159// allows it to be used to initialize test context, e.g.
160//
Colin Crossae8600b2020-10-29 17:09:13 -0700161// ctx := android.NewTestContext(config)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000162// RegisterBuildComponents(ctx)
Paul Duffin0a286832019-12-19 12:23:01 +0000163var InitRegistrationContext RegistrationContext = &initRegistrationContext{
164 moduleTypes: make(map[string]ModuleFactory),
165 singletonTypes: make(map[string]SingletonFactory),
166}
Paul Duffinf9b1da02019-12-18 19:51:55 +0000167
168// Make sure the TestContext implements RegistrationContext.
169var _ RegistrationContext = (*TestContext)(nil)
170
Paul Duffin0a286832019-12-19 12:23:01 +0000171type initRegistrationContext struct {
172 moduleTypes map[string]ModuleFactory
173 singletonTypes map[string]SingletonFactory
174}
Paul Duffinf9b1da02019-12-18 19:51:55 +0000175
Paul Duffin0a286832019-12-19 12:23:01 +0000176func (ctx *initRegistrationContext) RegisterModuleType(name string, factory ModuleFactory) {
177 if _, present := ctx.moduleTypes[name]; present {
178 panic(fmt.Sprintf("module type %q is already registered", name))
179 }
180 ctx.moduleTypes[name] = factory
Paul Duffinf9b1da02019-12-18 19:51:55 +0000181 RegisterModuleType(name, factory)
182}
183
Paul Duffin0a286832019-12-19 12:23:01 +0000184func (ctx *initRegistrationContext) RegisterSingletonType(name string, factory SingletonFactory) {
185 if _, present := ctx.singletonTypes[name]; present {
186 panic(fmt.Sprintf("singleton type %q is already registered", name))
187 }
188 ctx.singletonTypes[name] = factory
Paul Duffinf9b1da02019-12-18 19:51:55 +0000189 RegisterSingletonType(name, factory)
190}
Paul Duffina48f7582019-12-19 11:25:19 +0000191
Paul Duffin0a286832019-12-19 12:23:01 +0000192func (ctx *initRegistrationContext) PreArchMutators(f RegisterMutatorFunc) {
Paul Duffina48f7582019-12-19 11:25:19 +0000193 PreArchMutators(f)
194}
Paul Duffin2ccaffd2019-12-19 15:12:58 +0000195
Paul Duffina80ef842020-01-14 12:09:36 +0000196func (ctx *initRegistrationContext) HardCodedPreArchMutators(f RegisterMutatorFunc) {
197 // Nothing to do as the mutators are hard code in preArch in mutator.go
198}
199
Paul Duffin2ccaffd2019-12-19 15:12:58 +0000200func (ctx *initRegistrationContext) PreDepsMutators(f RegisterMutatorFunc) {
201 PreDepsMutators(f)
202}
203
204func (ctx *initRegistrationContext) PostDepsMutators(f RegisterMutatorFunc) {
205 PostDepsMutators(f)
206}
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000207
208func (ctx *initRegistrationContext) FinalDepsMutators(f RegisterMutatorFunc) {
209 FinalDepsMutators(f)
210}