Colin Cross | 8e0c511 | 2015-01-23 14:15:10 -0800 | [diff] [blame] | 1 | // Copyright 2014 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 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 15 | package blueprint |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Colin Cross | c581620 | 2017-12-11 14:39:10 -0800 | [diff] [blame] | 19 | |
Colin Cross | b519a7e | 2017-02-01 13:21:35 -0800 | [diff] [blame] | 20 | "github.com/google/blueprint/pathtools" |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | type Singleton interface { |
| 24 | GenerateBuildActions(SingletonContext) |
| 25 | } |
| 26 | |
| 27 | type SingletonContext interface { |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 28 | Config() interface{} |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 29 | |
Colin Cross | 9226d6c | 2019-02-25 18:07:44 -0800 | [diff] [blame] | 30 | Name() string |
| 31 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 32 | ModuleName(module Module) string |
| 33 | ModuleDir(module Module) string |
Colin Cross | 8c602f7 | 2015-12-17 18:02:11 -0800 | [diff] [blame] | 34 | ModuleSubDir(module Module) string |
Dan Willemsen | c98e55b | 2016-07-25 15:51:50 -0700 | [diff] [blame] | 35 | ModuleType(module Module) string |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 36 | BlueprintFile(module Module) string |
| 37 | |
| 38 | ModuleErrorf(module Module, format string, args ...interface{}) |
| 39 | Errorf(format string, args ...interface{}) |
Dan Willemsen | 265d92c | 2015-12-09 18:06:23 -0800 | [diff] [blame] | 40 | Failed() bool |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 41 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 42 | Variable(pctx PackageContext, name, value string) |
| 43 | Rule(pctx PackageContext, name string, params RuleParams, argNames ...string) Rule |
| 44 | Build(pctx PackageContext, params BuildParams) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 45 | RequireNinjaVersion(major, minor, micro int) |
| 46 | |
Colin Cross | a259945 | 2015-11-18 16:01:01 -0800 | [diff] [blame] | 47 | // SetNinjaBuildDir sets the value of the top-level "builddir" Ninja variable |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 48 | // that controls where Ninja stores its build log files. This value can be |
Colin Cross | a259945 | 2015-11-18 16:01:01 -0800 | [diff] [blame] | 49 | // set at most one time for a single build, later calls are ignored. |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 50 | SetNinjaBuildDir(pctx PackageContext, value string) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 51 | |
Dan Willemsen | ab223a5 | 2018-07-05 21:56:59 -0700 | [diff] [blame] | 52 | // AddSubninja adds a ninja file to include with subninja. This should likely |
| 53 | // only ever be used inside bootstrap to handle glob rules. |
| 54 | AddSubninja(file string) |
| 55 | |
Dan Willemsen | 4bb6276 | 2016-01-14 15:42:54 -0800 | [diff] [blame] | 56 | // Eval takes a string with embedded ninja variables, and returns a string |
| 57 | // with all of the variables recursively expanded. Any variables references |
| 58 | // are expanded in the scope of the PackageContext. |
| 59 | Eval(pctx PackageContext, ninjaStr string) (string, error) |
| 60 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 61 | VisitAllModules(visit func(Module)) |
| 62 | VisitAllModulesIf(pred func(Module) bool, visit func(Module)) |
Jamie Gennis | e98b8a9 | 2014-06-17 10:24:24 -0700 | [diff] [blame] | 63 | VisitDepsDepthFirst(module Module, visit func(Module)) |
| 64 | VisitDepsDepthFirstIf(module Module, pred func(Module) bool, |
| 65 | visit func(Module)) |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 66 | |
Colin Cross | 24ad587 | 2015-11-17 16:22:29 -0800 | [diff] [blame] | 67 | VisitAllModuleVariants(module Module, visit func(Module)) |
| 68 | |
| 69 | PrimaryModule(module Module) Module |
| 70 | FinalModule(module Module) Module |
| 71 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 72 | AddNinjaFileDeps(deps ...string) |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 73 | |
Dan Willemsen | b6c9023 | 2018-02-23 14:49:45 -0800 | [diff] [blame] | 74 | // GlobWithDeps returns a list of files and directories that match the |
| 75 | // specified pattern but do not match any of the patterns in excludes. |
| 76 | // Any directories will have a '/' suffix. It also adds efficient |
| 77 | // dependencies to rerun the primary builder whenever a file matching |
| 78 | // the pattern as added or removed, without rerunning if a file that |
| 79 | // does not match the pattern is added to a searched directory. |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 80 | GlobWithDeps(pattern string, excludes []string) ([]string, error) |
Colin Cross | b519a7e | 2017-02-01 13:21:35 -0800 | [diff] [blame] | 81 | |
| 82 | Fs() pathtools.FileSystem |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | var _ SingletonContext = (*singletonContext)(nil) |
| 86 | |
| 87 | type singletonContext struct { |
Colin Cross | 9226d6c | 2019-02-25 18:07:44 -0800 | [diff] [blame] | 88 | name string |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 89 | context *Context |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 90 | config interface{} |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 91 | scope *localScope |
Dan Willemsen | 4bb6276 | 2016-01-14 15:42:54 -0800 | [diff] [blame] | 92 | globals *liveTracker |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 93 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 94 | ninjaFileDeps []string |
| 95 | errs []error |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 96 | |
| 97 | actionDefs localBuildActions |
| 98 | } |
| 99 | |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 100 | func (s *singletonContext) Config() interface{} { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 101 | return s.config |
| 102 | } |
| 103 | |
Colin Cross | 9226d6c | 2019-02-25 18:07:44 -0800 | [diff] [blame] | 104 | func (s *singletonContext) Name() string { |
| 105 | return s.name |
| 106 | } |
| 107 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 108 | func (s *singletonContext) ModuleName(logicModule Module) string { |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 109 | return s.context.ModuleName(logicModule) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 112 | func (s *singletonContext) ModuleDir(logicModule Module) string { |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 113 | return s.context.ModuleDir(logicModule) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Colin Cross | 8c602f7 | 2015-12-17 18:02:11 -0800 | [diff] [blame] | 116 | func (s *singletonContext) ModuleSubDir(logicModule Module) string { |
| 117 | return s.context.ModuleSubDir(logicModule) |
| 118 | } |
| 119 | |
Dan Willemsen | c98e55b | 2016-07-25 15:51:50 -0700 | [diff] [blame] | 120 | func (s *singletonContext) ModuleType(logicModule Module) string { |
| 121 | return s.context.ModuleType(logicModule) |
| 122 | } |
| 123 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 124 | func (s *singletonContext) BlueprintFile(logicModule Module) string { |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 125 | return s.context.BlueprintFile(logicModule) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Colin Cross | 0aa6a5f | 2016-01-07 13:43:09 -0800 | [diff] [blame] | 128 | func (s *singletonContext) error(err error) { |
| 129 | if err != nil { |
| 130 | s.errs = append(s.errs, err) |
| 131 | } |
| 132 | } |
| 133 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 134 | func (s *singletonContext) ModuleErrorf(logicModule Module, format string, |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 135 | args ...interface{}) { |
| 136 | |
Colin Cross | 0aa6a5f | 2016-01-07 13:43:09 -0800 | [diff] [blame] | 137 | s.error(s.context.ModuleErrorf(logicModule, format, args...)) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | func (s *singletonContext) Errorf(format string, args ...interface{}) { |
| 141 | // TODO: Make this not result in the error being printed as "internal error" |
Colin Cross | 0aa6a5f | 2016-01-07 13:43:09 -0800 | [diff] [blame] | 142 | s.error(fmt.Errorf(format, args...)) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Dan Willemsen | 265d92c | 2015-12-09 18:06:23 -0800 | [diff] [blame] | 145 | func (s *singletonContext) Failed() bool { |
| 146 | return len(s.errs) > 0 |
| 147 | } |
| 148 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 149 | func (s *singletonContext) Variable(pctx PackageContext, name, value string) { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 150 | s.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 151 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 152 | v, err := s.scope.AddLocalVariable(name, value) |
| 153 | if err != nil { |
| 154 | panic(err) |
| 155 | } |
| 156 | |
| 157 | s.actionDefs.variables = append(s.actionDefs.variables, v) |
| 158 | } |
| 159 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 160 | func (s *singletonContext) Rule(pctx PackageContext, name string, |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 161 | params RuleParams, argNames ...string) Rule { |
Jamie Gennis | cbc6f86 | 2014-06-05 20:00:22 -0700 | [diff] [blame] | 162 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 163 | s.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 164 | |
Jamie Gennis | cbc6f86 | 2014-06-05 20:00:22 -0700 | [diff] [blame] | 165 | r, err := s.scope.AddLocalRule(name, ¶ms, argNames...) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 166 | if err != nil { |
| 167 | panic(err) |
| 168 | } |
| 169 | |
| 170 | s.actionDefs.rules = append(s.actionDefs.rules, r) |
| 171 | |
| 172 | return r |
| 173 | } |
| 174 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 175 | func (s *singletonContext) Build(pctx PackageContext, params BuildParams) { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 176 | s.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 177 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 178 | def, err := parseBuildParams(s.scope, ¶ms) |
| 179 | if err != nil { |
| 180 | panic(err) |
| 181 | } |
| 182 | |
| 183 | s.actionDefs.buildDefs = append(s.actionDefs.buildDefs, def) |
| 184 | } |
| 185 | |
Dan Willemsen | 4bb6276 | 2016-01-14 15:42:54 -0800 | [diff] [blame] | 186 | func (s *singletonContext) Eval(pctx PackageContext, str string) (string, error) { |
| 187 | s.scope.ReparentTo(pctx) |
| 188 | |
| 189 | ninjaStr, err := parseNinjaString(s.scope, str) |
| 190 | if err != nil { |
| 191 | return "", err |
| 192 | } |
| 193 | |
| 194 | err = s.globals.addNinjaStringDeps(ninjaStr) |
| 195 | if err != nil { |
| 196 | return "", err |
| 197 | } |
| 198 | |
| 199 | return ninjaStr.Eval(s.globals.variables) |
| 200 | } |
| 201 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 202 | func (s *singletonContext) RequireNinjaVersion(major, minor, micro int) { |
| 203 | s.context.requireNinjaVersion(major, minor, micro) |
| 204 | } |
| 205 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 206 | func (s *singletonContext) SetNinjaBuildDir(pctx PackageContext, value string) { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 207 | s.scope.ReparentTo(pctx) |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 208 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 209 | ninjaValue, err := parseNinjaString(s.scope, value) |
| 210 | if err != nil { |
| 211 | panic(err) |
| 212 | } |
| 213 | |
Colin Cross | a259945 | 2015-11-18 16:01:01 -0800 | [diff] [blame] | 214 | s.context.setNinjaBuildDir(ninjaValue) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Dan Willemsen | ab223a5 | 2018-07-05 21:56:59 -0700 | [diff] [blame] | 217 | func (s *singletonContext) AddSubninja(file string) { |
| 218 | s.context.subninjas = append(s.context.subninjas, file) |
| 219 | } |
| 220 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 221 | func (s *singletonContext) VisitAllModules(visit func(Module)) { |
Colin Cross | 7f90d17 | 2018-09-19 13:37:29 -0700 | [diff] [blame] | 222 | var visitingModule Module |
| 223 | defer func() { |
| 224 | if r := recover(); r != nil { |
| 225 | panic(newPanicErrorf(r, "VisitAllModules(%s) for module %s", |
Colin Cross | 818af3b | 2019-03-25 15:04:09 -0700 | [diff] [blame^] | 226 | funcName(visit), s.context.moduleInfo[visitingModule])) |
Colin Cross | 7f90d17 | 2018-09-19 13:37:29 -0700 | [diff] [blame] | 227 | } |
| 228 | }() |
| 229 | |
| 230 | s.context.VisitAllModules(func(m Module) { |
| 231 | visitingModule = m |
| 232 | visit(m) |
| 233 | }) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | func (s *singletonContext) VisitAllModulesIf(pred func(Module) bool, |
| 237 | visit func(Module)) { |
| 238 | |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 239 | s.context.VisitAllModulesIf(pred, visit) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 240 | } |
Jamie Gennis | e98b8a9 | 2014-06-17 10:24:24 -0700 | [diff] [blame] | 241 | |
| 242 | func (s *singletonContext) VisitDepsDepthFirst(module Module, |
| 243 | visit func(Module)) { |
| 244 | |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 245 | s.context.VisitDepsDepthFirst(module, visit) |
Jamie Gennis | e98b8a9 | 2014-06-17 10:24:24 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | func (s *singletonContext) VisitDepsDepthFirstIf(module Module, |
| 249 | pred func(Module) bool, visit func(Module)) { |
| 250 | |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 251 | s.context.VisitDepsDepthFirstIf(module, pred, visit) |
Jamie Gennis | e98b8a9 | 2014-06-17 10:24:24 -0700 | [diff] [blame] | 252 | } |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 253 | |
Colin Cross | 24ad587 | 2015-11-17 16:22:29 -0800 | [diff] [blame] | 254 | func (s *singletonContext) PrimaryModule(module Module) Module { |
| 255 | return s.context.PrimaryModule(module) |
| 256 | } |
| 257 | |
| 258 | func (s *singletonContext) FinalModule(module Module) Module { |
| 259 | return s.context.FinalModule(module) |
| 260 | } |
| 261 | |
| 262 | func (s *singletonContext) VisitAllModuleVariants(module Module, visit func(Module)) { |
| 263 | s.context.VisitAllModuleVariants(module, visit) |
| 264 | } |
| 265 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 266 | func (s *singletonContext) AddNinjaFileDeps(deps ...string) { |
| 267 | s.ninjaFileDeps = append(s.ninjaFileDeps, deps...) |
| 268 | } |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 269 | |
| 270 | func (s *singletonContext) GlobWithDeps(pattern string, |
| 271 | excludes []string) ([]string, error) { |
| 272 | return s.context.glob(pattern, excludes) |
| 273 | } |
Colin Cross | b519a7e | 2017-02-01 13:21:35 -0800 | [diff] [blame] | 274 | |
| 275 | func (s *singletonContext) Fs() pathtools.FileSystem { |
| 276 | return s.context.fs |
| 277 | } |