blob: b26f9b97a43d5da2475139cdad9a84d517b8c279 [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
Jingwen Chen4133ce62020-12-02 04:34:15 -050093// RegisterForBazelConversion registers an alternate shadow pipeline of
94// singletons, module types and mutators to register for converting Blueprint
95// files to semantically equivalent BUILD files.
96func (ctx *Context) RegisterForBazelConversion() {
97 for _, t := range moduleTypes {
98 ctx.RegisterModuleType(t.name, ModuleFactoryAdaptor(t.factory))
99 }
100
101 bazelConverterSingleton := singleton{"bp2build", BazelConverterSingleton}
102 ctx.RegisterSingletonType(bazelConverterSingleton.name,
103 SingletonFactoryAdaptor(ctx, bazelConverterSingleton.factory))
104
105 registerMutatorsForBazelConversion(ctx.Context)
106}
107
Colin Crosscec81712017-07-13 14:43:27 -0700108func (ctx *Context) Register() {
Colin Cross5a79c832017-11-07 13:35:38 -0800109 for _, t := range preSingletons {
Colin Cross06fa5882020-10-29 18:21:38 -0700110 ctx.RegisterPreSingletonType(t.name, SingletonFactoryAdaptor(ctx, t.factory))
Colin Cross5a79c832017-11-07 13:35:38 -0800111 }
112
Colin Cross463a90e2015-06-17 14:20:06 -0700113 for _, t := range moduleTypes {
Colin Cross7089c272019-01-25 22:43:35 -0800114 ctx.RegisterModuleType(t.name, ModuleFactoryAdaptor(t.factory))
Colin Cross463a90e2015-06-17 14:20:06 -0700115 }
116
117 for _, t := range singletons {
Colin Cross06fa5882020-10-29 18:21:38 -0700118 ctx.RegisterSingletonType(t.name, SingletonFactoryAdaptor(ctx, t.factory))
Colin Cross463a90e2015-06-17 14:20:06 -0700119 }
120
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000121 registerMutators(ctx.Context, preArch, preDeps, postDeps, finalDeps)
Colin Cross1e676be2016-10-12 14:38:15 -0700122
Colin Cross06fa5882020-10-29 18:21:38 -0700123 ctx.RegisterSingletonType("bazeldeps", SingletonFactoryAdaptor(ctx, BazelSingleton))
Chris Parsonsa798d962020-10-12 23:44:08 -0400124
Colin Crossc3d87d32020-06-04 13:25:17 -0700125 // Register phony just before makevars so it can write out its phony rules as Make rules
Colin Cross06fa5882020-10-29 18:21:38 -0700126 ctx.RegisterSingletonType("phony", SingletonFactoryAdaptor(ctx, phonySingletonFactory))
Colin Crossc3d87d32020-06-04 13:25:17 -0700127
Colin Cross580d2ce2019-02-09 22:59:32 -0800128 // Register makevars after other singletons so they can export values through makevars
Colin Cross06fa5882020-10-29 18:21:38 -0700129 ctx.RegisterSingletonType("makevars", SingletonFactoryAdaptor(ctx, makeVarsSingletonFunc))
Colin Cross580d2ce2019-02-09 22:59:32 -0800130
Colin Cross12129292020-10-29 18:23:58 -0700131 // Register env and ninjadeps last so that they can track all used environment variables and
132 // Ninja file dependencies stored in the config.
Colin Cross06fa5882020-10-29 18:21:38 -0700133 ctx.RegisterSingletonType("env", SingletonFactoryAdaptor(ctx, EnvSingleton))
Colin Cross12129292020-10-29 18:23:58 -0700134 ctx.RegisterSingletonType("ninjadeps", SingletonFactoryAdaptor(ctx, ninjaDepsSingletonFactory))
Colin Cross463a90e2015-06-17 14:20:06 -0700135}
Colin Cross7089c272019-01-25 22:43:35 -0800136
137func ModuleTypeFactories() map[string]ModuleFactory {
138 ret := make(map[string]ModuleFactory)
139 for _, t := range moduleTypes {
140 ret[t.name] = t.factory
141 }
142 return ret
143}
Paul Duffinf9b1da02019-12-18 19:51:55 +0000144
145// Interface for registering build components.
146//
147// Provided to allow registration of build components to be shared between the runtime
148// and test environments.
149type RegistrationContext interface {
150 RegisterModuleType(name string, factory ModuleFactory)
151 RegisterSingletonType(name string, factory SingletonFactory)
Paul Duffina48f7582019-12-19 11:25:19 +0000152 PreArchMutators(f RegisterMutatorFunc)
Paul Duffina80ef842020-01-14 12:09:36 +0000153
154 // Register pre arch mutators that are hard coded into mutator.go.
155 //
156 // Only registers mutators for testing, is a noop on the InitRegistrationContext.
157 HardCodedPreArchMutators(f RegisterMutatorFunc)
158
Paul Duffin2ccaffd2019-12-19 15:12:58 +0000159 PreDepsMutators(f RegisterMutatorFunc)
160 PostDepsMutators(f RegisterMutatorFunc)
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000161 FinalDepsMutators(f RegisterMutatorFunc)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000162}
163
164// Used to register build components from an init() method, e.g.
165//
166// init() {
167// RegisterBuildComponents(android.InitRegistrationContext)
168// }
169//
170// func RegisterBuildComponents(ctx android.RegistrationContext) {
171// ctx.RegisterModuleType(...)
172// ...
173// }
174//
175// Extracting the actual registration into a separate RegisterBuildComponents(ctx) function
176// allows it to be used to initialize test context, e.g.
177//
Colin Crossae8600b2020-10-29 17:09:13 -0700178// ctx := android.NewTestContext(config)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000179// RegisterBuildComponents(ctx)
Paul Duffin0a286832019-12-19 12:23:01 +0000180var InitRegistrationContext RegistrationContext = &initRegistrationContext{
181 moduleTypes: make(map[string]ModuleFactory),
182 singletonTypes: make(map[string]SingletonFactory),
183}
Paul Duffinf9b1da02019-12-18 19:51:55 +0000184
185// Make sure the TestContext implements RegistrationContext.
186var _ RegistrationContext = (*TestContext)(nil)
187
Paul Duffin0a286832019-12-19 12:23:01 +0000188type initRegistrationContext struct {
189 moduleTypes map[string]ModuleFactory
190 singletonTypes map[string]SingletonFactory
191}
Paul Duffinf9b1da02019-12-18 19:51:55 +0000192
Paul Duffin0a286832019-12-19 12:23:01 +0000193func (ctx *initRegistrationContext) RegisterModuleType(name string, factory ModuleFactory) {
194 if _, present := ctx.moduleTypes[name]; present {
195 panic(fmt.Sprintf("module type %q is already registered", name))
196 }
197 ctx.moduleTypes[name] = factory
Paul Duffinf9b1da02019-12-18 19:51:55 +0000198 RegisterModuleType(name, factory)
199}
200
Paul Duffin0a286832019-12-19 12:23:01 +0000201func (ctx *initRegistrationContext) RegisterSingletonType(name string, factory SingletonFactory) {
202 if _, present := ctx.singletonTypes[name]; present {
203 panic(fmt.Sprintf("singleton type %q is already registered", name))
204 }
205 ctx.singletonTypes[name] = factory
Paul Duffinf9b1da02019-12-18 19:51:55 +0000206 RegisterSingletonType(name, factory)
207}
Paul Duffina48f7582019-12-19 11:25:19 +0000208
Paul Duffin0a286832019-12-19 12:23:01 +0000209func (ctx *initRegistrationContext) PreArchMutators(f RegisterMutatorFunc) {
Paul Duffina48f7582019-12-19 11:25:19 +0000210 PreArchMutators(f)
211}
Paul Duffin2ccaffd2019-12-19 15:12:58 +0000212
Paul Duffina80ef842020-01-14 12:09:36 +0000213func (ctx *initRegistrationContext) HardCodedPreArchMutators(f RegisterMutatorFunc) {
214 // Nothing to do as the mutators are hard code in preArch in mutator.go
215}
216
Paul Duffin2ccaffd2019-12-19 15:12:58 +0000217func (ctx *initRegistrationContext) PreDepsMutators(f RegisterMutatorFunc) {
218 PreDepsMutators(f)
219}
220
221func (ctx *initRegistrationContext) PostDepsMutators(f RegisterMutatorFunc) {
222 PostDepsMutators(f)
223}
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000224
225func (ctx *initRegistrationContext) FinalDepsMutators(f RegisterMutatorFunc) {
226 FinalDepsMutators(f)
227}