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