blob: 6cfc205c3513846cbd51500feb8a5e05e597379b [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{}
Sam Delmerico286bf262022-03-09 20:46:37 +000062var moduleTypeByFactory = map[reflect.Value]string{}
Colin Cross463a90e2015-06-17 14:20:06 -070063
64type singleton struct {
Paul Duffin1d2d42f2021-03-06 20:08:12 +000065 // True if this should be registered as a pre-singleton, false otherwise.
66 pre bool
67
Colin Cross463a90e2015-06-17 14:20:06 -070068 name string
Colin Cross06fa5882020-10-29 18:21:38 -070069 factory SingletonFactory
Colin Cross463a90e2015-06-17 14:20:06 -070070}
71
Paul Duffin1d2d42f2021-03-06 20:08:12 +000072func newSingleton(name string, factory SingletonFactory) singleton {
73 return singleton{false, name, factory}
74}
75
76func newPreSingleton(name string, factory SingletonFactory) singleton {
77 return singleton{true, name, factory}
78}
79
80func (s singleton) componentName() string {
81 return s.name
82}
83
84func (s singleton) register(ctx *Context) {
85 adaptor := SingletonFactoryAdaptor(ctx, s.factory)
86 if s.pre {
87 ctx.RegisterPreSingletonType(s.name, adaptor)
88 } else {
89 ctx.RegisterSingletonType(s.name, adaptor)
90 }
91}
92
93var _ sortableComponent = singleton{}
94
95var singletons sortableComponents
96var preSingletons sortableComponents
Colin Cross463a90e2015-06-17 14:20:06 -070097
Colin Cross6362e272015-10-29 15:25:03 -070098type mutator struct {
Lukacs T. Berki6c716762022-06-13 20:50:39 +020099 name string
100 bottomUpMutator blueprint.BottomUpMutator
101 topDownMutator blueprint.TopDownMutator
102 transitionMutator blueprint.TransitionMutator
103 parallel bool
Colin Cross463a90e2015-06-17 14:20:06 -0700104}
105
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000106var _ sortableComponent = &mutator{}
107
Colin Cross36242852017-06-23 15:06:31 -0700108type ModuleFactory func() Module
109
Colin Cross0875c522017-11-28 17:34:01 -0800110// ModuleFactoryAdaptor wraps a ModuleFactory into a blueprint.ModuleFactory by converting a Module
Colin Cross36242852017-06-23 15:06:31 -0700111// into a blueprint.Module and a list of property structs
112func ModuleFactoryAdaptor(factory ModuleFactory) blueprint.ModuleFactory {
113 return func() (blueprint.Module, []interface{}) {
114 module := factory()
115 return module, module.GetProperties()
116 }
117}
118
Colin Cross0875c522017-11-28 17:34:01 -0800119type SingletonFactory func() Singleton
120
121// SingletonFactoryAdaptor wraps a SingletonFactory into a blueprint.SingletonFactory by converting
122// a Singleton into a blueprint.Singleton
Colin Cross06fa5882020-10-29 18:21:38 -0700123func SingletonFactoryAdaptor(ctx *Context, factory SingletonFactory) blueprint.SingletonFactory {
Colin Cross0875c522017-11-28 17:34:01 -0800124 return func() blueprint.Singleton {
125 singleton := factory()
Colin Crossed023ec2019-02-19 12:38:45 -0800126 if makevars, ok := singleton.(SingletonMakeVarsProvider); ok {
Colin Cross06fa5882020-10-29 18:21:38 -0700127 registerSingletonMakeVarsProvider(ctx.config, makevars)
Colin Crossed023ec2019-02-19 12:38:45 -0800128 }
Colin Cross4c83e5c2019-02-25 14:54:28 -0800129 return &singletonAdaptor{Singleton: singleton}
Colin Cross0875c522017-11-28 17:34:01 -0800130 }
131}
132
Colin Cross36242852017-06-23 15:06:31 -0700133func RegisterModuleType(name string, factory ModuleFactory) {
Colin Cross7089c272019-01-25 22:43:35 -0800134 moduleTypes = append(moduleTypes, moduleType{name, factory})
Colin Cross9aed5bc2020-12-28 15:15:34 -0800135 RegisterModuleTypeForDocs(name, reflect.ValueOf(factory))
136}
137
138// RegisterModuleTypeForDocs associates a module type name with a reflect.Value of the factory
139// function that has documentation for the module type. It is normally called automatically
140// by RegisterModuleType, but can be called manually after RegisterModuleType in order to
141// override the factory method used for documentation, for example if the method passed to
142// RegisterModuleType was a lambda.
143func RegisterModuleTypeForDocs(name string, factory reflect.Value) {
144 moduleTypesForDocs[name] = factory
Sam Delmerico286bf262022-03-09 20:46:37 +0000145 moduleTypeByFactory[factory] = name
Colin Cross463a90e2015-06-17 14:20:06 -0700146}
147
Colin Cross0875c522017-11-28 17:34:01 -0800148func RegisterSingletonType(name string, factory SingletonFactory) {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000149 singletons = append(singletons, newSingleton(name, factory))
Colin Cross463a90e2015-06-17 14:20:06 -0700150}
151
Colin Cross0875c522017-11-28 17:34:01 -0800152func RegisterPreSingletonType(name string, factory SingletonFactory) {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000153 preSingletons = append(preSingletons, newPreSingleton(name, factory))
Colin Cross5a79c832017-11-07 13:35:38 -0800154}
155
Colin Crosscec81712017-07-13 14:43:27 -0700156type Context struct {
157 *blueprint.Context
Colin Crossae8600b2020-10-29 17:09:13 -0700158 config Config
Colin Crosscec81712017-07-13 14:43:27 -0700159}
Colin Cross463a90e2015-06-17 14:20:06 -0700160
Colin Crossae8600b2020-10-29 17:09:13 -0700161func NewContext(config Config) *Context {
162 ctx := &Context{blueprint.NewContext(), config}
Colin Cross988414c2020-01-11 01:11:46 +0000163 ctx.SetSrcDir(absSrcDir)
Spandan Dasc5763832022-11-08 18:42:16 +0000164 ctx.AddIncludeTags(config.IncludeTags()...)
Colin Cross988414c2020-01-11 01:11:46 +0000165 return ctx
Colin Crosscec81712017-07-13 14:43:27 -0700166}
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
Spandan Das5af0bd32022-09-28 20:43:08 +0000184// RegisterForApiBazelConversion is similar to RegisterForBazelConversion except that
185// it only generates API targets in the generated workspace
186func (ctx *Context) RegisterForApiBazelConversion() {
187 for _, t := range moduleTypes {
188 t.register(ctx)
189 }
190
191 RegisterMutatorsForApiBazelConversion(ctx, bp2buildPreArchMutators)
192}
193
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500194// Register the pipeline of singletons, module types, and mutators for
195// generating build.ninja and other files for Kati, from Android.bp files.
Colin Crosscec81712017-07-13 14:43:27 -0700196func (ctx *Context) Register() {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000197 preSingletons.registerAll(ctx)
Colin Cross5a79c832017-11-07 13:35:38 -0800198
Colin Cross463a90e2015-06-17 14:20:06 -0700199 for _, t := range moduleTypes {
Paul Duffin1d2d42f2021-03-06 20:08:12 +0000200 t.register(ctx)
Colin Cross463a90e2015-06-17 14:20:06 -0700201 }
202
Paul Duffinc05b0342021-03-06 13:28:13 +0000203 mutators := collateGloballyRegisteredMutators()
204 mutators.registerAll(ctx)
Colin Cross1e676be2016-10-12 14:38:15 -0700205
Paul Duffin42d0b932021-03-06 20:24:50 +0000206 singletons := collateGloballyRegisteredSingletons()
207 singletons.registerAll(ctx)
208}
Chris Parsonsa798d962020-10-12 23:44:08 -0400209
Paul Duffin42d0b932021-03-06 20:24:50 +0000210func collateGloballyRegisteredSingletons() sortableComponents {
211 allSingletons := append(sortableComponents(nil), singletons...)
212 allSingletons = append(allSingletons,
213 singleton{false, "bazeldeps", BazelSingleton},
Colin Crossc3d87d32020-06-04 13:25:17 -0700214
Paul Duffin42d0b932021-03-06 20:24:50 +0000215 // Register phony just before makevars so it can write out its phony rules as Make rules
216 singleton{false, "phony", phonySingletonFactory},
Colin Cross580d2ce2019-02-09 22:59:32 -0800217
Paul Duffin42d0b932021-03-06 20:24:50 +0000218 // Register makevars after other singletons so they can export values through makevars
219 singleton{false, "makevars", makeVarsSingletonFunc},
220
221 // Register env and ninjadeps last so that they can track all used environment variables and
222 // Ninja file dependencies stored in the config.
Paul Duffin42d0b932021-03-06 20:24:50 +0000223 singleton{false, "ninjadeps", ninjaDepsSingletonFactory},
224 )
225
226 return allSingletons
Colin Cross463a90e2015-06-17 14:20:06 -0700227}
Colin Cross7089c272019-01-25 22:43:35 -0800228
229func ModuleTypeFactories() map[string]ModuleFactory {
230 ret := make(map[string]ModuleFactory)
231 for _, t := range moduleTypes {
232 ret[t.name] = t.factory
233 }
234 return ret
235}
Paul Duffinf9b1da02019-12-18 19:51:55 +0000236
Colin Cross9aed5bc2020-12-28 15:15:34 -0800237func ModuleTypeFactoriesForDocs() map[string]reflect.Value {
238 return moduleTypesForDocs
239}
240
Sam Delmerico286bf262022-03-09 20:46:37 +0000241func ModuleTypeByFactory() map[reflect.Value]string {
242 return moduleTypeByFactory
243}
244
Paul Duffinf9b1da02019-12-18 19:51:55 +0000245// Interface for registering build components.
246//
247// Provided to allow registration of build components to be shared between the runtime
248// and test environments.
249type RegistrationContext interface {
250 RegisterModuleType(name string, factory ModuleFactory)
Colin Cross9aed5bc2020-12-28 15:15:34 -0800251 RegisterSingletonModuleType(name string, factory SingletonModuleFactory)
Paul Duffineafc16b2021-02-24 01:43:18 +0000252 RegisterPreSingletonType(name string, factory SingletonFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000253 RegisterSingletonType(name string, factory SingletonFactory)
Paul Duffina48f7582019-12-19 11:25:19 +0000254 PreArchMutators(f RegisterMutatorFunc)
Paul Duffina80ef842020-01-14 12:09:36 +0000255
256 // Register pre arch mutators that are hard coded into mutator.go.
257 //
258 // Only registers mutators for testing, is a noop on the InitRegistrationContext.
259 HardCodedPreArchMutators(f RegisterMutatorFunc)
260
Paul Duffin2ccaffd2019-12-19 15:12:58 +0000261 PreDepsMutators(f RegisterMutatorFunc)
262 PostDepsMutators(f RegisterMutatorFunc)
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000263 FinalDepsMutators(f RegisterMutatorFunc)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000264}
265
266// Used to register build components from an init() method, e.g.
267//
Colin Crossd079e0b2022-08-16 10:27:33 -0700268// init() {
269// RegisterBuildComponents(android.InitRegistrationContext)
270// }
Paul Duffinf9b1da02019-12-18 19:51:55 +0000271//
Colin Crossd079e0b2022-08-16 10:27:33 -0700272// func RegisterBuildComponents(ctx android.RegistrationContext) {
273// ctx.RegisterModuleType(...)
274// ...
275// }
Paul Duffinf9b1da02019-12-18 19:51:55 +0000276//
277// Extracting the actual registration into a separate RegisterBuildComponents(ctx) function
278// allows it to be used to initialize test context, e.g.
279//
Colin Crossd079e0b2022-08-16 10:27:33 -0700280// ctx := android.NewTestContext(config)
281// RegisterBuildComponents(ctx)
Paul Duffin0a286832019-12-19 12:23:01 +0000282var InitRegistrationContext RegistrationContext = &initRegistrationContext{
Paul Duffin42da69d2021-03-22 13:41:36 +0000283 moduleTypes: make(map[string]ModuleFactory),
284 singletonTypes: make(map[string]SingletonFactory),
285 preSingletonTypes: make(map[string]SingletonFactory),
Paul Duffin0a286832019-12-19 12:23:01 +0000286}
Paul Duffinf9b1da02019-12-18 19:51:55 +0000287
288// Make sure the TestContext implements RegistrationContext.
289var _ RegistrationContext = (*TestContext)(nil)
290
Paul Duffin0a286832019-12-19 12:23:01 +0000291type initRegistrationContext struct {
Colin Cross9aed5bc2020-12-28 15:15:34 -0800292 moduleTypes map[string]ModuleFactory
293 singletonTypes map[string]SingletonFactory
Paul Duffineafc16b2021-02-24 01:43:18 +0000294 preSingletonTypes map[string]SingletonFactory
Colin Cross9aed5bc2020-12-28 15:15:34 -0800295 moduleTypesForDocs map[string]reflect.Value
Paul Duffin0a286832019-12-19 12:23:01 +0000296}
Paul Duffinf9b1da02019-12-18 19:51:55 +0000297
Paul Duffin0a286832019-12-19 12:23:01 +0000298func (ctx *initRegistrationContext) RegisterModuleType(name string, factory ModuleFactory) {
299 if _, present := ctx.moduleTypes[name]; present {
300 panic(fmt.Sprintf("module type %q is already registered", name))
301 }
302 ctx.moduleTypes[name] = factory
Paul Duffinf9b1da02019-12-18 19:51:55 +0000303 RegisterModuleType(name, factory)
Colin Cross9aed5bc2020-12-28 15:15:34 -0800304 RegisterModuleTypeForDocs(name, reflect.ValueOf(factory))
305}
306
307func (ctx *initRegistrationContext) RegisterSingletonModuleType(name string, factory SingletonModuleFactory) {
308 s, m := SingletonModuleFactoryAdaptor(name, factory)
309 ctx.RegisterSingletonType(name, s)
310 ctx.RegisterModuleType(name, m)
311 // Overwrite moduleTypesForDocs with the original factory instead of the lambda returned by
312 // SingletonModuleFactoryAdaptor so that docs can find the module type documentation on the
313 // factory method.
314 RegisterModuleTypeForDocs(name, reflect.ValueOf(factory))
Paul Duffinf9b1da02019-12-18 19:51:55 +0000315}
316
Paul Duffin0a286832019-12-19 12:23:01 +0000317func (ctx *initRegistrationContext) RegisterSingletonType(name string, factory SingletonFactory) {
318 if _, present := ctx.singletonTypes[name]; present {
319 panic(fmt.Sprintf("singleton type %q is already registered", name))
320 }
321 ctx.singletonTypes[name] = factory
Paul Duffinf9b1da02019-12-18 19:51:55 +0000322 RegisterSingletonType(name, factory)
323}
Paul Duffina48f7582019-12-19 11:25:19 +0000324
Paul Duffineafc16b2021-02-24 01:43:18 +0000325func (ctx *initRegistrationContext) RegisterPreSingletonType(name string, factory SingletonFactory) {
326 if _, present := ctx.preSingletonTypes[name]; present {
327 panic(fmt.Sprintf("pre singleton type %q is already registered", name))
328 }
329 ctx.preSingletonTypes[name] = factory
330 RegisterPreSingletonType(name, factory)
331}
332
Paul Duffin0a286832019-12-19 12:23:01 +0000333func (ctx *initRegistrationContext) PreArchMutators(f RegisterMutatorFunc) {
Paul Duffina48f7582019-12-19 11:25:19 +0000334 PreArchMutators(f)
335}
Paul Duffin2ccaffd2019-12-19 15:12:58 +0000336
Paul Duffina80ef842020-01-14 12:09:36 +0000337func (ctx *initRegistrationContext) HardCodedPreArchMutators(f RegisterMutatorFunc) {
338 // Nothing to do as the mutators are hard code in preArch in mutator.go
339}
340
Paul Duffin2ccaffd2019-12-19 15:12:58 +0000341func (ctx *initRegistrationContext) PreDepsMutators(f RegisterMutatorFunc) {
342 PreDepsMutators(f)
343}
344
345func (ctx *initRegistrationContext) PostDepsMutators(f RegisterMutatorFunc) {
346 PostDepsMutators(f)
347}
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000348
349func (ctx *initRegistrationContext) FinalDepsMutators(f RegisterMutatorFunc) {
350 FinalDepsMutators(f)
351}