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