| 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 |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 23 | factory ModuleFactory |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 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 | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 43 | type ModuleFactory func() Module |
| 44 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 45 | // ModuleFactoryAdaptor wraps a ModuleFactory into a blueprint.ModuleFactory by converting a Module |
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 46 | // into a blueprint.Module and a list of property structs |
| 47 | func ModuleFactoryAdaptor(factory ModuleFactory) blueprint.ModuleFactory { |
| 48 | return func() (blueprint.Module, []interface{}) { |
| 49 | module := factory() |
| 50 | return module, module.GetProperties() |
| 51 | } |
| 52 | } |
| 53 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 54 | type SingletonFactory func() Singleton |
| 55 | |
| 56 | // SingletonFactoryAdaptor wraps a SingletonFactory into a blueprint.SingletonFactory by converting |
| 57 | // a Singleton into a blueprint.Singleton |
| 58 | func SingletonFactoryAdaptor(factory SingletonFactory) blueprint.SingletonFactory { |
| 59 | return func() blueprint.Singleton { |
| 60 | singleton := factory() |
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame^] | 61 | if makevars, ok := singleton.(SingletonMakeVarsProvider); ok { |
| 62 | registerSingletonMakeVarsProvider(makevars) |
| 63 | } |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 64 | return singletonAdaptor{singleton} |
| 65 | } |
| 66 | } |
| 67 | |
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 68 | func RegisterModuleType(name string, factory ModuleFactory) { |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 69 | moduleTypes = append(moduleTypes, moduleType{name, factory}) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 72 | func RegisterSingletonType(name string, factory SingletonFactory) { |
| 73 | singletons = append(singletons, singleton{name, SingletonFactoryAdaptor(factory)}) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 76 | func RegisterPreSingletonType(name string, factory SingletonFactory) { |
| 77 | preSingletons = append(preSingletons, singleton{name, SingletonFactoryAdaptor(factory)}) |
| Colin Cross | 5a79c83 | 2017-11-07 13:35:38 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 80 | type Context struct { |
| 81 | *blueprint.Context |
| 82 | } |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 83 | |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 84 | func NewContext() *Context { |
| 85 | return &Context{blueprint.NewContext()} |
| 86 | } |
| 87 | |
| 88 | func (ctx *Context) Register() { |
| Colin Cross | 5a79c83 | 2017-11-07 13:35:38 -0800 | [diff] [blame] | 89 | for _, t := range preSingletons { |
| 90 | ctx.RegisterPreSingletonType(t.name, t.factory) |
| 91 | } |
| 92 | |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 93 | for _, t := range moduleTypes { |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 94 | ctx.RegisterModuleType(t.name, ModuleFactoryAdaptor(t.factory)) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | for _, t := range singletons { |
| 98 | ctx.RegisterSingletonType(t.name, t.factory) |
| 99 | } |
| 100 | |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 101 | registerMutators(ctx.Context, preArch, preDeps, postDeps) |
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 102 | |
| Colin Cross | 580d2ce | 2019-02-09 22:59:32 -0800 | [diff] [blame] | 103 | // Register makevars after other singletons so they can export values through makevars |
| 104 | ctx.RegisterSingletonType("makevars", SingletonFactoryAdaptor(makeVarsSingletonFunc)) |
| 105 | |
| 106 | // Register env last so that it can track all used environment variables |
| Colin Cross | 54855dd | 2017-11-28 23:55:23 -0800 | [diff] [blame] | 107 | ctx.RegisterSingletonType("env", SingletonFactoryAdaptor(EnvSingleton)) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 108 | } |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 109 | |
| 110 | func ModuleTypeFactories() map[string]ModuleFactory { |
| 111 | ret := make(map[string]ModuleFactory) |
| 112 | for _, t := range moduleTypes { |
| 113 | ret[t.name] = t.factory |
| 114 | } |
| 115 | return ret |
| 116 | } |