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{}) |
| 34 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 35 | Variable(pctx *PackageContext, name, value string) |
| 36 | Rule(pctx *PackageContext, name string, params RuleParams, argNames ...string) Rule |
| 37 | Build(pctx *PackageContext, params BuildParams) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 38 | RequireNinjaVersion(major, minor, micro int) |
| 39 | |
| 40 | // SetBuildDir sets the value of the top-level "builddir" Ninja variable |
| 41 | // that controls where Ninja stores its build log files. This value can be |
| 42 | // set at most one time for a single build. Setting it multiple times (even |
| 43 | // across different singletons) will result in a panic. |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 44 | SetBuildDir(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 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 100 | func (s *singletonContext) Variable(pctx *PackageContext, name, value string) { |
| 101 | s.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 102 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 103 | v, err := s.scope.AddLocalVariable(name, value) |
| 104 | if err != nil { |
| 105 | panic(err) |
| 106 | } |
| 107 | |
| 108 | s.actionDefs.variables = append(s.actionDefs.variables, v) |
| 109 | } |
| 110 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 111 | func (s *singletonContext) Rule(pctx *PackageContext, name string, |
| 112 | params RuleParams, argNames ...string) Rule { |
Jamie Gennis | cbc6f86 | 2014-06-05 20:00:22 -0700 | [diff] [blame] | 113 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 114 | s.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 115 | |
Jamie Gennis | cbc6f86 | 2014-06-05 20:00:22 -0700 | [diff] [blame] | 116 | r, err := s.scope.AddLocalRule(name, ¶ms, argNames...) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 117 | if err != nil { |
| 118 | panic(err) |
| 119 | } |
| 120 | |
| 121 | s.actionDefs.rules = append(s.actionDefs.rules, r) |
| 122 | |
| 123 | return r |
| 124 | } |
| 125 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 126 | func (s *singletonContext) Build(pctx *PackageContext, params BuildParams) { |
| 127 | s.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 128 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 129 | def, err := parseBuildParams(s.scope, ¶ms) |
| 130 | if err != nil { |
| 131 | panic(err) |
| 132 | } |
| 133 | |
| 134 | s.actionDefs.buildDefs = append(s.actionDefs.buildDefs, def) |
| 135 | } |
| 136 | |
| 137 | func (s *singletonContext) RequireNinjaVersion(major, minor, micro int) { |
| 138 | s.context.requireNinjaVersion(major, minor, micro) |
| 139 | } |
| 140 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 141 | func (s *singletonContext) SetBuildDir(pctx *PackageContext, value string) { |
| 142 | s.scope.ReparentTo(pctx) |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 143 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 144 | ninjaValue, err := parseNinjaString(s.scope, value) |
| 145 | if err != nil { |
| 146 | panic(err) |
| 147 | } |
| 148 | |
| 149 | s.context.setBuildDir(ninjaValue) |
| 150 | } |
| 151 | |
| 152 | func (s *singletonContext) VisitAllModules(visit func(Module)) { |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 153 | s.context.VisitAllModules(visit) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | func (s *singletonContext) VisitAllModulesIf(pred func(Module) bool, |
| 157 | visit func(Module)) { |
| 158 | |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 159 | s.context.VisitAllModulesIf(pred, visit) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 160 | } |
Jamie Gennis | e98b8a9 | 2014-06-17 10:24:24 -0700 | [diff] [blame] | 161 | |
| 162 | func (s *singletonContext) VisitDepsDepthFirst(module Module, |
| 163 | visit func(Module)) { |
| 164 | |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 165 | s.context.VisitDepsDepthFirst(module, visit) |
Jamie Gennis | e98b8a9 | 2014-06-17 10:24:24 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | func (s *singletonContext) VisitDepsDepthFirstIf(module Module, |
| 169 | pred func(Module) bool, visit func(Module)) { |
| 170 | |
Colin Cross | 4572edd | 2015-05-13 14:36:24 -0700 | [diff] [blame] | 171 | s.context.VisitDepsDepthFirstIf(module, pred, visit) |
Jamie Gennis | e98b8a9 | 2014-06-17 10:24:24 -0700 | [diff] [blame] | 172 | } |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 173 | |
Colin Cross | 24ad587 | 2015-11-17 16:22:29 -0800 | [diff] [blame^] | 174 | func (s *singletonContext) PrimaryModule(module Module) Module { |
| 175 | return s.context.PrimaryModule(module) |
| 176 | } |
| 177 | |
| 178 | func (s *singletonContext) FinalModule(module Module) Module { |
| 179 | return s.context.FinalModule(module) |
| 180 | } |
| 181 | |
| 182 | func (s *singletonContext) VisitAllModuleVariants(module Module, visit func(Module)) { |
| 183 | s.context.VisitAllModuleVariants(module, visit) |
| 184 | } |
| 185 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 186 | func (s *singletonContext) AddNinjaFileDeps(deps ...string) { |
| 187 | s.ninjaFileDeps = append(s.ninjaFileDeps, deps...) |
| 188 | } |