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