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