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 ( |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 18 | "crypto/sha256" |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 19 | "fmt" |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 20 | "path/filepath" |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 21 | "sort" |
| 22 | "strings" |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 23 | "testing" |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 24 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 25 | "github.com/golang/protobuf/proto" |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 26 | "github.com/google/blueprint" |
| 27 | "github.com/google/blueprint/proptools" |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 28 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 29 | "android/soong/cmd/sbox/sbox_proto" |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 30 | "android/soong/remoteexec" |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 31 | "android/soong/response" |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 32 | "android/soong/shared" |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 33 | ) |
| 34 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 35 | const sboxSandboxBaseDir = "__SBOX_SANDBOX_DIR__" |
| 36 | const sboxOutSubDir = "out" |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 37 | const sboxToolsSubDir = "tools" |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 38 | const sboxOutDir = sboxSandboxBaseDir + "/" + sboxOutSubDir |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 39 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 40 | // RuleBuilder provides an alternative to ModuleContext.Rule and ModuleContext.Build to add a command line to the build |
| 41 | // graph. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 42 | type RuleBuilder struct { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 43 | pctx PackageContext |
| 44 | ctx BuilderContext |
| 45 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 46 | commands []*RuleBuilderCommand |
| 47 | installs RuleBuilderInstalls |
| 48 | temporariesSet map[WritablePath]bool |
| 49 | restat bool |
| 50 | sbox bool |
| 51 | highmem bool |
| 52 | remoteable RemoteRuleSupports |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 53 | rbeParams *remoteexec.REParams |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 54 | outDir WritablePath |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 55 | sboxTools bool |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 56 | sboxInputs bool |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 57 | sboxManifestPath WritablePath |
| 58 | missingDeps []string |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 61 | // NewRuleBuilder returns a newly created RuleBuilder. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 62 | func NewRuleBuilder(pctx PackageContext, ctx BuilderContext) *RuleBuilder { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 63 | return &RuleBuilder{ |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 64 | pctx: pctx, |
| 65 | ctx: ctx, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 66 | temporariesSet: make(map[WritablePath]bool), |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 67 | } |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // RuleBuilderInstall is a tuple of install from and to locations. |
| 71 | type RuleBuilderInstall struct { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 72 | From Path |
| 73 | To string |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 76 | type RuleBuilderInstalls []RuleBuilderInstall |
| 77 | |
| 78 | // String returns the RuleBuilderInstalls in the form used by $(call copy-many-files) in Make, a space separated |
| 79 | // list of from:to tuples. |
| 80 | func (installs RuleBuilderInstalls) String() string { |
| 81 | sb := strings.Builder{} |
| 82 | for i, install := range installs { |
| 83 | if i != 0 { |
| 84 | sb.WriteRune(' ') |
| 85 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 86 | sb.WriteString(install.From.String()) |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 87 | sb.WriteRune(':') |
| 88 | sb.WriteString(install.To) |
| 89 | } |
| 90 | return sb.String() |
| 91 | } |
| 92 | |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 93 | // MissingDeps adds modules to the list of missing dependencies. If MissingDeps |
| 94 | // is called with a non-empty input, any call to Build will result in a rule |
| 95 | // that will print an error listing the missing dependencies and fail. |
| 96 | // MissingDeps should only be called if Config.AllowMissingDependencies() is |
| 97 | // true. |
| 98 | func (r *RuleBuilder) MissingDeps(missingDeps []string) { |
| 99 | r.missingDeps = append(r.missingDeps, missingDeps...) |
| 100 | } |
| 101 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 102 | // 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] | 103 | // |
| 104 | // Restat is not compatible with Sbox() |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 105 | func (r *RuleBuilder) Restat() *RuleBuilder { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 106 | if r.sbox { |
| 107 | panic("Restat() is not compatible with Sbox()") |
| 108 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 109 | r.restat = true |
| 110 | return r |
| 111 | } |
| 112 | |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 113 | // HighMem marks the rule as a high memory rule, which will limit how many run in parallel with other high memory |
| 114 | // rules. |
| 115 | func (r *RuleBuilder) HighMem() *RuleBuilder { |
| 116 | r.highmem = true |
| 117 | return r |
| 118 | } |
| 119 | |
| 120 | // Remoteable marks the rule as supporting remote execution. |
| 121 | func (r *RuleBuilder) Remoteable(supports RemoteRuleSupports) *RuleBuilder { |
| 122 | r.remoteable = supports |
| 123 | return r |
| 124 | } |
| 125 | |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 126 | // Rewrapper marks the rule as running inside rewrapper using the given params in order to support |
| 127 | // running on RBE. During RuleBuilder.Build the params will be combined with the inputs, outputs |
| 128 | // and tools known to RuleBuilder to prepend an appropriate rewrapper command line to the rule's |
| 129 | // command line. |
| 130 | func (r *RuleBuilder) Rewrapper(params *remoteexec.REParams) *RuleBuilder { |
| 131 | if !r.sboxInputs { |
| 132 | panic(fmt.Errorf("RuleBuilder.Rewrapper must be called after RuleBuilder.SandboxInputs")) |
| 133 | } |
| 134 | r.rbeParams = params |
| 135 | return r |
| 136 | } |
| 137 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 138 | // Sbox marks the rule as needing to be wrapped by sbox. The outputDir should point to the output |
| 139 | // directory that sbox will wipe. It should not be written to by any other rule. manifestPath should |
| 140 | // point to a location where sbox's manifest will be written and must be outside outputDir. sbox |
| 141 | // will ensure that all outputs have been written, and will discard any output files that were not |
| 142 | // specified. |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 143 | // |
| 144 | // Sbox is not compatible with Restat() |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 145 | func (r *RuleBuilder) Sbox(outputDir WritablePath, manifestPath WritablePath) *RuleBuilder { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 146 | if r.sbox { |
| 147 | panic("Sbox() may not be called more than once") |
| 148 | } |
| 149 | if len(r.commands) > 0 { |
| 150 | panic("Sbox() may not be called after Command()") |
| 151 | } |
| 152 | if r.restat { |
| 153 | panic("Sbox() is not compatible with Restat()") |
| 154 | } |
| 155 | r.sbox = true |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 156 | r.outDir = outputDir |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 157 | r.sboxManifestPath = manifestPath |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 158 | return r |
| 159 | } |
| 160 | |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 161 | // SandboxTools enables tool sandboxing for the rule by copying any referenced tools into the |
| 162 | // sandbox. |
| 163 | func (r *RuleBuilder) SandboxTools() *RuleBuilder { |
| 164 | if !r.sbox { |
| 165 | panic("SandboxTools() must be called after Sbox()") |
| 166 | } |
| 167 | if len(r.commands) > 0 { |
| 168 | panic("SandboxTools() may not be called after Command()") |
| 169 | } |
| 170 | r.sboxTools = true |
| 171 | return r |
| 172 | } |
| 173 | |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 174 | // SandboxInputs enables input sandboxing for the rule by copying any referenced inputs into the |
| 175 | // sandbox. It also implies SandboxTools(). |
| 176 | // |
| 177 | // Sandboxing inputs requires RuleBuilder to be aware of all references to input paths. Paths |
| 178 | // that are passed to RuleBuilder outside of the methods that expect inputs, for example |
| 179 | // FlagWithArg, must use RuleBuilderCommand.PathForInput to translate the path to one that matches |
| 180 | // the sandbox layout. |
| 181 | func (r *RuleBuilder) SandboxInputs() *RuleBuilder { |
| 182 | if !r.sbox { |
| 183 | panic("SandboxInputs() must be called after Sbox()") |
| 184 | } |
| 185 | if len(r.commands) > 0 { |
| 186 | panic("SandboxInputs() may not be called after Command()") |
| 187 | } |
| 188 | r.sboxTools = true |
| 189 | r.sboxInputs = true |
| 190 | return r |
| 191 | } |
| 192 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 193 | // Install associates an output of the rule with an install location, which can be retrieved later using |
| 194 | // RuleBuilder.Installs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 195 | func (r *RuleBuilder) Install(from Path, to string) { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 196 | r.installs = append(r.installs, RuleBuilderInstall{from, to}) |
| 197 | } |
| 198 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 199 | // Command returns a new RuleBuilderCommand for the rule. The commands will be ordered in the rule by when they were |
| 200 | // created by this method. That can be mutated through their methods in any order, as long as the mutations do not |
| 201 | // race with any call to Build. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 202 | func (r *RuleBuilder) Command() *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 203 | command := &RuleBuilderCommand{ |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 204 | rule: r, |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 205 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 206 | r.commands = append(r.commands, command) |
| 207 | return command |
| 208 | } |
| 209 | |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 210 | // Temporary marks an output of a command as an intermediate file that will be used as an input to another command |
| 211 | // in the same rule, and should not be listed in Outputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 212 | func (r *RuleBuilder) Temporary(path WritablePath) { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 213 | r.temporariesSet[path] = true |
| 214 | } |
| 215 | |
| 216 | // DeleteTemporaryFiles adds a command to the rule that deletes any outputs that have been marked using Temporary |
| 217 | // when the rule runs. DeleteTemporaryFiles should be called after all calls to Temporary. |
| 218 | func (r *RuleBuilder) DeleteTemporaryFiles() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 219 | var temporariesList WritablePaths |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 220 | |
| 221 | for intermediate := range r.temporariesSet { |
| 222 | temporariesList = append(temporariesList, intermediate) |
| 223 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 224 | |
| 225 | sort.Slice(temporariesList, func(i, j int) bool { |
| 226 | return temporariesList[i].String() < temporariesList[j].String() |
| 227 | }) |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 228 | |
| 229 | r.Command().Text("rm").Flag("-f").Outputs(temporariesList) |
| 230 | } |
| 231 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 232 | // Inputs returns the list of paths that were passed to the RuleBuilderCommand methods that take |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 233 | // input paths, such as RuleBuilderCommand.Input, RuleBuilderCommand.Implicit, or |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 234 | // RuleBuilderCommand.FlagWithInput. Inputs to a command that are also outputs of another command |
| 235 | // in the same RuleBuilder are filtered out. The list is sorted and duplicates removed. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 236 | func (r *RuleBuilder) Inputs() Paths { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 237 | outputs := r.outputSet() |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 238 | depFiles := r.depFileSet() |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 239 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 240 | inputs := make(map[string]Path) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 241 | for _, c := range r.commands { |
Ramy Medhat | 2f99eec | 2020-06-13 17:38:27 -0400 | [diff] [blame] | 242 | for _, input := range append(c.inputs, c.implicits...) { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 243 | inputStr := input.String() |
| 244 | if _, isOutput := outputs[inputStr]; !isOutput { |
| 245 | if _, isDepFile := depFiles[inputStr]; !isDepFile { |
| 246 | inputs[input.String()] = input |
| 247 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 252 | var inputList Paths |
| 253 | for _, input := range inputs { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 254 | inputList = append(inputList, input) |
| 255 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 256 | |
| 257 | sort.Slice(inputList, func(i, j int) bool { |
| 258 | return inputList[i].String() < inputList[j].String() |
| 259 | }) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 260 | |
| 261 | return inputList |
| 262 | } |
| 263 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 264 | // OrderOnlys returns the list of paths that were passed to the RuleBuilderCommand.OrderOnly or |
| 265 | // RuleBuilderCommand.OrderOnlys. The list is sorted and duplicates removed. |
| 266 | func (r *RuleBuilder) OrderOnlys() Paths { |
| 267 | orderOnlys := make(map[string]Path) |
| 268 | for _, c := range r.commands { |
| 269 | for _, orderOnly := range c.orderOnlys { |
| 270 | orderOnlys[orderOnly.String()] = orderOnly |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | var orderOnlyList Paths |
| 275 | for _, orderOnly := range orderOnlys { |
| 276 | orderOnlyList = append(orderOnlyList, orderOnly) |
| 277 | } |
| 278 | |
| 279 | sort.Slice(orderOnlyList, func(i, j int) bool { |
| 280 | return orderOnlyList[i].String() < orderOnlyList[j].String() |
| 281 | }) |
| 282 | |
| 283 | return orderOnlyList |
| 284 | } |
| 285 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 286 | func (r *RuleBuilder) outputSet() map[string]WritablePath { |
| 287 | outputs := make(map[string]WritablePath) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 288 | for _, c := range r.commands { |
| 289 | for _, output := range c.outputs { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 290 | outputs[output.String()] = output |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | return outputs |
| 294 | } |
| 295 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 296 | // Outputs returns the list of paths that were passed to the RuleBuilderCommand methods that take |
| 297 | // output paths, such as RuleBuilderCommand.Output, RuleBuilderCommand.ImplicitOutput, or |
| 298 | // RuleBuilderCommand.FlagWithInput. The list is sorted and duplicates removed. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 299 | func (r *RuleBuilder) Outputs() WritablePaths { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 300 | outputs := r.outputSet() |
| 301 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 302 | var outputList WritablePaths |
| 303 | for _, output := range outputs { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 304 | if !r.temporariesSet[output] { |
| 305 | outputList = append(outputList, output) |
| 306 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 307 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 308 | |
| 309 | sort.Slice(outputList, func(i, j int) bool { |
| 310 | return outputList[i].String() < outputList[j].String() |
| 311 | }) |
| 312 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 313 | return outputList |
| 314 | } |
| 315 | |
Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame] | 316 | func (r *RuleBuilder) symlinkOutputSet() map[string]WritablePath { |
| 317 | symlinkOutputs := make(map[string]WritablePath) |
| 318 | for _, c := range r.commands { |
| 319 | for _, symlinkOutput := range c.symlinkOutputs { |
| 320 | symlinkOutputs[symlinkOutput.String()] = symlinkOutput |
| 321 | } |
| 322 | } |
| 323 | return symlinkOutputs |
| 324 | } |
| 325 | |
| 326 | // SymlinkOutputs returns the list of paths that the executor (Ninja) would |
| 327 | // verify, after build edge completion, that: |
| 328 | // |
| 329 | // 1) Created output symlinks match the list of paths in this list exactly (no more, no fewer) |
| 330 | // 2) Created output files are *not* declared in this list. |
| 331 | // |
| 332 | // These symlink outputs are expected to be a subset of outputs or implicit |
| 333 | // outputs, or they would fail validation at build param construction time |
| 334 | // later, to support other non-rule-builder approaches for constructing |
| 335 | // statements. |
| 336 | func (r *RuleBuilder) SymlinkOutputs() WritablePaths { |
| 337 | symlinkOutputs := r.symlinkOutputSet() |
| 338 | |
| 339 | var symlinkOutputList WritablePaths |
| 340 | for _, symlinkOutput := range symlinkOutputs { |
| 341 | symlinkOutputList = append(symlinkOutputList, symlinkOutput) |
| 342 | } |
| 343 | |
| 344 | sort.Slice(symlinkOutputList, func(i, j int) bool { |
| 345 | return symlinkOutputList[i].String() < symlinkOutputList[j].String() |
| 346 | }) |
| 347 | |
| 348 | return symlinkOutputList |
| 349 | } |
| 350 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 351 | func (r *RuleBuilder) depFileSet() map[string]WritablePath { |
| 352 | depFiles := make(map[string]WritablePath) |
| 353 | for _, c := range r.commands { |
| 354 | for _, depFile := range c.depFiles { |
| 355 | depFiles[depFile.String()] = depFile |
| 356 | } |
| 357 | } |
| 358 | return depFiles |
| 359 | } |
| 360 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 361 | // DepFiles returns the list of paths that were passed to the RuleBuilderCommand methods that take depfile paths, such |
| 362 | // as RuleBuilderCommand.DepFile or RuleBuilderCommand.FlagWithDepFile. |
| 363 | func (r *RuleBuilder) DepFiles() WritablePaths { |
| 364 | var depFiles WritablePaths |
| 365 | |
| 366 | for _, c := range r.commands { |
| 367 | for _, depFile := range c.depFiles { |
| 368 | depFiles = append(depFiles, depFile) |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | return depFiles |
| 373 | } |
| 374 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 375 | // Installs returns the list of tuples passed to Install. |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 376 | func (r *RuleBuilder) Installs() RuleBuilderInstalls { |
| 377 | return append(RuleBuilderInstalls(nil), r.installs...) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 380 | func (r *RuleBuilder) toolsSet() map[string]Path { |
| 381 | tools := make(map[string]Path) |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 382 | for _, c := range r.commands { |
| 383 | for _, tool := range c.tools { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 384 | tools[tool.String()] = tool |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
| 388 | return tools |
| 389 | } |
| 390 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 391 | // Tools returns the list of paths that were passed to the RuleBuilderCommand.Tool method. The |
| 392 | // list is sorted and duplicates removed. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 393 | func (r *RuleBuilder) Tools() Paths { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 394 | toolsSet := r.toolsSet() |
| 395 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 396 | var toolsList Paths |
| 397 | for _, tool := range toolsSet { |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 398 | toolsList = append(toolsList, tool) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 399 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 400 | |
| 401 | sort.Slice(toolsList, func(i, j int) bool { |
| 402 | return toolsList[i].String() < toolsList[j].String() |
| 403 | }) |
| 404 | |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 405 | return toolsList |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 408 | // RspFileInputs returns the list of paths that were passed to the RuleBuilderCommand.FlagWithRspFileInputList method. |
| 409 | func (r *RuleBuilder) RspFileInputs() Paths { |
| 410 | var rspFileInputs Paths |
| 411 | for _, c := range r.commands { |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame^] | 412 | for _, rspFile := range c.rspFiles { |
| 413 | rspFileInputs = append(rspFileInputs, rspFile.paths...) |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | return rspFileInputs |
| 418 | } |
| 419 | |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame^] | 420 | func (r *RuleBuilder) rspFiles() []rspFileAndPaths { |
| 421 | var rspFiles []rspFileAndPaths |
Colin Cross | 70c4741 | 2021-03-12 17:48:14 -0800 | [diff] [blame] | 422 | for _, c := range r.commands { |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame^] | 423 | rspFiles = append(rspFiles, c.rspFiles...) |
Colin Cross | 70c4741 | 2021-03-12 17:48:14 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame^] | 426 | return rspFiles |
Colin Cross | 70c4741 | 2021-03-12 17:48:14 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 429 | // Commands returns a slice containing the built command line for each call to RuleBuilder.Command. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 430 | func (r *RuleBuilder) Commands() []string { |
| 431 | var commands []string |
| 432 | for _, c := range r.commands { |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 433 | commands = append(commands, c.String()) |
| 434 | } |
| 435 | return commands |
| 436 | } |
| 437 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 438 | // BuilderContext is a subset of ModuleContext and SingletonContext. |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 439 | type BuilderContext interface { |
| 440 | PathContext |
| 441 | Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule |
| 442 | Build(PackageContext, BuildParams) |
| 443 | } |
| 444 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 445 | var _ BuilderContext = ModuleContext(nil) |
| 446 | var _ BuilderContext = SingletonContext(nil) |
| 447 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 448 | func (r *RuleBuilder) depFileMergerCmd(depFiles WritablePaths) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 449 | return r.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 450 | BuiltTool("dep_fixer"). |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 451 | Inputs(depFiles.Paths()) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 454 | // Build adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for |
| 455 | // Outputs. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 456 | func (r *RuleBuilder) Build(name string, desc string) { |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 457 | name = ninjaNameEscape(name) |
| 458 | |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 459 | if len(r.missingDeps) > 0 { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 460 | r.ctx.Build(pctx, BuildParams{ |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 461 | Rule: ErrorRule, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 462 | Outputs: r.Outputs(), |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 463 | OrderOnly: r.OrderOnlys(), |
Colin Cross | 0d2f40a | 2019-02-05 22:31:15 -0800 | [diff] [blame] | 464 | Description: desc, |
| 465 | Args: map[string]string{ |
| 466 | "error": "missing dependencies: " + strings.Join(r.missingDeps, ", "), |
| 467 | }, |
| 468 | }) |
| 469 | return |
| 470 | } |
| 471 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 472 | var depFile WritablePath |
| 473 | var depFormat blueprint.Deps |
| 474 | if depFiles := r.DepFiles(); len(depFiles) > 0 { |
| 475 | depFile = depFiles[0] |
| 476 | depFormat = blueprint.DepsGCC |
| 477 | if len(depFiles) > 1 { |
| 478 | // Add a command locally that merges all depfiles together into the first depfile. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 479 | r.depFileMergerCmd(depFiles) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 480 | |
| 481 | if r.sbox { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 482 | // Check for Rel() errors, as all depfiles should be in the output dir. Errors |
| 483 | // will be reported to the ctx. |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 484 | for _, path := range depFiles[1:] { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 485 | Rel(r.ctx, r.outDir.String(), path.String()) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 486 | } |
| 487 | } |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 491 | tools := r.Tools() |
Colin Cross | b70a1a9 | 2021-03-12 17:51:32 -0800 | [diff] [blame] | 492 | commands := r.Commands() |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 493 | outputs := r.Outputs() |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 494 | inputs := r.Inputs() |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame^] | 495 | rspFiles := r.rspFiles() |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 496 | |
| 497 | if len(commands) == 0 { |
| 498 | return |
| 499 | } |
| 500 | if len(outputs) == 0 { |
| 501 | panic("No outputs specified from any Commands") |
| 502 | } |
| 503 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 504 | commandString := strings.Join(commands, " && ") |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 505 | |
| 506 | if r.sbox { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 507 | // If running the command inside sbox, write the rule data out to an sbox |
| 508 | // manifest.textproto. |
| 509 | manifest := sbox_proto.Manifest{} |
| 510 | command := sbox_proto.Command{} |
| 511 | manifest.Commands = append(manifest.Commands, &command) |
| 512 | command.Command = proto.String(commandString) |
Colin Cross | 151b9ff | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 513 | |
Colin Cross | 619b9ab | 2020-11-20 18:44:31 +0000 | [diff] [blame] | 514 | if depFile != nil { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 515 | manifest.OutputDepfile = proto.String(depFile.String()) |
Colin Cross | 619b9ab | 2020-11-20 18:44:31 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 518 | // If sandboxing tools is enabled, add copy rules to the manifest to copy each tool |
| 519 | // into the sbox directory. |
| 520 | if r.sboxTools { |
| 521 | for _, tool := range tools { |
| 522 | command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{ |
| 523 | From: proto.String(tool.String()), |
| 524 | To: proto.String(sboxPathForToolRel(r.ctx, tool)), |
| 525 | }) |
| 526 | } |
| 527 | for _, c := range r.commands { |
| 528 | for _, tool := range c.packagedTools { |
| 529 | command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{ |
| 530 | From: proto.String(tool.srcPath.String()), |
| 531 | To: proto.String(sboxPathForPackagedToolRel(tool)), |
| 532 | Executable: proto.Bool(tool.executable), |
| 533 | }) |
| 534 | tools = append(tools, tool.srcPath) |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 539 | // If sandboxing inputs is enabled, add copy rules to the manifest to copy each input |
| 540 | // into the sbox directory. |
| 541 | if r.sboxInputs { |
| 542 | for _, input := range inputs { |
| 543 | command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{ |
| 544 | From: proto.String(input.String()), |
| 545 | To: proto.String(r.sboxPathForInputRel(input)), |
| 546 | }) |
| 547 | } |
| 548 | |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame^] | 549 | // If using rsp files copy them and their contents into the sbox directory with |
| 550 | // the appropriate path mappings. |
| 551 | for _, rspFile := range rspFiles { |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 552 | command.RspFiles = append(command.RspFiles, &sbox_proto.RspFile{ |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame^] | 553 | File: proto.String(rspFile.file.String()), |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 554 | // These have to match the logic in sboxPathForInputRel |
| 555 | PathMappings: []*sbox_proto.PathMapping{ |
| 556 | { |
| 557 | From: proto.String(r.outDir.String()), |
| 558 | To: proto.String(sboxOutSubDir), |
| 559 | }, |
| 560 | { |
| 561 | From: proto.String(PathForOutput(r.ctx).String()), |
| 562 | To: proto.String(sboxOutSubDir), |
| 563 | }, |
| 564 | }, |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 565 | }) |
| 566 | } |
| 567 | |
| 568 | command.Chdir = proto.Bool(true) |
| 569 | } |
| 570 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 571 | // Add copy rules to the manifest to copy each output file from the sbox directory. |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 572 | // to the output directory after running the commands. |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 573 | sboxOutputs := make([]string, len(outputs)) |
| 574 | for i, output := range outputs { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 575 | rel := Rel(r.ctx, r.outDir.String(), output.String()) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 576 | sboxOutputs[i] = filepath.Join(sboxOutDir, rel) |
| 577 | command.CopyAfter = append(command.CopyAfter, &sbox_proto.Copy{ |
| 578 | From: proto.String(filepath.Join(sboxOutSubDir, rel)), |
| 579 | To: proto.String(output.String()), |
| 580 | }) |
| 581 | } |
Colin Cross | 619b9ab | 2020-11-20 18:44:31 +0000 | [diff] [blame] | 582 | |
Colin Cross | 5334edd | 2021-03-11 17:18:21 -0800 | [diff] [blame] | 583 | // Outputs that were marked Temporary will not be checked that they are in the output |
| 584 | // directory by the loop above, check them here. |
| 585 | for path := range r.temporariesSet { |
| 586 | Rel(r.ctx, r.outDir.String(), path.String()) |
| 587 | } |
| 588 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 589 | // Add a hash of the list of input files to the manifest so that the textproto file |
| 590 | // changes when the list of input files changes and causes the sbox rule that |
| 591 | // depends on it to rerun. |
| 592 | command.InputHash = proto.String(hashSrcFiles(inputs)) |
Colin Cross | 619b9ab | 2020-11-20 18:44:31 +0000 | [diff] [blame] | 593 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 594 | // Verify that the manifest textproto is not inside the sbox output directory, otherwise |
| 595 | // it will get deleted when the sbox rule clears its output directory. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 596 | _, manifestInOutDir := MaybeRel(r.ctx, r.outDir.String(), r.sboxManifestPath.String()) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 597 | if manifestInOutDir { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 598 | ReportPathErrorf(r.ctx, "sbox rule %q manifestPath %q must not be in outputDir %q", |
| 599 | name, r.sboxManifestPath.String(), r.outDir.String()) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | // Create a rule to write the manifest as a the textproto. |
Colin Cross | 1c217fd | 2021-03-12 17:24:18 -0800 | [diff] [blame] | 603 | WriteFileRule(r.ctx, r.sboxManifestPath, proto.MarshalTextString(&manifest)) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 604 | |
| 605 | // Generate a new string to use as the command line of the sbox rule. This uses |
| 606 | // a RuleBuilderCommand as a convenience method of building the command line, then |
| 607 | // converts it to a string to replace commandString. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 608 | sboxCmd := &RuleBuilderCommand{ |
| 609 | rule: &RuleBuilder{ |
| 610 | ctx: r.ctx, |
| 611 | }, |
| 612 | } |
| 613 | sboxCmd.Text("rm -rf").Output(r.outDir) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 614 | sboxCmd.Text("&&") |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 615 | sboxCmd.BuiltTool("sbox"). |
| 616 | Flag("--sandbox-path").Text(shared.TempDirForOutDir(PathForOutput(r.ctx).String())). |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 617 | Flag("--manifest").Input(r.sboxManifestPath) |
| 618 | |
| 619 | // Replace the command string, and add the sbox tool and manifest textproto to the |
| 620 | // dependencies of the final sbox rule. |
Colin Cross | cfec40c | 2019-07-08 17:07:18 -0700 | [diff] [blame] | 621 | commandString = sboxCmd.buf.String() |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 622 | tools = append(tools, sboxCmd.tools...) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 623 | inputs = append(inputs, sboxCmd.inputs...) |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 624 | |
| 625 | if r.rbeParams != nil { |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 626 | // RBE needs a list of input files to copy to the remote builder. For inputs already |
| 627 | // listed in an rsp file, pass the rsp file directly to rewrapper. For the rest, |
| 628 | // create a new rsp file to pass to rewrapper. |
| 629 | var remoteRspFiles Paths |
| 630 | var remoteInputs Paths |
| 631 | |
| 632 | remoteInputs = append(remoteInputs, inputs...) |
| 633 | remoteInputs = append(remoteInputs, tools...) |
| 634 | |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame^] | 635 | for _, rspFile := range rspFiles { |
| 636 | remoteInputs = append(remoteInputs, rspFile.file) |
| 637 | remoteRspFiles = append(remoteRspFiles, rspFile.file) |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 638 | } |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 639 | |
| 640 | if len(remoteInputs) > 0 { |
| 641 | inputsListFile := r.sboxManifestPath.ReplaceExtension(r.ctx, "rbe_inputs.list") |
| 642 | writeRspFileRule(r.ctx, inputsListFile, remoteInputs) |
| 643 | remoteRspFiles = append(remoteRspFiles, inputsListFile) |
| 644 | // Add the new rsp file as an extra input to the rule. |
| 645 | inputs = append(inputs, inputsListFile) |
| 646 | } |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 647 | |
| 648 | r.rbeParams.OutputFiles = outputs.Strings() |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 649 | r.rbeParams.RSPFiles = remoteRspFiles.Strings() |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 650 | rewrapperCommand := r.rbeParams.NoVarTemplate(r.ctx.Config().RBEWrapper()) |
| 651 | commandString = rewrapperCommand + " bash -c '" + strings.ReplaceAll(commandString, `'`, `'\''`) + "'" |
| 652 | } |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 653 | } else { |
| 654 | // If not using sbox the rule will run the command directly, put the hash of the |
| 655 | // list of input files in a comment at the end of the command line to ensure ninja |
| 656 | // reruns the rule when the list of input files changes. |
| 657 | commandString += " # hash of input list: " + hashSrcFiles(inputs) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 660 | // Ninja doesn't like multiple outputs when depfiles are enabled, move all but the first output to |
Colin Cross | 70c4741 | 2021-03-12 17:48:14 -0800 | [diff] [blame] | 661 | // ImplicitOutputs. RuleBuilder doesn't use "$out", so the distinction between Outputs and |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 662 | // ImplicitOutputs doesn't matter. |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 663 | output := outputs[0] |
| 664 | implicitOutputs := outputs[1:] |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 665 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 666 | var rspFile, rspFileContent string |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame^] | 667 | var rspFileInputs Paths |
| 668 | if len(rspFiles) > 0 { |
| 669 | // The first rsp files uses Ninja's rsp file support for the rule |
| 670 | rspFile = rspFiles[0].file.String() |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 671 | // Use "$in" for rspFileContent to avoid duplicating the list of files in the dependency |
| 672 | // list and in the contents of the rsp file. Inputs to the rule that are not in the |
| 673 | // rsp file will be listed in Implicits instead of Inputs so they don't show up in "$in". |
| 674 | rspFileContent = "$in" |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame^] | 675 | rspFileInputs = append(rspFileInputs, rspFiles[0].paths...) |
| 676 | |
| 677 | for _, rspFile := range rspFiles[1:] { |
| 678 | // Any additional rsp files need an extra rule to write the file. |
| 679 | writeRspFileRule(r.ctx, rspFile.file, rspFile.paths) |
| 680 | // The main rule needs to depend on the inputs listed in the extra rsp file. |
| 681 | inputs = append(inputs, rspFile.paths...) |
| 682 | // The main rule needs to depend on the extra rsp file. |
| 683 | inputs = append(inputs, rspFile.file) |
| 684 | } |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 687 | var pool blueprint.Pool |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 688 | if r.ctx.Config().UseGoma() && r.remoteable.Goma { |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 689 | // When USE_GOMA=true is set and the rule is supported by goma, allow jobs to run outside the local pool. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 690 | } else if r.ctx.Config().UseRBE() && r.remoteable.RBE { |
Ramy Medhat | 944839a | 2020-03-31 22:14:52 -0400 | [diff] [blame] | 691 | // When USE_RBE=true is set and the rule is supported by RBE, use the remotePool. |
| 692 | pool = remotePool |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 693 | } else if r.highmem { |
| 694 | pool = highmemPool |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 695 | } else if r.ctx.Config().UseRemoteBuild() { |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 696 | pool = localPool |
| 697 | } |
| 698 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 699 | r.ctx.Build(r.pctx, BuildParams{ |
| 700 | Rule: r.ctx.Rule(pctx, name, blueprint.RuleParams{ |
Colin Cross | b70a1a9 | 2021-03-12 17:51:32 -0800 | [diff] [blame] | 701 | Command: proptools.NinjaEscape(commandString), |
Colin Cross | 4502978 | 2021-03-16 16:49:52 -0700 | [diff] [blame] | 702 | CommandDeps: proptools.NinjaEscapeList(tools.Strings()), |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 703 | Restat: r.restat, |
Colin Cross | 4502978 | 2021-03-16 16:49:52 -0700 | [diff] [blame] | 704 | Rspfile: proptools.NinjaEscape(rspFile), |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 705 | RspfileContent: rspFileContent, |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 706 | Pool: pool, |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 707 | }), |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 708 | Inputs: rspFileInputs, |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 709 | Implicits: inputs, |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 710 | Output: output, |
| 711 | ImplicitOutputs: implicitOutputs, |
Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame] | 712 | SymlinkOutputs: r.SymlinkOutputs(), |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 713 | Depfile: depFile, |
| 714 | Deps: depFormat, |
| 715 | Description: desc, |
| 716 | }) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 717 | } |
| 718 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 719 | // RuleBuilderCommand is a builder for a command in a command line. It can be mutated by its methods to add to the |
| 720 | // command and track dependencies. The methods mutate the RuleBuilderCommand in place, as well as return the |
| 721 | // RuleBuilderCommand, so they can be used chained or unchained. All methods that add text implicitly add a single |
| 722 | // space as a separator from the previous method. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 723 | type RuleBuilderCommand struct { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 724 | rule *RuleBuilder |
| 725 | |
Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame] | 726 | buf strings.Builder |
| 727 | inputs Paths |
| 728 | implicits Paths |
| 729 | orderOnlys Paths |
| 730 | outputs WritablePaths |
| 731 | symlinkOutputs WritablePaths |
| 732 | depFiles WritablePaths |
| 733 | tools Paths |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 734 | packagedTools []PackagingSpec |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame^] | 735 | rspFiles []rspFileAndPaths |
| 736 | } |
| 737 | |
| 738 | type rspFileAndPaths struct { |
| 739 | file WritablePath |
| 740 | paths Paths |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | func (c *RuleBuilderCommand) addInput(path Path) string { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 744 | c.inputs = append(c.inputs, path) |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 745 | return c.PathForInput(path) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 748 | func (c *RuleBuilderCommand) addImplicit(path Path) { |
Ramy Medhat | 2f99eec | 2020-06-13 17:38:27 -0400 | [diff] [blame] | 749 | c.implicits = append(c.implicits, path) |
Ramy Medhat | 2f99eec | 2020-06-13 17:38:27 -0400 | [diff] [blame] | 750 | } |
| 751 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 752 | func (c *RuleBuilderCommand) addOrderOnly(path Path) { |
| 753 | c.orderOnlys = append(c.orderOnlys, path) |
| 754 | } |
| 755 | |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 756 | // PathForInput takes an input path and returns the appropriate path to use on the command line. If |
| 757 | // sbox was enabled via a call to RuleBuilder.Sbox() and the path was an output path it returns a |
| 758 | // path with the placeholder prefix used for outputs in sbox. If sbox is not enabled it returns the |
| 759 | // original path. |
| 760 | func (c *RuleBuilderCommand) PathForInput(path Path) string { |
| 761 | if c.rule.sbox { |
| 762 | rel, inSandbox := c.rule._sboxPathForInputRel(path) |
| 763 | if inSandbox { |
| 764 | rel = filepath.Join(sboxSandboxBaseDir, rel) |
| 765 | } |
| 766 | return rel |
| 767 | } |
| 768 | return path.String() |
| 769 | } |
| 770 | |
| 771 | // PathsForInputs takes a list of input paths and returns the appropriate paths to use on the |
| 772 | // command line. If sbox was enabled via a call to RuleBuilder.Sbox() a path was an output path, it |
| 773 | // returns the path with the placeholder prefix used for outputs in sbox. If sbox is not enabled it |
| 774 | // returns the original paths. |
| 775 | func (c *RuleBuilderCommand) PathsForInputs(paths Paths) []string { |
| 776 | ret := make([]string, len(paths)) |
| 777 | for i, path := range paths { |
| 778 | ret[i] = c.PathForInput(path) |
| 779 | } |
| 780 | return ret |
| 781 | } |
| 782 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 783 | // PathForOutput takes an output path and returns the appropriate path to use on the command |
| 784 | // line. If sbox was enabled via a call to RuleBuilder.Sbox(), it returns a path with the |
| 785 | // placeholder prefix used for outputs in sbox. If sbox is not enabled it returns the |
| 786 | // original path. |
| 787 | func (c *RuleBuilderCommand) PathForOutput(path WritablePath) string { |
| 788 | if c.rule.sbox { |
| 789 | // Errors will be handled in RuleBuilder.Build where we have a context to report them |
| 790 | rel, _, _ := maybeRelErr(c.rule.outDir.String(), path.String()) |
| 791 | return filepath.Join(sboxOutDir, rel) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 792 | } |
| 793 | return path.String() |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 794 | } |
| 795 | |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 796 | // SboxPathForTool takes a path to a tool, which may be an output file or a source file, and returns |
| 797 | // the corresponding path for the tool in the sbox sandbox. It assumes that sandboxing and tool |
| 798 | // sandboxing are enabled. |
| 799 | func SboxPathForTool(ctx BuilderContext, path Path) string { |
| 800 | return filepath.Join(sboxSandboxBaseDir, sboxPathForToolRel(ctx, path)) |
| 801 | } |
| 802 | |
| 803 | func sboxPathForToolRel(ctx BuilderContext, path Path) string { |
| 804 | // Errors will be handled in RuleBuilder.Build where we have a context to report them |
| 805 | relOut, isRelOut, _ := maybeRelErr(PathForOutput(ctx, "host", ctx.Config().PrebuiltOS()).String(), path.String()) |
| 806 | if isRelOut { |
| 807 | // The tool is in the output directory, it will be copied to __SBOX_OUT_DIR__/tools/out |
| 808 | return filepath.Join(sboxToolsSubDir, "out", relOut) |
| 809 | } |
| 810 | // The tool is in the source directory, it will be copied to __SBOX_OUT_DIR__/tools/src |
| 811 | return filepath.Join(sboxToolsSubDir, "src", path.String()) |
| 812 | } |
| 813 | |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 814 | func (r *RuleBuilder) _sboxPathForInputRel(path Path) (rel string, inSandbox bool) { |
| 815 | // Errors will be handled in RuleBuilder.Build where we have a context to report them |
| 816 | rel, isRelSboxOut, _ := maybeRelErr(r.outDir.String(), path.String()) |
| 817 | if isRelSboxOut { |
| 818 | return filepath.Join(sboxOutSubDir, rel), true |
| 819 | } |
| 820 | if r.sboxInputs { |
| 821 | // When sandboxing inputs all inputs have to be copied into the sandbox. Input files that |
| 822 | // are outputs of other rules could be an arbitrary absolute path if OUT_DIR is set, so they |
| 823 | // will be copied to relative paths under __SBOX_OUT_DIR__/out. |
| 824 | rel, isRelOut, _ := maybeRelErr(PathForOutput(r.ctx).String(), path.String()) |
| 825 | if isRelOut { |
| 826 | return filepath.Join(sboxOutSubDir, rel), true |
| 827 | } |
| 828 | } |
| 829 | return path.String(), false |
| 830 | } |
| 831 | |
| 832 | func (r *RuleBuilder) sboxPathForInputRel(path Path) string { |
| 833 | rel, _ := r._sboxPathForInputRel(path) |
| 834 | return rel |
| 835 | } |
| 836 | |
| 837 | func (r *RuleBuilder) sboxPathsForInputsRel(paths Paths) []string { |
| 838 | ret := make([]string, len(paths)) |
| 839 | for i, path := range paths { |
| 840 | ret[i] = r.sboxPathForInputRel(path) |
| 841 | } |
| 842 | return ret |
| 843 | } |
| 844 | |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 845 | // SboxPathForPackagedTool takes a PackageSpec for a tool and returns the corresponding path for the |
| 846 | // tool after copying it into the sandbox. This can be used on the RuleBuilder command line to |
| 847 | // reference the tool. |
| 848 | func SboxPathForPackagedTool(spec PackagingSpec) string { |
| 849 | return filepath.Join(sboxSandboxBaseDir, sboxPathForPackagedToolRel(spec)) |
| 850 | } |
| 851 | |
| 852 | func sboxPathForPackagedToolRel(spec PackagingSpec) string { |
| 853 | return filepath.Join(sboxToolsSubDir, "out", spec.relPathInPackage) |
| 854 | } |
| 855 | |
| 856 | // PathForTool takes a path to a tool, which may be an output file or a source file, and returns |
| 857 | // the corresponding path for the tool in the sbox sandbox if sbox is enabled, or the original path |
| 858 | // if it is not. This can be used on the RuleBuilder command line to reference the tool. |
| 859 | func (c *RuleBuilderCommand) PathForTool(path Path) string { |
| 860 | if c.rule.sbox && c.rule.sboxTools { |
| 861 | return filepath.Join(sboxSandboxBaseDir, sboxPathForToolRel(c.rule.ctx, path)) |
| 862 | } |
| 863 | return path.String() |
| 864 | } |
| 865 | |
| 866 | // PackagedTool adds the specified tool path to the command line. It can only be used with tool |
| 867 | // sandboxing enabled by SandboxTools(), and will copy the tool into the sandbox. |
| 868 | func (c *RuleBuilderCommand) PackagedTool(spec PackagingSpec) *RuleBuilderCommand { |
| 869 | if !c.rule.sboxTools { |
| 870 | panic("PackagedTool() requires SandboxTools()") |
| 871 | } |
| 872 | |
| 873 | c.packagedTools = append(c.packagedTools, spec) |
| 874 | c.Text(sboxPathForPackagedToolRel(spec)) |
| 875 | return c |
| 876 | } |
| 877 | |
| 878 | // ImplicitPackagedTool copies the specified tool into the sandbox without modifying the command |
| 879 | // line. It can only be used with tool sandboxing enabled by SandboxTools(). |
| 880 | func (c *RuleBuilderCommand) ImplicitPackagedTool(spec PackagingSpec) *RuleBuilderCommand { |
| 881 | if !c.rule.sboxTools { |
| 882 | panic("ImplicitPackagedTool() requires SandboxTools()") |
| 883 | } |
| 884 | |
| 885 | c.packagedTools = append(c.packagedTools, spec) |
| 886 | return c |
| 887 | } |
| 888 | |
| 889 | // ImplicitPackagedTools copies the specified tools into the sandbox without modifying the command |
| 890 | // line. It can only be used with tool sandboxing enabled by SandboxTools(). |
| 891 | func (c *RuleBuilderCommand) ImplicitPackagedTools(specs []PackagingSpec) *RuleBuilderCommand { |
| 892 | if !c.rule.sboxTools { |
| 893 | panic("ImplicitPackagedTools() requires SandboxTools()") |
| 894 | } |
| 895 | |
| 896 | c.packagedTools = append(c.packagedTools, specs...) |
| 897 | return c |
| 898 | } |
| 899 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 900 | // Text adds the specified raw text to the command line. The text should not contain input or output paths or the |
| 901 | // rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 902 | func (c *RuleBuilderCommand) Text(text string) *RuleBuilderCommand { |
Colin Cross | cfec40c | 2019-07-08 17:07:18 -0700 | [diff] [blame] | 903 | if c.buf.Len() > 0 { |
| 904 | c.buf.WriteByte(' ') |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 905 | } |
Colin Cross | cfec40c | 2019-07-08 17:07:18 -0700 | [diff] [blame] | 906 | c.buf.WriteString(text) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 907 | return c |
| 908 | } |
| 909 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 910 | // Textf adds the specified formatted text to the command line. The text should not contain input or output paths or |
| 911 | // the rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 912 | func (c *RuleBuilderCommand) Textf(format string, a ...interface{}) *RuleBuilderCommand { |
| 913 | return c.Text(fmt.Sprintf(format, a...)) |
| 914 | } |
| 915 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 916 | // Flag adds the specified raw text to the command line. The text should not contain input or output paths or the |
| 917 | // rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 918 | func (c *RuleBuilderCommand) Flag(flag string) *RuleBuilderCommand { |
| 919 | return c.Text(flag) |
| 920 | } |
| 921 | |
Colin Cross | ab05443 | 2019-07-15 16:13:59 -0700 | [diff] [blame] | 922 | // OptionalFlag adds the specified raw text to the command line if it is not nil. The text should not contain input or |
| 923 | // output paths or the rule will not have them listed in its dependencies or outputs. |
| 924 | func (c *RuleBuilderCommand) OptionalFlag(flag *string) *RuleBuilderCommand { |
| 925 | if flag != nil { |
| 926 | c.Text(*flag) |
| 927 | } |
| 928 | |
| 929 | return c |
| 930 | } |
| 931 | |
Colin Cross | 92b7d58 | 2019-03-29 15:32:51 -0700 | [diff] [blame] | 932 | // Flags adds the specified raw text to the command line. The text should not contain input or output paths or the |
| 933 | // rule will not have them listed in its dependencies or outputs. |
| 934 | func (c *RuleBuilderCommand) Flags(flags []string) *RuleBuilderCommand { |
| 935 | for _, flag := range flags { |
| 936 | c.Text(flag) |
| 937 | } |
| 938 | return c |
| 939 | } |
| 940 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 941 | // FlagWithArg adds the specified flag and argument text to the command line, with no separator between them. The flag |
| 942 | // and argument should not contain input or output paths or the rule will not have them listed in its dependencies or |
| 943 | // outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 944 | func (c *RuleBuilderCommand) FlagWithArg(flag, arg string) *RuleBuilderCommand { |
| 945 | return c.Text(flag + arg) |
| 946 | } |
| 947 | |
Colin Cross | c7ed004 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 948 | // FlagForEachArg adds the specified flag joined with each argument to the command line. The result is identical to |
| 949 | // calling FlagWithArg for argument. |
| 950 | func (c *RuleBuilderCommand) FlagForEachArg(flag string, args []string) *RuleBuilderCommand { |
| 951 | for _, arg := range args { |
| 952 | c.FlagWithArg(flag, arg) |
| 953 | } |
| 954 | return c |
| 955 | } |
| 956 | |
Roland Levillain | 2da5d9a | 2019-02-27 16:56:41 +0000 | [diff] [blame] | 957 | // 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] | 958 | // and no separator between the flag and arguments. The flag and arguments should not contain input or output paths or |
| 959 | // the rule will not have them listed in its dependencies or outputs. |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 960 | func (c *RuleBuilderCommand) FlagWithList(flag string, list []string, sep string) *RuleBuilderCommand { |
| 961 | return c.Text(flag + strings.Join(list, sep)) |
| 962 | } |
| 963 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 964 | // Tool adds the specified tool path to the command line. The path will be also added to the dependencies returned by |
| 965 | // RuleBuilder.Tools. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 966 | func (c *RuleBuilderCommand) Tool(path Path) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 967 | c.tools = append(c.tools, path) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 968 | return c.Text(c.PathForTool(path)) |
| 969 | } |
| 970 | |
| 971 | // Tool adds the specified tool path to the dependencies returned by RuleBuilder.Tools. |
| 972 | func (c *RuleBuilderCommand) ImplicitTool(path Path) *RuleBuilderCommand { |
| 973 | c.tools = append(c.tools, path) |
| 974 | return c |
| 975 | } |
| 976 | |
| 977 | // Tool adds the specified tool path to the dependencies returned by RuleBuilder.Tools. |
| 978 | func (c *RuleBuilderCommand) ImplicitTools(paths Paths) *RuleBuilderCommand { |
| 979 | c.tools = append(c.tools, paths...) |
| 980 | return c |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 981 | } |
| 982 | |
Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 983 | // BuiltTool adds the specified tool path that was built using a host Soong module to the command line. The path will |
| 984 | // be also added to the dependencies returned by RuleBuilder.Tools. |
| 985 | // |
| 986 | // It is equivalent to: |
| 987 | // cmd.Tool(ctx.Config().HostToolPath(ctx, tool)) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 988 | func (c *RuleBuilderCommand) BuiltTool(tool string) *RuleBuilderCommand { |
| 989 | return c.Tool(c.rule.ctx.Config().HostToolPath(c.rule.ctx, tool)) |
Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | // PrebuiltBuildTool adds the specified tool path from prebuils/build-tools. The path will be also added to the |
| 993 | // dependencies returned by RuleBuilder.Tools. |
| 994 | // |
| 995 | // It is equivalent to: |
| 996 | // cmd.Tool(ctx.Config().PrebuiltBuildTool(ctx, tool)) |
| 997 | func (c *RuleBuilderCommand) PrebuiltBuildTool(ctx PathContext, tool string) *RuleBuilderCommand { |
| 998 | return c.Tool(ctx.Config().PrebuiltBuildTool(ctx, tool)) |
| 999 | } |
| 1000 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1001 | // Input adds the specified input path to the command line. The path will also be added to the dependencies returned by |
| 1002 | // RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1003 | func (c *RuleBuilderCommand) Input(path Path) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1004 | return c.Text(c.addInput(path)) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1007 | // Inputs adds the specified input paths to the command line, separated by spaces. The paths will also be added to the |
| 1008 | // dependencies returned by RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1009 | func (c *RuleBuilderCommand) Inputs(paths Paths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1010 | for _, path := range paths { |
| 1011 | c.Input(path) |
| 1012 | } |
| 1013 | return c |
| 1014 | } |
| 1015 | |
| 1016 | // Implicit adds the specified input path to the dependencies returned by RuleBuilder.Inputs without modifying the |
| 1017 | // command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1018 | func (c *RuleBuilderCommand) Implicit(path Path) *RuleBuilderCommand { |
Ramy Medhat | 2f99eec | 2020-06-13 17:38:27 -0400 | [diff] [blame] | 1019 | c.addImplicit(path) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1020 | return c |
| 1021 | } |
| 1022 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1023 | // Implicits adds the specified input paths to the dependencies returned by RuleBuilder.Inputs without modifying the |
| 1024 | // command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1025 | func (c *RuleBuilderCommand) Implicits(paths Paths) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1026 | for _, path := range paths { |
Ramy Medhat | 2f99eec | 2020-06-13 17:38:27 -0400 | [diff] [blame] | 1027 | c.addImplicit(path) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1028 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1029 | return c |
| 1030 | } |
| 1031 | |
Ramy Medhat | 2f99eec | 2020-06-13 17:38:27 -0400 | [diff] [blame] | 1032 | // GetImplicits returns the command's implicit inputs. |
| 1033 | func (c *RuleBuilderCommand) GetImplicits() Paths { |
| 1034 | return c.implicits |
| 1035 | } |
| 1036 | |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 1037 | // OrderOnly adds the specified input path to the dependencies returned by RuleBuilder.OrderOnlys |
| 1038 | // without modifying the command line. |
| 1039 | func (c *RuleBuilderCommand) OrderOnly(path Path) *RuleBuilderCommand { |
| 1040 | c.addOrderOnly(path) |
| 1041 | return c |
| 1042 | } |
| 1043 | |
| 1044 | // OrderOnlys adds the specified input paths to the dependencies returned by RuleBuilder.OrderOnlys |
| 1045 | // without modifying the command line. |
| 1046 | func (c *RuleBuilderCommand) OrderOnlys(paths Paths) *RuleBuilderCommand { |
| 1047 | for _, path := range paths { |
| 1048 | c.addOrderOnly(path) |
| 1049 | } |
| 1050 | return c |
| 1051 | } |
| 1052 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1053 | // Output adds the specified output path to the command line. The path will also be added to the outputs returned by |
| 1054 | // RuleBuilder.Outputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1055 | func (c *RuleBuilderCommand) Output(path WritablePath) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1056 | c.outputs = append(c.outputs, path) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 1057 | return c.Text(c.PathForOutput(path)) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1058 | } |
| 1059 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1060 | // Outputs adds the specified output paths to the command line, separated by spaces. The paths will also be added to |
| 1061 | // the outputs returned by RuleBuilder.Outputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1062 | func (c *RuleBuilderCommand) Outputs(paths WritablePaths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1063 | for _, path := range paths { |
| 1064 | c.Output(path) |
| 1065 | } |
| 1066 | return c |
| 1067 | } |
| 1068 | |
Dan Willemsen | 1945a4b | 2019-06-04 17:10:41 -0700 | [diff] [blame] | 1069 | // OutputDir adds the output directory to the command line. This is only available when used with RuleBuilder.Sbox, |
| 1070 | // and will be the temporary output directory managed by sbox, not the final one. |
| 1071 | func (c *RuleBuilderCommand) OutputDir() *RuleBuilderCommand { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 1072 | if !c.rule.sbox { |
Dan Willemsen | 1945a4b | 2019-06-04 17:10:41 -0700 | [diff] [blame] | 1073 | panic("OutputDir only valid with Sbox") |
| 1074 | } |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 1075 | return c.Text(sboxOutDir) |
Dan Willemsen | 1945a4b | 2019-06-04 17:10:41 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 1078 | // DepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles and adds it to the command |
| 1079 | // line, and causes RuleBuilder.Build file to set the depfile flag for ninja. If multiple depfiles are added to |
| 1080 | // commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the depfiles together. |
| 1081 | func (c *RuleBuilderCommand) DepFile(path WritablePath) *RuleBuilderCommand { |
| 1082 | c.depFiles = append(c.depFiles, path) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 1083 | return c.Text(c.PathForOutput(path)) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1086 | // ImplicitOutput adds the specified output path to the dependencies returned by RuleBuilder.Outputs without modifying |
| 1087 | // the command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1088 | func (c *RuleBuilderCommand) ImplicitOutput(path WritablePath) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1089 | c.outputs = append(c.outputs, path) |
| 1090 | return c |
| 1091 | } |
| 1092 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1093 | // ImplicitOutputs adds the specified output paths to the dependencies returned by RuleBuilder.Outputs without modifying |
| 1094 | // the command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1095 | func (c *RuleBuilderCommand) ImplicitOutputs(paths WritablePaths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1096 | c.outputs = append(c.outputs, paths...) |
| 1097 | return c |
| 1098 | } |
| 1099 | |
Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame] | 1100 | // ImplicitSymlinkOutput declares the specified path as an implicit output that |
| 1101 | // will be a symlink instead of a regular file. Does not modify the command |
| 1102 | // line. |
| 1103 | func (c *RuleBuilderCommand) ImplicitSymlinkOutput(path WritablePath) *RuleBuilderCommand { |
| 1104 | c.symlinkOutputs = append(c.symlinkOutputs, path) |
| 1105 | return c.ImplicitOutput(path) |
| 1106 | } |
| 1107 | |
| 1108 | // ImplicitSymlinkOutputs declares the specified paths as implicit outputs that |
| 1109 | // will be a symlinks instead of regular files. Does not modify the command |
| 1110 | // line. |
| 1111 | func (c *RuleBuilderCommand) ImplicitSymlinkOutputs(paths WritablePaths) *RuleBuilderCommand { |
| 1112 | for _, path := range paths { |
| 1113 | c.ImplicitSymlinkOutput(path) |
| 1114 | } |
| 1115 | return c |
| 1116 | } |
| 1117 | |
| 1118 | // SymlinkOutput declares the specified path as an output that will be a symlink |
| 1119 | // instead of a regular file. Modifies the command line. |
| 1120 | func (c *RuleBuilderCommand) SymlinkOutput(path WritablePath) *RuleBuilderCommand { |
| 1121 | c.symlinkOutputs = append(c.symlinkOutputs, path) |
| 1122 | return c.Output(path) |
| 1123 | } |
| 1124 | |
| 1125 | // SymlinkOutputsl declares the specified paths as outputs that will be symlinks |
| 1126 | // instead of regular files. Modifies the command line. |
| 1127 | func (c *RuleBuilderCommand) SymlinkOutputs(paths WritablePaths) *RuleBuilderCommand { |
| 1128 | for _, path := range paths { |
| 1129 | c.SymlinkOutput(path) |
| 1130 | } |
| 1131 | return c |
| 1132 | } |
| 1133 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 1134 | // ImplicitDepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles without modifying |
| 1135 | // the command line, and causes RuleBuilder.Build file to set the depfile flag for ninja. If multiple depfiles |
| 1136 | // are added to commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the |
| 1137 | // depfiles together. |
| 1138 | func (c *RuleBuilderCommand) ImplicitDepFile(path WritablePath) *RuleBuilderCommand { |
| 1139 | c.depFiles = append(c.depFiles, path) |
| 1140 | return c |
| 1141 | } |
| 1142 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1143 | // FlagWithInput adds the specified flag and input path to the command line, with no separator between them. The path |
| 1144 | // will also be added to the dependencies returned by RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1145 | func (c *RuleBuilderCommand) FlagWithInput(flag string, path Path) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1146 | return c.Text(flag + c.addInput(path)) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1147 | } |
| 1148 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1149 | // FlagWithInputList adds the specified flag and input paths to the command line, with the inputs joined by sep |
| 1150 | // and no separator between the flag and inputs. The input paths will also be added to the dependencies returned by |
| 1151 | // RuleBuilder.Inputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1152 | func (c *RuleBuilderCommand) FlagWithInputList(flag string, paths Paths, sep string) *RuleBuilderCommand { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1153 | strs := make([]string, len(paths)) |
| 1154 | for i, path := range paths { |
| 1155 | strs[i] = c.addInput(path) |
| 1156 | } |
| 1157 | return c.FlagWithList(flag, strs, sep) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1158 | } |
| 1159 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1160 | // FlagForEachInput adds the specified flag joined with each input path to the command line. The input paths will also |
| 1161 | // be added to the dependencies returned by RuleBuilder.Inputs. The result is identical to calling FlagWithInput for |
| 1162 | // each input path. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1163 | func (c *RuleBuilderCommand) FlagForEachInput(flag string, paths Paths) *RuleBuilderCommand { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1164 | for _, path := range paths { |
| 1165 | c.FlagWithInput(flag, path) |
| 1166 | } |
| 1167 | return c |
| 1168 | } |
| 1169 | |
| 1170 | // FlagWithOutput adds the specified flag and output path to the command line, with no separator between them. The path |
| 1171 | // will also be added to the outputs returned by RuleBuilder.Outputs. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 1172 | func (c *RuleBuilderCommand) FlagWithOutput(flag string, path WritablePath) *RuleBuilderCommand { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1173 | c.outputs = append(c.outputs, path) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 1174 | return c.Text(flag + c.PathForOutput(path)) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 1177 | // FlagWithDepFile adds the specified flag and depfile path to the command line, with no separator between them. The path |
| 1178 | // will also be added to the outputs returned by RuleBuilder.Outputs. |
| 1179 | func (c *RuleBuilderCommand) FlagWithDepFile(flag string, path WritablePath) *RuleBuilderCommand { |
| 1180 | c.depFiles = append(c.depFiles, path) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 1181 | return c.Text(flag + c.PathForOutput(path)) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame^] | 1184 | // FlagWithRspFileInputList adds the specified flag and path to an rspfile to the command line, with |
| 1185 | // no separator between them. The paths will be written to the rspfile. If sbox is enabled, the |
| 1186 | // rspfile must be outside the sbox directory. The first use of FlagWithRspFileInputList in any |
| 1187 | // RuleBuilderCommand of a RuleBuilder will use Ninja's rsp file support for the rule, additional |
| 1188 | // uses will result in an auxiliary rules to write the rspFile contents. |
Colin Cross | 70c4741 | 2021-03-12 17:48:14 -0800 | [diff] [blame] | 1189 | func (c *RuleBuilderCommand) FlagWithRspFileInputList(flag string, rspFile WritablePath, paths Paths) *RuleBuilderCommand { |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 1190 | // Use an empty slice if paths is nil, the non-nil slice is used as an indicator that the rsp file must be |
| 1191 | // generated. |
| 1192 | if paths == nil { |
| 1193 | paths = Paths{} |
| 1194 | } |
| 1195 | |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame^] | 1196 | c.rspFiles = append(c.rspFiles, rspFileAndPaths{rspFile, paths}) |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 1197 | |
Colin Cross | 70c4741 | 2021-03-12 17:48:14 -0800 | [diff] [blame] | 1198 | if c.rule.sbox { |
| 1199 | if _, isRel, _ := maybeRelErr(c.rule.outDir.String(), rspFile.String()); isRel { |
| 1200 | panic(fmt.Errorf("FlagWithRspFileInputList rspfile %q must not be inside out dir %q", |
| 1201 | rspFile.String(), c.rule.outDir.String())) |
| 1202 | } |
| 1203 | } |
| 1204 | |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 1205 | c.FlagWithArg(flag, c.PathForInput(rspFile)) |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 1206 | return c |
| 1207 | } |
| 1208 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1209 | // String returns the command line. |
| 1210 | func (c *RuleBuilderCommand) String() string { |
Colin Cross | cfec40c | 2019-07-08 17:07:18 -0700 | [diff] [blame] | 1211 | return c.buf.String() |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 1212 | } |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 1213 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 1214 | // RuleBuilderSboxProtoForTests takes the BuildParams for the manifest passed to RuleBuilder.Sbox() |
| 1215 | // and returns sbox testproto generated by the RuleBuilder. |
| 1216 | func RuleBuilderSboxProtoForTests(t *testing.T, params TestingBuildParams) *sbox_proto.Manifest { |
| 1217 | t.Helper() |
| 1218 | content := ContentFromFileRuleForTests(t, params) |
| 1219 | manifest := sbox_proto.Manifest{} |
| 1220 | err := proto.UnmarshalText(content, &manifest) |
| 1221 | if err != nil { |
| 1222 | t.Fatalf("failed to unmarshal manifest: %s", err.Error()) |
| 1223 | } |
| 1224 | return &manifest |
| 1225 | } |
| 1226 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 1227 | func ninjaNameEscape(s string) string { |
| 1228 | b := []byte(s) |
| 1229 | escaped := false |
| 1230 | for i, c := range b { |
| 1231 | valid := (c >= 'a' && c <= 'z') || |
| 1232 | (c >= 'A' && c <= 'Z') || |
| 1233 | (c >= '0' && c <= '9') || |
| 1234 | (c == '_') || |
| 1235 | (c == '-') || |
| 1236 | (c == '.') |
| 1237 | if !valid { |
| 1238 | b[i] = '_' |
| 1239 | escaped = true |
| 1240 | } |
| 1241 | } |
| 1242 | if escaped { |
| 1243 | s = string(b) |
| 1244 | } |
| 1245 | return s |
| 1246 | } |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 1247 | |
| 1248 | // hashSrcFiles returns a hash of the list of source files. It is used to ensure the command line |
| 1249 | // or the sbox textproto manifest change even if the input files are not listed on the command line. |
| 1250 | func hashSrcFiles(srcFiles Paths) string { |
| 1251 | h := sha256.New() |
| 1252 | srcFileList := strings.Join(srcFiles.Strings(), "\n") |
| 1253 | h.Write([]byte(srcFileList)) |
| 1254 | return fmt.Sprintf("%x", h.Sum(nil)) |
| 1255 | } |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 1256 | |
| 1257 | // BuilderContextForTesting returns a BuilderContext for the given config that can be used for tests |
| 1258 | // that need to call methods that take a BuilderContext. |
| 1259 | func BuilderContextForTesting(config Config) BuilderContext { |
| 1260 | pathCtx := PathContextForTesting(config) |
| 1261 | return builderContextForTests{ |
| 1262 | PathContext: pathCtx, |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | type builderContextForTests struct { |
| 1267 | PathContext |
| 1268 | } |
| 1269 | |
| 1270 | func (builderContextForTests) Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule { |
| 1271 | return nil |
| 1272 | } |
| 1273 | func (builderContextForTests) Build(PackageContext, BuildParams) {} |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 1274 | |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 1275 | func writeRspFileRule(ctx BuilderContext, rspFile WritablePath, paths Paths) { |
| 1276 | buf := &strings.Builder{} |
| 1277 | err := response.WriteRspFile(buf, paths.Strings()) |
| 1278 | if err != nil { |
| 1279 | // There should never be I/O errors writing to a bytes.Buffer. |
| 1280 | panic(err) |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 1281 | } |
Colin Cross | e55bd42 | 2021-03-23 13:44:30 -0700 | [diff] [blame] | 1282 | WriteFileRule(ctx, rspFile, buf.String()) |
Colin Cross | ef97274 | 2021-03-12 17:24:45 -0800 | [diff] [blame] | 1283 | } |