| 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 | |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 24 | // 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. |
| 31 | type sortableComponent interface { |
| 32 | // componentName returns the name of the component. |
| 33 | // |
| 34 | // Uniquely identifies the components within the set of components used at runtimr and during |
| 35 | // tests. |
| 36 | componentName() string |
| 37 | |
| 38 | // register registers this component in the supplied context. |
| 39 | register(ctx *Context) |
| 40 | } |
| 41 | |
| 42 | type sortableComponents []sortableComponent |
| 43 | |
| 44 | // registerAll registers all components in this slice with the supplied context. |
| 45 | func (r sortableComponents) registerAll(ctx *Context) { |
| 46 | for _, c := range r { |
| 47 | c.register(ctx) |
| 48 | } |
| 49 | } |
| 50 | |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 51 | type moduleType struct { |
| 52 | name string |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 53 | factory ModuleFactory |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 56 | func (t moduleType) register(ctx *Context) { |
| 57 | ctx.RegisterModuleType(t.name, ModuleFactoryAdaptor(t.factory)) |
| 58 | } |
| 59 | |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 60 | var moduleTypes []moduleType |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 61 | var moduleTypesForDocs = map[string]reflect.Value{} |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 62 | |
| 63 | type singleton struct { |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 64 | // True if this should be registered as a pre-singleton, false otherwise. |
| 65 | pre bool |
| 66 | |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 67 | name string |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 68 | factory SingletonFactory |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 71 | func newSingleton(name string, factory SingletonFactory) singleton { |
| 72 | return singleton{false, name, factory} |
| 73 | } |
| 74 | |
| 75 | func newPreSingleton(name string, factory SingletonFactory) singleton { |
| 76 | return singleton{true, name, factory} |
| 77 | } |
| 78 | |
| 79 | func (s singleton) componentName() string { |
| 80 | return s.name |
| 81 | } |
| 82 | |
| 83 | func (s singleton) register(ctx *Context) { |
| 84 | adaptor := SingletonFactoryAdaptor(ctx, s.factory) |
| 85 | if s.pre { |
| 86 | ctx.RegisterPreSingletonType(s.name, adaptor) |
| 87 | } else { |
| 88 | ctx.RegisterSingletonType(s.name, adaptor) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | var _ sortableComponent = singleton{} |
| 93 | |
| 94 | var singletons sortableComponents |
| 95 | var preSingletons sortableComponents |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 96 | |
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 97 | type mutator struct { |
| 98 | name string |
| 99 | bottomUpMutator blueprint.BottomUpMutator |
| 100 | topDownMutator blueprint.TopDownMutator |
| Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 101 | parallel bool |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 104 | var _ sortableComponent = &mutator{} |
| 105 | |
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 106 | type ModuleFactory func() Module |
| 107 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 108 | // ModuleFactoryAdaptor wraps a ModuleFactory into a blueprint.ModuleFactory by converting a Module |
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 109 | // into a blueprint.Module and a list of property structs |
| 110 | func ModuleFactoryAdaptor(factory ModuleFactory) blueprint.ModuleFactory { |
| 111 | return func() (blueprint.Module, []interface{}) { |
| 112 | module := factory() |
| 113 | return module, module.GetProperties() |
| 114 | } |
| 115 | } |
| 116 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 117 | type SingletonFactory func() Singleton |
| 118 | |
| 119 | // SingletonFactoryAdaptor wraps a SingletonFactory into a blueprint.SingletonFactory by converting |
| 120 | // a Singleton into a blueprint.Singleton |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 121 | func SingletonFactoryAdaptor(ctx *Context, factory SingletonFactory) blueprint.SingletonFactory { |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 122 | return func() blueprint.Singleton { |
| 123 | singleton := factory() |
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 124 | if makevars, ok := singleton.(SingletonMakeVarsProvider); ok { |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 125 | registerSingletonMakeVarsProvider(ctx.config, makevars) |
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 126 | } |
| Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 127 | return &singletonAdaptor{Singleton: singleton} |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 131 | func RegisterModuleType(name string, factory ModuleFactory) { |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 132 | moduleTypes = append(moduleTypes, moduleType{name, factory}) |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 133 | RegisterModuleTypeForDocs(name, reflect.ValueOf(factory)) |
| 134 | } |
| 135 | |
| 136 | // RegisterModuleTypeForDocs associates a module type name with a reflect.Value of the factory |
| 137 | // function that has documentation for the module type. It is normally called automatically |
| 138 | // by RegisterModuleType, but can be called manually after RegisterModuleType in order to |
| 139 | // override the factory method used for documentation, for example if the method passed to |
| 140 | // RegisterModuleType was a lambda. |
| 141 | func RegisterModuleTypeForDocs(name string, factory reflect.Value) { |
| 142 | moduleTypesForDocs[name] = factory |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 145 | func RegisterSingletonType(name string, factory SingletonFactory) { |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 146 | singletons = append(singletons, newSingleton(name, factory)) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 149 | func RegisterPreSingletonType(name string, factory SingletonFactory) { |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 150 | preSingletons = append(preSingletons, newPreSingleton(name, factory)) |
| Colin Cross | 5a79c83 | 2017-11-07 13:35:38 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 153 | type Context struct { |
| 154 | *blueprint.Context |
| Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 155 | config Config |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 156 | } |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 157 | |
| Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 158 | func NewContext(config Config) *Context { |
| 159 | ctx := &Context{blueprint.NewContext(), config} |
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 160 | ctx.SetSrcDir(absSrcDir) |
| 161 | return ctx |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame^] | 164 | func (ctx *Context) SetRunningAsBp2build() { |
| 165 | ctx.config.runningAsBp2Build = true |
| 166 | } |
| 167 | |
| Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 168 | // 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. |
| 171 | func (ctx *Context) RegisterForBazelConversion() { |
| 172 | for _, t := range moduleTypes { |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 173 | t.register(ctx) |
| Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 174 | } |
| 175 | |
| Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 176 | // Required for SingletonModule types, even though we are not using them. |
| 177 | for _, t := range singletons { |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 178 | t.register(ctx) |
| Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 179 | } |
| Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 180 | |
| Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 181 | bp2buildMutatorList := []RegisterMutatorFunc{} |
| 182 | for t, f := range bp2buildMutators { |
| 183 | ctx.config.bp2buildModuleTypeConfig[t] = true |
| 184 | bp2buildMutatorList = append(bp2buildMutatorList, f) |
| 185 | } |
| 186 | |
| Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 187 | RegisterMutatorsForBazelConversion(ctx, bp2buildPreArchMutators, bp2buildMutatorList) |
| Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 188 | } |
| 189 | |
| Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 190 | // Register the pipeline of singletons, module types, and mutators for |
| 191 | // generating build.ninja and other files for Kati, from Android.bp files. |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 192 | func (ctx *Context) Register() { |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 193 | preSingletons.registerAll(ctx) |
| Colin Cross | 5a79c83 | 2017-11-07 13:35:38 -0800 | [diff] [blame] | 194 | |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 195 | for _, t := range moduleTypes { |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 196 | t.register(ctx) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| Chris Parsons | b164b38 | 2021-03-29 20:06:57 -0400 | [diff] [blame] | 199 | if ctx.config.BazelContext.BazelEnabled() { |
| 200 | // Hydrate the configuration of bp2build-enabled module types. This is |
| 201 | // required as a signal to identify which modules should be deferred to |
| 202 | // Bazel in mixed builds, if it is enabled. |
| 203 | for t, _ := range bp2buildMutators { |
| 204 | ctx.config.bp2buildModuleTypeConfig[t] = true |
| 205 | } |
| 206 | } |
| 207 | |
| Paul Duffin | c05b034 | 2021-03-06 13:28:13 +0000 | [diff] [blame] | 208 | mutators := collateGloballyRegisteredMutators() |
| 209 | mutators.registerAll(ctx) |
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 210 | |
| Paul Duffin | 42d0b93 | 2021-03-06 20:24:50 +0000 | [diff] [blame] | 211 | singletons := collateGloballyRegisteredSingletons() |
| 212 | singletons.registerAll(ctx) |
| 213 | } |
| Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 214 | |
| Paul Duffin | 42d0b93 | 2021-03-06 20:24:50 +0000 | [diff] [blame] | 215 | func collateGloballyRegisteredSingletons() sortableComponents { |
| 216 | allSingletons := append(sortableComponents(nil), singletons...) |
| 217 | allSingletons = append(allSingletons, |
| 218 | singleton{false, "bazeldeps", BazelSingleton}, |
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 219 | |
| Paul Duffin | 42d0b93 | 2021-03-06 20:24:50 +0000 | [diff] [blame] | 220 | // Register phony just before makevars so it can write out its phony rules as Make rules |
| 221 | singleton{false, "phony", phonySingletonFactory}, |
| Colin Cross | 580d2ce | 2019-02-09 22:59:32 -0800 | [diff] [blame] | 222 | |
| Paul Duffin | 42d0b93 | 2021-03-06 20:24:50 +0000 | [diff] [blame] | 223 | // Register makevars after other singletons so they can export values through makevars |
| 224 | singleton{false, "makevars", makeVarsSingletonFunc}, |
| 225 | |
| 226 | // Register env and ninjadeps last so that they can track all used environment variables and |
| 227 | // Ninja file dependencies stored in the config. |
| Paul Duffin | 42d0b93 | 2021-03-06 20:24:50 +0000 | [diff] [blame] | 228 | singleton{false, "ninjadeps", ninjaDepsSingletonFactory}, |
| 229 | ) |
| 230 | |
| 231 | return allSingletons |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 232 | } |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 233 | |
| 234 | func ModuleTypeFactories() map[string]ModuleFactory { |
| 235 | ret := make(map[string]ModuleFactory) |
| 236 | for _, t := range moduleTypes { |
| 237 | ret[t.name] = t.factory |
| 238 | } |
| 239 | return ret |
| 240 | } |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 241 | |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 242 | func ModuleTypeFactoriesForDocs() map[string]reflect.Value { |
| 243 | return moduleTypesForDocs |
| 244 | } |
| 245 | |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 246 | // Interface for registering build components. |
| 247 | // |
| 248 | // Provided to allow registration of build components to be shared between the runtime |
| 249 | // and test environments. |
| 250 | type RegistrationContext interface { |
| 251 | RegisterModuleType(name string, factory ModuleFactory) |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 252 | RegisterSingletonModuleType(name string, factory SingletonModuleFactory) |
| Paul Duffin | eafc16b | 2021-02-24 01:43:18 +0000 | [diff] [blame] | 253 | RegisterPreSingletonType(name string, factory SingletonFactory) |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 254 | RegisterSingletonType(name string, factory SingletonFactory) |
| Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 255 | PreArchMutators(f RegisterMutatorFunc) |
| Paul Duffin | a80ef84 | 2020-01-14 12:09:36 +0000 | [diff] [blame] | 256 | |
| 257 | // Register pre arch mutators that are hard coded into mutator.go. |
| 258 | // |
| 259 | // Only registers mutators for testing, is a noop on the InitRegistrationContext. |
| 260 | HardCodedPreArchMutators(f RegisterMutatorFunc) |
| 261 | |
| Paul Duffin | 2ccaffd | 2019-12-19 15:12:58 +0000 | [diff] [blame] | 262 | PreDepsMutators(f RegisterMutatorFunc) |
| 263 | PostDepsMutators(f RegisterMutatorFunc) |
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 264 | FinalDepsMutators(f RegisterMutatorFunc) |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // Used to register build components from an init() method, e.g. |
| 268 | // |
| 269 | // init() { |
| 270 | // RegisterBuildComponents(android.InitRegistrationContext) |
| 271 | // } |
| 272 | // |
| 273 | // func RegisterBuildComponents(ctx android.RegistrationContext) { |
| 274 | // ctx.RegisterModuleType(...) |
| 275 | // ... |
| 276 | // } |
| 277 | // |
| 278 | // Extracting the actual registration into a separate RegisterBuildComponents(ctx) function |
| 279 | // allows it to be used to initialize test context, e.g. |
| 280 | // |
| Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 281 | // ctx := android.NewTestContext(config) |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 282 | // RegisterBuildComponents(ctx) |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 283 | var InitRegistrationContext RegistrationContext = &initRegistrationContext{ |
| Paul Duffin | 42da69d | 2021-03-22 13:41:36 +0000 | [diff] [blame] | 284 | moduleTypes: make(map[string]ModuleFactory), |
| 285 | singletonTypes: make(map[string]SingletonFactory), |
| 286 | preSingletonTypes: make(map[string]SingletonFactory), |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 287 | } |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 288 | |
| 289 | // Make sure the TestContext implements RegistrationContext. |
| 290 | var _ RegistrationContext = (*TestContext)(nil) |
| 291 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 292 | type initRegistrationContext struct { |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 293 | moduleTypes map[string]ModuleFactory |
| 294 | singletonTypes map[string]SingletonFactory |
| Paul Duffin | eafc16b | 2021-02-24 01:43:18 +0000 | [diff] [blame] | 295 | preSingletonTypes map[string]SingletonFactory |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 296 | moduleTypesForDocs map[string]reflect.Value |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 297 | } |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 298 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 299 | func (ctx *initRegistrationContext) RegisterModuleType(name string, factory ModuleFactory) { |
| 300 | if _, present := ctx.moduleTypes[name]; present { |
| 301 | panic(fmt.Sprintf("module type %q is already registered", name)) |
| 302 | } |
| 303 | ctx.moduleTypes[name] = factory |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 304 | RegisterModuleType(name, factory) |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 305 | RegisterModuleTypeForDocs(name, reflect.ValueOf(factory)) |
| 306 | } |
| 307 | |
| 308 | func (ctx *initRegistrationContext) RegisterSingletonModuleType(name string, factory SingletonModuleFactory) { |
| 309 | s, m := SingletonModuleFactoryAdaptor(name, factory) |
| 310 | ctx.RegisterSingletonType(name, s) |
| 311 | ctx.RegisterModuleType(name, m) |
| 312 | // Overwrite moduleTypesForDocs with the original factory instead of the lambda returned by |
| 313 | // SingletonModuleFactoryAdaptor so that docs can find the module type documentation on the |
| 314 | // factory method. |
| 315 | RegisterModuleTypeForDocs(name, reflect.ValueOf(factory)) |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 318 | func (ctx *initRegistrationContext) RegisterSingletonType(name string, factory SingletonFactory) { |
| 319 | if _, present := ctx.singletonTypes[name]; present { |
| 320 | panic(fmt.Sprintf("singleton type %q is already registered", name)) |
| 321 | } |
| 322 | ctx.singletonTypes[name] = factory |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 323 | RegisterSingletonType(name, factory) |
| 324 | } |
| Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 325 | |
| Paul Duffin | eafc16b | 2021-02-24 01:43:18 +0000 | [diff] [blame] | 326 | func (ctx *initRegistrationContext) RegisterPreSingletonType(name string, factory SingletonFactory) { |
| 327 | if _, present := ctx.preSingletonTypes[name]; present { |
| 328 | panic(fmt.Sprintf("pre singleton type %q is already registered", name)) |
| 329 | } |
| 330 | ctx.preSingletonTypes[name] = factory |
| 331 | RegisterPreSingletonType(name, factory) |
| 332 | } |
| 333 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 334 | func (ctx *initRegistrationContext) PreArchMutators(f RegisterMutatorFunc) { |
| Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 335 | PreArchMutators(f) |
| 336 | } |
| Paul Duffin | 2ccaffd | 2019-12-19 15:12:58 +0000 | [diff] [blame] | 337 | |
| Paul Duffin | a80ef84 | 2020-01-14 12:09:36 +0000 | [diff] [blame] | 338 | func (ctx *initRegistrationContext) HardCodedPreArchMutators(f RegisterMutatorFunc) { |
| 339 | // Nothing to do as the mutators are hard code in preArch in mutator.go |
| 340 | } |
| 341 | |
| Paul Duffin | 2ccaffd | 2019-12-19 15:12:58 +0000 | [diff] [blame] | 342 | func (ctx *initRegistrationContext) PreDepsMutators(f RegisterMutatorFunc) { |
| 343 | PreDepsMutators(f) |
| 344 | } |
| 345 | |
| 346 | func (ctx *initRegistrationContext) PostDepsMutators(f RegisterMutatorFunc) { |
| 347 | PostDepsMutators(f) |
| 348 | } |
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 349 | |
| 350 | func (ctx *initRegistrationContext) FinalDepsMutators(f RegisterMutatorFunc) { |
| 351 | FinalDepsMutators(f) |
| 352 | } |