| 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 | |
| 93 | func (ctx *Context) Register() { |
| Colin Cross | 5a79c83 | 2017-11-07 13:35:38 -0800 | [diff] [blame] | 94 | for _, t := range preSingletons { |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame^] | 95 | ctx.RegisterPreSingletonType(t.name, SingletonFactoryAdaptor(ctx, t.factory)) |
| Colin Cross | 5a79c83 | 2017-11-07 13:35:38 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 98 | for _, t := range moduleTypes { |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 99 | ctx.RegisterModuleType(t.name, ModuleFactoryAdaptor(t.factory)) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | for _, t := range singletons { |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame^] | 103 | ctx.RegisterSingletonType(t.name, SingletonFactoryAdaptor(ctx, t.factory)) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 106 | registerMutators(ctx.Context, preArch, preDeps, postDeps, finalDeps) |
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 107 | |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame^] | 108 | ctx.RegisterSingletonType("bazeldeps", SingletonFactoryAdaptor(ctx, BazelSingleton)) |
| Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 109 | |
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 110 | // 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^] | 111 | ctx.RegisterSingletonType("phony", SingletonFactoryAdaptor(ctx, phonySingletonFactory)) |
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 112 | |
| Colin Cross | 580d2ce | 2019-02-09 22:59:32 -0800 | [diff] [blame] | 113 | // Register makevars after other singletons so they can export values through makevars |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame^] | 114 | ctx.RegisterSingletonType("makevars", SingletonFactoryAdaptor(ctx, makeVarsSingletonFunc)) |
| Colin Cross | 580d2ce | 2019-02-09 22:59:32 -0800 | [diff] [blame] | 115 | |
| 116 | // Register env last so that it can track all used environment variables |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame^] | 117 | ctx.RegisterSingletonType("env", SingletonFactoryAdaptor(ctx, EnvSingleton)) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 118 | } |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 119 | |
| 120 | func ModuleTypeFactories() map[string]ModuleFactory { |
| 121 | ret := make(map[string]ModuleFactory) |
| 122 | for _, t := range moduleTypes { |
| 123 | ret[t.name] = t.factory |
| 124 | } |
| 125 | return ret |
| 126 | } |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 127 | |
| 128 | // Interface for registering build components. |
| 129 | // |
| 130 | // Provided to allow registration of build components to be shared between the runtime |
| 131 | // and test environments. |
| 132 | type RegistrationContext interface { |
| 133 | RegisterModuleType(name string, factory ModuleFactory) |
| 134 | RegisterSingletonType(name string, factory SingletonFactory) |
| Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 135 | PreArchMutators(f RegisterMutatorFunc) |
| Paul Duffin | a80ef84 | 2020-01-14 12:09:36 +0000 | [diff] [blame] | 136 | |
| 137 | // Register pre arch mutators that are hard coded into mutator.go. |
| 138 | // |
| 139 | // Only registers mutators for testing, is a noop on the InitRegistrationContext. |
| 140 | HardCodedPreArchMutators(f RegisterMutatorFunc) |
| 141 | |
| Paul Duffin | 2ccaffd | 2019-12-19 15:12:58 +0000 | [diff] [blame] | 142 | PreDepsMutators(f RegisterMutatorFunc) |
| 143 | PostDepsMutators(f RegisterMutatorFunc) |
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 144 | FinalDepsMutators(f RegisterMutatorFunc) |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Used to register build components from an init() method, e.g. |
| 148 | // |
| 149 | // init() { |
| 150 | // RegisterBuildComponents(android.InitRegistrationContext) |
| 151 | // } |
| 152 | // |
| 153 | // func RegisterBuildComponents(ctx android.RegistrationContext) { |
| 154 | // ctx.RegisterModuleType(...) |
| 155 | // ... |
| 156 | // } |
| 157 | // |
| 158 | // Extracting the actual registration into a separate RegisterBuildComponents(ctx) function |
| 159 | // allows it to be used to initialize test context, e.g. |
| 160 | // |
| Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 161 | // ctx := android.NewTestContext(config) |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 162 | // RegisterBuildComponents(ctx) |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 163 | var InitRegistrationContext RegistrationContext = &initRegistrationContext{ |
| 164 | moduleTypes: make(map[string]ModuleFactory), |
| 165 | singletonTypes: make(map[string]SingletonFactory), |
| 166 | } |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 167 | |
| 168 | // Make sure the TestContext implements RegistrationContext. |
| 169 | var _ RegistrationContext = (*TestContext)(nil) |
| 170 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 171 | type initRegistrationContext struct { |
| 172 | moduleTypes map[string]ModuleFactory |
| 173 | singletonTypes map[string]SingletonFactory |
| 174 | } |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 175 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 176 | func (ctx *initRegistrationContext) RegisterModuleType(name string, factory ModuleFactory) { |
| 177 | if _, present := ctx.moduleTypes[name]; present { |
| 178 | panic(fmt.Sprintf("module type %q is already registered", name)) |
| 179 | } |
| 180 | ctx.moduleTypes[name] = factory |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 181 | RegisterModuleType(name, factory) |
| 182 | } |
| 183 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 184 | func (ctx *initRegistrationContext) RegisterSingletonType(name string, factory SingletonFactory) { |
| 185 | if _, present := ctx.singletonTypes[name]; present { |
| 186 | panic(fmt.Sprintf("singleton type %q is already registered", name)) |
| 187 | } |
| 188 | ctx.singletonTypes[name] = factory |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 189 | RegisterSingletonType(name, factory) |
| 190 | } |
| Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 191 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 192 | func (ctx *initRegistrationContext) PreArchMutators(f RegisterMutatorFunc) { |
| Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 193 | PreArchMutators(f) |
| 194 | } |
| Paul Duffin | 2ccaffd | 2019-12-19 15:12:58 +0000 | [diff] [blame] | 195 | |
| Paul Duffin | a80ef84 | 2020-01-14 12:09:36 +0000 | [diff] [blame] | 196 | func (ctx *initRegistrationContext) HardCodedPreArchMutators(f RegisterMutatorFunc) { |
| 197 | // Nothing to do as the mutators are hard code in preArch in mutator.go |
| 198 | } |
| 199 | |
| Paul Duffin | 2ccaffd | 2019-12-19 15:12:58 +0000 | [diff] [blame] | 200 | func (ctx *initRegistrationContext) PreDepsMutators(f RegisterMutatorFunc) { |
| 201 | PreDepsMutators(f) |
| 202 | } |
| 203 | |
| 204 | func (ctx *initRegistrationContext) PostDepsMutators(f RegisterMutatorFunc) { |
| 205 | PostDepsMutators(f) |
| 206 | } |
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 207 | |
| 208 | func (ctx *initRegistrationContext) FinalDepsMutators(f RegisterMutatorFunc) { |
| 209 | FinalDepsMutators(f) |
| 210 | } |