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" |
| 19 | "path/filepath" |
| 20 | "sort" |
| 21 | "strings" |
| 22 | |
| 23 | "github.com/google/blueprint" |
| 24 | "github.com/google/blueprint/proptools" |
| 25 | ) |
| 26 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 27 | // RuleBuilder provides an alternative to ModuleContext.Rule and ModuleContext.Build to add a command line to the build |
| 28 | // graph. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 29 | type RuleBuilder struct { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 30 | commands []*RuleBuilderCommand |
| 31 | installs []RuleBuilderInstall |
| 32 | temporariesSet map[string]bool |
| 33 | restat bool |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 34 | missingDeps []string |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 37 | // NewRuleBuilder returns a newly created RuleBuilder. |
| 38 | func NewRuleBuilder() *RuleBuilder { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 39 | return &RuleBuilder{ |
| 40 | temporariesSet: make(map[string]bool), |
| 41 | } |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // RuleBuilderInstall is a tuple of install from and to locations. |
| 45 | type RuleBuilderInstall struct { |
| 46 | From, To string |
| 47 | } |
| 48 | |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 49 | // MissingDeps adds modules to the list of missing dependencies. If MissingDeps |
| 50 | // is called with a non-empty input, any call to Build will result in a rule |
| 51 | // that will print an error listing the missing dependencies and fail. |
| 52 | // MissingDeps should only be called if Config.AllowMissingDependencies() is |
| 53 | // true. |
| 54 | func (r *RuleBuilder) MissingDeps(missingDeps []string) { |
| 55 | r.missingDeps = append(r.missingDeps, missingDeps...) |
| 56 | } |
| 57 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 58 | // Restat marks the rule as a restat rule, which will be passed to ModuleContext.Rule in BuildParams.Restat. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 59 | func (r *RuleBuilder) Restat() *RuleBuilder { |
| 60 | r.restat = true |
| 61 | return r |
| 62 | } |
| 63 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 64 | // Install associates an output of the rule with an install location, which can be retrieved later using |
| 65 | // RuleBuilder.Installs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 66 | func (r *RuleBuilder) Install(from, to string) { |
| 67 | r.installs = append(r.installs, RuleBuilderInstall{from, to}) |
| 68 | } |
| 69 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 70 | // Command returns a new RuleBuilderCommand for the rule. The commands will be ordered in the rule by when they were |
| 71 | // created by this method. That can be mutated through their methods in any order, as long as the mutations do not |
| 72 | // race with any call to Build. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 73 | func (r *RuleBuilder) Command() *RuleBuilderCommand { |
| 74 | command := &RuleBuilderCommand{} |
| 75 | r.commands = append(r.commands, command) |
| 76 | return command |
| 77 | } |
| 78 | |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 79 | // Temporary marks an output of a command as an intermediate file that will be used as an input to another command |
| 80 | // in the same rule, and should not be listed in Outputs. |
| 81 | func (r *RuleBuilder) Temporary(path string) { |
| 82 | r.temporariesSet[path] = true |
| 83 | } |
| 84 | |
| 85 | // DeleteTemporaryFiles adds a command to the rule that deletes any outputs that have been marked using Temporary |
| 86 | // when the rule runs. DeleteTemporaryFiles should be called after all calls to Temporary. |
| 87 | func (r *RuleBuilder) DeleteTemporaryFiles() { |
| 88 | var temporariesList []string |
| 89 | |
| 90 | for intermediate := range r.temporariesSet { |
| 91 | temporariesList = append(temporariesList, intermediate) |
| 92 | } |
| 93 | sort.Strings(temporariesList) |
| 94 | |
| 95 | r.Command().Text("rm").Flag("-f").Outputs(temporariesList) |
| 96 | } |
| 97 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 98 | // Inputs returns the list of paths that were passed to the RuleBuilderCommand methods that take input paths, such |
| 99 | // as RuleBuilderCommand.Input, RuleBuilderComand.Implicit, or RuleBuilderCommand.FlagWithInput. Inputs to a command |
| 100 | // that are also outputs of another command in the same RuleBuilder are filtered out. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 101 | func (r *RuleBuilder) Inputs() []string { |
| 102 | outputs := r.outputSet() |
| 103 | |
| 104 | inputs := make(map[string]bool) |
| 105 | for _, c := range r.commands { |
| 106 | for _, input := range c.inputs { |
| 107 | if !outputs[input] { |
| 108 | inputs[input] = true |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | var inputList []string |
| 114 | for input := range inputs { |
| 115 | inputList = append(inputList, input) |
| 116 | } |
| 117 | sort.Strings(inputList) |
| 118 | |
| 119 | return inputList |
| 120 | } |
| 121 | |
| 122 | func (r *RuleBuilder) outputSet() map[string]bool { |
| 123 | outputs := make(map[string]bool) |
| 124 | for _, c := range r.commands { |
| 125 | for _, output := range c.outputs { |
| 126 | outputs[output] = true |
| 127 | } |
| 128 | } |
| 129 | return outputs |
| 130 | } |
| 131 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 132 | // Outputs returns the list of paths that were passed to the RuleBuilderCommand methods that take output paths, such |
| 133 | // as RuleBuilderCommand.Output, RuleBuilderCommand.ImplicitOutput, or RuleBuilderCommand.FlagWithInput. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 134 | func (r *RuleBuilder) Outputs() []string { |
| 135 | outputs := r.outputSet() |
| 136 | |
| 137 | var outputList []string |
| 138 | for output := range outputs { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 139 | if !r.temporariesSet[output] { |
| 140 | outputList = append(outputList, output) |
| 141 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 142 | } |
| 143 | sort.Strings(outputList) |
| 144 | return outputList |
| 145 | } |
| 146 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 147 | // Installs returns the list of tuples passed to Install. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 148 | func (r *RuleBuilder) Installs() []RuleBuilderInstall { |
| 149 | return append([]RuleBuilderInstall(nil), r.installs...) |
| 150 | } |
| 151 | |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 152 | func (r *RuleBuilder) toolsSet() map[string]bool { |
| 153 | tools := make(map[string]bool) |
| 154 | for _, c := range r.commands { |
| 155 | for _, tool := range c.tools { |
| 156 | tools[tool] = true |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return tools |
| 161 | } |
| 162 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 163 | // Tools returns the list of paths that were passed to the RuleBuilderCommand.Tool method. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 164 | func (r *RuleBuilder) Tools() []string { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 165 | toolsSet := r.toolsSet() |
| 166 | |
| 167 | var toolsList []string |
| 168 | for tool := range toolsSet { |
| 169 | toolsList = append(toolsList, tool) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 170 | } |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 171 | sort.Strings(toolsList) |
| 172 | return toolsList |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 175 | // 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] | 176 | func (r *RuleBuilder) Commands() []string { |
| 177 | var commands []string |
| 178 | for _, c := range r.commands { |
| 179 | commands = append(commands, string(c.buf)) |
| 180 | } |
| 181 | return commands |
| 182 | } |
| 183 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 184 | // BuilderContext is a subset of ModuleContext and SingletonContext. |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 185 | type BuilderContext interface { |
| 186 | PathContext |
| 187 | Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule |
| 188 | Build(PackageContext, BuildParams) |
| 189 | } |
| 190 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 191 | var _ BuilderContext = ModuleContext(nil) |
| 192 | var _ BuilderContext = SingletonContext(nil) |
| 193 | |
| 194 | // Build adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for |
| 195 | // Outputs. |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 196 | func (r *RuleBuilder) Build(pctx PackageContext, ctx BuilderContext, name string, desc string) { |
| 197 | // TODO: convert RuleBuilder arguments and storage to Paths |
| 198 | mctx, _ := ctx.(ModuleContext) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 199 | var inputs Paths |
| 200 | for _, input := range r.Inputs() { |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 201 | // Module output paths |
| 202 | if mctx != nil { |
| 203 | rel, isRel := MaybeRel(ctx, PathForModuleOut(mctx).String(), input) |
| 204 | if isRel { |
| 205 | inputs = append(inputs, PathForModuleOut(mctx, rel)) |
| 206 | continue |
| 207 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 208 | } |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 209 | |
| 210 | // Other output paths |
| 211 | rel, isRel := MaybeRel(ctx, PathForOutput(ctx).String(), input) |
| 212 | if isRel { |
| 213 | inputs = append(inputs, PathForOutput(ctx, rel)) |
| 214 | continue |
| 215 | } |
| 216 | |
| 217 | // TODO: remove this once boot image is moved to where PathForOutput can find it. |
| 218 | inputs = append(inputs, &unknownRulePath{input}) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | var outputs WritablePaths |
| 222 | for _, output := range r.Outputs() { |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 223 | if mctx != nil { |
| 224 | rel := Rel(ctx, PathForModuleOut(mctx).String(), output) |
| 225 | outputs = append(outputs, PathForModuleOut(mctx, rel)) |
| 226 | } else { |
| 227 | rel := Rel(ctx, PathForOutput(ctx).String(), output) |
| 228 | outputs = append(outputs, PathForOutput(ctx, rel)) |
| 229 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 232 | if len(r.missingDeps) > 0 { |
| 233 | ctx.Build(pctx, BuildParams{ |
| 234 | Rule: ErrorRule, |
| 235 | Outputs: outputs, |
| 236 | Description: desc, |
| 237 | Args: map[string]string{ |
| 238 | "error": "missing dependencies: " + strings.Join(r.missingDeps, ", "), |
| 239 | }, |
| 240 | }) |
| 241 | return |
| 242 | } |
| 243 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 244 | if len(r.Commands()) > 0 { |
| 245 | ctx.Build(pctx, BuildParams{ |
| 246 | Rule: ctx.Rule(pctx, name, blueprint.RuleParams{ |
| 247 | Command: strings.Join(proptools.NinjaEscape(r.Commands()), " && "), |
| 248 | CommandDeps: r.Tools(), |
| 249 | }), |
| 250 | Implicits: inputs, |
| 251 | Outputs: outputs, |
| 252 | Description: desc, |
| 253 | }) |
| 254 | } |
| 255 | } |
| 256 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 257 | // RuleBuilderCommand is a builder for a command in a command line. It can be mutated by its methods to add to the |
| 258 | // command and track dependencies. The methods mutate the RuleBuilderCommand in place, as well as return the |
| 259 | // RuleBuilderCommand, so they can be used chained or unchained. All methods that add text implicitly add a single |
| 260 | // space as a separator from the previous method. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 261 | type RuleBuilderCommand struct { |
| 262 | buf []byte |
| 263 | inputs []string |
| 264 | outputs []string |
| 265 | tools []string |
| 266 | } |
| 267 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 268 | // Text adds the specified raw text to the command line. The text should not contain input or output paths or the |
| 269 | // rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 270 | func (c *RuleBuilderCommand) Text(text string) *RuleBuilderCommand { |
| 271 | if len(c.buf) > 0 { |
| 272 | c.buf = append(c.buf, ' ') |
| 273 | } |
| 274 | c.buf = append(c.buf, text...) |
| 275 | return c |
| 276 | } |
| 277 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 278 | // Textf adds the specified formatted text to the command line. The text should not contain input or output paths or |
| 279 | // the rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 280 | func (c *RuleBuilderCommand) Textf(format string, a ...interface{}) *RuleBuilderCommand { |
| 281 | return c.Text(fmt.Sprintf(format, a...)) |
| 282 | } |
| 283 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 284 | // Flag adds the specified raw text to the command line. The text should not contain input or output paths or the |
| 285 | // rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 286 | func (c *RuleBuilderCommand) Flag(flag string) *RuleBuilderCommand { |
| 287 | return c.Text(flag) |
| 288 | } |
| 289 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 290 | // FlagWithArg adds the specified flag and argument text to the command line, with no separator between them. The flag |
| 291 | // and argument should not contain input or output paths or the rule will not have them listed in its dependencies or |
| 292 | // outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 293 | func (c *RuleBuilderCommand) FlagWithArg(flag, arg string) *RuleBuilderCommand { |
| 294 | return c.Text(flag + arg) |
| 295 | } |
| 296 | |
Colin Cross | c7ed004 | 2019-02-11 14:11:09 -0800 | [diff] [blame^] | 297 | // FlagForEachArg adds the specified flag joined with each argument to the command line. The result is identical to |
| 298 | // calling FlagWithArg for argument. |
| 299 | func (c *RuleBuilderCommand) FlagForEachArg(flag string, args []string) *RuleBuilderCommand { |
| 300 | for _, arg := range args { |
| 301 | c.FlagWithArg(flag, arg) |
| 302 | } |
| 303 | return c |
| 304 | } |
| 305 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 306 | // FlagWithArg adds the specified flag and list of arguments to the command line, with the arguments joined by sep |
| 307 | // and no separator between the flag and arguments. The flag and arguments should not contain input or output paths or |
| 308 | // the rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 309 | func (c *RuleBuilderCommand) FlagWithList(flag string, list []string, sep string) *RuleBuilderCommand { |
| 310 | return c.Text(flag + strings.Join(list, sep)) |
| 311 | } |
| 312 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 313 | // Tool adds the specified tool path to the command line. The path will be also added to the dependencies returned by |
| 314 | // RuleBuilder.Tools. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 315 | func (c *RuleBuilderCommand) Tool(path string) *RuleBuilderCommand { |
| 316 | c.tools = append(c.tools, path) |
| 317 | return c.Text(path) |
| 318 | } |
| 319 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 320 | // Input adds the specified input path to the command line. The path will also be added to the dependencies returned by |
| 321 | // RuleBuilder.Inputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 322 | func (c *RuleBuilderCommand) Input(path string) *RuleBuilderCommand { |
| 323 | c.inputs = append(c.inputs, path) |
| 324 | return c.Text(path) |
| 325 | } |
| 326 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 327 | // Inputs adds the specified input paths to the command line, separated by spaces. The paths will also be added to the |
| 328 | // dependencies returned by RuleBuilder.Inputs. |
| 329 | func (c *RuleBuilderCommand) Inputs(paths []string) *RuleBuilderCommand { |
| 330 | for _, path := range paths { |
| 331 | c.Input(path) |
| 332 | } |
| 333 | return c |
| 334 | } |
| 335 | |
| 336 | // Implicit adds the specified input path to the dependencies returned by RuleBuilder.Inputs without modifying the |
| 337 | // command line. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 338 | func (c *RuleBuilderCommand) Implicit(path string) *RuleBuilderCommand { |
| 339 | c.inputs = append(c.inputs, path) |
| 340 | return c |
| 341 | } |
| 342 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 343 | // Implicits adds the specified input paths to the dependencies returned by RuleBuilder.Inputs without modifying the |
| 344 | // command line. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 345 | func (c *RuleBuilderCommand) Implicits(paths []string) *RuleBuilderCommand { |
| 346 | c.inputs = append(c.inputs, paths...) |
| 347 | return c |
| 348 | } |
| 349 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 350 | // Output adds the specified output path to the command line. The path will also be added to the outputs returned by |
| 351 | // RuleBuilder.Outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 352 | func (c *RuleBuilderCommand) Output(path string) *RuleBuilderCommand { |
| 353 | c.outputs = append(c.outputs, path) |
| 354 | return c.Text(path) |
| 355 | } |
| 356 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 357 | // Outputs adds the specified output paths to the command line, separated by spaces. The paths will also be added to |
| 358 | // the outputs returned by RuleBuilder.Outputs. |
| 359 | func (c *RuleBuilderCommand) Outputs(paths []string) *RuleBuilderCommand { |
| 360 | for _, path := range paths { |
| 361 | c.Output(path) |
| 362 | } |
| 363 | return c |
| 364 | } |
| 365 | |
| 366 | // ImplicitOutput adds the specified output path to the dependencies returned by RuleBuilder.Outputs without modifying |
| 367 | // the command line. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 368 | func (c *RuleBuilderCommand) ImplicitOutput(path string) *RuleBuilderCommand { |
| 369 | c.outputs = append(c.outputs, path) |
| 370 | return c |
| 371 | } |
| 372 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 373 | // ImplicitOutputs adds the specified output paths to the dependencies returned by RuleBuilder.Outputs without modifying |
| 374 | // the command line. |
| 375 | func (c *RuleBuilderCommand) ImplicitOutputs(paths []string) *RuleBuilderCommand { |
| 376 | c.outputs = append(c.outputs, paths...) |
| 377 | return c |
| 378 | } |
| 379 | |
| 380 | // FlagWithInput adds the specified flag and input path to the command line, with no separator between them. The path |
| 381 | // will also be added to the dependencies returned by RuleBuilder.Inputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 382 | func (c *RuleBuilderCommand) FlagWithInput(flag, path string) *RuleBuilderCommand { |
| 383 | c.inputs = append(c.inputs, path) |
| 384 | return c.Text(flag + path) |
| 385 | } |
| 386 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 387 | // FlagWithInputList adds the specified flag and input paths to the command line, with the inputs joined by sep |
| 388 | // and no separator between the flag and inputs. The input paths will also be added to the dependencies returned by |
| 389 | // RuleBuilder.Inputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 390 | func (c *RuleBuilderCommand) FlagWithInputList(flag string, paths []string, sep string) *RuleBuilderCommand { |
| 391 | c.inputs = append(c.inputs, paths...) |
| 392 | return c.FlagWithList(flag, paths, sep) |
| 393 | } |
| 394 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 395 | // FlagForEachInput adds the specified flag joined with each input path to the command line. The input paths will also |
| 396 | // be added to the dependencies returned by RuleBuilder.Inputs. The result is identical to calling FlagWithInput for |
| 397 | // each input path. |
| 398 | func (c *RuleBuilderCommand) FlagForEachInput(flag string, paths []string) *RuleBuilderCommand { |
| 399 | for _, path := range paths { |
| 400 | c.FlagWithInput(flag, path) |
| 401 | } |
| 402 | return c |
| 403 | } |
| 404 | |
| 405 | // FlagWithOutput adds the specified flag and output path to the command line, with no separator between them. The path |
| 406 | // will also be added to the outputs returned by RuleBuilder.Outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 407 | func (c *RuleBuilderCommand) FlagWithOutput(flag, path string) *RuleBuilderCommand { |
| 408 | c.outputs = append(c.outputs, path) |
| 409 | return c.Text(flag + path) |
| 410 | } |
| 411 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 412 | // String returns the command line. |
| 413 | func (c *RuleBuilderCommand) String() string { |
| 414 | return string(c.buf) |
| 415 | } |
| 416 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 417 | type unknownRulePath struct { |
| 418 | path string |
| 419 | } |
| 420 | |
| 421 | var _ Path = (*unknownRulePath)(nil) |
| 422 | |
| 423 | func (p *unknownRulePath) String() string { return p.path } |
| 424 | func (p *unknownRulePath) Ext() string { return filepath.Ext(p.path) } |
| 425 | func (p *unknownRulePath) Base() string { return filepath.Base(p.path) } |
| 426 | func (p *unknownRulePath) Rel() string { return p.path } |