blob: 47df9729ef020f50817baafd9a7b57d6b16b4990 [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
24type moduleType struct {
25 name string
Colin Cross7089c272019-01-25 22:43:35 -080026 factory ModuleFactory
Colin Cross463a90e2015-06-17 14:20:06 -070027}
28
29var moduleTypes []moduleType
Colin Cross9aed5bc2020-12-28 15:15:34 -080030var moduleTypesForDocs = map[string]reflect.Value{}
Colin Cross463a90e2015-06-17 14:20:06 -070031
32type singleton struct {
33 name string
Colin Cross06fa5882020-10-29 18:21:38 -070034 factory SingletonFactory
Colin Cross463a90e2015-06-17 14:20:06 -070035}
36
37var singletons []singleton
Colin Cross5a79c832017-11-07 13:35:38 -080038var preSingletons []singleton
Colin Cross463a90e2015-06-17 14:20:06 -070039
Colin Cross6362e272015-10-29 15:25:03 -070040type mutator struct {
41 name string
42 bottomUpMutator blueprint.BottomUpMutator
43 topDownMutator blueprint.TopDownMutator
Colin Crosse8a67a72016-08-07 21:17:54 -070044 parallel bool
Colin Cross463a90e2015-06-17 14:20:06 -070045}
46
Colin Cross36242852017-06-23 15:06:31 -070047type ModuleFactory func() Module
48
Colin Cross0875c522017-11-28 17:34:01 -080049// ModuleFactoryAdaptor wraps a ModuleFactory into a blueprint.ModuleFactory by converting a Module
Colin Cross36242852017-06-23 15:06:31 -070050// into a blueprint.Module and a list of property structs
51func ModuleFactoryAdaptor(factory ModuleFactory) blueprint.ModuleFactory {
52 return func() (blueprint.Module, []interface{}) {
53 module := factory()
54 return module, module.GetProperties()
55 }
56}
57
Colin Cross0875c522017-11-28 17:34:01 -080058type SingletonFactory func() Singleton
59
60// SingletonFactoryAdaptor wraps a SingletonFactory into a blueprint.SingletonFactory by converting
61// a Singleton into a blueprint.Singleton
Colin Cross06fa5882020-10-29 18:21:38 -070062func SingletonFactoryAdaptor(ctx *Context, factory SingletonFactory) blueprint.SingletonFactory {
Colin Cross0875c522017-11-28 17:34:01 -080063 return func() blueprint.Singleton {
64 singleton := factory()
Colin Crossed023ec2019-02-19 12:38:45 -080065 if makevars, ok := singleton.(SingletonMakeVarsProvider); ok {
Colin Cross06fa5882020-10-29 18:21:38 -070066 registerSingletonMakeVarsProvider(ctx.config, makevars)
Colin Crossed023ec2019-02-19 12:38:45 -080067 }
Colin Cross4c83e5c2019-02-25 14:54:28 -080068 return &singletonAdaptor{Singleton: singleton}
Colin Cross0875c522017-11-28 17:34:01 -080069 }
70}
71
Colin Cross36242852017-06-23 15:06:31 -070072func RegisterModuleType(name string, factory ModuleFactory) {
Colin Cross7089c272019-01-25 22:43:35 -080073 moduleTypes = append(moduleTypes, moduleType{name, factory})
Colin Cross9aed5bc2020-12-28 15:15:34 -080074 RegisterModuleTypeForDocs(name, reflect.ValueOf(factory))
75}
76
77// RegisterModuleTypeForDocs associates a module type name with a reflect.Value of the factory
78// function that has documentation for the module type. It is normally called automatically
79// by RegisterModuleType, but can be called manually after RegisterModuleType in order to
80// override the factory method used for documentation, for example if the method passed to
81// RegisterModuleType was a lambda.
82func RegisterModuleTypeForDocs(name string, factory reflect.Value) {
83 moduleTypesForDocs[name] = factory
Colin Cross463a90e2015-06-17 14:20:06 -070084}
85
Colin Cross0875c522017-11-28 17:34:01 -080086func RegisterSingletonType(name string, factory SingletonFactory) {
Colin Cross06fa5882020-10-29 18:21:38 -070087 singletons = append(singletons, singleton{name, factory})
Colin Cross463a90e2015-06-17 14:20:06 -070088}
89
Colin Cross0875c522017-11-28 17:34:01 -080090func RegisterPreSingletonType(name string, factory SingletonFactory) {
Colin Cross06fa5882020-10-29 18:21:38 -070091 preSingletons = append(preSingletons, singleton{name, factory})
Colin Cross5a79c832017-11-07 13:35:38 -080092}
93
Colin Crosscec81712017-07-13 14:43:27 -070094type Context struct {
95 *blueprint.Context
Colin Crossae8600b2020-10-29 17:09:13 -070096 config Config
Colin Crosscec81712017-07-13 14:43:27 -070097}
Colin Cross463a90e2015-06-17 14:20:06 -070098
Colin Crossae8600b2020-10-29 17:09:13 -070099func NewContext(config Config) *Context {
100 ctx := &Context{blueprint.NewContext(), config}
Colin Cross988414c2020-01-11 01:11:46 +0000101 ctx.SetSrcDir(absSrcDir)
102 return ctx
Colin Crosscec81712017-07-13 14:43:27 -0700103}
104
Jingwen Chen4133ce62020-12-02 04:34:15 -0500105// RegisterForBazelConversion registers an alternate shadow pipeline of
106// singletons, module types and mutators to register for converting Blueprint
107// files to semantically equivalent BUILD files.
108func (ctx *Context) RegisterForBazelConversion() {
109 for _, t := range moduleTypes {
110 ctx.RegisterModuleType(t.name, ModuleFactoryAdaptor(t.factory))
111 }
112
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500113 // Required for SingletonModule types, even though we are not using them.
114 for _, t := range singletons {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800115 ctx.RegisterSingletonType(t.name, SingletonFactoryAdaptor(ctx, t.factory))
116 }
Jingwen Chen4133ce62020-12-02 04:34:15 -0500117
Liz Kammer356f7d42021-01-26 09:18:53 -0500118 RegisterMutatorsForBazelConversion(ctx.Context, bp2buildPreArchMutators, bp2buildDepsMutators, bp2buildMutators)
Jingwen Chen4133ce62020-12-02 04:34:15 -0500119}
120
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500121// Register the pipeline of singletons, module types, and mutators for
122// generating build.ninja and other files for Kati, from Android.bp files.
Colin Crosscec81712017-07-13 14:43:27 -0700123func (ctx *Context) Register() {
Colin Cross5a79c832017-11-07 13:35:38 -0800124 for _, t := range preSingletons {
Colin Cross06fa5882020-10-29 18:21:38 -0700125 ctx.RegisterPreSingletonType(t.name, SingletonFactoryAdaptor(ctx, t.factory))
Colin Cross5a79c832017-11-07 13:35:38 -0800126 }
127
Colin Cross463a90e2015-06-17 14:20:06 -0700128 for _, t := range moduleTypes {
Colin Cross7089c272019-01-25 22:43:35 -0800129 ctx.RegisterModuleType(t.name, ModuleFactoryAdaptor(t.factory))
Colin Cross463a90e2015-06-17 14:20:06 -0700130 }
131
132 for _, t := range singletons {
Colin Cross06fa5882020-10-29 18:21:38 -0700133 ctx.RegisterSingletonType(t.name, SingletonFactoryAdaptor(ctx, t.factory))
Colin Cross463a90e2015-06-17 14:20:06 -0700134 }
135
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000136 registerMutators(ctx.Context, preArch, preDeps, postDeps, finalDeps)
Colin Cross1e676be2016-10-12 14:38:15 -0700137
Colin Cross06fa5882020-10-29 18:21:38 -0700138 ctx.RegisterSingletonType("bazeldeps", SingletonFactoryAdaptor(ctx, BazelSingleton))
Chris Parsonsa798d962020-10-12 23:44:08 -0400139
Colin Crossc3d87d32020-06-04 13:25:17 -0700140 // Register phony just before makevars so it can write out its phony rules as Make rules
Colin Cross06fa5882020-10-29 18:21:38 -0700141 ctx.RegisterSingletonType("phony", SingletonFactoryAdaptor(ctx, phonySingletonFactory))
Colin Crossc3d87d32020-06-04 13:25:17 -0700142
Colin Cross580d2ce2019-02-09 22:59:32 -0800143 // Register makevars after other singletons so they can export values through makevars
Colin Cross06fa5882020-10-29 18:21:38 -0700144 ctx.RegisterSingletonType("makevars", SingletonFactoryAdaptor(ctx, makeVarsSingletonFunc))
Colin Cross580d2ce2019-02-09 22:59:32 -0800145
Colin Cross12129292020-10-29 18:23:58 -0700146 // Register env and ninjadeps last so that they can track all used environment variables and
147 // Ninja file dependencies stored in the config.
Colin Cross06fa5882020-10-29 18:21:38 -0700148 ctx.RegisterSingletonType("env", SingletonFactoryAdaptor(ctx, EnvSingleton))
Colin Cross12129292020-10-29 18:23:58 -0700149 ctx.RegisterSingletonType("ninjadeps", SingletonFactoryAdaptor(ctx, ninjaDepsSingletonFactory))
Colin Cross463a90e2015-06-17 14:20:06 -0700150}
Colin Cross7089c272019-01-25 22:43:35 -0800151
152func ModuleTypeFactories() map[string]ModuleFactory {
153 ret := make(map[string]ModuleFactory)
154 for _, t := range moduleTypes {
155 ret[t.name] = t.factory
156 }
157 return ret
158}
Paul Duffinf9b1da02019-12-18 19:51:55 +0000159
Colin Cross9aed5bc2020-12-28 15:15:34 -0800160func ModuleTypeFactoriesForDocs() map[string]reflect.Value {
161 return moduleTypesForDocs
162}
163
Paul Duffinf9b1da02019-12-18 19:51:55 +0000164// Interface for registering build components.
165//
166// Provided to allow registration of build components to be shared between the runtime
167// and test environments.
168type RegistrationContext interface {
169 RegisterModuleType(name string, factory ModuleFactory)
Colin Cross9aed5bc2020-12-28 15:15:34 -0800170 RegisterSingletonModuleType(name string, factory SingletonModuleFactory)
Paul Duffineafc16b2021-02-24 01:43:18 +0000171 RegisterPreSingletonType(name string, factory SingletonFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000172 RegisterSingletonType(name string, factory SingletonFactory)
Paul Duffina48f7582019-12-19 11:25:19 +0000173 PreArchMutators(f RegisterMutatorFunc)
Paul Duffina80ef842020-01-14 12:09:36 +0000174
175 // Register pre arch mutators that are hard coded into mutator.go.
176 //
177 // Only registers mutators for testing, is a noop on the InitRegistrationContext.
178 HardCodedPreArchMutators(f RegisterMutatorFunc)
179
Paul Duffin2ccaffd2019-12-19 15:12:58 +0000180 PreDepsMutators(f RegisterMutatorFunc)
181 PostDepsMutators(f RegisterMutatorFunc)
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000182 FinalDepsMutators(f RegisterMutatorFunc)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000183}
184
185// Used to register build components from an init() method, e.g.
186//
187// init() {
188// RegisterBuildComponents(android.InitRegistrationContext)
189// }
190//
191// func RegisterBuildComponents(ctx android.RegistrationContext) {
192// ctx.RegisterModuleType(...)
193// ...
194// }
195//
196// Extracting the actual registration into a separate RegisterBuildComponents(ctx) function
197// allows it to be used to initialize test context, e.g.
198//
Colin Crossae8600b2020-10-29 17:09:13 -0700199// ctx := android.NewTestContext(config)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000200// RegisterBuildComponents(ctx)
Paul Duffin0a286832019-12-19 12:23:01 +0000201var InitRegistrationContext RegistrationContext = &initRegistrationContext{
202 moduleTypes: make(map[string]ModuleFactory),
203 singletonTypes: make(map[string]SingletonFactory),
204}
Paul Duffinf9b1da02019-12-18 19:51:55 +0000205
206// Make sure the TestContext implements RegistrationContext.
207var _ RegistrationContext = (*TestContext)(nil)
208
Paul Duffin0a286832019-12-19 12:23:01 +0000209type initRegistrationContext struct {
Colin Cross9aed5bc2020-12-28 15:15:34 -0800210 moduleTypes map[string]ModuleFactory
211 singletonTypes map[string]SingletonFactory
Paul Duffineafc16b2021-02-24 01:43:18 +0000212 preSingletonTypes map[string]SingletonFactory
Colin Cross9aed5bc2020-12-28 15:15:34 -0800213 moduleTypesForDocs map[string]reflect.Value
Paul Duffin0a286832019-12-19 12:23:01 +0000214}
Paul Duffinf9b1da02019-12-18 19:51:55 +0000215
Paul Duffin0a286832019-12-19 12:23:01 +0000216func (ctx *initRegistrationContext) RegisterModuleType(name string, factory ModuleFactory) {
217 if _, present := ctx.moduleTypes[name]; present {
218 panic(fmt.Sprintf("module type %q is already registered", name))
219 }
220 ctx.moduleTypes[name] = factory
Paul Duffinf9b1da02019-12-18 19:51:55 +0000221 RegisterModuleType(name, factory)
Colin Cross9aed5bc2020-12-28 15:15:34 -0800222 RegisterModuleTypeForDocs(name, reflect.ValueOf(factory))
223}
224
225func (ctx *initRegistrationContext) RegisterSingletonModuleType(name string, factory SingletonModuleFactory) {
226 s, m := SingletonModuleFactoryAdaptor(name, factory)
227 ctx.RegisterSingletonType(name, s)
228 ctx.RegisterModuleType(name, m)
229 // Overwrite moduleTypesForDocs with the original factory instead of the lambda returned by
230 // SingletonModuleFactoryAdaptor so that docs can find the module type documentation on the
231 // factory method.
232 RegisterModuleTypeForDocs(name, reflect.ValueOf(factory))
Paul Duffinf9b1da02019-12-18 19:51:55 +0000233}
234
Paul Duffin0a286832019-12-19 12:23:01 +0000235func (ctx *initRegistrationContext) RegisterSingletonType(name string, factory SingletonFactory) {
236 if _, present := ctx.singletonTypes[name]; present {
237 panic(fmt.Sprintf("singleton type %q is already registered", name))
238 }
239 ctx.singletonTypes[name] = factory
Paul Duffinf9b1da02019-12-18 19:51:55 +0000240 RegisterSingletonType(name, factory)
241}
Paul Duffina48f7582019-12-19 11:25:19 +0000242
Paul Duffineafc16b2021-02-24 01:43:18 +0000243func (ctx *initRegistrationContext) RegisterPreSingletonType(name string, factory SingletonFactory) {
244 if _, present := ctx.preSingletonTypes[name]; present {
245 panic(fmt.Sprintf("pre singleton type %q is already registered", name))
246 }
247 ctx.preSingletonTypes[name] = factory
248 RegisterPreSingletonType(name, factory)
249}
250
Paul Duffin0a286832019-12-19 12:23:01 +0000251func (ctx *initRegistrationContext) PreArchMutators(f RegisterMutatorFunc) {
Paul Duffina48f7582019-12-19 11:25:19 +0000252 PreArchMutators(f)
253}
Paul Duffin2ccaffd2019-12-19 15:12:58 +0000254
Paul Duffina80ef842020-01-14 12:09:36 +0000255func (ctx *initRegistrationContext) HardCodedPreArchMutators(f RegisterMutatorFunc) {
256 // Nothing to do as the mutators are hard code in preArch in mutator.go
257}
258
Paul Duffin2ccaffd2019-12-19 15:12:58 +0000259func (ctx *initRegistrationContext) PreDepsMutators(f RegisterMutatorFunc) {
260 PreDepsMutators(f)
261}
262
263func (ctx *initRegistrationContext) PostDepsMutators(f RegisterMutatorFunc) {
264 PostDepsMutators(f)
265}
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000266
267func (ctx *initRegistrationContext) FinalDepsMutators(f RegisterMutatorFunc) {
268 FinalDepsMutators(f)
269}