Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 18 | "crypto/sha256" |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 19 | "fmt" |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 20 | "path/filepath" |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 21 | "sort" |
| 22 | "strings" |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 23 | "testing" |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 24 | |
| 25 | "github.com/google/blueprint" |
| 26 | "github.com/google/blueprint/proptools" |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 27 | "google.golang.org/protobuf/encoding/prototext" |
| 28 | "google.golang.org/protobuf/proto" |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 29 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 30 | "android/soong/cmd/sbox/sbox_proto" |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 31 | "android/soong/remoteexec" |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 32 | "android/soong/response" |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 33 | "android/soong/shared" |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 34 | ) |
| 35 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 36 | const sboxSandboxBaseDir = "__SBOX_SANDBOX_DIR__" |
| 37 | const sboxOutSubDir = "out" |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 38 | const sboxToolsSubDir = "tools" |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 39 | const sboxOutDir = sboxSandboxBaseDir + "/" + sboxOutSubDir |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 40 | |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 41 | const nsjailToolsSubDir = "tools" |
| 42 | const nsjailOutDir = "out" |
| 43 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 44 | // RuleBuilder provides an alternative to ModuleContext.Rule and ModuleContext.Build to add a command line to the build |
| 45 | // graph. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 46 | type RuleBuilder struct { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 47 | pctx PackageContext |
| 48 | ctx BuilderContext |
| 49 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 50 | commands []*RuleBuilderCommand |
| 51 | installs RuleBuilderInstalls |
| 52 | temporariesSet map[WritablePath]bool |
| 53 | restat bool |
| 54 | sbox bool |
| 55 | highmem bool |
| 56 | remoteable RemoteRuleSupports |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 57 | rbeParams *remoteexec.REParams |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 58 | outDir WritablePath |
Spandan Das | af4ccaa | 2023-06-29 01:15:51 +0000 | [diff] [blame] | 59 | sboxOutSubDir string |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 60 | sboxTools bool |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 61 | sboxInputs bool |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 62 | sboxManifestPath WritablePath |
| 63 | missingDeps []string |
Devin Moore | b6cc64f | 2024-07-15 22:31:24 +0000 | [diff] [blame] | 64 | args map[string]string |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 65 | nsjail bool |
| 66 | nsjailBasePath WritablePath |
| 67 | nsjailImplicits Paths |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 70 | // NewRuleBuilder returns a newly created RuleBuilder. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 71 | func NewRuleBuilder(pctx PackageContext, ctx BuilderContext) *RuleBuilder { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 72 | return &RuleBuilder{ |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 73 | pctx: pctx, |
| 74 | ctx: ctx, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 75 | temporariesSet: make(map[WritablePath]bool), |
Spandan Das | af4ccaa | 2023-06-29 01:15:51 +0000 | [diff] [blame] | 76 | sboxOutSubDir: sboxOutSubDir, |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 77 | } |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Spandan Das | af4ccaa | 2023-06-29 01:15:51 +0000 | [diff] [blame] | 80 | // SetSboxOutDirDirAsEmpty sets the out subdirectory to an empty string |
| 81 | // This is useful for sandboxing actions that change the execution root to a path in out/ (e.g mixed builds) |
| 82 | // For such actions, SetSboxOutDirDirAsEmpty ensures that the path does not become $SBOX_SANDBOX_DIR/out/out/bazel/output/execroot/__main__/... |
| 83 | func (rb *RuleBuilder) SetSboxOutDirDirAsEmpty() *RuleBuilder { |
| 84 | rb.sboxOutSubDir = "" |
| 85 | return rb |
| 86 | } |
| 87 | |
Devin Moore | b6cc64f | 2024-07-15 22:31:24 +0000 | [diff] [blame] | 88 | // Set the phony_output argument. |
| 89 | // This causes the output files to be ignored. |
| 90 | // If the output isn't created, it's not treated as an error. |
| 91 | // The build rule is run every time whether or not the output is created. |
| 92 | func (rb *RuleBuilder) SetPhonyOutput() { |
| 93 | if rb.args == nil { |
| 94 | rb.args = make(map[string]string) |
| 95 | } |
| 96 | rb.args["phony_output"] = "true" |
| 97 | } |
| 98 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 99 | // RuleBuilderInstall is a tuple of install from and to locations. |
| 100 | type RuleBuilderInstall struct { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 101 | From Path |
| 102 | To string |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 105 | type RuleBuilderInstalls []RuleBuilderInstall |
| 106 | |
| 107 | // String returns the RuleBuilderInstalls in the form used by $(call copy-many-files) in Make, a space separated |
| 108 | // list of from:to tuples. |
| 109 | func (installs RuleBuilderInstalls) String() string { |
| 110 | sb := strings.Builder{} |
| 111 | for i, install := range installs { |
| 112 | if i != 0 { |
| 113 | sb.WriteRune(' ') |
| 114 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 115 | sb.WriteString(install.From.String()) |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 116 | sb.WriteRune(':') |
| 117 | sb.WriteString(install.To) |
| 118 | } |
| 119 | return sb.String() |
| 120 | } |
| 121 | |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 122 | // MissingDeps adds modules to the list of missing dependencies. If MissingDeps |
| 123 | // is called with a non-empty input, any call to Build will result in a rule |
| 124 | // that will print an error listing the missing dependencies and fail. |
| 125 | // MissingDeps should only be called if Config.AllowMissingDependencies() is |
| 126 | // true. |
| 127 | func (r *RuleBuilder) MissingDeps(missingDeps []string) { |
| 128 | r.missingDeps = append(r.missingDeps, missingDeps...) |
| 129 | } |
| 130 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 131 | // Restat marks the rule as a restat rule, which will be passed to ModuleContext.Rule in BuildParams.Restat. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 132 | func (r *RuleBuilder) Restat() *RuleBuilder { |
| 133 | r.restat = true |
| 134 | return r |
| 135 | } |
| 136 | |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 137 | // HighMem marks the rule as a high memory rule, which will limit how many run in parallel with other high memory |
| 138 | // rules. |
| 139 | func (r *RuleBuilder) HighMem() *RuleBuilder { |
| 140 | r.highmem = true |
| 141 | return r |
| 142 | } |
| 143 | |
| 144 | // Remoteable marks the rule as supporting remote execution. |
| 145 | func (r *RuleBuilder) Remoteable(supports RemoteRuleSupports) *RuleBuilder { |
| 146 | r.remoteable = supports |
| 147 | return r |
| 148 | } |
| 149 | |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 150 | // Rewrapper marks the rule as running inside rewrapper using the given params in order to support |
| 151 | // running on RBE. During RuleBuilder.Build the params will be combined with the inputs, outputs |
| 152 | // and tools known to RuleBuilder to prepend an appropriate rewrapper command line to the rule's |
| 153 | // command line. |
| 154 | func (r *RuleBuilder) Rewrapper(params *remoteexec.REParams) *RuleBuilder { |
| 155 | if !r.sboxInputs { |
| 156 | panic(fmt.Errorf("RuleBuilder.Rewrapper must be called after RuleBuilder.SandboxInputs")) |
| 157 | } |
| 158 | r.rbeParams = params |
| 159 | return r |
| 160 | } |
| 161 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 162 | // Sbox marks the rule as needing to be wrapped by sbox. The outputDir should point to the output |
| 163 | // directory that sbox will wipe. It should not be written to by any other rule. manifestPath should |
| 164 | // point to a location where sbox's manifest will be written and must be outside outputDir. sbox |
| 165 | // will ensure that all outputs have been written, and will discard any output files that were not |
| 166 | // specified. |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 167 | func (r *RuleBuilder) Sbox(outputDir WritablePath, manifestPath WritablePath) *RuleBuilder { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 168 | if r.sbox { |
| 169 | panic("Sbox() may not be called more than once") |
| 170 | } |
| 171 | if len(r.commands) > 0 { |
| 172 | panic("Sbox() may not be called after Command()") |
| 173 | } |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 174 | if r.nsjail { |
| 175 | panic("Sbox() may not be called after Nsjail()") |
| 176 | } |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 177 | r.sbox = true |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 178 | r.outDir = outputDir |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 179 | r.sboxManifestPath = manifestPath |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 180 | return r |
| 181 | } |
| 182 | |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 183 | // Nsjail marks the rule as needing to be wrapped by nsjail. The outputDir should point to the |
| 184 | // output directory that nsjail will mount to out/. It should not be written to by any other rule. |
| 185 | // baseDir should point to a location where nsjail will mount to /nsjail_build_sandbox, which will |
| 186 | // be the working directory of the command. |
| 187 | func (r *RuleBuilder) Nsjail(outputDir WritablePath, baseDir WritablePath) *RuleBuilder { |
| 188 | if len(r.commands) > 0 { |
| 189 | panic("Nsjail() may not be called after Command()") |
| 190 | } |
| 191 | if r.sbox { |
| 192 | panic("Nsjail() may not be called after Sbox()") |
| 193 | } |
| 194 | r.nsjail = true |
| 195 | r.outDir = outputDir |
| 196 | r.nsjailBasePath = baseDir |
| 197 | return r |
| 198 | } |
| 199 | |
| 200 | // NsjailImplicits adds implicit inputs that are not directly mounted. This is useful when |
| 201 | // the rule mounts directories, as files within those directories can be globbed and |
| 202 | // tracked as dependencies with NsjailImplicits(). |
| 203 | func (r *RuleBuilder) NsjailImplicits(inputs Paths) *RuleBuilder { |
| 204 | if !r.nsjail { |
| 205 | panic("NsjailImplicits() must be called after Nsjail()") |
| 206 | } |
| 207 | r.nsjailImplicits = append(r.nsjailImplicits, inputs...) |
| 208 | return r |
| 209 | } |
| 210 | |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 211 | // SandboxTools enables tool sandboxing for the rule by copying any referenced tools into the |
| 212 | // sandbox. |
| 213 | func (r *RuleBuilder) SandboxTools() *RuleBuilder { |
| 214 | if !r.sbox { |
| 215 | panic("SandboxTools() must be called after Sbox()") |
| 216 | } |
| 217 | if len(r.commands) > 0 { |
| 218 | panic("SandboxTools() may not be called after Command()") |
| 219 | } |
| 220 | r.sboxTools = true |
| 221 | return r |
| 222 | } |
| 223 | |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 224 | // SandboxInputs enables input sandboxing for the rule by copying any referenced inputs into the |
| 225 | // sandbox. It also implies SandboxTools(). |
| 226 | // |
| 227 | // Sandboxing inputs requires RuleBuilder to be aware of all references to input paths. Paths |
| 228 | // that are passed to RuleBuilder outside of the methods that expect inputs, for example |
| 229 | // FlagWithArg, must use RuleBuilderCommand.PathForInput to translate the path to one that matches |
| 230 | // the sandbox layout. |
| 231 | func (r *RuleBuilder) SandboxInputs() *RuleBuilder { |
| 232 | if !r.sbox { |
| 233 | panic("SandboxInputs() must be called after Sbox()") |
| 234 | } |
| 235 | if len(r.commands) > 0 { |
| 236 | panic("SandboxInputs() may not be called after Command()") |
| 237 | } |
| 238 | r.sboxTools = true |
| 239 | r.sboxInputs = true |
| 240 | return r |
| 241 | } |
| 242 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 243 | // Install associates an output of the rule with an install location, which can be retrieved later using |
| 244 | // RuleBuilder.Installs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 245 | func (r *RuleBuilder) Install(from Path, to string) { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 246 | r.installs = append(r.installs, RuleBuilderInstall{from, to}) |
| 247 | } |
| 248 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 249 | // Command returns a new RuleBuilderCommand for the rule. The commands will be ordered in the rule by when they were |
| 250 | // created by this method. That can be mutated through their methods in any order, as long as the mutations do not |
| 251 | // race with any call to Build. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 252 | func (r *RuleBuilder) Command() *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 253 | command := &RuleBuilderCommand{ |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 254 | rule: r, |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 255 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 256 | r.commands = append(r.commands, command) |
| 257 | return command |
| 258 | } |
| 259 | |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 260 | // Temporary marks an output of a command as an intermediate file that will be used as an input to another command |
| 261 | // in the same rule, and should not be listed in Outputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 262 | func (r *RuleBuilder) Temporary(path WritablePath) { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 263 | r.temporariesSet[path] = true |
| 264 | } |
| 265 | |
| 266 | // DeleteTemporaryFiles adds a command to the rule that deletes any outputs that have been marked using Temporary |
| 267 | // when the rule runs. DeleteTemporaryFiles should be called after all calls to Temporary. |
| 268 | func (r *RuleBuilder) DeleteTemporaryFiles() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 269 | var temporariesList WritablePaths |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 270 | |
| 271 | for intermediate := range r.temporariesSet { |
| 272 | temporariesList = append(temporariesList, intermediate) |
| 273 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 274 | |
| 275 | sort.Slice(temporariesList, func(i, j int) bool { |
| 276 | return temporariesList[i].String() < temporariesList[j].String() |
| 277 | }) |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 278 | |
| 279 | r.Command().Text("rm").Flag("-f").Outputs(temporariesList) |
| 280 | } |
| 281 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 282 | // Inputs returns the list of paths that were passed to the RuleBuilderCommand methods that take |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 283 | // input paths, such as RuleBuilderCommand.Input, RuleBuilderCommand.Implicit, or |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 284 | // RuleBuilderCommand.FlagWithInput. Inputs to a command that are also outputs of another command |
| 285 | // in the same RuleBuilder are filtered out. The list is sorted and duplicates removed. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 286 | func (r *RuleBuilder) Inputs() Paths { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 287 | outputs := r.outputSet() |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 288 | depFiles := r.depFileSet() |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 289 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 290 | inputs := make(map[string]Path) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 291 | for _, c := range r.commands { |
Ramy Medhat | 2f99eec | 2020-06-13 17:38:27 -0400 | [diff] [blame] | 292 | for _, input := range append(c.inputs, c.implicits...) { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 293 | inputStr := input.String() |
| 294 | if _, isOutput := outputs[inputStr]; !isOutput { |
| 295 | if _, isDepFile := depFiles[inputStr]; !isDepFile { |
| 296 | inputs[input.String()] = input |
| 297 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 302 | var inputList Paths |
| 303 | for _, input := range inputs { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 304 | inputList = append(inputList, input) |
| 305 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 306 | |
| 307 | sort.Slice(inputList, func(i, j int) bool { |
| 308 | return inputList[i].String() < inputList[j].String() |
| 309 | }) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 310 | |
| 311 | return inputList |
| 312 | } |
| 313 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 314 | // OrderOnlys returns the list of paths that were passed to the RuleBuilderCommand.OrderOnly or |
| 315 | // RuleBuilderCommand.OrderOnlys. The list is sorted and duplicates removed. |
| 316 | func (r *RuleBuilder) OrderOnlys() Paths { |
| 317 | orderOnlys := make(map[string]Path) |
| 318 | for _, c := range r.commands { |
| 319 | for _, orderOnly := range c.orderOnlys { |
| 320 | orderOnlys[orderOnly.String()] = orderOnly |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | var orderOnlyList Paths |
| 325 | for _, orderOnly := range orderOnlys { |
| 326 | orderOnlyList = append(orderOnlyList, orderOnly) |
| 327 | } |
| 328 | |
| 329 | sort.Slice(orderOnlyList, func(i, j int) bool { |
| 330 | return orderOnlyList[i].String() < orderOnlyList[j].String() |
| 331 | }) |
| 332 | |
| 333 | return orderOnlyList |
| 334 | } |
| 335 | |
Colin Cross | ae89abe | 2021-04-21 11:45:23 -0700 | [diff] [blame] | 336 | // Validations returns the list of paths that were passed to RuleBuilderCommand.Validation or |
| 337 | // RuleBuilderCommand.Validations. The list is sorted and duplicates removed. |
| 338 | func (r *RuleBuilder) Validations() Paths { |
| 339 | validations := make(map[string]Path) |
| 340 | for _, c := range r.commands { |
| 341 | for _, validation := range c.validations { |
| 342 | validations[validation.String()] = validation |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | var validationList Paths |
| 347 | for _, validation := range validations { |
| 348 | validationList = append(validationList, validation) |
| 349 | } |
| 350 | |
| 351 | sort.Slice(validationList, func(i, j int) bool { |
| 352 | return validationList[i].String() < validationList[j].String() |
| 353 | }) |
| 354 | |
| 355 | return validationList |
| 356 | } |
| 357 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 358 | func (r *RuleBuilder) outputSet() map[string]WritablePath { |
| 359 | outputs := make(map[string]WritablePath) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 360 | for _, c := range r.commands { |
| 361 | for _, output := range c.outputs { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 362 | outputs[output.String()] = output |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | return outputs |
| 366 | } |
| 367 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 368 | // Outputs returns the list of paths that were passed to the RuleBuilderCommand methods that take |
| 369 | // output paths, such as RuleBuilderCommand.Output, RuleBuilderCommand.ImplicitOutput, or |
| 370 | // RuleBuilderCommand.FlagWithInput. The list is sorted and duplicates removed. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 371 | func (r *RuleBuilder) Outputs() WritablePaths { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 372 | outputs := r.outputSet() |
| 373 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 374 | var outputList WritablePaths |
| 375 | for _, output := range outputs { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 376 | if !r.temporariesSet[output] { |
| 377 | outputList = append(outputList, output) |
| 378 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 379 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 380 | |
| 381 | sort.Slice(outputList, func(i, j int) bool { |
| 382 | return outputList[i].String() < outputList[j].String() |
| 383 | }) |
| 384 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 385 | return outputList |
| 386 | } |
| 387 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 388 | func (r *RuleBuilder) depFileSet() map[string]WritablePath { |
| 389 | depFiles := make(map[string]WritablePath) |
| 390 | for _, c := range r.commands { |
| 391 | for _, depFile := range c.depFiles { |
| 392 | depFiles[depFile.String()] = depFile |
| 393 | } |
| 394 | } |
| 395 | return depFiles |
| 396 | } |
| 397 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 398 | // DepFiles returns the list of paths that were passed to the RuleBuilderCommand methods that take depfile paths, such |
| 399 | // as RuleBuilderCommand.DepFile or RuleBuilderCommand.FlagWithDepFile. |
| 400 | func (r *RuleBuilder) DepFiles() WritablePaths { |
| 401 | var depFiles WritablePaths |
| 402 | |
| 403 | for _, c := range r.commands { |
| 404 | for _, depFile := range c.depFiles { |
| 405 | depFiles = append(depFiles, depFile) |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | return depFiles |
| 410 | } |
| 411 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 412 | // Installs returns the list of tuples passed to Install. |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 413 | func (r *RuleBuilder) Installs() RuleBuilderInstalls { |
| 414 | return append(RuleBuilderInstalls(nil), r.installs...) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 417 | func (r *RuleBuilder) toolsSet() map[string]Path { |
| 418 | tools := make(map[string]Path) |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 419 | for _, c := range r.commands { |
| 420 | for _, tool := range c.tools { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 421 | tools[tool.String()] = tool |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
| 425 | return tools |
| 426 | } |
| 427 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 428 | // Tools returns the list of paths that were passed to the RuleBuilderCommand.Tool method. The |
| 429 | // list is sorted and duplicates removed. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 430 | func (r *RuleBuilder) Tools() Paths { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 431 | toolsSet := r.toolsSet() |
| 432 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 433 | var toolsList Paths |
| 434 | for _, tool := range toolsSet { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 435 | toolsList = append(toolsList, tool) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 436 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 437 | |
| 438 | sort.Slice(toolsList, func(i, j int) bool { |
| 439 | return toolsList[i].String() < toolsList[j].String() |
| 440 | }) |
| 441 | |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 442 | return toolsList |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 445 | // RspFileInputs returns the list of paths that were passed to the RuleBuilderCommand.FlagWithRspFileInputList method. |
| 446 | func (r *RuleBuilder) RspFileInputs() Paths { |
| 447 | var rspFileInputs Paths |
| 448 | for _, c := range r.commands { |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 449 | for _, rspFile := range c.rspFiles { |
| 450 | rspFileInputs = append(rspFileInputs, rspFile.paths...) |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
| 454 | return rspFileInputs |
| 455 | } |
| 456 | |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 457 | func (r *RuleBuilder) rspFiles() []rspFileAndPaths { |
| 458 | var rspFiles []rspFileAndPaths |
Colin Cross | 70c4741 | 2021-03-12 17:48:14 -0800 | [diff] [blame] | 459 | for _, c := range r.commands { |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 460 | rspFiles = append(rspFiles, c.rspFiles...) |
Colin Cross | 70c4741 | 2021-03-12 17:48:14 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 463 | return rspFiles |
Colin Cross | 70c4741 | 2021-03-12 17:48:14 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 466 | // Commands returns a slice containing the built command line for each call to RuleBuilder.Command. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 467 | func (r *RuleBuilder) Commands() []string { |
| 468 | var commands []string |
| 469 | for _, c := range r.commands { |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 470 | commands = append(commands, c.String()) |
| 471 | } |
| 472 | return commands |
| 473 | } |
| 474 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 475 | // BuilderContext is a subset of ModuleContext and SingletonContext. |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 476 | type BuilderContext interface { |
| 477 | PathContext |
| 478 | Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule |
| 479 | Build(PackageContext, BuildParams) |
| 480 | } |
| 481 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 482 | var _ BuilderContext = ModuleContext(nil) |
| 483 | var _ BuilderContext = SingletonContext(nil) |
| 484 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 485 | func (r *RuleBuilder) depFileMergerCmd(depFiles WritablePaths) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 486 | return r.Command(). |
Colin Cross | 9b698b6 | 2021-12-22 09:55:32 -0800 | [diff] [blame] | 487 | builtToolWithoutDeps("dep_fixer"). |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 488 | Inputs(depFiles.Paths()) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 491 | // Build adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for |
| 492 | // Outputs. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 493 | func (r *RuleBuilder) Build(name string, desc string) { |
Cole Faust | 2f3791f | 2024-11-25 15:49:57 -0800 | [diff] [blame] | 494 | r.build(name, desc) |
Sam Delmerico | 285b66a | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Cole Faust | 63ea1f9 | 2024-08-27 11:42:26 -0700 | [diff] [blame] | 497 | var sandboxEnvOnceKey = NewOnceKey("sandbox_environment_variables") |
| 498 | |
Cole Faust | 2f3791f | 2024-11-25 15:49:57 -0800 | [diff] [blame] | 499 | func (r *RuleBuilder) build(name string, desc string) { |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 500 | name = ninjaNameEscape(name) |
| 501 | |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 502 | if len(r.missingDeps) > 0 { |
Sam Delmerico | 285b66a | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 503 | r.ctx.Build(r.pctx, BuildParams{ |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 504 | Rule: ErrorRule, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 505 | Outputs: r.Outputs(), |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 506 | Description: desc, |
| 507 | Args: map[string]string{ |
| 508 | "error": "missing dependencies: " + strings.Join(r.missingDeps, ", "), |
| 509 | }, |
| 510 | }) |
| 511 | return |
| 512 | } |
| 513 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 514 | var depFile WritablePath |
| 515 | var depFormat blueprint.Deps |
| 516 | if depFiles := r.DepFiles(); len(depFiles) > 0 { |
| 517 | depFile = depFiles[0] |
| 518 | depFormat = blueprint.DepsGCC |
| 519 | if len(depFiles) > 1 { |
| 520 | // Add a command locally that merges all depfiles together into the first depfile. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 521 | r.depFileMergerCmd(depFiles) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 522 | |
| 523 | if r.sbox { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 524 | // Check for Rel() errors, as all depfiles should be in the output dir. Errors |
| 525 | // will be reported to the ctx. |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 526 | for _, path := range depFiles[1:] { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 527 | Rel(r.ctx, r.outDir.String(), path.String()) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 528 | } |
| 529 | } |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 533 | tools := r.Tools() |
Colin Cross | b70a1a9 | 2021-03-12 17:51:32 -0800 | [diff] [blame] | 534 | commands := r.Commands() |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 535 | outputs := r.Outputs() |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 536 | inputs := r.Inputs() |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 537 | rspFiles := r.rspFiles() |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 538 | |
| 539 | if len(commands) == 0 { |
| 540 | return |
| 541 | } |
| 542 | if len(outputs) == 0 { |
| 543 | panic("No outputs specified from any Commands") |
| 544 | } |
| 545 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 546 | commandString := strings.Join(commands, " && ") |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 547 | |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 548 | if !r.sbox { |
| 549 | // If not using sbox the rule will run the command directly, put the hash of the |
| 550 | // list of input files in a comment at the end of the command line to ensure ninja |
| 551 | // reruns the rule when the list of input files changes. |
| 552 | commandString += " # hash of input list: " + hashSrcFiles(inputs) |
| 553 | } |
| 554 | |
| 555 | if r.nsjail { |
| 556 | var nsjailCmd strings.Builder |
| 557 | nsjailPath := r.ctx.Config().PrebuiltBuildTool(r.ctx, "nsjail") |
| 558 | nsjailCmd.WriteString("mkdir -p ") |
| 559 | nsjailCmd.WriteString(r.nsjailBasePath.String()) |
| 560 | nsjailCmd.WriteString(" && ") |
| 561 | nsjailCmd.WriteString(nsjailPath.String()) |
| 562 | nsjailCmd.WriteRune(' ') |
| 563 | nsjailCmd.WriteString("-B $PWD/") |
| 564 | nsjailCmd.WriteString(r.nsjailBasePath.String()) |
| 565 | nsjailCmd.WriteString(":nsjail_build_sandbox") |
| 566 | |
| 567 | // out is mounted to $(genDir). |
| 568 | nsjailCmd.WriteString(" -B $PWD/") |
| 569 | nsjailCmd.WriteString(r.outDir.String()) |
| 570 | nsjailCmd.WriteString(":nsjail_build_sandbox/out") |
| 571 | |
Inseob Kim | 93036a5 | 2024-10-25 17:02:21 +0900 | [diff] [blame] | 572 | addBindMount := func(src, dst string) { |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 573 | nsjailCmd.WriteString(" -R $PWD/") |
Inseob Kim | 93036a5 | 2024-10-25 17:02:21 +0900 | [diff] [blame] | 574 | nsjailCmd.WriteString(src) |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 575 | nsjailCmd.WriteString(":nsjail_build_sandbox/") |
Inseob Kim | 93036a5 | 2024-10-25 17:02:21 +0900 | [diff] [blame] | 576 | nsjailCmd.WriteString(dst) |
| 577 | } |
| 578 | |
| 579 | for _, input := range inputs { |
| 580 | addBindMount(input.String(), r.nsjailPathForInputRel(input)) |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 581 | } |
| 582 | for _, tool := range tools { |
Inseob Kim | 93036a5 | 2024-10-25 17:02:21 +0900 | [diff] [blame] | 583 | addBindMount(tool.String(), nsjailPathForToolRel(r.ctx, tool)) |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 584 | } |
| 585 | inputs = append(inputs, tools...) |
| 586 | for _, c := range r.commands { |
Inseob Kim | 93036a5 | 2024-10-25 17:02:21 +0900 | [diff] [blame] | 587 | for _, directory := range c.implicitDirectories { |
| 588 | addBindMount(directory.String(), directory.String()) |
| 589 | // TODO(b/375551969): Add implicitDirectories to BuildParams, rather than relying on implicits |
| 590 | inputs = append(inputs, SourcePath{basePath: directory.base()}) |
| 591 | } |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 592 | for _, tool := range c.packagedTools { |
Inseob Kim | 93036a5 | 2024-10-25 17:02:21 +0900 | [diff] [blame] | 593 | addBindMount(tool.srcPath.String(), nsjailPathForPackagedToolRel(tool)) |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 594 | inputs = append(inputs, tool.srcPath) |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | // These five directories are necessary to run native host tools like /bin/bash and py3-cmd. |
| 599 | nsjailCmd.WriteString(" -R /bin") |
| 600 | nsjailCmd.WriteString(" -R /lib") |
| 601 | nsjailCmd.WriteString(" -R /lib64") |
| 602 | nsjailCmd.WriteString(" -R /dev") |
| 603 | nsjailCmd.WriteString(" -R /usr") |
| 604 | |
| 605 | nsjailCmd.WriteString(" -m none:/tmp:tmpfs:size=1073741824") // 1GB, should be enough |
| 606 | nsjailCmd.WriteString(" -D nsjail_build_sandbox") |
| 607 | nsjailCmd.WriteString(" --disable_rlimits") |
Haamed Gheibi | c128dd7 | 2024-11-13 13:27:53 -0800 | [diff] [blame] | 608 | nsjailCmd.WriteString(" --skip_setsid") // ABFS relies on process-groups to track file operations |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 609 | nsjailCmd.WriteString(" -q") |
| 610 | nsjailCmd.WriteString(" -- ") |
| 611 | nsjailCmd.WriteString("/bin/bash -c ") |
| 612 | nsjailCmd.WriteString(proptools.ShellEscape(commandString)) |
| 613 | |
| 614 | commandString = nsjailCmd.String() |
| 615 | |
| 616 | inputs = append(inputs, nsjailPath) |
| 617 | inputs = append(inputs, r.nsjailImplicits...) |
| 618 | } else if r.sbox { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 619 | // If running the command inside sbox, write the rule data out to an sbox |
| 620 | // manifest.textproto. |
| 621 | manifest := sbox_proto.Manifest{} |
| 622 | command := sbox_proto.Command{} |
| 623 | manifest.Commands = append(manifest.Commands, &command) |
| 624 | command.Command = proto.String(commandString) |
Colin Cross | 151b9ff | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 625 | |
Colin Cross | 619b9ab | 2020-11-20 18:44:31 +0000 | [diff] [blame] | 626 | if depFile != nil { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 627 | manifest.OutputDepfile = proto.String(depFile.String()) |
Colin Cross | 619b9ab | 2020-11-20 18:44:31 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 630 | // If sandboxing tools is enabled, add copy rules to the manifest to copy each tool |
| 631 | // into the sbox directory. |
| 632 | if r.sboxTools { |
| 633 | for _, tool := range tools { |
| 634 | command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{ |
| 635 | From: proto.String(tool.String()), |
| 636 | To: proto.String(sboxPathForToolRel(r.ctx, tool)), |
| 637 | }) |
| 638 | } |
| 639 | for _, c := range r.commands { |
| 640 | for _, tool := range c.packagedTools { |
| 641 | command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{ |
| 642 | From: proto.String(tool.srcPath.String()), |
| 643 | To: proto.String(sboxPathForPackagedToolRel(tool)), |
| 644 | Executable: proto.Bool(tool.executable), |
| 645 | }) |
| 646 | tools = append(tools, tool.srcPath) |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 651 | // If sandboxing inputs is enabled, add copy rules to the manifest to copy each input |
| 652 | // into the sbox directory. |
| 653 | if r.sboxInputs { |
| 654 | for _, input := range inputs { |
| 655 | command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{ |
| 656 | From: proto.String(input.String()), |
| 657 | To: proto.String(r.sboxPathForInputRel(input)), |
| 658 | }) |
| 659 | } |
Cole Faust | 78f3c3a | 2024-08-15 17:19:34 -0700 | [diff] [blame] | 660 | for _, input := range r.OrderOnlys() { |
| 661 | command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{ |
| 662 | From: proto.String(input.String()), |
| 663 | To: proto.String(r.sboxPathForInputRel(input)), |
| 664 | }) |
| 665 | } |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 666 | |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 667 | // If using rsp files copy them and their contents into the sbox directory with |
| 668 | // the appropriate path mappings. |
| 669 | for _, rspFile := range rspFiles { |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 670 | command.RspFiles = append(command.RspFiles, &sbox_proto.RspFile{ |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 671 | File: proto.String(rspFile.file.String()), |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 672 | // These have to match the logic in sboxPathForInputRel |
| 673 | PathMappings: []*sbox_proto.PathMapping{ |
| 674 | { |
| 675 | From: proto.String(r.outDir.String()), |
| 676 | To: proto.String(sboxOutSubDir), |
| 677 | }, |
| 678 | { |
Cole Faust | e8561c6 | 2023-11-30 17:26:37 -0800 | [diff] [blame] | 679 | From: proto.String(r.ctx.Config().OutDir()), |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 680 | To: proto.String(sboxOutSubDir), |
| 681 | }, |
| 682 | }, |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 683 | }) |
| 684 | } |
| 685 | |
Cole Faust | 63ea1f9 | 2024-08-27 11:42:26 -0700 | [diff] [blame] | 686 | // Only allow the build to access certain environment variables |
| 687 | command.DontInheritEnv = proto.Bool(true) |
| 688 | command.Env = r.ctx.Config().Once(sandboxEnvOnceKey, func() interface{} { |
| 689 | // The list of allowed variables was found by running builds of all |
| 690 | // genrules and seeing what failed |
| 691 | var result []*sbox_proto.EnvironmentVariable |
| 692 | inheritedVars := []string{ |
| 693 | "PATH", |
| 694 | "JAVA_HOME", |
| 695 | "TMPDIR", |
| 696 | // Allow RBE variables because the art tests invoke RBE manually |
| 697 | "RBE_log_dir", |
| 698 | "RBE_platform", |
| 699 | "RBE_server_address", |
| 700 | // TODO: RBE_exec_root is set to the absolute path to the root of the source |
| 701 | // tree, which we don't want sandboxed actions to find. Remap it to ".". |
| 702 | "RBE_exec_root", |
| 703 | } |
| 704 | for _, v := range inheritedVars { |
| 705 | result = append(result, &sbox_proto.EnvironmentVariable{ |
| 706 | Name: proto.String(v), |
| 707 | State: &sbox_proto.EnvironmentVariable_Inherit{ |
| 708 | Inherit: true, |
| 709 | }, |
| 710 | }) |
| 711 | } |
| 712 | // Set OUT_DIR to the relative path of the sandboxed out directory. |
| 713 | // Otherwise, OUT_DIR will be inherited from the rest of the build, |
| 714 | // which will allow scripts to escape the sandbox if OUT_DIR is an |
| 715 | // absolute path. |
| 716 | result = append(result, &sbox_proto.EnvironmentVariable{ |
| 717 | Name: proto.String("OUT_DIR"), |
| 718 | State: &sbox_proto.EnvironmentVariable_Value{ |
| 719 | Value: sboxOutSubDir, |
| 720 | }, |
| 721 | }) |
| 722 | return result |
| 723 | }).([]*sbox_proto.EnvironmentVariable) |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 724 | command.Chdir = proto.Bool(true) |
| 725 | } |
| 726 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 727 | // Add copy rules to the manifest to copy each output file from the sbox directory. |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 728 | // to the output directory after running the commands. |
Spandan Das | 33e3097 | 2023-07-13 21:19:12 +0000 | [diff] [blame] | 729 | for _, output := range outputs { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 730 | rel := Rel(r.ctx, r.outDir.String(), output.String()) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 731 | command.CopyAfter = append(command.CopyAfter, &sbox_proto.Copy{ |
Spandan Das | af4ccaa | 2023-06-29 01:15:51 +0000 | [diff] [blame] | 732 | From: proto.String(filepath.Join(r.sboxOutSubDir, rel)), |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 733 | To: proto.String(output.String()), |
| 734 | }) |
| 735 | } |
Colin Cross | 619b9ab | 2020-11-20 18:44:31 +0000 | [diff] [blame] | 736 | |
Colin Cross | 5334edd | 2021-03-11 17:18:21 -0800 | [diff] [blame] | 737 | // Outputs that were marked Temporary will not be checked that they are in the output |
| 738 | // directory by the loop above, check them here. |
| 739 | for path := range r.temporariesSet { |
| 740 | Rel(r.ctx, r.outDir.String(), path.String()) |
| 741 | } |
| 742 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 743 | // Add a hash of the list of input files to the manifest so that the textproto file |
| 744 | // changes when the list of input files changes and causes the sbox rule that |
| 745 | // depends on it to rerun. |
| 746 | command.InputHash = proto.String(hashSrcFiles(inputs)) |
Colin Cross | 619b9ab | 2020-11-20 18:44:31 +0000 | [diff] [blame] | 747 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 748 | // Verify that the manifest textproto is not inside the sbox output directory, otherwise |
| 749 | // it will get deleted when the sbox rule clears its output directory. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 750 | _, manifestInOutDir := MaybeRel(r.ctx, r.outDir.String(), r.sboxManifestPath.String()) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 751 | if manifestInOutDir { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 752 | ReportPathErrorf(r.ctx, "sbox rule %q manifestPath %q must not be in outputDir %q", |
| 753 | name, r.sboxManifestPath.String(), r.outDir.String()) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 754 | } |
| 755 | |
Paul Duffin | 4a3a0a5 | 2023-10-12 15:01:29 +0100 | [diff] [blame] | 756 | // Create a rule to write the manifest as textproto. Pretty print it by indenting and |
| 757 | // splitting across multiple lines. |
| 758 | pbText, err := prototext.MarshalOptions{Indent: " "}.Marshal(&manifest) |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 759 | if err != nil { |
| 760 | ReportPathErrorf(r.ctx, "sbox manifest failed to marshal: %q", err) |
| 761 | } |
Cole Faust | 2f3791f | 2024-11-25 15:49:57 -0800 | [diff] [blame] | 762 | WriteFileRule(r.ctx, r.sboxManifestPath, string(pbText)) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 763 | |
| 764 | // Generate a new string to use as the command line of the sbox rule. This uses |
| 765 | // a RuleBuilderCommand as a convenience method of building the command line, then |
| 766 | // converts it to a string to replace commandString. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 767 | sboxCmd := &RuleBuilderCommand{ |
| 768 | rule: &RuleBuilder{ |
| 769 | ctx: r.ctx, |
| 770 | }, |
| 771 | } |
Colin Cross | 9b698b6 | 2021-12-22 09:55:32 -0800 | [diff] [blame] | 772 | sboxCmd.builtToolWithoutDeps("sbox"). |
Colin Cross | e52c2ac | 2022-03-28 17:03:35 -0700 | [diff] [blame] | 773 | FlagWithArg("--sandbox-path ", shared.TempDirForOutDir(PathForOutput(r.ctx).String())). |
| 774 | FlagWithArg("--output-dir ", r.outDir.String()). |
| 775 | FlagWithInput("--manifest ", r.sboxManifestPath) |
| 776 | |
| 777 | if r.restat { |
| 778 | sboxCmd.Flag("--write-if-changed") |
| 779 | } |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 780 | |
| 781 | // Replace the command string, and add the sbox tool and manifest textproto to the |
| 782 | // dependencies of the final sbox rule. |
Colin Cross | cfec40c | 2019-07-08 17:07:18 -0700 | [diff] [blame] | 783 | commandString = sboxCmd.buf.String() |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 784 | tools = append(tools, sboxCmd.tools...) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 785 | inputs = append(inputs, sboxCmd.inputs...) |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 786 | |
| 787 | if r.rbeParams != nil { |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 788 | // RBE needs a list of input files to copy to the remote builder. For inputs already |
| 789 | // listed in an rsp file, pass the rsp file directly to rewrapper. For the rest, |
| 790 | // create a new rsp file to pass to rewrapper. |
| 791 | var remoteRspFiles Paths |
| 792 | var remoteInputs Paths |
| 793 | |
| 794 | remoteInputs = append(remoteInputs, inputs...) |
| 795 | remoteInputs = append(remoteInputs, tools...) |
| 796 | |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 797 | for _, rspFile := range rspFiles { |
| 798 | remoteInputs = append(remoteInputs, rspFile.file) |
| 799 | remoteRspFiles = append(remoteRspFiles, rspFile.file) |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 800 | } |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 801 | |
| 802 | if len(remoteInputs) > 0 { |
| 803 | inputsListFile := r.sboxManifestPath.ReplaceExtension(r.ctx, "rbe_inputs.list") |
| 804 | writeRspFileRule(r.ctx, inputsListFile, remoteInputs) |
| 805 | remoteRspFiles = append(remoteRspFiles, inputsListFile) |
| 806 | // Add the new rsp file as an extra input to the rule. |
| 807 | inputs = append(inputs, inputsListFile) |
| 808 | } |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 809 | |
| 810 | r.rbeParams.OutputFiles = outputs.Strings() |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 811 | r.rbeParams.RSPFiles = remoteRspFiles.Strings() |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 812 | rewrapperCommand := r.rbeParams.NoVarTemplate(r.ctx.Config().RBEWrapper()) |
| 813 | commandString = rewrapperCommand + " bash -c '" + strings.ReplaceAll(commandString, `'`, `'\''`) + "'" |
| 814 | } |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 817 | // Ninja doesn't like multiple outputs when depfiles are enabled, move all but the first output to |
Colin Cross | 70c4741 | 2021-03-12 17:48:14 -0800 | [diff] [blame] | 818 | // ImplicitOutputs. RuleBuilder doesn't use "$out", so the distinction between Outputs and |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 819 | // ImplicitOutputs doesn't matter. |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 820 | output := outputs[0] |
| 821 | implicitOutputs := outputs[1:] |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 822 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 823 | var rspFile, rspFileContent string |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 824 | var rspFileInputs Paths |
| 825 | if len(rspFiles) > 0 { |
| 826 | // The first rsp files uses Ninja's rsp file support for the rule |
| 827 | rspFile = rspFiles[0].file.String() |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 828 | // Use "$in" for rspFileContent to avoid duplicating the list of files in the dependency |
| 829 | // list and in the contents of the rsp file. Inputs to the rule that are not in the |
| 830 | // rsp file will be listed in Implicits instead of Inputs so they don't show up in "$in". |
| 831 | rspFileContent = "$in" |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 832 | rspFileInputs = append(rspFileInputs, rspFiles[0].paths...) |
| 833 | |
| 834 | for _, rspFile := range rspFiles[1:] { |
| 835 | // Any additional rsp files need an extra rule to write the file. |
| 836 | writeRspFileRule(r.ctx, rspFile.file, rspFile.paths) |
| 837 | // The main rule needs to depend on the inputs listed in the extra rsp file. |
| 838 | inputs = append(inputs, rspFile.paths...) |
| 839 | // The main rule needs to depend on the extra rsp file. |
| 840 | inputs = append(inputs, rspFile.file) |
| 841 | } |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 844 | var pool blueprint.Pool |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 845 | if r.ctx.Config().UseGoma() && r.remoteable.Goma { |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 846 | // When USE_GOMA=true is set and the rule is supported by goma, allow jobs to run outside the local pool. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 847 | } else if r.ctx.Config().UseRBE() && r.remoteable.RBE { |
Ramy Medhat | 944839a | 2020-03-31 22:14:52 -0400 | [diff] [blame] | 848 | // When USE_RBE=true is set and the rule is supported by RBE, use the remotePool. |
| 849 | pool = remotePool |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 850 | } else if r.highmem { |
| 851 | pool = highmemPool |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 852 | } else if r.ctx.Config().UseRemoteBuild() { |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 853 | pool = localPool |
| 854 | } |
| 855 | |
Cole Faust | d7556eb | 2024-12-02 13:18:58 -0800 | [diff] [blame^] | 856 | // If the command length is getting close to linux's maximum, dump it to a file, which allows |
| 857 | // for longer commands. |
| 858 | if len(commandString) > 100000 { |
| 859 | hasher := sha256.New() |
| 860 | hasher.Write([]byte(output.String())) |
| 861 | script := PathForOutput(r.ctx, "rule_builder_scripts", fmt.Sprintf("%x.sh", hasher.Sum(nil))) |
| 862 | commandString = "set -eu\n\n" + commandString + "\n" |
| 863 | WriteExecutableFileRuleVerbatim(r.ctx, script, commandString) |
| 864 | inputs = append(inputs, script) |
| 865 | commandString = script.String() |
| 866 | } |
| 867 | |
Cole Faust | 2f3791f | 2024-11-25 15:49:57 -0800 | [diff] [blame] | 868 | commandString = proptools.NinjaEscape(commandString) |
Sam Delmerico | d46f6c8 | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 869 | |
Devin Moore | b6cc64f | 2024-07-15 22:31:24 +0000 | [diff] [blame] | 870 | args_vars := make([]string, len(r.args)) |
| 871 | i := 0 |
| 872 | for k, _ := range r.args { |
| 873 | args_vars[i] = k |
| 874 | i++ |
| 875 | } |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 876 | r.ctx.Build(r.pctx, BuildParams{ |
Sam Delmerico | 285b66a | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 877 | Rule: r.ctx.Rule(r.pctx, name, blueprint.RuleParams{ |
Sam Delmerico | d46f6c8 | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 878 | Command: commandString, |
Colin Cross | 4502978 | 2021-03-16 16:49:52 -0700 | [diff] [blame] | 879 | CommandDeps: proptools.NinjaEscapeList(tools.Strings()), |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 880 | Restat: r.restat, |
Colin Cross | 4502978 | 2021-03-16 16:49:52 -0700 | [diff] [blame] | 881 | Rspfile: proptools.NinjaEscape(rspFile), |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 882 | RspfileContent: rspFileContent, |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 883 | Pool: pool, |
Devin Moore | b6cc64f | 2024-07-15 22:31:24 +0000 | [diff] [blame] | 884 | }, args_vars...), |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 885 | Inputs: rspFileInputs, |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 886 | Implicits: inputs, |
Colin Cross | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame] | 887 | OrderOnly: r.OrderOnlys(), |
Colin Cross | ae89abe | 2021-04-21 11:45:23 -0700 | [diff] [blame] | 888 | Validations: r.Validations(), |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 889 | Output: output, |
| 890 | ImplicitOutputs: implicitOutputs, |
| 891 | Depfile: depFile, |
| 892 | Deps: depFormat, |
| 893 | Description: desc, |
Devin Moore | b6cc64f | 2024-07-15 22:31:24 +0000 | [diff] [blame] | 894 | Args: r.args, |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 895 | }) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 896 | } |
| 897 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 898 | // RuleBuilderCommand is a builder for a command in a command line. It can be mutated by its methods to add to the |
| 899 | // command and track dependencies. The methods mutate the RuleBuilderCommand in place, as well as return the |
| 900 | // RuleBuilderCommand, so they can be used chained or unchained. All methods that add text implicitly add a single |
| 901 | // space as a separator from the previous method. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 902 | type RuleBuilderCommand struct { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 903 | rule *RuleBuilder |
| 904 | |
Inseob Kim | 93036a5 | 2024-10-25 17:02:21 +0900 | [diff] [blame] | 905 | buf strings.Builder |
| 906 | inputs Paths |
| 907 | implicits Paths |
| 908 | orderOnlys Paths |
| 909 | validations Paths |
| 910 | outputs WritablePaths |
| 911 | depFiles WritablePaths |
| 912 | tools Paths |
| 913 | packagedTools []PackagingSpec |
| 914 | rspFiles []rspFileAndPaths |
| 915 | implicitDirectories DirectoryPaths |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | type rspFileAndPaths struct { |
| 919 | file WritablePath |
| 920 | paths Paths |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 921 | } |
| 922 | |
Paul Duffin | 3866b89 | 2021-10-04 11:24:48 +0100 | [diff] [blame] | 923 | func checkPathNotNil(path Path) { |
| 924 | if path == nil { |
| 925 | panic("rule_builder paths cannot be nil") |
| 926 | } |
| 927 | } |
| 928 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 929 | func (c *RuleBuilderCommand) addInput(path Path) string { |
Paul Duffin | 3866b89 | 2021-10-04 11:24:48 +0100 | [diff] [blame] | 930 | checkPathNotNil(path) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 931 | c.inputs = append(c.inputs, path) |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 932 | return c.PathForInput(path) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 935 | func (c *RuleBuilderCommand) addImplicit(path Path) { |
Paul Duffin | 3866b89 | 2021-10-04 11:24:48 +0100 | [diff] [blame] | 936 | checkPathNotNil(path) |
Ramy Medhat | 2f99eec | 2020-06-13 17:38:27 -0400 | [diff] [blame] | 937 | c.implicits = append(c.implicits, path) |
Ramy Medhat | 2f99eec | 2020-06-13 17:38:27 -0400 | [diff] [blame] | 938 | } |
| 939 | |
Inseob Kim | 93036a5 | 2024-10-25 17:02:21 +0900 | [diff] [blame] | 940 | func (c *RuleBuilderCommand) addImplicitDirectory(path DirectoryPath) { |
| 941 | c.implicitDirectories = append(c.implicitDirectories, path) |
| 942 | } |
| 943 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 944 | func (c *RuleBuilderCommand) addOrderOnly(path Path) { |
Paul Duffin | 3866b89 | 2021-10-04 11:24:48 +0100 | [diff] [blame] | 945 | checkPathNotNil(path) |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 946 | c.orderOnlys = append(c.orderOnlys, path) |
| 947 | } |
| 948 | |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 949 | // PathForInput takes an input path and returns the appropriate path to use on the command line. If |
| 950 | // sbox was enabled via a call to RuleBuilder.Sbox() and the path was an output path it returns a |
| 951 | // path with the placeholder prefix used for outputs in sbox. If sbox is not enabled it returns the |
| 952 | // original path. |
| 953 | func (c *RuleBuilderCommand) PathForInput(path Path) string { |
| 954 | if c.rule.sbox { |
| 955 | rel, inSandbox := c.rule._sboxPathForInputRel(path) |
| 956 | if inSandbox { |
| 957 | rel = filepath.Join(sboxSandboxBaseDir, rel) |
| 958 | } |
| 959 | return rel |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 960 | } else if c.rule.nsjail { |
| 961 | return c.rule.nsjailPathForInputRel(path) |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 962 | } |
| 963 | return path.String() |
| 964 | } |
| 965 | |
| 966 | // PathsForInputs takes a list of input paths and returns the appropriate paths to use on the |
| 967 | // command line. If sbox was enabled via a call to RuleBuilder.Sbox() a path was an output path, it |
| 968 | // returns the path with the placeholder prefix used for outputs in sbox. If sbox is not enabled it |
| 969 | // returns the original paths. |
| 970 | func (c *RuleBuilderCommand) PathsForInputs(paths Paths) []string { |
| 971 | ret := make([]string, len(paths)) |
| 972 | for i, path := range paths { |
| 973 | ret[i] = c.PathForInput(path) |
| 974 | } |
| 975 | return ret |
| 976 | } |
| 977 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 978 | // PathForOutput takes an output path and returns the appropriate path to use on the command |
| 979 | // line. If sbox was enabled via a call to RuleBuilder.Sbox(), it returns a path with the |
| 980 | // placeholder prefix used for outputs in sbox. If sbox is not enabled it returns the |
| 981 | // original path. |
| 982 | func (c *RuleBuilderCommand) PathForOutput(path WritablePath) string { |
| 983 | if c.rule.sbox { |
| 984 | // Errors will be handled in RuleBuilder.Build where we have a context to report them |
| 985 | rel, _, _ := maybeRelErr(c.rule.outDir.String(), path.String()) |
| 986 | return filepath.Join(sboxOutDir, rel) |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 987 | } else if c.rule.nsjail { |
| 988 | // Errors will be handled in RuleBuilder.Build where we have a context to report them |
| 989 | rel, _, _ := maybeRelErr(c.rule.outDir.String(), path.String()) |
| 990 | return filepath.Join(nsjailOutDir, rel) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 991 | } |
| 992 | return path.String() |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 993 | } |
| 994 | |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 995 | func sboxPathForToolRel(ctx BuilderContext, path Path) string { |
| 996 | // Errors will be handled in RuleBuilder.Build where we have a context to report them |
Cole Faust | 3b703f3 | 2023-10-16 13:30:51 -0700 | [diff] [blame] | 997 | toolDir := pathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "") |
Colin Cross | 790ef35 | 2021-10-25 19:15:55 -0700 | [diff] [blame] | 998 | relOutSoong, isRelOutSoong, _ := maybeRelErr(toolDir.String(), path.String()) |
| 999 | if isRelOutSoong { |
| 1000 | // The tool is in the Soong output directory, it will be copied to __SBOX_OUT_DIR__/tools/out |
| 1001 | return filepath.Join(sboxToolsSubDir, "out", relOutSoong) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 1002 | } |
| 1003 | // The tool is in the source directory, it will be copied to __SBOX_OUT_DIR__/tools/src |
| 1004 | return filepath.Join(sboxToolsSubDir, "src", path.String()) |
| 1005 | } |
| 1006 | |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 1007 | func (r *RuleBuilder) _sboxPathForInputRel(path Path) (rel string, inSandbox bool) { |
| 1008 | // Errors will be handled in RuleBuilder.Build where we have a context to report them |
| 1009 | rel, isRelSboxOut, _ := maybeRelErr(r.outDir.String(), path.String()) |
| 1010 | if isRelSboxOut { |
| 1011 | return filepath.Join(sboxOutSubDir, rel), true |
| 1012 | } |
| 1013 | if r.sboxInputs { |
| 1014 | // When sandboxing inputs all inputs have to be copied into the sandbox. Input files that |
| 1015 | // are outputs of other rules could be an arbitrary absolute path if OUT_DIR is set, so they |
| 1016 | // will be copied to relative paths under __SBOX_OUT_DIR__/out. |
Cole Faust | e8561c6 | 2023-11-30 17:26:37 -0800 | [diff] [blame] | 1017 | rel, isRelOut, _ := maybeRelErr(r.ctx.Config().OutDir(), path.String()) |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 1018 | if isRelOut { |
| 1019 | return filepath.Join(sboxOutSubDir, rel), true |
| 1020 | } |
| 1021 | } |
| 1022 | return path.String(), false |
| 1023 | } |
| 1024 | |
| 1025 | func (r *RuleBuilder) sboxPathForInputRel(path Path) string { |
| 1026 | rel, _ := r._sboxPathForInputRel(path) |
| 1027 | return rel |
| 1028 | } |
| 1029 | |
| 1030 | func (r *RuleBuilder) sboxPathsForInputsRel(paths Paths) []string { |
| 1031 | ret := make([]string, len(paths)) |
| 1032 | for i, path := range paths { |
| 1033 | ret[i] = r.sboxPathForInputRel(path) |
| 1034 | } |
| 1035 | return ret |
| 1036 | } |
| 1037 | |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 1038 | func sboxPathForPackagedToolRel(spec PackagingSpec) string { |
| 1039 | return filepath.Join(sboxToolsSubDir, "out", spec.relPathInPackage) |
| 1040 | } |
| 1041 | |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 1042 | func nsjailPathForToolRel(ctx BuilderContext, path Path) string { |
| 1043 | // Errors will be handled in RuleBuilder.Build where we have a context to report them |
| 1044 | toolDir := pathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "") |
| 1045 | relOutSoong, isRelOutSoong, _ := maybeRelErr(toolDir.String(), path.String()) |
| 1046 | if isRelOutSoong { |
| 1047 | // The tool is in the Soong output directory, it will be copied to __SBOX_OUT_DIR__/tools/out |
| 1048 | return filepath.Join(nsjailToolsSubDir, "out", relOutSoong) |
| 1049 | } |
| 1050 | // The tool is in the source directory, it will be copied to __SBOX_OUT_DIR__/tools/src |
| 1051 | return filepath.Join(nsjailToolsSubDir, "src", path.String()) |
| 1052 | } |
| 1053 | |
| 1054 | func (r *RuleBuilder) nsjailPathForInputRel(path Path) string { |
| 1055 | rel, isRelSboxOut, _ := maybeRelErr(r.outDir.String(), path.String()) |
| 1056 | if isRelSboxOut { |
| 1057 | return filepath.Join(nsjailOutDir, rel) |
| 1058 | } |
| 1059 | return path.String() |
| 1060 | } |
| 1061 | |
| 1062 | func (r *RuleBuilder) nsjailPathsForInputsRel(paths Paths) []string { |
| 1063 | ret := make([]string, len(paths)) |
| 1064 | for i, path := range paths { |
| 1065 | ret[i] = r.nsjailPathForInputRel(path) |
| 1066 | } |
| 1067 | return ret |
| 1068 | } |
| 1069 | |
| 1070 | func nsjailPathForPackagedToolRel(spec PackagingSpec) string { |
| 1071 | return filepath.Join(nsjailToolsSubDir, "out", spec.relPathInPackage) |
| 1072 | } |
| 1073 | |
Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 1074 | // PathForPackagedTool takes a PackageSpec for a tool and returns the corresponding path for the |
| 1075 | // tool after copying it into the sandbox. This can be used on the RuleBuilder command line to |
| 1076 | // reference the tool. |
| 1077 | func (c *RuleBuilderCommand) PathForPackagedTool(spec PackagingSpec) string { |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 1078 | if c.rule.sboxTools { |
| 1079 | return filepath.Join(sboxSandboxBaseDir, sboxPathForPackagedToolRel(spec)) |
| 1080 | } else if c.rule.nsjail { |
| 1081 | return nsjailPathForPackagedToolRel(spec) |
| 1082 | } else { |
| 1083 | panic("PathForPackagedTool() requires SandboxTools() or Nsjail()") |
Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 1084 | } |
Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 1087 | // PathForTool takes a path to a tool, which may be an output file or a source file, and returns |
| 1088 | // the corresponding path for the tool in the sbox sandbox if sbox is enabled, or the original path |
| 1089 | // if it is not. This can be used on the RuleBuilder command line to reference the tool. |
| 1090 | func (c *RuleBuilderCommand) PathForTool(path Path) string { |
| 1091 | if c.rule.sbox && c.rule.sboxTools { |
| 1092 | return filepath.Join(sboxSandboxBaseDir, sboxPathForToolRel(c.rule.ctx, path)) |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 1093 | } else if c.rule.nsjail { |
| 1094 | return nsjailPathForToolRel(c.rule.ctx, path) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 1095 | } |
| 1096 | return path.String() |
| 1097 | } |
| 1098 | |
Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 1099 | // PathsForTools takes a list of paths to tools, which may be output files or source files, and |
| 1100 | // returns the corresponding paths for the tools in the sbox sandbox if sbox is enabled, or the |
| 1101 | // original paths if it is not. This can be used on the RuleBuilder command line to reference the tool. |
| 1102 | func (c *RuleBuilderCommand) PathsForTools(paths Paths) []string { |
| 1103 | if c.rule.sbox && c.rule.sboxTools { |
| 1104 | var ret []string |
| 1105 | for _, path := range paths { |
| 1106 | ret = append(ret, filepath.Join(sboxSandboxBaseDir, sboxPathForToolRel(c.rule.ctx, path))) |
| 1107 | } |
| 1108 | return ret |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 1109 | } else if c.rule.nsjail { |
| 1110 | var ret []string |
| 1111 | for _, path := range paths { |
| 1112 | ret = append(ret, nsjailPathForToolRel(c.rule.ctx, path)) |
| 1113 | } |
| 1114 | return ret |
Colin Cross | d11cf62 | 2021-03-23 22:30:35 -0700 | [diff] [blame] | 1115 | } |
| 1116 | return paths.Strings() |
| 1117 | } |
| 1118 | |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 1119 | // PackagedTool adds the specified tool path to the command line. It can only be used with tool |
| 1120 | // sandboxing enabled by SandboxTools(), and will copy the tool into the sandbox. |
| 1121 | func (c *RuleBuilderCommand) PackagedTool(spec PackagingSpec) *RuleBuilderCommand { |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 1122 | c.packagedTools = append(c.packagedTools, spec) |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 1123 | if c.rule.sboxTools { |
| 1124 | c.Text(sboxPathForPackagedToolRel(spec)) |
| 1125 | } else if c.rule.nsjail { |
| 1126 | c.Text(nsjailPathForPackagedToolRel(spec)) |
| 1127 | } else { |
| 1128 | panic("PackagedTool() requires SandboxTools() or Nsjail()") |
| 1129 | } |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 1130 | return c |
| 1131 | } |
| 1132 | |
| 1133 | // ImplicitPackagedTool copies the specified tool into the sandbox without modifying the command |
| 1134 | // line. It can only be used with tool sandboxing enabled by SandboxTools(). |
| 1135 | func (c *RuleBuilderCommand) ImplicitPackagedTool(spec PackagingSpec) *RuleBuilderCommand { |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 1136 | if !c.rule.sboxTools && !c.rule.nsjail { |
| 1137 | panic("ImplicitPackagedTool() requires SandboxTools() or Nsjail()") |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | c.packagedTools = append(c.packagedTools, spec) |
| 1141 | return c |
| 1142 | } |
| 1143 | |
| 1144 | // ImplicitPackagedTools copies the specified tools into the sandbox without modifying the command |
| 1145 | // line. It can only be used with tool sandboxing enabled by SandboxTools(). |
| 1146 | func (c *RuleBuilderCommand) ImplicitPackagedTools(specs []PackagingSpec) *RuleBuilderCommand { |
Inseob Kim | f7cd03e | 2024-09-06 17:25:00 +0900 | [diff] [blame] | 1147 | if !c.rule.sboxTools && !c.rule.nsjail { |
| 1148 | panic("ImplicitPackagedTools() requires SandboxTools() or Nsjail()") |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | c.packagedTools = append(c.packagedTools, specs...) |
| 1152 | return c |
| 1153 | } |
| 1154 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1155 | // Text adds the specified raw text to the command line. The text should not contain input or output paths or the |
| 1156 | // rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1157 | func (c *RuleBuilderCommand) Text(text string) *RuleBuilderCommand { |
Colin Cross | cfec40c | 2019-07-08 17:07:18 -0700 | [diff] [blame] | 1158 | if c.buf.Len() > 0 { |
| 1159 | c.buf.WriteByte(' ') |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1160 | } |
Colin Cross | cfec40c | 2019-07-08 17:07:18 -0700 | [diff] [blame] | 1161 | c.buf.WriteString(text) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1162 | return c |
| 1163 | } |
| 1164 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1165 | // Textf adds the specified formatted text to the command line. The text should not contain input or output paths or |
| 1166 | // the rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1167 | func (c *RuleBuilderCommand) Textf(format string, a ...interface{}) *RuleBuilderCommand { |
| 1168 | return c.Text(fmt.Sprintf(format, a...)) |
| 1169 | } |
| 1170 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1171 | // Flag adds the specified raw text to the command line. The text should not contain input or output paths or the |
| 1172 | // rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1173 | func (c *RuleBuilderCommand) Flag(flag string) *RuleBuilderCommand { |
| 1174 | return c.Text(flag) |
| 1175 | } |
| 1176 | |
Colin Cross | ab05443 | 2019-07-15 16:13:59 -0700 | [diff] [blame] | 1177 | // OptionalFlag adds the specified raw text to the command line if it is not nil. The text should not contain input or |
| 1178 | // output paths or the rule will not have them listed in its dependencies or outputs. |
| 1179 | func (c *RuleBuilderCommand) OptionalFlag(flag *string) *RuleBuilderCommand { |
| 1180 | if flag != nil { |
| 1181 | c.Text(*flag) |
| 1182 | } |
| 1183 | |
| 1184 | return c |
| 1185 | } |
| 1186 | |
Colin Cross | 92b7d58 | 2019-03-29 15:32:51 -0700 | [diff] [blame] | 1187 | // Flags adds the specified raw text to the command line. The text should not contain input or output paths or the |
| 1188 | // rule will not have them listed in its dependencies or outputs. |
| 1189 | func (c *RuleBuilderCommand) Flags(flags []string) *RuleBuilderCommand { |
| 1190 | for _, flag := range flags { |
| 1191 | c.Text(flag) |
| 1192 | } |
| 1193 | return c |
| 1194 | } |
| 1195 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1196 | // FlagWithArg adds the specified flag and argument text to the command line, with no separator between them. The flag |
| 1197 | // and argument should not contain input or output paths or the rule will not have them listed in its dependencies or |
| 1198 | // outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1199 | func (c *RuleBuilderCommand) FlagWithArg(flag, arg string) *RuleBuilderCommand { |
| 1200 | return c.Text(flag + arg) |
| 1201 | } |
| 1202 | |
Colin Cross | c7ed004 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 1203 | // FlagForEachArg adds the specified flag joined with each argument to the command line. The result is identical to |
| 1204 | // calling FlagWithArg for argument. |
| 1205 | func (c *RuleBuilderCommand) FlagForEachArg(flag string, args []string) *RuleBuilderCommand { |
| 1206 | for _, arg := range args { |
| 1207 | c.FlagWithArg(flag, arg) |
| 1208 | } |
| 1209 | return c |
| 1210 | } |
| 1211 | |
Roland Levillain | 2da5d9a | 2019-02-27 16:56:41 +0000 | [diff] [blame] | 1212 | // FlagWithList adds the specified flag and list of arguments to the command line, with the arguments joined by sep |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1213 | // and no separator between the flag and arguments. The flag and arguments should not contain input or output paths or |
| 1214 | // the rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1215 | func (c *RuleBuilderCommand) FlagWithList(flag string, list []string, sep string) *RuleBuilderCommand { |
| 1216 | return c.Text(flag + strings.Join(list, sep)) |
| 1217 | } |
| 1218 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1219 | // Tool adds the specified tool path to the command line. The path will be also added to the dependencies returned by |
| 1220 | // RuleBuilder.Tools. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1221 | func (c *RuleBuilderCommand) Tool(path Path) *RuleBuilderCommand { |
Paul Duffin | 3866b89 | 2021-10-04 11:24:48 +0100 | [diff] [blame] | 1222 | checkPathNotNil(path) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1223 | c.tools = append(c.tools, path) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 1224 | return c.Text(c.PathForTool(path)) |
| 1225 | } |
| 1226 | |
| 1227 | // Tool adds the specified tool path to the dependencies returned by RuleBuilder.Tools. |
| 1228 | func (c *RuleBuilderCommand) ImplicitTool(path Path) *RuleBuilderCommand { |
Paul Duffin | 3866b89 | 2021-10-04 11:24:48 +0100 | [diff] [blame] | 1229 | checkPathNotNil(path) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 1230 | c.tools = append(c.tools, path) |
| 1231 | return c |
| 1232 | } |
| 1233 | |
| 1234 | // Tool adds the specified tool path to the dependencies returned by RuleBuilder.Tools. |
| 1235 | func (c *RuleBuilderCommand) ImplicitTools(paths Paths) *RuleBuilderCommand { |
Paul Duffin | 3866b89 | 2021-10-04 11:24:48 +0100 | [diff] [blame] | 1236 | for _, path := range paths { |
| 1237 | c.ImplicitTool(path) |
| 1238 | } |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 1239 | return c |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1240 | } |
| 1241 | |
Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 1242 | // BuiltTool adds the specified tool path that was built using a host Soong module to the command line. The path will |
| 1243 | // be also added to the dependencies returned by RuleBuilder.Tools. |
| 1244 | // |
| 1245 | // It is equivalent to: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 1246 | // |
| 1247 | // cmd.Tool(ctx.Config().HostToolPath(ctx, tool)) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 1248 | func (c *RuleBuilderCommand) BuiltTool(tool string) *RuleBuilderCommand { |
Colin Cross | 9b698b6 | 2021-12-22 09:55:32 -0800 | [diff] [blame] | 1249 | if c.rule.ctx.Config().UseHostMusl() { |
| 1250 | // If the host is using musl, assume that the tool was built against musl libc and include |
| 1251 | // libc_musl.so in the sandbox. |
| 1252 | // TODO(ccross): if we supported adding new dependencies during GenerateAndroidBuildActions |
| 1253 | // this could be a dependency + TransitivePackagingSpecs. |
| 1254 | c.ImplicitTool(c.rule.ctx.Config().HostJNIToolPath(c.rule.ctx, "libc_musl")) |
| 1255 | } |
| 1256 | return c.builtToolWithoutDeps(tool) |
| 1257 | } |
| 1258 | |
| 1259 | // builtToolWithoutDeps is similar to BuiltTool, but doesn't add any dependencies. It is used |
| 1260 | // internally by RuleBuilder for helper tools that are known to be compiled statically. |
| 1261 | func (c *RuleBuilderCommand) builtToolWithoutDeps(tool string) *RuleBuilderCommand { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 1262 | return c.Tool(c.rule.ctx.Config().HostToolPath(c.rule.ctx, tool)) |
Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | // PrebuiltBuildTool adds the specified tool path from prebuils/build-tools. The path will be also added to the |
| 1266 | // dependencies returned by RuleBuilder.Tools. |
| 1267 | // |
| 1268 | // It is equivalent to: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 1269 | // |
| 1270 | // cmd.Tool(ctx.Config().PrebuiltBuildTool(ctx, tool)) |
Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 1271 | func (c *RuleBuilderCommand) PrebuiltBuildTool(ctx PathContext, tool string) *RuleBuilderCommand { |
| 1272 | return c.Tool(ctx.Config().PrebuiltBuildTool(ctx, tool)) |
| 1273 | } |
| 1274 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1275 | // Input adds the specified input path to the command line. The path will also be added to the dependencies returned by |
| 1276 | // RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1277 | func (c *RuleBuilderCommand) Input(path Path) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1278 | return c.Text(c.addInput(path)) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1279 | } |
| 1280 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1281 | // Inputs adds the specified input paths to the command line, separated by spaces. The paths will also be added to the |
| 1282 | // dependencies returned by RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1283 | func (c *RuleBuilderCommand) Inputs(paths Paths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1284 | for _, path := range paths { |
| 1285 | c.Input(path) |
| 1286 | } |
| 1287 | return c |
| 1288 | } |
| 1289 | |
| 1290 | // Implicit adds the specified input path to the dependencies returned by RuleBuilder.Inputs without modifying the |
| 1291 | // command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1292 | func (c *RuleBuilderCommand) Implicit(path Path) *RuleBuilderCommand { |
Ramy Medhat | 2f99eec | 2020-06-13 17:38:27 -0400 | [diff] [blame] | 1293 | c.addImplicit(path) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1294 | return c |
| 1295 | } |
| 1296 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1297 | // Implicits adds the specified input paths to the dependencies returned by RuleBuilder.Inputs without modifying the |
| 1298 | // command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1299 | func (c *RuleBuilderCommand) Implicits(paths Paths) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1300 | for _, path := range paths { |
Ramy Medhat | 2f99eec | 2020-06-13 17:38:27 -0400 | [diff] [blame] | 1301 | c.addImplicit(path) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1302 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1303 | return c |
| 1304 | } |
| 1305 | |
Inseob Kim | 93036a5 | 2024-10-25 17:02:21 +0900 | [diff] [blame] | 1306 | // ImplicitDirectory adds the specified input directory to the dependencies without modifying the |
| 1307 | // command line. Added directories will be bind-mounted for the nsjail. |
| 1308 | func (c *RuleBuilderCommand) ImplicitDirectory(path DirectoryPath) *RuleBuilderCommand { |
| 1309 | if !c.rule.nsjail { |
| 1310 | panic("ImplicitDirectory() must be called after Nsjail()") |
| 1311 | } |
| 1312 | c.addImplicitDirectory(path) |
| 1313 | return c |
| 1314 | } |
| 1315 | |
Ramy Medhat | 2f99eec | 2020-06-13 17:38:27 -0400 | [diff] [blame] | 1316 | // GetImplicits returns the command's implicit inputs. |
| 1317 | func (c *RuleBuilderCommand) GetImplicits() Paths { |
| 1318 | return c.implicits |
| 1319 | } |
| 1320 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 1321 | // OrderOnly adds the specified input path to the dependencies returned by RuleBuilder.OrderOnlys |
| 1322 | // without modifying the command line. |
| 1323 | func (c *RuleBuilderCommand) OrderOnly(path Path) *RuleBuilderCommand { |
| 1324 | c.addOrderOnly(path) |
| 1325 | return c |
| 1326 | } |
| 1327 | |
| 1328 | // OrderOnlys adds the specified input paths to the dependencies returned by RuleBuilder.OrderOnlys |
| 1329 | // without modifying the command line. |
| 1330 | func (c *RuleBuilderCommand) OrderOnlys(paths Paths) *RuleBuilderCommand { |
| 1331 | for _, path := range paths { |
| 1332 | c.addOrderOnly(path) |
| 1333 | } |
| 1334 | return c |
| 1335 | } |
| 1336 | |
Colin Cross | ae89abe | 2021-04-21 11:45:23 -0700 | [diff] [blame] | 1337 | // Validation adds the specified input path to the validation dependencies by |
| 1338 | // RuleBuilder.Validations without modifying the command line. |
| 1339 | func (c *RuleBuilderCommand) Validation(path Path) *RuleBuilderCommand { |
Paul Duffin | 3866b89 | 2021-10-04 11:24:48 +0100 | [diff] [blame] | 1340 | checkPathNotNil(path) |
Colin Cross | ae89abe | 2021-04-21 11:45:23 -0700 | [diff] [blame] | 1341 | c.validations = append(c.validations, path) |
| 1342 | return c |
| 1343 | } |
| 1344 | |
| 1345 | // Validations adds the specified input paths to the validation dependencies by |
| 1346 | // RuleBuilder.Validations without modifying the command line. |
| 1347 | func (c *RuleBuilderCommand) Validations(paths Paths) *RuleBuilderCommand { |
Paul Duffin | 3866b89 | 2021-10-04 11:24:48 +0100 | [diff] [blame] | 1348 | for _, path := range paths { |
| 1349 | c.Validation(path) |
| 1350 | } |
Colin Cross | ae89abe | 2021-04-21 11:45:23 -0700 | [diff] [blame] | 1351 | return c |
| 1352 | } |
| 1353 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1354 | // Output adds the specified output path to the command line. The path will also be added to the outputs returned by |
| 1355 | // RuleBuilder.Outputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1356 | func (c *RuleBuilderCommand) Output(path WritablePath) *RuleBuilderCommand { |
Paul Duffin | 3866b89 | 2021-10-04 11:24:48 +0100 | [diff] [blame] | 1357 | checkPathNotNil(path) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1358 | c.outputs = append(c.outputs, path) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 1359 | return c.Text(c.PathForOutput(path)) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1360 | } |
| 1361 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1362 | // Outputs adds the specified output paths to the command line, separated by spaces. The paths will also be added to |
| 1363 | // the outputs returned by RuleBuilder.Outputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1364 | func (c *RuleBuilderCommand) Outputs(paths WritablePaths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1365 | for _, path := range paths { |
| 1366 | c.Output(path) |
| 1367 | } |
| 1368 | return c |
| 1369 | } |
| 1370 | |
Dan Willemsen | 1945a4b | 2019-06-04 17:10:41 -0700 | [diff] [blame] | 1371 | // OutputDir adds the output directory to the command line. This is only available when used with RuleBuilder.Sbox, |
| 1372 | // and will be the temporary output directory managed by sbox, not the final one. |
Anas Sulaiman | b4dff13 | 2024-02-07 21:58:46 +0000 | [diff] [blame] | 1373 | func (c *RuleBuilderCommand) OutputDir(subPathComponents ...string) *RuleBuilderCommand { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 1374 | if !c.rule.sbox { |
Dan Willemsen | 1945a4b | 2019-06-04 17:10:41 -0700 | [diff] [blame] | 1375 | panic("OutputDir only valid with Sbox") |
| 1376 | } |
Anas Sulaiman | b4dff13 | 2024-02-07 21:58:46 +0000 | [diff] [blame] | 1377 | path := sboxOutDir |
| 1378 | if len(subPathComponents) > 0 { |
| 1379 | path = filepath.Join(append([]string{sboxOutDir}, subPathComponents...)...) |
| 1380 | } |
| 1381 | return c.Text(path) |
Dan Willemsen | 1945a4b | 2019-06-04 17:10:41 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 1384 | // DepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles and adds it to the command |
| 1385 | // line, and causes RuleBuilder.Build file to set the depfile flag for ninja. If multiple depfiles are added to |
| 1386 | // commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the depfiles together. |
| 1387 | func (c *RuleBuilderCommand) DepFile(path WritablePath) *RuleBuilderCommand { |
Paul Duffin | 3866b89 | 2021-10-04 11:24:48 +0100 | [diff] [blame] | 1388 | checkPathNotNil(path) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 1389 | c.depFiles = append(c.depFiles, path) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 1390 | return c.Text(c.PathForOutput(path)) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1393 | // ImplicitOutput adds the specified output path to the dependencies returned by RuleBuilder.Outputs without modifying |
| 1394 | // the command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1395 | func (c *RuleBuilderCommand) ImplicitOutput(path WritablePath) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1396 | c.outputs = append(c.outputs, path) |
| 1397 | return c |
| 1398 | } |
| 1399 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1400 | // ImplicitOutputs adds the specified output paths to the dependencies returned by RuleBuilder.Outputs without modifying |
| 1401 | // the command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1402 | func (c *RuleBuilderCommand) ImplicitOutputs(paths WritablePaths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1403 | c.outputs = append(c.outputs, paths...) |
| 1404 | return c |
| 1405 | } |
| 1406 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 1407 | // ImplicitDepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles without modifying |
| 1408 | // the command line, and causes RuleBuilder.Build file to set the depfile flag for ninja. If multiple depfiles |
| 1409 | // are added to commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the |
| 1410 | // depfiles together. |
| 1411 | func (c *RuleBuilderCommand) ImplicitDepFile(path WritablePath) *RuleBuilderCommand { |
| 1412 | c.depFiles = append(c.depFiles, path) |
| 1413 | return c |
| 1414 | } |
| 1415 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1416 | // FlagWithInput adds the specified flag and input path to the command line, with no separator between them. The path |
| 1417 | // will also be added to the dependencies returned by RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1418 | func (c *RuleBuilderCommand) FlagWithInput(flag string, path Path) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1419 | return c.Text(flag + c.addInput(path)) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1420 | } |
| 1421 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1422 | // FlagWithInputList adds the specified flag and input paths to the command line, with the inputs joined by sep |
| 1423 | // and no separator between the flag and inputs. The input paths will also be added to the dependencies returned by |
| 1424 | // RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1425 | func (c *RuleBuilderCommand) FlagWithInputList(flag string, paths Paths, sep string) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1426 | strs := make([]string, len(paths)) |
| 1427 | for i, path := range paths { |
| 1428 | strs[i] = c.addInput(path) |
| 1429 | } |
| 1430 | return c.FlagWithList(flag, strs, sep) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1431 | } |
| 1432 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1433 | // FlagForEachInput adds the specified flag joined with each input path to the command line. The input paths will also |
| 1434 | // be added to the dependencies returned by RuleBuilder.Inputs. The result is identical to calling FlagWithInput for |
| 1435 | // each input path. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1436 | func (c *RuleBuilderCommand) FlagForEachInput(flag string, paths Paths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1437 | for _, path := range paths { |
| 1438 | c.FlagWithInput(flag, path) |
| 1439 | } |
| 1440 | return c |
| 1441 | } |
| 1442 | |
| 1443 | // FlagWithOutput adds the specified flag and output path to the command line, with no separator between them. The path |
| 1444 | // will also be added to the outputs returned by RuleBuilder.Outputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1445 | func (c *RuleBuilderCommand) FlagWithOutput(flag string, path WritablePath) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1446 | c.outputs = append(c.outputs, path) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 1447 | return c.Text(flag + c.PathForOutput(path)) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1448 | } |
| 1449 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 1450 | // FlagWithDepFile adds the specified flag and depfile path to the command line, with no separator between them. The path |
| 1451 | // will also be added to the outputs returned by RuleBuilder.Outputs. |
| 1452 | func (c *RuleBuilderCommand) FlagWithDepFile(flag string, path WritablePath) *RuleBuilderCommand { |
| 1453 | c.depFiles = append(c.depFiles, path) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 1454 | return c.Text(flag + c.PathForOutput(path)) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 1455 | } |
| 1456 | |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 1457 | // FlagWithRspFileInputList adds the specified flag and path to an rspfile to the command line, with |
| 1458 | // no separator between them. The paths will be written to the rspfile. If sbox is enabled, the |
| 1459 | // rspfile must be outside the sbox directory. The first use of FlagWithRspFileInputList in any |
| 1460 | // RuleBuilderCommand of a RuleBuilder will use Ninja's rsp file support for the rule, additional |
| 1461 | // uses will result in an auxiliary rules to write the rspFile contents. |
Colin Cross | 70c4741 | 2021-03-12 17:48:14 -0800 | [diff] [blame] | 1462 | func (c *RuleBuilderCommand) FlagWithRspFileInputList(flag string, rspFile WritablePath, paths Paths) *RuleBuilderCommand { |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 1463 | // Use an empty slice if paths is nil, the non-nil slice is used as an indicator that the rsp file must be |
| 1464 | // generated. |
| 1465 | if paths == nil { |
| 1466 | paths = Paths{} |
| 1467 | } |
| 1468 | |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 1469 | c.rspFiles = append(c.rspFiles, rspFileAndPaths{rspFile, paths}) |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 1470 | |
Colin Cross | 70c4741 | 2021-03-12 17:48:14 -0800 | [diff] [blame] | 1471 | if c.rule.sbox { |
| 1472 | if _, isRel, _ := maybeRelErr(c.rule.outDir.String(), rspFile.String()); isRel { |
| 1473 | panic(fmt.Errorf("FlagWithRspFileInputList rspfile %q must not be inside out dir %q", |
| 1474 | rspFile.String(), c.rule.outDir.String())) |
| 1475 | } |
| 1476 | } |
| 1477 | |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 1478 | c.FlagWithArg(flag, c.PathForInput(rspFile)) |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 1479 | return c |
| 1480 | } |
| 1481 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1482 | // String returns the command line. |
| 1483 | func (c *RuleBuilderCommand) String() string { |
Colin Cross | cfec40c | 2019-07-08 17:07:18 -0700 | [diff] [blame] | 1484 | return c.buf.String() |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1485 | } |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 1486 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 1487 | // RuleBuilderSboxProtoForTests takes the BuildParams for the manifest passed to RuleBuilder.Sbox() |
| 1488 | // and returns sbox testproto generated by the RuleBuilder. |
Colin Cross | f61d03d | 2023-11-02 16:56:39 -0700 | [diff] [blame] | 1489 | func RuleBuilderSboxProtoForTests(t *testing.T, ctx *TestContext, params TestingBuildParams) *sbox_proto.Manifest { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 1490 | t.Helper() |
Colin Cross | f61d03d | 2023-11-02 16:56:39 -0700 | [diff] [blame] | 1491 | content := ContentFromFileRuleForTests(t, ctx, params) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 1492 | manifest := sbox_proto.Manifest{} |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 1493 | err := prototext.Unmarshal([]byte(content), &manifest) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 1494 | if err != nil { |
| 1495 | t.Fatalf("failed to unmarshal manifest: %s", err.Error()) |
| 1496 | } |
| 1497 | return &manifest |
| 1498 | } |
| 1499 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 1500 | func ninjaNameEscape(s string) string { |
| 1501 | b := []byte(s) |
| 1502 | escaped := false |
| 1503 | for i, c := range b { |
| 1504 | valid := (c >= 'a' && c <= 'z') || |
| 1505 | (c >= 'A' && c <= 'Z') || |
| 1506 | (c >= '0' && c <= '9') || |
| 1507 | (c == '_') || |
| 1508 | (c == '-') || |
| 1509 | (c == '.') |
| 1510 | if !valid { |
| 1511 | b[i] = '_' |
| 1512 | escaped = true |
| 1513 | } |
| 1514 | } |
| 1515 | if escaped { |
| 1516 | s = string(b) |
| 1517 | } |
| 1518 | return s |
| 1519 | } |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 1520 | |
| 1521 | // hashSrcFiles returns a hash of the list of source files. It is used to ensure the command line |
| 1522 | // or the sbox textproto manifest change even if the input files are not listed on the command line. |
| 1523 | func hashSrcFiles(srcFiles Paths) string { |
| 1524 | h := sha256.New() |
| 1525 | srcFileList := strings.Join(srcFiles.Strings(), "\n") |
| 1526 | h.Write([]byte(srcFileList)) |
| 1527 | return fmt.Sprintf("%x", h.Sum(nil)) |
| 1528 | } |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 1529 | |
| 1530 | // BuilderContextForTesting returns a BuilderContext for the given config that can be used for tests |
| 1531 | // that need to call methods that take a BuilderContext. |
| 1532 | func BuilderContextForTesting(config Config) BuilderContext { |
| 1533 | pathCtx := PathContextForTesting(config) |
| 1534 | return builderContextForTests{ |
| 1535 | PathContext: pathCtx, |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | type builderContextForTests struct { |
| 1540 | PathContext |
| 1541 | } |
| 1542 | |
| 1543 | func (builderContextForTests) Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule { |
| 1544 | return nil |
| 1545 | } |
| 1546 | func (builderContextForTests) Build(PackageContext, BuildParams) {} |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 1547 | |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 1548 | func writeRspFileRule(ctx BuilderContext, rspFile WritablePath, paths Paths) { |
| 1549 | buf := &strings.Builder{} |
| 1550 | err := response.WriteRspFile(buf, paths.Strings()) |
| 1551 | if err != nil { |
| 1552 | // There should never be I/O errors writing to a bytes.Buffer. |
| 1553 | panic(err) |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 1554 | } |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 1555 | WriteFileRule(ctx, rspFile, buf.String()) |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 1556 | } |