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" |
| 24 | ) |
| 25 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 26 | // RuleBuilder provides an alternative to ModuleContext.Rule and ModuleContext.Build to add a command line to the build |
| 27 | // graph. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 28 | type RuleBuilder struct { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 29 | commands []*RuleBuilderCommand |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 30 | installs RuleBuilderInstalls |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 31 | temporariesSet map[WritablePath]bool |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 32 | restat bool |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 33 | missingDeps []string |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 36 | // NewRuleBuilder returns a newly created RuleBuilder. |
| 37 | func NewRuleBuilder() *RuleBuilder { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 38 | return &RuleBuilder{ |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 39 | temporariesSet: make(map[WritablePath]bool), |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 40 | } |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // RuleBuilderInstall is a tuple of install from and to locations. |
| 44 | type RuleBuilderInstall struct { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 45 | From Path |
| 46 | To string |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 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 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 59 | sb.WriteString(install.From.String()) |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 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 | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 83 | func (r *RuleBuilder) Install(from Path, to string) { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 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. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 98 | func (r *RuleBuilder) Temporary(path WritablePath) { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 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() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 105 | var temporariesList WritablePaths |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 106 | |
| 107 | for intermediate := range r.temporariesSet { |
| 108 | temporariesList = append(temporariesList, intermediate) |
| 109 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 110 | |
| 111 | sort.Slice(temporariesList, func(i, j int) bool { |
| 112 | return temporariesList[i].String() < temporariesList[j].String() |
| 113 | }) |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 114 | |
| 115 | r.Command().Text("rm").Flag("-f").Outputs(temporariesList) |
| 116 | } |
| 117 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 118 | // Inputs returns the list of paths that were passed to the RuleBuilderCommand methods that take input paths, such |
| 119 | // as RuleBuilderCommand.Input, RuleBuilderComand.Implicit, or RuleBuilderCommand.FlagWithInput. Inputs to a command |
| 120 | // 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] | 121 | func (r *RuleBuilder) Inputs() Paths { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 122 | outputs := r.outputSet() |
| 123 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 124 | inputs := make(map[string]Path) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 125 | for _, c := range r.commands { |
| 126 | for _, input := range c.inputs { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 127 | if _, isOutput := outputs[input.String()]; !isOutput { |
| 128 | inputs[input.String()] = input |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 133 | var inputList Paths |
| 134 | for _, input := range inputs { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 135 | inputList = append(inputList, input) |
| 136 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 137 | |
| 138 | sort.Slice(inputList, func(i, j int) bool { |
| 139 | return inputList[i].String() < inputList[j].String() |
| 140 | }) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 141 | |
| 142 | return inputList |
| 143 | } |
| 144 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 145 | func (r *RuleBuilder) outputSet() map[string]WritablePath { |
| 146 | outputs := make(map[string]WritablePath) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 147 | for _, c := range r.commands { |
| 148 | for _, output := range c.outputs { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 149 | outputs[output.String()] = output |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | return outputs |
| 153 | } |
| 154 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 155 | // Outputs returns the list of paths that were passed to the RuleBuilderCommand methods that take output paths, such |
| 156 | // as RuleBuilderCommand.Output, RuleBuilderCommand.ImplicitOutput, or RuleBuilderCommand.FlagWithInput. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 157 | func (r *RuleBuilder) Outputs() WritablePaths { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 158 | outputs := r.outputSet() |
| 159 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 160 | var outputList WritablePaths |
| 161 | for _, output := range outputs { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 162 | if !r.temporariesSet[output] { |
| 163 | outputList = append(outputList, output) |
| 164 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 165 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 166 | |
| 167 | sort.Slice(outputList, func(i, j int) bool { |
| 168 | return outputList[i].String() < outputList[j].String() |
| 169 | }) |
| 170 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 171 | return outputList |
| 172 | } |
| 173 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 174 | // Installs returns the list of tuples passed to Install. |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 175 | func (r *RuleBuilder) Installs() RuleBuilderInstalls { |
| 176 | return append(RuleBuilderInstalls(nil), r.installs...) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 179 | func (r *RuleBuilder) toolsSet() map[string]Path { |
| 180 | tools := make(map[string]Path) |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 181 | for _, c := range r.commands { |
| 182 | for _, tool := range c.tools { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 183 | tools[tool.String()] = tool |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | return tools |
| 188 | } |
| 189 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 190 | // 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] | 191 | func (r *RuleBuilder) Tools() Paths { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 192 | toolsSet := r.toolsSet() |
| 193 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 194 | var toolsList Paths |
| 195 | for _, tool := range toolsSet { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 196 | toolsList = append(toolsList, tool) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 197 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 198 | |
| 199 | sort.Slice(toolsList, func(i, j int) bool { |
| 200 | return toolsList[i].String() < toolsList[j].String() |
| 201 | }) |
| 202 | |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 203 | return toolsList |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 206 | // 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] | 207 | func (r *RuleBuilder) Commands() []string { |
| 208 | var commands []string |
| 209 | for _, c := range r.commands { |
| 210 | commands = append(commands, string(c.buf)) |
| 211 | } |
| 212 | return commands |
| 213 | } |
| 214 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 215 | // BuilderContext is a subset of ModuleContext and SingletonContext. |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 216 | type BuilderContext interface { |
| 217 | PathContext |
| 218 | Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule |
| 219 | Build(PackageContext, BuildParams) |
| 220 | } |
| 221 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 222 | var _ BuilderContext = ModuleContext(nil) |
| 223 | var _ BuilderContext = SingletonContext(nil) |
| 224 | |
| 225 | // Build adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for |
| 226 | // Outputs. |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 227 | func (r *RuleBuilder) Build(pctx PackageContext, ctx BuilderContext, name string, desc string) { |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 228 | if len(r.missingDeps) > 0 { |
| 229 | ctx.Build(pctx, BuildParams{ |
| 230 | Rule: ErrorRule, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 231 | Outputs: r.Outputs(), |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 232 | Description: desc, |
| 233 | Args: map[string]string{ |
| 234 | "error": "missing dependencies: " + strings.Join(r.missingDeps, ", "), |
| 235 | }, |
| 236 | }) |
| 237 | return |
| 238 | } |
| 239 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 240 | if len(r.Commands()) > 0 { |
| 241 | ctx.Build(pctx, BuildParams{ |
| 242 | Rule: ctx.Rule(pctx, name, blueprint.RuleParams{ |
Colin Cross | 0b9f31f | 2019-02-28 11:00:01 -0800 | [diff] [blame] | 243 | Command: strings.Join(proptools.NinjaEscapeList(r.Commands()), " && "), |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 244 | CommandDeps: r.Tools().Strings(), |
Colin Cross | baa676f | 2019-02-25 14:56:01 -0800 | [diff] [blame] | 245 | Restat: r.restat, |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 246 | }), |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 247 | Implicits: r.Inputs(), |
| 248 | Outputs: r.Outputs(), |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 249 | Description: desc, |
| 250 | }) |
| 251 | } |
| 252 | } |
| 253 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 254 | // RuleBuilderCommand is a builder for a command in a command line. It can be mutated by its methods to add to the |
| 255 | // command and track dependencies. The methods mutate the RuleBuilderCommand in place, as well as return the |
| 256 | // RuleBuilderCommand, so they can be used chained or unchained. All methods that add text implicitly add a single |
| 257 | // space as a separator from the previous method. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 258 | type RuleBuilderCommand struct { |
| 259 | buf []byte |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 260 | inputs Paths |
| 261 | outputs WritablePaths |
| 262 | tools Paths |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 265 | // Text adds the specified raw text to the command line. The text should not contain input or output paths or the |
| 266 | // rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 267 | func (c *RuleBuilderCommand) Text(text string) *RuleBuilderCommand { |
| 268 | if len(c.buf) > 0 { |
| 269 | c.buf = append(c.buf, ' ') |
| 270 | } |
| 271 | c.buf = append(c.buf, text...) |
| 272 | return c |
| 273 | } |
| 274 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 275 | // Textf adds the specified formatted text to the command line. The text should not contain input or output paths or |
| 276 | // the rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 277 | func (c *RuleBuilderCommand) Textf(format string, a ...interface{}) *RuleBuilderCommand { |
| 278 | return c.Text(fmt.Sprintf(format, a...)) |
| 279 | } |
| 280 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 281 | // Flag adds the specified raw text to the command line. The text should not contain input or output paths or the |
| 282 | // rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 283 | func (c *RuleBuilderCommand) Flag(flag string) *RuleBuilderCommand { |
| 284 | return c.Text(flag) |
| 285 | } |
| 286 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 287 | // FlagWithArg adds the specified flag and argument text to the command line, with no separator between them. The flag |
| 288 | // and argument should not contain input or output paths or the rule will not have them listed in its dependencies or |
| 289 | // outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 290 | func (c *RuleBuilderCommand) FlagWithArg(flag, arg string) *RuleBuilderCommand { |
| 291 | return c.Text(flag + arg) |
| 292 | } |
| 293 | |
Colin Cross | c7ed004 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 294 | // FlagForEachArg adds the specified flag joined with each argument to the command line. The result is identical to |
| 295 | // calling FlagWithArg for argument. |
| 296 | func (c *RuleBuilderCommand) FlagForEachArg(flag string, args []string) *RuleBuilderCommand { |
| 297 | for _, arg := range args { |
| 298 | c.FlagWithArg(flag, arg) |
| 299 | } |
| 300 | return c |
| 301 | } |
| 302 | |
Roland Levillain | 2da5d9a | 2019-02-27 16:56:41 +0000 | [diff] [blame] | 303 | // 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] | 304 | // and no separator between the flag and arguments. The flag and arguments should not contain input or output paths or |
| 305 | // the rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 306 | func (c *RuleBuilderCommand) FlagWithList(flag string, list []string, sep string) *RuleBuilderCommand { |
| 307 | return c.Text(flag + strings.Join(list, sep)) |
| 308 | } |
| 309 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 310 | // Tool adds the specified tool path to the command line. The path will be also added to the dependencies returned by |
| 311 | // RuleBuilder.Tools. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 312 | func (c *RuleBuilderCommand) Tool(path Path) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 313 | c.tools = append(c.tools, path) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 314 | return c.Text(path.String()) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 317 | // Input adds the specified input path to the command line. The path will also be added to the dependencies returned by |
| 318 | // RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 319 | func (c *RuleBuilderCommand) Input(path Path) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 320 | c.inputs = append(c.inputs, path) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 321 | return c.Text(path.String()) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 324 | // Inputs adds the specified input paths to the command line, separated by spaces. The paths will also be added to the |
| 325 | // dependencies returned by RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 326 | func (c *RuleBuilderCommand) Inputs(paths Paths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 327 | for _, path := range paths { |
| 328 | c.Input(path) |
| 329 | } |
| 330 | return c |
| 331 | } |
| 332 | |
| 333 | // Implicit adds the specified input path to the dependencies returned by RuleBuilder.Inputs without modifying the |
| 334 | // command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 335 | func (c *RuleBuilderCommand) Implicit(path Path) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 336 | c.inputs = append(c.inputs, path) |
| 337 | return c |
| 338 | } |
| 339 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 340 | // Implicits adds the specified input paths to the dependencies returned by RuleBuilder.Inputs without modifying the |
| 341 | // command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 342 | func (c *RuleBuilderCommand) Implicits(paths Paths) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 343 | c.inputs = append(c.inputs, paths...) |
| 344 | return c |
| 345 | } |
| 346 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 347 | // Output adds the specified output path to the command line. The path will also be added to the outputs returned by |
| 348 | // RuleBuilder.Outputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 349 | func (c *RuleBuilderCommand) Output(path WritablePath) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 350 | c.outputs = append(c.outputs, path) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 351 | return c.Text(path.String()) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 354 | // Outputs adds the specified output paths to the command line, separated by spaces. The paths will also be added to |
| 355 | // the outputs returned by RuleBuilder.Outputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 356 | func (c *RuleBuilderCommand) Outputs(paths WritablePaths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 357 | for _, path := range paths { |
| 358 | c.Output(path) |
| 359 | } |
| 360 | return c |
| 361 | } |
| 362 | |
| 363 | // ImplicitOutput adds the specified output path to the dependencies returned by RuleBuilder.Outputs without modifying |
| 364 | // the command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 365 | func (c *RuleBuilderCommand) ImplicitOutput(path WritablePath) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 366 | c.outputs = append(c.outputs, path) |
| 367 | return c |
| 368 | } |
| 369 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 370 | // ImplicitOutputs adds the specified output paths to the dependencies returned by RuleBuilder.Outputs without modifying |
| 371 | // the command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 372 | func (c *RuleBuilderCommand) ImplicitOutputs(paths WritablePaths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 373 | c.outputs = append(c.outputs, paths...) |
| 374 | return c |
| 375 | } |
| 376 | |
| 377 | // FlagWithInput adds the specified flag and input path to the command line, with no separator between them. The path |
| 378 | // will also be added to the dependencies returned by RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 379 | func (c *RuleBuilderCommand) FlagWithInput(flag string, path Path) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 380 | c.inputs = append(c.inputs, path) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 381 | return c.Text(flag + path.String()) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 384 | // FlagWithInputList adds the specified flag and input paths to the command line, with the inputs joined by sep |
| 385 | // and no separator between the flag and inputs. The input paths will also be added to the dependencies returned by |
| 386 | // RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 387 | func (c *RuleBuilderCommand) FlagWithInputList(flag string, paths Paths, sep string) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 388 | c.inputs = append(c.inputs, paths...) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 389 | return c.FlagWithList(flag, paths.Strings(), sep) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 392 | // FlagForEachInput adds the specified flag joined with each input path to the command line. The input paths will also |
| 393 | // be added to the dependencies returned by RuleBuilder.Inputs. The result is identical to calling FlagWithInput for |
| 394 | // each input path. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 395 | func (c *RuleBuilderCommand) FlagForEachInput(flag string, paths Paths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 396 | for _, path := range paths { |
| 397 | c.FlagWithInput(flag, path) |
| 398 | } |
| 399 | return c |
| 400 | } |
| 401 | |
| 402 | // FlagWithOutput adds the specified flag and output path to the command line, with no separator between them. The path |
| 403 | // will also be added to the outputs returned by RuleBuilder.Outputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 404 | func (c *RuleBuilderCommand) FlagWithOutput(flag string, path WritablePath) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 405 | c.outputs = append(c.outputs, path) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 406 | return c.Text(flag + path.String()) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 409 | // String returns the command line. |
| 410 | func (c *RuleBuilderCommand) String() string { |
| 411 | return string(c.buf) |
| 412 | } |