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