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