blob: 95e2b92f621526d6ca9eb6f5ff5f11dd99be9cad [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
25 "github.com/google/blueprint"
26 "github.com/google/blueprint/proptools"
Dan Willemsen4591b642021-05-24 14:24:12 -070027 "google.golang.org/protobuf/encoding/prototext"
28 "google.golang.org/protobuf/proto"
Dan Willemsen633c5022019-04-12 11:11:38 -070029
Colin Crosse16ce362020-11-12 08:29:30 -080030 "android/soong/cmd/sbox/sbox_proto"
Colin Crossef972742021-03-12 17:24:45 -080031 "android/soong/remoteexec"
Colin Crosse55bd422021-03-23 13:44:30 -070032 "android/soong/response"
Dan Willemsen633c5022019-04-12 11:11:38 -070033 "android/soong/shared"
Colin Crossfeec25b2019-01-30 17:32:39 -080034)
35
Colin Crosse16ce362020-11-12 08:29:30 -080036const sboxSandboxBaseDir = "__SBOX_SANDBOX_DIR__"
37const sboxOutSubDir = "out"
Colin Crossba9e4032020-11-24 16:32:22 -080038const sboxToolsSubDir = "tools"
Colin Crosse16ce362020-11-12 08:29:30 -080039const sboxOutDir = sboxSandboxBaseDir + "/" + sboxOutSubDir
Colin Cross3d680512020-11-13 16:23:53 -080040
Colin Cross758290d2019-02-01 16:42:32 -080041// RuleBuilder provides an alternative to ModuleContext.Rule and ModuleContext.Build to add a command line to the build
42// graph.
Colin Crossfeec25b2019-01-30 17:32:39 -080043type RuleBuilder struct {
Colin Crossf1a035e2020-11-16 17:32:30 -080044 pctx PackageContext
45 ctx BuilderContext
46
Colin Crosse16ce362020-11-12 08:29:30 -080047 commands []*RuleBuilderCommand
48 installs RuleBuilderInstalls
49 temporariesSet map[WritablePath]bool
50 restat bool
51 sbox bool
52 highmem bool
53 remoteable RemoteRuleSupports
Colin Crossef972742021-03-12 17:24:45 -080054 rbeParams *remoteexec.REParams
Colin Crossf1a035e2020-11-16 17:32:30 -080055 outDir WritablePath
Spandan Dasaf4ccaa2023-06-29 01:15:51 +000056 sboxOutSubDir string
Colin Crossba9e4032020-11-24 16:32:22 -080057 sboxTools bool
Colin Crossab020a72021-03-12 17:52:23 -080058 sboxInputs bool
Colin Crosse16ce362020-11-12 08:29:30 -080059 sboxManifestPath WritablePath
60 missingDeps []string
Devin Mooreb6cc64f2024-07-15 22:31:24 +000061 args map[string]string
Colin Crossfeec25b2019-01-30 17:32:39 -080062}
63
Colin Cross758290d2019-02-01 16:42:32 -080064// NewRuleBuilder returns a newly created RuleBuilder.
Colin Crossf1a035e2020-11-16 17:32:30 -080065func NewRuleBuilder(pctx PackageContext, ctx BuilderContext) *RuleBuilder {
Colin Cross5cb5b092019-02-02 21:25:18 -080066 return &RuleBuilder{
Colin Crossf1a035e2020-11-16 17:32:30 -080067 pctx: pctx,
68 ctx: ctx,
Colin Cross69f59a32019-02-15 10:39:37 -080069 temporariesSet: make(map[WritablePath]bool),
Spandan Dasaf4ccaa2023-06-29 01:15:51 +000070 sboxOutSubDir: sboxOutSubDir,
Colin Cross5cb5b092019-02-02 21:25:18 -080071 }
Colin Cross758290d2019-02-01 16:42:32 -080072}
73
Spandan Dasaf4ccaa2023-06-29 01:15:51 +000074// SetSboxOutDirDirAsEmpty sets the out subdirectory to an empty string
75// This is useful for sandboxing actions that change the execution root to a path in out/ (e.g mixed builds)
76// For such actions, SetSboxOutDirDirAsEmpty ensures that the path does not become $SBOX_SANDBOX_DIR/out/out/bazel/output/execroot/__main__/...
77func (rb *RuleBuilder) SetSboxOutDirDirAsEmpty() *RuleBuilder {
78 rb.sboxOutSubDir = ""
79 return rb
80}
81
Devin Mooreb6cc64f2024-07-15 22:31:24 +000082// Set the phony_output argument.
83// This causes the output files to be ignored.
84// If the output isn't created, it's not treated as an error.
85// The build rule is run every time whether or not the output is created.
86func (rb *RuleBuilder) SetPhonyOutput() {
87 if rb.args == nil {
88 rb.args = make(map[string]string)
89 }
90 rb.args["phony_output"] = "true"
91}
92
Colin Cross758290d2019-02-01 16:42:32 -080093// RuleBuilderInstall is a tuple of install from and to locations.
94type RuleBuilderInstall struct {
Colin Cross69f59a32019-02-15 10:39:37 -080095 From Path
96 To string
Colin Cross758290d2019-02-01 16:42:32 -080097}
98
Colin Crossdeabb942019-02-11 14:11:09 -080099type RuleBuilderInstalls []RuleBuilderInstall
100
101// String returns the RuleBuilderInstalls in the form used by $(call copy-many-files) in Make, a space separated
102// list of from:to tuples.
103func (installs RuleBuilderInstalls) String() string {
104 sb := strings.Builder{}
105 for i, install := range installs {
106 if i != 0 {
107 sb.WriteRune(' ')
108 }
Colin Cross69f59a32019-02-15 10:39:37 -0800109 sb.WriteString(install.From.String())
Colin Crossdeabb942019-02-11 14:11:09 -0800110 sb.WriteRune(':')
111 sb.WriteString(install.To)
112 }
113 return sb.String()
114}
115
Colin Cross0d2f40a2019-02-05 22:31:15 -0800116// MissingDeps adds modules to the list of missing dependencies. If MissingDeps
117// is called with a non-empty input, any call to Build will result in a rule
118// that will print an error listing the missing dependencies and fail.
119// MissingDeps should only be called if Config.AllowMissingDependencies() is
120// true.
121func (r *RuleBuilder) MissingDeps(missingDeps []string) {
122 r.missingDeps = append(r.missingDeps, missingDeps...)
123}
124
Colin Cross758290d2019-02-01 16:42:32 -0800125// Restat marks the rule as a restat rule, which will be passed to ModuleContext.Rule in BuildParams.Restat.
Colin Crossfeec25b2019-01-30 17:32:39 -0800126func (r *RuleBuilder) Restat() *RuleBuilder {
127 r.restat = true
128 return r
129}
130
Colin Cross8b8bec32019-11-15 13:18:43 -0800131// HighMem marks the rule as a high memory rule, which will limit how many run in parallel with other high memory
132// rules.
133func (r *RuleBuilder) HighMem() *RuleBuilder {
134 r.highmem = true
135 return r
136}
137
138// Remoteable marks the rule as supporting remote execution.
139func (r *RuleBuilder) Remoteable(supports RemoteRuleSupports) *RuleBuilder {
140 r.remoteable = supports
141 return r
142}
143
Colin Crossef972742021-03-12 17:24:45 -0800144// Rewrapper marks the rule as running inside rewrapper using the given params in order to support
145// running on RBE. During RuleBuilder.Build the params will be combined with the inputs, outputs
146// and tools known to RuleBuilder to prepend an appropriate rewrapper command line to the rule's
147// command line.
148func (r *RuleBuilder) Rewrapper(params *remoteexec.REParams) *RuleBuilder {
149 if !r.sboxInputs {
150 panic(fmt.Errorf("RuleBuilder.Rewrapper must be called after RuleBuilder.SandboxInputs"))
151 }
152 r.rbeParams = params
153 return r
154}
155
Colin Crosse16ce362020-11-12 08:29:30 -0800156// Sbox marks the rule as needing to be wrapped by sbox. The outputDir should point to the output
157// directory that sbox will wipe. It should not be written to by any other rule. manifestPath should
158// point to a location where sbox's manifest will be written and must be outside outputDir. sbox
159// will ensure that all outputs have been written, and will discard any output files that were not
160// specified.
Colin Crosse16ce362020-11-12 08:29:30 -0800161func (r *RuleBuilder) Sbox(outputDir WritablePath, manifestPath WritablePath) *RuleBuilder {
Dan Willemsen633c5022019-04-12 11:11:38 -0700162 if r.sbox {
163 panic("Sbox() may not be called more than once")
164 }
165 if len(r.commands) > 0 {
166 panic("Sbox() may not be called after Command()")
167 }
Dan Willemsen633c5022019-04-12 11:11:38 -0700168 r.sbox = true
Colin Crossf1a035e2020-11-16 17:32:30 -0800169 r.outDir = outputDir
Colin Crosse16ce362020-11-12 08:29:30 -0800170 r.sboxManifestPath = manifestPath
Dan Willemsen633c5022019-04-12 11:11:38 -0700171 return r
172}
173
Colin Crossba9e4032020-11-24 16:32:22 -0800174// SandboxTools enables tool sandboxing for the rule by copying any referenced tools into the
175// sandbox.
176func (r *RuleBuilder) SandboxTools() *RuleBuilder {
177 if !r.sbox {
178 panic("SandboxTools() must be called after Sbox()")
179 }
180 if len(r.commands) > 0 {
181 panic("SandboxTools() may not be called after Command()")
182 }
183 r.sboxTools = true
184 return r
185}
186
Colin Crossab020a72021-03-12 17:52:23 -0800187// SandboxInputs enables input sandboxing for the rule by copying any referenced inputs into the
188// sandbox. It also implies SandboxTools().
189//
190// Sandboxing inputs requires RuleBuilder to be aware of all references to input paths. Paths
191// that are passed to RuleBuilder outside of the methods that expect inputs, for example
192// FlagWithArg, must use RuleBuilderCommand.PathForInput to translate the path to one that matches
193// the sandbox layout.
194func (r *RuleBuilder) SandboxInputs() *RuleBuilder {
195 if !r.sbox {
196 panic("SandboxInputs() must be called after Sbox()")
197 }
198 if len(r.commands) > 0 {
199 panic("SandboxInputs() may not be called after Command()")
200 }
201 r.sboxTools = true
202 r.sboxInputs = true
203 return r
204}
205
Colin Cross758290d2019-02-01 16:42:32 -0800206// Install associates an output of the rule with an install location, which can be retrieved later using
207// RuleBuilder.Installs.
Colin Cross69f59a32019-02-15 10:39:37 -0800208func (r *RuleBuilder) Install(from Path, to string) {
Colin Crossfeec25b2019-01-30 17:32:39 -0800209 r.installs = append(r.installs, RuleBuilderInstall{from, to})
210}
211
Colin Cross758290d2019-02-01 16:42:32 -0800212// Command returns a new RuleBuilderCommand for the rule. The commands will be ordered in the rule by when they were
213// created by this method. That can be mutated through their methods in any order, as long as the mutations do not
214// race with any call to Build.
Colin Crossfeec25b2019-01-30 17:32:39 -0800215func (r *RuleBuilder) Command() *RuleBuilderCommand {
Dan Willemsen633c5022019-04-12 11:11:38 -0700216 command := &RuleBuilderCommand{
Colin Crossf1a035e2020-11-16 17:32:30 -0800217 rule: r,
Dan Willemsen633c5022019-04-12 11:11:38 -0700218 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800219 r.commands = append(r.commands, command)
220 return command
221}
222
Colin Cross5cb5b092019-02-02 21:25:18 -0800223// Temporary marks an output of a command as an intermediate file that will be used as an input to another command
224// in the same rule, and should not be listed in Outputs.
Colin Cross69f59a32019-02-15 10:39:37 -0800225func (r *RuleBuilder) Temporary(path WritablePath) {
Colin Cross5cb5b092019-02-02 21:25:18 -0800226 r.temporariesSet[path] = true
227}
228
229// DeleteTemporaryFiles adds a command to the rule that deletes any outputs that have been marked using Temporary
230// when the rule runs. DeleteTemporaryFiles should be called after all calls to Temporary.
231func (r *RuleBuilder) DeleteTemporaryFiles() {
Colin Cross69f59a32019-02-15 10:39:37 -0800232 var temporariesList WritablePaths
Colin Cross5cb5b092019-02-02 21:25:18 -0800233
234 for intermediate := range r.temporariesSet {
235 temporariesList = append(temporariesList, intermediate)
236 }
Colin Cross69f59a32019-02-15 10:39:37 -0800237
238 sort.Slice(temporariesList, func(i, j int) bool {
239 return temporariesList[i].String() < temporariesList[j].String()
240 })
Colin Cross5cb5b092019-02-02 21:25:18 -0800241
242 r.Command().Text("rm").Flag("-f").Outputs(temporariesList)
243}
244
Colin Crossda71eda2020-02-21 16:55:19 -0800245// Inputs returns the list of paths that were passed to the RuleBuilderCommand methods that take
Colin Cross3d680512020-11-13 16:23:53 -0800246// input paths, such as RuleBuilderCommand.Input, RuleBuilderCommand.Implicit, or
Colin Crossda71eda2020-02-21 16:55:19 -0800247// RuleBuilderCommand.FlagWithInput. Inputs to a command that are also outputs of another command
248// in the same RuleBuilder are filtered out. The list is sorted and duplicates removed.
Colin Cross69f59a32019-02-15 10:39:37 -0800249func (r *RuleBuilder) Inputs() Paths {
Colin Crossfeec25b2019-01-30 17:32:39 -0800250 outputs := r.outputSet()
Dan Willemsen633c5022019-04-12 11:11:38 -0700251 depFiles := r.depFileSet()
Colin Crossfeec25b2019-01-30 17:32:39 -0800252
Colin Cross69f59a32019-02-15 10:39:37 -0800253 inputs := make(map[string]Path)
Colin Crossfeec25b2019-01-30 17:32:39 -0800254 for _, c := range r.commands {
Ramy Medhat2f99eec2020-06-13 17:38:27 -0400255 for _, input := range append(c.inputs, c.implicits...) {
Dan Willemsen633c5022019-04-12 11:11:38 -0700256 inputStr := input.String()
257 if _, isOutput := outputs[inputStr]; !isOutput {
258 if _, isDepFile := depFiles[inputStr]; !isDepFile {
259 inputs[input.String()] = input
260 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800261 }
262 }
263 }
264
Colin Cross69f59a32019-02-15 10:39:37 -0800265 var inputList Paths
266 for _, input := range inputs {
Colin Crossfeec25b2019-01-30 17:32:39 -0800267 inputList = append(inputList, input)
268 }
Colin Cross69f59a32019-02-15 10:39:37 -0800269
270 sort.Slice(inputList, func(i, j int) bool {
271 return inputList[i].String() < inputList[j].String()
272 })
Colin Crossfeec25b2019-01-30 17:32:39 -0800273
274 return inputList
275}
276
Colin Crossda71eda2020-02-21 16:55:19 -0800277// OrderOnlys returns the list of paths that were passed to the RuleBuilderCommand.OrderOnly or
278// RuleBuilderCommand.OrderOnlys. The list is sorted and duplicates removed.
279func (r *RuleBuilder) OrderOnlys() Paths {
280 orderOnlys := make(map[string]Path)
281 for _, c := range r.commands {
282 for _, orderOnly := range c.orderOnlys {
283 orderOnlys[orderOnly.String()] = orderOnly
284 }
285 }
286
287 var orderOnlyList Paths
288 for _, orderOnly := range orderOnlys {
289 orderOnlyList = append(orderOnlyList, orderOnly)
290 }
291
292 sort.Slice(orderOnlyList, func(i, j int) bool {
293 return orderOnlyList[i].String() < orderOnlyList[j].String()
294 })
295
296 return orderOnlyList
297}
298
Colin Crossae89abe2021-04-21 11:45:23 -0700299// Validations returns the list of paths that were passed to RuleBuilderCommand.Validation or
300// RuleBuilderCommand.Validations. The list is sorted and duplicates removed.
301func (r *RuleBuilder) Validations() Paths {
302 validations := make(map[string]Path)
303 for _, c := range r.commands {
304 for _, validation := range c.validations {
305 validations[validation.String()] = validation
306 }
307 }
308
309 var validationList Paths
310 for _, validation := range validations {
311 validationList = append(validationList, validation)
312 }
313
314 sort.Slice(validationList, func(i, j int) bool {
315 return validationList[i].String() < validationList[j].String()
316 })
317
318 return validationList
319}
320
Colin Cross69f59a32019-02-15 10:39:37 -0800321func (r *RuleBuilder) outputSet() map[string]WritablePath {
322 outputs := make(map[string]WritablePath)
Colin Crossfeec25b2019-01-30 17:32:39 -0800323 for _, c := range r.commands {
324 for _, output := range c.outputs {
Colin Cross69f59a32019-02-15 10:39:37 -0800325 outputs[output.String()] = output
Colin Crossfeec25b2019-01-30 17:32:39 -0800326 }
327 }
328 return outputs
329}
330
Colin Crossda71eda2020-02-21 16:55:19 -0800331// Outputs returns the list of paths that were passed to the RuleBuilderCommand methods that take
332// output paths, such as RuleBuilderCommand.Output, RuleBuilderCommand.ImplicitOutput, or
333// RuleBuilderCommand.FlagWithInput. The list is sorted and duplicates removed.
Colin Cross69f59a32019-02-15 10:39:37 -0800334func (r *RuleBuilder) Outputs() WritablePaths {
Colin Crossfeec25b2019-01-30 17:32:39 -0800335 outputs := r.outputSet()
336
Colin Cross69f59a32019-02-15 10:39:37 -0800337 var outputList WritablePaths
338 for _, output := range outputs {
Colin Cross5cb5b092019-02-02 21:25:18 -0800339 if !r.temporariesSet[output] {
340 outputList = append(outputList, output)
341 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800342 }
Colin Cross69f59a32019-02-15 10:39:37 -0800343
344 sort.Slice(outputList, func(i, j int) bool {
345 return outputList[i].String() < outputList[j].String()
346 })
347
Colin Crossfeec25b2019-01-30 17:32:39 -0800348 return outputList
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 Cross9b698b62021-12-22 09:55:32 -0800450 builtToolWithoutDeps("dep_fixer").
Dan Willemsen633c5022019-04-12 11:11:38 -0700451 Inputs(depFiles.Paths())
Colin Cross1d2cf042019-03-29 15:33:06 -0700452}
453
Sam Delmerico285b66a2023-09-25 12:13:17 +0000454// BuildWithNinjaVars adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for
455// Outputs. This function will not escape Ninja variables, so it may be used to write sandbox manifests using Ninja variables.
456func (r *RuleBuilder) BuildWithUnescapedNinjaVars(name string, desc string) {
457 r.build(name, desc, false)
458}
459
Colin Cross758290d2019-02-01 16:42:32 -0800460// Build adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for
461// Outputs.
Colin Crossf1a035e2020-11-16 17:32:30 -0800462func (r *RuleBuilder) Build(name string, desc string) {
Sam Delmerico285b66a2023-09-25 12:13:17 +0000463 r.build(name, desc, true)
464}
465
466func (r *RuleBuilder) build(name string, desc string, ninjaEscapeCommandString bool) {
Colin Cross1d2cf042019-03-29 15:33:06 -0700467 name = ninjaNameEscape(name)
468
Colin Cross0d2f40a2019-02-05 22:31:15 -0800469 if len(r.missingDeps) > 0 {
Sam Delmerico285b66a2023-09-25 12:13:17 +0000470 r.ctx.Build(r.pctx, BuildParams{
Colin Cross0d2f40a2019-02-05 22:31:15 -0800471 Rule: ErrorRule,
Colin Cross69f59a32019-02-15 10:39:37 -0800472 Outputs: r.Outputs(),
Colin Cross0d2f40a2019-02-05 22:31:15 -0800473 Description: desc,
474 Args: map[string]string{
475 "error": "missing dependencies: " + strings.Join(r.missingDeps, ", "),
476 },
477 })
478 return
479 }
480
Colin Cross1d2cf042019-03-29 15:33:06 -0700481 var depFile WritablePath
482 var depFormat blueprint.Deps
483 if depFiles := r.DepFiles(); len(depFiles) > 0 {
484 depFile = depFiles[0]
485 depFormat = blueprint.DepsGCC
486 if len(depFiles) > 1 {
487 // Add a command locally that merges all depfiles together into the first depfile.
Colin Crossf1a035e2020-11-16 17:32:30 -0800488 r.depFileMergerCmd(depFiles)
Dan Willemsen633c5022019-04-12 11:11:38 -0700489
490 if r.sbox {
Colin Crosse16ce362020-11-12 08:29:30 -0800491 // Check for Rel() errors, as all depfiles should be in the output dir. Errors
492 // will be reported to the ctx.
Dan Willemsen633c5022019-04-12 11:11:38 -0700493 for _, path := range depFiles[1:] {
Colin Crossf1a035e2020-11-16 17:32:30 -0800494 Rel(r.ctx, r.outDir.String(), path.String())
Dan Willemsen633c5022019-04-12 11:11:38 -0700495 }
496 }
Colin Cross1d2cf042019-03-29 15:33:06 -0700497 }
498 }
499
Dan Willemsen633c5022019-04-12 11:11:38 -0700500 tools := r.Tools()
Colin Crossb70a1a92021-03-12 17:51:32 -0800501 commands := r.Commands()
Dan Willemsen633c5022019-04-12 11:11:38 -0700502 outputs := r.Outputs()
Colin Cross3d680512020-11-13 16:23:53 -0800503 inputs := r.Inputs()
Colin Crossce3a51d2021-03-19 16:22:12 -0700504 rspFiles := r.rspFiles()
Dan Willemsen633c5022019-04-12 11:11:38 -0700505
506 if len(commands) == 0 {
507 return
508 }
509 if len(outputs) == 0 {
510 panic("No outputs specified from any Commands")
511 }
512
Colin Cross0cb0d7b2019-07-11 10:59:15 -0700513 commandString := strings.Join(commands, " && ")
Dan Willemsen633c5022019-04-12 11:11:38 -0700514
515 if r.sbox {
Colin Crosse16ce362020-11-12 08:29:30 -0800516 // If running the command inside sbox, write the rule data out to an sbox
517 // manifest.textproto.
518 manifest := sbox_proto.Manifest{}
519 command := sbox_proto.Command{}
520 manifest.Commands = append(manifest.Commands, &command)
521 command.Command = proto.String(commandString)
Colin Cross151b9ff2020-11-12 08:29:30 -0800522
Colin Cross619b9ab2020-11-20 18:44:31 +0000523 if depFile != nil {
Colin Crosse16ce362020-11-12 08:29:30 -0800524 manifest.OutputDepfile = proto.String(depFile.String())
Colin Cross619b9ab2020-11-20 18:44:31 +0000525 }
526
Colin Crossba9e4032020-11-24 16:32:22 -0800527 // If sandboxing tools is enabled, add copy rules to the manifest to copy each tool
528 // into the sbox directory.
529 if r.sboxTools {
530 for _, tool := range tools {
531 command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{
532 From: proto.String(tool.String()),
533 To: proto.String(sboxPathForToolRel(r.ctx, tool)),
534 })
535 }
536 for _, c := range r.commands {
537 for _, tool := range c.packagedTools {
538 command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{
539 From: proto.String(tool.srcPath.String()),
540 To: proto.String(sboxPathForPackagedToolRel(tool)),
541 Executable: proto.Bool(tool.executable),
542 })
543 tools = append(tools, tool.srcPath)
544 }
545 }
546 }
547
Colin Crossab020a72021-03-12 17:52:23 -0800548 // If sandboxing inputs is enabled, add copy rules to the manifest to copy each input
549 // into the sbox directory.
550 if r.sboxInputs {
551 for _, input := range inputs {
552 command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{
553 From: proto.String(input.String()),
554 To: proto.String(r.sboxPathForInputRel(input)),
555 })
556 }
Cole Faust78f3c3a2024-08-15 17:19:34 -0700557 for _, input := range r.OrderOnlys() {
558 command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{
559 From: proto.String(input.String()),
560 To: proto.String(r.sboxPathForInputRel(input)),
561 })
562 }
Colin Crossab020a72021-03-12 17:52:23 -0800563
Colin Crossce3a51d2021-03-19 16:22:12 -0700564 // If using rsp files copy them and their contents into the sbox directory with
565 // the appropriate path mappings.
566 for _, rspFile := range rspFiles {
Colin Crosse55bd422021-03-23 13:44:30 -0700567 command.RspFiles = append(command.RspFiles, &sbox_proto.RspFile{
Colin Crossce3a51d2021-03-19 16:22:12 -0700568 File: proto.String(rspFile.file.String()),
Colin Crosse55bd422021-03-23 13:44:30 -0700569 // These have to match the logic in sboxPathForInputRel
570 PathMappings: []*sbox_proto.PathMapping{
571 {
572 From: proto.String(r.outDir.String()),
573 To: proto.String(sboxOutSubDir),
574 },
575 {
Cole Fauste8561c62023-11-30 17:26:37 -0800576 From: proto.String(r.ctx.Config().OutDir()),
Colin Crosse55bd422021-03-23 13:44:30 -0700577 To: proto.String(sboxOutSubDir),
578 },
579 },
Colin Crossab020a72021-03-12 17:52:23 -0800580 })
581 }
582
Cole Faust1ead86c2024-08-23 14:41:51 -0700583 // Set OUT_DIR to the relative path of the sandboxed out directory.
584 // Otherwise, OUT_DIR will be inherited from the rest of the build,
585 // which will allow scripts to escape the sandbox if OUT_DIR is an
586 // absolute path.
587 command.Env = append(command.Env, &sbox_proto.EnvironmentVariable{
588 Name: proto.String("OUT_DIR"),
589 State: &sbox_proto.EnvironmentVariable_Value{
590 Value: sboxOutSubDir,
591 },
592 })
Colin Crossab020a72021-03-12 17:52:23 -0800593 command.Chdir = proto.Bool(true)
594 }
595
Colin Crosse16ce362020-11-12 08:29:30 -0800596 // Add copy rules to the manifest to copy each output file from the sbox directory.
Colin Crossba9e4032020-11-24 16:32:22 -0800597 // to the output directory after running the commands.
Spandan Das33e30972023-07-13 21:19:12 +0000598 for _, output := range outputs {
Colin Crossf1a035e2020-11-16 17:32:30 -0800599 rel := Rel(r.ctx, r.outDir.String(), output.String())
Colin Crosse16ce362020-11-12 08:29:30 -0800600 command.CopyAfter = append(command.CopyAfter, &sbox_proto.Copy{
Spandan Dasaf4ccaa2023-06-29 01:15:51 +0000601 From: proto.String(filepath.Join(r.sboxOutSubDir, rel)),
Colin Crosse16ce362020-11-12 08:29:30 -0800602 To: proto.String(output.String()),
603 })
604 }
Colin Cross619b9ab2020-11-20 18:44:31 +0000605
Colin Cross5334edd2021-03-11 17:18:21 -0800606 // Outputs that were marked Temporary will not be checked that they are in the output
607 // directory by the loop above, check them here.
608 for path := range r.temporariesSet {
609 Rel(r.ctx, r.outDir.String(), path.String())
610 }
611
Colin Crosse16ce362020-11-12 08:29:30 -0800612 // Add a hash of the list of input files to the manifest so that the textproto file
613 // changes when the list of input files changes and causes the sbox rule that
614 // depends on it to rerun.
615 command.InputHash = proto.String(hashSrcFiles(inputs))
Colin Cross619b9ab2020-11-20 18:44:31 +0000616
Colin Crosse16ce362020-11-12 08:29:30 -0800617 // Verify that the manifest textproto is not inside the sbox output directory, otherwise
618 // it will get deleted when the sbox rule clears its output directory.
Colin Crossf1a035e2020-11-16 17:32:30 -0800619 _, manifestInOutDir := MaybeRel(r.ctx, r.outDir.String(), r.sboxManifestPath.String())
Colin Crosse16ce362020-11-12 08:29:30 -0800620 if manifestInOutDir {
Colin Crossf1a035e2020-11-16 17:32:30 -0800621 ReportPathErrorf(r.ctx, "sbox rule %q manifestPath %q must not be in outputDir %q",
622 name, r.sboxManifestPath.String(), r.outDir.String())
Colin Crosse16ce362020-11-12 08:29:30 -0800623 }
624
Paul Duffin4a3a0a52023-10-12 15:01:29 +0100625 // Create a rule to write the manifest as textproto. Pretty print it by indenting and
626 // splitting across multiple lines.
627 pbText, err := prototext.MarshalOptions{Indent: " "}.Marshal(&manifest)
Dan Willemsen4591b642021-05-24 14:24:12 -0700628 if err != nil {
629 ReportPathErrorf(r.ctx, "sbox manifest failed to marshal: %q", err)
630 }
Sam Delmerico285b66a2023-09-25 12:13:17 +0000631 if ninjaEscapeCommandString {
632 WriteFileRule(r.ctx, r.sboxManifestPath, string(pbText))
633 } else {
634 // We need to have a rule to write files that is
635 // defined on the RuleBuilder's pctx in order to
636 // write Ninja variables in the string.
637 // The WriteFileRule function above rule can only write
638 // raw strings because it is defined on the android
639 // package's pctx, and it can't access variables defined
640 // in another context.
641 r.ctx.Build(r.pctx, BuildParams{
642 Rule: r.ctx.Rule(r.pctx, "unescapedWriteFile", blueprint.RuleParams{
643 Command: `rm -rf ${out} && cat ${out}.rsp > ${out}`,
644 Rspfile: "${out}.rsp",
645 RspfileContent: "${content}",
646 Description: "write file",
647 }, "content"),
648 Output: r.sboxManifestPath,
649 Description: "write sbox manifest " + r.sboxManifestPath.Base(),
650 Args: map[string]string{
651 "content": string(pbText),
652 },
653 })
654 }
Colin Crosse16ce362020-11-12 08:29:30 -0800655
656 // Generate a new string to use as the command line of the sbox rule. This uses
657 // a RuleBuilderCommand as a convenience method of building the command line, then
658 // converts it to a string to replace commandString.
Colin Crossf1a035e2020-11-16 17:32:30 -0800659 sboxCmd := &RuleBuilderCommand{
660 rule: &RuleBuilder{
661 ctx: r.ctx,
662 },
663 }
Colin Cross9b698b62021-12-22 09:55:32 -0800664 sboxCmd.builtToolWithoutDeps("sbox").
Colin Crosse52c2ac2022-03-28 17:03:35 -0700665 FlagWithArg("--sandbox-path ", shared.TempDirForOutDir(PathForOutput(r.ctx).String())).
666 FlagWithArg("--output-dir ", r.outDir.String()).
667 FlagWithInput("--manifest ", r.sboxManifestPath)
668
669 if r.restat {
670 sboxCmd.Flag("--write-if-changed")
671 }
Colin Crosse16ce362020-11-12 08:29:30 -0800672
673 // Replace the command string, and add the sbox tool and manifest textproto to the
674 // dependencies of the final sbox rule.
Colin Crosscfec40c2019-07-08 17:07:18 -0700675 commandString = sboxCmd.buf.String()
Dan Willemsen633c5022019-04-12 11:11:38 -0700676 tools = append(tools, sboxCmd.tools...)
Colin Crosse16ce362020-11-12 08:29:30 -0800677 inputs = append(inputs, sboxCmd.inputs...)
Colin Crossef972742021-03-12 17:24:45 -0800678
679 if r.rbeParams != nil {
Colin Crosse55bd422021-03-23 13:44:30 -0700680 // RBE needs a list of input files to copy to the remote builder. For inputs already
681 // listed in an rsp file, pass the rsp file directly to rewrapper. For the rest,
682 // create a new rsp file to pass to rewrapper.
683 var remoteRspFiles Paths
684 var remoteInputs Paths
685
686 remoteInputs = append(remoteInputs, inputs...)
687 remoteInputs = append(remoteInputs, tools...)
688
Colin Crossce3a51d2021-03-19 16:22:12 -0700689 for _, rspFile := range rspFiles {
690 remoteInputs = append(remoteInputs, rspFile.file)
691 remoteRspFiles = append(remoteRspFiles, rspFile.file)
Colin Crossef972742021-03-12 17:24:45 -0800692 }
Colin Crosse55bd422021-03-23 13:44:30 -0700693
694 if len(remoteInputs) > 0 {
695 inputsListFile := r.sboxManifestPath.ReplaceExtension(r.ctx, "rbe_inputs.list")
696 writeRspFileRule(r.ctx, inputsListFile, remoteInputs)
697 remoteRspFiles = append(remoteRspFiles, inputsListFile)
698 // Add the new rsp file as an extra input to the rule.
699 inputs = append(inputs, inputsListFile)
700 }
Colin Crossef972742021-03-12 17:24:45 -0800701
702 r.rbeParams.OutputFiles = outputs.Strings()
Colin Crosse55bd422021-03-23 13:44:30 -0700703 r.rbeParams.RSPFiles = remoteRspFiles.Strings()
Colin Crossef972742021-03-12 17:24:45 -0800704 rewrapperCommand := r.rbeParams.NoVarTemplate(r.ctx.Config().RBEWrapper())
705 commandString = rewrapperCommand + " bash -c '" + strings.ReplaceAll(commandString, `'`, `'\''`) + "'"
706 }
Colin Cross3d680512020-11-13 16:23:53 -0800707 } else {
708 // If not using sbox the rule will run the command directly, put the hash of the
709 // list of input files in a comment at the end of the command line to ensure ninja
710 // reruns the rule when the list of input files changes.
711 commandString += " # hash of input list: " + hashSrcFiles(inputs)
Dan Willemsen633c5022019-04-12 11:11:38 -0700712 }
713
Colin Cross1d2cf042019-03-29 15:33:06 -0700714 // Ninja doesn't like multiple outputs when depfiles are enabled, move all but the first output to
Colin Cross70c47412021-03-12 17:48:14 -0800715 // ImplicitOutputs. RuleBuilder doesn't use "$out", so the distinction between Outputs and
Colin Cross0cb0d7b2019-07-11 10:59:15 -0700716 // ImplicitOutputs doesn't matter.
Dan Willemsen633c5022019-04-12 11:11:38 -0700717 output := outputs[0]
718 implicitOutputs := outputs[1:]
Colin Cross1d2cf042019-03-29 15:33:06 -0700719
Colin Cross0cb0d7b2019-07-11 10:59:15 -0700720 var rspFile, rspFileContent string
Colin Crossce3a51d2021-03-19 16:22:12 -0700721 var rspFileInputs Paths
722 if len(rspFiles) > 0 {
723 // The first rsp files uses Ninja's rsp file support for the rule
724 rspFile = rspFiles[0].file.String()
Colin Crosse55bd422021-03-23 13:44:30 -0700725 // Use "$in" for rspFileContent to avoid duplicating the list of files in the dependency
726 // list and in the contents of the rsp file. Inputs to the rule that are not in the
727 // rsp file will be listed in Implicits instead of Inputs so they don't show up in "$in".
728 rspFileContent = "$in"
Colin Crossce3a51d2021-03-19 16:22:12 -0700729 rspFileInputs = append(rspFileInputs, rspFiles[0].paths...)
730
731 for _, rspFile := range rspFiles[1:] {
732 // Any additional rsp files need an extra rule to write the file.
733 writeRspFileRule(r.ctx, rspFile.file, rspFile.paths)
734 // The main rule needs to depend on the inputs listed in the extra rsp file.
735 inputs = append(inputs, rspFile.paths...)
736 // The main rule needs to depend on the extra rsp file.
737 inputs = append(inputs, rspFile.file)
738 }
Colin Cross0cb0d7b2019-07-11 10:59:15 -0700739 }
740
Colin Cross8b8bec32019-11-15 13:18:43 -0800741 var pool blueprint.Pool
Colin Crossf1a035e2020-11-16 17:32:30 -0800742 if r.ctx.Config().UseGoma() && r.remoteable.Goma {
Colin Cross8b8bec32019-11-15 13:18:43 -0800743 // 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 -0800744 } else if r.ctx.Config().UseRBE() && r.remoteable.RBE {
Ramy Medhat944839a2020-03-31 22:14:52 -0400745 // When USE_RBE=true is set and the rule is supported by RBE, use the remotePool.
746 pool = remotePool
Colin Cross8b8bec32019-11-15 13:18:43 -0800747 } else if r.highmem {
748 pool = highmemPool
Colin Crossf1a035e2020-11-16 17:32:30 -0800749 } else if r.ctx.Config().UseRemoteBuild() {
Colin Cross8b8bec32019-11-15 13:18:43 -0800750 pool = localPool
751 }
752
Sam Delmericod46f6c82023-09-25 12:13:17 +0000753 if ninjaEscapeCommandString {
754 commandString = proptools.NinjaEscape(commandString)
755 }
756
Devin Mooreb6cc64f2024-07-15 22:31:24 +0000757 args_vars := make([]string, len(r.args))
758 i := 0
759 for k, _ := range r.args {
760 args_vars[i] = k
761 i++
762 }
Colin Crossf1a035e2020-11-16 17:32:30 -0800763 r.ctx.Build(r.pctx, BuildParams{
Sam Delmerico285b66a2023-09-25 12:13:17 +0000764 Rule: r.ctx.Rule(r.pctx, name, blueprint.RuleParams{
Sam Delmericod46f6c82023-09-25 12:13:17 +0000765 Command: commandString,
Colin Cross45029782021-03-16 16:49:52 -0700766 CommandDeps: proptools.NinjaEscapeList(tools.Strings()),
Colin Cross0cb0d7b2019-07-11 10:59:15 -0700767 Restat: r.restat,
Colin Cross45029782021-03-16 16:49:52 -0700768 Rspfile: proptools.NinjaEscape(rspFile),
Colin Cross0cb0d7b2019-07-11 10:59:15 -0700769 RspfileContent: rspFileContent,
Colin Cross8b8bec32019-11-15 13:18:43 -0800770 Pool: pool,
Devin Mooreb6cc64f2024-07-15 22:31:24 +0000771 }, args_vars...),
Colin Cross0cb0d7b2019-07-11 10:59:15 -0700772 Inputs: rspFileInputs,
Colin Cross3d680512020-11-13 16:23:53 -0800773 Implicits: inputs,
Colin Crossda6401b2021-04-21 11:32:19 -0700774 OrderOnly: r.OrderOnlys(),
Colin Crossae89abe2021-04-21 11:45:23 -0700775 Validations: r.Validations(),
Dan Willemsen633c5022019-04-12 11:11:38 -0700776 Output: output,
777 ImplicitOutputs: implicitOutputs,
778 Depfile: depFile,
779 Deps: depFormat,
780 Description: desc,
Devin Mooreb6cc64f2024-07-15 22:31:24 +0000781 Args: r.args,
Dan Willemsen633c5022019-04-12 11:11:38 -0700782 })
Colin Crossfeec25b2019-01-30 17:32:39 -0800783}
784
Colin Cross758290d2019-02-01 16:42:32 -0800785// RuleBuilderCommand is a builder for a command in a command line. It can be mutated by its methods to add to the
786// command and track dependencies. The methods mutate the RuleBuilderCommand in place, as well as return the
787// RuleBuilderCommand, so they can be used chained or unchained. All methods that add text implicitly add a single
788// space as a separator from the previous method.
Colin Crossfeec25b2019-01-30 17:32:39 -0800789type RuleBuilderCommand struct {
Colin Crossf1a035e2020-11-16 17:32:30 -0800790 rule *RuleBuilder
791
Cole Faust9a346f62024-01-18 20:12:02 +0000792 buf strings.Builder
793 inputs Paths
794 implicits Paths
795 orderOnlys Paths
796 validations Paths
797 outputs WritablePaths
798 depFiles WritablePaths
799 tools Paths
800 packagedTools []PackagingSpec
801 rspFiles []rspFileAndPaths
Colin Crossce3a51d2021-03-19 16:22:12 -0700802}
803
804type rspFileAndPaths struct {
805 file WritablePath
806 paths Paths
Dan Willemsen633c5022019-04-12 11:11:38 -0700807}
808
Paul Duffin3866b892021-10-04 11:24:48 +0100809func checkPathNotNil(path Path) {
810 if path == nil {
811 panic("rule_builder paths cannot be nil")
812 }
813}
814
Dan Willemsen633c5022019-04-12 11:11:38 -0700815func (c *RuleBuilderCommand) addInput(path Path) string {
Paul Duffin3866b892021-10-04 11:24:48 +0100816 checkPathNotNil(path)
Dan Willemsen633c5022019-04-12 11:11:38 -0700817 c.inputs = append(c.inputs, path)
Colin Crossab020a72021-03-12 17:52:23 -0800818 return c.PathForInput(path)
Dan Willemsen633c5022019-04-12 11:11:38 -0700819}
820
Colin Crossab020a72021-03-12 17:52:23 -0800821func (c *RuleBuilderCommand) addImplicit(path Path) {
Paul Duffin3866b892021-10-04 11:24:48 +0100822 checkPathNotNil(path)
Ramy Medhat2f99eec2020-06-13 17:38:27 -0400823 c.implicits = append(c.implicits, path)
Ramy Medhat2f99eec2020-06-13 17:38:27 -0400824}
825
Colin Crossda71eda2020-02-21 16:55:19 -0800826func (c *RuleBuilderCommand) addOrderOnly(path Path) {
Paul Duffin3866b892021-10-04 11:24:48 +0100827 checkPathNotNil(path)
Colin Crossda71eda2020-02-21 16:55:19 -0800828 c.orderOnlys = append(c.orderOnlys, path)
829}
830
Colin Crossab020a72021-03-12 17:52:23 -0800831// PathForInput takes an input path and returns the appropriate path to use on the command line. If
832// sbox was enabled via a call to RuleBuilder.Sbox() and the path was an output path it returns a
833// path with the placeholder prefix used for outputs in sbox. If sbox is not enabled it returns the
834// original path.
835func (c *RuleBuilderCommand) PathForInput(path Path) string {
836 if c.rule.sbox {
837 rel, inSandbox := c.rule._sboxPathForInputRel(path)
838 if inSandbox {
839 rel = filepath.Join(sboxSandboxBaseDir, rel)
840 }
841 return rel
842 }
843 return path.String()
844}
845
846// PathsForInputs takes a list of input paths and returns the appropriate paths to use on the
847// command line. If sbox was enabled via a call to RuleBuilder.Sbox() a path was an output path, it
848// returns the path with the placeholder prefix used for outputs in sbox. If sbox is not enabled it
849// returns the original paths.
850func (c *RuleBuilderCommand) PathsForInputs(paths Paths) []string {
851 ret := make([]string, len(paths))
852 for i, path := range paths {
853 ret[i] = c.PathForInput(path)
854 }
855 return ret
856}
857
Colin Crossf1a035e2020-11-16 17:32:30 -0800858// PathForOutput takes an output path and returns the appropriate path to use on the command
859// line. If sbox was enabled via a call to RuleBuilder.Sbox(), it returns a path with the
860// placeholder prefix used for outputs in sbox. If sbox is not enabled it returns the
861// original path.
862func (c *RuleBuilderCommand) PathForOutput(path WritablePath) string {
863 if c.rule.sbox {
864 // Errors will be handled in RuleBuilder.Build where we have a context to report them
865 rel, _, _ := maybeRelErr(c.rule.outDir.String(), path.String())
866 return filepath.Join(sboxOutDir, rel)
Dan Willemsen633c5022019-04-12 11:11:38 -0700867 }
868 return path.String()
Colin Crossfeec25b2019-01-30 17:32:39 -0800869}
870
Colin Crossba9e4032020-11-24 16:32:22 -0800871func sboxPathForToolRel(ctx BuilderContext, path Path) string {
872 // Errors will be handled in RuleBuilder.Build where we have a context to report them
Cole Faust3b703f32023-10-16 13:30:51 -0700873 toolDir := pathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "")
Colin Cross790ef352021-10-25 19:15:55 -0700874 relOutSoong, isRelOutSoong, _ := maybeRelErr(toolDir.String(), path.String())
875 if isRelOutSoong {
876 // The tool is in the Soong output directory, it will be copied to __SBOX_OUT_DIR__/tools/out
877 return filepath.Join(sboxToolsSubDir, "out", relOutSoong)
Colin Crossba9e4032020-11-24 16:32:22 -0800878 }
879 // The tool is in the source directory, it will be copied to __SBOX_OUT_DIR__/tools/src
880 return filepath.Join(sboxToolsSubDir, "src", path.String())
881}
882
Colin Crossab020a72021-03-12 17:52:23 -0800883func (r *RuleBuilder) _sboxPathForInputRel(path Path) (rel string, inSandbox bool) {
884 // Errors will be handled in RuleBuilder.Build where we have a context to report them
885 rel, isRelSboxOut, _ := maybeRelErr(r.outDir.String(), path.String())
886 if isRelSboxOut {
887 return filepath.Join(sboxOutSubDir, rel), true
888 }
889 if r.sboxInputs {
890 // When sandboxing inputs all inputs have to be copied into the sandbox. Input files that
891 // are outputs of other rules could be an arbitrary absolute path if OUT_DIR is set, so they
892 // will be copied to relative paths under __SBOX_OUT_DIR__/out.
Cole Fauste8561c62023-11-30 17:26:37 -0800893 rel, isRelOut, _ := maybeRelErr(r.ctx.Config().OutDir(), path.String())
Colin Crossab020a72021-03-12 17:52:23 -0800894 if isRelOut {
895 return filepath.Join(sboxOutSubDir, rel), true
896 }
897 }
898 return path.String(), false
899}
900
901func (r *RuleBuilder) sboxPathForInputRel(path Path) string {
902 rel, _ := r._sboxPathForInputRel(path)
903 return rel
904}
905
906func (r *RuleBuilder) sboxPathsForInputsRel(paths Paths) []string {
907 ret := make([]string, len(paths))
908 for i, path := range paths {
909 ret[i] = r.sboxPathForInputRel(path)
910 }
911 return ret
912}
913
Colin Crossba9e4032020-11-24 16:32:22 -0800914func sboxPathForPackagedToolRel(spec PackagingSpec) string {
915 return filepath.Join(sboxToolsSubDir, "out", spec.relPathInPackage)
916}
917
Colin Crossd11cf622021-03-23 22:30:35 -0700918// PathForPackagedTool takes a PackageSpec for a tool and returns the corresponding path for the
919// tool after copying it into the sandbox. This can be used on the RuleBuilder command line to
920// reference the tool.
921func (c *RuleBuilderCommand) PathForPackagedTool(spec PackagingSpec) string {
922 if !c.rule.sboxTools {
923 panic("PathForPackagedTool() requires SandboxTools()")
924 }
925
926 return filepath.Join(sboxSandboxBaseDir, sboxPathForPackagedToolRel(spec))
927}
928
Colin Crossba9e4032020-11-24 16:32:22 -0800929// PathForTool takes a path to a tool, which may be an output file or a source file, and returns
930// the corresponding path for the tool in the sbox sandbox if sbox is enabled, or the original path
931// if it is not. This can be used on the RuleBuilder command line to reference the tool.
932func (c *RuleBuilderCommand) PathForTool(path Path) string {
933 if c.rule.sbox && c.rule.sboxTools {
934 return filepath.Join(sboxSandboxBaseDir, sboxPathForToolRel(c.rule.ctx, path))
935 }
936 return path.String()
937}
938
Colin Crossd11cf622021-03-23 22:30:35 -0700939// PathsForTools takes a list of paths to tools, which may be output files or source files, and
940// returns the corresponding paths for the tools in the sbox sandbox if sbox is enabled, or the
941// original paths if it is not. This can be used on the RuleBuilder command line to reference the tool.
942func (c *RuleBuilderCommand) PathsForTools(paths Paths) []string {
943 if c.rule.sbox && c.rule.sboxTools {
944 var ret []string
945 for _, path := range paths {
946 ret = append(ret, filepath.Join(sboxSandboxBaseDir, sboxPathForToolRel(c.rule.ctx, path)))
947 }
948 return ret
949 }
950 return paths.Strings()
951}
952
Colin Crossba9e4032020-11-24 16:32:22 -0800953// PackagedTool adds the specified tool path to the command line. It can only be used with tool
954// sandboxing enabled by SandboxTools(), and will copy the tool into the sandbox.
955func (c *RuleBuilderCommand) PackagedTool(spec PackagingSpec) *RuleBuilderCommand {
956 if !c.rule.sboxTools {
957 panic("PackagedTool() requires SandboxTools()")
958 }
959
960 c.packagedTools = append(c.packagedTools, spec)
961 c.Text(sboxPathForPackagedToolRel(spec))
962 return c
963}
964
965// ImplicitPackagedTool copies the specified tool into the sandbox without modifying the command
966// line. It can only be used with tool sandboxing enabled by SandboxTools().
967func (c *RuleBuilderCommand) ImplicitPackagedTool(spec PackagingSpec) *RuleBuilderCommand {
968 if !c.rule.sboxTools {
969 panic("ImplicitPackagedTool() requires SandboxTools()")
970 }
971
972 c.packagedTools = append(c.packagedTools, spec)
973 return c
974}
975
976// ImplicitPackagedTools copies the specified tools into the sandbox without modifying the command
977// line. It can only be used with tool sandboxing enabled by SandboxTools().
978func (c *RuleBuilderCommand) ImplicitPackagedTools(specs []PackagingSpec) *RuleBuilderCommand {
979 if !c.rule.sboxTools {
980 panic("ImplicitPackagedTools() requires SandboxTools()")
981 }
982
983 c.packagedTools = append(c.packagedTools, specs...)
984 return c
985}
986
Colin Cross758290d2019-02-01 16:42:32 -0800987// Text adds the specified raw text to the command line. The text should not contain input or output paths or the
988// rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800989func (c *RuleBuilderCommand) Text(text string) *RuleBuilderCommand {
Colin Crosscfec40c2019-07-08 17:07:18 -0700990 if c.buf.Len() > 0 {
991 c.buf.WriteByte(' ')
Colin Crossfeec25b2019-01-30 17:32:39 -0800992 }
Colin Crosscfec40c2019-07-08 17:07:18 -0700993 c.buf.WriteString(text)
Colin Crossfeec25b2019-01-30 17:32:39 -0800994 return c
995}
996
Colin Cross758290d2019-02-01 16:42:32 -0800997// Textf adds the specified formatted text to the command line. The text should not contain input or output paths or
998// the rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -0800999func (c *RuleBuilderCommand) Textf(format string, a ...interface{}) *RuleBuilderCommand {
1000 return c.Text(fmt.Sprintf(format, a...))
1001}
1002
Colin Cross758290d2019-02-01 16:42:32 -08001003// Flag adds the specified raw text to the command line. The text should not contain input or output paths or the
1004// rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -08001005func (c *RuleBuilderCommand) Flag(flag string) *RuleBuilderCommand {
1006 return c.Text(flag)
1007}
1008
Colin Crossab054432019-07-15 16:13:59 -07001009// OptionalFlag adds the specified raw text to the command line if it is not nil. The text should not contain input or
1010// output paths or the rule will not have them listed in its dependencies or outputs.
1011func (c *RuleBuilderCommand) OptionalFlag(flag *string) *RuleBuilderCommand {
1012 if flag != nil {
1013 c.Text(*flag)
1014 }
1015
1016 return c
1017}
1018
Colin Cross92b7d582019-03-29 15:32:51 -07001019// Flags adds the specified raw text to the command line. The text should not contain input or output paths or the
1020// rule will not have them listed in its dependencies or outputs.
1021func (c *RuleBuilderCommand) Flags(flags []string) *RuleBuilderCommand {
1022 for _, flag := range flags {
1023 c.Text(flag)
1024 }
1025 return c
1026}
1027
Colin Cross758290d2019-02-01 16:42:32 -08001028// FlagWithArg adds the specified flag and argument text to the command line, with no separator between them. The flag
1029// and argument should not contain input or output paths or the rule will not have them listed in its dependencies or
1030// outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -08001031func (c *RuleBuilderCommand) FlagWithArg(flag, arg string) *RuleBuilderCommand {
1032 return c.Text(flag + arg)
1033}
1034
Colin Crossc7ed0042019-02-11 14:11:09 -08001035// FlagForEachArg adds the specified flag joined with each argument to the command line. The result is identical to
1036// calling FlagWithArg for argument.
1037func (c *RuleBuilderCommand) FlagForEachArg(flag string, args []string) *RuleBuilderCommand {
1038 for _, arg := range args {
1039 c.FlagWithArg(flag, arg)
1040 }
1041 return c
1042}
1043
Roland Levillain2da5d9a2019-02-27 16:56:41 +00001044// 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 -08001045// and no separator between the flag and arguments. The flag and arguments should not contain input or output paths or
1046// the rule will not have them listed in its dependencies or outputs.
Colin Crossfeec25b2019-01-30 17:32:39 -08001047func (c *RuleBuilderCommand) FlagWithList(flag string, list []string, sep string) *RuleBuilderCommand {
1048 return c.Text(flag + strings.Join(list, sep))
1049}
1050
Colin Cross758290d2019-02-01 16:42:32 -08001051// Tool adds the specified tool path to the command line. The path will be also added to the dependencies returned by
1052// RuleBuilder.Tools.
Colin Cross69f59a32019-02-15 10:39:37 -08001053func (c *RuleBuilderCommand) Tool(path Path) *RuleBuilderCommand {
Paul Duffin3866b892021-10-04 11:24:48 +01001054 checkPathNotNil(path)
Colin Crossfeec25b2019-01-30 17:32:39 -08001055 c.tools = append(c.tools, path)
Colin Crossba9e4032020-11-24 16:32:22 -08001056 return c.Text(c.PathForTool(path))
1057}
1058
1059// Tool adds the specified tool path to the dependencies returned by RuleBuilder.Tools.
1060func (c *RuleBuilderCommand) ImplicitTool(path Path) *RuleBuilderCommand {
Paul Duffin3866b892021-10-04 11:24:48 +01001061 checkPathNotNil(path)
Colin Crossba9e4032020-11-24 16:32:22 -08001062 c.tools = append(c.tools, path)
1063 return c
1064}
1065
1066// Tool adds the specified tool path to the dependencies returned by RuleBuilder.Tools.
1067func (c *RuleBuilderCommand) ImplicitTools(paths Paths) *RuleBuilderCommand {
Paul Duffin3866b892021-10-04 11:24:48 +01001068 for _, path := range paths {
1069 c.ImplicitTool(path)
1070 }
Colin Crossba9e4032020-11-24 16:32:22 -08001071 return c
Colin Crossfeec25b2019-01-30 17:32:39 -08001072}
1073
Colin Crossee94d6a2019-07-08 17:08:34 -07001074// BuiltTool adds the specified tool path that was built using a host Soong module to the command line. The path will
1075// be also added to the dependencies returned by RuleBuilder.Tools.
1076//
1077// It is equivalent to:
Colin Crossd079e0b2022-08-16 10:27:33 -07001078//
1079// cmd.Tool(ctx.Config().HostToolPath(ctx, tool))
Colin Crossf1a035e2020-11-16 17:32:30 -08001080func (c *RuleBuilderCommand) BuiltTool(tool string) *RuleBuilderCommand {
Colin Cross9b698b62021-12-22 09:55:32 -08001081 if c.rule.ctx.Config().UseHostMusl() {
1082 // If the host is using musl, assume that the tool was built against musl libc and include
1083 // libc_musl.so in the sandbox.
1084 // TODO(ccross): if we supported adding new dependencies during GenerateAndroidBuildActions
1085 // this could be a dependency + TransitivePackagingSpecs.
1086 c.ImplicitTool(c.rule.ctx.Config().HostJNIToolPath(c.rule.ctx, "libc_musl"))
1087 }
1088 return c.builtToolWithoutDeps(tool)
1089}
1090
1091// builtToolWithoutDeps is similar to BuiltTool, but doesn't add any dependencies. It is used
1092// internally by RuleBuilder for helper tools that are known to be compiled statically.
1093func (c *RuleBuilderCommand) builtToolWithoutDeps(tool string) *RuleBuilderCommand {
Colin Crossf1a035e2020-11-16 17:32:30 -08001094 return c.Tool(c.rule.ctx.Config().HostToolPath(c.rule.ctx, tool))
Colin Crossee94d6a2019-07-08 17:08:34 -07001095}
1096
1097// PrebuiltBuildTool adds the specified tool path from prebuils/build-tools. The path will be also added to the
1098// dependencies returned by RuleBuilder.Tools.
1099//
1100// It is equivalent to:
Colin Crossd079e0b2022-08-16 10:27:33 -07001101//
1102// cmd.Tool(ctx.Config().PrebuiltBuildTool(ctx, tool))
Colin Crossee94d6a2019-07-08 17:08:34 -07001103func (c *RuleBuilderCommand) PrebuiltBuildTool(ctx PathContext, tool string) *RuleBuilderCommand {
1104 return c.Tool(ctx.Config().PrebuiltBuildTool(ctx, tool))
1105}
1106
Colin Cross758290d2019-02-01 16:42:32 -08001107// Input adds the specified input path to the command line. The path will also be added to the dependencies returned by
1108// RuleBuilder.Inputs.
Colin Cross69f59a32019-02-15 10:39:37 -08001109func (c *RuleBuilderCommand) Input(path Path) *RuleBuilderCommand {
Dan Willemsen633c5022019-04-12 11:11:38 -07001110 return c.Text(c.addInput(path))
Colin Crossfeec25b2019-01-30 17:32:39 -08001111}
1112
Colin Cross758290d2019-02-01 16:42:32 -08001113// Inputs adds the specified input paths to the command line, separated by spaces. The paths will also be added to the
1114// dependencies returned by RuleBuilder.Inputs.
Colin Cross69f59a32019-02-15 10:39:37 -08001115func (c *RuleBuilderCommand) Inputs(paths Paths) *RuleBuilderCommand {
Colin Cross758290d2019-02-01 16:42:32 -08001116 for _, path := range paths {
1117 c.Input(path)
1118 }
1119 return c
1120}
1121
1122// Implicit adds the specified input path to the dependencies returned by RuleBuilder.Inputs without modifying the
1123// command line.
Colin Cross69f59a32019-02-15 10:39:37 -08001124func (c *RuleBuilderCommand) Implicit(path Path) *RuleBuilderCommand {
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001125 c.addImplicit(path)
Colin Crossfeec25b2019-01-30 17:32:39 -08001126 return c
1127}
1128
Colin Cross758290d2019-02-01 16:42:32 -08001129// Implicits adds the specified input paths to the dependencies returned by RuleBuilder.Inputs without modifying the
1130// command line.
Colin Cross69f59a32019-02-15 10:39:37 -08001131func (c *RuleBuilderCommand) Implicits(paths Paths) *RuleBuilderCommand {
Dan Willemsen633c5022019-04-12 11:11:38 -07001132 for _, path := range paths {
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001133 c.addImplicit(path)
Dan Willemsen633c5022019-04-12 11:11:38 -07001134 }
Colin Crossfeec25b2019-01-30 17:32:39 -08001135 return c
1136}
1137
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001138// GetImplicits returns the command's implicit inputs.
1139func (c *RuleBuilderCommand) GetImplicits() Paths {
1140 return c.implicits
1141}
1142
Colin Crossda71eda2020-02-21 16:55:19 -08001143// OrderOnly adds the specified input path to the dependencies returned by RuleBuilder.OrderOnlys
1144// without modifying the command line.
1145func (c *RuleBuilderCommand) OrderOnly(path Path) *RuleBuilderCommand {
1146 c.addOrderOnly(path)
1147 return c
1148}
1149
1150// OrderOnlys adds the specified input paths to the dependencies returned by RuleBuilder.OrderOnlys
1151// without modifying the command line.
1152func (c *RuleBuilderCommand) OrderOnlys(paths Paths) *RuleBuilderCommand {
1153 for _, path := range paths {
1154 c.addOrderOnly(path)
1155 }
1156 return c
1157}
1158
Colin Crossae89abe2021-04-21 11:45:23 -07001159// Validation adds the specified input path to the validation dependencies by
1160// RuleBuilder.Validations without modifying the command line.
1161func (c *RuleBuilderCommand) Validation(path Path) *RuleBuilderCommand {
Paul Duffin3866b892021-10-04 11:24:48 +01001162 checkPathNotNil(path)
Colin Crossae89abe2021-04-21 11:45:23 -07001163 c.validations = append(c.validations, path)
1164 return c
1165}
1166
1167// Validations adds the specified input paths to the validation dependencies by
1168// RuleBuilder.Validations without modifying the command line.
1169func (c *RuleBuilderCommand) Validations(paths Paths) *RuleBuilderCommand {
Paul Duffin3866b892021-10-04 11:24:48 +01001170 for _, path := range paths {
1171 c.Validation(path)
1172 }
Colin Crossae89abe2021-04-21 11:45:23 -07001173 return c
1174}
1175
Colin Cross758290d2019-02-01 16:42:32 -08001176// Output adds the specified output path to the command line. The path will also be added to the outputs returned by
1177// RuleBuilder.Outputs.
Colin Cross69f59a32019-02-15 10:39:37 -08001178func (c *RuleBuilderCommand) Output(path WritablePath) *RuleBuilderCommand {
Paul Duffin3866b892021-10-04 11:24:48 +01001179 checkPathNotNil(path)
Colin Crossfeec25b2019-01-30 17:32:39 -08001180 c.outputs = append(c.outputs, path)
Colin Crossf1a035e2020-11-16 17:32:30 -08001181 return c.Text(c.PathForOutput(path))
Colin Crossfeec25b2019-01-30 17:32:39 -08001182}
1183
Colin Cross758290d2019-02-01 16:42:32 -08001184// Outputs adds the specified output paths to the command line, separated by spaces. The paths will also be added to
1185// the outputs returned by RuleBuilder.Outputs.
Colin Cross69f59a32019-02-15 10:39:37 -08001186func (c *RuleBuilderCommand) Outputs(paths WritablePaths) *RuleBuilderCommand {
Colin Cross758290d2019-02-01 16:42:32 -08001187 for _, path := range paths {
1188 c.Output(path)
1189 }
1190 return c
1191}
1192
Dan Willemsen1945a4b2019-06-04 17:10:41 -07001193// OutputDir adds the output directory to the command line. This is only available when used with RuleBuilder.Sbox,
1194// and will be the temporary output directory managed by sbox, not the final one.
Anas Sulaimanb4dff132024-02-07 21:58:46 +00001195func (c *RuleBuilderCommand) OutputDir(subPathComponents ...string) *RuleBuilderCommand {
Colin Crossf1a035e2020-11-16 17:32:30 -08001196 if !c.rule.sbox {
Dan Willemsen1945a4b2019-06-04 17:10:41 -07001197 panic("OutputDir only valid with Sbox")
1198 }
Anas Sulaimanb4dff132024-02-07 21:58:46 +00001199 path := sboxOutDir
1200 if len(subPathComponents) > 0 {
1201 path = filepath.Join(append([]string{sboxOutDir}, subPathComponents...)...)
1202 }
1203 return c.Text(path)
Dan Willemsen1945a4b2019-06-04 17:10:41 -07001204}
1205
Colin Cross1d2cf042019-03-29 15:33:06 -07001206// DepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles and adds it to the command
1207// line, and causes RuleBuilder.Build file to set the depfile flag for ninja. If multiple depfiles are added to
1208// commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the depfiles together.
1209func (c *RuleBuilderCommand) DepFile(path WritablePath) *RuleBuilderCommand {
Paul Duffin3866b892021-10-04 11:24:48 +01001210 checkPathNotNil(path)
Colin Cross1d2cf042019-03-29 15:33:06 -07001211 c.depFiles = append(c.depFiles, path)
Colin Crossf1a035e2020-11-16 17:32:30 -08001212 return c.Text(c.PathForOutput(path))
Colin Cross1d2cf042019-03-29 15:33:06 -07001213}
1214
Colin Cross758290d2019-02-01 16:42:32 -08001215// ImplicitOutput adds the specified output path to the dependencies returned by RuleBuilder.Outputs without modifying
1216// the command line.
Colin Cross69f59a32019-02-15 10:39:37 -08001217func (c *RuleBuilderCommand) ImplicitOutput(path WritablePath) *RuleBuilderCommand {
Colin Crossfeec25b2019-01-30 17:32:39 -08001218 c.outputs = append(c.outputs, path)
1219 return c
1220}
1221
Colin Cross758290d2019-02-01 16:42:32 -08001222// ImplicitOutputs adds the specified output paths to the dependencies returned by RuleBuilder.Outputs without modifying
1223// the command line.
Colin Cross69f59a32019-02-15 10:39:37 -08001224func (c *RuleBuilderCommand) ImplicitOutputs(paths WritablePaths) *RuleBuilderCommand {
Colin Cross758290d2019-02-01 16:42:32 -08001225 c.outputs = append(c.outputs, paths...)
1226 return c
1227}
1228
Colin Cross1d2cf042019-03-29 15:33:06 -07001229// ImplicitDepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles without modifying
1230// the command line, and causes RuleBuilder.Build file to set the depfile flag for ninja. If multiple depfiles
1231// are added to commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the
1232// depfiles together.
1233func (c *RuleBuilderCommand) ImplicitDepFile(path WritablePath) *RuleBuilderCommand {
1234 c.depFiles = append(c.depFiles, path)
1235 return c
1236}
1237
Colin Cross758290d2019-02-01 16:42:32 -08001238// FlagWithInput adds the specified flag and input path to the command line, with no separator between them. The path
1239// will also be added to the dependencies returned by RuleBuilder.Inputs.
Colin Cross69f59a32019-02-15 10:39:37 -08001240func (c *RuleBuilderCommand) FlagWithInput(flag string, path Path) *RuleBuilderCommand {
Dan Willemsen633c5022019-04-12 11:11:38 -07001241 return c.Text(flag + c.addInput(path))
Colin Crossfeec25b2019-01-30 17:32:39 -08001242}
1243
Colin Cross758290d2019-02-01 16:42:32 -08001244// FlagWithInputList adds the specified flag and input paths to the command line, with the inputs joined by sep
1245// and no separator between the flag and inputs. The input paths will also be added to the dependencies returned by
1246// RuleBuilder.Inputs.
Colin Cross69f59a32019-02-15 10:39:37 -08001247func (c *RuleBuilderCommand) FlagWithInputList(flag string, paths Paths, sep string) *RuleBuilderCommand {
Dan Willemsen633c5022019-04-12 11:11:38 -07001248 strs := make([]string, len(paths))
1249 for i, path := range paths {
1250 strs[i] = c.addInput(path)
1251 }
1252 return c.FlagWithList(flag, strs, sep)
Colin Crossfeec25b2019-01-30 17:32:39 -08001253}
1254
Colin Cross758290d2019-02-01 16:42:32 -08001255// FlagForEachInput adds the specified flag joined with each input path to the command line. The input paths will also
1256// be added to the dependencies returned by RuleBuilder.Inputs. The result is identical to calling FlagWithInput for
1257// each input path.
Colin Cross69f59a32019-02-15 10:39:37 -08001258func (c *RuleBuilderCommand) FlagForEachInput(flag string, paths Paths) *RuleBuilderCommand {
Colin Cross758290d2019-02-01 16:42:32 -08001259 for _, path := range paths {
1260 c.FlagWithInput(flag, path)
1261 }
1262 return c
1263}
1264
1265// FlagWithOutput adds the specified flag and output path to the command line, with no separator between them. The path
1266// will also be added to the outputs returned by RuleBuilder.Outputs.
Colin Cross69f59a32019-02-15 10:39:37 -08001267func (c *RuleBuilderCommand) FlagWithOutput(flag string, path WritablePath) *RuleBuilderCommand {
Colin Crossfeec25b2019-01-30 17:32:39 -08001268 c.outputs = append(c.outputs, path)
Colin Crossf1a035e2020-11-16 17:32:30 -08001269 return c.Text(flag + c.PathForOutput(path))
Colin Crossfeec25b2019-01-30 17:32:39 -08001270}
1271
Colin Cross1d2cf042019-03-29 15:33:06 -07001272// FlagWithDepFile adds the specified flag and depfile path to the command line, with no separator between them. The path
1273// will also be added to the outputs returned by RuleBuilder.Outputs.
1274func (c *RuleBuilderCommand) FlagWithDepFile(flag string, path WritablePath) *RuleBuilderCommand {
1275 c.depFiles = append(c.depFiles, path)
Colin Crossf1a035e2020-11-16 17:32:30 -08001276 return c.Text(flag + c.PathForOutput(path))
Colin Cross1d2cf042019-03-29 15:33:06 -07001277}
1278
Colin Crossce3a51d2021-03-19 16:22:12 -07001279// FlagWithRspFileInputList adds the specified flag and path to an rspfile to the command line, with
1280// no separator between them. The paths will be written to the rspfile. If sbox is enabled, the
1281// rspfile must be outside the sbox directory. The first use of FlagWithRspFileInputList in any
1282// RuleBuilderCommand of a RuleBuilder will use Ninja's rsp file support for the rule, additional
1283// uses will result in an auxiliary rules to write the rspFile contents.
Colin Cross70c47412021-03-12 17:48:14 -08001284func (c *RuleBuilderCommand) FlagWithRspFileInputList(flag string, rspFile WritablePath, paths Paths) *RuleBuilderCommand {
Colin Cross0cb0d7b2019-07-11 10:59:15 -07001285 // Use an empty slice if paths is nil, the non-nil slice is used as an indicator that the rsp file must be
1286 // generated.
1287 if paths == nil {
1288 paths = Paths{}
1289 }
1290
Colin Crossce3a51d2021-03-19 16:22:12 -07001291 c.rspFiles = append(c.rspFiles, rspFileAndPaths{rspFile, paths})
Colin Cross0cb0d7b2019-07-11 10:59:15 -07001292
Colin Cross70c47412021-03-12 17:48:14 -08001293 if c.rule.sbox {
1294 if _, isRel, _ := maybeRelErr(c.rule.outDir.String(), rspFile.String()); isRel {
1295 panic(fmt.Errorf("FlagWithRspFileInputList rspfile %q must not be inside out dir %q",
1296 rspFile.String(), c.rule.outDir.String()))
1297 }
1298 }
1299
Colin Crossab020a72021-03-12 17:52:23 -08001300 c.FlagWithArg(flag, c.PathForInput(rspFile))
Colin Cross0cb0d7b2019-07-11 10:59:15 -07001301 return c
1302}
1303
Colin Cross758290d2019-02-01 16:42:32 -08001304// String returns the command line.
1305func (c *RuleBuilderCommand) String() string {
Colin Crosscfec40c2019-07-08 17:07:18 -07001306 return c.buf.String()
Colin Cross758290d2019-02-01 16:42:32 -08001307}
Colin Cross1d2cf042019-03-29 15:33:06 -07001308
Colin Crosse16ce362020-11-12 08:29:30 -08001309// RuleBuilderSboxProtoForTests takes the BuildParams for the manifest passed to RuleBuilder.Sbox()
1310// and returns sbox testproto generated by the RuleBuilder.
Colin Crossf61d03d2023-11-02 16:56:39 -07001311func RuleBuilderSboxProtoForTests(t *testing.T, ctx *TestContext, params TestingBuildParams) *sbox_proto.Manifest {
Colin Crosse16ce362020-11-12 08:29:30 -08001312 t.Helper()
Colin Crossf61d03d2023-11-02 16:56:39 -07001313 content := ContentFromFileRuleForTests(t, ctx, params)
Colin Crosse16ce362020-11-12 08:29:30 -08001314 manifest := sbox_proto.Manifest{}
Dan Willemsen4591b642021-05-24 14:24:12 -07001315 err := prototext.Unmarshal([]byte(content), &manifest)
Colin Crosse16ce362020-11-12 08:29:30 -08001316 if err != nil {
1317 t.Fatalf("failed to unmarshal manifest: %s", err.Error())
1318 }
1319 return &manifest
1320}
1321
Colin Cross1d2cf042019-03-29 15:33:06 -07001322func ninjaNameEscape(s string) string {
1323 b := []byte(s)
1324 escaped := false
1325 for i, c := range b {
1326 valid := (c >= 'a' && c <= 'z') ||
1327 (c >= 'A' && c <= 'Z') ||
1328 (c >= '0' && c <= '9') ||
1329 (c == '_') ||
1330 (c == '-') ||
1331 (c == '.')
1332 if !valid {
1333 b[i] = '_'
1334 escaped = true
1335 }
1336 }
1337 if escaped {
1338 s = string(b)
1339 }
1340 return s
1341}
Colin Cross3d680512020-11-13 16:23:53 -08001342
1343// hashSrcFiles returns a hash of the list of source files. It is used to ensure the command line
1344// or the sbox textproto manifest change even if the input files are not listed on the command line.
1345func hashSrcFiles(srcFiles Paths) string {
1346 h := sha256.New()
1347 srcFileList := strings.Join(srcFiles.Strings(), "\n")
1348 h.Write([]byte(srcFileList))
1349 return fmt.Sprintf("%x", h.Sum(nil))
1350}
Colin Crossf1a035e2020-11-16 17:32:30 -08001351
1352// BuilderContextForTesting returns a BuilderContext for the given config that can be used for tests
1353// that need to call methods that take a BuilderContext.
1354func BuilderContextForTesting(config Config) BuilderContext {
1355 pathCtx := PathContextForTesting(config)
1356 return builderContextForTests{
1357 PathContext: pathCtx,
1358 }
1359}
1360
1361type builderContextForTests struct {
1362 PathContext
1363}
1364
1365func (builderContextForTests) Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule {
1366 return nil
1367}
1368func (builderContextForTests) Build(PackageContext, BuildParams) {}
Colin Crossef972742021-03-12 17:24:45 -08001369
Colin Crosse55bd422021-03-23 13:44:30 -07001370func writeRspFileRule(ctx BuilderContext, rspFile WritablePath, paths Paths) {
1371 buf := &strings.Builder{}
1372 err := response.WriteRspFile(buf, paths.Strings())
1373 if err != nil {
1374 // There should never be I/O errors writing to a bytes.Buffer.
1375 panic(err)
Colin Crossef972742021-03-12 17:24:45 -08001376 }
Colin Crosse55bd422021-03-23 13:44:30 -07001377 WriteFileRule(ctx, rspFile, buf.String())
Colin Crossef972742021-03-12 17:24:45 -08001378}