| 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 ( |
| Colin Cross | 4498afc | 2016-10-13 14:18:27 -0700 | [diff] [blame] | 18 | "github.com/google/blueprint" |
| 19 | ) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 20 | |
| 21 | type moduleType struct { |
| 22 | name string |
| 23 | factory blueprint.ModuleFactory |
| 24 | } |
| 25 | |
| 26 | var moduleTypes []moduleType |
| 27 | |
| 28 | type singleton struct { |
| 29 | name string |
| 30 | factory blueprint.SingletonFactory |
| 31 | } |
| 32 | |
| 33 | var singletons []singleton |
| Colin Cross | 5a79c83 | 2017-11-07 13:35:38 -0800 | [diff] [blame^] | 34 | var preSingletons []singleton |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 35 | |
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 36 | type mutator struct { |
| 37 | name string |
| 38 | bottomUpMutator blueprint.BottomUpMutator |
| 39 | topDownMutator blueprint.TopDownMutator |
| Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 40 | parallel bool |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 43 | var mutators []*mutator |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 44 | |
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 45 | type ModuleFactory func() Module |
| 46 | |
| Steven Moreland | b1448e4 | 2017-10-27 14:46:35 -0700 | [diff] [blame] | 47 | // ModuleFactoryAdaptor Wraps a ModuleFactory into a blueprint.ModuleFactory by converting an 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 | |
| 56 | func RegisterModuleType(name string, factory ModuleFactory) { |
| 57 | moduleTypes = append(moduleTypes, moduleType{name, ModuleFactoryAdaptor(factory)}) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | func RegisterSingletonType(name string, factory blueprint.SingletonFactory) { |
| 61 | singletons = append(singletons, singleton{name, factory}) |
| 62 | } |
| 63 | |
| Colin Cross | 5a79c83 | 2017-11-07 13:35:38 -0800 | [diff] [blame^] | 64 | func RegisterPreSingletonType(name string, factory blueprint.SingletonFactory) { |
| 65 | preSingletons = append(preSingletons, singleton{name, factory}) |
| 66 | } |
| 67 | |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 68 | type Context struct { |
| 69 | *blueprint.Context |
| 70 | } |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 71 | |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 72 | func NewContext() *Context { |
| 73 | return &Context{blueprint.NewContext()} |
| 74 | } |
| 75 | |
| 76 | func (ctx *Context) Register() { |
| Colin Cross | 5a79c83 | 2017-11-07 13:35:38 -0800 | [diff] [blame^] | 77 | for _, t := range preSingletons { |
| 78 | ctx.RegisterPreSingletonType(t.name, t.factory) |
| 79 | } |
| 80 | |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 81 | for _, t := range moduleTypes { |
| 82 | ctx.RegisterModuleType(t.name, t.factory) |
| 83 | } |
| 84 | |
| 85 | for _, t := range singletons { |
| 86 | ctx.RegisterSingletonType(t.name, t.factory) |
| 87 | } |
| 88 | |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 89 | registerMutators(ctx.Context, preArch, preDeps, postDeps) |
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 90 | |
| Fabien Sanglard | d61f1f4 | 2017-01-10 16:21:22 -0800 | [diff] [blame] | 91 | ctx.RegisterSingletonType("env", EnvSingleton) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 92 | } |