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