| 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 | |
| 15 | package soong |
| 16 | |
| 17 | import "github.com/google/blueprint" |
| 18 | |
| 19 | type moduleType struct { |
| 20 | name string |
| 21 | factory blueprint.ModuleFactory |
| 22 | } |
| 23 | |
| 24 | var moduleTypes []moduleType |
| 25 | |
| 26 | type singleton struct { |
| 27 | name string |
| 28 | factory blueprint.SingletonFactory |
| 29 | } |
| 30 | |
| 31 | var singletons []singleton |
| 32 | |
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 33 | type mutator struct { |
| 34 | name string |
| 35 | bottomUpMutator blueprint.BottomUpMutator |
| 36 | topDownMutator blueprint.TopDownMutator |
| Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 37 | parallel bool |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 40 | var mutators []*mutator |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 41 | |
| 42 | func RegisterModuleType(name string, factory blueprint.ModuleFactory) { |
| 43 | moduleTypes = append(moduleTypes, moduleType{name, factory}) |
| 44 | } |
| 45 | |
| 46 | func RegisterSingletonType(name string, factory blueprint.SingletonFactory) { |
| 47 | singletons = append(singletons, singleton{name, factory}) |
| 48 | } |
| 49 | |
| Colin Cross | 76f2f97 | 2016-08-11 14:10:54 -0700 | [diff] [blame^] | 50 | func RegisterBottomUpMutator(name string, m blueprint.BottomUpMutator) MutatorHandle { |
| Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 51 | mutator := &mutator{name: name, bottomUpMutator: m} |
| 52 | mutators = append(mutators, mutator) |
| 53 | return mutator |
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| Colin Cross | 76f2f97 | 2016-08-11 14:10:54 -0700 | [diff] [blame^] | 56 | func RegisterTopDownMutator(name string, m blueprint.TopDownMutator) MutatorHandle { |
| 57 | mutator := &mutator{name: name, topDownMutator: m} |
| 58 | mutators = append(mutators, mutator) |
| 59 | return mutator |
| Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| Colin Cross | 76f2f97 | 2016-08-11 14:10:54 -0700 | [diff] [blame^] | 62 | type MutatorHandle interface { |
| 63 | Parallel() MutatorHandle |
| Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| Colin Cross | 76f2f97 | 2016-08-11 14:10:54 -0700 | [diff] [blame^] | 66 | func (mutator *mutator) Parallel() MutatorHandle { |
| Colin Cross | e8a67a7 | 2016-08-07 21:17:54 -0700 | [diff] [blame] | 67 | mutator.parallel = true |
| 68 | return mutator |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | func NewContext() *blueprint.Context { |
| 72 | ctx := blueprint.NewContext() |
| 73 | |
| 74 | for _, t := range moduleTypes { |
| 75 | ctx.RegisterModuleType(t.name, t.factory) |
| 76 | } |
| 77 | |
| 78 | for _, t := range singletons { |
| 79 | ctx.RegisterSingletonType(t.name, t.factory) |
| 80 | } |
| 81 | |
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 82 | for _, t := range mutators { |
| Colin Cross | 76f2f97 | 2016-08-11 14:10:54 -0700 | [diff] [blame^] | 83 | var handle blueprint.MutatorHandle |
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 84 | if t.bottomUpMutator != nil { |
| Colin Cross | 76f2f97 | 2016-08-11 14:10:54 -0700 | [diff] [blame^] | 85 | handle = ctx.RegisterBottomUpMutator(t.name, t.bottomUpMutator) |
| 86 | } else if t.topDownMutator != nil { |
| 87 | handle = ctx.RegisterTopDownMutator(t.name, t.topDownMutator) |
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 88 | } |
| Colin Cross | 76f2f97 | 2016-08-11 14:10:54 -0700 | [diff] [blame^] | 89 | if t.parallel { |
| 90 | handle.Parallel() |
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 91 | } |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | return ctx |
| 95 | } |