blob: 10e14e04d1d8127baf5dee356849a0afe44f9759 [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"
Colin Cross9aed5bc2020-12-28 15:15:34 -080019 "reflect"
Paul Duffin0a286832019-12-19 12:23:01 +000020
Colin Cross4498afc2016-10-13 14:18:27 -070021 "github.com/google/blueprint"
22)
Colin Cross463a90e2015-06-17 14:20:06 -070023
Paul Duffin1d2d42f2021-03-06 20:08:12 +000024// A sortable component is one whose registration order affects the order in which it is executed
25// and so affects the behavior of the build system. As a result it is important for the order in
26// which they are registered during tests to match the order used at runtime and so the test
27// infrastructure will sort them to match.
28//
29// The sortable components are mutators, singletons and pre-singletons. Module types are not
30// sortable because their order of registration does not affect the runtime behavior.
31type sortableComponent interface {
32 // componentName returns the name of the component.
33 //
Usta Shresthac725f472022-01-11 02:44:21 -050034 // Uniquely identifies the components within the set of components used at runtime and during
Paul Duffin1d2d42f2021-03-06 20:08:12 +000035 // tests.
36 componentName() string
37
38 // register registers this component in the supplied context.
39 register(ctx *Context)
40}
41
42type sortableComponents []sortableComponent
43
44// registerAll registers all components in this slice with the supplied context.
45func (r sortableComponents) registerAll(ctx *Context) {
46 for _, c := range r {
47 c.register(ctx)
48 }
49}
50
Colin Cross463a90e2015-06-17 14:20:06 -070051type moduleType struct {
52 name string
Colin Cross7089c272019-01-25 22:43:35 -080053 factory ModuleFactory
Colin Cross463a90e2015-06-17 14:20:06 -070054}
55
Paul Duffin1d2d42f2021-03-06 20:08:12 +000056func (t moduleType) register(ctx *Context) {
57 ctx.RegisterModuleType(t.name, ModuleFactoryAdaptor(t.factory))
58}
59
Colin Cross463a90e2015-06-17 14:20:06 -070060var moduleTypes []moduleType
Colin Cross9aed5bc2020-12-28 15:15:34 -080061var moduleTypesForDocs = map[string]reflect.Value{}
Colin Cross463a90e2015-06-17 14:20:06 -070062
63type singleton struct {
Paul Duffin1d2d42f2021-03-06 20:08:12 +000064 // True if this should be registered as a pre-singleton, false otherwise.
65 pre bool
66
Colin Cross463a90e2015-06-17 14:20:06 -070067 name string
Colin Cross06fa5882020-10-29 18:21:38 -070068 factory SingletonFactory
Colin Cross463a90e2015-06-17 14:20:06 -070069}
70
Paul Duffin1d2d42f2021-03-06 20:08:12 +000071func newSingleton(name string, factory SingletonFactory) singleton {
72 return singleton{false, name, factory}
73}
74
75func newPreSingleton(name string, factory SingletonFactory) singleton {
76 return singleton{true, name, factory}
77}
78
79func (s singleton) componentName() string {
80 return s.name
81}
82
83func (s singleton) register(ctx *Context) {
84 adaptor := SingletonFactoryAdaptor(ctx, s.factory)
85 if s.pre {
86 ctx.RegisterPreSingletonType(s.name, adaptor)
87 } else {
88 ctx.RegisterSingletonType(s.name, adaptor)
89 }
90}
91
92var _ sortableComponent = singleton{}
93
94var singletons sortableComponents
95var preSingletons sortableComponents
Colin Cross463a90e2015-06-17 14:20:06 -070096
Colin Cross6362e272015-10-29 15:25:03 -070097type mutator struct {
98 name string
99 bottomUpMutator blueprint.BottomUpMutator
100 topDownMutator blueprint.TopDownMutator
Colin Crosse8a67a72016-08-07 21:17:54 -0700101 parallel bool
Colin Cross463a90e2015-06-17 14:20:06 -0700102}
103
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000104var _ sortableComponent = &mutator{}
105
Colin Cross36242852017-06-23 15:06:31 -0700106type ModuleFactory func() Module
107
Colin Cross0875c522017-11-28 17:34:01 -0800108// ModuleFactoryAdaptor wraps a ModuleFactory into a blueprint.ModuleFactory by converting a Module
Colin Cross36242852017-06-23 15:06:31 -0700109// into a blueprint.Module and a list of property structs
110func ModuleFactoryAdaptor(factory ModuleFactory) blueprint.ModuleFactory {
111 return func() (blueprint.Module, []interface{}) {
112 module := factory()
113 return module, module.GetProperties()
114 }
115}
116
Colin Cross0875c522017-11-28 17:34:01 -0800117type SingletonFactory func() Singleton
118
119// SingletonFactoryAdaptor wraps a SingletonFactory into a blueprint.SingletonFactory by converting
120// a Singleton into a blueprint.Singleton
Colin Cross06fa5882020-10-29 18:21:38 -0700121func SingletonFactoryAdaptor(ctx *Context, factory SingletonFactory) blueprint.SingletonFactory {
Colin Cross0875c522017-11-28 17:34:01 -0800122 return func() blueprint.Singleton {
123 singleton := factory()
Colin Crossed023ec2019-02-19 12:38:45 -0800124 if makevars, ok := singleton.(SingletonMakeVarsProvider); ok {
Colin Cross06fa5882020-10-29 18:21:38 -0700125 registerSingletonMakeVarsProvider(ctx.config, makevars)
Colin Crossed023ec2019-02-19 12:38:45 -0800126 }
Colin Cross4c83e5c2019-02-25 14:54:28 -0800127 return &singletonAdaptor{Singleton: singleton}
Colin Cross0875c522017-11-28 17:34:01 -0800128 }
129}
130
Colin Cross36242852017-06-23 15:06:31 -0700131func RegisterModuleType(name string, factory ModuleFactory) {
Colin Cross7089c272019-01-25 22:43:35 -0800132 moduleTypes = append(moduleTypes, moduleType{name, factory})
Colin Cross9aed5bc2020-12-28 15:15:34 -0800133 RegisterModuleTypeForDocs(name, reflect.ValueOf(factory))
134}
135
136// RegisterModuleTypeForDocs associates a module type name with a reflect.Value of the factory
137// function that has documentation for the module type. It is normally called automatically
138// by RegisterModuleType, but can be called manually after RegisterModuleType in order to
139// override the factory method used for documentation, for example if the method passed to
140// RegisterModuleType was a lambda.
141func RegisterModuleTypeForDocs(name string, factory reflect.Value) {
142 moduleTypesForDocs[name] = factory
Colin Cross463a90e2015-06-17 14:20:06 -0700143}
144
Colin Cross0875c522017-11-28 17:34:01 -0800145func RegisterSingletonType(name string, factory SingletonFactory) {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000146 singletons = append(singletons, newSingleton(name, factory))
Colin Cross463a90e2015-06-17 14:20:06 -0700147}
148
Colin Cross0875c522017-11-28 17:34:01 -0800149func RegisterPreSingletonType(name string, factory SingletonFactory) {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000150 preSingletons = append(preSingletons, newPreSingleton(name, factory))
Colin Cross5a79c832017-11-07 13:35:38 -0800151}
152
Colin Crosscec81712017-07-13 14:43:27 -0700153type Context struct {
154 *blueprint.Context
Colin Crossae8600b2020-10-29 17:09:13 -0700155 config Config
Colin Crosscec81712017-07-13 14:43:27 -0700156}
Colin Cross463a90e2015-06-17 14:20:06 -0700157
Colin Crossae8600b2020-10-29 17:09:13 -0700158func NewContext(config Config) *Context {
159 ctx := &Context{blueprint.NewContext(), config}
Colin Cross988414c2020-01-11 01:11:46 +0000160 ctx.SetSrcDir(absSrcDir)
161 return ctx
Colin Crosscec81712017-07-13 14:43:27 -0700162}
163
Jingwen Chena47f28d2021-11-02 16:43:57 +0000164func (ctx *Context) SetRunningAsBp2build() {
165 ctx.config.runningAsBp2Build = true
166}
167
Jingwen Chen4133ce62020-12-02 04:34:15 -0500168// RegisterForBazelConversion registers an alternate shadow pipeline of
169// singletons, module types and mutators to register for converting Blueprint
170// files to semantically equivalent BUILD files.
171func (ctx *Context) RegisterForBazelConversion() {
172 for _, t := range moduleTypes {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000173 t.register(ctx)
Jingwen Chen4133ce62020-12-02 04:34:15 -0500174 }
175
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500176 // Required for SingletonModule types, even though we are not using them.
177 for _, t := range singletons {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000178 t.register(ctx)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800179 }
Jingwen Chen4133ce62020-12-02 04:34:15 -0500180
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400181 RegisterMutatorsForBazelConversion(ctx, bp2buildPreArchMutators)
Jingwen Chen4133ce62020-12-02 04:34:15 -0500182}
183
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500184// Register the pipeline of singletons, module types, and mutators for
185// generating build.ninja and other files for Kati, from Android.bp files.
Colin Crosscec81712017-07-13 14:43:27 -0700186func (ctx *Context) Register() {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000187 preSingletons.registerAll(ctx)
Colin Cross5a79c832017-11-07 13:35:38 -0800188
Colin Cross463a90e2015-06-17 14:20:06 -0700189 for _, t := range moduleTypes {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000190 t.register(ctx)
Colin Cross463a90e2015-06-17 14:20:06 -0700191 }
192
Paul Duffinc05b0342021-03-06 13:28:13 +0000193 mutators := collateGloballyRegisteredMutators()
194 mutators.registerAll(ctx)
Colin Cross1e676be2016-10-12 14:38:15 -0700195
Paul Duffin42d0b932021-03-06 20:24:50 +0000196 singletons := collateGloballyRegisteredSingletons()
197 singletons.registerAll(ctx)
198}
Chris Parsonsa798d962020-10-12 23:44:08 -0400199
Paul Duffin42d0b932021-03-06 20:24:50 +0000200func collateGloballyRegisteredSingletons() sortableComponents {
201 allSingletons := append(sortableComponents(nil), singletons...)
202 allSingletons = append(allSingletons,
203 singleton{false, "bazeldeps", BazelSingleton},
Colin Crossc3d87d32020-06-04 13:25:17 -0700204
Paul Duffin42d0b932021-03-06 20:24:50 +0000205 // Register phony just before makevars so it can write out its phony rules as Make rules
206 singleton{false, "phony", phonySingletonFactory},
Colin Cross580d2ce2019-02-09 22:59:32 -0800207
Paul Duffin42d0b932021-03-06 20:24:50 +0000208 // Register makevars after other singletons so they can export values through makevars
209 singleton{false, "makevars", makeVarsSingletonFunc},
210
211 // Register env and ninjadeps last so that they can track all used environment variables and
212 // Ninja file dependencies stored in the config.
Paul Duffin42d0b932021-03-06 20:24:50 +0000213 singleton{false, "ninjadeps", ninjaDepsSingletonFactory},
214 )
215
216 return allSingletons
Colin Cross463a90e2015-06-17 14:20:06 -0700217}
Colin Cross7089c272019-01-25 22:43:35 -0800218
219func ModuleTypeFactories() map[string]ModuleFactory {
220 ret := make(map[string]ModuleFactory)
221 for _, t := range moduleTypes {
222 ret[t.name] = t.factory
223 }
224 return ret
225}
Paul Duffinf9b1da02019-12-18 19:51:55 +0000226
Colin Cross9aed5bc2020-12-28 15:15:34 -0800227func ModuleTypeFactoriesForDocs() map[string]reflect.Value {
228 return moduleTypesForDocs
229}
230
Paul Duffinf9b1da02019-12-18 19:51:55 +0000231// Interface for registering build components.
232//
233// Provided to allow registration of build components to be shared between the runtime
234// and test environments.
235type RegistrationContext interface {
236 RegisterModuleType(name string, factory ModuleFactory)
Colin Cross9aed5bc2020-12-28 15:15:34 -0800237 RegisterSingletonModuleType(name string, factory SingletonModuleFactory)
Paul Duffineafc16b2021-02-24 01:43:18 +0000238 RegisterPreSingletonType(name string, factory SingletonFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000239 RegisterSingletonType(name string, factory SingletonFactory)
Paul Duffina48f7582019-12-19 11:25:19 +0000240 PreArchMutators(f RegisterMutatorFunc)
Paul Duffina80ef842020-01-14 12:09:36 +0000241
242 // Register pre arch mutators that are hard coded into mutator.go.
243 //
244 // Only registers mutators for testing, is a noop on the InitRegistrationContext.
245 HardCodedPreArchMutators(f RegisterMutatorFunc)
246
Paul Duffin2ccaffd2019-12-19 15:12:58 +0000247 PreDepsMutators(f RegisterMutatorFunc)
248 PostDepsMutators(f RegisterMutatorFunc)
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000249 FinalDepsMutators(f RegisterMutatorFunc)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000250}
251
252// Used to register build components from an init() method, e.g.
253//
254// init() {
255// RegisterBuildComponents(android.InitRegistrationContext)
256// }
257//
258// func RegisterBuildComponents(ctx android.RegistrationContext) {
259// ctx.RegisterModuleType(...)
260// ...
261// }
262//
263// Extracting the actual registration into a separate RegisterBuildComponents(ctx) function
264// allows it to be used to initialize test context, e.g.
265//
Colin Crossae8600b2020-10-29 17:09:13 -0700266// ctx := android.NewTestContext(config)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000267// RegisterBuildComponents(ctx)
Paul Duffin0a286832019-12-19 12:23:01 +0000268var InitRegistrationContext RegistrationContext = &initRegistrationContext{
Paul Duffin42da69d2021-03-22 13:41:36 +0000269 moduleTypes: make(map[string]ModuleFactory),
270 singletonTypes: make(map[string]SingletonFactory),
271 preSingletonTypes: make(map[string]SingletonFactory),
Paul Duffin0a286832019-12-19 12:23:01 +0000272}
Paul Duffinf9b1da02019-12-18 19:51:55 +0000273
274// Make sure the TestContext implements RegistrationContext.
275var _ RegistrationContext = (*TestContext)(nil)
276
Paul Duffin0a286832019-12-19 12:23:01 +0000277type initRegistrationContext struct {
Colin Cross9aed5bc2020-12-28 15:15:34 -0800278 moduleTypes map[string]ModuleFactory
279 singletonTypes map[string]SingletonFactory
Paul Duffineafc16b2021-02-24 01:43:18 +0000280 preSingletonTypes map[string]SingletonFactory
Colin Cross9aed5bc2020-12-28 15:15:34 -0800281 moduleTypesForDocs map[string]reflect.Value
Paul Duffin0a286832019-12-19 12:23:01 +0000282}
Paul Duffinf9b1da02019-12-18 19:51:55 +0000283
Paul Duffin0a286832019-12-19 12:23:01 +0000284func (ctx *initRegistrationContext) RegisterModuleType(name string, factory ModuleFactory) {
285 if _, present := ctx.moduleTypes[name]; present {
286 panic(fmt.Sprintf("module type %q is already registered", name))
287 }
288 ctx.moduleTypes[name] = factory
Paul Duffinf9b1da02019-12-18 19:51:55 +0000289 RegisterModuleType(name, factory)
Colin Cross9aed5bc2020-12-28 15:15:34 -0800290 RegisterModuleTypeForDocs(name, reflect.ValueOf(factory))
291}
292
293func (ctx *initRegistrationContext) RegisterSingletonModuleType(name string, factory SingletonModuleFactory) {
294 s, m := SingletonModuleFactoryAdaptor(name, factory)
295 ctx.RegisterSingletonType(name, s)
296 ctx.RegisterModuleType(name, m)
297 // Overwrite moduleTypesForDocs with the original factory instead of the lambda returned by
298 // SingletonModuleFactoryAdaptor so that docs can find the module type documentation on the
299 // factory method.
300 RegisterModuleTypeForDocs(name, reflect.ValueOf(factory))
Paul Duffinf9b1da02019-12-18 19:51:55 +0000301}
302
Paul Duffin0a286832019-12-19 12:23:01 +0000303func (ctx *initRegistrationContext) RegisterSingletonType(name string, factory SingletonFactory) {
304 if _, present := ctx.singletonTypes[name]; present {
305 panic(fmt.Sprintf("singleton type %q is already registered", name))
306 }
307 ctx.singletonTypes[name] = factory
Paul Duffinf9b1da02019-12-18 19:51:55 +0000308 RegisterSingletonType(name, factory)
309}
Paul Duffina48f7582019-12-19 11:25:19 +0000310
Paul Duffineafc16b2021-02-24 01:43:18 +0000311func (ctx *initRegistrationContext) RegisterPreSingletonType(name string, factory SingletonFactory) {
312 if _, present := ctx.preSingletonTypes[name]; present {
313 panic(fmt.Sprintf("pre singleton type %q is already registered", name))
314 }
315 ctx.preSingletonTypes[name] = factory
316 RegisterPreSingletonType(name, factory)
317}
318
Paul Duffin0a286832019-12-19 12:23:01 +0000319func (ctx *initRegistrationContext) PreArchMutators(f RegisterMutatorFunc) {
Paul Duffina48f7582019-12-19 11:25:19 +0000320 PreArchMutators(f)
321}
Paul Duffin2ccaffd2019-12-19 15:12:58 +0000322
Paul Duffina80ef842020-01-14 12:09:36 +0000323func (ctx *initRegistrationContext) HardCodedPreArchMutators(f RegisterMutatorFunc) {
324 // Nothing to do as the mutators are hard code in preArch in mutator.go
325}
326
Paul Duffin2ccaffd2019-12-19 15:12:58 +0000327func (ctx *initRegistrationContext) PreDepsMutators(f RegisterMutatorFunc) {
328 PreDepsMutators(f)
329}
330
331func (ctx *initRegistrationContext) PostDepsMutators(f RegisterMutatorFunc) {
332 PostDepsMutators(f)
333}
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000334
335func (ctx *initRegistrationContext) FinalDepsMutators(f RegisterMutatorFunc) {
336 FinalDepsMutators(f)
337}