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" |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 19 | ) |
| 20 | |
| 21 | type Singleton interface { |
| 22 | GenerateBuildActions(SingletonContext) |
| 23 | } |
| 24 | |
| 25 | type SingletonContext interface { |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 26 | Config() interface{} |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 27 | |
| 28 | ModuleName(module Module) string |
| 29 | ModuleDir(module Module) string |
Colin Cross | 8c602f7 | 2015-12-17 18:02:11 -0800 | [diff] [blame] | 30 | ModuleSubDir(module Module) string |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 31 | BlueprintFile(module Module) string |
| 32 | |
| 33 | ModuleErrorf(module Module, format string, args ...interface{}) |
| 34 | Errorf(format string, args ...interface{}) |
Dan Willemsen | 265d92c | 2015-12-09 18:06:23 -0800 | [diff] [blame] | 35 | Failed() bool |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 36 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 37 | Variable(pctx PackageContext, name, value string) |
| 38 | Rule(pctx PackageContext, name string, params RuleParams, argNames ...string) Rule |
| 39 | Build(pctx PackageContext, params BuildParams) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 40 | RequireNinjaVersion(major, minor, micro int) |
| 41 | |
Colin Cross | a259945 | 2015-11-18 16:01:01 -0800 | [diff] [blame] | 42 | // SetNinjaBuildDir sets the value of the top-level "builddir" Ninja variable |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 43 | // 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] | 44 | // 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] | 45 | SetNinjaBuildDir(pctx PackageContext, value string) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 46 | |
| 47 | VisitAllModules(visit func(Module)) |
| 48 | VisitAllModulesIf(pred func(Module) bool, visit func(Module)) |
Jamie Gennis | e98b8a9 | 2014-06-17 10:24:24 -0700 | [diff] [blame] | 49 | VisitDepsDepthFirst(module Module, visit func(Module)) |
| 50 | VisitDepsDepthFirstIf(module Module, pred func(Module) bool, |
| 51 | visit func(Module)) |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 52 | |
Colin Cross | 24ad587 | 2015-11-17 16:22:29 -0800 | [diff] [blame] | 53 | VisitAllModuleVariants(module Module, visit func(Module)) |
| 54 | |
| 55 | PrimaryModule(module Module) Module |
| 56 | FinalModule(module Module) Module |
| 57 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 58 | AddNinjaFileDeps(deps ...string) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | var _ SingletonContext = (*singletonContext)(nil) |
| 62 | |
| 63 | type singletonContext struct { |
| 64 | context *Context |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 65 | config interface{} |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 66 | scope *localScope |
| 67 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 68 | ninjaFileDeps []string |
| 69 | errs []error |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 70 | |
| 71 | actionDefs localBuildActions |
| 72 | } |
| 73 | |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 74 | func (s *singletonContext) Config() interface{} { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 75 | return s.config |
| 76 | } |
| 77 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 78 | func (s *singletonContext) ModuleName(logicModule Module) string { |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 79 | return s.context.ModuleName(logicModule) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 82 | func (s *singletonContext) ModuleDir(logicModule Module) string { |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 83 | return s.context.ModuleDir(logicModule) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Colin Cross | 8c602f7 | 2015-12-17 18:02:11 -0800 | [diff] [blame] | 86 | func (s *singletonContext) ModuleSubDir(logicModule Module) string { |
| 87 | return s.context.ModuleSubDir(logicModule) |
| 88 | } |
| 89 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 90 | func (s *singletonContext) BlueprintFile(logicModule Module) string { |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 91 | return s.context.BlueprintFile(logicModule) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Colin Cross | 0aa6a5f | 2016-01-07 13:43:09 -0800 | [diff] [blame^] | 94 | func (s *singletonContext) error(err error) { |
| 95 | if err != nil { |
| 96 | s.errs = append(s.errs, err) |
| 97 | } |
| 98 | } |
| 99 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 100 | func (s *singletonContext) ModuleErrorf(logicModule Module, format string, |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 101 | args ...interface{}) { |
| 102 | |
Colin Cross | 0aa6a5f | 2016-01-07 13:43:09 -0800 | [diff] [blame^] | 103 | s.error(s.context.ModuleErrorf(logicModule, format, args...)) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | func (s *singletonContext) Errorf(format string, args ...interface{}) { |
| 107 | // 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^] | 108 | s.error(fmt.Errorf(format, args...)) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Dan Willemsen | 265d92c | 2015-12-09 18:06:23 -0800 | [diff] [blame] | 111 | func (s *singletonContext) Failed() bool { |
| 112 | return len(s.errs) > 0 |
| 113 | } |
| 114 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 115 | func (s *singletonContext) Variable(pctx PackageContext, name, value string) { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 116 | s.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 117 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 118 | v, err := s.scope.AddLocalVariable(name, value) |
| 119 | if err != nil { |
| 120 | panic(err) |
| 121 | } |
| 122 | |
| 123 | s.actionDefs.variables = append(s.actionDefs.variables, v) |
| 124 | } |
| 125 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 126 | func (s *singletonContext) Rule(pctx PackageContext, name string, |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 127 | params RuleParams, argNames ...string) Rule { |
Jamie Gennis | cbc6f86 | 2014-06-05 20:00:22 -0700 | [diff] [blame] | 128 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 129 | s.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 130 | |
Jamie Gennis | cbc6f86 | 2014-06-05 20:00:22 -0700 | [diff] [blame] | 131 | r, err := s.scope.AddLocalRule(name, ¶ms, argNames...) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 132 | if err != nil { |
| 133 | panic(err) |
| 134 | } |
| 135 | |
| 136 | s.actionDefs.rules = append(s.actionDefs.rules, r) |
| 137 | |
| 138 | return r |
| 139 | } |
| 140 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 141 | func (s *singletonContext) Build(pctx PackageContext, params BuildParams) { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 142 | s.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 143 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 144 | def, err := parseBuildParams(s.scope, ¶ms) |
| 145 | if err != nil { |
| 146 | panic(err) |
| 147 | } |
| 148 | |
| 149 | s.actionDefs.buildDefs = append(s.actionDefs.buildDefs, def) |
| 150 | } |
| 151 | |
| 152 | func (s *singletonContext) RequireNinjaVersion(major, minor, micro int) { |
| 153 | s.context.requireNinjaVersion(major, minor, micro) |
| 154 | } |
| 155 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 156 | func (s *singletonContext) SetNinjaBuildDir(pctx PackageContext, value string) { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 157 | s.scope.ReparentTo(pctx) |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 158 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 159 | ninjaValue, err := parseNinjaString(s.scope, value) |
| 160 | if err != nil { |
| 161 | panic(err) |
| 162 | } |
| 163 | |
Colin Cross | a259945 | 2015-11-18 16:01:01 -0800 | [diff] [blame] | 164 | s.context.setNinjaBuildDir(ninjaValue) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | func (s *singletonContext) VisitAllModules(visit func(Module)) { |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 168 | s.context.VisitAllModules(visit) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | func (s *singletonContext) VisitAllModulesIf(pred func(Module) bool, |
| 172 | visit func(Module)) { |
| 173 | |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 174 | s.context.VisitAllModulesIf(pred, visit) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 175 | } |
Jamie Gennis | e98b8a9 | 2014-06-17 10:24:24 -0700 | [diff] [blame] | 176 | |
| 177 | func (s *singletonContext) VisitDepsDepthFirst(module Module, |
| 178 | visit func(Module)) { |
| 179 | |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 180 | s.context.VisitDepsDepthFirst(module, visit) |
Jamie Gennis | e98b8a9 | 2014-06-17 10:24:24 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | func (s *singletonContext) VisitDepsDepthFirstIf(module Module, |
| 184 | pred func(Module) bool, visit func(Module)) { |
| 185 | |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 186 | s.context.VisitDepsDepthFirstIf(module, pred, visit) |
Jamie Gennis | e98b8a9 | 2014-06-17 10:24:24 -0700 | [diff] [blame] | 187 | } |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 188 | |
Colin Cross | 24ad587 | 2015-11-17 16:22:29 -0800 | [diff] [blame] | 189 | func (s *singletonContext) PrimaryModule(module Module) Module { |
| 190 | return s.context.PrimaryModule(module) |
| 191 | } |
| 192 | |
| 193 | func (s *singletonContext) FinalModule(module Module) Module { |
| 194 | return s.context.FinalModule(module) |
| 195 | } |
| 196 | |
| 197 | func (s *singletonContext) VisitAllModuleVariants(module Module, visit func(Module)) { |
| 198 | s.context.VisitAllModuleVariants(module, visit) |
| 199 | } |
| 200 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 201 | func (s *singletonContext) AddNinjaFileDeps(deps ...string) { |
| 202 | s.ninjaFileDeps = append(s.ninjaFileDeps, deps...) |
| 203 | } |