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