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