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