github.com/westcoastroms/westcoastroms-build@v0.0.0-20190928114312-2350e5a73030/build/soong/android/register.go (about)

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