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