blob: f926435b1d7ac11829b7cff6b18620b033047f3d [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 {
Colin Crossaabf6792017-11-29 00:27:14 -080024 Config() Config
Colin Cross0875c522017-11-28 17:34:01 -080025
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)
Colin Cross59014392017-12-12 11:05:06 -080037 Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
Colin Cross0875c522017-11-28 17:34:01 -080038 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
Colin Cross2465c3d2018-09-28 10:19:18 -070051 VisitAllModulesBlueprint(visit func(blueprint.Module))
Colin Cross0875c522017-11-28 17:34:01 -080052 VisitAllModules(visit func(Module))
53 VisitAllModulesIf(pred func(Module) bool, visit func(Module))
Colin Cross6b753602018-06-21 13:03:07 -070054 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
Colin Cross0875c522017-11-28 17:34:01 -080055 VisitDepsDepthFirst(module Module, visit func(Module))
Colin Cross6b753602018-06-21 13:03:07 -070056 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
Colin Cross0875c522017-11-28 17:34:01 -080057 VisitDepsDepthFirstIf(module Module, pred func(Module) bool,
58 visit func(Module))
59
60 VisitAllModuleVariants(module Module, visit func(Module))
61
62 PrimaryModule(module Module) Module
63 FinalModule(module Module) Module
64
65 AddNinjaFileDeps(deps ...string)
66
67 // GlobWithDeps returns a list of files that match the specified pattern but do not match any
68 // of the patterns in excludes. It also adds efficient dependencies to rerun the primary
69 // builder whenever a file matching the pattern as added or removed, without rerunning if a
70 // file that does not match the pattern is added to a searched directory.
71 GlobWithDeps(pattern string, excludes []string) ([]string, error)
72
73 Fs() pathtools.FileSystem
74}
75
76type singletonAdaptor struct {
77 Singleton
78}
79
80func (s singletonAdaptor) GenerateBuildActions(ctx blueprint.SingletonContext) {
81 s.Singleton.GenerateBuildActions(singletonContextAdaptor{ctx})
82}
83
84type Singleton interface {
85 GenerateBuildActions(SingletonContext)
86}
87
88type singletonContextAdaptor struct {
89 blueprint.SingletonContext
90}
91
Colin Crossaabf6792017-11-29 00:27:14 -080092func (s singletonContextAdaptor) Config() Config {
93 return s.SingletonContext.Config().(Config)
94}
95
Colin Cross0875c522017-11-28 17:34:01 -080096func (s singletonContextAdaptor) Variable(pctx PackageContext, name, value string) {
97 s.SingletonContext.Variable(pctx.PackageContext, name, value)
98}
99
Colin Cross59014392017-12-12 11:05:06 -0800100func (s singletonContextAdaptor) Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule {
101 return s.SingletonContext.Rule(pctx.PackageContext, name, params, argNames...)
Colin Cross0875c522017-11-28 17:34:01 -0800102}
103
104func (s singletonContextAdaptor) Build(pctx PackageContext, params BuildParams) {
105 bparams := convertBuildParams(params)
106 s.SingletonContext.Build(pctx.PackageContext, bparams)
107
108}
109
110func (s singletonContextAdaptor) SetNinjaBuildDir(pctx PackageContext, value string) {
111 s.SingletonContext.SetNinjaBuildDir(pctx.PackageContext, value)
112}
113
114func (s singletonContextAdaptor) Eval(pctx PackageContext, ninjaStr string) (string, error) {
115 return s.SingletonContext.Eval(pctx.PackageContext, ninjaStr)
116}
117
118// visitAdaptor wraps a visit function that takes an android.Module parameter into
119// a function that takes an blueprint.Module parameter and only calls the visit function if the
120// blueprint.Module is an android.Module.
121func visitAdaptor(visit func(Module)) func(blueprint.Module) {
122 return func(module blueprint.Module) {
123 if aModule, ok := module.(Module); ok {
124 visit(aModule)
125 }
126 }
127}
128
129// predAdaptor wraps a pred function that takes an android.Module parameter
130// into a function that takes an blueprint.Module parameter and only calls the visit function if the
131// blueprint.Module is an android.Module, otherwise returns false.
132func predAdaptor(pred func(Module) bool) func(blueprint.Module) bool {
133 return func(module blueprint.Module) bool {
134 if aModule, ok := module.(Module); ok {
135 return pred(aModule)
136 } else {
137 return false
138 }
139 }
140}
141
Colin Cross2465c3d2018-09-28 10:19:18 -0700142func (s singletonContextAdaptor) VisitAllModulesBlueprint(visit func(blueprint.Module)) {
143 s.SingletonContext.VisitAllModules(visit)
144}
145
Colin Cross0875c522017-11-28 17:34:01 -0800146func (s singletonContextAdaptor) VisitAllModules(visit func(Module)) {
147 s.SingletonContext.VisitAllModules(visitAdaptor(visit))
148}
149
150func (s singletonContextAdaptor) VisitAllModulesIf(pred func(Module) bool, visit func(Module)) {
151 s.SingletonContext.VisitAllModulesIf(predAdaptor(pred), visitAdaptor(visit))
152}
153
154func (s singletonContextAdaptor) VisitDepsDepthFirst(module Module, visit func(Module)) {
155 s.SingletonContext.VisitDepsDepthFirst(module, visitAdaptor(visit))
156}
157
158func (s singletonContextAdaptor) VisitDepsDepthFirstIf(module Module, pred func(Module) bool, visit func(Module)) {
159 s.SingletonContext.VisitDepsDepthFirstIf(module, predAdaptor(pred), visitAdaptor(visit))
160}
161
162func (s singletonContextAdaptor) VisitAllModuleVariants(module Module, visit func(Module)) {
163 s.SingletonContext.VisitAllModuleVariants(module, visitAdaptor(visit))
164}
165
166func (s singletonContextAdaptor) PrimaryModule(module Module) Module {
167 return s.SingletonContext.PrimaryModule(module).(Module)
168}
169
170func (s singletonContextAdaptor) FinalModule(module Module) Module {
171 return s.SingletonContext.FinalModule(module).(Module)
172}