blob: 468b617b5726428fa6c46f343d272b2f19bb1b62 [file] [log] [blame]
Colin Crossfeec25b2019-01-30 17:32:39 -08001// Copyright 2018 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
15package android
16
17import (
18 "fmt"
19 "path/filepath"
20 "sort"
21 "strings"
22
23 "github.com/google/blueprint"
24 "github.com/google/blueprint/proptools"
25)
26
Colin Cross758290d2019-02-01 16:42:32 -080027// RuleBuilder provides an alternative to ModuleContext.Rule and ModuleContext.Build to add a command line to the build
28// graph.
Colin Crossfeec25b2019-01-30 17:32:39 -080029type RuleBuilder struct {
Colin Cross5cb5b092019-02-02 21:25:18 -080030 commands []*RuleBuilderCommand
31 installs []RuleBuilderInstall
32 temporariesSet map[string]bool
33 restat bool
Colin Cross0d2f40a2019-02-05 22:31:15 -080034 missingDeps []string
Colin Crossfeec25b2019-01-30 17:32:39 -080035}
36
Colin Cross758290d2019-02-01 16:42:32 -080037// NewRuleBuilder returns a newly created RuleBuilder.
38func NewRuleBuilder() *RuleBuilder {
Colin Cross5cb5b092019-02-02 21:25:18 -080039 return &RuleBuilder{
40 temporariesSet: make(map[string]bool),
41 }
Colin Cross758290d2019-02-01 16:42:32 -080042}
43
44// RuleBuilderInstall is a tuple of install from and to locations.
45type RuleBuilderInstall struct {
46 From, To string
47}
48
Colin Cross0d2f40a2019-02-05 22:31:15 -080049// MissingDeps adds modules to the list of missing dependencies. If MissingDeps
50// is called with a non-empty input, any call to Build will result in a rule
51// that will print an error listing the missing dependencies and fail.
52// MissingDeps should only be called if Config.AllowMissingDependencies() is
53// true.
54func (r *RuleBuilder) MissingDeps(missingDeps []string) {
55 r.missingDeps = append(r.missingDeps, missingDeps...)
56}
57
Colin Cross758290d2019-02-01 16:42:32 -080058// Restat marks the rule as a restat rule, which will be passed to ModuleContext.Rule in BuildParams.Restat.
Colin Crossfeec25b2019-01-30 17:32:39 -080059func (r *RuleBuilder) Restat() *RuleBuilder {
60 r.restat = true
61 return r
62}
63
Colin Cross758290d2019-02-01 16:42:32 -080064// Install associates an output of the rule with an install location, which can be retrieved later using
65// RuleBuilder.Installs.
Colin Crossfeec25b2019-01-30 17:32:39 -080066func (r *RuleBuilder) Install(from, to string) {
67 r.installs = append(r.installs, RuleBuilderInstall{from, to})
68}
69
Colin Cross758290d2019-02-01 16:42:32 -080070// Command returns a new RuleBuilderCommand for the rule. The commands will be ordered in the rule by when they were
71// created by this method. That can be mutated through their methods in any order, as long as the mutations do not
72// race with any call to Build.
Colin Crossfeec25b2019-01-30 17:32:39 -080073func (r *RuleBuilder) Command() *RuleBuilderCommand {
74 command := &RuleBuilderCommand{}
75 r.commands = append(r.commands, command)
76 return command
77}
78
Colin Cross5cb5b092019-02-02 21:25:18 -080079// Temporary marks an output of a command as an intermediate file that will be used as an input to another command
80// in the same rule, and should not be listed in Outputs.
81func (r *RuleBuilder) Temporary(path string) {
82 r.temporariesSet[path] = true
83}
84
85// DeleteTemporaryFiles adds a command to the rule that deletes any outputs that have been marked using Temporary
86// when the rule runs. DeleteTemporaryFiles should be called after all calls to Temporary.
87func (r *RuleBuilder) DeleteTemporaryFiles() {
88 var temporariesList []string
89
90 for intermediate := range r.temporariesSet {
91 temporariesList = append(temporariesList, intermediate)
92 }
93 sort.Strings(temporariesList)
94
95 r.Command().Text("rm").Flag("-f").Outputs(temporariesList)
96}
97
Colin Cross758290d2019-02-01 16:42:32 -080098// Inputs returns the list of paths that were passed to the RuleBuilderCommand methods that take input paths, such
99// as RuleBuilderCommand.Input, RuleBuilderComand.Implicit, or RuleBuilderCommand.FlagWithInput. Inputs to a command
100// that are also outputs of another command in the same RuleBuilder are filtered out.
Colin Crossfeec25b2019-01-30 17:32:39 -0800101func (r *RuleBuilder) Inputs() []string {
102 outputs := r.outputSet()
103
104 inputs := make(map[string]bool)
105 for _, c := range r.commands {
106 for _, input := range c.inputs {
107 if !outputs[input] {
108 inputs[input] = true
109 }
110 }
111 }
112
113 var inputList []string
114 for input := range inputs {
115 inputList = append(inputList, input)
116 }
117 sort.Strings(inputList)
118
119 return inputList
120}
121
122func (r *RuleBuilder) outputSet() map[string]bool {
123 outputs := make(map[string]bool)
124 for _, c := range r.commands {
125 for _, output := range c.outputs {
126 outputs[output] = true
127 }
128 }
129 return outputs
130}
131
Colin Cross758290d2019-02-01 16:42:32 -0800132// Outputs returns the list of paths that were passed to the RuleBuilderCommand methods that take output paths, such
133// as RuleBuilderCommand.Output, RuleBuilderCommand.ImplicitOutput, or RuleBuilderCommand.FlagWithInput.
Colin Crossfeec25b2019-01-30 17:32:39 -0800134func (r *RuleBuilder) Outputs() []string {
135 outputs := r.outputSet()
136
137 var outputList []string
138 for output := range outputs {
Colin Cross5cb5b092019-02-02 21:25:18 -0800139 if !r.temporariesSet[output] {
140 outputList = append(outputList, output)
141 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800142 }
143 sort.Strings(outputList)
144 return outputList
145}
146
Colin Cross758290d2019-02-01 16:42:32 -0800147// Installs returns the list of tuples passed to Install.
Colin Crossfeec25b2019-01-30 17:32:39 -0800148func (r *RuleBuilder) Installs() []RuleBuilderInstall {
149 return append([]RuleBuilderInstall(nil), r.installs...)
150}
151
Colin Cross5cb5b092019-02-02 21:25:18 -0800152func (r *RuleBuilder) toolsSet() map[string]bool {
153 tools := make(map[string]bool)
154 for _, c := range r.commands {
155 for _, tool := range c.tools {
156 tools[tool] = true
157 }
158 }
159
160 return tools
161}
162
Colin Cross758290d2019-02-01 16:42:32 -0800163// Tools returns the list of paths that were passed to the RuleBuilderCommand.Tool method.
Colin Crossfeec25b2019-01-30 17:32:39 -0800164func (r *RuleBuilder) Tools() []string {
Colin Cross5cb5b092019-02-02 21:25:18 -0800165 toolsSet := r.toolsSet()
166
167 var toolsList []string
168 for tool := range toolsSet {
169 toolsList = append(toolsList, tool)
Colin Crossfeec25b2019-01-30 17:32:39 -0800170 }
Colin Cross5cb5b092019-02-02 21:25:18 -0800171 sort.Strings(toolsList)
172 return toolsList
Colin Crossfeec25b2019-01-30 17:32:39 -0800173}
174
Colin Cross758290d2019-02-01 16:42:32 -0800175// Commands returns a slice containing a the built command line for each call to RuleBuilder.Command.
Colin Crossfeec25b2019-01-30 17:32:39 -0800176func (r *RuleBuilder) Commands() []string {
177 var commands []string
178 for _, c := range r.commands {
179 commands = append(commands, string(c.buf))
180 }
181 return commands
182}
183
Colin Cross758290d2019-02-01 16:42:32 -0800184// BuilderContext is a subset of ModuleContext and SingletonContext.
Colin Cross786cd6d2019-02-01 16:41:11 -0800185type BuilderContext interface {
186 PathContext
187 Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule
188 Build(PackageContext, BuildParams)
189}
190
Colin Cross758290d2019-02-01 16:42:32 -0800191var _ BuilderContext = ModuleContext(nil)
192var _ BuilderContext = SingletonContext(nil)
193
194// Build adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for
195// Outputs.
Colin Cross786cd6d2019-02-01 16:41:11 -0800196func (r *RuleBuilder) Build(pctx PackageContext, ctx BuilderContext, name string, desc string) {
197 // TODO: convert RuleBuilder arguments and storage to Paths
198 mctx, _ := ctx.(ModuleContext)
Colin Crossfeec25b2019-01-30 17:32:39 -0800199 var inputs Paths
200 for _, input := range r.Inputs() {
Colin Cross786cd6d2019-02-01 16:41:11 -0800201 // Module output paths
202 if mctx != nil {
203 rel, isRel := MaybeRel(ctx, PathForModuleOut(mctx).String(), input)
204 if isRel {
205 inputs = append(inputs, PathForModuleOut(mctx, rel))
206 continue
207 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800208 }
Colin Cross786cd6d2019-02-01 16:41:11 -0800209
210 // Other output paths
211 rel, isRel := MaybeRel(ctx, PathForOutput(ctx).String(), input)
212 if isRel {
213 inputs = append(inputs, PathForOutput(ctx, rel))
214 continue
215 }
216
217 // TODO: remove this once boot image is moved to where PathForOutput can find it.
218 inputs = append(inputs, &unknownRulePath{input})
Colin Crossfeec25b2019-01-30 17:32:39 -0800219 }
220
221 var outputs WritablePaths
222 for _, output := range r.Outputs() {
Colin Cross786cd6d2019-02-01 16:41:11 -0800223 if mctx != nil {
224 rel := Rel(ctx, PathForModuleOut(mctx).String(), output)
225 outputs = append(outputs, PathForModuleOut(mctx, rel))
226 } else {
227 rel := Rel(ctx, PathForOutput(ctx).String(), output)
228 outputs = append(outputs, PathForOutput(ctx, rel))
229 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800230 }
231
Colin Cross0d2f40a2019-02-05 22:31:15 -0800232 if len(r.missingDeps) > 0 {
233 ctx.Build(pctx, BuildParams{
234 Rule: ErrorRule,
235 Outputs: outputs,
236 Description: desc,
237 Args: map[string]string{
238 "error": "missing dependencies: " + strings.Join(r.missingDeps, ", "),
239 },
240 })
241 return
242 }
243
Colin Crossfeec25b2019-01-30 17:32:39 -0800244 if len(r.Commands()) > 0 {
245 ctx.Build(pctx, BuildParams{
246 Rule: ctx.Rule(pctx, name, blueprint.RuleParams{
247 Command: strings.Join(proptools.NinjaEscape(r.Commands()), " && "),
248 CommandDeps: r.Tools(),
249 }),
250 Implicits: inputs,
251 Outputs: outputs,
252 Description: desc,
253 })
254 }
255}
256
Colin Cross758290d2019-02-01 16:42:32 -0800257// RuleBuilderCommand is a builder for a command in a command line. It can be mutated by its methods to add to the
258// command and track dependencies. The methods mutate the RuleBuilderCommand in place, as well as return the
259// RuleBuilderCommand, so they can be used chained or unchained. All methods that add text implicitly add a single
260// space as a separator from the previous method.
Colin Crossfeec25b2019-01-30 17:32:39 -0800261type RuleBuilderCommand struct {
262 buf []byte
263 inputs []string
264 outputs []string
265 tools []string
266}
267
Colin Cross758290d2019-02-01 16:42:32 -0800268// Text adds the specified raw text to the command line. The text should not contain input or output paths or the
269// rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800270func (c *RuleBuilderCommand) Text(text string) *RuleBuilderCommand {
271 if len(c.buf) > 0 {
272 c.buf = append(c.buf, ' ')
273 }
274 c.buf = append(c.buf, text...)
275 return c
276}
277
Colin Cross758290d2019-02-01 16:42:32 -0800278// Textf adds the specified formatted text to the command line. The text should not contain input or output paths or
279// the rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800280func (c *RuleBuilderCommand) Textf(format string, a ...interface{}) *RuleBuilderCommand {
281 return c.Text(fmt.Sprintf(format, a...))
282}
283
Colin Cross758290d2019-02-01 16:42:32 -0800284// Flag adds the specified raw text to the command line. The text should not contain input or output paths or the
285// rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800286func (c *RuleBuilderCommand) Flag(flag string) *RuleBuilderCommand {
287 return c.Text(flag)
288}
289
Colin Cross758290d2019-02-01 16:42:32 -0800290// FlagWithArg adds the specified flag and argument text to the command line, with no separator between them. The flag
291// and argument should not contain input or output paths or the rule will not have them listed in its dependencies or
292// outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800293func (c *RuleBuilderCommand) FlagWithArg(flag, arg string) *RuleBuilderCommand {
294 return c.Text(flag + arg)
295}
296
Colin Crossc7ed0042019-02-11 14:11:09 -0800297// FlagForEachArg adds the specified flag joined with each argument to the command line. The result is identical to
298// calling FlagWithArg for argument.
299func (c *RuleBuilderCommand) FlagForEachArg(flag string, args []string) *RuleBuilderCommand {
300 for _, arg := range args {
301 c.FlagWithArg(flag, arg)
302 }
303 return c
304}
305
Colin Cross758290d2019-02-01 16:42:32 -0800306// FlagWithArg adds the specified flag and list of arguments to the command line, with the arguments joined by sep
307// and no separator between the flag and arguments. The flag and arguments should not contain input or output paths or
308// the rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800309func (c *RuleBuilderCommand) FlagWithList(flag string, list []string, sep string) *RuleBuilderCommand {
310 return c.Text(flag + strings.Join(list, sep))
311}
312
Colin Cross758290d2019-02-01 16:42:32 -0800313// Tool adds the specified tool path to the command line. The path will be also added to the dependencies returned by
314// RuleBuilder.Tools.
Colin Crossfeec25b2019-01-30 17:32:39 -0800315func (c *RuleBuilderCommand) Tool(path string) *RuleBuilderCommand {
316 c.tools = append(c.tools, path)
317 return c.Text(path)
318}
319
Colin Cross758290d2019-02-01 16:42:32 -0800320// Input adds the specified input path to the command line. The path will also be added to the dependencies returned by
321// RuleBuilder.Inputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800322func (c *RuleBuilderCommand) Input(path string) *RuleBuilderCommand {
323 c.inputs = append(c.inputs, path)
324 return c.Text(path)
325}
326
Colin Cross758290d2019-02-01 16:42:32 -0800327// Inputs adds the specified input paths to the command line, separated by spaces. The paths will also be added to the
328// dependencies returned by RuleBuilder.Inputs.
329func (c *RuleBuilderCommand) Inputs(paths []string) *RuleBuilderCommand {
330 for _, path := range paths {
331 c.Input(path)
332 }
333 return c
334}
335
336// Implicit adds the specified input path to the dependencies returned by RuleBuilder.Inputs without modifying the
337// command line.
Colin Crossfeec25b2019-01-30 17:32:39 -0800338func (c *RuleBuilderCommand) Implicit(path string) *RuleBuilderCommand {
339 c.inputs = append(c.inputs, path)
340 return c
341}
342
Colin Cross758290d2019-02-01 16:42:32 -0800343// Implicits adds the specified input paths to the dependencies returned by RuleBuilder.Inputs without modifying the
344// command line.
Colin Crossfeec25b2019-01-30 17:32:39 -0800345func (c *RuleBuilderCommand) Implicits(paths []string) *RuleBuilderCommand {
346 c.inputs = append(c.inputs, paths...)
347 return c
348}
349
Colin Cross758290d2019-02-01 16:42:32 -0800350// Output adds the specified output path to the command line. The path will also be added to the outputs returned by
351// RuleBuilder.Outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800352func (c *RuleBuilderCommand) Output(path string) *RuleBuilderCommand {
353 c.outputs = append(c.outputs, path)
354 return c.Text(path)
355}
356
Colin Cross758290d2019-02-01 16:42:32 -0800357// Outputs adds the specified output paths to the command line, separated by spaces. The paths will also be added to
358// the outputs returned by RuleBuilder.Outputs.
359func (c *RuleBuilderCommand) Outputs(paths []string) *RuleBuilderCommand {
360 for _, path := range paths {
361 c.Output(path)
362 }
363 return c
364}
365
366// ImplicitOutput adds the specified output path to the dependencies returned by RuleBuilder.Outputs without modifying
367// the command line.
Colin Crossfeec25b2019-01-30 17:32:39 -0800368func (c *RuleBuilderCommand) ImplicitOutput(path string) *RuleBuilderCommand {
369 c.outputs = append(c.outputs, path)
370 return c
371}
372
Colin Cross758290d2019-02-01 16:42:32 -0800373// ImplicitOutputs adds the specified output paths to the dependencies returned by RuleBuilder.Outputs without modifying
374// the command line.
375func (c *RuleBuilderCommand) ImplicitOutputs(paths []string) *RuleBuilderCommand {
376 c.outputs = append(c.outputs, paths...)
377 return c
378}
379
380// FlagWithInput adds the specified flag and input path to the command line, with no separator between them. The path
381// will also be added to the dependencies returned by RuleBuilder.Inputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800382func (c *RuleBuilderCommand) FlagWithInput(flag, path string) *RuleBuilderCommand {
383 c.inputs = append(c.inputs, path)
384 return c.Text(flag + path)
385}
386
Colin Cross758290d2019-02-01 16:42:32 -0800387// FlagWithInputList adds the specified flag and input paths to the command line, with the inputs joined by sep
388// and no separator between the flag and inputs. The input paths will also be added to the dependencies returned by
389// RuleBuilder.Inputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800390func (c *RuleBuilderCommand) FlagWithInputList(flag string, paths []string, sep string) *RuleBuilderCommand {
391 c.inputs = append(c.inputs, paths...)
392 return c.FlagWithList(flag, paths, sep)
393}
394
Colin Cross758290d2019-02-01 16:42:32 -0800395// FlagForEachInput adds the specified flag joined with each input path to the command line. The input paths will also
396// be added to the dependencies returned by RuleBuilder.Inputs. The result is identical to calling FlagWithInput for
397// each input path.
398func (c *RuleBuilderCommand) FlagForEachInput(flag string, paths []string) *RuleBuilderCommand {
399 for _, path := range paths {
400 c.FlagWithInput(flag, path)
401 }
402 return c
403}
404
405// FlagWithOutput adds the specified flag and output path to the command line, with no separator between them. The path
406// will also be added to the outputs returned by RuleBuilder.Outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800407func (c *RuleBuilderCommand) FlagWithOutput(flag, path string) *RuleBuilderCommand {
408 c.outputs = append(c.outputs, path)
409 return c.Text(flag + path)
410}
411
Colin Cross758290d2019-02-01 16:42:32 -0800412// String returns the command line.
413func (c *RuleBuilderCommand) String() string {
414 return string(c.buf)
415}
416
Colin Crossfeec25b2019-01-30 17:32:39 -0800417type unknownRulePath struct {
418 path string
419}
420
421var _ Path = (*unknownRulePath)(nil)
422
423func (p *unknownRulePath) String() string { return p.path }
424func (p *unknownRulePath) Ext() string { return filepath.Ext(p.path) }
425func (p *unknownRulePath) Base() string { return filepath.Base(p.path) }
426func (p *unknownRulePath) Rel() string { return p.path }