blob: bbfce00a5910c99d8c8c7f805eb22b7ac8cc0446 [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
Dan Willemsenb6c90232018-02-23 14:49:45 -080068 // GlobWithDeps returns a list of files and directories that match the
69 // specified pattern but do not match any of the patterns in excludes.
70 // Any directories will have a '/' suffix. It also adds efficient
71 // dependencies to rerun the primary builder whenever a file matching
72 // the pattern as added or removed, without rerunning if a file that
73 // does not match the pattern is added to a searched directory.
Colin Cross127d2ea2016-11-01 11:10:51 -070074 GlobWithDeps(pattern string, excludes []string) ([]string, error)
Colin Crossb519a7e2017-02-01 13:21:35 -080075
76 Fs() pathtools.FileSystem
Jamie Gennis1bc967e2014-05-27 16:34:41 -070077}
78
79var _ SingletonContext = (*singletonContext)(nil)
80
81type singletonContext struct {
82 context *Context
Jamie Gennis6eb4d242014-06-11 18:31:16 -070083 config interface{}
Jamie Gennis1bc967e2014-05-27 16:34:41 -070084 scope *localScope
Dan Willemsen4bb62762016-01-14 15:42:54 -080085 globals *liveTracker
Jamie Gennis1bc967e2014-05-27 16:34:41 -070086
Mathias Agopian5b8477d2014-06-25 17:21:54 -070087 ninjaFileDeps []string
88 errs []error
Jamie Gennis1bc967e2014-05-27 16:34:41 -070089
90 actionDefs localBuildActions
91}
92
Jamie Gennis6eb4d242014-06-11 18:31:16 -070093func (s *singletonContext) Config() interface{} {
Jamie Gennis1bc967e2014-05-27 16:34:41 -070094 return s.config
95}
96
Colin Crossbbfa51a2014-12-17 16:12:41 -080097func (s *singletonContext) ModuleName(logicModule Module) string {
Colin Cross4572edd2015-05-13 14:36:24 -070098 return s.context.ModuleName(logicModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -070099}
100
Colin Crossbbfa51a2014-12-17 16:12:41 -0800101func (s *singletonContext) ModuleDir(logicModule Module) string {
Colin Cross4572edd2015-05-13 14:36:24 -0700102 return s.context.ModuleDir(logicModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700103}
104
Colin Cross8c602f72015-12-17 18:02:11 -0800105func (s *singletonContext) ModuleSubDir(logicModule Module) string {
106 return s.context.ModuleSubDir(logicModule)
107}
108
Dan Willemsenc98e55b2016-07-25 15:51:50 -0700109func (s *singletonContext) ModuleType(logicModule Module) string {
110 return s.context.ModuleType(logicModule)
111}
112
Colin Crossbbfa51a2014-12-17 16:12:41 -0800113func (s *singletonContext) BlueprintFile(logicModule Module) string {
Colin Cross4572edd2015-05-13 14:36:24 -0700114 return s.context.BlueprintFile(logicModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700115}
116
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800117func (s *singletonContext) error(err error) {
118 if err != nil {
119 s.errs = append(s.errs, err)
120 }
121}
122
Colin Crossbbfa51a2014-12-17 16:12:41 -0800123func (s *singletonContext) ModuleErrorf(logicModule Module, format string,
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700124 args ...interface{}) {
125
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800126 s.error(s.context.ModuleErrorf(logicModule, format, args...))
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700127}
128
129func (s *singletonContext) Errorf(format string, args ...interface{}) {
130 // TODO: Make this not result in the error being printed as "internal error"
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800131 s.error(fmt.Errorf(format, args...))
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700132}
133
Dan Willemsen265d92c2015-12-09 18:06:23 -0800134func (s *singletonContext) Failed() bool {
135 return len(s.errs) > 0
136}
137
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800138func (s *singletonContext) Variable(pctx PackageContext, name, value string) {
Jamie Gennis2fb20952014-10-03 02:49:58 -0700139 s.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700140
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700141 v, err := s.scope.AddLocalVariable(name, value)
142 if err != nil {
143 panic(err)
144 }
145
146 s.actionDefs.variables = append(s.actionDefs.variables, v)
147}
148
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800149func (s *singletonContext) Rule(pctx PackageContext, name string,
Jamie Gennis2fb20952014-10-03 02:49:58 -0700150 params RuleParams, argNames ...string) Rule {
Jamie Genniscbc6f862014-06-05 20:00:22 -0700151
Jamie Gennis2fb20952014-10-03 02:49:58 -0700152 s.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700153
Jamie Genniscbc6f862014-06-05 20:00:22 -0700154 r, err := s.scope.AddLocalRule(name, &params, argNames...)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700155 if err != nil {
156 panic(err)
157 }
158
159 s.actionDefs.rules = append(s.actionDefs.rules, r)
160
161 return r
162}
163
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800164func (s *singletonContext) Build(pctx PackageContext, params BuildParams) {
Jamie Gennis2fb20952014-10-03 02:49:58 -0700165 s.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700166
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700167 def, err := parseBuildParams(s.scope, &params)
168 if err != nil {
169 panic(err)
170 }
171
172 s.actionDefs.buildDefs = append(s.actionDefs.buildDefs, def)
173}
174
Dan Willemsen4bb62762016-01-14 15:42:54 -0800175func (s *singletonContext) Eval(pctx PackageContext, str string) (string, error) {
176 s.scope.ReparentTo(pctx)
177
178 ninjaStr, err := parseNinjaString(s.scope, str)
179 if err != nil {
180 return "", err
181 }
182
183 err = s.globals.addNinjaStringDeps(ninjaStr)
184 if err != nil {
185 return "", err
186 }
187
188 return ninjaStr.Eval(s.globals.variables)
189}
190
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700191func (s *singletonContext) RequireNinjaVersion(major, minor, micro int) {
192 s.context.requireNinjaVersion(major, minor, micro)
193}
194
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800195func (s *singletonContext) SetNinjaBuildDir(pctx PackageContext, value string) {
Jamie Gennis2fb20952014-10-03 02:49:58 -0700196 s.scope.ReparentTo(pctx)
Jamie Gennis7d5b2f82014-09-24 17:51:52 -0700197
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700198 ninjaValue, err := parseNinjaString(s.scope, value)
199 if err != nil {
200 panic(err)
201 }
202
Colin Crossa2599452015-11-18 16:01:01 -0800203 s.context.setNinjaBuildDir(ninjaValue)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700204}
205
206func (s *singletonContext) VisitAllModules(visit func(Module)) {
Colin Cross4572edd2015-05-13 14:36:24 -0700207 s.context.VisitAllModules(visit)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700208}
209
210func (s *singletonContext) VisitAllModulesIf(pred func(Module) bool,
211 visit func(Module)) {
212
Colin Cross4572edd2015-05-13 14:36:24 -0700213 s.context.VisitAllModulesIf(pred, visit)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700214}
Jamie Gennise98b8a92014-06-17 10:24:24 -0700215
216func (s *singletonContext) VisitDepsDepthFirst(module Module,
217 visit func(Module)) {
218
Colin Cross4572edd2015-05-13 14:36:24 -0700219 s.context.VisitDepsDepthFirst(module, visit)
Jamie Gennise98b8a92014-06-17 10:24:24 -0700220}
221
222func (s *singletonContext) VisitDepsDepthFirstIf(module Module,
223 pred func(Module) bool, visit func(Module)) {
224
Colin Cross4572edd2015-05-13 14:36:24 -0700225 s.context.VisitDepsDepthFirstIf(module, pred, visit)
Jamie Gennise98b8a92014-06-17 10:24:24 -0700226}
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700227
Colin Cross24ad5872015-11-17 16:22:29 -0800228func (s *singletonContext) PrimaryModule(module Module) Module {
229 return s.context.PrimaryModule(module)
230}
231
232func (s *singletonContext) FinalModule(module Module) Module {
233 return s.context.FinalModule(module)
234}
235
236func (s *singletonContext) VisitAllModuleVariants(module Module, visit func(Module)) {
237 s.context.VisitAllModuleVariants(module, visit)
238}
239
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700240func (s *singletonContext) AddNinjaFileDeps(deps ...string) {
241 s.ninjaFileDeps = append(s.ninjaFileDeps, deps...)
242}
Colin Cross127d2ea2016-11-01 11:10:51 -0700243
244func (s *singletonContext) GlobWithDeps(pattern string,
245 excludes []string) ([]string, error) {
246 return s.context.glob(pattern, excludes)
247}
Colin Crossb519a7e2017-02-01 13:21:35 -0800248
249func (s *singletonContext) Fs() pathtools.FileSystem {
250 return s.context.fs
251}