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