blob: 995be0c3bc7c9f32c5c3b4277b646a0f67d501cf [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
Liz Kammer2dd9ca42020-11-25 16:06:39 -080038var bazelConverterSingletons []singleton
39var bazelConverterPreSingletons []singleton
40
Colin Cross6362e272015-10-29 15:25:03 -070041type mutator struct {
42 name string
43 bottomUpMutator blueprint.BottomUpMutator
44 topDownMutator blueprint.TopDownMutator
Colin Crosse8a67a72016-08-07 21:17:54 -070045 parallel bool
Colin Cross463a90e2015-06-17 14:20:06 -070046}
47
Colin Cross36242852017-06-23 15:06:31 -070048type ModuleFactory func() Module
49
Colin Cross0875c522017-11-28 17:34:01 -080050// ModuleFactoryAdaptor wraps a ModuleFactory into a blueprint.ModuleFactory by converting a Module
Colin Cross36242852017-06-23 15:06:31 -070051// into a blueprint.Module and a list of property structs
52func ModuleFactoryAdaptor(factory ModuleFactory) blueprint.ModuleFactory {
53 return func() (blueprint.Module, []interface{}) {
54 module := factory()
55 return module, module.GetProperties()
56 }
57}
58
Colin Cross0875c522017-11-28 17:34:01 -080059type SingletonFactory func() Singleton
60
61// SingletonFactoryAdaptor wraps a SingletonFactory into a blueprint.SingletonFactory by converting
62// a Singleton into a blueprint.Singleton
Colin Cross06fa5882020-10-29 18:21:38 -070063func SingletonFactoryAdaptor(ctx *Context, factory SingletonFactory) blueprint.SingletonFactory {
Colin Cross0875c522017-11-28 17:34:01 -080064 return func() blueprint.Singleton {
65 singleton := factory()
Colin Crossed023ec2019-02-19 12:38:45 -080066 if makevars, ok := singleton.(SingletonMakeVarsProvider); ok {
Colin Cross06fa5882020-10-29 18:21:38 -070067 registerSingletonMakeVarsProvider(ctx.config, makevars)
Colin Crossed023ec2019-02-19 12:38:45 -080068 }
Colin Cross4c83e5c2019-02-25 14:54:28 -080069 return &singletonAdaptor{Singleton: singleton}
Colin Cross0875c522017-11-28 17:34:01 -080070 }
71}
72
Colin Cross36242852017-06-23 15:06:31 -070073func RegisterModuleType(name string, factory ModuleFactory) {
Colin Cross7089c272019-01-25 22:43:35 -080074 moduleTypes = append(moduleTypes, moduleType{name, factory})
Colin Cross463a90e2015-06-17 14:20:06 -070075}
76
Colin Cross0875c522017-11-28 17:34:01 -080077func RegisterSingletonType(name string, factory SingletonFactory) {
Colin Cross06fa5882020-10-29 18:21:38 -070078 singletons = append(singletons, singleton{name, factory})
Colin Cross463a90e2015-06-17 14:20:06 -070079}
80
Colin Cross0875c522017-11-28 17:34:01 -080081func RegisterPreSingletonType(name string, factory SingletonFactory) {
Colin Cross06fa5882020-10-29 18:21:38 -070082 preSingletons = append(preSingletons, singleton{name, factory})
Colin Cross5a79c832017-11-07 13:35:38 -080083}
84
Liz Kammer2dd9ca42020-11-25 16:06:39 -080085func RegisterBazelConverterSingletonType(name string, factory SingletonFactory) {
86 bazelConverterSingletons = append(bazelConverterSingletons, singleton{name, factory})
87}
88
89func RegisterBazelConverterPreSingletonType(name string, factory SingletonFactory) {
90 bazelConverterPreSingletons = append(bazelConverterPreSingletons, singleton{name, factory})
91}
92
Colin Crosscec81712017-07-13 14:43:27 -070093type Context struct {
94 *blueprint.Context
Colin Crossae8600b2020-10-29 17:09:13 -070095 config Config
Colin Crosscec81712017-07-13 14:43:27 -070096}
Colin Cross463a90e2015-06-17 14:20:06 -070097
Colin Crossae8600b2020-10-29 17:09:13 -070098func NewContext(config Config) *Context {
99 ctx := &Context{blueprint.NewContext(), config}
Colin Cross988414c2020-01-11 01:11:46 +0000100 ctx.SetSrcDir(absSrcDir)
101 return ctx
Colin Crosscec81712017-07-13 14:43:27 -0700102}
103
Jingwen Chen4133ce62020-12-02 04:34:15 -0500104// RegisterForBazelConversion registers an alternate shadow pipeline of
105// singletons, module types and mutators to register for converting Blueprint
106// files to semantically equivalent BUILD files.
107func (ctx *Context) RegisterForBazelConversion() {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800108 for _, t := range bazelConverterPreSingletons {
109 ctx.RegisterPreSingletonType(t.name, SingletonFactoryAdaptor(ctx, t.factory))
110 }
111
Jingwen Chen4133ce62020-12-02 04:34:15 -0500112 for _, t := range moduleTypes {
113 ctx.RegisterModuleType(t.name, ModuleFactoryAdaptor(t.factory))
114 }
115
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800116 for _, t := range bazelConverterSingletons {
117 ctx.RegisterSingletonType(t.name, SingletonFactoryAdaptor(ctx, t.factory))
118 }
Jingwen Chen4133ce62020-12-02 04:34:15 -0500119
120 registerMutatorsForBazelConversion(ctx.Context)
121}
122
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
160// Interface for registering build components.
161//
162// Provided to allow registration of build components to be shared between the runtime
163// and test environments.
164type RegistrationContext interface {
165 RegisterModuleType(name string, factory ModuleFactory)
166 RegisterSingletonType(name string, factory SingletonFactory)
Paul Duffina48f7582019-12-19 11:25:19 +0000167 PreArchMutators(f RegisterMutatorFunc)
Paul Duffina80ef842020-01-14 12:09:36 +0000168
169 // Register pre arch mutators that are hard coded into mutator.go.
170 //
171 // Only registers mutators for testing, is a noop on the InitRegistrationContext.
172 HardCodedPreArchMutators(f RegisterMutatorFunc)
173
Paul Duffin2ccaffd2019-12-19 15:12:58 +0000174 PreDepsMutators(f RegisterMutatorFunc)
175 PostDepsMutators(f RegisterMutatorFunc)
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000176 FinalDepsMutators(f RegisterMutatorFunc)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000177}
178
179// Used to register build components from an init() method, e.g.
180//
181// init() {
182// RegisterBuildComponents(android.InitRegistrationContext)
183// }
184//
185// func RegisterBuildComponents(ctx android.RegistrationContext) {
186// ctx.RegisterModuleType(...)
187// ...
188// }
189//
190// Extracting the actual registration into a separate RegisterBuildComponents(ctx) function
191// allows it to be used to initialize test context, e.g.
192//
Colin Crossae8600b2020-10-29 17:09:13 -0700193// ctx := android.NewTestContext(config)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000194// RegisterBuildComponents(ctx)
Paul Duffin0a286832019-12-19 12:23:01 +0000195var InitRegistrationContext RegistrationContext = &initRegistrationContext{
196 moduleTypes: make(map[string]ModuleFactory),
197 singletonTypes: make(map[string]SingletonFactory),
198}
Paul Duffinf9b1da02019-12-18 19:51:55 +0000199
200// Make sure the TestContext implements RegistrationContext.
201var _ RegistrationContext = (*TestContext)(nil)
202
Paul Duffin0a286832019-12-19 12:23:01 +0000203type initRegistrationContext struct {
204 moduleTypes map[string]ModuleFactory
205 singletonTypes map[string]SingletonFactory
206}
Paul Duffinf9b1da02019-12-18 19:51:55 +0000207
Paul Duffin0a286832019-12-19 12:23:01 +0000208func (ctx *initRegistrationContext) RegisterModuleType(name string, factory ModuleFactory) {
209 if _, present := ctx.moduleTypes[name]; present {
210 panic(fmt.Sprintf("module type %q is already registered", name))
211 }
212 ctx.moduleTypes[name] = factory
Paul Duffinf9b1da02019-12-18 19:51:55 +0000213 RegisterModuleType(name, factory)
214}
215
Paul Duffin0a286832019-12-19 12:23:01 +0000216func (ctx *initRegistrationContext) RegisterSingletonType(name string, factory SingletonFactory) {
217 if _, present := ctx.singletonTypes[name]; present {
218 panic(fmt.Sprintf("singleton type %q is already registered", name))
219 }
220 ctx.singletonTypes[name] = factory
Paul Duffinf9b1da02019-12-18 19:51:55 +0000221 RegisterSingletonType(name, factory)
222}
Paul Duffina48f7582019-12-19 11:25:19 +0000223
Paul Duffin0a286832019-12-19 12:23:01 +0000224func (ctx *initRegistrationContext) PreArchMutators(f RegisterMutatorFunc) {
Paul Duffina48f7582019-12-19 11:25:19 +0000225 PreArchMutators(f)
226}
Paul Duffin2ccaffd2019-12-19 15:12:58 +0000227
Paul Duffina80ef842020-01-14 12:09:36 +0000228func (ctx *initRegistrationContext) HardCodedPreArchMutators(f RegisterMutatorFunc) {
229 // Nothing to do as the mutators are hard code in preArch in mutator.go
230}
231
Paul Duffin2ccaffd2019-12-19 15:12:58 +0000232func (ctx *initRegistrationContext) PreDepsMutators(f RegisterMutatorFunc) {
233 PreDepsMutators(f)
234}
235
236func (ctx *initRegistrationContext) PostDepsMutators(f RegisterMutatorFunc) {
237 PostDepsMutators(f)
238}
Martin Stjernholm710ec3a2020-01-16 15:12:04 +0000239
240func (ctx *initRegistrationContext) FinalDepsMutators(f RegisterMutatorFunc) {
241 FinalDepsMutators(f)
242}