blob: 72c0d10cdb412398b513badfa9e2c15e94a13d98 [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 (
Colin Cross3d680512020-11-13 16:23:53 -080018 "crypto/sha256"
Colin Crossfeec25b2019-01-30 17:32:39 -080019 "fmt"
Colin Cross3d680512020-11-13 16:23:53 -080020 "path/filepath"
Colin Crossfeec25b2019-01-30 17:32:39 -080021 "sort"
22 "strings"
Colin Crosse16ce362020-11-12 08:29:30 -080023 "testing"
Colin Crossfeec25b2019-01-30 17:32:39 -080024
Colin Crosse16ce362020-11-12 08:29:30 -080025 "github.com/golang/protobuf/proto"
Colin Crossfeec25b2019-01-30 17:32:39 -080026 "github.com/google/blueprint"
27 "github.com/google/blueprint/proptools"
Dan Willemsen633c5022019-04-12 11:11:38 -070028
Colin Crosse16ce362020-11-12 08:29:30 -080029 "android/soong/cmd/sbox/sbox_proto"
Colin Crossef972742021-03-12 17:24:45 -080030 "android/soong/remoteexec"
Colin Crosse55bd422021-03-23 13:44:30 -070031 "android/soong/response"
Dan Willemsen633c5022019-04-12 11:11:38 -070032 "android/soong/shared"
Colin Crossfeec25b2019-01-30 17:32:39 -080033)
34
Colin Crosse16ce362020-11-12 08:29:30 -080035const sboxSandboxBaseDir = "__SBOX_SANDBOX_DIR__"
36const sboxOutSubDir = "out"
Colin Crossba9e4032020-11-24 16:32:22 -080037const sboxToolsSubDir = "tools"
Colin Crosse16ce362020-11-12 08:29:30 -080038const sboxOutDir = sboxSandboxBaseDir + "/" + sboxOutSubDir
Colin Cross3d680512020-11-13 16:23:53 -080039
Colin Cross758290d2019-02-01 16:42:32 -080040// RuleBuilder provides an alternative to ModuleContext.Rule and ModuleContext.Build to add a command line to the build
41// graph.
Colin Crossfeec25b2019-01-30 17:32:39 -080042type RuleBuilder struct {
Colin Crossf1a035e2020-11-16 17:32:30 -080043 pctx PackageContext
44 ctx BuilderContext
45
Colin Crosse16ce362020-11-12 08:29:30 -080046 commands []*RuleBuilderCommand
47 installs RuleBuilderInstalls
48 temporariesSet map[WritablePath]bool
49 restat bool
50 sbox bool
51 highmem bool
52 remoteable RemoteRuleSupports
Colin Crossef972742021-03-12 17:24:45 -080053 rbeParams *remoteexec.REParams
Colin Crossf1a035e2020-11-16 17:32:30 -080054 outDir WritablePath
Colin Crossba9e4032020-11-24 16:32:22 -080055 sboxTools bool
Colin Crossab020a72021-03-12 17:52:23 -080056 sboxInputs bool
Colin Crosse16ce362020-11-12 08:29:30 -080057 sboxManifestPath WritablePath
58 missingDeps []string
Colin Crossfeec25b2019-01-30 17:32:39 -080059}
60
Colin Cross758290d2019-02-01 16:42:32 -080061// NewRuleBuilder returns a newly created RuleBuilder.
Colin Crossf1a035e2020-11-16 17:32:30 -080062func NewRuleBuilder(pctx PackageContext, ctx BuilderContext) *RuleBuilder {
Colin Cross5cb5b092019-02-02 21:25:18 -080063 return &RuleBuilder{
Colin Crossf1a035e2020-11-16 17:32:30 -080064 pctx: pctx,
65 ctx: ctx,
Colin Cross69f59a32019-02-15 10:39:37 -080066 temporariesSet: make(map[WritablePath]bool),
Colin Cross5cb5b092019-02-02 21:25:18 -080067 }
Colin Cross758290d2019-02-01 16:42:32 -080068}
69
70// RuleBuilderInstall is a tuple of install from and to locations.
71type RuleBuilderInstall struct {
Colin Cross69f59a32019-02-15 10:39:37 -080072 From Path
73 To string
Colin Cross758290d2019-02-01 16:42:32 -080074}
75
Colin Crossdeabb942019-02-11 14:11:09 -080076type RuleBuilderInstalls []RuleBuilderInstall
77
78// String returns the RuleBuilderInstalls in the form used by $(call copy-many-files) in Make, a space separated
79// list of from:to tuples.
80func (installs RuleBuilderInstalls) String() string {
81 sb := strings.Builder{}
82 for i, install := range installs {
83 if i != 0 {
84 sb.WriteRune(' ')
85 }
Colin Cross69f59a32019-02-15 10:39:37 -080086 sb.WriteString(install.From.String())
Colin Crossdeabb942019-02-11 14:11:09 -080087 sb.WriteRune(':')
88 sb.WriteString(install.To)
89 }
90 return sb.String()
91}
92
Colin Cross0d2f40a2019-02-05 22:31:15 -080093// MissingDeps adds modules to the list of missing dependencies. If MissingDeps
94// is called with a non-empty input, any call to Build will result in a rule
95// that will print an error listing the missing dependencies and fail.
96// MissingDeps should only be called if Config.AllowMissingDependencies() is
97// true.
98func (r *RuleBuilder) MissingDeps(missingDeps []string) {
99 r.missingDeps = append(r.missingDeps, missingDeps...)
100}
101
Colin Cross758290d2019-02-01 16:42:32 -0800102// 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 -0700103//
104// Restat is not compatible with Sbox()
Colin Crossfeec25b2019-01-30 17:32:39 -0800105func (r *RuleBuilder) Restat() *RuleBuilder {
Dan Willemsen633c5022019-04-12 11:11:38 -0700106 if r.sbox {
107 panic("Restat() is not compatible with Sbox()")
108 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800109 r.restat = true
110 return r
111}
112
Colin Cross8b8bec32019-11-15 13:18:43 -0800113// HighMem marks the rule as a high memory rule, which will limit how many run in parallel with other high memory
114// rules.
115func (r *RuleBuilder) HighMem() *RuleBuilder {
116 r.highmem = true
117 return r
118}
119
120// Remoteable marks the rule as supporting remote execution.
121func (r *RuleBuilder) Remoteable(supports RemoteRuleSupports) *RuleBuilder {
122 r.remoteable = supports
123 return r
124}
125
Colin Crossef972742021-03-12 17:24:45 -0800126// Rewrapper marks the rule as running inside rewrapper using the given params in order to support
127// running on RBE. During RuleBuilder.Build the params will be combined with the inputs, outputs
128// and tools known to RuleBuilder to prepend an appropriate rewrapper command line to the rule's
129// command line.
130func (r *RuleBuilder) Rewrapper(params *remoteexec.REParams) *RuleBuilder {
131 if !r.sboxInputs {
132 panic(fmt.Errorf("RuleBuilder.Rewrapper must be called after RuleBuilder.SandboxInputs"))
133 }
134 r.rbeParams = params
135 return r
136}
137
Colin Crosse16ce362020-11-12 08:29:30 -0800138// Sbox marks the rule as needing to be wrapped by sbox. The outputDir should point to the output
139// directory that sbox will wipe. It should not be written to by any other rule. manifestPath should
140// point to a location where sbox's manifest will be written and must be outside outputDir. sbox
141// will ensure that all outputs have been written, and will discard any output files that were not
142// specified.
Dan Willemsen633c5022019-04-12 11:11:38 -0700143//
144// Sbox is not compatible with Restat()
Colin Crosse16ce362020-11-12 08:29:30 -0800145func (r *RuleBuilder) Sbox(outputDir WritablePath, manifestPath WritablePath) *RuleBuilder {
Dan Willemsen633c5022019-04-12 11:11:38 -0700146 if r.sbox {
147 panic("Sbox() may not be called more than once")
148 }
149 if len(r.commands) > 0 {
150 panic("Sbox() may not be called after Command()")
151 }
152 if r.restat {
153 panic("Sbox() is not compatible with Restat()")
154 }
155 r.sbox = true
Colin Crossf1a035e2020-11-16 17:32:30 -0800156 r.outDir = outputDir
Colin Crosse16ce362020-11-12 08:29:30 -0800157 r.sboxManifestPath = manifestPath
Dan Willemsen633c5022019-04-12 11:11:38 -0700158 return r
159}
160
Colin Crossba9e4032020-11-24 16:32:22 -0800161// SandboxTools enables tool sandboxing for the rule by copying any referenced tools into the
162// sandbox.
163func (r *RuleBuilder) SandboxTools() *RuleBuilder {
164 if !r.sbox {
165 panic("SandboxTools() must be called after Sbox()")
166 }
167 if len(r.commands) > 0 {
168 panic("SandboxTools() may not be called after Command()")
169 }
170 r.sboxTools = true
171 return r
172}
173
Colin Crossab020a72021-03-12 17:52:23 -0800174// SandboxInputs enables input sandboxing for the rule by copying any referenced inputs into the
175// sandbox. It also implies SandboxTools().
176//
177// Sandboxing inputs requires RuleBuilder to be aware of all references to input paths. Paths
178// that are passed to RuleBuilder outside of the methods that expect inputs, for example
179// FlagWithArg, must use RuleBuilderCommand.PathForInput to translate the path to one that matches
180// the sandbox layout.
181func (r *RuleBuilder) SandboxInputs() *RuleBuilder {
182 if !r.sbox {
183 panic("SandboxInputs() must be called after Sbox()")
184 }
185 if len(r.commands) > 0 {
186 panic("SandboxInputs() may not be called after Command()")
187 }
188 r.sboxTools = true
189 r.sboxInputs = true
190 return r
191}
192
Colin Cross758290d2019-02-01 16:42:32 -0800193// Install associates an output of the rule with an install location, which can be retrieved later using
194// RuleBuilder.Installs.
Colin Cross69f59a32019-02-15 10:39:37 -0800195func (r *RuleBuilder) Install(from Path, to string) {
Colin Crossfeec25b2019-01-30 17:32:39 -0800196 r.installs = append(r.installs, RuleBuilderInstall{from, to})
197}
198
Colin Cross758290d2019-02-01 16:42:32 -0800199// Command returns a new RuleBuilderCommand for the rule. The commands will be ordered in the rule by when they were
200// created by this method. That can be mutated through their methods in any order, as long as the mutations do not
201// race with any call to Build.
Colin Crossfeec25b2019-01-30 17:32:39 -0800202func (r *RuleBuilder) Command() *RuleBuilderCommand {
Dan Willemsen633c5022019-04-12 11:11:38 -0700203 command := &RuleBuilderCommand{
Colin Crossf1a035e2020-11-16 17:32:30 -0800204 rule: r,
Dan Willemsen633c5022019-04-12 11:11:38 -0700205 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800206 r.commands = append(r.commands, command)
207 return command
208}
209
Colin Cross5cb5b092019-02-02 21:25:18 -0800210// Temporary marks an output of a command as an intermediate file that will be used as an input to another command
211// in the same rule, and should not be listed in Outputs.
Colin Cross69f59a32019-02-15 10:39:37 -0800212func (r *RuleBuilder) Temporary(path WritablePath) {
Colin Cross5cb5b092019-02-02 21:25:18 -0800213 r.temporariesSet[path] = true
214}
215
216// DeleteTemporaryFiles adds a command to the rule that deletes any outputs that have been marked using Temporary
217// when the rule runs. DeleteTemporaryFiles should be called after all calls to Temporary.
218func (r *RuleBuilder) DeleteTemporaryFiles() {
Colin Cross69f59a32019-02-15 10:39:37 -0800219 var temporariesList WritablePaths
Colin Cross5cb5b092019-02-02 21:25:18 -0800220
221 for intermediate := range r.temporariesSet {
222 temporariesList = append(temporariesList, intermediate)
223 }
Colin Cross69f59a32019-02-15 10:39:37 -0800224
225 sort.Slice(temporariesList, func(i, j int) bool {
226 return temporariesList[i].String() < temporariesList[j].String()
227 })
Colin Cross5cb5b092019-02-02 21:25:18 -0800228
229 r.Command().Text("rm").Flag("-f").Outputs(temporariesList)
230}
231
Colin Crossda71eda2020-02-21 16:55:19 -0800232// Inputs returns the list of paths that were passed to the RuleBuilderCommand methods that take
Colin Cross3d680512020-11-13 16:23:53 -0800233// input paths, such as RuleBuilderCommand.Input, RuleBuilderCommand.Implicit, or
Colin Crossda71eda2020-02-21 16:55:19 -0800234// RuleBuilderCommand.FlagWithInput. Inputs to a command that are also outputs of another command
235// in the same RuleBuilder are filtered out. The list is sorted and duplicates removed.
Colin Cross69f59a32019-02-15 10:39:37 -0800236func (r *RuleBuilder) Inputs() Paths {
Colin Crossfeec25b2019-01-30 17:32:39 -0800237 outputs := r.outputSet()
Dan Willemsen633c5022019-04-12 11:11:38 -0700238 depFiles := r.depFileSet()
Colin Crossfeec25b2019-01-30 17:32:39 -0800239
Colin Cross69f59a32019-02-15 10:39:37 -0800240 inputs := make(map[string]Path)
Colin Crossfeec25b2019-01-30 17:32:39 -0800241 for _, c := range r.commands {
Ramy Medhat2f99eec2020-06-13 17:38:27 -0400242 for _, input := range append(c.inputs, c.implicits...) {
Dan Willemsen633c5022019-04-12 11:11:38 -0700243 inputStr := input.String()
244 if _, isOutput := outputs[inputStr]; !isOutput {
245 if _, isDepFile := depFiles[inputStr]; !isDepFile {
246 inputs[input.String()] = input
247 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800248 }
249 }
250 }
251
Colin Cross69f59a32019-02-15 10:39:37 -0800252 var inputList Paths
253 for _, input := range inputs {
Colin Crossfeec25b2019-01-30 17:32:39 -0800254 inputList = append(inputList, input)
255 }
Colin Cross69f59a32019-02-15 10:39:37 -0800256
257 sort.Slice(inputList, func(i, j int) bool {
258 return inputList[i].String() < inputList[j].String()
259 })
Colin Crossfeec25b2019-01-30 17:32:39 -0800260
261 return inputList
262}
263
Colin Crossda71eda2020-02-21 16:55:19 -0800264// OrderOnlys returns the list of paths that were passed to the RuleBuilderCommand.OrderOnly or
265// RuleBuilderCommand.OrderOnlys. The list is sorted and duplicates removed.
266func (r *RuleBuilder) OrderOnlys() Paths {
267 orderOnlys := make(map[string]Path)
268 for _, c := range r.commands {
269 for _, orderOnly := range c.orderOnlys {
270 orderOnlys[orderOnly.String()] = orderOnly
271 }
272 }
273
274 var orderOnlyList Paths
275 for _, orderOnly := range orderOnlys {
276 orderOnlyList = append(orderOnlyList, orderOnly)
277 }
278
279 sort.Slice(orderOnlyList, func(i, j int) bool {
280 return orderOnlyList[i].String() < orderOnlyList[j].String()
281 })
282
283 return orderOnlyList
284}
285
Colin Cross69f59a32019-02-15 10:39:37 -0800286func (r *RuleBuilder) outputSet() map[string]WritablePath {
287 outputs := make(map[string]WritablePath)
Colin Crossfeec25b2019-01-30 17:32:39 -0800288 for _, c := range r.commands {
289 for _, output := range c.outputs {
Colin Cross69f59a32019-02-15 10:39:37 -0800290 outputs[output.String()] = output
Colin Crossfeec25b2019-01-30 17:32:39 -0800291 }
292 }
293 return outputs
294}
295
Colin Crossda71eda2020-02-21 16:55:19 -0800296// Outputs returns the list of paths that were passed to the RuleBuilderCommand methods that take
297// output paths, such as RuleBuilderCommand.Output, RuleBuilderCommand.ImplicitOutput, or
298// RuleBuilderCommand.FlagWithInput. The list is sorted and duplicates removed.
Colin Cross69f59a32019-02-15 10:39:37 -0800299func (r *RuleBuilder) Outputs() WritablePaths {
Colin Crossfeec25b2019-01-30 17:32:39 -0800300 outputs := r.outputSet()
301
Colin Cross69f59a32019-02-15 10:39:37 -0800302 var outputList WritablePaths
303 for _, output := range outputs {
Colin Cross5cb5b092019-02-02 21:25:18 -0800304 if !r.temporariesSet[output] {
305 outputList = append(outputList, output)
306 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800307 }
Colin Cross69f59a32019-02-15 10:39:37 -0800308
309 sort.Slice(outputList, func(i, j int) bool {
310 return outputList[i].String() < outputList[j].String()
311 })
312
Colin Crossfeec25b2019-01-30 17:32:39 -0800313 return outputList
314}
315
Jingwen Chence679d22020-09-23 04:30:02 +0000316func (r *RuleBuilder) symlinkOutputSet() map[string]WritablePath {
317 symlinkOutputs := make(map[string]WritablePath)
318 for _, c := range r.commands {
319 for _, symlinkOutput := range c.symlinkOutputs {
320 symlinkOutputs[symlinkOutput.String()] = symlinkOutput
321 }
322 }
323 return symlinkOutputs
324}
325
326// SymlinkOutputs returns the list of paths that the executor (Ninja) would
327// verify, after build edge completion, that:
328//
329// 1) Created output symlinks match the list of paths in this list exactly (no more, no fewer)
330// 2) Created output files are *not* declared in this list.
331//
332// These symlink outputs are expected to be a subset of outputs or implicit
333// outputs, or they would fail validation at build param construction time
334// later, to support other non-rule-builder approaches for constructing
335// statements.
336func (r *RuleBuilder) SymlinkOutputs() WritablePaths {
337 symlinkOutputs := r.symlinkOutputSet()
338
339 var symlinkOutputList WritablePaths
340 for _, symlinkOutput := range symlinkOutputs {
341 symlinkOutputList = append(symlinkOutputList, symlinkOutput)
342 }
343
344 sort.Slice(symlinkOutputList, func(i, j int) bool {
345 return symlinkOutputList[i].String() < symlinkOutputList[j].String()
346 })
347
348 return symlinkOutputList
349}
350
Dan Willemsen633c5022019-04-12 11:11:38 -0700351func (r *RuleBuilder) depFileSet() map[string]WritablePath {
352 depFiles := make(map[string]WritablePath)
353 for _, c := range r.commands {
354 for _, depFile := range c.depFiles {
355 depFiles[depFile.String()] = depFile
356 }
357 }
358 return depFiles
359}
360
Colin Cross1d2cf042019-03-29 15:33:06 -0700361// DepFiles returns the list of paths that were passed to the RuleBuilderCommand methods that take depfile paths, such
362// as RuleBuilderCommand.DepFile or RuleBuilderCommand.FlagWithDepFile.
363func (r *RuleBuilder) DepFiles() WritablePaths {
364 var depFiles WritablePaths
365
366 for _, c := range r.commands {
367 for _, depFile := range c.depFiles {
368 depFiles = append(depFiles, depFile)
369 }
370 }
371
372 return depFiles
373}
374
Colin Cross758290d2019-02-01 16:42:32 -0800375// Installs returns the list of tuples passed to Install.
Colin Crossdeabb942019-02-11 14:11:09 -0800376func (r *RuleBuilder) Installs() RuleBuilderInstalls {
377 return append(RuleBuilderInstalls(nil), r.installs...)
Colin Crossfeec25b2019-01-30 17:32:39 -0800378}
379
Colin Cross69f59a32019-02-15 10:39:37 -0800380func (r *RuleBuilder) toolsSet() map[string]Path {
381 tools := make(map[string]Path)
Colin Cross5cb5b092019-02-02 21:25:18 -0800382 for _, c := range r.commands {
383 for _, tool := range c.tools {
Colin Cross69f59a32019-02-15 10:39:37 -0800384 tools[tool.String()] = tool
Colin Cross5cb5b092019-02-02 21:25:18 -0800385 }
386 }
387
388 return tools
389}
390
Colin Crossda71eda2020-02-21 16:55:19 -0800391// Tools returns the list of paths that were passed to the RuleBuilderCommand.Tool method. The
392// list is sorted and duplicates removed.
Colin Cross69f59a32019-02-15 10:39:37 -0800393func (r *RuleBuilder) Tools() Paths {
Colin Cross5cb5b092019-02-02 21:25:18 -0800394 toolsSet := r.toolsSet()
395
Colin Cross69f59a32019-02-15 10:39:37 -0800396 var toolsList Paths
397 for _, tool := range toolsSet {
Colin Cross5cb5b092019-02-02 21:25:18 -0800398 toolsList = append(toolsList, tool)
Colin Crossfeec25b2019-01-30 17:32:39 -0800399 }
Colin Cross69f59a32019-02-15 10:39:37 -0800400
401 sort.Slice(toolsList, func(i, j int) bool {
402 return toolsList[i].String() < toolsList[j].String()
403 })
404
Colin Cross5cb5b092019-02-02 21:25:18 -0800405 return toolsList
Colin Crossfeec25b2019-01-30 17:32:39 -0800406}
407
Colin Cross0cb0d7b2019-07-11 10:59:15 -0700408// RspFileInputs returns the list of paths that were passed to the RuleBuilderCommand.FlagWithRspFileInputList method.
409func (r *RuleBuilder) RspFileInputs() Paths {
410 var rspFileInputs Paths
411 for _, c := range r.commands {
Colin Crossce3a51d2021-03-19 16:22:12 -0700412 for _, rspFile := range c.rspFiles {
413 rspFileInputs = append(rspFileInputs, rspFile.paths...)
Colin Cross0cb0d7b2019-07-11 10:59:15 -0700414 }
415 }
416
417 return rspFileInputs
418}
419
Colin Crossce3a51d2021-03-19 16:22:12 -0700420func (r *RuleBuilder) rspFiles() []rspFileAndPaths {
421 var rspFiles []rspFileAndPaths
Colin Cross70c47412021-03-12 17:48:14 -0800422 for _, c := range r.commands {
Colin Crossce3a51d2021-03-19 16:22:12 -0700423 rspFiles = append(rspFiles, c.rspFiles...)
Colin Cross70c47412021-03-12 17:48:14 -0800424 }
425
Colin Crossce3a51d2021-03-19 16:22:12 -0700426 return rspFiles
Colin Cross70c47412021-03-12 17:48:14 -0800427}
428
Colin Cross0cb0d7b2019-07-11 10:59:15 -0700429// Commands returns a slice containing the built command line for each call to RuleBuilder.Command.
Colin Crossfeec25b2019-01-30 17:32:39 -0800430func (r *RuleBuilder) Commands() []string {
431 var commands []string
432 for _, c := range r.commands {
Colin Cross0cb0d7b2019-07-11 10:59:15 -0700433 commands = append(commands, c.String())
434 }
435 return commands
436}
437
Colin Cross758290d2019-02-01 16:42:32 -0800438// BuilderContext is a subset of ModuleContext and SingletonContext.
Colin Cross786cd6d2019-02-01 16:41:11 -0800439type BuilderContext interface {
440 PathContext
441 Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule
442 Build(PackageContext, BuildParams)
443}
444
Colin Cross758290d2019-02-01 16:42:32 -0800445var _ BuilderContext = ModuleContext(nil)
446var _ BuilderContext = SingletonContext(nil)
447
Colin Crossf1a035e2020-11-16 17:32:30 -0800448func (r *RuleBuilder) depFileMergerCmd(depFiles WritablePaths) *RuleBuilderCommand {
Dan Willemsen633c5022019-04-12 11:11:38 -0700449 return r.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800450 BuiltTool("dep_fixer").
Dan Willemsen633c5022019-04-12 11:11:38 -0700451 Inputs(depFiles.Paths())
Colin Cross1d2cf042019-03-29 15:33:06 -0700452}
453
Colin Cross758290d2019-02-01 16:42:32 -0800454// Build adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for
455// Outputs.
Colin Crossf1a035e2020-11-16 17:32:30 -0800456func (r *RuleBuilder) Build(name string, desc string) {
Colin Cross1d2cf042019-03-29 15:33:06 -0700457 name = ninjaNameEscape(name)
458
Colin Cross0d2f40a2019-02-05 22:31:15 -0800459 if len(r.missingDeps) > 0 {
Colin Crossf1a035e2020-11-16 17:32:30 -0800460 r.ctx.Build(pctx, BuildParams{
Colin Cross0d2f40a2019-02-05 22:31:15 -0800461 Rule: ErrorRule,
Colin Cross69f59a32019-02-15 10:39:37 -0800462 Outputs: r.Outputs(),
Colin Crossda71eda2020-02-21 16:55:19 -0800463 OrderOnly: r.OrderOnlys(),
Colin Cross0d2f40a2019-02-05 22:31:15 -0800464 Description: desc,
465 Args: map[string]string{
466 "error": "missing dependencies: " + strings.Join(r.missingDeps, ", "),
467 },
468 })
469 return
470 }
471
Colin Cross1d2cf042019-03-29 15:33:06 -0700472 var depFile WritablePath
473 var depFormat blueprint.Deps
474 if depFiles := r.DepFiles(); len(depFiles) > 0 {
475 depFile = depFiles[0]
476 depFormat = blueprint.DepsGCC
477 if len(depFiles) > 1 {
478 // Add a command locally that merges all depfiles together into the first depfile.
Colin Crossf1a035e2020-11-16 17:32:30 -0800479 r.depFileMergerCmd(depFiles)
Dan Willemsen633c5022019-04-12 11:11:38 -0700480
481 if r.sbox {
Colin Crosse16ce362020-11-12 08:29:30 -0800482 // Check for Rel() errors, as all depfiles should be in the output dir. Errors
483 // will be reported to the ctx.
Dan Willemsen633c5022019-04-12 11:11:38 -0700484 for _, path := range depFiles[1:] {
Colin Crossf1a035e2020-11-16 17:32:30 -0800485 Rel(r.ctx, r.outDir.String(), path.String())
Dan Willemsen633c5022019-04-12 11:11:38 -0700486 }
487 }
Colin Cross1d2cf042019-03-29 15:33:06 -0700488 }
489 }
490
Dan Willemsen633c5022019-04-12 11:11:38 -0700491 tools := r.Tools()
Colin Crossb70a1a92021-03-12 17:51:32 -0800492 commands := r.Commands()
Dan Willemsen633c5022019-04-12 11:11:38 -0700493 outputs := r.Outputs()
Colin Cross3d680512020-11-13 16:23:53 -0800494 inputs := r.Inputs()
Colin Crossce3a51d2021-03-19 16:22:12 -0700495 rspFiles := r.rspFiles()
Dan Willemsen633c5022019-04-12 11:11:38 -0700496
497 if len(commands) == 0 {
498 return
499 }
500 if len(outputs) == 0 {
501 panic("No outputs specified from any Commands")
502 }
503
Colin Cross0cb0d7b2019-07-11 10:59:15 -0700504 commandString := strings.Join(commands, " && ")
Dan Willemsen633c5022019-04-12 11:11:38 -0700505
506 if r.sbox {
Colin Crosse16ce362020-11-12 08:29:30 -0800507 // If running the command inside sbox, write the rule data out to an sbox
508 // manifest.textproto.
509 manifest := sbox_proto.Manifest{}
510 command := sbox_proto.Command{}
511 manifest.Commands = append(manifest.Commands, &command)
512 command.Command = proto.String(commandString)
Colin Cross151b9ff2020-11-12 08:29:30 -0800513
Colin Cross619b9ab2020-11-20 18:44:31 +0000514 if depFile != nil {
Colin Crosse16ce362020-11-12 08:29:30 -0800515 manifest.OutputDepfile = proto.String(depFile.String())
Colin Cross619b9ab2020-11-20 18:44:31 +0000516 }
517
Colin Crossba9e4032020-11-24 16:32:22 -0800518 // If sandboxing tools is enabled, add copy rules to the manifest to copy each tool
519 // into the sbox directory.
520 if r.sboxTools {
521 for _, tool := range tools {
522 command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{
523 From: proto.String(tool.String()),
524 To: proto.String(sboxPathForToolRel(r.ctx, tool)),
525 })
526 }
527 for _, c := range r.commands {
528 for _, tool := range c.packagedTools {
529 command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{
530 From: proto.String(tool.srcPath.String()),
531 To: proto.String(sboxPathForPackagedToolRel(tool)),
532 Executable: proto.Bool(tool.executable),
533 })
534 tools = append(tools, tool.srcPath)
535 }
536 }
537 }
538
Colin Crossab020a72021-03-12 17:52:23 -0800539 // If sandboxing inputs is enabled, add copy rules to the manifest to copy each input
540 // into the sbox directory.
541 if r.sboxInputs {
542 for _, input := range inputs {
543 command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{
544 From: proto.String(input.String()),
545 To: proto.String(r.sboxPathForInputRel(input)),
546 })
547 }
548
Colin Crossce3a51d2021-03-19 16:22:12 -0700549 // If using rsp files copy them and their contents into the sbox directory with
550 // the appropriate path mappings.
551 for _, rspFile := range rspFiles {
Colin Crosse55bd422021-03-23 13:44:30 -0700552 command.RspFiles = append(command.RspFiles, &sbox_proto.RspFile{
Colin Crossce3a51d2021-03-19 16:22:12 -0700553 File: proto.String(rspFile.file.String()),
Colin Crosse55bd422021-03-23 13:44:30 -0700554 // These have to match the logic in sboxPathForInputRel
555 PathMappings: []*sbox_proto.PathMapping{
556 {
557 From: proto.String(r.outDir.String()),
558 To: proto.String(sboxOutSubDir),
559 },
560 {
561 From: proto.String(PathForOutput(r.ctx).String()),
562 To: proto.String(sboxOutSubDir),
563 },
564 },
Colin Crossab020a72021-03-12 17:52:23 -0800565 })
566 }
567
568 command.Chdir = proto.Bool(true)
569 }
570
Colin Crosse16ce362020-11-12 08:29:30 -0800571 // Add copy rules to the manifest to copy each output file from the sbox directory.
Colin Crossba9e4032020-11-24 16:32:22 -0800572 // to the output directory after running the commands.
Colin Crosse16ce362020-11-12 08:29:30 -0800573 sboxOutputs := make([]string, len(outputs))
574 for i, output := range outputs {
Colin Crossf1a035e2020-11-16 17:32:30 -0800575 rel := Rel(r.ctx, r.outDir.String(), output.String())
Colin Crosse16ce362020-11-12 08:29:30 -0800576 sboxOutputs[i] = filepath.Join(sboxOutDir, rel)
577 command.CopyAfter = append(command.CopyAfter, &sbox_proto.Copy{
578 From: proto.String(filepath.Join(sboxOutSubDir, rel)),
579 To: proto.String(output.String()),
580 })
581 }
Colin Cross619b9ab2020-11-20 18:44:31 +0000582
Colin Cross5334edd2021-03-11 17:18:21 -0800583 // Outputs that were marked Temporary will not be checked that they are in the output
584 // directory by the loop above, check them here.
585 for path := range r.temporariesSet {
586 Rel(r.ctx, r.outDir.String(), path.String())
587 }
588
Colin Crosse16ce362020-11-12 08:29:30 -0800589 // Add a hash of the list of input files to the manifest so that the textproto file
590 // changes when the list of input files changes and causes the sbox rule that
591 // depends on it to rerun.
592 command.InputHash = proto.String(hashSrcFiles(inputs))
Colin Cross619b9ab2020-11-20 18:44:31 +0000593
Colin Crosse16ce362020-11-12 08:29:30 -0800594 // Verify that the manifest textproto is not inside the sbox output directory, otherwise
595 // it will get deleted when the sbox rule clears its output directory.
Colin Crossf1a035e2020-11-16 17:32:30 -0800596 _, manifestInOutDir := MaybeRel(r.ctx, r.outDir.String(), r.sboxManifestPath.String())
Colin Crosse16ce362020-11-12 08:29:30 -0800597 if manifestInOutDir {
Colin Crossf1a035e2020-11-16 17:32:30 -0800598 ReportPathErrorf(r.ctx, "sbox rule %q manifestPath %q must not be in outputDir %q",
599 name, r.sboxManifestPath.String(), r.outDir.String())
Colin Crosse16ce362020-11-12 08:29:30 -0800600 }
601
602 // Create a rule to write the manifest as a the textproto.
Colin Cross1c217fd2021-03-12 17:24:18 -0800603 WriteFileRule(r.ctx, r.sboxManifestPath, proto.MarshalTextString(&manifest))
Colin Crosse16ce362020-11-12 08:29:30 -0800604
605 // Generate a new string to use as the command line of the sbox rule. This uses
606 // a RuleBuilderCommand as a convenience method of building the command line, then
607 // converts it to a string to replace commandString.
Colin Crossf1a035e2020-11-16 17:32:30 -0800608 sboxCmd := &RuleBuilderCommand{
609 rule: &RuleBuilder{
610 ctx: r.ctx,
611 },
612 }
613 sboxCmd.Text("rm -rf").Output(r.outDir)
Colin Crosse16ce362020-11-12 08:29:30 -0800614 sboxCmd.Text("&&")
Colin Crossf1a035e2020-11-16 17:32:30 -0800615 sboxCmd.BuiltTool("sbox").
616 Flag("--sandbox-path").Text(shared.TempDirForOutDir(PathForOutput(r.ctx).String())).
Colin Crosse16ce362020-11-12 08:29:30 -0800617 Flag("--manifest").Input(r.sboxManifestPath)
618
619 // Replace the command string, and add the sbox tool and manifest textproto to the
620 // dependencies of the final sbox rule.
Colin Crosscfec40c2019-07-08 17:07:18 -0700621 commandString = sboxCmd.buf.String()
Dan Willemsen633c5022019-04-12 11:11:38 -0700622 tools = append(tools, sboxCmd.tools...)
Colin Crosse16ce362020-11-12 08:29:30 -0800623 inputs = append(inputs, sboxCmd.inputs...)
Colin Crossef972742021-03-12 17:24:45 -0800624
625 if r.rbeParams != nil {
Colin Crosse55bd422021-03-23 13:44:30 -0700626 // RBE needs a list of input files to copy to the remote builder. For inputs already
627 // listed in an rsp file, pass the rsp file directly to rewrapper. For the rest,
628 // create a new rsp file to pass to rewrapper.
629 var remoteRspFiles Paths
630 var remoteInputs Paths
631
632 remoteInputs = append(remoteInputs, inputs...)
633 remoteInputs = append(remoteInputs, tools...)
634
Colin Crossce3a51d2021-03-19 16:22:12 -0700635 for _, rspFile := range rspFiles {
636 remoteInputs = append(remoteInputs, rspFile.file)
637 remoteRspFiles = append(remoteRspFiles, rspFile.file)
Colin Crossef972742021-03-12 17:24:45 -0800638 }
Colin Crosse55bd422021-03-23 13:44:30 -0700639
640 if len(remoteInputs) > 0 {
641 inputsListFile := r.sboxManifestPath.ReplaceExtension(r.ctx, "rbe_inputs.list")
642 writeRspFileRule(r.ctx, inputsListFile, remoteInputs)
643 remoteRspFiles = append(remoteRspFiles, inputsListFile)
644 // Add the new rsp file as an extra input to the rule.
645 inputs = append(inputs, inputsListFile)
646 }
Colin Crossef972742021-03-12 17:24:45 -0800647
648 r.rbeParams.OutputFiles = outputs.Strings()
Colin Crosse55bd422021-03-23 13:44:30 -0700649 r.rbeParams.RSPFiles = remoteRspFiles.Strings()
Colin Crossef972742021-03-12 17:24:45 -0800650 rewrapperCommand := r.rbeParams.NoVarTemplate(r.ctx.Config().RBEWrapper())
651 commandString = rewrapperCommand + " bash -c '" + strings.ReplaceAll(commandString, `'`, `'\''`) + "'"
652 }
Colin Cross3d680512020-11-13 16:23:53 -0800653 } else {
654 // If not using sbox the rule will run the command directly, put the hash of the
655 // list of input files in a comment at the end of the command line to ensure ninja
656 // reruns the rule when the list of input files changes.
657 commandString += " # hash of input list: " + hashSrcFiles(inputs)
Dan Willemsen633c5022019-04-12 11:11:38 -0700658 }
659
Colin Cross1d2cf042019-03-29 15:33:06 -0700660 // Ninja doesn't like multiple outputs when depfiles are enabled, move all but the first output to
Colin Cross70c47412021-03-12 17:48:14 -0800661 // ImplicitOutputs. RuleBuilder doesn't use "$out", so the distinction between Outputs and
Colin Cross0cb0d7b2019-07-11 10:59:15 -0700662 // ImplicitOutputs doesn't matter.
Dan Willemsen633c5022019-04-12 11:11:38 -0700663 output := outputs[0]
664 implicitOutputs := outputs[1:]
Colin Cross1d2cf042019-03-29 15:33:06 -0700665
Colin Cross0cb0d7b2019-07-11 10:59:15 -0700666 var rspFile, rspFileContent string
Colin Crossce3a51d2021-03-19 16:22:12 -0700667 var rspFileInputs Paths
668 if len(rspFiles) > 0 {
669 // The first rsp files uses Ninja's rsp file support for the rule
670 rspFile = rspFiles[0].file.String()
Colin Crosse55bd422021-03-23 13:44:30 -0700671 // Use "$in" for rspFileContent to avoid duplicating the list of files in the dependency
672 // list and in the contents of the rsp file. Inputs to the rule that are not in the
673 // rsp file will be listed in Implicits instead of Inputs so they don't show up in "$in".
674 rspFileContent = "$in"
Colin Crossce3a51d2021-03-19 16:22:12 -0700675 rspFileInputs = append(rspFileInputs, rspFiles[0].paths...)
676
677 for _, rspFile := range rspFiles[1:] {
678 // Any additional rsp files need an extra rule to write the file.
679 writeRspFileRule(r.ctx, rspFile.file, rspFile.paths)
680 // The main rule needs to depend on the inputs listed in the extra rsp file.
681 inputs = append(inputs, rspFile.paths...)
682 // The main rule needs to depend on the extra rsp file.
683 inputs = append(inputs, rspFile.file)
684 }
Colin Cross0cb0d7b2019-07-11 10:59:15 -0700685 }
686
Colin Cross8b8bec32019-11-15 13:18:43 -0800687 var pool blueprint.Pool
Colin Crossf1a035e2020-11-16 17:32:30 -0800688 if r.ctx.Config().UseGoma() && r.remoteable.Goma {
Colin Cross8b8bec32019-11-15 13:18:43 -0800689 // When USE_GOMA=true is set and the rule is supported by goma, allow jobs to run outside the local pool.
Colin Crossf1a035e2020-11-16 17:32:30 -0800690 } else if r.ctx.Config().UseRBE() && r.remoteable.RBE {
Ramy Medhat944839a2020-03-31 22:14:52 -0400691 // When USE_RBE=true is set and the rule is supported by RBE, use the remotePool.
692 pool = remotePool
Colin Cross8b8bec32019-11-15 13:18:43 -0800693 } else if r.highmem {
694 pool = highmemPool
Colin Crossf1a035e2020-11-16 17:32:30 -0800695 } else if r.ctx.Config().UseRemoteBuild() {
Colin Cross8b8bec32019-11-15 13:18:43 -0800696 pool = localPool
697 }
698
Colin Crossf1a035e2020-11-16 17:32:30 -0800699 r.ctx.Build(r.pctx, BuildParams{
700 Rule: r.ctx.Rule(pctx, name, blueprint.RuleParams{
Colin Crossb70a1a92021-03-12 17:51:32 -0800701 Command: proptools.NinjaEscape(commandString),
Colin Cross45029782021-03-16 16:49:52 -0700702 CommandDeps: proptools.NinjaEscapeList(tools.Strings()),
Colin Cross0cb0d7b2019-07-11 10:59:15 -0700703 Restat: r.restat,
Colin Cross45029782021-03-16 16:49:52 -0700704 Rspfile: proptools.NinjaEscape(rspFile),
Colin Cross0cb0d7b2019-07-11 10:59:15 -0700705 RspfileContent: rspFileContent,
Colin Cross8b8bec32019-11-15 13:18:43 -0800706 Pool: pool,
Dan Willemsen633c5022019-04-12 11:11:38 -0700707 }),
Colin Cross0cb0d7b2019-07-11 10:59:15 -0700708 Inputs: rspFileInputs,
Colin Cross3d680512020-11-13 16:23:53 -0800709 Implicits: inputs,
Dan Willemsen633c5022019-04-12 11:11:38 -0700710 Output: output,
711 ImplicitOutputs: implicitOutputs,
Jingwen Chence679d22020-09-23 04:30:02 +0000712 SymlinkOutputs: r.SymlinkOutputs(),
Dan Willemsen633c5022019-04-12 11:11:38 -0700713 Depfile: depFile,
714 Deps: depFormat,
715 Description: desc,
716 })
Colin Crossfeec25b2019-01-30 17:32:39 -0800717}
718
Colin Cross758290d2019-02-01 16:42:32 -0800719// RuleBuilderCommand is a builder for a command in a command line. It can be mutated by its methods to add to the
720// command and track dependencies. The methods mutate the RuleBuilderCommand in place, as well as return the
721// RuleBuilderCommand, so they can be used chained or unchained. All methods that add text implicitly add a single
722// space as a separator from the previous method.
Colin Crossfeec25b2019-01-30 17:32:39 -0800723type RuleBuilderCommand struct {
Colin Crossf1a035e2020-11-16 17:32:30 -0800724 rule *RuleBuilder
725
Jingwen Chence679d22020-09-23 04:30:02 +0000726 buf strings.Builder
727 inputs Paths
728 implicits Paths
729 orderOnlys Paths
730 outputs WritablePaths
731 symlinkOutputs WritablePaths
732 depFiles WritablePaths
733 tools Paths
Colin Crossba9e4032020-11-24 16:32:22 -0800734 packagedTools []PackagingSpec
Colin Crossce3a51d2021-03-19 16:22:12 -0700735 rspFiles []rspFileAndPaths
736}
737
738type rspFileAndPaths struct {
739 file WritablePath
740 paths Paths
Dan Willemsen633c5022019-04-12 11:11:38 -0700741}
742
743func (c *RuleBuilderCommand) addInput(path Path) string {
Dan Willemsen633c5022019-04-12 11:11:38 -0700744 c.inputs = append(c.inputs, path)
Colin Crossab020a72021-03-12 17:52:23 -0800745 return c.PathForInput(path)
Dan Willemsen633c5022019-04-12 11:11:38 -0700746}
747
Colin Crossab020a72021-03-12 17:52:23 -0800748func (c *RuleBuilderCommand) addImplicit(path Path) {
Ramy Medhat2f99eec2020-06-13 17:38:27 -0400749 c.implicits = append(c.implicits, path)
Ramy Medhat2f99eec2020-06-13 17:38:27 -0400750}
751
Colin Crossda71eda2020-02-21 16:55:19 -0800752func (c *RuleBuilderCommand) addOrderOnly(path Path) {
753 c.orderOnlys = append(c.orderOnlys, path)
754}
755
Colin Crossab020a72021-03-12 17:52:23 -0800756// PathForInput takes an input path and returns the appropriate path to use on the command line. If
757// sbox was enabled via a call to RuleBuilder.Sbox() and the path was an output path it returns a
758// path with the placeholder prefix used for outputs in sbox. If sbox is not enabled it returns the
759// original path.
760func (c *RuleBuilderCommand) PathForInput(path Path) string {
761 if c.rule.sbox {
762 rel, inSandbox := c.rule._sboxPathForInputRel(path)
763 if inSandbox {
764 rel = filepath.Join(sboxSandboxBaseDir, rel)
765 }
766 return rel
767 }
768 return path.String()
769}
770
771// PathsForInputs takes a list of input paths and returns the appropriate paths to use on the
772// command line. If sbox was enabled via a call to RuleBuilder.Sbox() a path was an output path, it
773// returns the path with the placeholder prefix used for outputs in sbox. If sbox is not enabled it
774// returns the original paths.
775func (c *RuleBuilderCommand) PathsForInputs(paths Paths) []string {
776 ret := make([]string, len(paths))
777 for i, path := range paths {
778 ret[i] = c.PathForInput(path)
779 }
780 return ret
781}
782
Colin Crossf1a035e2020-11-16 17:32:30 -0800783// PathForOutput takes an output path and returns the appropriate path to use on the command
784// line. If sbox was enabled via a call to RuleBuilder.Sbox(), it returns a path with the
785// placeholder prefix used for outputs in sbox. If sbox is not enabled it returns the
786// original path.
787func (c *RuleBuilderCommand) PathForOutput(path WritablePath) string {
788 if c.rule.sbox {
789 // Errors will be handled in RuleBuilder.Build where we have a context to report them
790 rel, _, _ := maybeRelErr(c.rule.outDir.String(), path.String())
791 return filepath.Join(sboxOutDir, rel)
Dan Willemsen633c5022019-04-12 11:11:38 -0700792 }
793 return path.String()
Colin Crossfeec25b2019-01-30 17:32:39 -0800794}
795
Colin Crossba9e4032020-11-24 16:32:22 -0800796// SboxPathForTool takes a path to a tool, which may be an output file or a source file, and returns
797// the corresponding path for the tool in the sbox sandbox. It assumes that sandboxing and tool
798// sandboxing are enabled.
799func SboxPathForTool(ctx BuilderContext, path Path) string {
800 return filepath.Join(sboxSandboxBaseDir, sboxPathForToolRel(ctx, path))
801}
802
803func sboxPathForToolRel(ctx BuilderContext, path Path) string {
804 // Errors will be handled in RuleBuilder.Build where we have a context to report them
805 relOut, isRelOut, _ := maybeRelErr(PathForOutput(ctx, "host", ctx.Config().PrebuiltOS()).String(), path.String())
806 if isRelOut {
807 // The tool is in the output directory, it will be copied to __SBOX_OUT_DIR__/tools/out
808 return filepath.Join(sboxToolsSubDir, "out", relOut)
809 }
810 // The tool is in the source directory, it will be copied to __SBOX_OUT_DIR__/tools/src
811 return filepath.Join(sboxToolsSubDir, "src", path.String())
812}
813
Colin Crossab020a72021-03-12 17:52:23 -0800814func (r *RuleBuilder) _sboxPathForInputRel(path Path) (rel string, inSandbox bool) {
815 // Errors will be handled in RuleBuilder.Build where we have a context to report them
816 rel, isRelSboxOut, _ := maybeRelErr(r.outDir.String(), path.String())
817 if isRelSboxOut {
818 return filepath.Join(sboxOutSubDir, rel), true
819 }
820 if r.sboxInputs {
821 // When sandboxing inputs all inputs have to be copied into the sandbox. Input files that
822 // are outputs of other rules could be an arbitrary absolute path if OUT_DIR is set, so they
823 // will be copied to relative paths under __SBOX_OUT_DIR__/out.
824 rel, isRelOut, _ := maybeRelErr(PathForOutput(r.ctx).String(), path.String())
825 if isRelOut {
826 return filepath.Join(sboxOutSubDir, rel), true
827 }
828 }
829 return path.String(), false
830}
831
832func (r *RuleBuilder) sboxPathForInputRel(path Path) string {
833 rel, _ := r._sboxPathForInputRel(path)
834 return rel
835}
836
837func (r *RuleBuilder) sboxPathsForInputsRel(paths Paths) []string {
838 ret := make([]string, len(paths))
839 for i, path := range paths {
840 ret[i] = r.sboxPathForInputRel(path)
841 }
842 return ret
843}
844
Colin Crossba9e4032020-11-24 16:32:22 -0800845// SboxPathForPackagedTool takes a PackageSpec for a tool and returns the corresponding path for the
846// tool after copying it into the sandbox. This can be used on the RuleBuilder command line to
847// reference the tool.
848func SboxPathForPackagedTool(spec PackagingSpec) string {
849 return filepath.Join(sboxSandboxBaseDir, sboxPathForPackagedToolRel(spec))
850}
851
852func sboxPathForPackagedToolRel(spec PackagingSpec) string {
853 return filepath.Join(sboxToolsSubDir, "out", spec.relPathInPackage)
854}
855
856// PathForTool takes a path to a tool, which may be an output file or a source file, and returns
857// the corresponding path for the tool in the sbox sandbox if sbox is enabled, or the original path
858// if it is not. This can be used on the RuleBuilder command line to reference the tool.
859func (c *RuleBuilderCommand) PathForTool(path Path) string {
860 if c.rule.sbox && c.rule.sboxTools {
861 return filepath.Join(sboxSandboxBaseDir, sboxPathForToolRel(c.rule.ctx, path))
862 }
863 return path.String()
864}
865
866// PackagedTool adds the specified tool path to the command line. It can only be used with tool
867// sandboxing enabled by SandboxTools(), and will copy the tool into the sandbox.
868func (c *RuleBuilderCommand) PackagedTool(spec PackagingSpec) *RuleBuilderCommand {
869 if !c.rule.sboxTools {
870 panic("PackagedTool() requires SandboxTools()")
871 }
872
873 c.packagedTools = append(c.packagedTools, spec)
874 c.Text(sboxPathForPackagedToolRel(spec))
875 return c
876}
877
878// ImplicitPackagedTool copies the specified tool into the sandbox without modifying the command
879// line. It can only be used with tool sandboxing enabled by SandboxTools().
880func (c *RuleBuilderCommand) ImplicitPackagedTool(spec PackagingSpec) *RuleBuilderCommand {
881 if !c.rule.sboxTools {
882 panic("ImplicitPackagedTool() requires SandboxTools()")
883 }
884
885 c.packagedTools = append(c.packagedTools, spec)
886 return c
887}
888
889// ImplicitPackagedTools copies the specified tools into the sandbox without modifying the command
890// line. It can only be used with tool sandboxing enabled by SandboxTools().
891func (c *RuleBuilderCommand) ImplicitPackagedTools(specs []PackagingSpec) *RuleBuilderCommand {
892 if !c.rule.sboxTools {
893 panic("ImplicitPackagedTools() requires SandboxTools()")
894 }
895
896 c.packagedTools = append(c.packagedTools, specs...)
897 return c
898}
899
Colin Cross758290d2019-02-01 16:42:32 -0800900// Text adds the specified raw text to the command line. The text should not contain input or output paths or the
901// rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800902func (c *RuleBuilderCommand) Text(text string) *RuleBuilderCommand {
Colin Crosscfec40c2019-07-08 17:07:18 -0700903 if c.buf.Len() > 0 {
904 c.buf.WriteByte(' ')
Colin Crossfeec25b2019-01-30 17:32:39 -0800905 }
Colin Crosscfec40c2019-07-08 17:07:18 -0700906 c.buf.WriteString(text)
Colin Crossfeec25b2019-01-30 17:32:39 -0800907 return c
908}
909
Colin Cross758290d2019-02-01 16:42:32 -0800910// Textf adds the specified formatted text to the command line. The text should not contain input or output paths or
911// the rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800912func (c *RuleBuilderCommand) Textf(format string, a ...interface{}) *RuleBuilderCommand {
913 return c.Text(fmt.Sprintf(format, a...))
914}
915
Colin Cross758290d2019-02-01 16:42:32 -0800916// Flag adds the specified raw text to the command line. The text should not contain input or output paths or the
917// rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800918func (c *RuleBuilderCommand) Flag(flag string) *RuleBuilderCommand {
919 return c.Text(flag)
920}
921
Colin Crossab054432019-07-15 16:13:59 -0700922// OptionalFlag adds the specified raw text to the command line if it is not nil. The text should not contain input or
923// output paths or the rule will not have them listed in its dependencies or outputs.
924func (c *RuleBuilderCommand) OptionalFlag(flag *string) *RuleBuilderCommand {
925 if flag != nil {
926 c.Text(*flag)
927 }
928
929 return c
930}
931
Colin Cross92b7d582019-03-29 15:32:51 -0700932// Flags adds the specified raw text to the command line. The text should not contain input or output paths or the
933// rule will not have them listed in its dependencies or outputs.
934func (c *RuleBuilderCommand) Flags(flags []string) *RuleBuilderCommand {
935 for _, flag := range flags {
936 c.Text(flag)
937 }
938 return c
939}
940
Colin Cross758290d2019-02-01 16:42:32 -0800941// FlagWithArg adds the specified flag and argument text to the command line, with no separator between them. The flag
942// and argument should not contain input or output paths or the rule will not have them listed in its dependencies or
943// outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800944func (c *RuleBuilderCommand) FlagWithArg(flag, arg string) *RuleBuilderCommand {
945 return c.Text(flag + arg)
946}
947
Colin Crossc7ed0042019-02-11 14:11:09 -0800948// FlagForEachArg adds the specified flag joined with each argument to the command line. The result is identical to
949// calling FlagWithArg for argument.
950func (c *RuleBuilderCommand) FlagForEachArg(flag string, args []string) *RuleBuilderCommand {
951 for _, arg := range args {
952 c.FlagWithArg(flag, arg)
953 }
954 return c
955}
956
Roland Levillain2da5d9a2019-02-27 16:56:41 +0000957// 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 -0800958// and no separator between the flag and arguments. The flag and arguments should not contain input or output paths or
959// the rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800960func (c *RuleBuilderCommand) FlagWithList(flag string, list []string, sep string) *RuleBuilderCommand {
961 return c.Text(flag + strings.Join(list, sep))
962}
963
Colin Cross758290d2019-02-01 16:42:32 -0800964// Tool adds the specified tool path to the command line. The path will be also added to the dependencies returned by
965// RuleBuilder.Tools.
Colin Cross69f59a32019-02-15 10:39:37 -0800966func (c *RuleBuilderCommand) Tool(path Path) *RuleBuilderCommand {
Colin Crossfeec25b2019-01-30 17:32:39 -0800967 c.tools = append(c.tools, path)
Colin Crossba9e4032020-11-24 16:32:22 -0800968 return c.Text(c.PathForTool(path))
969}
970
971// Tool adds the specified tool path to the dependencies returned by RuleBuilder.Tools.
972func (c *RuleBuilderCommand) ImplicitTool(path Path) *RuleBuilderCommand {
973 c.tools = append(c.tools, path)
974 return c
975}
976
977// Tool adds the specified tool path to the dependencies returned by RuleBuilder.Tools.
978func (c *RuleBuilderCommand) ImplicitTools(paths Paths) *RuleBuilderCommand {
979 c.tools = append(c.tools, paths...)
980 return c
Colin Crossfeec25b2019-01-30 17:32:39 -0800981}
982
Colin Crossee94d6a2019-07-08 17:08:34 -0700983// BuiltTool adds the specified tool path that was built using a host Soong module to the command line. The path will
984// be also added to the dependencies returned by RuleBuilder.Tools.
985//
986// It is equivalent to:
987// cmd.Tool(ctx.Config().HostToolPath(ctx, tool))
Colin Crossf1a035e2020-11-16 17:32:30 -0800988func (c *RuleBuilderCommand) BuiltTool(tool string) *RuleBuilderCommand {
989 return c.Tool(c.rule.ctx.Config().HostToolPath(c.rule.ctx, tool))
Colin Crossee94d6a2019-07-08 17:08:34 -0700990}
991
992// PrebuiltBuildTool adds the specified tool path from prebuils/build-tools. The path will be also added to the
993// dependencies returned by RuleBuilder.Tools.
994//
995// It is equivalent to:
996// cmd.Tool(ctx.Config().PrebuiltBuildTool(ctx, tool))
997func (c *RuleBuilderCommand) PrebuiltBuildTool(ctx PathContext, tool string) *RuleBuilderCommand {
998 return c.Tool(ctx.Config().PrebuiltBuildTool(ctx, tool))
999}
1000
Colin Cross758290d2019-02-01 16:42:32 -08001001// Input adds the specified input path to the command line. The path will also be added to the dependencies returned by
1002// RuleBuilder.Inputs.
Colin Cross69f59a32019-02-15 10:39:37 -08001003func (c *RuleBuilderCommand) Input(path Path) *RuleBuilderCommand {
Dan Willemsen633c5022019-04-12 11:11:38 -07001004 return c.Text(c.addInput(path))
Colin Crossfeec25b2019-01-30 17:32:39 -08001005}
1006
Colin Cross758290d2019-02-01 16:42:32 -08001007// Inputs adds the specified input paths to the command line, separated by spaces. The paths will also be added to the
1008// dependencies returned by RuleBuilder.Inputs.
Colin Cross69f59a32019-02-15 10:39:37 -08001009func (c *RuleBuilderCommand) Inputs(paths Paths) *RuleBuilderCommand {
Colin Cross758290d2019-02-01 16:42:32 -08001010 for _, path := range paths {
1011 c.Input(path)
1012 }
1013 return c
1014}
1015
1016// Implicit adds the specified input path to the dependencies returned by RuleBuilder.Inputs without modifying the
1017// command line.
Colin Cross69f59a32019-02-15 10:39:37 -08001018func (c *RuleBuilderCommand) Implicit(path Path) *RuleBuilderCommand {
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001019 c.addImplicit(path)
Colin Crossfeec25b2019-01-30 17:32:39 -08001020 return c
1021}
1022
Colin Cross758290d2019-02-01 16:42:32 -08001023// Implicits adds the specified input paths to the dependencies returned by RuleBuilder.Inputs without modifying the
1024// command line.
Colin Cross69f59a32019-02-15 10:39:37 -08001025func (c *RuleBuilderCommand) Implicits(paths Paths) *RuleBuilderCommand {
Dan Willemsen633c5022019-04-12 11:11:38 -07001026 for _, path := range paths {
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001027 c.addImplicit(path)
Dan Willemsen633c5022019-04-12 11:11:38 -07001028 }
Colin Crossfeec25b2019-01-30 17:32:39 -08001029 return c
1030}
1031
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001032// GetImplicits returns the command's implicit inputs.
1033func (c *RuleBuilderCommand) GetImplicits() Paths {
1034 return c.implicits
1035}
1036
Colin Crossda71eda2020-02-21 16:55:19 -08001037// OrderOnly adds the specified input path to the dependencies returned by RuleBuilder.OrderOnlys
1038// without modifying the command line.
1039func (c *RuleBuilderCommand) OrderOnly(path Path) *RuleBuilderCommand {
1040 c.addOrderOnly(path)
1041 return c
1042}
1043
1044// OrderOnlys adds the specified input paths to the dependencies returned by RuleBuilder.OrderOnlys
1045// without modifying the command line.
1046func (c *RuleBuilderCommand) OrderOnlys(paths Paths) *RuleBuilderCommand {
1047 for _, path := range paths {
1048 c.addOrderOnly(path)
1049 }
1050 return c
1051}
1052
Colin Cross758290d2019-02-01 16:42:32 -08001053// Output adds the specified output path to the command line. The path will also be added to the outputs returned by
1054// RuleBuilder.Outputs.
Colin Cross69f59a32019-02-15 10:39:37 -08001055func (c *RuleBuilderCommand) Output(path WritablePath) *RuleBuilderCommand {
Colin Crossfeec25b2019-01-30 17:32:39 -08001056 c.outputs = append(c.outputs, path)
Colin Crossf1a035e2020-11-16 17:32:30 -08001057 return c.Text(c.PathForOutput(path))
Colin Crossfeec25b2019-01-30 17:32:39 -08001058}
1059
Colin Cross758290d2019-02-01 16:42:32 -08001060// Outputs adds the specified output paths to the command line, separated by spaces. The paths will also be added to
1061// the outputs returned by RuleBuilder.Outputs.
Colin Cross69f59a32019-02-15 10:39:37 -08001062func (c *RuleBuilderCommand) Outputs(paths WritablePaths) *RuleBuilderCommand {
Colin Cross758290d2019-02-01 16:42:32 -08001063 for _, path := range paths {
1064 c.Output(path)
1065 }
1066 return c
1067}
1068
Dan Willemsen1945a4b2019-06-04 17:10:41 -07001069// OutputDir adds the output directory to the command line. This is only available when used with RuleBuilder.Sbox,
1070// and will be the temporary output directory managed by sbox, not the final one.
1071func (c *RuleBuilderCommand) OutputDir() *RuleBuilderCommand {
Colin Crossf1a035e2020-11-16 17:32:30 -08001072 if !c.rule.sbox {
Dan Willemsen1945a4b2019-06-04 17:10:41 -07001073 panic("OutputDir only valid with Sbox")
1074 }
Colin Cross3d680512020-11-13 16:23:53 -08001075 return c.Text(sboxOutDir)
Dan Willemsen1945a4b2019-06-04 17:10:41 -07001076}
1077
Colin Cross1d2cf042019-03-29 15:33:06 -07001078// DepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles and adds it to the command
1079// line, and causes RuleBuilder.Build file to set the depfile flag for ninja. If multiple depfiles are added to
1080// commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the depfiles together.
1081func (c *RuleBuilderCommand) DepFile(path WritablePath) *RuleBuilderCommand {
1082 c.depFiles = append(c.depFiles, path)
Colin Crossf1a035e2020-11-16 17:32:30 -08001083 return c.Text(c.PathForOutput(path))
Colin Cross1d2cf042019-03-29 15:33:06 -07001084}
1085
Colin Cross758290d2019-02-01 16:42:32 -08001086// ImplicitOutput adds the specified output path to the dependencies returned by RuleBuilder.Outputs without modifying
1087// the command line.
Colin Cross69f59a32019-02-15 10:39:37 -08001088func (c *RuleBuilderCommand) ImplicitOutput(path WritablePath) *RuleBuilderCommand {
Colin Crossfeec25b2019-01-30 17:32:39 -08001089 c.outputs = append(c.outputs, path)
1090 return c
1091}
1092
Colin Cross758290d2019-02-01 16:42:32 -08001093// ImplicitOutputs adds the specified output paths to the dependencies returned by RuleBuilder.Outputs without modifying
1094// the command line.
Colin Cross69f59a32019-02-15 10:39:37 -08001095func (c *RuleBuilderCommand) ImplicitOutputs(paths WritablePaths) *RuleBuilderCommand {
Colin Cross758290d2019-02-01 16:42:32 -08001096 c.outputs = append(c.outputs, paths...)
1097 return c
1098}
1099
Jingwen Chence679d22020-09-23 04:30:02 +00001100// ImplicitSymlinkOutput declares the specified path as an implicit output that
1101// will be a symlink instead of a regular file. Does not modify the command
1102// line.
1103func (c *RuleBuilderCommand) ImplicitSymlinkOutput(path WritablePath) *RuleBuilderCommand {
1104 c.symlinkOutputs = append(c.symlinkOutputs, path)
1105 return c.ImplicitOutput(path)
1106}
1107
1108// ImplicitSymlinkOutputs declares the specified paths as implicit outputs that
1109// will be a symlinks instead of regular files. Does not modify the command
1110// line.
1111func (c *RuleBuilderCommand) ImplicitSymlinkOutputs(paths WritablePaths) *RuleBuilderCommand {
1112 for _, path := range paths {
1113 c.ImplicitSymlinkOutput(path)
1114 }
1115 return c
1116}
1117
1118// SymlinkOutput declares the specified path as an output that will be a symlink
1119// instead of a regular file. Modifies the command line.
1120func (c *RuleBuilderCommand) SymlinkOutput(path WritablePath) *RuleBuilderCommand {
1121 c.symlinkOutputs = append(c.symlinkOutputs, path)
1122 return c.Output(path)
1123}
1124
1125// SymlinkOutputsl declares the specified paths as outputs that will be symlinks
1126// instead of regular files. Modifies the command line.
1127func (c *RuleBuilderCommand) SymlinkOutputs(paths WritablePaths) *RuleBuilderCommand {
1128 for _, path := range paths {
1129 c.SymlinkOutput(path)
1130 }
1131 return c
1132}
1133
Colin Cross1d2cf042019-03-29 15:33:06 -07001134// ImplicitDepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles without modifying
1135// the command line, and causes RuleBuilder.Build file to set the depfile flag for ninja. If multiple depfiles
1136// are added to commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the
1137// depfiles together.
1138func (c *RuleBuilderCommand) ImplicitDepFile(path WritablePath) *RuleBuilderCommand {
1139 c.depFiles = append(c.depFiles, path)
1140 return c
1141}
1142
Colin Cross758290d2019-02-01 16:42:32 -08001143// FlagWithInput adds the specified flag and input path to the command line, with no separator between them. The path
1144// will also be added to the dependencies returned by RuleBuilder.Inputs.
Colin Cross69f59a32019-02-15 10:39:37 -08001145func (c *RuleBuilderCommand) FlagWithInput(flag string, path Path) *RuleBuilderCommand {
Dan Willemsen633c5022019-04-12 11:11:38 -07001146 return c.Text(flag + c.addInput(path))
Colin Crossfeec25b2019-01-30 17:32:39 -08001147}
1148
Colin Cross758290d2019-02-01 16:42:32 -08001149// FlagWithInputList adds the specified flag and input paths to the command line, with the inputs joined by sep
1150// and no separator between the flag and inputs. The input paths will also be added to the dependencies returned by
1151// RuleBuilder.Inputs.
Colin Cross69f59a32019-02-15 10:39:37 -08001152func (c *RuleBuilderCommand) FlagWithInputList(flag string, paths Paths, sep string) *RuleBuilderCommand {
Dan Willemsen633c5022019-04-12 11:11:38 -07001153 strs := make([]string, len(paths))
1154 for i, path := range paths {
1155 strs[i] = c.addInput(path)
1156 }
1157 return c.FlagWithList(flag, strs, sep)
Colin Crossfeec25b2019-01-30 17:32:39 -08001158}
1159
Colin Cross758290d2019-02-01 16:42:32 -08001160// FlagForEachInput adds the specified flag joined with each input path to the command line. The input paths will also
1161// be added to the dependencies returned by RuleBuilder.Inputs. The result is identical to calling FlagWithInput for
1162// each input path.
Colin Cross69f59a32019-02-15 10:39:37 -08001163func (c *RuleBuilderCommand) FlagForEachInput(flag string, paths Paths) *RuleBuilderCommand {
Colin Cross758290d2019-02-01 16:42:32 -08001164 for _, path := range paths {
1165 c.FlagWithInput(flag, path)
1166 }
1167 return c
1168}
1169
1170// FlagWithOutput adds the specified flag and output path to the command line, with no separator between them. The path
1171// will also be added to the outputs returned by RuleBuilder.Outputs.
Colin Cross69f59a32019-02-15 10:39:37 -08001172func (c *RuleBuilderCommand) FlagWithOutput(flag string, path WritablePath) *RuleBuilderCommand {
Colin Crossfeec25b2019-01-30 17:32:39 -08001173 c.outputs = append(c.outputs, path)
Colin Crossf1a035e2020-11-16 17:32:30 -08001174 return c.Text(flag + c.PathForOutput(path))
Colin Crossfeec25b2019-01-30 17:32:39 -08001175}
1176
Colin Cross1d2cf042019-03-29 15:33:06 -07001177// FlagWithDepFile adds the specified flag and depfile path to the command line, with no separator between them. The path
1178// will also be added to the outputs returned by RuleBuilder.Outputs.
1179func (c *RuleBuilderCommand) FlagWithDepFile(flag string, path WritablePath) *RuleBuilderCommand {
1180 c.depFiles = append(c.depFiles, path)
Colin Crossf1a035e2020-11-16 17:32:30 -08001181 return c.Text(flag + c.PathForOutput(path))
Colin Cross1d2cf042019-03-29 15:33:06 -07001182}
1183
Colin Crossce3a51d2021-03-19 16:22:12 -07001184// FlagWithRspFileInputList adds the specified flag and path to an rspfile to the command line, with
1185// no separator between them. The paths will be written to the rspfile. If sbox is enabled, the
1186// rspfile must be outside the sbox directory. The first use of FlagWithRspFileInputList in any
1187// RuleBuilderCommand of a RuleBuilder will use Ninja's rsp file support for the rule, additional
1188// uses will result in an auxiliary rules to write the rspFile contents.
Colin Cross70c47412021-03-12 17:48:14 -08001189func (c *RuleBuilderCommand) FlagWithRspFileInputList(flag string, rspFile WritablePath, paths Paths) *RuleBuilderCommand {
Colin Cross0cb0d7b2019-07-11 10:59:15 -07001190 // Use an empty slice if paths is nil, the non-nil slice is used as an indicator that the rsp file must be
1191 // generated.
1192 if paths == nil {
1193 paths = Paths{}
1194 }
1195
Colin Crossce3a51d2021-03-19 16:22:12 -07001196 c.rspFiles = append(c.rspFiles, rspFileAndPaths{rspFile, paths})
Colin Cross0cb0d7b2019-07-11 10:59:15 -07001197
Colin Cross70c47412021-03-12 17:48:14 -08001198 if c.rule.sbox {
1199 if _, isRel, _ := maybeRelErr(c.rule.outDir.String(), rspFile.String()); isRel {
1200 panic(fmt.Errorf("FlagWithRspFileInputList rspfile %q must not be inside out dir %q",
1201 rspFile.String(), c.rule.outDir.String()))
1202 }
1203 }
1204
Colin Crossab020a72021-03-12 17:52:23 -08001205 c.FlagWithArg(flag, c.PathForInput(rspFile))
Colin Cross0cb0d7b2019-07-11 10:59:15 -07001206 return c
1207}
1208
Colin Cross758290d2019-02-01 16:42:32 -08001209// String returns the command line.
1210func (c *RuleBuilderCommand) String() string {
Colin Crosscfec40c2019-07-08 17:07:18 -07001211 return c.buf.String()
Colin Cross758290d2019-02-01 16:42:32 -08001212}
Colin Cross1d2cf042019-03-29 15:33:06 -07001213
Colin Crosse16ce362020-11-12 08:29:30 -08001214// RuleBuilderSboxProtoForTests takes the BuildParams for the manifest passed to RuleBuilder.Sbox()
1215// and returns sbox testproto generated by the RuleBuilder.
1216func RuleBuilderSboxProtoForTests(t *testing.T, params TestingBuildParams) *sbox_proto.Manifest {
1217 t.Helper()
1218 content := ContentFromFileRuleForTests(t, params)
1219 manifest := sbox_proto.Manifest{}
1220 err := proto.UnmarshalText(content, &manifest)
1221 if err != nil {
1222 t.Fatalf("failed to unmarshal manifest: %s", err.Error())
1223 }
1224 return &manifest
1225}
1226
Colin Cross1d2cf042019-03-29 15:33:06 -07001227func ninjaNameEscape(s string) string {
1228 b := []byte(s)
1229 escaped := false
1230 for i, c := range b {
1231 valid := (c >= 'a' && c <= 'z') ||
1232 (c >= 'A' && c <= 'Z') ||
1233 (c >= '0' && c <= '9') ||
1234 (c == '_') ||
1235 (c == '-') ||
1236 (c == '.')
1237 if !valid {
1238 b[i] = '_'
1239 escaped = true
1240 }
1241 }
1242 if escaped {
1243 s = string(b)
1244 }
1245 return s
1246}
Colin Cross3d680512020-11-13 16:23:53 -08001247
1248// hashSrcFiles returns a hash of the list of source files. It is used to ensure the command line
1249// or the sbox textproto manifest change even if the input files are not listed on the command line.
1250func hashSrcFiles(srcFiles Paths) string {
1251 h := sha256.New()
1252 srcFileList := strings.Join(srcFiles.Strings(), "\n")
1253 h.Write([]byte(srcFileList))
1254 return fmt.Sprintf("%x", h.Sum(nil))
1255}
Colin Crossf1a035e2020-11-16 17:32:30 -08001256
1257// BuilderContextForTesting returns a BuilderContext for the given config that can be used for tests
1258// that need to call methods that take a BuilderContext.
1259func BuilderContextForTesting(config Config) BuilderContext {
1260 pathCtx := PathContextForTesting(config)
1261 return builderContextForTests{
1262 PathContext: pathCtx,
1263 }
1264}
1265
1266type builderContextForTests struct {
1267 PathContext
1268}
1269
1270func (builderContextForTests) Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule {
1271 return nil
1272}
1273func (builderContextForTests) Build(PackageContext, BuildParams) {}
Colin Crossef972742021-03-12 17:24:45 -08001274
Colin Crosse55bd422021-03-23 13:44:30 -07001275func writeRspFileRule(ctx BuilderContext, rspFile WritablePath, paths Paths) {
1276 buf := &strings.Builder{}
1277 err := response.WriteRspFile(buf, paths.Strings())
1278 if err != nil {
1279 // There should never be I/O errors writing to a bytes.Buffer.
1280 panic(err)
Colin Crossef972742021-03-12 17:24:45 -08001281 }
Colin Crosse55bd422021-03-23 13:44:30 -07001282 WriteFileRule(ctx, rspFile, buf.String())
Colin Crossef972742021-03-12 17:24:45 -08001283}