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 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 25 | "github.com/golang/protobuf/proto" |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 26 | "github.com/google/blueprint" |
| 27 | "github.com/google/blueprint/proptools" |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 28 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 29 | "android/soong/cmd/sbox/sbox_proto" |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 30 | "android/soong/shared" |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 31 | ) |
| 32 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 33 | const sboxSandboxBaseDir = "__SBOX_SANDBOX_DIR__" |
| 34 | const sboxOutSubDir = "out" |
| 35 | const sboxOutDir = sboxSandboxBaseDir + "/" + sboxOutSubDir |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 36 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 37 | // RuleBuilder provides an alternative to ModuleContext.Rule and ModuleContext.Build to add a command line to the build |
| 38 | // graph. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 39 | type RuleBuilder struct { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 40 | commands []*RuleBuilderCommand |
| 41 | installs RuleBuilderInstalls |
| 42 | temporariesSet map[WritablePath]bool |
| 43 | restat bool |
| 44 | sbox bool |
| 45 | highmem bool |
| 46 | remoteable RemoteRuleSupports |
| 47 | sboxOutDir WritablePath |
| 48 | sboxManifestPath WritablePath |
| 49 | missingDeps []string |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 52 | // NewRuleBuilder returns a newly created RuleBuilder. |
| 53 | func NewRuleBuilder() *RuleBuilder { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 54 | return &RuleBuilder{ |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 55 | temporariesSet: make(map[WritablePath]bool), |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 56 | } |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // RuleBuilderInstall is a tuple of install from and to locations. |
| 60 | type RuleBuilderInstall struct { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 61 | From Path |
| 62 | To string |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 65 | type RuleBuilderInstalls []RuleBuilderInstall |
| 66 | |
| 67 | // String returns the RuleBuilderInstalls in the form used by $(call copy-many-files) in Make, a space separated |
| 68 | // list of from:to tuples. |
| 69 | func (installs RuleBuilderInstalls) String() string { |
| 70 | sb := strings.Builder{} |
| 71 | for i, install := range installs { |
| 72 | if i != 0 { |
| 73 | sb.WriteRune(' ') |
| 74 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 75 | sb.WriteString(install.From.String()) |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 76 | sb.WriteRune(':') |
| 77 | sb.WriteString(install.To) |
| 78 | } |
| 79 | return sb.String() |
| 80 | } |
| 81 | |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 82 | // MissingDeps adds modules to the list of missing dependencies. If MissingDeps |
| 83 | // is called with a non-empty input, any call to Build will result in a rule |
| 84 | // that will print an error listing the missing dependencies and fail. |
| 85 | // MissingDeps should only be called if Config.AllowMissingDependencies() is |
| 86 | // true. |
| 87 | func (r *RuleBuilder) MissingDeps(missingDeps []string) { |
| 88 | r.missingDeps = append(r.missingDeps, missingDeps...) |
| 89 | } |
| 90 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 91 | // Restat marks the rule as a restat rule, which will be passed to ModuleContext.Rule in BuildParams.Restat. |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 92 | // |
| 93 | // Restat is not compatible with Sbox() |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 94 | func (r *RuleBuilder) Restat() *RuleBuilder { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 95 | if r.sbox { |
| 96 | panic("Restat() is not compatible with Sbox()") |
| 97 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 98 | r.restat = true |
| 99 | return r |
| 100 | } |
| 101 | |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 102 | // HighMem marks the rule as a high memory rule, which will limit how many run in parallel with other high memory |
| 103 | // rules. |
| 104 | func (r *RuleBuilder) HighMem() *RuleBuilder { |
| 105 | r.highmem = true |
| 106 | return r |
| 107 | } |
| 108 | |
| 109 | // Remoteable marks the rule as supporting remote execution. |
| 110 | func (r *RuleBuilder) Remoteable(supports RemoteRuleSupports) *RuleBuilder { |
| 111 | r.remoteable = supports |
| 112 | return r |
| 113 | } |
| 114 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 115 | // Sbox marks the rule as needing to be wrapped by sbox. The outputDir should point to the output |
| 116 | // directory that sbox will wipe. It should not be written to by any other rule. manifestPath should |
| 117 | // point to a location where sbox's manifest will be written and must be outside outputDir. sbox |
| 118 | // will ensure that all outputs have been written, and will discard any output files that were not |
| 119 | // specified. |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 120 | // |
| 121 | // Sbox is not compatible with Restat() |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 122 | func (r *RuleBuilder) Sbox(outputDir WritablePath, manifestPath WritablePath) *RuleBuilder { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 123 | if r.sbox { |
| 124 | panic("Sbox() may not be called more than once") |
| 125 | } |
| 126 | if len(r.commands) > 0 { |
| 127 | panic("Sbox() may not be called after Command()") |
| 128 | } |
| 129 | if r.restat { |
| 130 | panic("Sbox() is not compatible with Restat()") |
| 131 | } |
| 132 | r.sbox = true |
| 133 | r.sboxOutDir = outputDir |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 134 | r.sboxManifestPath = manifestPath |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 135 | return r |
| 136 | } |
| 137 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 138 | // Install associates an output of the rule with an install location, which can be retrieved later using |
| 139 | // RuleBuilder.Installs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 140 | func (r *RuleBuilder) Install(from Path, to string) { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 141 | r.installs = append(r.installs, RuleBuilderInstall{from, to}) |
| 142 | } |
| 143 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 144 | // Command returns a new RuleBuilderCommand for the rule. The commands will be ordered in the rule by when they were |
| 145 | // created by this method. That can be mutated through their methods in any order, as long as the mutations do not |
| 146 | // race with any call to Build. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 147 | func (r *RuleBuilder) Command() *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 148 | command := &RuleBuilderCommand{ |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 149 | sbox: r.sbox, |
| 150 | outDir: r.sboxOutDir, |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 151 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 152 | r.commands = append(r.commands, command) |
| 153 | return command |
| 154 | } |
| 155 | |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 156 | // Temporary marks an output of a command as an intermediate file that will be used as an input to another command |
| 157 | // in the same rule, and should not be listed in Outputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 158 | func (r *RuleBuilder) Temporary(path WritablePath) { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 159 | r.temporariesSet[path] = true |
| 160 | } |
| 161 | |
| 162 | // DeleteTemporaryFiles adds a command to the rule that deletes any outputs that have been marked using Temporary |
| 163 | // when the rule runs. DeleteTemporaryFiles should be called after all calls to Temporary. |
| 164 | func (r *RuleBuilder) DeleteTemporaryFiles() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 165 | var temporariesList WritablePaths |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 166 | |
| 167 | for intermediate := range r.temporariesSet { |
| 168 | temporariesList = append(temporariesList, intermediate) |
| 169 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 170 | |
| 171 | sort.Slice(temporariesList, func(i, j int) bool { |
| 172 | return temporariesList[i].String() < temporariesList[j].String() |
| 173 | }) |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 174 | |
| 175 | r.Command().Text("rm").Flag("-f").Outputs(temporariesList) |
| 176 | } |
| 177 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 178 | // 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] | 179 | // input paths, such as RuleBuilderCommand.Input, RuleBuilderCommand.Implicit, or |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 180 | // RuleBuilderCommand.FlagWithInput. Inputs to a command that are also outputs of another command |
| 181 | // 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] | 182 | func (r *RuleBuilder) Inputs() Paths { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 183 | outputs := r.outputSet() |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 184 | depFiles := r.depFileSet() |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 185 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 186 | inputs := make(map[string]Path) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 187 | for _, c := range r.commands { |
Ramy Medhat | 2f99eec | 2020-06-13 17:38:27 -0400 | [diff] [blame] | 188 | for _, input := range append(c.inputs, c.implicits...) { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 189 | inputStr := input.String() |
| 190 | if _, isOutput := outputs[inputStr]; !isOutput { |
| 191 | if _, isDepFile := depFiles[inputStr]; !isDepFile { |
| 192 | inputs[input.String()] = input |
| 193 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 198 | var inputList Paths |
| 199 | for _, input := range inputs { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 200 | inputList = append(inputList, input) |
| 201 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 202 | |
| 203 | sort.Slice(inputList, func(i, j int) bool { |
| 204 | return inputList[i].String() < inputList[j].String() |
| 205 | }) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 206 | |
| 207 | return inputList |
| 208 | } |
| 209 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 210 | // OrderOnlys returns the list of paths that were passed to the RuleBuilderCommand.OrderOnly or |
| 211 | // RuleBuilderCommand.OrderOnlys. The list is sorted and duplicates removed. |
| 212 | func (r *RuleBuilder) OrderOnlys() Paths { |
| 213 | orderOnlys := make(map[string]Path) |
| 214 | for _, c := range r.commands { |
| 215 | for _, orderOnly := range c.orderOnlys { |
| 216 | orderOnlys[orderOnly.String()] = orderOnly |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | var orderOnlyList Paths |
| 221 | for _, orderOnly := range orderOnlys { |
| 222 | orderOnlyList = append(orderOnlyList, orderOnly) |
| 223 | } |
| 224 | |
| 225 | sort.Slice(orderOnlyList, func(i, j int) bool { |
| 226 | return orderOnlyList[i].String() < orderOnlyList[j].String() |
| 227 | }) |
| 228 | |
| 229 | return orderOnlyList |
| 230 | } |
| 231 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 232 | func (r *RuleBuilder) outputSet() map[string]WritablePath { |
| 233 | outputs := make(map[string]WritablePath) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 234 | for _, c := range r.commands { |
| 235 | for _, output := range c.outputs { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 236 | outputs[output.String()] = output |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | return outputs |
| 240 | } |
| 241 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 242 | // Outputs returns the list of paths that were passed to the RuleBuilderCommand methods that take |
| 243 | // output paths, such as RuleBuilderCommand.Output, RuleBuilderCommand.ImplicitOutput, or |
| 244 | // RuleBuilderCommand.FlagWithInput. The list is sorted and duplicates removed. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 245 | func (r *RuleBuilder) Outputs() WritablePaths { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 246 | outputs := r.outputSet() |
| 247 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 248 | var outputList WritablePaths |
| 249 | for _, output := range outputs { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 250 | if !r.temporariesSet[output] { |
| 251 | outputList = append(outputList, output) |
| 252 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 253 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 254 | |
| 255 | sort.Slice(outputList, func(i, j int) bool { |
| 256 | return outputList[i].String() < outputList[j].String() |
| 257 | }) |
| 258 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 259 | return outputList |
| 260 | } |
| 261 | |
Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame] | 262 | func (r *RuleBuilder) symlinkOutputSet() map[string]WritablePath { |
| 263 | symlinkOutputs := make(map[string]WritablePath) |
| 264 | for _, c := range r.commands { |
| 265 | for _, symlinkOutput := range c.symlinkOutputs { |
| 266 | symlinkOutputs[symlinkOutput.String()] = symlinkOutput |
| 267 | } |
| 268 | } |
| 269 | return symlinkOutputs |
| 270 | } |
| 271 | |
| 272 | // SymlinkOutputs returns the list of paths that the executor (Ninja) would |
| 273 | // verify, after build edge completion, that: |
| 274 | // |
| 275 | // 1) Created output symlinks match the list of paths in this list exactly (no more, no fewer) |
| 276 | // 2) Created output files are *not* declared in this list. |
| 277 | // |
| 278 | // These symlink outputs are expected to be a subset of outputs or implicit |
| 279 | // outputs, or they would fail validation at build param construction time |
| 280 | // later, to support other non-rule-builder approaches for constructing |
| 281 | // statements. |
| 282 | func (r *RuleBuilder) SymlinkOutputs() WritablePaths { |
| 283 | symlinkOutputs := r.symlinkOutputSet() |
| 284 | |
| 285 | var symlinkOutputList WritablePaths |
| 286 | for _, symlinkOutput := range symlinkOutputs { |
| 287 | symlinkOutputList = append(symlinkOutputList, symlinkOutput) |
| 288 | } |
| 289 | |
| 290 | sort.Slice(symlinkOutputList, func(i, j int) bool { |
| 291 | return symlinkOutputList[i].String() < symlinkOutputList[j].String() |
| 292 | }) |
| 293 | |
| 294 | return symlinkOutputList |
| 295 | } |
| 296 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 297 | func (r *RuleBuilder) depFileSet() map[string]WritablePath { |
| 298 | depFiles := make(map[string]WritablePath) |
| 299 | for _, c := range r.commands { |
| 300 | for _, depFile := range c.depFiles { |
| 301 | depFiles[depFile.String()] = depFile |
| 302 | } |
| 303 | } |
| 304 | return depFiles |
| 305 | } |
| 306 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 307 | // DepFiles returns the list of paths that were passed to the RuleBuilderCommand methods that take depfile paths, such |
| 308 | // as RuleBuilderCommand.DepFile or RuleBuilderCommand.FlagWithDepFile. |
| 309 | func (r *RuleBuilder) DepFiles() WritablePaths { |
| 310 | var depFiles WritablePaths |
| 311 | |
| 312 | for _, c := range r.commands { |
| 313 | for _, depFile := range c.depFiles { |
| 314 | depFiles = append(depFiles, depFile) |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | return depFiles |
| 319 | } |
| 320 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 321 | // Installs returns the list of tuples passed to Install. |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 322 | func (r *RuleBuilder) Installs() RuleBuilderInstalls { |
| 323 | return append(RuleBuilderInstalls(nil), r.installs...) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 326 | func (r *RuleBuilder) toolsSet() map[string]Path { |
| 327 | tools := make(map[string]Path) |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 328 | for _, c := range r.commands { |
| 329 | for _, tool := range c.tools { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 330 | tools[tool.String()] = tool |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
| 334 | return tools |
| 335 | } |
| 336 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 337 | // Tools returns the list of paths that were passed to the RuleBuilderCommand.Tool method. The |
| 338 | // list is sorted and duplicates removed. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 339 | func (r *RuleBuilder) Tools() Paths { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 340 | toolsSet := r.toolsSet() |
| 341 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 342 | var toolsList Paths |
| 343 | for _, tool := range toolsSet { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 344 | toolsList = append(toolsList, tool) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 345 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 346 | |
| 347 | sort.Slice(toolsList, func(i, j int) bool { |
| 348 | return toolsList[i].String() < toolsList[j].String() |
| 349 | }) |
| 350 | |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 351 | return toolsList |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 354 | // RspFileInputs returns the list of paths that were passed to the RuleBuilderCommand.FlagWithRspFileInputList method. |
| 355 | func (r *RuleBuilder) RspFileInputs() Paths { |
| 356 | var rspFileInputs Paths |
| 357 | for _, c := range r.commands { |
| 358 | if c.rspFileInputs != nil { |
| 359 | if rspFileInputs != nil { |
| 360 | panic("Multiple commands in a rule may not have rsp file inputs") |
| 361 | } |
| 362 | rspFileInputs = c.rspFileInputs |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | return rspFileInputs |
| 367 | } |
| 368 | |
| 369 | // 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] | 370 | func (r *RuleBuilder) Commands() []string { |
| 371 | var commands []string |
| 372 | for _, c := range r.commands { |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 373 | commands = append(commands, c.String()) |
| 374 | } |
| 375 | return commands |
| 376 | } |
| 377 | |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 378 | // NinjaEscapedCommands returns a slice containing the built command line after ninja escaping for each call to |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 379 | // RuleBuilder.Command. |
| 380 | func (r *RuleBuilder) NinjaEscapedCommands() []string { |
| 381 | var commands []string |
| 382 | for _, c := range r.commands { |
| 383 | commands = append(commands, c.NinjaEscapedString()) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 384 | } |
| 385 | return commands |
| 386 | } |
| 387 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 388 | // BuilderContext is a subset of ModuleContext and SingletonContext. |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 389 | type BuilderContext interface { |
| 390 | PathContext |
| 391 | Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule |
| 392 | Build(PackageContext, BuildParams) |
| 393 | } |
| 394 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 395 | var _ BuilderContext = ModuleContext(nil) |
| 396 | var _ BuilderContext = SingletonContext(nil) |
| 397 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 398 | func (r *RuleBuilder) depFileMergerCmd(ctx PathContext, depFiles WritablePaths) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 399 | return r.Command(). |
Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 400 | BuiltTool(ctx, "dep_fixer"). |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 401 | Inputs(depFiles.Paths()) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 404 | // Build adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for |
| 405 | // Outputs. |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 406 | func (r *RuleBuilder) Build(pctx PackageContext, ctx BuilderContext, name string, desc string) { |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 407 | name = ninjaNameEscape(name) |
| 408 | |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 409 | if len(r.missingDeps) > 0 { |
| 410 | ctx.Build(pctx, BuildParams{ |
| 411 | Rule: ErrorRule, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 412 | Outputs: r.Outputs(), |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 413 | OrderOnly: r.OrderOnlys(), |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 414 | Description: desc, |
| 415 | Args: map[string]string{ |
| 416 | "error": "missing dependencies: " + strings.Join(r.missingDeps, ", "), |
| 417 | }, |
| 418 | }) |
| 419 | return |
| 420 | } |
| 421 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 422 | var depFile WritablePath |
| 423 | var depFormat blueprint.Deps |
| 424 | if depFiles := r.DepFiles(); len(depFiles) > 0 { |
| 425 | depFile = depFiles[0] |
| 426 | depFormat = blueprint.DepsGCC |
| 427 | if len(depFiles) > 1 { |
| 428 | // Add a command locally that merges all depfiles together into the first depfile. |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 429 | r.depFileMergerCmd(ctx, depFiles) |
| 430 | |
| 431 | if r.sbox { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 432 | // Check for Rel() errors, as all depfiles should be in the output dir. Errors |
| 433 | // will be reported to the ctx. |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 434 | for _, path := range depFiles[1:] { |
| 435 | Rel(ctx, r.sboxOutDir.String(), path.String()) |
| 436 | } |
| 437 | } |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 441 | tools := r.Tools() |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 442 | commands := r.NinjaEscapedCommands() |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 443 | outputs := r.Outputs() |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 444 | inputs := r.Inputs() |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 445 | |
| 446 | if len(commands) == 0 { |
| 447 | return |
| 448 | } |
| 449 | if len(outputs) == 0 { |
| 450 | panic("No outputs specified from any Commands") |
| 451 | } |
| 452 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 453 | commandString := strings.Join(commands, " && ") |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 454 | |
| 455 | if r.sbox { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 456 | // If running the command inside sbox, write the rule data out to an sbox |
| 457 | // manifest.textproto. |
| 458 | manifest := sbox_proto.Manifest{} |
| 459 | command := sbox_proto.Command{} |
| 460 | manifest.Commands = append(manifest.Commands, &command) |
| 461 | command.Command = proto.String(commandString) |
Colin Cross | 151b9ff | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 462 | |
Colin Cross | 619b9ab | 2020-11-20 18:44:31 +0000 | [diff] [blame] | 463 | if depFile != nil { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 464 | manifest.OutputDepfile = proto.String(depFile.String()) |
Colin Cross | 619b9ab | 2020-11-20 18:44:31 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 467 | // Add copy rules to the manifest to copy each output file from the sbox directory. |
| 468 | // to the output directory. |
| 469 | sboxOutputs := make([]string, len(outputs)) |
| 470 | for i, output := range outputs { |
| 471 | rel := Rel(ctx, r.sboxOutDir.String(), output.String()) |
| 472 | sboxOutputs[i] = filepath.Join(sboxOutDir, rel) |
| 473 | command.CopyAfter = append(command.CopyAfter, &sbox_proto.Copy{ |
| 474 | From: proto.String(filepath.Join(sboxOutSubDir, rel)), |
| 475 | To: proto.String(output.String()), |
| 476 | }) |
| 477 | } |
Colin Cross | 619b9ab | 2020-11-20 18:44:31 +0000 | [diff] [blame] | 478 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 479 | // Add a hash of the list of input files to the manifest so that the textproto file |
| 480 | // changes when the list of input files changes and causes the sbox rule that |
| 481 | // depends on it to rerun. |
| 482 | command.InputHash = proto.String(hashSrcFiles(inputs)) |
Colin Cross | 619b9ab | 2020-11-20 18:44:31 +0000 | [diff] [blame] | 483 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 484 | // Verify that the manifest textproto is not inside the sbox output directory, otherwise |
| 485 | // it will get deleted when the sbox rule clears its output directory. |
| 486 | _, manifestInOutDir := MaybeRel(ctx, r.sboxOutDir.String(), r.sboxManifestPath.String()) |
| 487 | if manifestInOutDir { |
| 488 | ReportPathErrorf(ctx, "sbox rule %q manifestPath %q must not be in outputDir %q", |
| 489 | name, r.sboxManifestPath.String(), r.sboxOutDir.String()) |
| 490 | } |
| 491 | |
| 492 | // Create a rule to write the manifest as a the textproto. |
| 493 | WriteFileRule(ctx, r.sboxManifestPath, proto.MarshalTextString(&manifest)) |
| 494 | |
| 495 | // Generate a new string to use as the command line of the sbox rule. This uses |
| 496 | // a RuleBuilderCommand as a convenience method of building the command line, then |
| 497 | // converts it to a string to replace commandString. |
| 498 | sboxCmd := &RuleBuilderCommand{} |
| 499 | sboxCmd.Text("rm -rf").Output(r.sboxOutDir) |
| 500 | sboxCmd.Text("&&") |
| 501 | sboxCmd.BuiltTool(ctx, "sbox"). |
| 502 | Flag("--sandbox-path").Text(shared.TempDirForOutDir(PathForOutput(ctx).String())). |
| 503 | Flag("--manifest").Input(r.sboxManifestPath) |
| 504 | |
| 505 | // Replace the command string, and add the sbox tool and manifest textproto to the |
| 506 | // dependencies of the final sbox rule. |
Colin Cross | cfec40c | 2019-07-08 17:07:18 -0700 | [diff] [blame] | 507 | commandString = sboxCmd.buf.String() |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 508 | tools = append(tools, sboxCmd.tools...) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 509 | inputs = append(inputs, sboxCmd.inputs...) |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 510 | } else { |
| 511 | // If not using sbox the rule will run the command directly, put the hash of the |
| 512 | // list of input files in a comment at the end of the command line to ensure ninja |
| 513 | // reruns the rule when the list of input files changes. |
| 514 | commandString += " # hash of input list: " + hashSrcFiles(inputs) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 517 | // Ninja doesn't like multiple outputs when depfiles are enabled, move all but the first output to |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 518 | // ImplicitOutputs. RuleBuilder only uses "$out" for the rsp file location, so the distinction between Outputs and |
| 519 | // ImplicitOutputs doesn't matter. |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 520 | output := outputs[0] |
| 521 | implicitOutputs := outputs[1:] |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 522 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 523 | var rspFile, rspFileContent string |
| 524 | rspFileInputs := r.RspFileInputs() |
| 525 | if rspFileInputs != nil { |
| 526 | rspFile = "$out.rsp" |
| 527 | rspFileContent = "$in" |
| 528 | } |
| 529 | |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 530 | var pool blueprint.Pool |
Ramy Medhat | 8ea054a | 2020-01-27 14:19:44 -0500 | [diff] [blame] | 531 | if ctx.Config().UseGoma() && r.remoteable.Goma { |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 532 | // When USE_GOMA=true is set and the rule is supported by goma, allow jobs to run outside the local pool. |
Ramy Medhat | 8ea054a | 2020-01-27 14:19:44 -0500 | [diff] [blame] | 533 | } else if ctx.Config().UseRBE() && r.remoteable.RBE { |
Ramy Medhat | 944839a | 2020-03-31 22:14:52 -0400 | [diff] [blame] | 534 | // When USE_RBE=true is set and the rule is supported by RBE, use the remotePool. |
| 535 | pool = remotePool |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 536 | } else if r.highmem { |
| 537 | pool = highmemPool |
| 538 | } else if ctx.Config().UseRemoteBuild() { |
| 539 | pool = localPool |
| 540 | } |
| 541 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 542 | ctx.Build(pctx, BuildParams{ |
| 543 | Rule: ctx.Rule(pctx, name, blueprint.RuleParams{ |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 544 | Command: commandString, |
| 545 | CommandDeps: tools.Strings(), |
| 546 | Restat: r.restat, |
| 547 | Rspfile: rspFile, |
| 548 | RspfileContent: rspFileContent, |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 549 | Pool: pool, |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 550 | }), |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 551 | Inputs: rspFileInputs, |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 552 | Implicits: inputs, |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 553 | Output: output, |
| 554 | ImplicitOutputs: implicitOutputs, |
Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame] | 555 | SymlinkOutputs: r.SymlinkOutputs(), |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 556 | Depfile: depFile, |
| 557 | Deps: depFormat, |
| 558 | Description: desc, |
| 559 | }) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 560 | } |
| 561 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 562 | // RuleBuilderCommand is a builder for a command in a command line. It can be mutated by its methods to add to the |
| 563 | // command and track dependencies. The methods mutate the RuleBuilderCommand in place, as well as return the |
| 564 | // RuleBuilderCommand, so they can be used chained or unchained. All methods that add text implicitly add a single |
| 565 | // space as a separator from the previous method. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 566 | type RuleBuilderCommand struct { |
Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame] | 567 | buf strings.Builder |
| 568 | inputs Paths |
| 569 | implicits Paths |
| 570 | orderOnlys Paths |
| 571 | outputs WritablePaths |
| 572 | symlinkOutputs WritablePaths |
| 573 | depFiles WritablePaths |
| 574 | tools Paths |
| 575 | rspFileInputs Paths |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 576 | |
| 577 | // spans [start,end) of the command that should not be ninja escaped |
| 578 | unescapedSpans [][2]int |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 579 | |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 580 | sbox bool |
| 581 | // outDir is the directory that will contain the output files of the rules. sbox will copy |
| 582 | // the output files from the sandbox directory to this directory when it finishes. |
| 583 | outDir WritablePath |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | func (c *RuleBuilderCommand) addInput(path Path) string { |
| 587 | if c.sbox { |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 588 | if rel, isRel, _ := maybeRelErr(c.outDir.String(), path.String()); isRel { |
| 589 | return filepath.Join(sboxOutDir, rel) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | c.inputs = append(c.inputs, path) |
| 593 | return path.String() |
| 594 | } |
| 595 | |
Ramy Medhat | 2f99eec | 2020-06-13 17:38:27 -0400 | [diff] [blame] | 596 | func (c *RuleBuilderCommand) addImplicit(path Path) string { |
| 597 | if c.sbox { |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 598 | if rel, isRel, _ := maybeRelErr(c.outDir.String(), path.String()); isRel { |
| 599 | return filepath.Join(sboxOutDir, rel) |
Ramy Medhat | 2f99eec | 2020-06-13 17:38:27 -0400 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | c.implicits = append(c.implicits, path) |
| 603 | return path.String() |
| 604 | } |
| 605 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 606 | func (c *RuleBuilderCommand) addOrderOnly(path Path) { |
| 607 | c.orderOnlys = append(c.orderOnlys, path) |
| 608 | } |
| 609 | |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 610 | func (c *RuleBuilderCommand) outputStr(path WritablePath) string { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 611 | if c.sbox { |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 612 | return SboxPathForOutput(path, c.outDir) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 613 | } |
| 614 | return path.String() |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 615 | } |
| 616 | |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 617 | // SboxPathForOutput takes an output path and the out directory passed to RuleBuilder.Sbox(), |
| 618 | // and returns the corresponding path for the output in the sbox sandbox. This can be used |
| 619 | // on the RuleBuilder command line to reference the output. |
| 620 | func SboxPathForOutput(path WritablePath, outDir WritablePath) string { |
| 621 | // Errors will be handled in RuleBuilder.Build where we have a context to report them |
| 622 | rel, _, _ := maybeRelErr(outDir.String(), path.String()) |
| 623 | return filepath.Join(sboxOutDir, rel) |
| 624 | } |
| 625 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 626 | // Text adds the specified raw text to the command line. The text should not contain input or output paths or the |
| 627 | // rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 628 | func (c *RuleBuilderCommand) Text(text string) *RuleBuilderCommand { |
Colin Cross | cfec40c | 2019-07-08 17:07:18 -0700 | [diff] [blame] | 629 | if c.buf.Len() > 0 { |
| 630 | c.buf.WriteByte(' ') |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 631 | } |
Colin Cross | cfec40c | 2019-07-08 17:07:18 -0700 | [diff] [blame] | 632 | c.buf.WriteString(text) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 633 | return c |
| 634 | } |
| 635 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 636 | // Textf adds the specified formatted text to the command line. The text should not contain input or output paths or |
| 637 | // the rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 638 | func (c *RuleBuilderCommand) Textf(format string, a ...interface{}) *RuleBuilderCommand { |
| 639 | return c.Text(fmt.Sprintf(format, a...)) |
| 640 | } |
| 641 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 642 | // Flag adds the specified raw text to the command line. The text should not contain input or output paths or the |
| 643 | // rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 644 | func (c *RuleBuilderCommand) Flag(flag string) *RuleBuilderCommand { |
| 645 | return c.Text(flag) |
| 646 | } |
| 647 | |
Colin Cross | ab05443 | 2019-07-15 16:13:59 -0700 | [diff] [blame] | 648 | // OptionalFlag adds the specified raw text to the command line if it is not nil. The text should not contain input or |
| 649 | // output paths or the rule will not have them listed in its dependencies or outputs. |
| 650 | func (c *RuleBuilderCommand) OptionalFlag(flag *string) *RuleBuilderCommand { |
| 651 | if flag != nil { |
| 652 | c.Text(*flag) |
| 653 | } |
| 654 | |
| 655 | return c |
| 656 | } |
| 657 | |
Colin Cross | 92b7d58 | 2019-03-29 15:32:51 -0700 | [diff] [blame] | 658 | // Flags adds the specified raw text to the command line. The text should not contain input or output paths or the |
| 659 | // rule will not have them listed in its dependencies or outputs. |
| 660 | func (c *RuleBuilderCommand) Flags(flags []string) *RuleBuilderCommand { |
| 661 | for _, flag := range flags { |
| 662 | c.Text(flag) |
| 663 | } |
| 664 | return c |
| 665 | } |
| 666 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 667 | // FlagWithArg adds the specified flag and argument text to the command line, with no separator between them. The flag |
| 668 | // and argument should not contain input or output paths or the rule will not have them listed in its dependencies or |
| 669 | // outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 670 | func (c *RuleBuilderCommand) FlagWithArg(flag, arg string) *RuleBuilderCommand { |
| 671 | return c.Text(flag + arg) |
| 672 | } |
| 673 | |
Colin Cross | c7ed004 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 674 | // FlagForEachArg adds the specified flag joined with each argument to the command line. The result is identical to |
| 675 | // calling FlagWithArg for argument. |
| 676 | func (c *RuleBuilderCommand) FlagForEachArg(flag string, args []string) *RuleBuilderCommand { |
| 677 | for _, arg := range args { |
| 678 | c.FlagWithArg(flag, arg) |
| 679 | } |
| 680 | return c |
| 681 | } |
| 682 | |
Roland Levillain | 2da5d9a | 2019-02-27 16:56:41 +0000 | [diff] [blame] | 683 | // 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] | 684 | // and no separator between the flag and arguments. The flag and arguments should not contain input or output paths or |
| 685 | // the rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 686 | func (c *RuleBuilderCommand) FlagWithList(flag string, list []string, sep string) *RuleBuilderCommand { |
| 687 | return c.Text(flag + strings.Join(list, sep)) |
| 688 | } |
| 689 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 690 | // Tool adds the specified tool path to the command line. The path will be also added to the dependencies returned by |
| 691 | // RuleBuilder.Tools. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 692 | func (c *RuleBuilderCommand) Tool(path Path) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 693 | c.tools = append(c.tools, path) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 694 | return c.Text(path.String()) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 695 | } |
| 696 | |
Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 697 | // BuiltTool adds the specified tool path that was built using a host Soong module to the command line. The path will |
| 698 | // be also added to the dependencies returned by RuleBuilder.Tools. |
| 699 | // |
| 700 | // It is equivalent to: |
| 701 | // cmd.Tool(ctx.Config().HostToolPath(ctx, tool)) |
| 702 | func (c *RuleBuilderCommand) BuiltTool(ctx PathContext, tool string) *RuleBuilderCommand { |
| 703 | return c.Tool(ctx.Config().HostToolPath(ctx, tool)) |
| 704 | } |
| 705 | |
| 706 | // PrebuiltBuildTool adds the specified tool path from prebuils/build-tools. The path will be also added to the |
| 707 | // dependencies returned by RuleBuilder.Tools. |
| 708 | // |
| 709 | // It is equivalent to: |
| 710 | // cmd.Tool(ctx.Config().PrebuiltBuildTool(ctx, tool)) |
| 711 | func (c *RuleBuilderCommand) PrebuiltBuildTool(ctx PathContext, tool string) *RuleBuilderCommand { |
| 712 | return c.Tool(ctx.Config().PrebuiltBuildTool(ctx, tool)) |
| 713 | } |
| 714 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 715 | // Input adds the specified input path to the command line. The path will also be added to the dependencies returned by |
| 716 | // RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 717 | func (c *RuleBuilderCommand) Input(path Path) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 718 | return c.Text(c.addInput(path)) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 719 | } |
| 720 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 721 | // Inputs adds the specified input paths to the command line, separated by spaces. The paths will also be added to the |
| 722 | // dependencies returned by RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 723 | func (c *RuleBuilderCommand) Inputs(paths Paths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 724 | for _, path := range paths { |
| 725 | c.Input(path) |
| 726 | } |
| 727 | return c |
| 728 | } |
| 729 | |
| 730 | // Implicit adds the specified input path to the dependencies returned by RuleBuilder.Inputs without modifying the |
| 731 | // command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 732 | func (c *RuleBuilderCommand) Implicit(path Path) *RuleBuilderCommand { |
Ramy Medhat | 2f99eec | 2020-06-13 17:38:27 -0400 | [diff] [blame] | 733 | c.addImplicit(path) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 734 | return c |
| 735 | } |
| 736 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 737 | // Implicits adds the specified input paths to the dependencies returned by RuleBuilder.Inputs without modifying the |
| 738 | // command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 739 | func (c *RuleBuilderCommand) Implicits(paths Paths) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 740 | for _, path := range paths { |
Ramy Medhat | 2f99eec | 2020-06-13 17:38:27 -0400 | [diff] [blame] | 741 | c.addImplicit(path) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 742 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 743 | return c |
| 744 | } |
| 745 | |
Ramy Medhat | 2f99eec | 2020-06-13 17:38:27 -0400 | [diff] [blame] | 746 | // GetImplicits returns the command's implicit inputs. |
| 747 | func (c *RuleBuilderCommand) GetImplicits() Paths { |
| 748 | return c.implicits |
| 749 | } |
| 750 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 751 | // OrderOnly adds the specified input path to the dependencies returned by RuleBuilder.OrderOnlys |
| 752 | // without modifying the command line. |
| 753 | func (c *RuleBuilderCommand) OrderOnly(path Path) *RuleBuilderCommand { |
| 754 | c.addOrderOnly(path) |
| 755 | return c |
| 756 | } |
| 757 | |
| 758 | // OrderOnlys adds the specified input paths to the dependencies returned by RuleBuilder.OrderOnlys |
| 759 | // without modifying the command line. |
| 760 | func (c *RuleBuilderCommand) OrderOnlys(paths Paths) *RuleBuilderCommand { |
| 761 | for _, path := range paths { |
| 762 | c.addOrderOnly(path) |
| 763 | } |
| 764 | return c |
| 765 | } |
| 766 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 767 | // Output adds the specified output path to the command line. The path will also be added to the outputs returned by |
| 768 | // RuleBuilder.Outputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 769 | func (c *RuleBuilderCommand) Output(path WritablePath) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 770 | c.outputs = append(c.outputs, path) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 771 | return c.Text(c.outputStr(path)) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 772 | } |
| 773 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 774 | // Outputs adds the specified output paths to the command line, separated by spaces. The paths will also be added to |
| 775 | // the outputs returned by RuleBuilder.Outputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 776 | func (c *RuleBuilderCommand) Outputs(paths WritablePaths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 777 | for _, path := range paths { |
| 778 | c.Output(path) |
| 779 | } |
| 780 | return c |
| 781 | } |
| 782 | |
Dan Willemsen | 1945a4b | 2019-06-04 17:10:41 -0700 | [diff] [blame] | 783 | // OutputDir adds the output directory to the command line. This is only available when used with RuleBuilder.Sbox, |
| 784 | // and will be the temporary output directory managed by sbox, not the final one. |
| 785 | func (c *RuleBuilderCommand) OutputDir() *RuleBuilderCommand { |
| 786 | if !c.sbox { |
| 787 | panic("OutputDir only valid with Sbox") |
| 788 | } |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 789 | return c.Text(sboxOutDir) |
Dan Willemsen | 1945a4b | 2019-06-04 17:10:41 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 792 | // DepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles and adds it to the command |
| 793 | // line, and causes RuleBuilder.Build file to set the depfile flag for ninja. If multiple depfiles are added to |
| 794 | // commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the depfiles together. |
| 795 | func (c *RuleBuilderCommand) DepFile(path WritablePath) *RuleBuilderCommand { |
| 796 | c.depFiles = append(c.depFiles, path) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 797 | return c.Text(c.outputStr(path)) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 800 | // ImplicitOutput adds the specified output path to the dependencies returned by RuleBuilder.Outputs without modifying |
| 801 | // the command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 802 | func (c *RuleBuilderCommand) ImplicitOutput(path WritablePath) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 803 | c.outputs = append(c.outputs, path) |
| 804 | return c |
| 805 | } |
| 806 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 807 | // ImplicitOutputs adds the specified output paths to the dependencies returned by RuleBuilder.Outputs without modifying |
| 808 | // the command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 809 | func (c *RuleBuilderCommand) ImplicitOutputs(paths WritablePaths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 810 | c.outputs = append(c.outputs, paths...) |
| 811 | return c |
| 812 | } |
| 813 | |
Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame] | 814 | // ImplicitSymlinkOutput declares the specified path as an implicit output that |
| 815 | // will be a symlink instead of a regular file. Does not modify the command |
| 816 | // line. |
| 817 | func (c *RuleBuilderCommand) ImplicitSymlinkOutput(path WritablePath) *RuleBuilderCommand { |
| 818 | c.symlinkOutputs = append(c.symlinkOutputs, path) |
| 819 | return c.ImplicitOutput(path) |
| 820 | } |
| 821 | |
| 822 | // ImplicitSymlinkOutputs declares the specified paths as implicit outputs that |
| 823 | // will be a symlinks instead of regular files. Does not modify the command |
| 824 | // line. |
| 825 | func (c *RuleBuilderCommand) ImplicitSymlinkOutputs(paths WritablePaths) *RuleBuilderCommand { |
| 826 | for _, path := range paths { |
| 827 | c.ImplicitSymlinkOutput(path) |
| 828 | } |
| 829 | return c |
| 830 | } |
| 831 | |
| 832 | // SymlinkOutput declares the specified path as an output that will be a symlink |
| 833 | // instead of a regular file. Modifies the command line. |
| 834 | func (c *RuleBuilderCommand) SymlinkOutput(path WritablePath) *RuleBuilderCommand { |
| 835 | c.symlinkOutputs = append(c.symlinkOutputs, path) |
| 836 | return c.Output(path) |
| 837 | } |
| 838 | |
| 839 | // SymlinkOutputsl declares the specified paths as outputs that will be symlinks |
| 840 | // instead of regular files. Modifies the command line. |
| 841 | func (c *RuleBuilderCommand) SymlinkOutputs(paths WritablePaths) *RuleBuilderCommand { |
| 842 | for _, path := range paths { |
| 843 | c.SymlinkOutput(path) |
| 844 | } |
| 845 | return c |
| 846 | } |
| 847 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 848 | // ImplicitDepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles without modifying |
| 849 | // the command line, and causes RuleBuilder.Build file to set the depfile flag for ninja. If multiple depfiles |
| 850 | // are added to commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the |
| 851 | // depfiles together. |
| 852 | func (c *RuleBuilderCommand) ImplicitDepFile(path WritablePath) *RuleBuilderCommand { |
| 853 | c.depFiles = append(c.depFiles, path) |
| 854 | return c |
| 855 | } |
| 856 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 857 | // FlagWithInput adds the specified flag and input path to the command line, with no separator between them. The path |
| 858 | // will also be added to the dependencies returned by RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 859 | func (c *RuleBuilderCommand) FlagWithInput(flag string, path Path) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 860 | return c.Text(flag + c.addInput(path)) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 861 | } |
| 862 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 863 | // FlagWithInputList adds the specified flag and input paths to the command line, with the inputs joined by sep |
| 864 | // and no separator between the flag and inputs. The input paths will also be added to the dependencies returned by |
| 865 | // RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 866 | func (c *RuleBuilderCommand) FlagWithInputList(flag string, paths Paths, sep string) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 867 | strs := make([]string, len(paths)) |
| 868 | for i, path := range paths { |
| 869 | strs[i] = c.addInput(path) |
| 870 | } |
| 871 | return c.FlagWithList(flag, strs, sep) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 872 | } |
| 873 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 874 | // FlagForEachInput adds the specified flag joined with each input path to the command line. The input paths will also |
| 875 | // be added to the dependencies returned by RuleBuilder.Inputs. The result is identical to calling FlagWithInput for |
| 876 | // each input path. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 877 | func (c *RuleBuilderCommand) FlagForEachInput(flag string, paths Paths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 878 | for _, path := range paths { |
| 879 | c.FlagWithInput(flag, path) |
| 880 | } |
| 881 | return c |
| 882 | } |
| 883 | |
| 884 | // FlagWithOutput adds the specified flag and output path to the command line, with no separator between them. The path |
| 885 | // will also be added to the outputs returned by RuleBuilder.Outputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 886 | func (c *RuleBuilderCommand) FlagWithOutput(flag string, path WritablePath) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 887 | c.outputs = append(c.outputs, path) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 888 | return c.Text(flag + c.outputStr(path)) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 889 | } |
| 890 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 891 | // FlagWithDepFile adds the specified flag and depfile path to the command line, with no separator between them. The path |
| 892 | // will also be added to the outputs returned by RuleBuilder.Outputs. |
| 893 | func (c *RuleBuilderCommand) FlagWithDepFile(flag string, path WritablePath) *RuleBuilderCommand { |
| 894 | c.depFiles = append(c.depFiles, path) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 895 | return c.Text(flag + c.outputStr(path)) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 896 | } |
| 897 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 898 | // FlagWithRspFileInputList adds the specified flag and path to an rspfile to the command line, with no separator |
| 899 | // between them. The paths will be written to the rspfile. |
| 900 | func (c *RuleBuilderCommand) FlagWithRspFileInputList(flag string, paths Paths) *RuleBuilderCommand { |
| 901 | if c.rspFileInputs != nil { |
| 902 | panic("FlagWithRspFileInputList cannot be called if rsp file inputs have already been provided") |
| 903 | } |
| 904 | |
| 905 | // Use an empty slice if paths is nil, the non-nil slice is used as an indicator that the rsp file must be |
| 906 | // generated. |
| 907 | if paths == nil { |
| 908 | paths = Paths{} |
| 909 | } |
| 910 | |
| 911 | c.rspFileInputs = paths |
| 912 | |
| 913 | rspFile := "$out.rsp" |
| 914 | c.FlagWithArg(flag, rspFile) |
| 915 | c.unescapedSpans = append(c.unescapedSpans, [2]int{c.buf.Len() - len(rspFile), c.buf.Len()}) |
| 916 | return c |
| 917 | } |
| 918 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 919 | // String returns the command line. |
| 920 | func (c *RuleBuilderCommand) String() string { |
Colin Cross | cfec40c | 2019-07-08 17:07:18 -0700 | [diff] [blame] | 921 | return c.buf.String() |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 922 | } |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 923 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 924 | // String returns the command line. |
| 925 | func (c *RuleBuilderCommand) NinjaEscapedString() string { |
| 926 | return ninjaEscapeExceptForSpans(c.String(), c.unescapedSpans) |
| 927 | } |
| 928 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 929 | // RuleBuilderSboxProtoForTests takes the BuildParams for the manifest passed to RuleBuilder.Sbox() |
| 930 | // and returns sbox testproto generated by the RuleBuilder. |
| 931 | func RuleBuilderSboxProtoForTests(t *testing.T, params TestingBuildParams) *sbox_proto.Manifest { |
| 932 | t.Helper() |
| 933 | content := ContentFromFileRuleForTests(t, params) |
| 934 | manifest := sbox_proto.Manifest{} |
| 935 | err := proto.UnmarshalText(content, &manifest) |
| 936 | if err != nil { |
| 937 | t.Fatalf("failed to unmarshal manifest: %s", err.Error()) |
| 938 | } |
| 939 | return &manifest |
| 940 | } |
| 941 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 942 | func ninjaEscapeExceptForSpans(s string, spans [][2]int) string { |
| 943 | if len(spans) == 0 { |
| 944 | return proptools.NinjaEscape(s) |
| 945 | } |
| 946 | |
| 947 | sb := strings.Builder{} |
| 948 | sb.Grow(len(s) * 11 / 10) |
| 949 | |
| 950 | i := 0 |
| 951 | for _, span := range spans { |
| 952 | sb.WriteString(proptools.NinjaEscape(s[i:span[0]])) |
| 953 | sb.WriteString(s[span[0]:span[1]]) |
| 954 | i = span[1] |
| 955 | } |
| 956 | sb.WriteString(proptools.NinjaEscape(s[i:])) |
| 957 | |
| 958 | return sb.String() |
| 959 | } |
| 960 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 961 | func ninjaNameEscape(s string) string { |
| 962 | b := []byte(s) |
| 963 | escaped := false |
| 964 | for i, c := range b { |
| 965 | valid := (c >= 'a' && c <= 'z') || |
| 966 | (c >= 'A' && c <= 'Z') || |
| 967 | (c >= '0' && c <= '9') || |
| 968 | (c == '_') || |
| 969 | (c == '-') || |
| 970 | (c == '.') |
| 971 | if !valid { |
| 972 | b[i] = '_' |
| 973 | escaped = true |
| 974 | } |
| 975 | } |
| 976 | if escaped { |
| 977 | s = string(b) |
| 978 | } |
| 979 | return s |
| 980 | } |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 981 | |
| 982 | // hashSrcFiles returns a hash of the list of source files. It is used to ensure the command line |
| 983 | // or the sbox textproto manifest change even if the input files are not listed on the command line. |
| 984 | func hashSrcFiles(srcFiles Paths) string { |
| 985 | h := sha256.New() |
| 986 | srcFileList := strings.Join(srcFiles.Strings(), "\n") |
| 987 | h.Write([]byte(srcFileList)) |
| 988 | return fmt.Sprintf("%x", h.Sum(nil)) |
| 989 | } |