blob: 333811d6e4050be018dfd337b04ce74ed73e414b [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 Willemsenab223a52018-07-05 21:56:59 -070050 // AddSubninja adds a ninja file to include with subninja. This should likely
51 // only ever be used inside bootstrap to handle glob rules.
52 AddSubninja(file string)
53
Dan Willemsen4bb62762016-01-14 15:42:54 -080054 // Eval takes a string with embedded ninja variables, and returns a string
55 // with all of the variables recursively expanded. Any variables references
56 // are expanded in the scope of the PackageContext.
57 Eval(pctx PackageContext, ninjaStr string) (string, error)
58
Jamie Gennis1bc967e2014-05-27 16:34:41 -070059 VisitAllModules(visit func(Module))
60 VisitAllModulesIf(pred func(Module) bool, visit func(Module))
Jamie Gennise98b8a92014-06-17 10:24:24 -070061 VisitDepsDepthFirst(module Module, visit func(Module))
62 VisitDepsDepthFirstIf(module Module, pred func(Module) bool,
63 visit func(Module))
Mathias Agopian5b8477d2014-06-25 17:21:54 -070064
Colin Cross24ad5872015-11-17 16:22:29 -080065 VisitAllModuleVariants(module Module, visit func(Module))
66
67 PrimaryModule(module Module) Module
68 FinalModule(module Module) Module
69
Mathias Agopian5b8477d2014-06-25 17:21:54 -070070 AddNinjaFileDeps(deps ...string)
Colin Cross127d2ea2016-11-01 11:10:51 -070071
Dan Willemsenb6c90232018-02-23 14:49:45 -080072 // GlobWithDeps returns a list of files and directories that match the
73 // specified pattern but do not match any of the patterns in excludes.
74 // Any directories will have a '/' suffix. It also adds efficient
75 // dependencies to rerun the primary builder whenever a file matching
76 // the pattern as added or removed, without rerunning if a file that
77 // does not match the pattern is added to a searched directory.
Colin Cross127d2ea2016-11-01 11:10:51 -070078 GlobWithDeps(pattern string, excludes []string) ([]string, error)
Colin Crossb519a7e2017-02-01 13:21:35 -080079
80 Fs() pathtools.FileSystem
Jamie Gennis1bc967e2014-05-27 16:34:41 -070081}
82
83var _ SingletonContext = (*singletonContext)(nil)
84
85type singletonContext struct {
86 context *Context
Jamie Gennis6eb4d242014-06-11 18:31:16 -070087 config interface{}
Jamie Gennis1bc967e2014-05-27 16:34:41 -070088 scope *localScope
Dan Willemsen4bb62762016-01-14 15:42:54 -080089 globals *liveTracker
Jamie Gennis1bc967e2014-05-27 16:34:41 -070090
Mathias Agopian5b8477d2014-06-25 17:21:54 -070091 ninjaFileDeps []string
92 errs []error
Jamie Gennis1bc967e2014-05-27 16:34:41 -070093
94 actionDefs localBuildActions
95}
96
Jamie Gennis6eb4d242014-06-11 18:31:16 -070097func (s *singletonContext) Config() interface{} {
Jamie Gennis1bc967e2014-05-27 16:34:41 -070098 return s.config
99}
100
Colin Crossbbfa51a2014-12-17 16:12:41 -0800101func (s *singletonContext) ModuleName(logicModule Module) string {
Colin Cross4572edd2015-05-13 14:36:24 -0700102 return s.context.ModuleName(logicModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700103}
104
Colin Crossbbfa51a2014-12-17 16:12:41 -0800105func (s *singletonContext) ModuleDir(logicModule Module) string {
Colin Cross4572edd2015-05-13 14:36:24 -0700106 return s.context.ModuleDir(logicModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700107}
108
Colin Cross8c602f72015-12-17 18:02:11 -0800109func (s *singletonContext) ModuleSubDir(logicModule Module) string {
110 return s.context.ModuleSubDir(logicModule)
111}
112
Dan Willemsenc98e55b2016-07-25 15:51:50 -0700113func (s *singletonContext) ModuleType(logicModule Module) string {
114 return s.context.ModuleType(logicModule)
115}
116
Colin Crossbbfa51a2014-12-17 16:12:41 -0800117func (s *singletonContext) BlueprintFile(logicModule Module) string {
Colin Cross4572edd2015-05-13 14:36:24 -0700118 return s.context.BlueprintFile(logicModule)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700119}
120
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800121func (s *singletonContext) error(err error) {
122 if err != nil {
123 s.errs = append(s.errs, err)
124 }
125}
126
Colin Crossbbfa51a2014-12-17 16:12:41 -0800127func (s *singletonContext) ModuleErrorf(logicModule Module, format string,
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700128 args ...interface{}) {
129
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800130 s.error(s.context.ModuleErrorf(logicModule, format, args...))
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700131}
132
133func (s *singletonContext) Errorf(format string, args ...interface{}) {
134 // TODO: Make this not result in the error being printed as "internal error"
Colin Cross0aa6a5f2016-01-07 13:43:09 -0800135 s.error(fmt.Errorf(format, args...))
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700136}
137
Dan Willemsen265d92c2015-12-09 18:06:23 -0800138func (s *singletonContext) Failed() bool {
139 return len(s.errs) > 0
140}
141
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800142func (s *singletonContext) Variable(pctx PackageContext, name, value string) {
Jamie Gennis2fb20952014-10-03 02:49:58 -0700143 s.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700144
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700145 v, err := s.scope.AddLocalVariable(name, value)
146 if err != nil {
147 panic(err)
148 }
149
150 s.actionDefs.variables = append(s.actionDefs.variables, v)
151}
152
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800153func (s *singletonContext) Rule(pctx PackageContext, name string,
Jamie Gennis2fb20952014-10-03 02:49:58 -0700154 params RuleParams, argNames ...string) Rule {
Jamie Genniscbc6f862014-06-05 20:00:22 -0700155
Jamie Gennis2fb20952014-10-03 02:49:58 -0700156 s.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700157
Jamie Genniscbc6f862014-06-05 20:00:22 -0700158 r, err := s.scope.AddLocalRule(name, &params, argNames...)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700159 if err != nil {
160 panic(err)
161 }
162
163 s.actionDefs.rules = append(s.actionDefs.rules, r)
164
165 return r
166}
167
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800168func (s *singletonContext) Build(pctx PackageContext, params BuildParams) {
Jamie Gennis2fb20952014-10-03 02:49:58 -0700169 s.scope.ReparentTo(pctx)
Jamie Gennis0ed63ef2014-06-30 18:07:17 -0700170
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700171 def, err := parseBuildParams(s.scope, &params)
172 if err != nil {
173 panic(err)
174 }
175
176 s.actionDefs.buildDefs = append(s.actionDefs.buildDefs, def)
177}
178
Dan Willemsen4bb62762016-01-14 15:42:54 -0800179func (s *singletonContext) Eval(pctx PackageContext, str string) (string, error) {
180 s.scope.ReparentTo(pctx)
181
182 ninjaStr, err := parseNinjaString(s.scope, str)
183 if err != nil {
184 return "", err
185 }
186
187 err = s.globals.addNinjaStringDeps(ninjaStr)
188 if err != nil {
189 return "", err
190 }
191
192 return ninjaStr.Eval(s.globals.variables)
193}
194
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700195func (s *singletonContext) RequireNinjaVersion(major, minor, micro int) {
196 s.context.requireNinjaVersion(major, minor, micro)
197}
198
Dan Willemsenaeffbf72015-11-25 15:29:32 -0800199func (s *singletonContext) SetNinjaBuildDir(pctx PackageContext, value string) {
Jamie Gennis2fb20952014-10-03 02:49:58 -0700200 s.scope.ReparentTo(pctx)
Jamie Gennis7d5b2f82014-09-24 17:51:52 -0700201
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700202 ninjaValue, err := parseNinjaString(s.scope, value)
203 if err != nil {
204 panic(err)
205 }
206
Colin Crossa2599452015-11-18 16:01:01 -0800207 s.context.setNinjaBuildDir(ninjaValue)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700208}
209
Dan Willemsenab223a52018-07-05 21:56:59 -0700210func (s *singletonContext) AddSubninja(file string) {
211 s.context.subninjas = append(s.context.subninjas, file)
212}
213
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700214func (s *singletonContext) VisitAllModules(visit func(Module)) {
Colin Cross4572edd2015-05-13 14:36:24 -0700215 s.context.VisitAllModules(visit)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700216}
217
218func (s *singletonContext) VisitAllModulesIf(pred func(Module) bool,
219 visit func(Module)) {
220
Colin Cross4572edd2015-05-13 14:36:24 -0700221 s.context.VisitAllModulesIf(pred, visit)
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700222}
Jamie Gennise98b8a92014-06-17 10:24:24 -0700223
224func (s *singletonContext) VisitDepsDepthFirst(module Module,
225 visit func(Module)) {
226
Colin Cross4572edd2015-05-13 14:36:24 -0700227 s.context.VisitDepsDepthFirst(module, visit)
Jamie Gennise98b8a92014-06-17 10:24:24 -0700228}
229
230func (s *singletonContext) VisitDepsDepthFirstIf(module Module,
231 pred func(Module) bool, visit func(Module)) {
232
Colin Cross4572edd2015-05-13 14:36:24 -0700233 s.context.VisitDepsDepthFirstIf(module, pred, visit)
Jamie Gennise98b8a92014-06-17 10:24:24 -0700234}
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700235
Colin Cross24ad5872015-11-17 16:22:29 -0800236func (s *singletonContext) PrimaryModule(module Module) Module {
237 return s.context.PrimaryModule(module)
238}
239
240func (s *singletonContext) FinalModule(module Module) Module {
241 return s.context.FinalModule(module)
242}
243
244func (s *singletonContext) VisitAllModuleVariants(module Module, visit func(Module)) {
245 s.context.VisitAllModuleVariants(module, visit)
246}
247
Mathias Agopian5b8477d2014-06-25 17:21:54 -0700248func (s *singletonContext) AddNinjaFileDeps(deps ...string) {
249 s.ninjaFileDeps = append(s.ninjaFileDeps, deps...)
250}
Colin Cross127d2ea2016-11-01 11:10:51 -0700251
252func (s *singletonContext) GlobWithDeps(pattern string,
253 excludes []string) ([]string, error) {
254 return s.context.glob(pattern, excludes)
255}
Colin Crossb519a7e2017-02-01 13:21:35 -0800256
257func (s *singletonContext) Fs() pathtools.FileSystem {
258 return s.context.fs
259}