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