| 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 | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 266 | // Commands returns a slice containing a the built command line for each call to RuleBuilder.Command. | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 267 | func (r *RuleBuilder) Commands() []string { | 
|  | 268 | var commands []string | 
|  | 269 | for _, c := range r.commands { | 
|  | 270 | commands = append(commands, string(c.buf)) | 
|  | 271 | } | 
|  | 272 | return commands | 
|  | 273 | } | 
|  | 274 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 275 | // BuilderContext is a subset of ModuleContext and SingletonContext. | 
| Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 276 | type BuilderContext interface { | 
|  | 277 | PathContext | 
|  | 278 | Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule | 
|  | 279 | Build(PackageContext, BuildParams) | 
|  | 280 | } | 
|  | 281 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 282 | var _ BuilderContext = ModuleContext(nil) | 
|  | 283 | var _ BuilderContext = SingletonContext(nil) | 
|  | 284 |  | 
| Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 285 | func (r *RuleBuilder) depFileMergerCmd(ctx PathContext, depFiles WritablePaths) *RuleBuilderCommand { | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 286 | return r.Command(). | 
| Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 287 | Tool(ctx.Config().HostToolPath(ctx, "dep_fixer")). | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 288 | Inputs(depFiles.Paths()) | 
| Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 289 | } | 
|  | 290 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 291 | // Build adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for | 
|  | 292 | // Outputs. | 
| Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 293 | func (r *RuleBuilder) Build(pctx PackageContext, ctx BuilderContext, name string, desc string) { | 
| Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 294 | name = ninjaNameEscape(name) | 
|  | 295 |  | 
| Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 296 | if len(r.missingDeps) > 0 { | 
|  | 297 | ctx.Build(pctx, BuildParams{ | 
|  | 298 | Rule:        ErrorRule, | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 299 | Outputs:     r.Outputs(), | 
| Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 300 | Description: desc, | 
|  | 301 | Args: map[string]string{ | 
|  | 302 | "error": "missing dependencies: " + strings.Join(r.missingDeps, ", "), | 
|  | 303 | }, | 
|  | 304 | }) | 
|  | 305 | return | 
|  | 306 | } | 
|  | 307 |  | 
| Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 308 | var depFile WritablePath | 
|  | 309 | var depFormat blueprint.Deps | 
|  | 310 | if depFiles := r.DepFiles(); len(depFiles) > 0 { | 
|  | 311 | depFile = depFiles[0] | 
|  | 312 | depFormat = blueprint.DepsGCC | 
|  | 313 | if len(depFiles) > 1 { | 
|  | 314 | // 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] | 315 | r.depFileMergerCmd(ctx, depFiles) | 
|  | 316 |  | 
|  | 317 | if r.sbox { | 
|  | 318 | // Check for Rel() errors, as all depfiles should be in the output dir | 
|  | 319 | for _, path := range depFiles[1:] { | 
|  | 320 | Rel(ctx, r.sboxOutDir.String(), path.String()) | 
|  | 321 | } | 
|  | 322 | } | 
| Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 323 | } | 
|  | 324 | } | 
|  | 325 |  | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 326 | tools := r.Tools() | 
|  | 327 | commands := r.Commands() | 
|  | 328 | outputs := r.Outputs() | 
|  | 329 |  | 
|  | 330 | if len(commands) == 0 { | 
|  | 331 | return | 
|  | 332 | } | 
|  | 333 | if len(outputs) == 0 { | 
|  | 334 | panic("No outputs specified from any Commands") | 
|  | 335 | } | 
|  | 336 |  | 
|  | 337 | commandString := strings.Join(proptools.NinjaEscapeList(commands), " && ") | 
|  | 338 |  | 
|  | 339 | if r.sbox { | 
|  | 340 | sboxOutputs := make([]string, len(outputs)) | 
|  | 341 | for i, output := range outputs { | 
|  | 342 | sboxOutputs[i] = "__SBOX_OUT_DIR__/" + Rel(ctx, r.sboxOutDir.String(), output.String()) | 
|  | 343 | } | 
|  | 344 |  | 
|  | 345 | if depFile != nil { | 
|  | 346 | sboxOutputs = append(sboxOutputs, "__SBOX_OUT_DIR__/"+Rel(ctx, r.sboxOutDir.String(), depFile.String())) | 
|  | 347 | } | 
|  | 348 |  | 
|  | 349 | commandString = proptools.ShellEscape(commandString) | 
|  | 350 | if !strings.HasPrefix(commandString, `'`) { | 
|  | 351 | commandString = `'` + commandString + `'` | 
|  | 352 | } | 
|  | 353 |  | 
|  | 354 | sboxCmd := &RuleBuilderCommand{} | 
|  | 355 | sboxCmd.Tool(ctx.Config().HostToolPath(ctx, "sbox")). | 
|  | 356 | Flag("-c").Text(commandString). | 
|  | 357 | Flag("--sandbox-path").Text(shared.TempDirForOutDir(PathForOutput(ctx).String())). | 
|  | 358 | Flag("--output-root").Text(r.sboxOutDir.String()). | 
|  | 359 | Flags(sboxOutputs) | 
|  | 360 |  | 
|  | 361 | commandString = string(sboxCmd.buf) | 
|  | 362 | tools = append(tools, sboxCmd.tools...) | 
|  | 363 | } | 
|  | 364 |  | 
| Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 365 | // Ninja doesn't like multiple outputs when depfiles are enabled, move all but the first output to | 
|  | 366 | // ImplicitOutputs.  RuleBuilder never uses "$out", so the distinction between Outputs and ImplicitOutputs | 
|  | 367 | // doesn't matter. | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 368 | output := outputs[0] | 
|  | 369 | implicitOutputs := outputs[1:] | 
| Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 370 |  | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 371 | ctx.Build(pctx, BuildParams{ | 
|  | 372 | Rule: ctx.Rule(pctx, name, blueprint.RuleParams{ | 
|  | 373 | Command:     commandString, | 
|  | 374 | CommandDeps: tools.Strings(), | 
|  | 375 | Restat:      r.restat, | 
|  | 376 | }), | 
|  | 377 | Implicits:       r.Inputs(), | 
|  | 378 | Output:          output, | 
|  | 379 | ImplicitOutputs: implicitOutputs, | 
|  | 380 | Depfile:         depFile, | 
|  | 381 | Deps:            depFormat, | 
|  | 382 | Description:     desc, | 
|  | 383 | }) | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 384 | } | 
|  | 385 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 386 | // RuleBuilderCommand is a builder for a command in a command line.  It can be mutated by its methods to add to the | 
|  | 387 | // command and track dependencies.  The methods mutate the RuleBuilderCommand in place, as well as return the | 
|  | 388 | // RuleBuilderCommand, so they can be used chained or unchained.  All methods that add text implicitly add a single | 
|  | 389 | // space as a separator from the previous method. | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 390 | type RuleBuilderCommand struct { | 
| Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 391 | buf      []byte | 
|  | 392 | inputs   Paths | 
|  | 393 | outputs  WritablePaths | 
|  | 394 | depFiles WritablePaths | 
|  | 395 | tools    Paths | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 396 |  | 
|  | 397 | sbox       bool | 
|  | 398 | sboxOutDir WritablePath | 
|  | 399 | } | 
|  | 400 |  | 
|  | 401 | func (c *RuleBuilderCommand) addInput(path Path) string { | 
|  | 402 | if c.sbox { | 
|  | 403 | if rel, isRel, _ := maybeRelErr(c.sboxOutDir.String(), path.String()); isRel { | 
|  | 404 | return "__SBOX_OUT_DIR__/" + rel | 
|  | 405 | } | 
|  | 406 | } | 
|  | 407 | c.inputs = append(c.inputs, path) | 
|  | 408 | return path.String() | 
|  | 409 | } | 
|  | 410 |  | 
|  | 411 | func (c *RuleBuilderCommand) outputStr(path Path) string { | 
|  | 412 | if c.sbox { | 
|  | 413 | // Errors will be handled in RuleBuilder.Build where we have a context to report them | 
|  | 414 | rel, _, _ := maybeRelErr(c.sboxOutDir.String(), path.String()) | 
|  | 415 | return "__SBOX_OUT_DIR__/" + rel | 
|  | 416 | } | 
|  | 417 | return path.String() | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 418 | } | 
|  | 419 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 420 | // Text adds the specified raw text to the command line.  The text should not contain input or output paths or the | 
|  | 421 | // rule will not have them listed in its dependencies or outputs. | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 422 | func (c *RuleBuilderCommand) Text(text string) *RuleBuilderCommand { | 
|  | 423 | if len(c.buf) > 0 { | 
|  | 424 | c.buf = append(c.buf, ' ') | 
|  | 425 | } | 
|  | 426 | c.buf = append(c.buf, text...) | 
|  | 427 | return c | 
|  | 428 | } | 
|  | 429 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 430 | // Textf adds the specified formatted text to the command line.  The text should not contain input or output paths or | 
|  | 431 | // the rule will not have them listed in its dependencies or outputs. | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 432 | func (c *RuleBuilderCommand) Textf(format string, a ...interface{}) *RuleBuilderCommand { | 
|  | 433 | return c.Text(fmt.Sprintf(format, a...)) | 
|  | 434 | } | 
|  | 435 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 436 | // Flag adds the specified raw text to the command line.  The text should not contain input or output paths or the | 
|  | 437 | // rule will not have them listed in its dependencies or outputs. | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 438 | func (c *RuleBuilderCommand) Flag(flag string) *RuleBuilderCommand { | 
|  | 439 | return c.Text(flag) | 
|  | 440 | } | 
|  | 441 |  | 
| Colin Cross | 92b7d58 | 2019-03-29 15:32:51 -0700 | [diff] [blame] | 442 | // Flags adds the specified raw text to the command line.  The text should not contain input or output paths or the | 
|  | 443 | // rule will not have them listed in its dependencies or outputs. | 
|  | 444 | func (c *RuleBuilderCommand) Flags(flags []string) *RuleBuilderCommand { | 
|  | 445 | for _, flag := range flags { | 
|  | 446 | c.Text(flag) | 
|  | 447 | } | 
|  | 448 | return c | 
|  | 449 | } | 
|  | 450 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 451 | // FlagWithArg adds the specified flag and argument text to the command line, with no separator between them.  The flag | 
|  | 452 | // and argument should not contain input or output paths or the rule will not have them listed in its dependencies or | 
|  | 453 | // outputs. | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 454 | func (c *RuleBuilderCommand) FlagWithArg(flag, arg string) *RuleBuilderCommand { | 
|  | 455 | return c.Text(flag + arg) | 
|  | 456 | } | 
|  | 457 |  | 
| Colin Cross | c7ed004 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 458 | // FlagForEachArg adds the specified flag joined with each argument to the command line.  The result is identical to | 
|  | 459 | // calling FlagWithArg for argument. | 
|  | 460 | func (c *RuleBuilderCommand) FlagForEachArg(flag string, args []string) *RuleBuilderCommand { | 
|  | 461 | for _, arg := range args { | 
|  | 462 | c.FlagWithArg(flag, arg) | 
|  | 463 | } | 
|  | 464 | return c | 
|  | 465 | } | 
|  | 466 |  | 
| Roland Levillain | 2da5d9a | 2019-02-27 16:56:41 +0000 | [diff] [blame] | 467 | // 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] | 468 | // and no separator between the flag and arguments.  The flag and arguments should not contain input or output paths or | 
|  | 469 | // the rule will not have them listed in its dependencies or outputs. | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 470 | func (c *RuleBuilderCommand) FlagWithList(flag string, list []string, sep string) *RuleBuilderCommand { | 
|  | 471 | return c.Text(flag + strings.Join(list, sep)) | 
|  | 472 | } | 
|  | 473 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 474 | // Tool adds the specified tool path to the command line.  The path will be also added to the dependencies returned by | 
|  | 475 | // RuleBuilder.Tools. | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 476 | func (c *RuleBuilderCommand) Tool(path Path) *RuleBuilderCommand { | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 477 | c.tools = append(c.tools, path) | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 478 | return c.Text(path.String()) | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 479 | } | 
|  | 480 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 481 | // Input adds the specified input path to the command line.  The path will also be added to the dependencies returned by | 
|  | 482 | // RuleBuilder.Inputs. | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 483 | func (c *RuleBuilderCommand) Input(path Path) *RuleBuilderCommand { | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 484 | return c.Text(c.addInput(path)) | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 485 | } | 
|  | 486 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 487 | // Inputs adds the specified input paths to the command line, separated by spaces.  The paths will also be added to the | 
|  | 488 | // dependencies returned by RuleBuilder.Inputs. | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 489 | func (c *RuleBuilderCommand) Inputs(paths Paths) *RuleBuilderCommand { | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 490 | for _, path := range paths { | 
|  | 491 | c.Input(path) | 
|  | 492 | } | 
|  | 493 | return c | 
|  | 494 | } | 
|  | 495 |  | 
|  | 496 | // Implicit adds the specified input path to the dependencies returned by RuleBuilder.Inputs without modifying the | 
|  | 497 | // command line. | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 498 | func (c *RuleBuilderCommand) Implicit(path Path) *RuleBuilderCommand { | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 499 | c.addInput(path) | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 500 | return c | 
|  | 501 | } | 
|  | 502 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 503 | // Implicits adds the specified input paths to the dependencies returned by RuleBuilder.Inputs without modifying the | 
|  | 504 | // command line. | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 505 | func (c *RuleBuilderCommand) Implicits(paths Paths) *RuleBuilderCommand { | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 506 | for _, path := range paths { | 
|  | 507 | c.addInput(path) | 
|  | 508 | } | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 509 | return c | 
|  | 510 | } | 
|  | 511 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 512 | // Output adds the specified output path to the command line.  The path will also be added to the outputs returned by | 
|  | 513 | // RuleBuilder.Outputs. | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 514 | func (c *RuleBuilderCommand) Output(path WritablePath) *RuleBuilderCommand { | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 515 | c.outputs = append(c.outputs, path) | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 516 | return c.Text(c.outputStr(path)) | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 517 | } | 
|  | 518 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 519 | // Outputs adds the specified output paths to the command line, separated by spaces.  The paths will also be added to | 
|  | 520 | // the outputs returned by RuleBuilder.Outputs. | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 521 | func (c *RuleBuilderCommand) Outputs(paths WritablePaths) *RuleBuilderCommand { | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 522 | for _, path := range paths { | 
|  | 523 | c.Output(path) | 
|  | 524 | } | 
|  | 525 | return c | 
|  | 526 | } | 
|  | 527 |  | 
| Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 528 | // DepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles and adds it to the command | 
|  | 529 | // line, and causes RuleBuilder.Build file to set the depfile flag for ninja.  If multiple depfiles are added to | 
|  | 530 | // commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the depfiles together. | 
|  | 531 | func (c *RuleBuilderCommand) DepFile(path WritablePath) *RuleBuilderCommand { | 
|  | 532 | c.depFiles = append(c.depFiles, path) | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 533 | return c.Text(c.outputStr(path)) | 
| Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 534 | } | 
|  | 535 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 536 | // ImplicitOutput adds the specified output path to the dependencies returned by RuleBuilder.Outputs without modifying | 
|  | 537 | // the command line. | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 538 | func (c *RuleBuilderCommand) ImplicitOutput(path WritablePath) *RuleBuilderCommand { | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 539 | c.outputs = append(c.outputs, path) | 
|  | 540 | return c | 
|  | 541 | } | 
|  | 542 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 543 | // ImplicitOutputs adds the specified output paths to the dependencies returned by RuleBuilder.Outputs without modifying | 
|  | 544 | // the command line. | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 545 | func (c *RuleBuilderCommand) ImplicitOutputs(paths WritablePaths) *RuleBuilderCommand { | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 546 | c.outputs = append(c.outputs, paths...) | 
|  | 547 | return c | 
|  | 548 | } | 
|  | 549 |  | 
| Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 550 | // ImplicitDepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles without modifying | 
|  | 551 | // the command line, and causes RuleBuilder.Build file to set the depfile flag for ninja.  If multiple depfiles | 
|  | 552 | // are added to commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the | 
|  | 553 | // depfiles together. | 
|  | 554 | func (c *RuleBuilderCommand) ImplicitDepFile(path WritablePath) *RuleBuilderCommand { | 
|  | 555 | c.depFiles = append(c.depFiles, path) | 
|  | 556 | return c | 
|  | 557 | } | 
|  | 558 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 559 | // FlagWithInput adds the specified flag and input path to the command line, with no separator between them.  The path | 
|  | 560 | // will also be added to the dependencies returned by RuleBuilder.Inputs. | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 561 | func (c *RuleBuilderCommand) FlagWithInput(flag string, path Path) *RuleBuilderCommand { | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 562 | return c.Text(flag + c.addInput(path)) | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 563 | } | 
|  | 564 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 565 | // FlagWithInputList adds the specified flag and input paths to the command line, with the inputs joined by sep | 
|  | 566 | // and no separator between the flag and inputs.  The input paths will also be added to the dependencies returned by | 
|  | 567 | // RuleBuilder.Inputs. | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 568 | func (c *RuleBuilderCommand) FlagWithInputList(flag string, paths Paths, sep string) *RuleBuilderCommand { | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 569 | strs := make([]string, len(paths)) | 
|  | 570 | for i, path := range paths { | 
|  | 571 | strs[i] = c.addInput(path) | 
|  | 572 | } | 
|  | 573 | return c.FlagWithList(flag, strs, sep) | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 574 | } | 
|  | 575 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 576 | // FlagForEachInput adds the specified flag joined with each input path to the command line.  The input paths will also | 
|  | 577 | // be added to the dependencies returned by RuleBuilder.Inputs.  The result is identical to calling FlagWithInput for | 
|  | 578 | // each input path. | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 579 | func (c *RuleBuilderCommand) FlagForEachInput(flag string, paths Paths) *RuleBuilderCommand { | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 580 | for _, path := range paths { | 
|  | 581 | c.FlagWithInput(flag, path) | 
|  | 582 | } | 
|  | 583 | return c | 
|  | 584 | } | 
|  | 585 |  | 
|  | 586 | // FlagWithOutput adds the specified flag and output path to the command line, with no separator between them.  The path | 
|  | 587 | // will also be added to the outputs returned by RuleBuilder.Outputs. | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 588 | func (c *RuleBuilderCommand) FlagWithOutput(flag string, path WritablePath) *RuleBuilderCommand { | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 589 | c.outputs = append(c.outputs, path) | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 590 | return c.Text(flag + c.outputStr(path)) | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 591 | } | 
|  | 592 |  | 
| Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 593 | // FlagWithDepFile adds the specified flag and depfile path to the command line, with no separator between them.  The path | 
|  | 594 | // will also be added to the outputs returned by RuleBuilder.Outputs. | 
|  | 595 | func (c *RuleBuilderCommand) FlagWithDepFile(flag string, path WritablePath) *RuleBuilderCommand { | 
|  | 596 | c.depFiles = append(c.depFiles, path) | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 597 | return c.Text(flag + c.outputStr(path)) | 
| Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 598 | } | 
|  | 599 |  | 
| Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 600 | // String returns the command line. | 
|  | 601 | func (c *RuleBuilderCommand) String() string { | 
|  | 602 | return string(c.buf) | 
|  | 603 | } | 
| Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 604 |  | 
|  | 605 | func ninjaNameEscape(s string) string { | 
|  | 606 | b := []byte(s) | 
|  | 607 | escaped := false | 
|  | 608 | for i, c := range b { | 
|  | 609 | valid := (c >= 'a' && c <= 'z') || | 
|  | 610 | (c >= 'A' && c <= 'Z') || | 
|  | 611 | (c >= '0' && c <= '9') || | 
|  | 612 | (c == '_') || | 
|  | 613 | (c == '-') || | 
|  | 614 | (c == '.') | 
|  | 615 | if !valid { | 
|  | 616 | b[i] = '_' | 
|  | 617 | escaped = true | 
|  | 618 | } | 
|  | 619 | } | 
|  | 620 | if escaped { | 
|  | 621 | s = string(b) | 
|  | 622 | } | 
|  | 623 | return s | 
|  | 624 | } |