Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 1 | // Copyright 2019 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 | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 18 | "fmt" |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 19 | "io/ioutil" |
| 20 | "os" |
| 21 | "path/filepath" |
| 22 | "reflect" |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 23 | "strings" |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 24 | "testing" |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 25 | |
| 26 | "github.com/google/blueprint" |
| 27 | |
| 28 | "android/soong/shared" |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 29 | ) |
| 30 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 31 | func pathContext() PathContext { |
| 32 | return PathContextForTesting(TestConfig("out", nil), |
| 33 | map[string][]byte{ |
| 34 | "ld": nil, |
| 35 | "a.o": nil, |
| 36 | "b.o": nil, |
| 37 | "cp": nil, |
| 38 | "a": nil, |
| 39 | "b": nil, |
| 40 | "ls": nil, |
| 41 | "turbine": nil, |
| 42 | "java": nil, |
| 43 | }) |
| 44 | } |
| 45 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 46 | func ExampleRuleBuilder() { |
| 47 | rule := NewRuleBuilder() |
| 48 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 49 | ctx := pathContext() |
| 50 | |
| 51 | rule.Command(). |
| 52 | Tool(PathForSource(ctx, "ld")). |
| 53 | Inputs(PathsForTesting("a.o", "b.o")). |
| 54 | FlagWithOutput("-o ", PathForOutput(ctx, "linked")) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 55 | rule.Command().Text("echo success") |
| 56 | |
| 57 | // To add the command to the build graph: |
| 58 | // rule.Build(pctx, ctx, "link", "link") |
| 59 | |
| 60 | fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && ")) |
| 61 | fmt.Printf("tools: %q\n", rule.Tools()) |
| 62 | fmt.Printf("inputs: %q\n", rule.Inputs()) |
| 63 | fmt.Printf("outputs: %q\n", rule.Outputs()) |
| 64 | |
| 65 | // Output: |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 66 | // commands: "ld a.o b.o -o out/linked && echo success" |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 67 | // tools: ["ld"] |
| 68 | // inputs: ["a.o" "b.o"] |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 69 | // outputs: ["out/linked"] |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 72 | func ExampleRuleBuilder_Temporary() { |
| 73 | rule := NewRuleBuilder() |
| 74 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 75 | ctx := pathContext() |
| 76 | |
| 77 | rule.Command(). |
| 78 | Tool(PathForSource(ctx, "cp")). |
| 79 | Input(PathForSource(ctx, "a")). |
| 80 | Output(PathForOutput(ctx, "b")) |
| 81 | rule.Command(). |
| 82 | Tool(PathForSource(ctx, "cp")). |
| 83 | Input(PathForOutput(ctx, "b")). |
| 84 | Output(PathForOutput(ctx, "c")) |
| 85 | rule.Temporary(PathForOutput(ctx, "b")) |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 86 | |
| 87 | fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && ")) |
| 88 | fmt.Printf("tools: %q\n", rule.Tools()) |
| 89 | fmt.Printf("inputs: %q\n", rule.Inputs()) |
| 90 | fmt.Printf("outputs: %q\n", rule.Outputs()) |
| 91 | |
| 92 | // Output: |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 93 | // commands: "cp a out/b && cp out/b out/c" |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 94 | // tools: ["cp"] |
| 95 | // inputs: ["a"] |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 96 | // outputs: ["out/c"] |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | func ExampleRuleBuilder_DeleteTemporaryFiles() { |
| 100 | rule := NewRuleBuilder() |
| 101 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 102 | ctx := pathContext() |
| 103 | |
| 104 | rule.Command(). |
| 105 | Tool(PathForSource(ctx, "cp")). |
| 106 | Input(PathForSource(ctx, "a")). |
| 107 | Output(PathForOutput(ctx, "b")) |
| 108 | rule.Command(). |
| 109 | Tool(PathForSource(ctx, "cp")). |
| 110 | Input(PathForOutput(ctx, "b")). |
| 111 | Output(PathForOutput(ctx, "c")) |
| 112 | rule.Temporary(PathForOutput(ctx, "b")) |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 113 | rule.DeleteTemporaryFiles() |
| 114 | |
| 115 | fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && ")) |
| 116 | fmt.Printf("tools: %q\n", rule.Tools()) |
| 117 | fmt.Printf("inputs: %q\n", rule.Inputs()) |
| 118 | fmt.Printf("outputs: %q\n", rule.Outputs()) |
| 119 | |
| 120 | // Output: |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 121 | // commands: "cp a out/b && cp out/b out/c && rm -f out/b" |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 122 | // tools: ["cp"] |
| 123 | // inputs: ["a"] |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 124 | // outputs: ["out/c"] |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 127 | func ExampleRuleBuilder_Installs() { |
| 128 | rule := NewRuleBuilder() |
| 129 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 130 | ctx := pathContext() |
| 131 | |
| 132 | out := PathForOutput(ctx, "linked") |
| 133 | |
| 134 | rule.Command(). |
| 135 | Tool(PathForSource(ctx, "ld")). |
| 136 | Inputs(PathsForTesting("a.o", "b.o")). |
| 137 | FlagWithOutput("-o ", out) |
| 138 | rule.Install(out, "/bin/linked") |
| 139 | rule.Install(out, "/sbin/linked") |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 140 | |
| 141 | fmt.Printf("rule.Installs().String() = %q\n", rule.Installs().String()) |
| 142 | |
| 143 | // Output: |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 144 | // rule.Installs().String() = "out/linked:/bin/linked out/linked:/sbin/linked" |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 147 | func ExampleRuleBuilderCommand() { |
| 148 | rule := NewRuleBuilder() |
| 149 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 150 | ctx := pathContext() |
| 151 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 152 | // chained |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 153 | rule.Command(). |
| 154 | Tool(PathForSource(ctx, "ld")). |
| 155 | Inputs(PathsForTesting("a.o", "b.o")). |
| 156 | FlagWithOutput("-o ", PathForOutput(ctx, "linked")) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 157 | |
| 158 | // unchained |
| 159 | cmd := rule.Command() |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 160 | cmd.Tool(PathForSource(ctx, "ld")) |
| 161 | cmd.Inputs(PathsForTesting("a.o", "b.o")) |
| 162 | cmd.FlagWithOutput("-o ", PathForOutput(ctx, "linked")) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 163 | |
| 164 | // mixed: |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 165 | cmd = rule.Command().Tool(PathForSource(ctx, "ld")) |
| 166 | cmd.Inputs(PathsForTesting("a.o", "b.o")) |
| 167 | cmd.FlagWithOutput("-o ", PathForOutput(ctx, "linked")) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | func ExampleRuleBuilderCommand_Flag() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 171 | ctx := pathContext() |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 172 | fmt.Println(NewRuleBuilder().Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 173 | Tool(PathForSource(ctx, "ls")).Flag("-l")) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 174 | // Output: |
| 175 | // ls -l |
| 176 | } |
| 177 | |
Colin Cross | 92b7d58 | 2019-03-29 15:32:51 -0700 | [diff] [blame] | 178 | func ExampleRuleBuilderCommand_Flags() { |
| 179 | ctx := pathContext() |
| 180 | fmt.Println(NewRuleBuilder().Command(). |
| 181 | Tool(PathForSource(ctx, "ls")).Flags([]string{"-l", "-a"})) |
| 182 | // Output: |
| 183 | // ls -l -a |
| 184 | } |
| 185 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 186 | func ExampleRuleBuilderCommand_FlagWithArg() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 187 | ctx := pathContext() |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 188 | fmt.Println(NewRuleBuilder().Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 189 | Tool(PathForSource(ctx, "ls")). |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 190 | FlagWithArg("--sort=", "time")) |
| 191 | // Output: |
| 192 | // ls --sort=time |
| 193 | } |
| 194 | |
Colin Cross | c7ed004 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 195 | func ExampleRuleBuilderCommand_FlagForEachArg() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 196 | ctx := pathContext() |
Colin Cross | c7ed004 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 197 | fmt.Println(NewRuleBuilder().Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 198 | Tool(PathForSource(ctx, "ls")). |
Colin Cross | c7ed004 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 199 | FlagForEachArg("--sort=", []string{"time", "size"})) |
| 200 | // Output: |
| 201 | // ls --sort=time --sort=size |
| 202 | } |
| 203 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 204 | func ExampleRuleBuilderCommand_FlagForEachInput() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 205 | ctx := pathContext() |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 206 | fmt.Println(NewRuleBuilder().Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 207 | Tool(PathForSource(ctx, "turbine")). |
| 208 | FlagForEachInput("--classpath ", PathsForTesting("a.jar", "b.jar"))) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 209 | // Output: |
| 210 | // turbine --classpath a.jar --classpath b.jar |
| 211 | } |
| 212 | |
| 213 | func ExampleRuleBuilderCommand_FlagWithInputList() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 214 | ctx := pathContext() |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 215 | fmt.Println(NewRuleBuilder().Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 216 | Tool(PathForSource(ctx, "java")). |
| 217 | FlagWithInputList("-classpath=", PathsForTesting("a.jar", "b.jar"), ":")) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 218 | // Output: |
| 219 | // java -classpath=a.jar:b.jar |
| 220 | } |
| 221 | |
| 222 | func ExampleRuleBuilderCommand_FlagWithInput() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 223 | ctx := pathContext() |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 224 | fmt.Println(NewRuleBuilder().Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 225 | Tool(PathForSource(ctx, "java")). |
| 226 | FlagWithInput("-classpath=", PathForSource(ctx, "a"))) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 227 | // Output: |
| 228 | // java -classpath=a |
| 229 | } |
| 230 | |
| 231 | func ExampleRuleBuilderCommand_FlagWithList() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 232 | ctx := pathContext() |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 233 | fmt.Println(NewRuleBuilder().Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 234 | Tool(PathForSource(ctx, "ls")). |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 235 | FlagWithList("--sort=", []string{"time", "size"}, ",")) |
| 236 | // Output: |
| 237 | // ls --sort=time,size |
| 238 | } |
| 239 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 240 | func TestRuleBuilder(t *testing.T) { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 241 | fs := map[string][]byte{ |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 242 | "dep_fixer": nil, |
| 243 | "input": nil, |
| 244 | "Implicit": nil, |
| 245 | "Input": nil, |
| 246 | "Tool": nil, |
| 247 | "input2": nil, |
| 248 | "tool2": nil, |
| 249 | "input3": nil, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | ctx := PathContextForTesting(TestConfig("out", nil), fs) |
| 253 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 254 | addCommands := func(rule *RuleBuilder) { |
| 255 | cmd := rule.Command(). |
| 256 | DepFile(PathForOutput(ctx, "DepFile")). |
| 257 | Flag("Flag"). |
| 258 | FlagWithArg("FlagWithArg=", "arg"). |
| 259 | FlagWithDepFile("FlagWithDepFile=", PathForOutput(ctx, "depfile")). |
| 260 | FlagWithInput("FlagWithInput=", PathForSource(ctx, "input")). |
| 261 | FlagWithOutput("FlagWithOutput=", PathForOutput(ctx, "output")). |
| 262 | Implicit(PathForSource(ctx, "Implicit")). |
| 263 | ImplicitDepFile(PathForOutput(ctx, "ImplicitDepFile")). |
| 264 | ImplicitOutput(PathForOutput(ctx, "ImplicitOutput")). |
| 265 | Input(PathForSource(ctx, "Input")). |
| 266 | Output(PathForOutput(ctx, "Output")). |
| 267 | Text("Text"). |
| 268 | Tool(PathForSource(ctx, "Tool")) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 269 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 270 | rule.Command(). |
| 271 | Text("command2"). |
| 272 | DepFile(PathForOutput(ctx, "depfile2")). |
| 273 | Input(PathForSource(ctx, "input2")). |
| 274 | Output(PathForOutput(ctx, "output2")). |
| 275 | Tool(PathForSource(ctx, "tool2")) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 276 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 277 | // Test updates to the first command after the second command has been started |
| 278 | cmd.Text("after command2") |
| 279 | // Test updating a command when the previous update did not replace the cmd variable |
| 280 | cmd.Text("old cmd") |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 281 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 282 | // Test a command that uses the output of a previous command as an input |
| 283 | rule.Command(). |
| 284 | Text("command3"). |
| 285 | Input(PathForSource(ctx, "input3")). |
| 286 | Input(PathForOutput(ctx, "output2")). |
| 287 | Output(PathForOutput(ctx, "output3")) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 288 | } |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 289 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 290 | wantInputs := PathsForSource(ctx, []string{"Implicit", "Input", "input", "input2", "input3"}) |
| 291 | wantOutputs := PathsForOutput(ctx, []string{"ImplicitOutput", "Output", "output", "output2", "output3"}) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 292 | wantDepFiles := PathsForOutput(ctx, []string{"DepFile", "depfile", "ImplicitDepFile", "depfile2"}) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 293 | wantTools := PathsForSource(ctx, []string{"Tool", "tool2"}) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 294 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 295 | t.Run("normal", func(t *testing.T) { |
| 296 | rule := NewRuleBuilder() |
| 297 | addCommands(rule) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 298 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 299 | wantCommands := []string{ |
| 300 | "out/DepFile Flag FlagWithArg=arg FlagWithDepFile=out/depfile FlagWithInput=input FlagWithOutput=out/output Input out/Output Text Tool after command2 old cmd", |
| 301 | "command2 out/depfile2 input2 out/output2 tool2", |
| 302 | "command3 input3 out/output2 out/output3", |
| 303 | } |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 304 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 305 | wantDepMergerCommand := "out/host/" + ctx.Config().PrebuiltOS() + "/bin/dep_fixer out/DepFile out/depfile out/ImplicitDepFile out/depfile2" |
| 306 | |
| 307 | if g, w := rule.Commands(), wantCommands; !reflect.DeepEqual(g, w) { |
| 308 | t.Errorf("\nwant rule.Commands() = %#v\n got %#v", w, g) |
| 309 | } |
| 310 | |
| 311 | if g, w := rule.Inputs(), wantInputs; !reflect.DeepEqual(w, g) { |
| 312 | t.Errorf("\nwant rule.Inputs() = %#v\n got %#v", w, g) |
| 313 | } |
| 314 | if g, w := rule.Outputs(), wantOutputs; !reflect.DeepEqual(w, g) { |
| 315 | t.Errorf("\nwant rule.Outputs() = %#v\n got %#v", w, g) |
| 316 | } |
| 317 | if g, w := rule.DepFiles(), wantDepFiles; !reflect.DeepEqual(w, g) { |
| 318 | t.Errorf("\nwant rule.DepFiles() = %#v\n got %#v", w, g) |
| 319 | } |
| 320 | if g, w := rule.Tools(), wantTools; !reflect.DeepEqual(w, g) { |
| 321 | t.Errorf("\nwant rule.Tools() = %#v\n got %#v", w, g) |
| 322 | } |
| 323 | |
| 324 | if g, w := rule.depFileMergerCmd(ctx, rule.DepFiles()).String(), wantDepMergerCommand; g != w { |
| 325 | t.Errorf("\nwant rule.depFileMergerCmd() = %#v\n got %#v", w, g) |
| 326 | } |
| 327 | }) |
| 328 | |
| 329 | t.Run("sbox", func(t *testing.T) { |
| 330 | rule := NewRuleBuilder().Sbox(PathForOutput(ctx)) |
| 331 | addCommands(rule) |
| 332 | |
| 333 | wantCommands := []string{ |
| 334 | "__SBOX_OUT_DIR__/DepFile Flag FlagWithArg=arg FlagWithDepFile=__SBOX_OUT_DIR__/depfile FlagWithInput=input FlagWithOutput=__SBOX_OUT_DIR__/output Input __SBOX_OUT_DIR__/Output Text Tool after command2 old cmd", |
| 335 | "command2 __SBOX_OUT_DIR__/depfile2 input2 __SBOX_OUT_DIR__/output2 tool2", |
| 336 | "command3 input3 __SBOX_OUT_DIR__/output2 __SBOX_OUT_DIR__/output3", |
| 337 | } |
| 338 | |
| 339 | wantDepMergerCommand := "out/host/" + ctx.Config().PrebuiltOS() + "/bin/dep_fixer __SBOX_OUT_DIR__/DepFile __SBOX_OUT_DIR__/depfile __SBOX_OUT_DIR__/ImplicitDepFile __SBOX_OUT_DIR__/depfile2" |
| 340 | |
| 341 | if g, w := rule.Commands(), wantCommands; !reflect.DeepEqual(g, w) { |
| 342 | t.Errorf("\nwant rule.Commands() = %#v\n got %#v", w, g) |
| 343 | } |
| 344 | |
| 345 | if g, w := rule.Inputs(), wantInputs; !reflect.DeepEqual(w, g) { |
| 346 | t.Errorf("\nwant rule.Inputs() = %#v\n got %#v", w, g) |
| 347 | } |
| 348 | if g, w := rule.Outputs(), wantOutputs; !reflect.DeepEqual(w, g) { |
| 349 | t.Errorf("\nwant rule.Outputs() = %#v\n got %#v", w, g) |
| 350 | } |
| 351 | if g, w := rule.DepFiles(), wantDepFiles; !reflect.DeepEqual(w, g) { |
| 352 | t.Errorf("\nwant rule.DepFiles() = %#v\n got %#v", w, g) |
| 353 | } |
| 354 | if g, w := rule.Tools(), wantTools; !reflect.DeepEqual(w, g) { |
| 355 | t.Errorf("\nwant rule.Tools() = %#v\n got %#v", w, g) |
| 356 | } |
| 357 | |
| 358 | if g, w := rule.depFileMergerCmd(ctx, rule.DepFiles()).String(), wantDepMergerCommand; g != w { |
| 359 | t.Errorf("\nwant rule.depFileMergerCmd() = %#v\n got %#v", w, g) |
| 360 | } |
| 361 | }) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | func testRuleBuilderFactory() Module { |
| 365 | module := &testRuleBuilderModule{} |
| 366 | module.AddProperties(&module.properties) |
| 367 | InitAndroidModule(module) |
| 368 | return module |
| 369 | } |
| 370 | |
| 371 | type testRuleBuilderModule struct { |
| 372 | ModuleBase |
| 373 | properties struct { |
| 374 | Src string |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 375 | |
| 376 | Restat bool |
| 377 | Sbox bool |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
| 381 | func (t *testRuleBuilderModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 382 | in := PathForSource(ctx, t.properties.Src) |
| 383 | out := PathForModuleOut(ctx, ctx.ModuleName()) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 384 | outDep := PathForModuleOut(ctx, ctx.ModuleName()+".d") |
| 385 | outDir := PathForModuleOut(ctx) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 386 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 387 | testRuleBuilder_Build(ctx, in, out, outDep, outDir, t.properties.Restat, t.properties.Sbox) |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | type testRuleBuilderSingleton struct{} |
| 391 | |
| 392 | func testRuleBuilderSingletonFactory() Singleton { |
| 393 | return &testRuleBuilderSingleton{} |
| 394 | } |
| 395 | |
| 396 | func (t *testRuleBuilderSingleton) GenerateBuildActions(ctx SingletonContext) { |
| 397 | in := PathForSource(ctx, "bar") |
| 398 | out := PathForOutput(ctx, "baz") |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 399 | outDep := PathForOutput(ctx, "baz.d") |
| 400 | outDir := PathForOutput(ctx) |
| 401 | testRuleBuilder_Build(ctx, in, out, outDep, outDir, true, false) |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 404 | func testRuleBuilder_Build(ctx BuilderContext, in Path, out, outDep, outDir WritablePath, restat, sbox bool) { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 405 | rule := NewRuleBuilder() |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 406 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 407 | if sbox { |
| 408 | rule.Sbox(outDir) |
| 409 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 410 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 411 | rule.Command().Tool(PathForSource(ctx, "cp")).Input(in).Output(out).ImplicitDepFile(outDep) |
| 412 | |
| 413 | if restat { |
| 414 | rule.Restat() |
| 415 | } |
Colin Cross | baa676f | 2019-02-25 14:56:01 -0800 | [diff] [blame] | 416 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 417 | rule.Build(pctx, ctx, "rule", "desc") |
| 418 | } |
| 419 | |
| 420 | func TestRuleBuilder_Build(t *testing.T) { |
| 421 | buildDir, err := ioutil.TempDir("", "soong_test_rule_builder") |
| 422 | if err != nil { |
| 423 | t.Fatal(err) |
| 424 | } |
| 425 | defer os.RemoveAll(buildDir) |
| 426 | |
| 427 | bp := ` |
| 428 | rule_builder_test { |
| 429 | name: "foo", |
| 430 | src: "bar", |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 431 | restat: true, |
| 432 | } |
| 433 | rule_builder_test { |
| 434 | name: "foo_sbox", |
| 435 | src: "bar", |
| 436 | sbox: true, |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 437 | } |
| 438 | ` |
| 439 | |
| 440 | config := TestConfig(buildDir, nil) |
| 441 | ctx := NewTestContext() |
| 442 | ctx.MockFileSystem(map[string][]byte{ |
| 443 | "Android.bp": []byte(bp), |
| 444 | "bar": nil, |
| 445 | "cp": nil, |
| 446 | }) |
| 447 | ctx.RegisterModuleType("rule_builder_test", ModuleFactoryAdaptor(testRuleBuilderFactory)) |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 448 | ctx.RegisterSingletonType("rule_builder_test", SingletonFactoryAdaptor(testRuleBuilderSingletonFactory)) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 449 | ctx.Register() |
| 450 | |
| 451 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 452 | FailIfErrored(t, errs) |
| 453 | _, errs = ctx.PrepareBuildActions(config) |
| 454 | FailIfErrored(t, errs) |
| 455 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 456 | check := func(t *testing.T, params TestingBuildParams, wantCommand, wantOutput, wantDepfile string, wantRestat bool, extraCmdDeps []string) { |
| 457 | if params.RuleParams.Command != wantCommand { |
| 458 | t.Errorf("\nwant RuleParams.Command = %q\n got %q", wantCommand, params.RuleParams.Command) |
| 459 | } |
| 460 | |
| 461 | wantDeps := append([]string{"cp"}, extraCmdDeps...) |
| 462 | if !reflect.DeepEqual(params.RuleParams.CommandDeps, wantDeps) { |
| 463 | t.Errorf("\nwant RuleParams.CommandDeps = %q\n got %q", wantDeps, params.RuleParams.CommandDeps) |
| 464 | } |
| 465 | |
| 466 | if params.RuleParams.Restat != wantRestat { |
| 467 | t.Errorf("want RuleParams.Restat = %v, got %v", wantRestat, params.RuleParams.Restat) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 468 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 469 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 470 | if len(params.Implicits) != 1 || params.Implicits[0].String() != "bar" { |
| 471 | t.Errorf("want Implicits = [%q], got %q", "bar", params.Implicits.Strings()) |
| 472 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 473 | |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 474 | if params.Output.String() != wantOutput { |
| 475 | t.Errorf("want Output = %q, got %q", wantOutput, params.Output) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 476 | } |
Colin Cross | baa676f | 2019-02-25 14:56:01 -0800 | [diff] [blame] | 477 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 478 | if len(params.ImplicitOutputs) != 0 { |
| 479 | t.Errorf("want ImplicitOutputs = [], got %q", params.ImplicitOutputs.Strings()) |
| 480 | } |
| 481 | |
| 482 | if params.Depfile.String() != wantDepfile { |
| 483 | t.Errorf("want Depfile = %q, got %q", wantDepfile, params.Depfile) |
| 484 | } |
| 485 | |
| 486 | if params.Deps != blueprint.DepsGCC { |
| 487 | t.Errorf("want Deps = %q, got %q", blueprint.DepsGCC, params.Deps) |
Colin Cross | baa676f | 2019-02-25 14:56:01 -0800 | [diff] [blame] | 488 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 491 | t.Run("module", func(t *testing.T) { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 492 | outFile := filepath.Join(buildDir, ".intermediates", "foo", "foo") |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 493 | check(t, ctx.ModuleForTests("foo", "").Rule("rule"), |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 494 | "cp bar "+outFile, |
| 495 | outFile, outFile+".d", true, nil) |
| 496 | }) |
| 497 | t.Run("sbox", func(t *testing.T) { |
| 498 | outDir := filepath.Join(buildDir, ".intermediates", "foo_sbox") |
| 499 | outFile := filepath.Join(outDir, "foo_sbox") |
| 500 | sbox := filepath.Join(buildDir, "host", config.PrebuiltOS(), "bin/sbox") |
| 501 | sandboxPath := shared.TempDirForOutDir(buildDir) |
| 502 | |
| 503 | cmd := sbox + ` -c 'cp bar __SBOX_OUT_DIR__/foo_sbox' --sandbox-path ` + sandboxPath + " --output-root " + outDir + " __SBOX_OUT_DIR__/foo_sbox __SBOX_OUT_DIR__/foo_sbox.d" |
| 504 | |
| 505 | check(t, ctx.ModuleForTests("foo_sbox", "").Rule("rule"), |
| 506 | cmd, outFile, outFile+".d", false, []string{sbox}) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 507 | }) |
| 508 | t.Run("singleton", func(t *testing.T) { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 509 | outFile := filepath.Join(buildDir, "baz") |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 510 | check(t, ctx.SingletonForTests("rule_builder_test").Rule("rule"), |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame^] | 511 | "cp bar "+outFile, outFile, outFile+".d", true, nil) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 512 | }) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 513 | } |