• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15package android
16
17import (
18	"github.com/google/blueprint"
19)
20
21type moduleType struct {
22	name    string
23	factory ModuleFactory
24}
25
26var moduleTypes []moduleType
27
28type singleton struct {
29	name    string
30	factory blueprint.SingletonFactory
31}
32
33var singletons []singleton
34var preSingletons []singleton
35
36type mutator struct {
37	name            string
38	bottomUpMutator blueprint.BottomUpMutator
39	topDownMutator  blueprint.TopDownMutator
40	parallel        bool
41}
42
43type ModuleFactory func() Module
44
45// ModuleFactoryAdaptor wraps a ModuleFactory into a blueprint.ModuleFactory by converting a Module
46// into a blueprint.Module and a list of property structs
47func ModuleFactoryAdaptor(factory ModuleFactory) blueprint.ModuleFactory {
48	return func() (blueprint.Module, []interface{}) {
49		module := factory()
50		return module, module.GetProperties()
51	}
52}
53
54type SingletonFactory func() Singleton
55
56// SingletonFactoryAdaptor wraps a SingletonFactory into a blueprint.SingletonFactory by converting
57// a Singleton into a blueprint.Singleton
58func SingletonFactoryAdaptor(factory SingletonFactory) blueprint.SingletonFactory {
59	return func() blueprint.Singleton {
60		singleton := factory()
61		if makevars, ok := singleton.(SingletonMakeVarsProvider); ok {
62			registerSingletonMakeVarsProvider(makevars)
63		}
64		return &singletonAdaptor{Singleton: singleton}
65	}
66}
67
68func RegisterModuleType(name string, factory ModuleFactory) {
69	moduleTypes = append(moduleTypes, moduleType{name, factory})
70}
71
72func RegisterSingletonType(name string, factory SingletonFactory) {
73	singletons = append(singletons, singleton{name, SingletonFactoryAdaptor(factory)})
74}
75
76func RegisterPreSingletonType(name string, factory SingletonFactory) {
77	preSingletons = append(preSingletons, singleton{name, SingletonFactoryAdaptor(factory)})
78}
79
80type Context struct {
81	*blueprint.Context
82}
83
84func NewContext() *Context {
85	return &Context{blueprint.NewContext()}
86}
87
88func (ctx *Context) Register() {
89	for _, t := range preSingletons {
90		ctx.RegisterPreSingletonType(t.name, t.factory)
91	}
92
93	for _, t := range moduleTypes {
94		ctx.RegisterModuleType(t.name, ModuleFactoryAdaptor(t.factory))
95	}
96
97	for _, t := range singletons {
98		ctx.RegisterSingletonType(t.name, t.factory)
99	}
100
101	registerMutators(ctx.Context, preArch, preDeps, postDeps)
102
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
107	ctx.RegisterSingletonType("env", SingletonFactoryAdaptor(EnvSingleton))
108}
109
110func 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}
117