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