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