Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 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" |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 19 | ) |
| 20 | |
| 21 | // SingletonContext |
| 22 | type SingletonContext interface { |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 23 | Config() Config |
Colin Cross | 65494b9 | 2019-02-07 14:25:51 -0800 | [diff] [blame] | 24 | DeviceConfig() DeviceConfig |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 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) |
Colin Cross | 5901439 | 2017-12-12 11:05:06 -0800 | [diff] [blame] | 37 | Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 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 | |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 51 | VisitAllModulesBlueprint(visit func(blueprint.Module)) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 52 | VisitAllModules(visit func(Module)) |
| 53 | VisitAllModulesIf(pred func(Module) bool, visit func(Module)) |
Mitch Phillips | 3a7a31b | 2019-10-16 15:00:12 -0700 | [diff] [blame] | 54 | |
| 55 | VisitDirectDeps(module Module, visit func(Module)) |
| 56 | VisitDirectDepsIf(module Module, pred func(Module) bool, visit func(Module)) |
| 57 | |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 58 | // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 59 | VisitDepsDepthFirst(module Module, visit func(Module)) |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 60 | // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 61 | VisitDepsDepthFirstIf(module Module, pred func(Module) bool, |
| 62 | visit func(Module)) |
| 63 | |
| 64 | VisitAllModuleVariants(module Module, visit func(Module)) |
| 65 | |
| 66 | PrimaryModule(module Module) Module |
| 67 | FinalModule(module Module) Module |
| 68 | |
| 69 | AddNinjaFileDeps(deps ...string) |
| 70 | |
| 71 | // GlobWithDeps returns a list of files that match the specified pattern but do not match any |
| 72 | // of the patterns in excludes. It also adds efficient dependencies to rerun the primary |
| 73 | // builder whenever a file matching the pattern as added or removed, without rerunning if a |
| 74 | // file that does not match the pattern is added to a searched directory. |
| 75 | GlobWithDeps(pattern string, excludes []string) ([]string, error) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | type singletonAdaptor struct { |
| 79 | Singleton |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 80 | |
| 81 | buildParams []BuildParams |
| 82 | ruleParams map[blueprint.Rule]blueprint.RuleParams |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 85 | var _ testBuildProvider = (*singletonAdaptor)(nil) |
| 86 | |
| 87 | func (s *singletonAdaptor) GenerateBuildActions(ctx blueprint.SingletonContext) { |
| 88 | sctx := &singletonContextAdaptor{SingletonContext: ctx} |
| 89 | if sctx.Config().captureBuild { |
| 90 | sctx.ruleParams = make(map[blueprint.Rule]blueprint.RuleParams) |
| 91 | } |
| 92 | |
| 93 | s.Singleton.GenerateBuildActions(sctx) |
| 94 | |
| 95 | s.buildParams = sctx.buildParams |
| 96 | s.ruleParams = sctx.ruleParams |
| 97 | } |
| 98 | |
| 99 | func (s *singletonAdaptor) BuildParamsForTests() []BuildParams { |
| 100 | return s.buildParams |
| 101 | } |
| 102 | |
| 103 | func (s *singletonAdaptor) RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams { |
| 104 | return s.ruleParams |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | type Singleton interface { |
| 108 | GenerateBuildActions(SingletonContext) |
| 109 | } |
| 110 | |
| 111 | type singletonContextAdaptor struct { |
| 112 | blueprint.SingletonContext |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 113 | |
| 114 | buildParams []BuildParams |
| 115 | ruleParams map[blueprint.Rule]blueprint.RuleParams |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 118 | func (s *singletonContextAdaptor) Config() Config { |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 119 | return s.SingletonContext.Config().(Config) |
| 120 | } |
| 121 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 122 | func (s *singletonContextAdaptor) DeviceConfig() DeviceConfig { |
Colin Cross | 65494b9 | 2019-02-07 14:25:51 -0800 | [diff] [blame] | 123 | return DeviceConfig{s.Config().deviceConfig} |
| 124 | } |
| 125 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 126 | func (s *singletonContextAdaptor) Variable(pctx PackageContext, name, value string) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 127 | s.SingletonContext.Variable(pctx.PackageContext, name, value) |
| 128 | } |
| 129 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 130 | func (s *singletonContextAdaptor) Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule { |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 131 | if s.Config().UseRemoteBuild() && params.Pool == nil { |
Ramy Medhat | dd0418a | 2019-11-04 18:16:11 -0500 | [diff] [blame] | 132 | // When USE_GOMA=true or USE_RBE=true are set and the rule is not supported by goma/RBE, restrict |
| 133 | // jobs to the local parallelism value |
Colin Cross | 2e2dbc2 | 2019-09-25 13:31:46 -0700 | [diff] [blame] | 134 | params.Pool = localPool |
| 135 | } |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 136 | rule := s.SingletonContext.Rule(pctx.PackageContext, name, params, argNames...) |
| 137 | if s.Config().captureBuild { |
| 138 | s.ruleParams[rule] = params |
| 139 | } |
| 140 | return rule |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 143 | func (s *singletonContextAdaptor) Build(pctx PackageContext, params BuildParams) { |
| 144 | if s.Config().captureBuild { |
| 145 | s.buildParams = append(s.buildParams, params) |
| 146 | } |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 147 | bparams := convertBuildParams(params) |
| 148 | s.SingletonContext.Build(pctx.PackageContext, bparams) |
| 149 | |
| 150 | } |
| 151 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 152 | func (s *singletonContextAdaptor) SetNinjaBuildDir(pctx PackageContext, value string) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 153 | s.SingletonContext.SetNinjaBuildDir(pctx.PackageContext, value) |
| 154 | } |
| 155 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 156 | func (s *singletonContextAdaptor) Eval(pctx PackageContext, ninjaStr string) (string, error) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 157 | return s.SingletonContext.Eval(pctx.PackageContext, ninjaStr) |
| 158 | } |
| 159 | |
| 160 | // visitAdaptor wraps a visit function that takes an android.Module parameter into |
| 161 | // a function that takes an blueprint.Module parameter and only calls the visit function if the |
| 162 | // blueprint.Module is an android.Module. |
| 163 | func visitAdaptor(visit func(Module)) func(blueprint.Module) { |
| 164 | return func(module blueprint.Module) { |
| 165 | if aModule, ok := module.(Module); ok { |
| 166 | visit(aModule) |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // predAdaptor wraps a pred function that takes an android.Module parameter |
| 172 | // into a function that takes an blueprint.Module parameter and only calls the visit function if the |
| 173 | // blueprint.Module is an android.Module, otherwise returns false. |
| 174 | func predAdaptor(pred func(Module) bool) func(blueprint.Module) bool { |
| 175 | return func(module blueprint.Module) bool { |
| 176 | if aModule, ok := module.(Module); ok { |
| 177 | return pred(aModule) |
| 178 | } else { |
| 179 | return false |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 184 | func (s *singletonContextAdaptor) VisitAllModulesBlueprint(visit func(blueprint.Module)) { |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 185 | s.SingletonContext.VisitAllModules(visit) |
| 186 | } |
| 187 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 188 | func (s *singletonContextAdaptor) VisitAllModules(visit func(Module)) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 189 | s.SingletonContext.VisitAllModules(visitAdaptor(visit)) |
| 190 | } |
| 191 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 192 | func (s *singletonContextAdaptor) VisitAllModulesIf(pred func(Module) bool, visit func(Module)) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 193 | s.SingletonContext.VisitAllModulesIf(predAdaptor(pred), visitAdaptor(visit)) |
| 194 | } |
| 195 | |
Mitch Phillips | 3a7a31b | 2019-10-16 15:00:12 -0700 | [diff] [blame] | 196 | func (s *singletonContextAdaptor) VisitDirectDeps(module Module, visit func(Module)) { |
| 197 | s.SingletonContext.VisitDirectDeps(module, visitAdaptor(visit)) |
| 198 | } |
| 199 | |
| 200 | func (s *singletonContextAdaptor) VisitDirectDepsIf(module Module, pred func(Module) bool, visit func(Module)) { |
| 201 | s.SingletonContext.VisitDirectDepsIf(module, predAdaptor(pred), visitAdaptor(visit)) |
| 202 | } |
| 203 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 204 | func (s *singletonContextAdaptor) VisitDepsDepthFirst(module Module, visit func(Module)) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 205 | s.SingletonContext.VisitDepsDepthFirst(module, visitAdaptor(visit)) |
| 206 | } |
| 207 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 208 | func (s *singletonContextAdaptor) VisitDepsDepthFirstIf(module Module, pred func(Module) bool, visit func(Module)) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 209 | s.SingletonContext.VisitDepsDepthFirstIf(module, predAdaptor(pred), visitAdaptor(visit)) |
| 210 | } |
| 211 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 212 | func (s *singletonContextAdaptor) VisitAllModuleVariants(module Module, visit func(Module)) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 213 | s.SingletonContext.VisitAllModuleVariants(module, visitAdaptor(visit)) |
| 214 | } |
| 215 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 216 | func (s *singletonContextAdaptor) PrimaryModule(module Module) Module { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 217 | return s.SingletonContext.PrimaryModule(module).(Module) |
| 218 | } |
| 219 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 220 | func (s *singletonContextAdaptor) FinalModule(module Module) Module { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 221 | return s.SingletonContext.FinalModule(module).(Module) |
| 222 | } |