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" |
| 19 | "path/filepath" |
| 20 | ) |
| 21 | |
| 22 | type Singleton interface { |
| 23 | GenerateBuildActions(SingletonContext) |
| 24 | } |
| 25 | |
| 26 | type SingletonContext interface { |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 27 | Config() interface{} |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 28 | |
| 29 | ModuleName(module Module) string |
| 30 | ModuleDir(module Module) string |
| 31 | BlueprintFile(module Module) string |
| 32 | |
| 33 | ModuleErrorf(module Module, format string, args ...interface{}) |
| 34 | Errorf(format string, args ...interface{}) |
| 35 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [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 | |
| 41 | // SetBuildDir sets the value of the top-level "builddir" Ninja variable |
| 42 | // that controls where Ninja stores its build log files. This value can be |
| 43 | // set at most one time for a single build. Setting it multiple times (even |
| 44 | // across different singletons) will result in a panic. |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 45 | SetBuildDir(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 | |
| 53 | AddNinjaFileDeps(deps ...string) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | var _ SingletonContext = (*singletonContext)(nil) |
| 57 | |
| 58 | type singletonContext struct { |
| 59 | context *Context |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 60 | config interface{} |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 61 | scope *localScope |
| 62 | |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 63 | ninjaFileDeps []string |
| 64 | errs []error |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 65 | |
| 66 | actionDefs localBuildActions |
| 67 | } |
| 68 | |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 69 | func (s *singletonContext) Config() interface{} { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 70 | return s.config |
| 71 | } |
| 72 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 73 | func (s *singletonContext) ModuleName(logicModule Module) string { |
| 74 | module := s.context.moduleInfo[logicModule] |
| 75 | return module.group.properties.Name |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 78 | func (s *singletonContext) ModuleDir(logicModule Module) string { |
| 79 | module := s.context.moduleInfo[logicModule] |
| 80 | return filepath.Dir(module.group.relBlueprintsFile) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 83 | func (s *singletonContext) BlueprintFile(logicModule Module) string { |
| 84 | module := s.context.moduleInfo[logicModule] |
| 85 | return module.group.relBlueprintsFile |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 88 | func (s *singletonContext) ModuleErrorf(logicModule Module, format string, |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 89 | args ...interface{}) { |
| 90 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 91 | module := s.context.moduleInfo[logicModule] |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 92 | s.errs = append(s.errs, &Error{ |
| 93 | Err: fmt.Errorf(format, args...), |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 94 | Pos: module.group.pos, |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 95 | }) |
| 96 | } |
| 97 | |
| 98 | func (s *singletonContext) Errorf(format string, args ...interface{}) { |
| 99 | // TODO: Make this not result in the error being printed as "internal error" |
| 100 | s.errs = append(s.errs, fmt.Errorf(format, args...)) |
| 101 | } |
| 102 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 103 | func (s *singletonContext) Variable(pctx *PackageContext, name, value string) { |
| 104 | s.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 105 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 106 | v, err := s.scope.AddLocalVariable(name, value) |
| 107 | if err != nil { |
| 108 | panic(err) |
| 109 | } |
| 110 | |
| 111 | s.actionDefs.variables = append(s.actionDefs.variables, v) |
| 112 | } |
| 113 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 114 | func (s *singletonContext) Rule(pctx *PackageContext, name string, |
| 115 | params RuleParams, argNames ...string) Rule { |
Jamie Gennis | cbc6f86 | 2014-06-05 20:00:22 -0700 | [diff] [blame] | 116 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 117 | s.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 118 | |
Jamie Gennis | cbc6f86 | 2014-06-05 20:00:22 -0700 | [diff] [blame] | 119 | r, err := s.scope.AddLocalRule(name, ¶ms, argNames...) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 120 | if err != nil { |
| 121 | panic(err) |
| 122 | } |
| 123 | |
| 124 | s.actionDefs.rules = append(s.actionDefs.rules, r) |
| 125 | |
| 126 | return r |
| 127 | } |
| 128 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 129 | func (s *singletonContext) Build(pctx *PackageContext, params BuildParams) { |
| 130 | s.scope.ReparentTo(pctx) |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 131 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 132 | def, err := parseBuildParams(s.scope, ¶ms) |
| 133 | if err != nil { |
| 134 | panic(err) |
| 135 | } |
| 136 | |
| 137 | s.actionDefs.buildDefs = append(s.actionDefs.buildDefs, def) |
| 138 | } |
| 139 | |
| 140 | func (s *singletonContext) RequireNinjaVersion(major, minor, micro int) { |
| 141 | s.context.requireNinjaVersion(major, minor, micro) |
| 142 | } |
| 143 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 144 | func (s *singletonContext) SetBuildDir(pctx *PackageContext, value string) { |
| 145 | s.scope.ReparentTo(pctx) |
Jamie Gennis | 7d5b2f8 | 2014-09-24 17:51:52 -0700 | [diff] [blame] | 146 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 147 | ninjaValue, err := parseNinjaString(s.scope, value) |
| 148 | if err != nil { |
| 149 | panic(err) |
| 150 | } |
| 151 | |
| 152 | s.context.setBuildDir(ninjaValue) |
| 153 | } |
| 154 | |
| 155 | func (s *singletonContext) VisitAllModules(visit func(Module)) { |
| 156 | s.context.visitAllModules(visit) |
| 157 | } |
| 158 | |
| 159 | func (s *singletonContext) VisitAllModulesIf(pred func(Module) bool, |
| 160 | visit func(Module)) { |
| 161 | |
| 162 | s.context.visitAllModulesIf(pred, visit) |
| 163 | } |
Jamie Gennis | e98b8a9 | 2014-06-17 10:24:24 -0700 | [diff] [blame] | 164 | |
| 165 | func (s *singletonContext) VisitDepsDepthFirst(module Module, |
| 166 | visit func(Module)) { |
| 167 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 168 | s.context.visitDepsDepthFirst(s.context.moduleInfo[module], visit) |
Jamie Gennis | e98b8a9 | 2014-06-17 10:24:24 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | func (s *singletonContext) VisitDepsDepthFirstIf(module Module, |
| 172 | pred func(Module) bool, visit func(Module)) { |
| 173 | |
Colin Cross | bbfa51a | 2014-12-17 16:12:41 -0800 | [diff] [blame] | 174 | s.context.visitDepsDepthFirstIf(s.context.moduleInfo[module], pred, visit) |
Jamie Gennis | e98b8a9 | 2014-06-17 10:24:24 -0700 | [diff] [blame] | 175 | } |
Mathias Agopian | 5b8477d | 2014-06-25 17:21:54 -0700 | [diff] [blame] | 176 | |
| 177 | func (s *singletonContext) AddNinjaFileDeps(deps ...string) { |
| 178 | s.ninjaFileDeps = append(s.ninjaFileDeps, deps...) |
| 179 | } |