blob: 0ad9d30cbc569de8ed6cae9249f8c8b32f12ffc1 [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
Colin Cross798bfce2016-10-12 14:28:16 -070015package android
Colin Cross463a90e2015-06-17 14:20:06 -070016
Colin Cross4498afc2016-10-13 14:18:27 -070017import (
18 "sync"
19
20 "github.com/google/blueprint"
21)
Colin Cross463a90e2015-06-17 14:20:06 -070022
23type moduleType struct {
24 name string
25 factory blueprint.ModuleFactory
26}
27
28var moduleTypes []moduleType
29
30type singleton struct {
31 name string
32 factory blueprint.SingletonFactory
33}
34
35var singletons []singleton
36
Colin Cross6362e272015-10-29 15:25:03 -070037type mutator struct {
38 name string
39 bottomUpMutator blueprint.BottomUpMutator
40 topDownMutator blueprint.TopDownMutator
Colin Crosse8a67a72016-08-07 21:17:54 -070041 parallel bool
Colin Cross463a90e2015-06-17 14:20:06 -070042}
43
Colin Crosse8a67a72016-08-07 21:17:54 -070044var mutators []*mutator
Colin Cross463a90e2015-06-17 14:20:06 -070045
46func RegisterModuleType(name string, factory blueprint.ModuleFactory) {
47 moduleTypes = append(moduleTypes, moduleType{name, factory})
48}
49
50func RegisterSingletonType(name string, factory blueprint.SingletonFactory) {
51 singletons = append(singletons, singleton{name, factory})
52}
53
Colin Cross4498afc2016-10-13 14:18:27 -070054var registerMutatorsOnce sync.Once
55
Colin Cross463a90e2015-06-17 14:20:06 -070056func 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 Cross4498afc2016-10-13 14:18:27 -070067 registerMutatorsOnce.Do(registerMutators)
Colin Cross1e676be2016-10-12 14:38:15 -070068
Colin Cross6362e272015-10-29 15:25:03 -070069 for _, t := range mutators {
Colin Cross76f2f972016-08-11 14:10:54 -070070 var handle blueprint.MutatorHandle
Colin Cross6362e272015-10-29 15:25:03 -070071 if t.bottomUpMutator != nil {
Colin Cross76f2f972016-08-11 14:10:54 -070072 handle = ctx.RegisterBottomUpMutator(t.name, t.bottomUpMutator)
73 } else if t.topDownMutator != nil {
74 handle = ctx.RegisterTopDownMutator(t.name, t.topDownMutator)
Colin Cross6362e272015-10-29 15:25:03 -070075 }
Colin Cross76f2f972016-08-11 14:10:54 -070076 if t.parallel {
77 handle.Parallel()
Colin Cross6362e272015-10-29 15:25:03 -070078 }
Colin Cross463a90e2015-06-17 14:20:06 -070079 }
80
81 return ctx
82}