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

     1  // Copyright 2017 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  	"github.com/google/blueprint/pathtools"
    20  )
    21  
    22  // SingletonContext
    23  type SingletonContext interface {
    24  	Config() Config
    25  
    26  	ModuleName(module blueprint.Module) string
    27  	ModuleDir(module blueprint.Module) string
    28  	ModuleSubDir(module blueprint.Module) string
    29  	ModuleType(module blueprint.Module) string
    30  	BlueprintFile(module blueprint.Module) string
    31  
    32  	ModuleErrorf(module blueprint.Module, format string, args ...interface{})
    33  	Errorf(format string, args ...interface{})
    34  	Failed() bool
    35  
    36  	Variable(pctx PackageContext, name, value string)
    37  	Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
    38  	Build(pctx PackageContext, params BuildParams)
    39  	RequireNinjaVersion(major, minor, micro int)
    40  
    41  	// SetNinjaBuildDir sets the value of the top-level "builddir" Ninja variable
    42  	// that controls where Ninja stores its build log files.  This value can be
    43  	// set at most one time for a single build, later calls are ignored.
    44  	SetNinjaBuildDir(pctx PackageContext, value string)
    45  
    46  	// Eval takes a string with embedded ninja variables, and returns a string
    47  	// with all of the variables recursively expanded. Any variables references
    48  	// are expanded in the scope of the PackageContext.
    49  	Eval(pctx PackageContext, ninjaStr string) (string, error)
    50  
    51  	VisitAllModules(visit func(Module))
    52  	VisitAllModulesIf(pred func(Module) bool, visit func(Module))
    53  	VisitDepsDepthFirst(module Module, visit func(Module))
    54  	VisitDepsDepthFirstIf(module Module, pred func(Module) bool,
    55  		visit func(Module))
    56  
    57  	VisitAllModuleVariants(module Module, visit func(Module))
    58  
    59  	PrimaryModule(module Module) Module
    60  	FinalModule(module Module) Module
    61  
    62  	AddNinjaFileDeps(deps ...string)
    63  
    64  	// GlobWithDeps returns a list of files that match the specified pattern but do not match any
    65  	// of the patterns in excludes.  It also adds efficient dependencies to rerun the primary
    66  	// builder whenever a file matching the pattern as added or removed, without rerunning if a
    67  	// file that does not match the pattern is added to a searched directory.
    68  	GlobWithDeps(pattern string, excludes []string) ([]string, error)
    69  
    70  	Fs() pathtools.FileSystem
    71  }
    72  
    73  type singletonAdaptor struct {
    74  	Singleton
    75  }
    76  
    77  func (s singletonAdaptor) GenerateBuildActions(ctx blueprint.SingletonContext) {
    78  	s.Singleton.GenerateBuildActions(singletonContextAdaptor{ctx})
    79  }
    80  
    81  type Singleton interface {
    82  	GenerateBuildActions(SingletonContext)
    83  }
    84  
    85  type singletonContextAdaptor struct {
    86  	blueprint.SingletonContext
    87  }
    88  
    89  func (s singletonContextAdaptor) Config() Config {
    90  	return s.SingletonContext.Config().(Config)
    91  }
    92  
    93  func (s singletonContextAdaptor) Variable(pctx PackageContext, name, value string) {
    94  	s.SingletonContext.Variable(pctx.PackageContext, name, value)
    95  }
    96  
    97  func (s singletonContextAdaptor) Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule {
    98  	return s.SingletonContext.Rule(pctx.PackageContext, name, params, argNames...)
    99  }
   100  
   101  func (s singletonContextAdaptor) Build(pctx PackageContext, params BuildParams) {
   102  	bparams := convertBuildParams(params)
   103  	s.SingletonContext.Build(pctx.PackageContext, bparams)
   104  
   105  }
   106  
   107  func (s singletonContextAdaptor) SetNinjaBuildDir(pctx PackageContext, value string) {
   108  	s.SingletonContext.SetNinjaBuildDir(pctx.PackageContext, value)
   109  }
   110  
   111  func (s singletonContextAdaptor) Eval(pctx PackageContext, ninjaStr string) (string, error) {
   112  	return s.SingletonContext.Eval(pctx.PackageContext, ninjaStr)
   113  }
   114  
   115  // visitAdaptor wraps a visit function that takes an android.Module parameter into
   116  // a function that takes an blueprint.Module parameter and only calls the visit function if the
   117  // blueprint.Module is an android.Module.
   118  func visitAdaptor(visit func(Module)) func(blueprint.Module) {
   119  	return func(module blueprint.Module) {
   120  		if aModule, ok := module.(Module); ok {
   121  			visit(aModule)
   122  		}
   123  	}
   124  }
   125  
   126  // predAdaptor wraps a pred function that takes an android.Module parameter
   127  // into a function that takes an blueprint.Module parameter and only calls the visit function if the
   128  // blueprint.Module is an android.Module, otherwise returns false.
   129  func predAdaptor(pred func(Module) bool) func(blueprint.Module) bool {
   130  	return func(module blueprint.Module) bool {
   131  		if aModule, ok := module.(Module); ok {
   132  			return pred(aModule)
   133  		} else {
   134  			return false
   135  		}
   136  	}
   137  }
   138  
   139  func (s singletonContextAdaptor) VisitAllModules(visit func(Module)) {
   140  	s.SingletonContext.VisitAllModules(visitAdaptor(visit))
   141  }
   142  
   143  func (s singletonContextAdaptor) VisitAllModulesIf(pred func(Module) bool, visit func(Module)) {
   144  	s.SingletonContext.VisitAllModulesIf(predAdaptor(pred), visitAdaptor(visit))
   145  }
   146  
   147  func (s singletonContextAdaptor) VisitDepsDepthFirst(module Module, visit func(Module)) {
   148  	s.SingletonContext.VisitDepsDepthFirst(module, visitAdaptor(visit))
   149  }
   150  
   151  func (s singletonContextAdaptor) VisitDepsDepthFirstIf(module Module, pred func(Module) bool, visit func(Module)) {
   152  	s.SingletonContext.VisitDepsDepthFirstIf(module, predAdaptor(pred), visitAdaptor(visit))
   153  }
   154  
   155  func (s singletonContextAdaptor) VisitAllModuleVariants(module Module, visit func(Module)) {
   156  	s.SingletonContext.VisitAllModuleVariants(module, visitAdaptor(visit))
   157  }
   158  
   159  func (s singletonContextAdaptor) PrimaryModule(module Module) Module {
   160  	return s.SingletonContext.PrimaryModule(module).(Module)
   161  }
   162  
   163  func (s singletonContextAdaptor) FinalModule(module Module) Module {
   164  	return s.SingletonContext.FinalModule(module).(Module)
   165  }