blob: fc4a78107d6d68cc496d1f897a8cd3df8c977e6a [file] [log] [blame]
Colin Cross8e0c5112015-01-23 14:15:10 -08001// 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 Gennis1bc967e2014-05-27 16:34:41 -070015package blueprint
16
17import (
18 "fmt"
Jamie Gennis1bc967e2014-05-27 16:34:41 -070019)
20
21type Singleton interface {
22 GenerateBuildActions(SingletonContext)
23}
24
25type SingletonContext interface {
Jamie Gennis6eb4d242014-06-11 18:31:16 -070026 Config() interface{}
Jamie Gennis1bc967e2014-05-27 16:34:41 -070027
28 ModuleName(module Module) string
29 ModuleDir(module Module) string
Colin Cross8c602f72015-12-17 18:02:11 -080030 ModuleSubDir(module Module) string
Dan Willemsenc98e55b2016-07-25 15:51:50 -070031 ModuleType(module Module) string
Jamie Gennis1bc967e2014-05-27 16:34:41 -070032 BlueprintFile(module Module) string
33
34 ModuleErrorf(module Module, format string, args ...interface{})
35 Errorf(format string, args ...interface{})
Dan Willemsen265d92c2015-12-09 18:06:23 -080036 Failed() bool
Jamie Gennis1bc967e2014-05-27 16:34:41 -070037
Dan Willemsenaeffbf72015-11-25 15:29:32 -080038 Variable(pctx PackageContext, name, value string)
39 Rule(pctx PackageContext, name string, params RuleParams, argNames ...string) Rule
40 Build(pctx PackageContext, params BuildParams)
Jamie Gennis1bc967e2014-05-27 16:34:41 -070041 RequireNinjaVersion(major, minor, micro int)
42
Colin Crossa2599452015-11-18 16:01:01 -080043 // SetNinjaBuildDir sets the value of the top-level "builddir" Ninja variable
Jamie Gennis1bc967e2014-05-27 16:34:41 -070044 // that controls where Ninja stores its build log files. This value can be
Colin Crossa2599452015-11-18 16:01:01 -080045 // set at most one time for a single build, later calls are ignored.
Dan Willemsenaeffbf72015-11-25 15:29:32 -080046 SetNinjaBuildDir(pctx PackageContext, value string)
Jamie Gennis1bc967e2014-05-27 16:34:41 -070047
Dan Willemsen4bb62762016-01-14 15:42:54 -080048 // Eval takes a string with embedded ninja variables, and returns a string
49 // with all of the variables recursively expanded. Any variables references
50 // are expanded in the scope of the PackageContext.
51 Eval(pctx PackageContext, ninjaStr string) (string, error)
52
Jamie Gennis1bc967e2014-05-27 16:34:41 -070053 VisitAllModules(visit func(Module))
54 VisitAllModulesIf(pred func(Module) bool, visit func(Module))
Jamie Gennise98b8a92014-06-17 10:24:24 -070055 VisitDepsDepthFirst(module Module, visit func(Module))
56 VisitDepsDepthFirstIf(module Module, pred func(Module) bool,
57 visit func(Module))
Mathias Agopian5b8477d2014-06-25 17:21:54 -070058
Colin Cross24ad5872015-11-17 16:22:29 -080059 VisitAllModuleVariants(module Module, visit func(Module))
60
61 PrimaryModule(module Module) Module
62 FinalModule(module Module) Module
63
Mathias Agopian5b8477d2014-06-25 17:21:54 -070064 AddNinjaFileDeps(deps ...string)
Colin Cross127d2ea2016-11-01 11:10:51 -070065
66 // GlobWithDeps returns a list of files that match the specified pattern but do not match any
67 // of the patterns in excludes. It also adds efficient dependencies to rerun the primary
68 // builder whenever a file matching the pattern as added or removed, without rerunning if a
69 // file that does not match the pattern is added to a searched directory.
70 GlobWithDeps(pattern string, excludes []string) ([]string, error)
Jamie Gennis1bc967e2014-05-27 16:34:41 -070071}
72
73var _ SingletonContext = (*singletonContext)(nil)
74
75type singletonContext struct {
76 context *Context
Jamie Gennis6eb4d242014-06-11 18:31:16 -070077 config interface{}
Jamie Gennis1bc967e2014-05-27 16:34:41 -070078 scope *localScope
Dan Willemsen4bb62762016-01-14 15:42:54 -080079 globals *liveTracker
Jamie Gennis1bc967e2014-05-27 16:34:41 -070080
Mathias Agopian5b8477d2014-06-25 17:21:54 -070081 ninjaFileDeps []string
82 errs []error
Jamie Gennis1bc967e2014-05-27 16:34:41 -070083
84 actionDefs localBuildActions
85}
86
Jamie Gennis6eb4d242014-06-11 18:31:16 -070087func (s *singletonContext) Config() interface{} {
Jamie Gennis1bc967e2014-05-27 16:34:41 -070088 return s.config
89}
90
Colin Crossbbfa51a2014-12-17 16:12:41 -080091func (s *singletonContext) ModuleName(logicModule Module) string {
Colin Cross4572edd2015-05-13 14:36:24 -070092 return s.context.ModuleName(logicModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -070093}
94
Colin Crossbbfa51a2014-12-17 16:12:41 -080095func (s *singletonContext) ModuleDir(logicModule Module) string {
Colin Cross4572edd2015-05-13 14:36:24 -070096 return s.context.ModuleDir(logicModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -070097}
98
Colin Cross8c602f72015-12-17 18:02:11 -080099func (s *singletonContext) ModuleSubDir(logicModule Module) string {
100 return s.context.ModuleSubDir(logicModule)
101}
102
Dan Willemsenc98e55b2016-07-25 15:51:50 -0700103func (s *singletonContext) ModuleType(logicModule Module) string {
104 return s.context.ModuleType(logicModule)
105}
106
Colin Crossbbfa51a2014-12-17 16:12:41 -0800107func (s *singletonContext) BlueprintFile(logicModule Module) string {
Colin Cross4572edd2015-05-13 14:36:24 -0700108 return s.context.BlueprintFile(logicModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700109}
110
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800111func (s *singletonContext) error(err error) {
112 if err != nil {
113 s.errs = append(s.errs, err)
114 }
115}
116
Colin Crossbbfa51a2014-12-17 16:12:41 -0800117func (s *singletonContext) ModuleErrorf(logicModule Module, format string,
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700118 args ...interface{}) {
119
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800120 s.error(s.context.ModuleErrorf(logicModule, format, args...))
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700121}
122
123func (s *singletonContext) Errorf(format string, args ...interface{}) {
124 // TODO: Make this not result in the error being printed as "internal error"
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800125 s.error(fmt.Errorf(format, args...))
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700126}
127
Dan Willemsen265d92c2015-12-09 18:06:23 -0800128func (s *singletonContext) Failed() bool {
129 return len(s.errs) > 0
130}
131
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800132func (s *singletonContext) Variable(pctx PackageContext, name, value string) {
Jamie Gennis2fb20952014-10-03 02:49:58 -0700133 s.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700134
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700135 v, err := s.scope.AddLocalVariable(name, value)
136 if err != nil {
137 panic(err)
138 }
139
140 s.actionDefs.variables = append(s.actionDefs.variables, v)
141}
142
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800143func (s *singletonContext) Rule(pctx PackageContext, name string,
Jamie Gennis2fb20952014-10-03 02:49:58 -0700144 params RuleParams, argNames ...string) Rule {
Jamie Genniscbc6f862014-06-05 20:00:22 -0700145
Jamie Gennis2fb20952014-10-03 02:49:58 -0700146 s.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700147
Jamie Genniscbc6f862014-06-05 20:00:22 -0700148 r, err := s.scope.AddLocalRule(name, &params, argNames...)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700149 if err != nil {
150 panic(err)
151 }
152
153 s.actionDefs.rules = append(s.actionDefs.rules, r)
154
155 return r
156}
157
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800158func (s *singletonContext) Build(pctx PackageContext, params BuildParams) {
Jamie Gennis2fb20952014-10-03 02:49:58 -0700159 s.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700160
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700161 def, err := parseBuildParams(s.scope, &params)
162 if err != nil {
163 panic(err)
164 }
165
166 s.actionDefs.buildDefs = append(s.actionDefs.buildDefs, def)
167}
168
Dan Willemsen4bb62762016-01-14 15:42:54 -0800169func (s *singletonContext) Eval(pctx PackageContext, str string) (string, error) {
170 s.scope.ReparentTo(pctx)
171
172 ninjaStr, err := parseNinjaString(s.scope, str)
173 if err != nil {
174 return "", err
175 }
176
177 err = s.globals.addNinjaStringDeps(ninjaStr)
178 if err != nil {
179 return "", err
180 }
181
182 return ninjaStr.Eval(s.globals.variables)
183}
184
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700185func (s *singletonContext) RequireNinjaVersion(major, minor, micro int) {
186 s.context.requireNinjaVersion(major, minor, micro)
187}
188
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800189func (s *singletonContext) SetNinjaBuildDir(pctx PackageContext, value string) {
Jamie Gennis2fb20952014-10-03 02:49:58 -0700190 s.scope.ReparentTo(pctx)
Jamie Gennis7d5b2f82014-09-24 17:51:52 -0700191
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700192 ninjaValue, err := parseNinjaString(s.scope, value)
193 if err != nil {
194 panic(err)
195 }
196
Colin Crossa2599452015-11-18 16:01:01 -0800197 s.context.setNinjaBuildDir(ninjaValue)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700198}
199
200func (s *singletonContext) VisitAllModules(visit func(Module)) {
Colin Cross4572edd2015-05-13 14:36:24 -0700201 s.context.VisitAllModules(visit)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700202}
203
204func (s *singletonContext) VisitAllModulesIf(pred func(Module) bool,
205 visit func(Module)) {
206
Colin Cross4572edd2015-05-13 14:36:24 -0700207 s.context.VisitAllModulesIf(pred, visit)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700208}
Jamie Gennise98b8a92014-06-17 10:24:24 -0700209
210func (s *singletonContext) VisitDepsDepthFirst(module Module,
211 visit func(Module)) {
212
Colin Cross4572edd2015-05-13 14:36:24 -0700213 s.context.VisitDepsDepthFirst(module, visit)
Jamie Gennise98b8a92014-06-17 10:24:24 -0700214}
215
216func (s *singletonContext) VisitDepsDepthFirstIf(module Module,
217 pred func(Module) bool, visit func(Module)) {
218
Colin Cross4572edd2015-05-13 14:36:24 -0700219 s.context.VisitDepsDepthFirstIf(module, pred, visit)
Jamie Gennise98b8a92014-06-17 10:24:24 -0700220}
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700221
Colin Cross24ad5872015-11-17 16:22:29 -0800222func (s *singletonContext) PrimaryModule(module Module) Module {
223 return s.context.PrimaryModule(module)
224}
225
226func (s *singletonContext) FinalModule(module Module) Module {
227 return s.context.FinalModule(module)
228}
229
230func (s *singletonContext) VisitAllModuleVariants(module Module, visit func(Module)) {
231 s.context.VisitAllModuleVariants(module, visit)
232}
233
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700234func (s *singletonContext) AddNinjaFileDeps(deps ...string) {
235 s.ninjaFileDeps = append(s.ninjaFileDeps, deps...)
236}
Colin Cross127d2ea2016-11-01 11:10:51 -0700237
238func (s *singletonContext) GlobWithDeps(pattern string,
239 excludes []string) ([]string, error) {
240 return s.context.glob(pattern, excludes)
241}