blob: 28fecf0fd3e1f67067b410b4a5dc5696bf6a0974 [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"
Colin Crossc5816202017-12-11 14:39:10 -080019
Colin Crossb519a7e2017-02-01 13:21:35 -080020 "github.com/google/blueprint/pathtools"
Jamie Gennis1bc967e2014-05-27 16:34:41 -070021)
22
23type Singleton interface {
24 GenerateBuildActions(SingletonContext)
25}
26
27type SingletonContext interface {
Jamie Gennis6eb4d242014-06-11 18:31:16 -070028 Config() interface{}
Jamie Gennis1bc967e2014-05-27 16:34:41 -070029
30 ModuleName(module Module) string
31 ModuleDir(module Module) string
Colin Cross8c602f72015-12-17 18:02:11 -080032 ModuleSubDir(module Module) string
Dan Willemsenc98e55b2016-07-25 15:51:50 -070033 ModuleType(module Module) string
Jamie Gennis1bc967e2014-05-27 16:34:41 -070034 BlueprintFile(module Module) string
35
36 ModuleErrorf(module Module, format string, args ...interface{})
37 Errorf(format string, args ...interface{})
Dan Willemsen265d92c2015-12-09 18:06:23 -080038 Failed() bool
Jamie Gennis1bc967e2014-05-27 16:34:41 -070039
Dan Willemsenaeffbf72015-11-25 15:29:32 -080040 Variable(pctx PackageContext, name, value string)
41 Rule(pctx PackageContext, name string, params RuleParams, argNames ...string) Rule
42 Build(pctx PackageContext, params BuildParams)
Jamie Gennis1bc967e2014-05-27 16:34:41 -070043 RequireNinjaVersion(major, minor, micro int)
44
Colin Crossa2599452015-11-18 16:01:01 -080045 // SetNinjaBuildDir sets the value of the top-level "builddir" Ninja variable
Jamie Gennis1bc967e2014-05-27 16:34:41 -070046 // that controls where Ninja stores its build log files. This value can be
Colin Crossa2599452015-11-18 16:01:01 -080047 // set at most one time for a single build, later calls are ignored.
Dan Willemsenaeffbf72015-11-25 15:29:32 -080048 SetNinjaBuildDir(pctx PackageContext, value string)
Jamie Gennis1bc967e2014-05-27 16:34:41 -070049
Dan Willemsen4bb62762016-01-14 15:42:54 -080050 // Eval takes a string with embedded ninja variables, and returns a string
51 // with all of the variables recursively expanded. Any variables references
52 // are expanded in the scope of the PackageContext.
53 Eval(pctx PackageContext, ninjaStr string) (string, error)
54
Jamie Gennis1bc967e2014-05-27 16:34:41 -070055 VisitAllModules(visit func(Module))
56 VisitAllModulesIf(pred func(Module) bool, visit func(Module))
Jamie Gennise98b8a92014-06-17 10:24:24 -070057 VisitDepsDepthFirst(module Module, visit func(Module))
58 VisitDepsDepthFirstIf(module Module, pred func(Module) bool,
59 visit func(Module))
Mathias Agopian5b8477d2014-06-25 17:21:54 -070060
Colin Cross24ad5872015-11-17 16:22:29 -080061 VisitAllModuleVariants(module Module, visit func(Module))
62
63 PrimaryModule(module Module) Module
64 FinalModule(module Module) Module
65
Mathias Agopian5b8477d2014-06-25 17:21:54 -070066 AddNinjaFileDeps(deps ...string)
Colin Cross127d2ea2016-11-01 11:10:51 -070067
68 // GlobWithDeps returns a list of files that match the specified pattern but do not match any
69 // of the patterns in excludes. It also adds efficient dependencies to rerun the primary
70 // builder whenever a file matching the pattern as added or removed, without rerunning if a
71 // file that does not match the pattern is added to a searched directory.
72 GlobWithDeps(pattern string, excludes []string) ([]string, error)
Colin Crossb519a7e2017-02-01 13:21:35 -080073
74 Fs() pathtools.FileSystem
Jamie Gennis1bc967e2014-05-27 16:34:41 -070075}
76
77var _ SingletonContext = (*singletonContext)(nil)
78
79type singletonContext struct {
80 context *Context
Jamie Gennis6eb4d242014-06-11 18:31:16 -070081 config interface{}
Jamie Gennis1bc967e2014-05-27 16:34:41 -070082 scope *localScope
Dan Willemsen4bb62762016-01-14 15:42:54 -080083 globals *liveTracker
Jamie Gennis1bc967e2014-05-27 16:34:41 -070084
Mathias Agopian5b8477d2014-06-25 17:21:54 -070085 ninjaFileDeps []string
86 errs []error
Jamie Gennis1bc967e2014-05-27 16:34:41 -070087
88 actionDefs localBuildActions
89}
90
Jamie Gennis6eb4d242014-06-11 18:31:16 -070091func (s *singletonContext) Config() interface{} {
Jamie Gennis1bc967e2014-05-27 16:34:41 -070092 return s.config
93}
94
Colin Crossbbfa51a2014-12-17 16:12:41 -080095func (s *singletonContext) ModuleName(logicModule Module) string {
Colin Cross4572edd2015-05-13 14:36:24 -070096 return s.context.ModuleName(logicModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -070097}
98
Colin Crossbbfa51a2014-12-17 16:12:41 -080099func (s *singletonContext) ModuleDir(logicModule Module) string {
Colin Cross4572edd2015-05-13 14:36:24 -0700100 return s.context.ModuleDir(logicModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700101}
102
Colin Cross8c602f72015-12-17 18:02:11 -0800103func (s *singletonContext) ModuleSubDir(logicModule Module) string {
104 return s.context.ModuleSubDir(logicModule)
105}
106
Dan Willemsenc98e55b2016-07-25 15:51:50 -0700107func (s *singletonContext) ModuleType(logicModule Module) string {
108 return s.context.ModuleType(logicModule)
109}
110
Colin Crossbbfa51a2014-12-17 16:12:41 -0800111func (s *singletonContext) BlueprintFile(logicModule Module) string {
Colin Cross4572edd2015-05-13 14:36:24 -0700112 return s.context.BlueprintFile(logicModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700113}
114
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800115func (s *singletonContext) error(err error) {
116 if err != nil {
117 s.errs = append(s.errs, err)
118 }
119}
120
Colin Crossbbfa51a2014-12-17 16:12:41 -0800121func (s *singletonContext) ModuleErrorf(logicModule Module, format string,
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700122 args ...interface{}) {
123
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800124 s.error(s.context.ModuleErrorf(logicModule, format, args...))
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700125}
126
127func (s *singletonContext) Errorf(format string, args ...interface{}) {
128 // TODO: Make this not result in the error being printed as "internal error"
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800129 s.error(fmt.Errorf(format, args...))
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700130}
131
Dan Willemsen265d92c2015-12-09 18:06:23 -0800132func (s *singletonContext) Failed() bool {
133 return len(s.errs) > 0
134}
135
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800136func (s *singletonContext) Variable(pctx PackageContext, name, value string) {
Jamie Gennis2fb20952014-10-03 02:49:58 -0700137 s.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700138
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700139 v, err := s.scope.AddLocalVariable(name, value)
140 if err != nil {
141 panic(err)
142 }
143
144 s.actionDefs.variables = append(s.actionDefs.variables, v)
145}
146
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800147func (s *singletonContext) Rule(pctx PackageContext, name string,
Jamie Gennis2fb20952014-10-03 02:49:58 -0700148 params RuleParams, argNames ...string) Rule {
Jamie Genniscbc6f862014-06-05 20:00:22 -0700149
Jamie Gennis2fb20952014-10-03 02:49:58 -0700150 s.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700151
Jamie Genniscbc6f862014-06-05 20:00:22 -0700152 r, err := s.scope.AddLocalRule(name, &params, argNames...)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700153 if err != nil {
154 panic(err)
155 }
156
157 s.actionDefs.rules = append(s.actionDefs.rules, r)
158
159 return r
160}
161
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800162func (s *singletonContext) Build(pctx PackageContext, params BuildParams) {
Jamie Gennis2fb20952014-10-03 02:49:58 -0700163 s.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700164
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700165 def, err := parseBuildParams(s.scope, &params)
166 if err != nil {
167 panic(err)
168 }
169
170 s.actionDefs.buildDefs = append(s.actionDefs.buildDefs, def)
171}
172
Dan Willemsen4bb62762016-01-14 15:42:54 -0800173func (s *singletonContext) Eval(pctx PackageContext, str string) (string, error) {
174 s.scope.ReparentTo(pctx)
175
176 ninjaStr, err := parseNinjaString(s.scope, str)
177 if err != nil {
178 return "", err
179 }
180
181 err = s.globals.addNinjaStringDeps(ninjaStr)
182 if err != nil {
183 return "", err
184 }
185
186 return ninjaStr.Eval(s.globals.variables)
187}
188
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700189func (s *singletonContext) RequireNinjaVersion(major, minor, micro int) {
190 s.context.requireNinjaVersion(major, minor, micro)
191}
192
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800193func (s *singletonContext) SetNinjaBuildDir(pctx PackageContext, value string) {
Jamie Gennis2fb20952014-10-03 02:49:58 -0700194 s.scope.ReparentTo(pctx)
Jamie Gennis7d5b2f82014-09-24 17:51:52 -0700195
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700196 ninjaValue, err := parseNinjaString(s.scope, value)
197 if err != nil {
198 panic(err)
199 }
200
Colin Crossa2599452015-11-18 16:01:01 -0800201 s.context.setNinjaBuildDir(ninjaValue)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700202}
203
204func (s *singletonContext) VisitAllModules(visit func(Module)) {
Colin Cross4572edd2015-05-13 14:36:24 -0700205 s.context.VisitAllModules(visit)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700206}
207
208func (s *singletonContext) VisitAllModulesIf(pred func(Module) bool,
209 visit func(Module)) {
210
Colin Cross4572edd2015-05-13 14:36:24 -0700211 s.context.VisitAllModulesIf(pred, visit)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700212}
Jamie Gennise98b8a92014-06-17 10:24:24 -0700213
214func (s *singletonContext) VisitDepsDepthFirst(module Module,
215 visit func(Module)) {
216
Colin Cross4572edd2015-05-13 14:36:24 -0700217 s.context.VisitDepsDepthFirst(module, visit)
Jamie Gennise98b8a92014-06-17 10:24:24 -0700218}
219
220func (s *singletonContext) VisitDepsDepthFirstIf(module Module,
221 pred func(Module) bool, visit func(Module)) {
222
Colin Cross4572edd2015-05-13 14:36:24 -0700223 s.context.VisitDepsDepthFirstIf(module, pred, visit)
Jamie Gennise98b8a92014-06-17 10:24:24 -0700224}
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700225
Colin Cross24ad5872015-11-17 16:22:29 -0800226func (s *singletonContext) PrimaryModule(module Module) Module {
227 return s.context.PrimaryModule(module)
228}
229
230func (s *singletonContext) FinalModule(module Module) Module {
231 return s.context.FinalModule(module)
232}
233
234func (s *singletonContext) VisitAllModuleVariants(module Module, visit func(Module)) {
235 s.context.VisitAllModuleVariants(module, visit)
236}
237
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700238func (s *singletonContext) AddNinjaFileDeps(deps ...string) {
239 s.ninjaFileDeps = append(s.ninjaFileDeps, deps...)
240}
Colin Cross127d2ea2016-11-01 11:10:51 -0700241
242func (s *singletonContext) GlobWithDeps(pattern string,
243 excludes []string) ([]string, error) {
244 return s.context.glob(pattern, excludes)
245}
Colin Crossb519a7e2017-02-01 13:21:35 -0800246
247func (s *singletonContext) Fs() pathtools.FileSystem {
248 return s.context.fs
249}