blob: 3b869470242d5210ac5ac3496867dc352daa0aff [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
Colin Crossdeabb942019-02-11 14:11:09 -080031 installs RuleBuilderInstalls
Colin Cross5cb5b092019-02-02 21:25:18 -080032 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 Crossdeabb942019-02-11 14:11:09 -080049type RuleBuilderInstalls []RuleBuilderInstall
50
51// String returns the RuleBuilderInstalls in the form used by $(call copy-many-files) in Make, a space separated
52// list of from:to tuples.
53func (installs RuleBuilderInstalls) String() string {
54 sb := strings.Builder{}
55 for i, install := range installs {
56 if i != 0 {
57 sb.WriteRune(' ')
58 }
59 sb.WriteString(install.From)
60 sb.WriteRune(':')
61 sb.WriteString(install.To)
62 }
63 return sb.String()
64}
65
Colin Cross0d2f40a2019-02-05 22:31:15 -080066// MissingDeps adds modules to the list of missing dependencies. If MissingDeps
67// is called with a non-empty input, any call to Build will result in a rule
68// that will print an error listing the missing dependencies and fail.
69// MissingDeps should only be called if Config.AllowMissingDependencies() is
70// true.
71func (r *RuleBuilder) MissingDeps(missingDeps []string) {
72 r.missingDeps = append(r.missingDeps, missingDeps...)
73}
74
Colin Cross758290d2019-02-01 16:42:32 -080075// 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 -080076func (r *RuleBuilder) Restat() *RuleBuilder {
77 r.restat = true
78 return r
79}
80
Colin Cross758290d2019-02-01 16:42:32 -080081// Install associates an output of the rule with an install location, which can be retrieved later using
82// RuleBuilder.Installs.
Colin Crossfeec25b2019-01-30 17:32:39 -080083func (r *RuleBuilder) Install(from, to string) {
84 r.installs = append(r.installs, RuleBuilderInstall{from, to})
85}
86
Colin Cross758290d2019-02-01 16:42:32 -080087// Command returns a new RuleBuilderCommand for the rule. The commands will be ordered in the rule by when they were
88// created by this method. That can be mutated through their methods in any order, as long as the mutations do not
89// race with any call to Build.
Colin Crossfeec25b2019-01-30 17:32:39 -080090func (r *RuleBuilder) Command() *RuleBuilderCommand {
91 command := &RuleBuilderCommand{}
92 r.commands = append(r.commands, command)
93 return command
94}
95
Colin Cross5cb5b092019-02-02 21:25:18 -080096// Temporary marks an output of a command as an intermediate file that will be used as an input to another command
97// in the same rule, and should not be listed in Outputs.
98func (r *RuleBuilder) Temporary(path string) {
99 r.temporariesSet[path] = true
100}
101
102// DeleteTemporaryFiles adds a command to the rule that deletes any outputs that have been marked using Temporary
103// when the rule runs. DeleteTemporaryFiles should be called after all calls to Temporary.
104func (r *RuleBuilder) DeleteTemporaryFiles() {
105 var temporariesList []string
106
107 for intermediate := range r.temporariesSet {
108 temporariesList = append(temporariesList, intermediate)
109 }
110 sort.Strings(temporariesList)
111
112 r.Command().Text("rm").Flag("-f").Outputs(temporariesList)
113}
114
Colin Cross758290d2019-02-01 16:42:32 -0800115// Inputs returns the list of paths that were passed to the RuleBuilderCommand methods that take input paths, such
116// as RuleBuilderCommand.Input, RuleBuilderComand.Implicit, or RuleBuilderCommand.FlagWithInput. Inputs to a command
117// that are also outputs of another command in the same RuleBuilder are filtered out.
Colin Crossfeec25b2019-01-30 17:32:39 -0800118func (r *RuleBuilder) Inputs() []string {
119 outputs := r.outputSet()
120
121 inputs := make(map[string]bool)
122 for _, c := range r.commands {
123 for _, input := range c.inputs {
124 if !outputs[input] {
125 inputs[input] = true
126 }
127 }
128 }
129
130 var inputList []string
131 for input := range inputs {
132 inputList = append(inputList, input)
133 }
134 sort.Strings(inputList)
135
136 return inputList
137}
138
139func (r *RuleBuilder) outputSet() map[string]bool {
140 outputs := make(map[string]bool)
141 for _, c := range r.commands {
142 for _, output := range c.outputs {
143 outputs[output] = true
144 }
145 }
146 return outputs
147}
148
Colin Cross758290d2019-02-01 16:42:32 -0800149// Outputs returns the list of paths that were passed to the RuleBuilderCommand methods that take output paths, such
150// as RuleBuilderCommand.Output, RuleBuilderCommand.ImplicitOutput, or RuleBuilderCommand.FlagWithInput.
Colin Crossfeec25b2019-01-30 17:32:39 -0800151func (r *RuleBuilder) Outputs() []string {
152 outputs := r.outputSet()
153
154 var outputList []string
155 for output := range outputs {
Colin Cross5cb5b092019-02-02 21:25:18 -0800156 if !r.temporariesSet[output] {
157 outputList = append(outputList, output)
158 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800159 }
160 sort.Strings(outputList)
161 return outputList
162}
163
Colin Cross758290d2019-02-01 16:42:32 -0800164// Installs returns the list of tuples passed to Install.
Colin Crossdeabb942019-02-11 14:11:09 -0800165func (r *RuleBuilder) Installs() RuleBuilderInstalls {
166 return append(RuleBuilderInstalls(nil), r.installs...)
Colin Crossfeec25b2019-01-30 17:32:39 -0800167}
168
Colin Cross5cb5b092019-02-02 21:25:18 -0800169func (r *RuleBuilder) toolsSet() map[string]bool {
170 tools := make(map[string]bool)
171 for _, c := range r.commands {
172 for _, tool := range c.tools {
173 tools[tool] = true
174 }
175 }
176
177 return tools
178}
179
Colin Cross758290d2019-02-01 16:42:32 -0800180// Tools returns the list of paths that were passed to the RuleBuilderCommand.Tool method.
Colin Crossfeec25b2019-01-30 17:32:39 -0800181func (r *RuleBuilder) Tools() []string {
Colin Cross5cb5b092019-02-02 21:25:18 -0800182 toolsSet := r.toolsSet()
183
184 var toolsList []string
185 for tool := range toolsSet {
186 toolsList = append(toolsList, tool)
Colin Crossfeec25b2019-01-30 17:32:39 -0800187 }
Colin Cross5cb5b092019-02-02 21:25:18 -0800188 sort.Strings(toolsList)
189 return toolsList
Colin Crossfeec25b2019-01-30 17:32:39 -0800190}
191
Colin Cross758290d2019-02-01 16:42:32 -0800192// Commands returns a slice containing a the built command line for each call to RuleBuilder.Command.
Colin Crossfeec25b2019-01-30 17:32:39 -0800193func (r *RuleBuilder) Commands() []string {
194 var commands []string
195 for _, c := range r.commands {
196 commands = append(commands, string(c.buf))
197 }
198 return commands
199}
200
Colin Cross758290d2019-02-01 16:42:32 -0800201// BuilderContext is a subset of ModuleContext and SingletonContext.
Colin Cross786cd6d2019-02-01 16:41:11 -0800202type BuilderContext interface {
203 PathContext
204 Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule
205 Build(PackageContext, BuildParams)
206}
207
Colin Cross758290d2019-02-01 16:42:32 -0800208var _ BuilderContext = ModuleContext(nil)
209var _ BuilderContext = SingletonContext(nil)
210
211// Build adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for
212// Outputs.
Colin Cross786cd6d2019-02-01 16:41:11 -0800213func (r *RuleBuilder) Build(pctx PackageContext, ctx BuilderContext, name string, desc string) {
214 // TODO: convert RuleBuilder arguments and storage to Paths
215 mctx, _ := ctx.(ModuleContext)
Colin Crossfeec25b2019-01-30 17:32:39 -0800216 var inputs Paths
217 for _, input := range r.Inputs() {
Colin Cross786cd6d2019-02-01 16:41:11 -0800218 // Module output paths
219 if mctx != nil {
220 rel, isRel := MaybeRel(ctx, PathForModuleOut(mctx).String(), input)
221 if isRel {
222 inputs = append(inputs, PathForModuleOut(mctx, rel))
223 continue
224 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800225 }
Colin Cross786cd6d2019-02-01 16:41:11 -0800226
227 // Other output paths
228 rel, isRel := MaybeRel(ctx, PathForOutput(ctx).String(), input)
229 if isRel {
230 inputs = append(inputs, PathForOutput(ctx, rel))
231 continue
232 }
233
234 // TODO: remove this once boot image is moved to where PathForOutput can find it.
235 inputs = append(inputs, &unknownRulePath{input})
Colin Crossfeec25b2019-01-30 17:32:39 -0800236 }
237
238 var outputs WritablePaths
239 for _, output := range r.Outputs() {
Colin Cross786cd6d2019-02-01 16:41:11 -0800240 if mctx != nil {
241 rel := Rel(ctx, PathForModuleOut(mctx).String(), output)
242 outputs = append(outputs, PathForModuleOut(mctx, rel))
243 } else {
244 rel := Rel(ctx, PathForOutput(ctx).String(), output)
245 outputs = append(outputs, PathForOutput(ctx, rel))
246 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800247 }
248
Colin Cross0d2f40a2019-02-05 22:31:15 -0800249 if len(r.missingDeps) > 0 {
250 ctx.Build(pctx, BuildParams{
251 Rule: ErrorRule,
252 Outputs: outputs,
253 Description: desc,
254 Args: map[string]string{
255 "error": "missing dependencies: " + strings.Join(r.missingDeps, ", "),
256 },
257 })
258 return
259 }
260
Colin Crossfeec25b2019-01-30 17:32:39 -0800261 if len(r.Commands()) > 0 {
262 ctx.Build(pctx, BuildParams{
263 Rule: ctx.Rule(pctx, name, blueprint.RuleParams{
264 Command: strings.Join(proptools.NinjaEscape(r.Commands()), " && "),
265 CommandDeps: r.Tools(),
266 }),
267 Implicits: inputs,
268 Outputs: outputs,
269 Description: desc,
270 })
271 }
272}
273
Colin Cross758290d2019-02-01 16:42:32 -0800274// RuleBuilderCommand is a builder for a command in a command line. It can be mutated by its methods to add to the
275// command and track dependencies. The methods mutate the RuleBuilderCommand in place, as well as return the
276// RuleBuilderCommand, so they can be used chained or unchained. All methods that add text implicitly add a single
277// space as a separator from the previous method.
Colin Crossfeec25b2019-01-30 17:32:39 -0800278type RuleBuilderCommand struct {
279 buf []byte
280 inputs []string
281 outputs []string
282 tools []string
283}
284
Colin Cross758290d2019-02-01 16:42:32 -0800285// Text adds the specified raw text to the command line. The text should not contain input or output paths or the
286// rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800287func (c *RuleBuilderCommand) Text(text string) *RuleBuilderCommand {
288 if len(c.buf) > 0 {
289 c.buf = append(c.buf, ' ')
290 }
291 c.buf = append(c.buf, text...)
292 return c
293}
294
Colin Cross758290d2019-02-01 16:42:32 -0800295// Textf adds the specified formatted text to the command line. The text should not contain input or output paths or
296// the rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800297func (c *RuleBuilderCommand) Textf(format string, a ...interface{}) *RuleBuilderCommand {
298 return c.Text(fmt.Sprintf(format, a...))
299}
300
Colin Cross758290d2019-02-01 16:42:32 -0800301// Flag adds the specified raw text to the command line. The text should not contain input or output paths or the
302// rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800303func (c *RuleBuilderCommand) Flag(flag string) *RuleBuilderCommand {
304 return c.Text(flag)
305}
306
Colin Cross758290d2019-02-01 16:42:32 -0800307// FlagWithArg adds the specified flag and argument text to the command line, with no separator between them. The flag
308// and argument should not contain input or output paths or the rule will not have them listed in its dependencies or
309// outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800310func (c *RuleBuilderCommand) FlagWithArg(flag, arg string) *RuleBuilderCommand {
311 return c.Text(flag + arg)
312}
313
Colin Crossc7ed0042019-02-11 14:11:09 -0800314// FlagForEachArg adds the specified flag joined with each argument to the command line. The result is identical to
315// calling FlagWithArg for argument.
316func (c *RuleBuilderCommand) FlagForEachArg(flag string, args []string) *RuleBuilderCommand {
317 for _, arg := range args {
318 c.FlagWithArg(flag, arg)
319 }
320 return c
321}
322
Colin Cross758290d2019-02-01 16:42:32 -0800323// FlagWithArg adds the specified flag and list of arguments to the command line, with the arguments joined by sep
324// and no separator between the flag and arguments. The flag and arguments should not contain input or output paths or
325// the rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800326func (c *RuleBuilderCommand) FlagWithList(flag string, list []string, sep string) *RuleBuilderCommand {
327 return c.Text(flag + strings.Join(list, sep))
328}
329
Colin Cross758290d2019-02-01 16:42:32 -0800330// Tool adds the specified tool path to the command line. The path will be also added to the dependencies returned by
331// RuleBuilder.Tools.
Colin Crossfeec25b2019-01-30 17:32:39 -0800332func (c *RuleBuilderCommand) Tool(path string) *RuleBuilderCommand {
333 c.tools = append(c.tools, path)
334 return c.Text(path)
335}
336
Colin Cross758290d2019-02-01 16:42:32 -0800337// Input adds the specified input path to the command line. The path will also be added to the dependencies returned by
338// RuleBuilder.Inputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800339func (c *RuleBuilderCommand) Input(path string) *RuleBuilderCommand {
340 c.inputs = append(c.inputs, path)
341 return c.Text(path)
342}
343
Colin Cross758290d2019-02-01 16:42:32 -0800344// Inputs adds the specified input paths to the command line, separated by spaces. The paths will also be added to the
345// dependencies returned by RuleBuilder.Inputs.
346func (c *RuleBuilderCommand) Inputs(paths []string) *RuleBuilderCommand {
347 for _, path := range paths {
348 c.Input(path)
349 }
350 return c
351}
352
353// Implicit adds the specified input path to the dependencies returned by RuleBuilder.Inputs without modifying the
354// command line.
Colin Crossfeec25b2019-01-30 17:32:39 -0800355func (c *RuleBuilderCommand) Implicit(path string) *RuleBuilderCommand {
356 c.inputs = append(c.inputs, path)
357 return c
358}
359
Colin Cross758290d2019-02-01 16:42:32 -0800360// Implicits adds the specified input paths to the dependencies returned by RuleBuilder.Inputs without modifying the
361// command line.
Colin Crossfeec25b2019-01-30 17:32:39 -0800362func (c *RuleBuilderCommand) Implicits(paths []string) *RuleBuilderCommand {
363 c.inputs = append(c.inputs, paths...)
364 return c
365}
366
Colin Cross758290d2019-02-01 16:42:32 -0800367// Output adds the specified output path to the command line. The path will also be added to the outputs returned by
368// RuleBuilder.Outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800369func (c *RuleBuilderCommand) Output(path string) *RuleBuilderCommand {
370 c.outputs = append(c.outputs, path)
371 return c.Text(path)
372}
373
Colin Cross758290d2019-02-01 16:42:32 -0800374// Outputs adds the specified output paths to the command line, separated by spaces. The paths will also be added to
375// the outputs returned by RuleBuilder.Outputs.
376func (c *RuleBuilderCommand) Outputs(paths []string) *RuleBuilderCommand {
377 for _, path := range paths {
378 c.Output(path)
379 }
380 return c
381}
382
383// ImplicitOutput adds the specified output path to the dependencies returned by RuleBuilder.Outputs without modifying
384// the command line.
Colin Crossfeec25b2019-01-30 17:32:39 -0800385func (c *RuleBuilderCommand) ImplicitOutput(path string) *RuleBuilderCommand {
386 c.outputs = append(c.outputs, path)
387 return c
388}
389
Colin Cross758290d2019-02-01 16:42:32 -0800390// ImplicitOutputs adds the specified output paths to the dependencies returned by RuleBuilder.Outputs without modifying
391// the command line.
392func (c *RuleBuilderCommand) ImplicitOutputs(paths []string) *RuleBuilderCommand {
393 c.outputs = append(c.outputs, paths...)
394 return c
395}
396
397// FlagWithInput adds the specified flag and input path to the command line, with no separator between them. The path
398// will also be added to the dependencies returned by RuleBuilder.Inputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800399func (c *RuleBuilderCommand) FlagWithInput(flag, path string) *RuleBuilderCommand {
400 c.inputs = append(c.inputs, path)
401 return c.Text(flag + path)
402}
403
Colin Cross758290d2019-02-01 16:42:32 -0800404// FlagWithInputList adds the specified flag and input paths to the command line, with the inputs joined by sep
405// and no separator between the flag and inputs. The input paths will also be added to the dependencies returned by
406// RuleBuilder.Inputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800407func (c *RuleBuilderCommand) FlagWithInputList(flag string, paths []string, sep string) *RuleBuilderCommand {
408 c.inputs = append(c.inputs, paths...)
409 return c.FlagWithList(flag, paths, sep)
410}
411
Colin Cross758290d2019-02-01 16:42:32 -0800412// FlagForEachInput adds the specified flag joined with each input path to the command line. The input paths will also
413// be added to the dependencies returned by RuleBuilder.Inputs. The result is identical to calling FlagWithInput for
414// each input path.
415func (c *RuleBuilderCommand) FlagForEachInput(flag string, paths []string) *RuleBuilderCommand {
416 for _, path := range paths {
417 c.FlagWithInput(flag, path)
418 }
419 return c
420}
421
422// FlagWithOutput adds the specified flag and output path to the command line, with no separator between them. The path
423// will also be added to the outputs returned by RuleBuilder.Outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800424func (c *RuleBuilderCommand) FlagWithOutput(flag, path string) *RuleBuilderCommand {
425 c.outputs = append(c.outputs, path)
426 return c.Text(flag + path)
427}
428
Colin Cross758290d2019-02-01 16:42:32 -0800429// String returns the command line.
430func (c *RuleBuilderCommand) String() string {
431 return string(c.buf)
432}
433
Colin Crossfeec25b2019-01-30 17:32:39 -0800434type unknownRulePath struct {
435 path string
436}
437
438var _ Path = (*unknownRulePath)(nil)
439
440func (p *unknownRulePath) String() string { return p.path }
441func (p *unknownRulePath) Ext() string { return filepath.Ext(p.path) }
442func (p *unknownRulePath) Base() string { return filepath.Base(p.path) }
443func (p *unknownRulePath) Rel() string { return p.path }