blob: e53eb0d2a967afb9bdd6e638960b36bf2c8d1a69 [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"
Colin Crossfeec25b2019-01-30 17:32:39 -080019 "sort"
20 "strings"
21
22 "github.com/google/blueprint"
23 "github.com/google/blueprint/proptools"
Dan Willemsen633c5022019-04-12 11:11:38 -070024
25 "android/soong/shared"
Colin Crossfeec25b2019-01-30 17:32:39 -080026)
27
Colin Cross758290d2019-02-01 16:42:32 -080028// RuleBuilder provides an alternative to ModuleContext.Rule and ModuleContext.Build to add a command line to the build
29// graph.
Colin Crossfeec25b2019-01-30 17:32:39 -080030type RuleBuilder struct {
Colin Cross5cb5b092019-02-02 21:25:18 -080031 commands []*RuleBuilderCommand
Colin Crossdeabb942019-02-11 14:11:09 -080032 installs RuleBuilderInstalls
Colin Cross69f59a32019-02-15 10:39:37 -080033 temporariesSet map[WritablePath]bool
Colin Cross5cb5b092019-02-02 21:25:18 -080034 restat bool
Dan Willemsen633c5022019-04-12 11:11:38 -070035 sbox bool
36 sboxOutDir WritablePath
Colin Cross0d2f40a2019-02-05 22:31:15 -080037 missingDeps []string
Colin Crossfeec25b2019-01-30 17:32:39 -080038}
39
Colin Cross758290d2019-02-01 16:42:32 -080040// NewRuleBuilder returns a newly created RuleBuilder.
41func NewRuleBuilder() *RuleBuilder {
Colin Cross5cb5b092019-02-02 21:25:18 -080042 return &RuleBuilder{
Colin Cross69f59a32019-02-15 10:39:37 -080043 temporariesSet: make(map[WritablePath]bool),
Colin Cross5cb5b092019-02-02 21:25:18 -080044 }
Colin Cross758290d2019-02-01 16:42:32 -080045}
46
47// RuleBuilderInstall is a tuple of install from and to locations.
48type RuleBuilderInstall struct {
Colin Cross69f59a32019-02-15 10:39:37 -080049 From Path
50 To string
Colin Cross758290d2019-02-01 16:42:32 -080051}
52
Colin Crossdeabb942019-02-11 14:11:09 -080053type RuleBuilderInstalls []RuleBuilderInstall
54
55// String returns the RuleBuilderInstalls in the form used by $(call copy-many-files) in Make, a space separated
56// list of from:to tuples.
57func (installs RuleBuilderInstalls) String() string {
58 sb := strings.Builder{}
59 for i, install := range installs {
60 if i != 0 {
61 sb.WriteRune(' ')
62 }
Colin Cross69f59a32019-02-15 10:39:37 -080063 sb.WriteString(install.From.String())
Colin Crossdeabb942019-02-11 14:11:09 -080064 sb.WriteRune(':')
65 sb.WriteString(install.To)
66 }
67 return sb.String()
68}
69
Colin Cross0d2f40a2019-02-05 22:31:15 -080070// MissingDeps adds modules to the list of missing dependencies. If MissingDeps
71// is called with a non-empty input, any call to Build will result in a rule
72// that will print an error listing the missing dependencies and fail.
73// MissingDeps should only be called if Config.AllowMissingDependencies() is
74// true.
75func (r *RuleBuilder) MissingDeps(missingDeps []string) {
76 r.missingDeps = append(r.missingDeps, missingDeps...)
77}
78
Colin Cross758290d2019-02-01 16:42:32 -080079// Restat marks the rule as a restat rule, which will be passed to ModuleContext.Rule in BuildParams.Restat.
Dan Willemsen633c5022019-04-12 11:11:38 -070080//
81// Restat is not compatible with Sbox()
Colin Crossfeec25b2019-01-30 17:32:39 -080082func (r *RuleBuilder) Restat() *RuleBuilder {
Dan Willemsen633c5022019-04-12 11:11:38 -070083 if r.sbox {
84 panic("Restat() is not compatible with Sbox()")
85 }
Colin Crossfeec25b2019-01-30 17:32:39 -080086 r.restat = true
87 return r
88}
89
Dan Willemsen633c5022019-04-12 11:11:38 -070090// Sbox marks the rule as needing to be wrapped by sbox. The WritablePath should point to the output
91// directory that sbox will wipe. It should not be written to by any other rule. sbox will ensure
92// that all outputs have been written, and will discard any output files that were not specified.
93//
94// Sbox is not compatible with Restat()
95func (r *RuleBuilder) Sbox(outputDir WritablePath) *RuleBuilder {
96 if r.sbox {
97 panic("Sbox() may not be called more than once")
98 }
99 if len(r.commands) > 0 {
100 panic("Sbox() may not be called after Command()")
101 }
102 if r.restat {
103 panic("Sbox() is not compatible with Restat()")
104 }
105 r.sbox = true
106 r.sboxOutDir = outputDir
107 return r
108}
109
Colin Cross758290d2019-02-01 16:42:32 -0800110// Install associates an output of the rule with an install location, which can be retrieved later using
111// RuleBuilder.Installs.
Colin Cross69f59a32019-02-15 10:39:37 -0800112func (r *RuleBuilder) Install(from Path, to string) {
Colin Crossfeec25b2019-01-30 17:32:39 -0800113 r.installs = append(r.installs, RuleBuilderInstall{from, to})
114}
115
Colin Cross758290d2019-02-01 16:42:32 -0800116// Command returns a new RuleBuilderCommand for the rule. The commands will be ordered in the rule by when they were
117// created by this method. That can be mutated through their methods in any order, as long as the mutations do not
118// race with any call to Build.
Colin Crossfeec25b2019-01-30 17:32:39 -0800119func (r *RuleBuilder) Command() *RuleBuilderCommand {
Dan Willemsen633c5022019-04-12 11:11:38 -0700120 command := &RuleBuilderCommand{
121 sbox: r.sbox,
122 sboxOutDir: r.sboxOutDir,
123 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800124 r.commands = append(r.commands, command)
125 return command
126}
127
Colin Cross5cb5b092019-02-02 21:25:18 -0800128// Temporary marks an output of a command as an intermediate file that will be used as an input to another command
129// in the same rule, and should not be listed in Outputs.
Colin Cross69f59a32019-02-15 10:39:37 -0800130func (r *RuleBuilder) Temporary(path WritablePath) {
Colin Cross5cb5b092019-02-02 21:25:18 -0800131 r.temporariesSet[path] = true
132}
133
134// DeleteTemporaryFiles adds a command to the rule that deletes any outputs that have been marked using Temporary
135// when the rule runs. DeleteTemporaryFiles should be called after all calls to Temporary.
136func (r *RuleBuilder) DeleteTemporaryFiles() {
Colin Cross69f59a32019-02-15 10:39:37 -0800137 var temporariesList WritablePaths
Colin Cross5cb5b092019-02-02 21:25:18 -0800138
139 for intermediate := range r.temporariesSet {
140 temporariesList = append(temporariesList, intermediate)
141 }
Colin Cross69f59a32019-02-15 10:39:37 -0800142
143 sort.Slice(temporariesList, func(i, j int) bool {
144 return temporariesList[i].String() < temporariesList[j].String()
145 })
Colin Cross5cb5b092019-02-02 21:25:18 -0800146
147 r.Command().Text("rm").Flag("-f").Outputs(temporariesList)
148}
149
Colin Cross758290d2019-02-01 16:42:32 -0800150// Inputs returns the list of paths that were passed to the RuleBuilderCommand methods that take input paths, such
151// as RuleBuilderCommand.Input, RuleBuilderComand.Implicit, or RuleBuilderCommand.FlagWithInput. Inputs to a command
152// that are also outputs of another command in the same RuleBuilder are filtered out.
Colin Cross69f59a32019-02-15 10:39:37 -0800153func (r *RuleBuilder) Inputs() Paths {
Colin Crossfeec25b2019-01-30 17:32:39 -0800154 outputs := r.outputSet()
Dan Willemsen633c5022019-04-12 11:11:38 -0700155 depFiles := r.depFileSet()
Colin Crossfeec25b2019-01-30 17:32:39 -0800156
Colin Cross69f59a32019-02-15 10:39:37 -0800157 inputs := make(map[string]Path)
Colin Crossfeec25b2019-01-30 17:32:39 -0800158 for _, c := range r.commands {
159 for _, input := range c.inputs {
Dan Willemsen633c5022019-04-12 11:11:38 -0700160 inputStr := input.String()
161 if _, isOutput := outputs[inputStr]; !isOutput {
162 if _, isDepFile := depFiles[inputStr]; !isDepFile {
163 inputs[input.String()] = input
164 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800165 }
166 }
167 }
168
Colin Cross69f59a32019-02-15 10:39:37 -0800169 var inputList Paths
170 for _, input := range inputs {
Colin Crossfeec25b2019-01-30 17:32:39 -0800171 inputList = append(inputList, input)
172 }
Colin Cross69f59a32019-02-15 10:39:37 -0800173
174 sort.Slice(inputList, func(i, j int) bool {
175 return inputList[i].String() < inputList[j].String()
176 })
Colin Crossfeec25b2019-01-30 17:32:39 -0800177
178 return inputList
179}
180
Colin Cross69f59a32019-02-15 10:39:37 -0800181func (r *RuleBuilder) outputSet() map[string]WritablePath {
182 outputs := make(map[string]WritablePath)
Colin Crossfeec25b2019-01-30 17:32:39 -0800183 for _, c := range r.commands {
184 for _, output := range c.outputs {
Colin Cross69f59a32019-02-15 10:39:37 -0800185 outputs[output.String()] = output
Colin Crossfeec25b2019-01-30 17:32:39 -0800186 }
187 }
188 return outputs
189}
190
Colin Cross758290d2019-02-01 16:42:32 -0800191// Outputs returns the list of paths that were passed to the RuleBuilderCommand methods that take output paths, such
192// as RuleBuilderCommand.Output, RuleBuilderCommand.ImplicitOutput, or RuleBuilderCommand.FlagWithInput.
Colin Cross69f59a32019-02-15 10:39:37 -0800193func (r *RuleBuilder) Outputs() WritablePaths {
Colin Crossfeec25b2019-01-30 17:32:39 -0800194 outputs := r.outputSet()
195
Colin Cross69f59a32019-02-15 10:39:37 -0800196 var outputList WritablePaths
197 for _, output := range outputs {
Colin Cross5cb5b092019-02-02 21:25:18 -0800198 if !r.temporariesSet[output] {
199 outputList = append(outputList, output)
200 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800201 }
Colin Cross69f59a32019-02-15 10:39:37 -0800202
203 sort.Slice(outputList, func(i, j int) bool {
204 return outputList[i].String() < outputList[j].String()
205 })
206
Colin Crossfeec25b2019-01-30 17:32:39 -0800207 return outputList
208}
209
Dan Willemsen633c5022019-04-12 11:11:38 -0700210func (r *RuleBuilder) depFileSet() map[string]WritablePath {
211 depFiles := make(map[string]WritablePath)
212 for _, c := range r.commands {
213 for _, depFile := range c.depFiles {
214 depFiles[depFile.String()] = depFile
215 }
216 }
217 return depFiles
218}
219
Colin Cross1d2cf042019-03-29 15:33:06 -0700220// DepFiles returns the list of paths that were passed to the RuleBuilderCommand methods that take depfile paths, such
221// as RuleBuilderCommand.DepFile or RuleBuilderCommand.FlagWithDepFile.
222func (r *RuleBuilder) DepFiles() WritablePaths {
223 var depFiles WritablePaths
224
225 for _, c := range r.commands {
226 for _, depFile := range c.depFiles {
227 depFiles = append(depFiles, depFile)
228 }
229 }
230
231 return depFiles
232}
233
Colin Cross758290d2019-02-01 16:42:32 -0800234// Installs returns the list of tuples passed to Install.
Colin Crossdeabb942019-02-11 14:11:09 -0800235func (r *RuleBuilder) Installs() RuleBuilderInstalls {
236 return append(RuleBuilderInstalls(nil), r.installs...)
Colin Crossfeec25b2019-01-30 17:32:39 -0800237}
238
Colin Cross69f59a32019-02-15 10:39:37 -0800239func (r *RuleBuilder) toolsSet() map[string]Path {
240 tools := make(map[string]Path)
Colin Cross5cb5b092019-02-02 21:25:18 -0800241 for _, c := range r.commands {
242 for _, tool := range c.tools {
Colin Cross69f59a32019-02-15 10:39:37 -0800243 tools[tool.String()] = tool
Colin Cross5cb5b092019-02-02 21:25:18 -0800244 }
245 }
246
247 return tools
248}
249
Colin Cross758290d2019-02-01 16:42:32 -0800250// Tools returns the list of paths that were passed to the RuleBuilderCommand.Tool method.
Colin Cross69f59a32019-02-15 10:39:37 -0800251func (r *RuleBuilder) Tools() Paths {
Colin Cross5cb5b092019-02-02 21:25:18 -0800252 toolsSet := r.toolsSet()
253
Colin Cross69f59a32019-02-15 10:39:37 -0800254 var toolsList Paths
255 for _, tool := range toolsSet {
Colin Cross5cb5b092019-02-02 21:25:18 -0800256 toolsList = append(toolsList, tool)
Colin Crossfeec25b2019-01-30 17:32:39 -0800257 }
Colin Cross69f59a32019-02-15 10:39:37 -0800258
259 sort.Slice(toolsList, func(i, j int) bool {
260 return toolsList[i].String() < toolsList[j].String()
261 })
262
Colin Cross5cb5b092019-02-02 21:25:18 -0800263 return toolsList
Colin Crossfeec25b2019-01-30 17:32:39 -0800264}
265
Colin Cross758290d2019-02-01 16:42:32 -0800266// Commands returns a slice containing a the built command line for each call to RuleBuilder.Command.
Colin Crossfeec25b2019-01-30 17:32:39 -0800267func (r *RuleBuilder) Commands() []string {
268 var commands []string
269 for _, c := range r.commands {
Colin Crosscfec40c2019-07-08 17:07:18 -0700270 commands = append(commands, c.buf.String())
Colin Crossfeec25b2019-01-30 17:32:39 -0800271 }
272 return commands
273}
274
Colin Cross758290d2019-02-01 16:42:32 -0800275// BuilderContext is a subset of ModuleContext and SingletonContext.
Colin Cross786cd6d2019-02-01 16:41:11 -0800276type BuilderContext interface {
277 PathContext
278 Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule
279 Build(PackageContext, BuildParams)
280}
281
Colin Cross758290d2019-02-01 16:42:32 -0800282var _ BuilderContext = ModuleContext(nil)
283var _ BuilderContext = SingletonContext(nil)
284
Colin Cross1d2cf042019-03-29 15:33:06 -0700285func (r *RuleBuilder) depFileMergerCmd(ctx PathContext, depFiles WritablePaths) *RuleBuilderCommand {
Dan Willemsen633c5022019-04-12 11:11:38 -0700286 return r.Command().
Colin Cross1d2cf042019-03-29 15:33:06 -0700287 Tool(ctx.Config().HostToolPath(ctx, "dep_fixer")).
Dan Willemsen633c5022019-04-12 11:11:38 -0700288 Inputs(depFiles.Paths())
Colin Cross1d2cf042019-03-29 15:33:06 -0700289}
290
Colin Cross758290d2019-02-01 16:42:32 -0800291// Build adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for
292// Outputs.
Colin Cross786cd6d2019-02-01 16:41:11 -0800293func (r *RuleBuilder) Build(pctx PackageContext, ctx BuilderContext, name string, desc string) {
Colin Cross1d2cf042019-03-29 15:33:06 -0700294 name = ninjaNameEscape(name)
295
Colin Cross0d2f40a2019-02-05 22:31:15 -0800296 if len(r.missingDeps) > 0 {
297 ctx.Build(pctx, BuildParams{
298 Rule: ErrorRule,
Colin Cross69f59a32019-02-15 10:39:37 -0800299 Outputs: r.Outputs(),
Colin Cross0d2f40a2019-02-05 22:31:15 -0800300 Description: desc,
301 Args: map[string]string{
302 "error": "missing dependencies: " + strings.Join(r.missingDeps, ", "),
303 },
304 })
305 return
306 }
307
Colin Cross1d2cf042019-03-29 15:33:06 -0700308 var depFile WritablePath
309 var depFormat blueprint.Deps
310 if depFiles := r.DepFiles(); len(depFiles) > 0 {
311 depFile = depFiles[0]
312 depFormat = blueprint.DepsGCC
313 if len(depFiles) > 1 {
314 // Add a command locally that merges all depfiles together into the first depfile.
Dan Willemsen633c5022019-04-12 11:11:38 -0700315 r.depFileMergerCmd(ctx, depFiles)
316
317 if r.sbox {
318 // Check for Rel() errors, as all depfiles should be in the output dir
319 for _, path := range depFiles[1:] {
320 Rel(ctx, r.sboxOutDir.String(), path.String())
321 }
322 }
Colin Cross1d2cf042019-03-29 15:33:06 -0700323 }
324 }
325
Dan Willemsen633c5022019-04-12 11:11:38 -0700326 tools := r.Tools()
327 commands := r.Commands()
328 outputs := r.Outputs()
329
330 if len(commands) == 0 {
331 return
332 }
333 if len(outputs) == 0 {
334 panic("No outputs specified from any Commands")
335 }
336
337 commandString := strings.Join(proptools.NinjaEscapeList(commands), " && ")
338
339 if r.sbox {
340 sboxOutputs := make([]string, len(outputs))
341 for i, output := range outputs {
342 sboxOutputs[i] = "__SBOX_OUT_DIR__/" + Rel(ctx, r.sboxOutDir.String(), output.String())
343 }
344
345 if depFile != nil {
346 sboxOutputs = append(sboxOutputs, "__SBOX_OUT_DIR__/"+Rel(ctx, r.sboxOutDir.String(), depFile.String()))
347 }
348
349 commandString = proptools.ShellEscape(commandString)
350 if !strings.HasPrefix(commandString, `'`) {
351 commandString = `'` + commandString + `'`
352 }
353
354 sboxCmd := &RuleBuilderCommand{}
355 sboxCmd.Tool(ctx.Config().HostToolPath(ctx, "sbox")).
356 Flag("-c").Text(commandString).
357 Flag("--sandbox-path").Text(shared.TempDirForOutDir(PathForOutput(ctx).String())).
358 Flag("--output-root").Text(r.sboxOutDir.String()).
359 Flags(sboxOutputs)
360
Colin Crosscfec40c2019-07-08 17:07:18 -0700361 commandString = sboxCmd.buf.String()
Dan Willemsen633c5022019-04-12 11:11:38 -0700362 tools = append(tools, sboxCmd.tools...)
363 }
364
Colin Cross1d2cf042019-03-29 15:33:06 -0700365 // Ninja doesn't like multiple outputs when depfiles are enabled, move all but the first output to
366 // ImplicitOutputs. RuleBuilder never uses "$out", so the distinction between Outputs and ImplicitOutputs
367 // doesn't matter.
Dan Willemsen633c5022019-04-12 11:11:38 -0700368 output := outputs[0]
369 implicitOutputs := outputs[1:]
Colin Cross1d2cf042019-03-29 15:33:06 -0700370
Dan Willemsen633c5022019-04-12 11:11:38 -0700371 ctx.Build(pctx, BuildParams{
372 Rule: ctx.Rule(pctx, name, blueprint.RuleParams{
373 Command: commandString,
374 CommandDeps: tools.Strings(),
375 Restat: r.restat,
376 }),
377 Implicits: r.Inputs(),
378 Output: output,
379 ImplicitOutputs: implicitOutputs,
380 Depfile: depFile,
381 Deps: depFormat,
382 Description: desc,
383 })
Colin Crossfeec25b2019-01-30 17:32:39 -0800384}
385
Colin Cross758290d2019-02-01 16:42:32 -0800386// RuleBuilderCommand is a builder for a command in a command line. It can be mutated by its methods to add to the
387// command and track dependencies. The methods mutate the RuleBuilderCommand in place, as well as return the
388// RuleBuilderCommand, so they can be used chained or unchained. All methods that add text implicitly add a single
389// space as a separator from the previous method.
Colin Crossfeec25b2019-01-30 17:32:39 -0800390type RuleBuilderCommand struct {
Colin Crosscfec40c2019-07-08 17:07:18 -0700391 buf strings.Builder
Colin Cross1d2cf042019-03-29 15:33:06 -0700392 inputs Paths
393 outputs WritablePaths
394 depFiles WritablePaths
395 tools Paths
Dan Willemsen633c5022019-04-12 11:11:38 -0700396
397 sbox bool
398 sboxOutDir WritablePath
399}
400
401func (c *RuleBuilderCommand) addInput(path Path) string {
402 if c.sbox {
403 if rel, isRel, _ := maybeRelErr(c.sboxOutDir.String(), path.String()); isRel {
404 return "__SBOX_OUT_DIR__/" + rel
405 }
406 }
407 c.inputs = append(c.inputs, path)
408 return path.String()
409}
410
411func (c *RuleBuilderCommand) outputStr(path Path) string {
412 if c.sbox {
413 // Errors will be handled in RuleBuilder.Build where we have a context to report them
414 rel, _, _ := maybeRelErr(c.sboxOutDir.String(), path.String())
415 return "__SBOX_OUT_DIR__/" + rel
416 }
417 return path.String()
Colin Crossfeec25b2019-01-30 17:32:39 -0800418}
419
Colin Cross758290d2019-02-01 16:42:32 -0800420// Text adds the specified raw text to the command line. The text should not contain input or output paths or the
421// rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800422func (c *RuleBuilderCommand) Text(text string) *RuleBuilderCommand {
Colin Crosscfec40c2019-07-08 17:07:18 -0700423 if c.buf.Len() > 0 {
424 c.buf.WriteByte(' ')
Colin Crossfeec25b2019-01-30 17:32:39 -0800425 }
Colin Crosscfec40c2019-07-08 17:07:18 -0700426 c.buf.WriteString(text)
Colin Crossfeec25b2019-01-30 17:32:39 -0800427 return c
428}
429
Colin Cross758290d2019-02-01 16:42:32 -0800430// Textf adds the specified formatted text to the command line. The text should not contain input or output paths or
431// the rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800432func (c *RuleBuilderCommand) Textf(format string, a ...interface{}) *RuleBuilderCommand {
433 return c.Text(fmt.Sprintf(format, a...))
434}
435
Colin Cross758290d2019-02-01 16:42:32 -0800436// Flag adds the specified raw text to the command line. The text should not contain input or output paths or the
437// rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800438func (c *RuleBuilderCommand) Flag(flag string) *RuleBuilderCommand {
439 return c.Text(flag)
440}
441
Colin Cross92b7d582019-03-29 15:32:51 -0700442// Flags adds the specified raw text to the command line. The text should not contain input or output paths or the
443// rule will not have them listed in its dependencies or outputs.
444func (c *RuleBuilderCommand) Flags(flags []string) *RuleBuilderCommand {
445 for _, flag := range flags {
446 c.Text(flag)
447 }
448 return c
449}
450
Colin Cross758290d2019-02-01 16:42:32 -0800451// FlagWithArg adds the specified flag and argument text to the command line, with no separator between them. The flag
452// and argument should not contain input or output paths or the rule will not have them listed in its dependencies or
453// outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800454func (c *RuleBuilderCommand) FlagWithArg(flag, arg string) *RuleBuilderCommand {
455 return c.Text(flag + arg)
456}
457
Colin Crossc7ed0042019-02-11 14:11:09 -0800458// FlagForEachArg adds the specified flag joined with each argument to the command line. The result is identical to
459// calling FlagWithArg for argument.
460func (c *RuleBuilderCommand) FlagForEachArg(flag string, args []string) *RuleBuilderCommand {
461 for _, arg := range args {
462 c.FlagWithArg(flag, arg)
463 }
464 return c
465}
466
Roland Levillain2da5d9a2019-02-27 16:56:41 +0000467// FlagWithList adds the specified flag and list of arguments to the command line, with the arguments joined by sep
Colin Cross758290d2019-02-01 16:42:32 -0800468// and no separator between the flag and arguments. The flag and arguments should not contain input or output paths or
469// the rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800470func (c *RuleBuilderCommand) FlagWithList(flag string, list []string, sep string) *RuleBuilderCommand {
471 return c.Text(flag + strings.Join(list, sep))
472}
473
Colin Cross758290d2019-02-01 16:42:32 -0800474// Tool adds the specified tool path to the command line. The path will be also added to the dependencies returned by
475// RuleBuilder.Tools.
Colin Cross69f59a32019-02-15 10:39:37 -0800476func (c *RuleBuilderCommand) Tool(path Path) *RuleBuilderCommand {
Colin Crossfeec25b2019-01-30 17:32:39 -0800477 c.tools = append(c.tools, path)
Colin Cross69f59a32019-02-15 10:39:37 -0800478 return c.Text(path.String())
Colin Crossfeec25b2019-01-30 17:32:39 -0800479}
480
Colin Cross758290d2019-02-01 16:42:32 -0800481// Input adds the specified input path to the command line. The path will also be added to the dependencies returned by
482// RuleBuilder.Inputs.
Colin Cross69f59a32019-02-15 10:39:37 -0800483func (c *RuleBuilderCommand) Input(path Path) *RuleBuilderCommand {
Dan Willemsen633c5022019-04-12 11:11:38 -0700484 return c.Text(c.addInput(path))
Colin Crossfeec25b2019-01-30 17:32:39 -0800485}
486
Colin Cross758290d2019-02-01 16:42:32 -0800487// Inputs adds the specified input paths to the command line, separated by spaces. The paths will also be added to the
488// dependencies returned by RuleBuilder.Inputs.
Colin Cross69f59a32019-02-15 10:39:37 -0800489func (c *RuleBuilderCommand) Inputs(paths Paths) *RuleBuilderCommand {
Colin Cross758290d2019-02-01 16:42:32 -0800490 for _, path := range paths {
491 c.Input(path)
492 }
493 return c
494}
495
496// Implicit adds the specified input path to the dependencies returned by RuleBuilder.Inputs without modifying the
497// command line.
Colin Cross69f59a32019-02-15 10:39:37 -0800498func (c *RuleBuilderCommand) Implicit(path Path) *RuleBuilderCommand {
Dan Willemsen633c5022019-04-12 11:11:38 -0700499 c.addInput(path)
Colin Crossfeec25b2019-01-30 17:32:39 -0800500 return c
501}
502
Colin Cross758290d2019-02-01 16:42:32 -0800503// Implicits adds the specified input paths to the dependencies returned by RuleBuilder.Inputs without modifying the
504// command line.
Colin Cross69f59a32019-02-15 10:39:37 -0800505func (c *RuleBuilderCommand) Implicits(paths Paths) *RuleBuilderCommand {
Dan Willemsen633c5022019-04-12 11:11:38 -0700506 for _, path := range paths {
507 c.addInput(path)
508 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800509 return c
510}
511
Colin Cross758290d2019-02-01 16:42:32 -0800512// Output adds the specified output path to the command line. The path will also be added to the outputs returned by
513// RuleBuilder.Outputs.
Colin Cross69f59a32019-02-15 10:39:37 -0800514func (c *RuleBuilderCommand) Output(path WritablePath) *RuleBuilderCommand {
Colin Crossfeec25b2019-01-30 17:32:39 -0800515 c.outputs = append(c.outputs, path)
Dan Willemsen633c5022019-04-12 11:11:38 -0700516 return c.Text(c.outputStr(path))
Colin Crossfeec25b2019-01-30 17:32:39 -0800517}
518
Colin Cross758290d2019-02-01 16:42:32 -0800519// Outputs adds the specified output paths to the command line, separated by spaces. The paths will also be added to
520// the outputs returned by RuleBuilder.Outputs.
Colin Cross69f59a32019-02-15 10:39:37 -0800521func (c *RuleBuilderCommand) Outputs(paths WritablePaths) *RuleBuilderCommand {
Colin Cross758290d2019-02-01 16:42:32 -0800522 for _, path := range paths {
523 c.Output(path)
524 }
525 return c
526}
527
Dan Willemsen1945a4b2019-06-04 17:10:41 -0700528// OutputDir adds the output directory to the command line. This is only available when used with RuleBuilder.Sbox,
529// and will be the temporary output directory managed by sbox, not the final one.
530func (c *RuleBuilderCommand) OutputDir() *RuleBuilderCommand {
531 if !c.sbox {
532 panic("OutputDir only valid with Sbox")
533 }
534 return c.Text("__SBOX_OUT_DIR__")
535}
536
Colin Cross1d2cf042019-03-29 15:33:06 -0700537// DepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles and adds it to the command
538// line, and causes RuleBuilder.Build file to set the depfile flag for ninja. If multiple depfiles are added to
539// commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the depfiles together.
540func (c *RuleBuilderCommand) DepFile(path WritablePath) *RuleBuilderCommand {
541 c.depFiles = append(c.depFiles, path)
Dan Willemsen633c5022019-04-12 11:11:38 -0700542 return c.Text(c.outputStr(path))
Colin Cross1d2cf042019-03-29 15:33:06 -0700543}
544
Colin Cross758290d2019-02-01 16:42:32 -0800545// ImplicitOutput adds the specified output path to the dependencies returned by RuleBuilder.Outputs without modifying
546// the command line.
Colin Cross69f59a32019-02-15 10:39:37 -0800547func (c *RuleBuilderCommand) ImplicitOutput(path WritablePath) *RuleBuilderCommand {
Colin Crossfeec25b2019-01-30 17:32:39 -0800548 c.outputs = append(c.outputs, path)
549 return c
550}
551
Colin Cross758290d2019-02-01 16:42:32 -0800552// ImplicitOutputs adds the specified output paths to the dependencies returned by RuleBuilder.Outputs without modifying
553// the command line.
Colin Cross69f59a32019-02-15 10:39:37 -0800554func (c *RuleBuilderCommand) ImplicitOutputs(paths WritablePaths) *RuleBuilderCommand {
Colin Cross758290d2019-02-01 16:42:32 -0800555 c.outputs = append(c.outputs, paths...)
556 return c
557}
558
Colin Cross1d2cf042019-03-29 15:33:06 -0700559// ImplicitDepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles without modifying
560// the command line, and causes RuleBuilder.Build file to set the depfile flag for ninja. If multiple depfiles
561// are added to commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the
562// depfiles together.
563func (c *RuleBuilderCommand) ImplicitDepFile(path WritablePath) *RuleBuilderCommand {
564 c.depFiles = append(c.depFiles, path)
565 return c
566}
567
Colin Cross758290d2019-02-01 16:42:32 -0800568// FlagWithInput adds the specified flag and input path to the command line, with no separator between them. The path
569// will also be added to the dependencies returned by RuleBuilder.Inputs.
Colin Cross69f59a32019-02-15 10:39:37 -0800570func (c *RuleBuilderCommand) FlagWithInput(flag string, path Path) *RuleBuilderCommand {
Dan Willemsen633c5022019-04-12 11:11:38 -0700571 return c.Text(flag + c.addInput(path))
Colin Crossfeec25b2019-01-30 17:32:39 -0800572}
573
Colin Cross758290d2019-02-01 16:42:32 -0800574// FlagWithInputList adds the specified flag and input paths to the command line, with the inputs joined by sep
575// and no separator between the flag and inputs. The input paths will also be added to the dependencies returned by
576// RuleBuilder.Inputs.
Colin Cross69f59a32019-02-15 10:39:37 -0800577func (c *RuleBuilderCommand) FlagWithInputList(flag string, paths Paths, sep string) *RuleBuilderCommand {
Dan Willemsen633c5022019-04-12 11:11:38 -0700578 strs := make([]string, len(paths))
579 for i, path := range paths {
580 strs[i] = c.addInput(path)
581 }
582 return c.FlagWithList(flag, strs, sep)
Colin Crossfeec25b2019-01-30 17:32:39 -0800583}
584
Colin Cross758290d2019-02-01 16:42:32 -0800585// FlagForEachInput adds the specified flag joined with each input path to the command line. The input paths will also
586// be added to the dependencies returned by RuleBuilder.Inputs. The result is identical to calling FlagWithInput for
587// each input path.
Colin Cross69f59a32019-02-15 10:39:37 -0800588func (c *RuleBuilderCommand) FlagForEachInput(flag string, paths Paths) *RuleBuilderCommand {
Colin Cross758290d2019-02-01 16:42:32 -0800589 for _, path := range paths {
590 c.FlagWithInput(flag, path)
591 }
592 return c
593}
594
595// FlagWithOutput adds the specified flag and output path to the command line, with no separator between them. The path
596// will also be added to the outputs returned by RuleBuilder.Outputs.
Colin Cross69f59a32019-02-15 10:39:37 -0800597func (c *RuleBuilderCommand) FlagWithOutput(flag string, path WritablePath) *RuleBuilderCommand {
Colin Crossfeec25b2019-01-30 17:32:39 -0800598 c.outputs = append(c.outputs, path)
Dan Willemsen633c5022019-04-12 11:11:38 -0700599 return c.Text(flag + c.outputStr(path))
Colin Crossfeec25b2019-01-30 17:32:39 -0800600}
601
Colin Cross1d2cf042019-03-29 15:33:06 -0700602// FlagWithDepFile adds the specified flag and depfile path to the command line, with no separator between them. The path
603// will also be added to the outputs returned by RuleBuilder.Outputs.
604func (c *RuleBuilderCommand) FlagWithDepFile(flag string, path WritablePath) *RuleBuilderCommand {
605 c.depFiles = append(c.depFiles, path)
Dan Willemsen633c5022019-04-12 11:11:38 -0700606 return c.Text(flag + c.outputStr(path))
Colin Cross1d2cf042019-03-29 15:33:06 -0700607}
608
Colin Cross758290d2019-02-01 16:42:32 -0800609// String returns the command line.
610func (c *RuleBuilderCommand) String() string {
Colin Crosscfec40c2019-07-08 17:07:18 -0700611 return c.buf.String()
Colin Cross758290d2019-02-01 16:42:32 -0800612}
Colin Cross1d2cf042019-03-29 15:33:06 -0700613
614func ninjaNameEscape(s string) string {
615 b := []byte(s)
616 escaped := false
617 for i, c := range b {
618 valid := (c >= 'a' && c <= 'z') ||
619 (c >= 'A' && c <= 'Z') ||
620 (c >= '0' && c <= '9') ||
621 (c == '_') ||
622 (c == '-') ||
623 (c == '.')
624 if !valid {
625 b[i] = '_'
626 escaped = true
627 }
628 }
629 if escaped {
630 s = string(b)
631 }
632 return s
633}