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 { |
| 175 | for _, input := range c.inputs { |
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 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 249 | func (r *RuleBuilder) depFileSet() map[string]WritablePath { |
| 250 | depFiles := make(map[string]WritablePath) |
| 251 | for _, c := range r.commands { |
| 252 | for _, depFile := range c.depFiles { |
| 253 | depFiles[depFile.String()] = depFile |
| 254 | } |
| 255 | } |
| 256 | return depFiles |
| 257 | } |
| 258 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 259 | // DepFiles returns the list of paths that were passed to the RuleBuilderCommand methods that take depfile paths, such |
| 260 | // as RuleBuilderCommand.DepFile or RuleBuilderCommand.FlagWithDepFile. |
| 261 | func (r *RuleBuilder) DepFiles() WritablePaths { |
| 262 | var depFiles WritablePaths |
| 263 | |
| 264 | for _, c := range r.commands { |
| 265 | for _, depFile := range c.depFiles { |
| 266 | depFiles = append(depFiles, depFile) |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | return depFiles |
| 271 | } |
| 272 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 273 | // Installs returns the list of tuples passed to Install. |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 274 | func (r *RuleBuilder) Installs() RuleBuilderInstalls { |
| 275 | return append(RuleBuilderInstalls(nil), r.installs...) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 278 | func (r *RuleBuilder) toolsSet() map[string]Path { |
| 279 | tools := make(map[string]Path) |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 280 | for _, c := range r.commands { |
| 281 | for _, tool := range c.tools { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 282 | tools[tool.String()] = tool |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | return tools |
| 287 | } |
| 288 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame^] | 289 | // Tools returns the list of paths that were passed to the RuleBuilderCommand.Tool method. The |
| 290 | // list is sorted and duplicates removed. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 291 | func (r *RuleBuilder) Tools() Paths { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 292 | toolsSet := r.toolsSet() |
| 293 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 294 | var toolsList Paths |
| 295 | for _, tool := range toolsSet { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 296 | toolsList = append(toolsList, tool) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 297 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 298 | |
| 299 | sort.Slice(toolsList, func(i, j int) bool { |
| 300 | return toolsList[i].String() < toolsList[j].String() |
| 301 | }) |
| 302 | |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 303 | return toolsList |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 306 | // RspFileInputs returns the list of paths that were passed to the RuleBuilderCommand.FlagWithRspFileInputList method. |
| 307 | func (r *RuleBuilder) RspFileInputs() Paths { |
| 308 | var rspFileInputs Paths |
| 309 | for _, c := range r.commands { |
| 310 | if c.rspFileInputs != nil { |
| 311 | if rspFileInputs != nil { |
| 312 | panic("Multiple commands in a rule may not have rsp file inputs") |
| 313 | } |
| 314 | rspFileInputs = c.rspFileInputs |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | return rspFileInputs |
| 319 | } |
| 320 | |
| 321 | // 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] | 322 | func (r *RuleBuilder) Commands() []string { |
| 323 | var commands []string |
| 324 | for _, c := range r.commands { |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 325 | commands = append(commands, c.String()) |
| 326 | } |
| 327 | return commands |
| 328 | } |
| 329 | |
| 330 | // NinjaEscapedCommands returns a slice containin the built command line after ninja escaping for each call to |
| 331 | // RuleBuilder.Command. |
| 332 | func (r *RuleBuilder) NinjaEscapedCommands() []string { |
| 333 | var commands []string |
| 334 | for _, c := range r.commands { |
| 335 | commands = append(commands, c.NinjaEscapedString()) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 336 | } |
| 337 | return commands |
| 338 | } |
| 339 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 340 | // BuilderContext is a subset of ModuleContext and SingletonContext. |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 341 | type BuilderContext interface { |
| 342 | PathContext |
| 343 | Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule |
| 344 | Build(PackageContext, BuildParams) |
| 345 | } |
| 346 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 347 | var _ BuilderContext = ModuleContext(nil) |
| 348 | var _ BuilderContext = SingletonContext(nil) |
| 349 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 350 | func (r *RuleBuilder) depFileMergerCmd(ctx PathContext, depFiles WritablePaths) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 351 | return r.Command(). |
Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 352 | BuiltTool(ctx, "dep_fixer"). |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 353 | Inputs(depFiles.Paths()) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 356 | // Build adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for |
| 357 | // Outputs. |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 358 | func (r *RuleBuilder) Build(pctx PackageContext, ctx BuilderContext, name string, desc string) { |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 359 | name = ninjaNameEscape(name) |
| 360 | |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 361 | if len(r.missingDeps) > 0 { |
| 362 | ctx.Build(pctx, BuildParams{ |
| 363 | Rule: ErrorRule, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 364 | Outputs: r.Outputs(), |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame^] | 365 | OrderOnly: r.OrderOnlys(), |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 366 | Description: desc, |
| 367 | Args: map[string]string{ |
| 368 | "error": "missing dependencies: " + strings.Join(r.missingDeps, ", "), |
| 369 | }, |
| 370 | }) |
| 371 | return |
| 372 | } |
| 373 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 374 | var depFile WritablePath |
| 375 | var depFormat blueprint.Deps |
| 376 | if depFiles := r.DepFiles(); len(depFiles) > 0 { |
| 377 | depFile = depFiles[0] |
| 378 | depFormat = blueprint.DepsGCC |
| 379 | if len(depFiles) > 1 { |
| 380 | // 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] | 381 | r.depFileMergerCmd(ctx, depFiles) |
| 382 | |
| 383 | if r.sbox { |
| 384 | // Check for Rel() errors, as all depfiles should be in the output dir |
| 385 | for _, path := range depFiles[1:] { |
| 386 | Rel(ctx, r.sboxOutDir.String(), path.String()) |
| 387 | } |
| 388 | } |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 392 | tools := r.Tools() |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 393 | commands := r.NinjaEscapedCommands() |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 394 | outputs := r.Outputs() |
| 395 | |
| 396 | if len(commands) == 0 { |
| 397 | return |
| 398 | } |
| 399 | if len(outputs) == 0 { |
| 400 | panic("No outputs specified from any Commands") |
| 401 | } |
| 402 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 403 | commandString := strings.Join(commands, " && ") |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 404 | |
| 405 | if r.sbox { |
| 406 | sboxOutputs := make([]string, len(outputs)) |
| 407 | for i, output := range outputs { |
| 408 | sboxOutputs[i] = "__SBOX_OUT_DIR__/" + Rel(ctx, r.sboxOutDir.String(), output.String()) |
| 409 | } |
| 410 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 411 | commandString = proptools.ShellEscape(commandString) |
| 412 | if !strings.HasPrefix(commandString, `'`) { |
| 413 | commandString = `'` + commandString + `'` |
| 414 | } |
| 415 | |
| 416 | sboxCmd := &RuleBuilderCommand{} |
Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 417 | sboxCmd.BuiltTool(ctx, "sbox"). |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 418 | Flag("-c").Text(commandString). |
| 419 | Flag("--sandbox-path").Text(shared.TempDirForOutDir(PathForOutput(ctx).String())). |
Dan Willemsen | c89b6f1 | 2019-08-29 14:47:40 -0700 | [diff] [blame] | 420 | Flag("--output-root").Text(r.sboxOutDir.String()) |
| 421 | |
| 422 | if depFile != nil { |
| 423 | sboxCmd.Flag("--depfile-out").Text(depFile.String()) |
| 424 | } |
| 425 | |
| 426 | sboxCmd.Flags(sboxOutputs) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 427 | |
Colin Cross | cfec40c | 2019-07-08 17:07:18 -0700 | [diff] [blame] | 428 | commandString = sboxCmd.buf.String() |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 429 | tools = append(tools, sboxCmd.tools...) |
| 430 | } |
| 431 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 432 | // 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] | 433 | // ImplicitOutputs. RuleBuilder only uses "$out" for the rsp file location, so the distinction between Outputs and |
| 434 | // ImplicitOutputs doesn't matter. |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 435 | output := outputs[0] |
| 436 | implicitOutputs := outputs[1:] |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 437 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 438 | var rspFile, rspFileContent string |
| 439 | rspFileInputs := r.RspFileInputs() |
| 440 | if rspFileInputs != nil { |
| 441 | rspFile = "$out.rsp" |
| 442 | rspFileContent = "$in" |
| 443 | } |
| 444 | |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 445 | var pool blueprint.Pool |
Ramy Medhat | 8ea054a | 2020-01-27 14:19:44 -0500 | [diff] [blame] | 446 | if ctx.Config().UseGoma() && r.remoteable.Goma { |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 447 | // 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] | 448 | } else if ctx.Config().UseRBE() && r.remoteable.RBE { |
| 449 | // When USE_RBE=true is set and the rule is supported by RBE, allow jobs to run outside the local pool. |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 450 | } else if r.highmem { |
| 451 | pool = highmemPool |
| 452 | } else if ctx.Config().UseRemoteBuild() { |
| 453 | pool = localPool |
| 454 | } |
| 455 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 456 | ctx.Build(pctx, BuildParams{ |
| 457 | Rule: ctx.Rule(pctx, name, blueprint.RuleParams{ |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 458 | Command: commandString, |
| 459 | CommandDeps: tools.Strings(), |
| 460 | Restat: r.restat, |
| 461 | Rspfile: rspFile, |
| 462 | RspfileContent: rspFileContent, |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 463 | Pool: pool, |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 464 | }), |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 465 | Inputs: rspFileInputs, |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 466 | Implicits: r.Inputs(), |
| 467 | Output: output, |
| 468 | ImplicitOutputs: implicitOutputs, |
| 469 | Depfile: depFile, |
| 470 | Deps: depFormat, |
| 471 | Description: desc, |
| 472 | }) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 475 | // RuleBuilderCommand is a builder for a command in a command line. It can be mutated by its methods to add to the |
| 476 | // command and track dependencies. The methods mutate the RuleBuilderCommand in place, as well as return the |
| 477 | // RuleBuilderCommand, so they can be used chained or unchained. All methods that add text implicitly add a single |
| 478 | // space as a separator from the previous method. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 479 | type RuleBuilderCommand struct { |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 480 | buf strings.Builder |
| 481 | inputs Paths |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame^] | 482 | orderOnlys Paths |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 483 | outputs WritablePaths |
| 484 | depFiles WritablePaths |
| 485 | tools Paths |
| 486 | rspFileInputs Paths |
| 487 | |
| 488 | // spans [start,end) of the command that should not be ninja escaped |
| 489 | unescapedSpans [][2]int |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 490 | |
| 491 | sbox bool |
| 492 | sboxOutDir WritablePath |
| 493 | } |
| 494 | |
| 495 | func (c *RuleBuilderCommand) addInput(path Path) string { |
| 496 | if c.sbox { |
| 497 | if rel, isRel, _ := maybeRelErr(c.sboxOutDir.String(), path.String()); isRel { |
| 498 | return "__SBOX_OUT_DIR__/" + rel |
| 499 | } |
| 500 | } |
| 501 | c.inputs = append(c.inputs, path) |
| 502 | return path.String() |
| 503 | } |
| 504 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame^] | 505 | func (c *RuleBuilderCommand) addOrderOnly(path Path) { |
| 506 | c.orderOnlys = append(c.orderOnlys, path) |
| 507 | } |
| 508 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 509 | func (c *RuleBuilderCommand) outputStr(path Path) string { |
| 510 | if c.sbox { |
| 511 | // Errors will be handled in RuleBuilder.Build where we have a context to report them |
| 512 | rel, _, _ := maybeRelErr(c.sboxOutDir.String(), path.String()) |
| 513 | return "__SBOX_OUT_DIR__/" + rel |
| 514 | } |
| 515 | return path.String() |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 516 | } |
| 517 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 518 | // Text adds the specified raw text to the command line. The text should not contain input or output paths or the |
| 519 | // rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 520 | func (c *RuleBuilderCommand) Text(text string) *RuleBuilderCommand { |
Colin Cross | cfec40c | 2019-07-08 17:07:18 -0700 | [diff] [blame] | 521 | if c.buf.Len() > 0 { |
| 522 | c.buf.WriteByte(' ') |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 523 | } |
Colin Cross | cfec40c | 2019-07-08 17:07:18 -0700 | [diff] [blame] | 524 | c.buf.WriteString(text) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 525 | return c |
| 526 | } |
| 527 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 528 | // Textf adds the specified formatted text to the command line. The text should not contain input or output paths or |
| 529 | // the rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 530 | func (c *RuleBuilderCommand) Textf(format string, a ...interface{}) *RuleBuilderCommand { |
| 531 | return c.Text(fmt.Sprintf(format, a...)) |
| 532 | } |
| 533 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 534 | // Flag adds the specified raw text to the command line. The text should not contain input or output paths or the |
| 535 | // rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 536 | func (c *RuleBuilderCommand) Flag(flag string) *RuleBuilderCommand { |
| 537 | return c.Text(flag) |
| 538 | } |
| 539 | |
Colin Cross | ab05443 | 2019-07-15 16:13:59 -0700 | [diff] [blame] | 540 | // OptionalFlag adds the specified raw text to the command line if it is not nil. The text should not contain input or |
| 541 | // output paths or the rule will not have them listed in its dependencies or outputs. |
| 542 | func (c *RuleBuilderCommand) OptionalFlag(flag *string) *RuleBuilderCommand { |
| 543 | if flag != nil { |
| 544 | c.Text(*flag) |
| 545 | } |
| 546 | |
| 547 | return c |
| 548 | } |
| 549 | |
Colin Cross | 92b7d58 | 2019-03-29 15:32:51 -0700 | [diff] [blame] | 550 | // Flags adds the specified raw text to the command line. The text should not contain input or output paths or the |
| 551 | // rule will not have them listed in its dependencies or outputs. |
| 552 | func (c *RuleBuilderCommand) Flags(flags []string) *RuleBuilderCommand { |
| 553 | for _, flag := range flags { |
| 554 | c.Text(flag) |
| 555 | } |
| 556 | return c |
| 557 | } |
| 558 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 559 | // FlagWithArg adds the specified flag and argument text to the command line, with no separator between them. The flag |
| 560 | // and argument should not contain input or output paths or the rule will not have them listed in its dependencies or |
| 561 | // outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 562 | func (c *RuleBuilderCommand) FlagWithArg(flag, arg string) *RuleBuilderCommand { |
| 563 | return c.Text(flag + arg) |
| 564 | } |
| 565 | |
Colin Cross | c7ed004 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 566 | // FlagForEachArg adds the specified flag joined with each argument to the command line. The result is identical to |
| 567 | // calling FlagWithArg for argument. |
| 568 | func (c *RuleBuilderCommand) FlagForEachArg(flag string, args []string) *RuleBuilderCommand { |
| 569 | for _, arg := range args { |
| 570 | c.FlagWithArg(flag, arg) |
| 571 | } |
| 572 | return c |
| 573 | } |
| 574 | |
Roland Levillain | 2da5d9a | 2019-02-27 16:56:41 +0000 | [diff] [blame] | 575 | // 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] | 576 | // and no separator between the flag and arguments. The flag and arguments should not contain input or output paths or |
| 577 | // the rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 578 | func (c *RuleBuilderCommand) FlagWithList(flag string, list []string, sep string) *RuleBuilderCommand { |
| 579 | return c.Text(flag + strings.Join(list, sep)) |
| 580 | } |
| 581 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 582 | // Tool adds the specified tool path to the command line. The path will be also added to the dependencies returned by |
| 583 | // RuleBuilder.Tools. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 584 | func (c *RuleBuilderCommand) Tool(path Path) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 585 | c.tools = append(c.tools, path) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 586 | return c.Text(path.String()) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 589 | // BuiltTool adds the specified tool path that was built using a host Soong module to the command line. The path will |
| 590 | // be also added to the dependencies returned by RuleBuilder.Tools. |
| 591 | // |
| 592 | // It is equivalent to: |
| 593 | // cmd.Tool(ctx.Config().HostToolPath(ctx, tool)) |
| 594 | func (c *RuleBuilderCommand) BuiltTool(ctx PathContext, tool string) *RuleBuilderCommand { |
| 595 | return c.Tool(ctx.Config().HostToolPath(ctx, tool)) |
| 596 | } |
| 597 | |
| 598 | // PrebuiltBuildTool adds the specified tool path from prebuils/build-tools. The path will be also added to the |
| 599 | // dependencies returned by RuleBuilder.Tools. |
| 600 | // |
| 601 | // It is equivalent to: |
| 602 | // cmd.Tool(ctx.Config().PrebuiltBuildTool(ctx, tool)) |
| 603 | func (c *RuleBuilderCommand) PrebuiltBuildTool(ctx PathContext, tool string) *RuleBuilderCommand { |
| 604 | return c.Tool(ctx.Config().PrebuiltBuildTool(ctx, tool)) |
| 605 | } |
| 606 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 607 | // Input adds the specified input path to the command line. The path will also be added to the dependencies returned by |
| 608 | // RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 609 | func (c *RuleBuilderCommand) Input(path Path) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 610 | return c.Text(c.addInput(path)) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 611 | } |
| 612 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 613 | // Inputs adds the specified input paths to the command line, separated by spaces. The paths will also be added to the |
| 614 | // dependencies returned by RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 615 | func (c *RuleBuilderCommand) Inputs(paths Paths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 616 | for _, path := range paths { |
| 617 | c.Input(path) |
| 618 | } |
| 619 | return c |
| 620 | } |
| 621 | |
| 622 | // Implicit adds the specified input path to the dependencies returned by RuleBuilder.Inputs without modifying the |
| 623 | // command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 624 | func (c *RuleBuilderCommand) Implicit(path Path) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 625 | c.addInput(path) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 626 | return c |
| 627 | } |
| 628 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 629 | // Implicits adds the specified input paths to the dependencies returned by RuleBuilder.Inputs without modifying the |
| 630 | // command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 631 | func (c *RuleBuilderCommand) Implicits(paths Paths) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 632 | for _, path := range paths { |
| 633 | c.addInput(path) |
| 634 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 635 | return c |
| 636 | } |
| 637 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame^] | 638 | // OrderOnly adds the specified input path to the dependencies returned by RuleBuilder.OrderOnlys |
| 639 | // without modifying the command line. |
| 640 | func (c *RuleBuilderCommand) OrderOnly(path Path) *RuleBuilderCommand { |
| 641 | c.addOrderOnly(path) |
| 642 | return c |
| 643 | } |
| 644 | |
| 645 | // OrderOnlys adds the specified input paths to the dependencies returned by RuleBuilder.OrderOnlys |
| 646 | // without modifying the command line. |
| 647 | func (c *RuleBuilderCommand) OrderOnlys(paths Paths) *RuleBuilderCommand { |
| 648 | for _, path := range paths { |
| 649 | c.addOrderOnly(path) |
| 650 | } |
| 651 | return c |
| 652 | } |
| 653 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 654 | // Output adds the specified output path to the command line. The path will also be added to the outputs returned by |
| 655 | // RuleBuilder.Outputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 656 | func (c *RuleBuilderCommand) Output(path WritablePath) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 657 | c.outputs = append(c.outputs, path) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 658 | return c.Text(c.outputStr(path)) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 659 | } |
| 660 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 661 | // Outputs adds the specified output paths to the command line, separated by spaces. The paths will also be added to |
| 662 | // the outputs returned by RuleBuilder.Outputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 663 | func (c *RuleBuilderCommand) Outputs(paths WritablePaths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 664 | for _, path := range paths { |
| 665 | c.Output(path) |
| 666 | } |
| 667 | return c |
| 668 | } |
| 669 | |
Dan Willemsen | 1945a4b | 2019-06-04 17:10:41 -0700 | [diff] [blame] | 670 | // OutputDir adds the output directory to the command line. This is only available when used with RuleBuilder.Sbox, |
| 671 | // and will be the temporary output directory managed by sbox, not the final one. |
| 672 | func (c *RuleBuilderCommand) OutputDir() *RuleBuilderCommand { |
| 673 | if !c.sbox { |
| 674 | panic("OutputDir only valid with Sbox") |
| 675 | } |
| 676 | return c.Text("__SBOX_OUT_DIR__") |
| 677 | } |
| 678 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 679 | // DepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles and adds it to the command |
| 680 | // line, and causes RuleBuilder.Build file to set the depfile flag for ninja. If multiple depfiles are added to |
| 681 | // commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the depfiles together. |
| 682 | func (c *RuleBuilderCommand) DepFile(path WritablePath) *RuleBuilderCommand { |
| 683 | c.depFiles = append(c.depFiles, path) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 684 | return c.Text(c.outputStr(path)) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 687 | // ImplicitOutput adds the specified output path to the dependencies returned by RuleBuilder.Outputs without modifying |
| 688 | // the command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 689 | func (c *RuleBuilderCommand) ImplicitOutput(path WritablePath) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 690 | c.outputs = append(c.outputs, path) |
| 691 | return c |
| 692 | } |
| 693 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 694 | // ImplicitOutputs adds the specified output paths to the dependencies returned by RuleBuilder.Outputs without modifying |
| 695 | // the command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 696 | func (c *RuleBuilderCommand) ImplicitOutputs(paths WritablePaths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 697 | c.outputs = append(c.outputs, paths...) |
| 698 | return c |
| 699 | } |
| 700 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 701 | // ImplicitDepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles without modifying |
| 702 | // the command line, and causes RuleBuilder.Build file to set the depfile flag for ninja. If multiple depfiles |
| 703 | // are added to commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the |
| 704 | // depfiles together. |
| 705 | func (c *RuleBuilderCommand) ImplicitDepFile(path WritablePath) *RuleBuilderCommand { |
| 706 | c.depFiles = append(c.depFiles, path) |
| 707 | return c |
| 708 | } |
| 709 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 710 | // FlagWithInput adds the specified flag and input path to the command line, with no separator between them. The path |
| 711 | // will also be added to the dependencies returned by RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 712 | func (c *RuleBuilderCommand) FlagWithInput(flag string, path Path) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 713 | return c.Text(flag + c.addInput(path)) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 714 | } |
| 715 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 716 | // FlagWithInputList adds the specified flag and input paths to the command line, with the inputs joined by sep |
| 717 | // and no separator between the flag and inputs. The input paths 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) FlagWithInputList(flag string, paths Paths, sep string) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 720 | strs := make([]string, len(paths)) |
| 721 | for i, path := range paths { |
| 722 | strs[i] = c.addInput(path) |
| 723 | } |
| 724 | return c.FlagWithList(flag, strs, sep) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 725 | } |
| 726 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 727 | // FlagForEachInput adds the specified flag joined with each input path to the command line. The input paths will also |
| 728 | // be added to the dependencies returned by RuleBuilder.Inputs. The result is identical to calling FlagWithInput for |
| 729 | // each input path. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 730 | func (c *RuleBuilderCommand) FlagForEachInput(flag string, paths Paths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 731 | for _, path := range paths { |
| 732 | c.FlagWithInput(flag, path) |
| 733 | } |
| 734 | return c |
| 735 | } |
| 736 | |
| 737 | // FlagWithOutput adds the specified flag and output path to the command line, with no separator between them. The path |
| 738 | // will also be added to the outputs returned by RuleBuilder.Outputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 739 | func (c *RuleBuilderCommand) FlagWithOutput(flag string, path WritablePath) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 740 | c.outputs = append(c.outputs, path) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 741 | return c.Text(flag + c.outputStr(path)) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 742 | } |
| 743 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 744 | // FlagWithDepFile adds the specified flag and depfile path to the command line, with no separator between them. The path |
| 745 | // will also be added to the outputs returned by RuleBuilder.Outputs. |
| 746 | func (c *RuleBuilderCommand) FlagWithDepFile(flag string, path WritablePath) *RuleBuilderCommand { |
| 747 | c.depFiles = append(c.depFiles, path) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 748 | return c.Text(flag + c.outputStr(path)) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 751 | // FlagWithRspFileInputList adds the specified flag and path to an rspfile to the command line, with no separator |
| 752 | // between them. The paths will be written to the rspfile. |
| 753 | func (c *RuleBuilderCommand) FlagWithRspFileInputList(flag string, paths Paths) *RuleBuilderCommand { |
| 754 | if c.rspFileInputs != nil { |
| 755 | panic("FlagWithRspFileInputList cannot be called if rsp file inputs have already been provided") |
| 756 | } |
| 757 | |
| 758 | // Use an empty slice if paths is nil, the non-nil slice is used as an indicator that the rsp file must be |
| 759 | // generated. |
| 760 | if paths == nil { |
| 761 | paths = Paths{} |
| 762 | } |
| 763 | |
| 764 | c.rspFileInputs = paths |
| 765 | |
| 766 | rspFile := "$out.rsp" |
| 767 | c.FlagWithArg(flag, rspFile) |
| 768 | c.unescapedSpans = append(c.unescapedSpans, [2]int{c.buf.Len() - len(rspFile), c.buf.Len()}) |
| 769 | return c |
| 770 | } |
| 771 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 772 | // String returns the command line. |
| 773 | func (c *RuleBuilderCommand) String() string { |
Colin Cross | cfec40c | 2019-07-08 17:07:18 -0700 | [diff] [blame] | 774 | return c.buf.String() |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 775 | } |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 776 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 777 | // String returns the command line. |
| 778 | func (c *RuleBuilderCommand) NinjaEscapedString() string { |
| 779 | return ninjaEscapeExceptForSpans(c.String(), c.unescapedSpans) |
| 780 | } |
| 781 | |
| 782 | func ninjaEscapeExceptForSpans(s string, spans [][2]int) string { |
| 783 | if len(spans) == 0 { |
| 784 | return proptools.NinjaEscape(s) |
| 785 | } |
| 786 | |
| 787 | sb := strings.Builder{} |
| 788 | sb.Grow(len(s) * 11 / 10) |
| 789 | |
| 790 | i := 0 |
| 791 | for _, span := range spans { |
| 792 | sb.WriteString(proptools.NinjaEscape(s[i:span[0]])) |
| 793 | sb.WriteString(s[span[0]:span[1]]) |
| 794 | i = span[1] |
| 795 | } |
| 796 | sb.WriteString(proptools.NinjaEscape(s[i:])) |
| 797 | |
| 798 | return sb.String() |
| 799 | } |
| 800 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 801 | func ninjaNameEscape(s string) string { |
| 802 | b := []byte(s) |
| 803 | escaped := false |
| 804 | for i, c := range b { |
| 805 | valid := (c >= 'a' && c <= 'z') || |
| 806 | (c >= 'A' && c <= 'Z') || |
| 807 | (c >= '0' && c <= '9') || |
| 808 | (c == '_') || |
| 809 | (c == '-') || |
| 810 | (c == '.') |
| 811 | if !valid { |
| 812 | b[i] = '_' |
| 813 | escaped = true |
| 814 | } |
| 815 | } |
| 816 | if escaped { |
| 817 | s = string(b) |
| 818 | } |
| 819 | return s |
| 820 | } |