blob: 2a1bd91ce45058e7b130ac9523ca21296408ffe7 [file] [log] [blame]
Colin Cross463a90e2015-06-17 14:20:06 -07001// 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
15package soong
16
17import "github.com/google/blueprint"
18
19type moduleType struct {
20 name string
21 factory blueprint.ModuleFactory
22}
23
24var moduleTypes []moduleType
25
26type singleton struct {
27 name string
28 factory blueprint.SingletonFactory
29}
30
31var singletons []singleton
32
Colin Cross6362e272015-10-29 15:25:03 -070033type mutator struct {
34 name string
35 bottomUpMutator blueprint.BottomUpMutator
36 topDownMutator blueprint.TopDownMutator
Colin Crosse8a67a72016-08-07 21:17:54 -070037 parallel bool
Colin Cross463a90e2015-06-17 14:20:06 -070038}
39
Colin Crosse8a67a72016-08-07 21:17:54 -070040var mutators []*mutator
Colin Cross463a90e2015-06-17 14:20:06 -070041
42func RegisterModuleType(name string, factory blueprint.ModuleFactory) {
43 moduleTypes = append(moduleTypes, moduleType{name, factory})
44}
45
46func RegisterSingletonType(name string, factory blueprint.SingletonFactory) {
47 singletons = append(singletons, singleton{name, factory})
48}
49
Colin Cross76f2f972016-08-11 14:10:54 -070050func RegisterBottomUpMutator(name string, m blueprint.BottomUpMutator) MutatorHandle {
Colin Crosse8a67a72016-08-07 21:17:54 -070051 mutator := &mutator{name: name, bottomUpMutator: m}
52 mutators = append(mutators, mutator)
53 return mutator
Colin Cross6362e272015-10-29 15:25:03 -070054}
55
Colin Cross76f2f972016-08-11 14:10:54 -070056func RegisterTopDownMutator(name string, m blueprint.TopDownMutator) MutatorHandle {
57 mutator := &mutator{name: name, topDownMutator: m}
58 mutators = append(mutators, mutator)
59 return mutator
Colin Crosse8a67a72016-08-07 21:17:54 -070060}
61
Colin Cross76f2f972016-08-11 14:10:54 -070062type MutatorHandle interface {
63 Parallel() MutatorHandle
Colin Crosse8a67a72016-08-07 21:17:54 -070064}
65
Colin Cross76f2f972016-08-11 14:10:54 -070066func (mutator *mutator) Parallel() MutatorHandle {
Colin Crosse8a67a72016-08-07 21:17:54 -070067 mutator.parallel = true
68 return mutator
Colin Cross463a90e2015-06-17 14:20:06 -070069}
70
71func 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 Cross6362e272015-10-29 15:25:03 -070082 for _, t := range mutators {
Colin Cross76f2f972016-08-11 14:10:54 -070083 var handle blueprint.MutatorHandle
Colin Cross6362e272015-10-29 15:25:03 -070084 if t.bottomUpMutator != nil {
Colin Cross76f2f972016-08-11 14:10:54 -070085 handle = ctx.RegisterBottomUpMutator(t.name, t.bottomUpMutator)
86 } else if t.topDownMutator != nil {
87 handle = ctx.RegisterTopDownMutator(t.name, t.topDownMutator)
Colin Cross6362e272015-10-29 15:25:03 -070088 }
Colin Cross76f2f972016-08-11 14:10:54 -070089 if t.parallel {
90 handle.Parallel()
Colin Cross6362e272015-10-29 15:25:03 -070091 }
Colin Cross463a90e2015-06-17 14:20:06 -070092 }
93
94 return ctx
95}