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