| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 1 | // 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 Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 15 | package android |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 16 | |
| Colin Cross | 4498afc | 2016-10-13 14:18:27 -0700 | [diff] [blame] | 17 | import ( |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 18 | "fmt" |
| 19 | |
| Colin Cross | 4498afc | 2016-10-13 14:18:27 -0700 | [diff] [blame] | 20 | "github.com/google/blueprint" |
| 21 | ) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 22 | |
| 23 | type moduleType struct { |
| 24 | name string |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 25 | factory ModuleFactory |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | var moduleTypes []moduleType |
| 29 | |
| 30 | type singleton struct { |
| 31 | name string |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 32 | factory SingletonFactory |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | var singletons []singleton |
| Colin Cross | 5a79c83 | 2017-11-07 13:35:38 -0800 | [diff] [blame] | 36 | var preSingletons []singleton |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 37 | |
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 38 | type mutator struct { |
| 39 | name string |
| 40 | bottomUpMutator blueprint.BottomUpMutator |
| 41 | topDownMutator blueprint.TopDownMutator |
| Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 42 | parallel bool |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 45 | type ModuleFactory func() Module |
| 46 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 47 | // ModuleFactoryAdaptor wraps a ModuleFactory into a blueprint.ModuleFactory by converting a Module |
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 48 | // into a blueprint.Module and a list of property structs |
| 49 | func ModuleFactoryAdaptor(factory ModuleFactory) blueprint.ModuleFactory { |
| 50 | return func() (blueprint.Module, []interface{}) { |
| 51 | module := factory() |
| 52 | return module, module.GetProperties() |
| 53 | } |
| 54 | } |
| 55 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 56 | type SingletonFactory func() Singleton |
| 57 | |
| 58 | // SingletonFactoryAdaptor wraps a SingletonFactory into a blueprint.SingletonFactory by converting |
| 59 | // a Singleton into a blueprint.Singleton |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 60 | func SingletonFactoryAdaptor(ctx *Context, factory SingletonFactory) blueprint.SingletonFactory { |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 61 | return func() blueprint.Singleton { |
| 62 | singleton := factory() |
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 63 | if makevars, ok := singleton.(SingletonMakeVarsProvider); ok { |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 64 | registerSingletonMakeVarsProvider(ctx.config, makevars) |
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 65 | } |
| Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 66 | return &singletonAdaptor{Singleton: singleton} |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 70 | func RegisterModuleType(name string, factory ModuleFactory) { |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 71 | moduleTypes = append(moduleTypes, moduleType{name, factory}) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 74 | func RegisterSingletonType(name string, factory SingletonFactory) { |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 75 | singletons = append(singletons, singleton{name, factory}) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 78 | func RegisterPreSingletonType(name string, factory SingletonFactory) { |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 79 | preSingletons = append(preSingletons, singleton{name, factory}) |
| Colin Cross | 5a79c83 | 2017-11-07 13:35:38 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 82 | type Context struct { |
| 83 | *blueprint.Context |
| Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 84 | config Config |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 85 | } |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 86 | |
| Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 87 | func NewContext(config Config) *Context { |
| 88 | ctx := &Context{blueprint.NewContext(), config} |
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 89 | ctx.SetSrcDir(absSrcDir) |
| 90 | return ctx |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame^] | 93 | // 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. |
| 96 | func (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 Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 108 | func (ctx *Context) Register() { |
| Colin Cross | 5a79c83 | 2017-11-07 13:35:38 -0800 | [diff] [blame] | 109 | for _, t := range preSingletons { |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 110 | ctx.RegisterPreSingletonType(t.name, SingletonFactoryAdaptor(ctx, t.factory)) |
| Colin Cross | 5a79c83 | 2017-11-07 13:35:38 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 113 | for _, t := range moduleTypes { |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 114 | ctx.RegisterModuleType(t.name, ModuleFactoryAdaptor(t.factory)) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | for _, t := range singletons { |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 118 | ctx.RegisterSingletonType(t.name, SingletonFactoryAdaptor(ctx, t.factory)) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 121 | registerMutators(ctx.Context, preArch, preDeps, postDeps, finalDeps) |
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 122 | |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 123 | ctx.RegisterSingletonType("bazeldeps", SingletonFactoryAdaptor(ctx, BazelSingleton)) |
| Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 124 | |
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 125 | // Register phony just before makevars so it can write out its phony rules as Make rules |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 126 | ctx.RegisterSingletonType("phony", SingletonFactoryAdaptor(ctx, phonySingletonFactory)) |
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 127 | |
| Colin Cross | 580d2ce | 2019-02-09 22:59:32 -0800 | [diff] [blame] | 128 | // Register makevars after other singletons so they can export values through makevars |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 129 | ctx.RegisterSingletonType("makevars", SingletonFactoryAdaptor(ctx, makeVarsSingletonFunc)) |
| Colin Cross | 580d2ce | 2019-02-09 22:59:32 -0800 | [diff] [blame] | 130 | |
| Colin Cross | 1212929 | 2020-10-29 18:23:58 -0700 | [diff] [blame] | 131 | // Register env and ninjadeps last so that they can track all used environment variables and |
| 132 | // Ninja file dependencies stored in the config. |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 133 | ctx.RegisterSingletonType("env", SingletonFactoryAdaptor(ctx, EnvSingleton)) |
| Colin Cross | 1212929 | 2020-10-29 18:23:58 -0700 | [diff] [blame] | 134 | ctx.RegisterSingletonType("ninjadeps", SingletonFactoryAdaptor(ctx, ninjaDepsSingletonFactory)) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 135 | } |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 136 | |
| 137 | func 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 Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 144 | |
| 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. |
| 149 | type RegistrationContext interface { |
| 150 | RegisterModuleType(name string, factory ModuleFactory) |
| 151 | RegisterSingletonType(name string, factory SingletonFactory) |
| Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 152 | PreArchMutators(f RegisterMutatorFunc) |
| Paul Duffin | a80ef84 | 2020-01-14 12:09:36 +0000 | [diff] [blame] | 153 | |
| 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 Duffin | 2ccaffd | 2019-12-19 15:12:58 +0000 | [diff] [blame] | 159 | PreDepsMutators(f RegisterMutatorFunc) |
| 160 | PostDepsMutators(f RegisterMutatorFunc) |
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 161 | FinalDepsMutators(f RegisterMutatorFunc) |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 162 | } |
| 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 Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 178 | // ctx := android.NewTestContext(config) |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 179 | // RegisterBuildComponents(ctx) |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 180 | var InitRegistrationContext RegistrationContext = &initRegistrationContext{ |
| 181 | moduleTypes: make(map[string]ModuleFactory), |
| 182 | singletonTypes: make(map[string]SingletonFactory), |
| 183 | } |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 184 | |
| 185 | // Make sure the TestContext implements RegistrationContext. |
| 186 | var _ RegistrationContext = (*TestContext)(nil) |
| 187 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 188 | type initRegistrationContext struct { |
| 189 | moduleTypes map[string]ModuleFactory |
| 190 | singletonTypes map[string]SingletonFactory |
| 191 | } |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 192 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 193 | func (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 Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 198 | RegisterModuleType(name, factory) |
| 199 | } |
| 200 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 201 | func (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 Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 206 | RegisterSingletonType(name, factory) |
| 207 | } |
| Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 208 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 209 | func (ctx *initRegistrationContext) PreArchMutators(f RegisterMutatorFunc) { |
| Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 210 | PreArchMutators(f) |
| 211 | } |
| Paul Duffin | 2ccaffd | 2019-12-19 15:12:58 +0000 | [diff] [blame] | 212 | |
| Paul Duffin | a80ef84 | 2020-01-14 12:09:36 +0000 | [diff] [blame] | 213 | func (ctx *initRegistrationContext) HardCodedPreArchMutators(f RegisterMutatorFunc) { |
| 214 | // Nothing to do as the mutators are hard code in preArch in mutator.go |
| 215 | } |
| 216 | |
| Paul Duffin | 2ccaffd | 2019-12-19 15:12:58 +0000 | [diff] [blame] | 217 | func (ctx *initRegistrationContext) PreDepsMutators(f RegisterMutatorFunc) { |
| 218 | PreDepsMutators(f) |
| 219 | } |
| 220 | |
| 221 | func (ctx *initRegistrationContext) PostDepsMutators(f RegisterMutatorFunc) { |
| 222 | PostDepsMutators(f) |
| 223 | } |
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 224 | |
| 225 | func (ctx *initRegistrationContext) FinalDepsMutators(f RegisterMutatorFunc) { |
| 226 | FinalDepsMutators(f) |
| 227 | } |