| 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 ( |
| 18 | "sync" |
| 19 | |
| 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 |
| 25 | factory blueprint.ModuleFactory |
| 26 | } |
| 27 | |
| 28 | var moduleTypes []moduleType |
| 29 | |
| 30 | type singleton struct { |
| 31 | name string |
| 32 | factory blueprint.SingletonFactory |
| 33 | } |
| 34 | |
| 35 | var singletons []singleton |
| 36 | |
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 37 | type mutator struct { |
| 38 | name string |
| 39 | bottomUpMutator blueprint.BottomUpMutator |
| 40 | topDownMutator blueprint.TopDownMutator |
| Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 41 | parallel bool |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 44 | var mutators []*mutator |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 45 | |
| 46 | func RegisterModuleType(name string, factory blueprint.ModuleFactory) { |
| 47 | moduleTypes = append(moduleTypes, moduleType{name, factory}) |
| 48 | } |
| 49 | |
| 50 | func RegisterSingletonType(name string, factory blueprint.SingletonFactory) { |
| 51 | singletons = append(singletons, singleton{name, factory}) |
| 52 | } |
| 53 | |
| Colin Cross | 4498afc | 2016-10-13 14:18:27 -0700 | [diff] [blame^] | 54 | var registerMutatorsOnce sync.Once |
| 55 | |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 56 | func NewContext() *blueprint.Context { |
| 57 | ctx := blueprint.NewContext() |
| 58 | |
| 59 | for _, t := range moduleTypes { |
| 60 | ctx.RegisterModuleType(t.name, t.factory) |
| 61 | } |
| 62 | |
| 63 | for _, t := range singletons { |
| 64 | ctx.RegisterSingletonType(t.name, t.factory) |
| 65 | } |
| 66 | |
| Colin Cross | 4498afc | 2016-10-13 14:18:27 -0700 | [diff] [blame^] | 67 | registerMutatorsOnce.Do(registerMutators) |
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 68 | |
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 69 | for _, t := range mutators { |
| Colin Cross | 76f2f97 | 2016-08-11 14:10:54 -0700 | [diff] [blame] | 70 | var handle blueprint.MutatorHandle |
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 71 | if t.bottomUpMutator != nil { |
| Colin Cross | 76f2f97 | 2016-08-11 14:10:54 -0700 | [diff] [blame] | 72 | handle = ctx.RegisterBottomUpMutator(t.name, t.bottomUpMutator) |
| 73 | } else if t.topDownMutator != nil { |
| 74 | handle = ctx.RegisterTopDownMutator(t.name, t.topDownMutator) |
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 75 | } |
| Colin Cross | 76f2f97 | 2016-08-11 14:10:54 -0700 | [diff] [blame] | 76 | if t.parallel { |
| 77 | handle.Parallel() |
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 78 | } |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | return ctx |
| 82 | } |