| 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" |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame^] | 19 | "reflect" |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 20 | |
| Colin Cross | 4498afc | 2016-10-13 14:18:27 -0700 | [diff] [blame] | 21 | "github.com/google/blueprint" |
| 22 | ) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 23 | |
| 24 | type moduleType struct { |
| 25 | name string |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 26 | factory ModuleFactory |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | var moduleTypes []moduleType |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame^] | 30 | var moduleTypesForDocs = map[string]reflect.Value{} |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 31 | |
| 32 | type singleton struct { |
| 33 | name string |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 34 | factory SingletonFactory |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | var singletons []singleton |
| Colin Cross | 5a79c83 | 2017-11-07 13:35:38 -0800 | [diff] [blame] | 38 | var preSingletons []singleton |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 39 | |
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 40 | type mutator struct { |
| 41 | name string |
| 42 | bottomUpMutator blueprint.BottomUpMutator |
| 43 | topDownMutator blueprint.TopDownMutator |
| Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 44 | parallel bool |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 47 | type ModuleFactory func() Module |
| 48 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 49 | // ModuleFactoryAdaptor wraps a ModuleFactory into a blueprint.ModuleFactory by converting a Module |
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 50 | // into a blueprint.Module and a list of property structs |
| 51 | func ModuleFactoryAdaptor(factory ModuleFactory) blueprint.ModuleFactory { |
| 52 | return func() (blueprint.Module, []interface{}) { |
| 53 | module := factory() |
| 54 | return module, module.GetProperties() |
| 55 | } |
| 56 | } |
| 57 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 58 | type SingletonFactory func() Singleton |
| 59 | |
| 60 | // SingletonFactoryAdaptor wraps a SingletonFactory into a blueprint.SingletonFactory by converting |
| 61 | // a Singleton into a blueprint.Singleton |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 62 | func SingletonFactoryAdaptor(ctx *Context, factory SingletonFactory) blueprint.SingletonFactory { |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 63 | return func() blueprint.Singleton { |
| 64 | singleton := factory() |
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 65 | if makevars, ok := singleton.(SingletonMakeVarsProvider); ok { |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 66 | registerSingletonMakeVarsProvider(ctx.config, makevars) |
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 67 | } |
| Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 68 | return &singletonAdaptor{Singleton: singleton} |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 72 | func RegisterModuleType(name string, factory ModuleFactory) { |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 73 | moduleTypes = append(moduleTypes, moduleType{name, factory}) |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame^] | 74 | 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. |
| 82 | func RegisterModuleTypeForDocs(name string, factory reflect.Value) { |
| 83 | moduleTypesForDocs[name] = factory |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 86 | func RegisterSingletonType(name string, factory SingletonFactory) { |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 87 | singletons = append(singletons, singleton{name, factory}) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 90 | func RegisterPreSingletonType(name string, factory SingletonFactory) { |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 91 | preSingletons = append(preSingletons, singleton{name, factory}) |
| Colin Cross | 5a79c83 | 2017-11-07 13:35:38 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 94 | type Context struct { |
| 95 | *blueprint.Context |
| Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 96 | config Config |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 97 | } |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 98 | |
| Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 99 | func NewContext(config Config) *Context { |
| 100 | ctx := &Context{blueprint.NewContext(), config} |
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 101 | ctx.SetSrcDir(absSrcDir) |
| 102 | return ctx |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 105 | // 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. |
| 108 | func (ctx *Context) RegisterForBazelConversion() { |
| 109 | for _, t := range moduleTypes { |
| 110 | ctx.RegisterModuleType(t.name, ModuleFactoryAdaptor(t.factory)) |
| 111 | } |
| 112 | |
| 113 | bazelConverterSingleton := singleton{"bp2build", BazelConverterSingleton} |
| 114 | ctx.RegisterSingletonType(bazelConverterSingleton.name, |
| 115 | SingletonFactoryAdaptor(ctx, bazelConverterSingleton.factory)) |
| 116 | |
| 117 | registerMutatorsForBazelConversion(ctx.Context) |
| 118 | } |
| 119 | |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 120 | func (ctx *Context) Register() { |
| Colin Cross | 5a79c83 | 2017-11-07 13:35:38 -0800 | [diff] [blame] | 121 | for _, t := range preSingletons { |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 122 | ctx.RegisterPreSingletonType(t.name, SingletonFactoryAdaptor(ctx, t.factory)) |
| Colin Cross | 5a79c83 | 2017-11-07 13:35:38 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 125 | for _, t := range moduleTypes { |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 126 | ctx.RegisterModuleType(t.name, ModuleFactoryAdaptor(t.factory)) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | for _, t := range singletons { |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 130 | ctx.RegisterSingletonType(t.name, SingletonFactoryAdaptor(ctx, t.factory)) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 133 | registerMutators(ctx.Context, preArch, preDeps, postDeps, finalDeps) |
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 134 | |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 135 | ctx.RegisterSingletonType("bazeldeps", SingletonFactoryAdaptor(ctx, BazelSingleton)) |
| Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 136 | |
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 137 | // 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] | 138 | ctx.RegisterSingletonType("phony", SingletonFactoryAdaptor(ctx, phonySingletonFactory)) |
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 139 | |
| Colin Cross | 580d2ce | 2019-02-09 22:59:32 -0800 | [diff] [blame] | 140 | // Register makevars after other singletons so they can export values through makevars |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 141 | ctx.RegisterSingletonType("makevars", SingletonFactoryAdaptor(ctx, makeVarsSingletonFunc)) |
| Colin Cross | 580d2ce | 2019-02-09 22:59:32 -0800 | [diff] [blame] | 142 | |
| Colin Cross | 1212929 | 2020-10-29 18:23:58 -0700 | [diff] [blame] | 143 | // Register env and ninjadeps last so that they can track all used environment variables and |
| 144 | // Ninja file dependencies stored in the config. |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 145 | ctx.RegisterSingletonType("env", SingletonFactoryAdaptor(ctx, EnvSingleton)) |
| Colin Cross | 1212929 | 2020-10-29 18:23:58 -0700 | [diff] [blame] | 146 | ctx.RegisterSingletonType("ninjadeps", SingletonFactoryAdaptor(ctx, ninjaDepsSingletonFactory)) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 147 | } |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 148 | |
| 149 | func ModuleTypeFactories() map[string]ModuleFactory { |
| 150 | ret := make(map[string]ModuleFactory) |
| 151 | for _, t := range moduleTypes { |
| 152 | ret[t.name] = t.factory |
| 153 | } |
| 154 | return ret |
| 155 | } |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 156 | |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame^] | 157 | func ModuleTypeFactoriesForDocs() map[string]reflect.Value { |
| 158 | return moduleTypesForDocs |
| 159 | } |
| 160 | |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 161 | // Interface for registering build components. |
| 162 | // |
| 163 | // Provided to allow registration of build components to be shared between the runtime |
| 164 | // and test environments. |
| 165 | type RegistrationContext interface { |
| 166 | RegisterModuleType(name string, factory ModuleFactory) |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame^] | 167 | RegisterSingletonModuleType(name string, factory SingletonModuleFactory) |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 168 | RegisterSingletonType(name string, factory SingletonFactory) |
| Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 169 | PreArchMutators(f RegisterMutatorFunc) |
| Paul Duffin | a80ef84 | 2020-01-14 12:09:36 +0000 | [diff] [blame] | 170 | |
| 171 | // Register pre arch mutators that are hard coded into mutator.go. |
| 172 | // |
| 173 | // Only registers mutators for testing, is a noop on the InitRegistrationContext. |
| 174 | HardCodedPreArchMutators(f RegisterMutatorFunc) |
| 175 | |
| Paul Duffin | 2ccaffd | 2019-12-19 15:12:58 +0000 | [diff] [blame] | 176 | PreDepsMutators(f RegisterMutatorFunc) |
| 177 | PostDepsMutators(f RegisterMutatorFunc) |
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 178 | FinalDepsMutators(f RegisterMutatorFunc) |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // Used to register build components from an init() method, e.g. |
| 182 | // |
| 183 | // init() { |
| 184 | // RegisterBuildComponents(android.InitRegistrationContext) |
| 185 | // } |
| 186 | // |
| 187 | // func RegisterBuildComponents(ctx android.RegistrationContext) { |
| 188 | // ctx.RegisterModuleType(...) |
| 189 | // ... |
| 190 | // } |
| 191 | // |
| 192 | // Extracting the actual registration into a separate RegisterBuildComponents(ctx) function |
| 193 | // allows it to be used to initialize test context, e.g. |
| 194 | // |
| Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 195 | // ctx := android.NewTestContext(config) |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 196 | // RegisterBuildComponents(ctx) |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 197 | var InitRegistrationContext RegistrationContext = &initRegistrationContext{ |
| 198 | moduleTypes: make(map[string]ModuleFactory), |
| 199 | singletonTypes: make(map[string]SingletonFactory), |
| 200 | } |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 201 | |
| 202 | // Make sure the TestContext implements RegistrationContext. |
| 203 | var _ RegistrationContext = (*TestContext)(nil) |
| 204 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 205 | type initRegistrationContext struct { |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame^] | 206 | moduleTypes map[string]ModuleFactory |
| 207 | singletonTypes map[string]SingletonFactory |
| 208 | moduleTypesForDocs map[string]reflect.Value |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 209 | } |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 210 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 211 | func (ctx *initRegistrationContext) RegisterModuleType(name string, factory ModuleFactory) { |
| 212 | if _, present := ctx.moduleTypes[name]; present { |
| 213 | panic(fmt.Sprintf("module type %q is already registered", name)) |
| 214 | } |
| 215 | ctx.moduleTypes[name] = factory |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 216 | RegisterModuleType(name, factory) |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame^] | 217 | RegisterModuleTypeForDocs(name, reflect.ValueOf(factory)) |
| 218 | } |
| 219 | |
| 220 | func (ctx *initRegistrationContext) RegisterSingletonModuleType(name string, factory SingletonModuleFactory) { |
| 221 | s, m := SingletonModuleFactoryAdaptor(name, factory) |
| 222 | ctx.RegisterSingletonType(name, s) |
| 223 | ctx.RegisterModuleType(name, m) |
| 224 | // Overwrite moduleTypesForDocs with the original factory instead of the lambda returned by |
| 225 | // SingletonModuleFactoryAdaptor so that docs can find the module type documentation on the |
| 226 | // factory method. |
| 227 | RegisterModuleTypeForDocs(name, reflect.ValueOf(factory)) |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 230 | func (ctx *initRegistrationContext) RegisterSingletonType(name string, factory SingletonFactory) { |
| 231 | if _, present := ctx.singletonTypes[name]; present { |
| 232 | panic(fmt.Sprintf("singleton type %q is already registered", name)) |
| 233 | } |
| 234 | ctx.singletonTypes[name] = factory |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 235 | RegisterSingletonType(name, factory) |
| 236 | } |
| Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 237 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 238 | func (ctx *initRegistrationContext) PreArchMutators(f RegisterMutatorFunc) { |
| Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 239 | PreArchMutators(f) |
| 240 | } |
| Paul Duffin | 2ccaffd | 2019-12-19 15:12:58 +0000 | [diff] [blame] | 241 | |
| Paul Duffin | a80ef84 | 2020-01-14 12:09:36 +0000 | [diff] [blame] | 242 | func (ctx *initRegistrationContext) HardCodedPreArchMutators(f RegisterMutatorFunc) { |
| 243 | // Nothing to do as the mutators are hard code in preArch in mutator.go |
| 244 | } |
| 245 | |
| Paul Duffin | 2ccaffd | 2019-12-19 15:12:58 +0000 | [diff] [blame] | 246 | func (ctx *initRegistrationContext) PreDepsMutators(f RegisterMutatorFunc) { |
| 247 | PreDepsMutators(f) |
| 248 | } |
| 249 | |
| 250 | func (ctx *initRegistrationContext) PostDepsMutators(f RegisterMutatorFunc) { |
| 251 | PostDepsMutators(f) |
| 252 | } |
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 253 | |
| 254 | func (ctx *initRegistrationContext) FinalDepsMutators(f RegisterMutatorFunc) { |
| 255 | FinalDepsMutators(f) |
| 256 | } |