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" |
| 25 | ) |
| 26 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 27 | func pathContext() PathContext { |
| 28 | return PathContextForTesting(TestConfig("out", nil), |
| 29 | map[string][]byte{ |
| 30 | "ld": nil, |
| 31 | "a.o": nil, |
| 32 | "b.o": nil, |
| 33 | "cp": nil, |
| 34 | "a": nil, |
| 35 | "b": nil, |
| 36 | "ls": nil, |
| 37 | "turbine": nil, |
| 38 | "java": nil, |
| 39 | }) |
| 40 | } |
| 41 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 42 | func ExampleRuleBuilder() { |
| 43 | rule := NewRuleBuilder() |
| 44 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 45 | ctx := pathContext() |
| 46 | |
| 47 | rule.Command(). |
| 48 | Tool(PathForSource(ctx, "ld")). |
| 49 | Inputs(PathsForTesting("a.o", "b.o")). |
| 50 | FlagWithOutput("-o ", PathForOutput(ctx, "linked")) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 51 | rule.Command().Text("echo success") |
| 52 | |
| 53 | // To add the command to the build graph: |
| 54 | // rule.Build(pctx, ctx, "link", "link") |
| 55 | |
| 56 | fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && ")) |
| 57 | fmt.Printf("tools: %q\n", rule.Tools()) |
| 58 | fmt.Printf("inputs: %q\n", rule.Inputs()) |
| 59 | fmt.Printf("outputs: %q\n", rule.Outputs()) |
| 60 | |
| 61 | // Output: |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 62 | // commands: "ld a.o b.o -o out/linked && echo success" |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 63 | // tools: ["ld"] |
| 64 | // inputs: ["a.o" "b.o"] |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 65 | // outputs: ["out/linked"] |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 68 | func ExampleRuleBuilder_Temporary() { |
| 69 | rule := NewRuleBuilder() |
| 70 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 71 | ctx := pathContext() |
| 72 | |
| 73 | rule.Command(). |
| 74 | Tool(PathForSource(ctx, "cp")). |
| 75 | Input(PathForSource(ctx, "a")). |
| 76 | Output(PathForOutput(ctx, "b")) |
| 77 | rule.Command(). |
| 78 | Tool(PathForSource(ctx, "cp")). |
| 79 | Input(PathForOutput(ctx, "b")). |
| 80 | Output(PathForOutput(ctx, "c")) |
| 81 | rule.Temporary(PathForOutput(ctx, "b")) |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 82 | |
| 83 | fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && ")) |
| 84 | fmt.Printf("tools: %q\n", rule.Tools()) |
| 85 | fmt.Printf("inputs: %q\n", rule.Inputs()) |
| 86 | fmt.Printf("outputs: %q\n", rule.Outputs()) |
| 87 | |
| 88 | // Output: |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 89 | // commands: "cp a out/b && cp out/b out/c" |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 90 | // tools: ["cp"] |
| 91 | // inputs: ["a"] |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 92 | // outputs: ["out/c"] |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | func ExampleRuleBuilder_DeleteTemporaryFiles() { |
| 96 | rule := NewRuleBuilder() |
| 97 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 98 | ctx := pathContext() |
| 99 | |
| 100 | rule.Command(). |
| 101 | Tool(PathForSource(ctx, "cp")). |
| 102 | Input(PathForSource(ctx, "a")). |
| 103 | Output(PathForOutput(ctx, "b")) |
| 104 | rule.Command(). |
| 105 | Tool(PathForSource(ctx, "cp")). |
| 106 | Input(PathForOutput(ctx, "b")). |
| 107 | Output(PathForOutput(ctx, "c")) |
| 108 | rule.Temporary(PathForOutput(ctx, "b")) |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 109 | rule.DeleteTemporaryFiles() |
| 110 | |
| 111 | fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && ")) |
| 112 | fmt.Printf("tools: %q\n", rule.Tools()) |
| 113 | fmt.Printf("inputs: %q\n", rule.Inputs()) |
| 114 | fmt.Printf("outputs: %q\n", rule.Outputs()) |
| 115 | |
| 116 | // Output: |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 117 | // 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] | 118 | // tools: ["cp"] |
| 119 | // inputs: ["a"] |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 120 | // outputs: ["out/c"] |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 123 | func ExampleRuleBuilder_Installs() { |
| 124 | rule := NewRuleBuilder() |
| 125 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 126 | ctx := pathContext() |
| 127 | |
| 128 | out := PathForOutput(ctx, "linked") |
| 129 | |
| 130 | rule.Command(). |
| 131 | Tool(PathForSource(ctx, "ld")). |
| 132 | Inputs(PathsForTesting("a.o", "b.o")). |
| 133 | FlagWithOutput("-o ", out) |
| 134 | rule.Install(out, "/bin/linked") |
| 135 | rule.Install(out, "/sbin/linked") |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 136 | |
| 137 | fmt.Printf("rule.Installs().String() = %q\n", rule.Installs().String()) |
| 138 | |
| 139 | // Output: |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 140 | // rule.Installs().String() = "out/linked:/bin/linked out/linked:/sbin/linked" |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 143 | func ExampleRuleBuilderCommand() { |
| 144 | rule := NewRuleBuilder() |
| 145 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 146 | ctx := pathContext() |
| 147 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 148 | // chained |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 149 | rule.Command(). |
| 150 | Tool(PathForSource(ctx, "ld")). |
| 151 | Inputs(PathsForTesting("a.o", "b.o")). |
| 152 | FlagWithOutput("-o ", PathForOutput(ctx, "linked")) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 153 | |
| 154 | // unchained |
| 155 | cmd := rule.Command() |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 156 | cmd.Tool(PathForSource(ctx, "ld")) |
| 157 | cmd.Inputs(PathsForTesting("a.o", "b.o")) |
| 158 | cmd.FlagWithOutput("-o ", PathForOutput(ctx, "linked")) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 159 | |
| 160 | // mixed: |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 161 | cmd = rule.Command().Tool(PathForSource(ctx, "ld")) |
| 162 | cmd.Inputs(PathsForTesting("a.o", "b.o")) |
| 163 | cmd.FlagWithOutput("-o ", PathForOutput(ctx, "linked")) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | func ExampleRuleBuilderCommand_Flag() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 167 | ctx := pathContext() |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 168 | fmt.Println(NewRuleBuilder().Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 169 | Tool(PathForSource(ctx, "ls")).Flag("-l")) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 170 | // Output: |
| 171 | // ls -l |
| 172 | } |
| 173 | |
| 174 | func ExampleRuleBuilderCommand_FlagWithArg() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 175 | ctx := pathContext() |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 176 | fmt.Println(NewRuleBuilder().Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 177 | Tool(PathForSource(ctx, "ls")). |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 178 | FlagWithArg("--sort=", "time")) |
| 179 | // Output: |
| 180 | // ls --sort=time |
| 181 | } |
| 182 | |
Colin Cross | c7ed004 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 183 | func ExampleRuleBuilderCommand_FlagForEachArg() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 184 | ctx := pathContext() |
Colin Cross | c7ed004 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 185 | fmt.Println(NewRuleBuilder().Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 186 | Tool(PathForSource(ctx, "ls")). |
Colin Cross | c7ed004 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 187 | FlagForEachArg("--sort=", []string{"time", "size"})) |
| 188 | // Output: |
| 189 | // ls --sort=time --sort=size |
| 190 | } |
| 191 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 192 | func ExampleRuleBuilderCommand_FlagForEachInput() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 193 | ctx := pathContext() |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 194 | fmt.Println(NewRuleBuilder().Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 195 | Tool(PathForSource(ctx, "turbine")). |
| 196 | FlagForEachInput("--classpath ", PathsForTesting("a.jar", "b.jar"))) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 197 | // Output: |
| 198 | // turbine --classpath a.jar --classpath b.jar |
| 199 | } |
| 200 | |
| 201 | func ExampleRuleBuilderCommand_FlagWithInputList() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 202 | ctx := pathContext() |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 203 | fmt.Println(NewRuleBuilder().Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 204 | Tool(PathForSource(ctx, "java")). |
| 205 | FlagWithInputList("-classpath=", PathsForTesting("a.jar", "b.jar"), ":")) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 206 | // Output: |
| 207 | // java -classpath=a.jar:b.jar |
| 208 | } |
| 209 | |
| 210 | func ExampleRuleBuilderCommand_FlagWithInput() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 211 | ctx := pathContext() |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 212 | fmt.Println(NewRuleBuilder().Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 213 | Tool(PathForSource(ctx, "java")). |
| 214 | FlagWithInput("-classpath=", PathForSource(ctx, "a"))) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 215 | // Output: |
| 216 | // java -classpath=a |
| 217 | } |
| 218 | |
| 219 | func ExampleRuleBuilderCommand_FlagWithList() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 220 | ctx := pathContext() |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 221 | fmt.Println(NewRuleBuilder().Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 222 | Tool(PathForSource(ctx, "ls")). |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 223 | FlagWithList("--sort=", []string{"time", "size"}, ",")) |
| 224 | // Output: |
| 225 | // ls --sort=time,size |
| 226 | } |
| 227 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 228 | func TestRuleBuilder(t *testing.T) { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 229 | rule := NewRuleBuilder() |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 230 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 231 | fs := map[string][]byte{ |
| 232 | "input": nil, |
| 233 | "Implicit": nil, |
| 234 | "Input": nil, |
| 235 | "Tool": nil, |
| 236 | "input2": nil, |
| 237 | "tool2": nil, |
| 238 | "input3": nil, |
| 239 | } |
| 240 | |
| 241 | ctx := PathContextForTesting(TestConfig("out", nil), fs) |
| 242 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 243 | cmd := rule.Command(). |
| 244 | Flag("Flag"). |
| 245 | FlagWithArg("FlagWithArg=", "arg"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 246 | FlagWithInput("FlagWithInput=", PathForSource(ctx, "input")). |
| 247 | FlagWithOutput("FlagWithOutput=", PathForOutput(ctx, "output")). |
| 248 | Implicit(PathForSource(ctx, "Implicit")). |
| 249 | ImplicitOutput(PathForOutput(ctx, "ImplicitOutput")). |
| 250 | Input(PathForSource(ctx, "Input")). |
| 251 | Output(PathForOutput(ctx, "Output")). |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 252 | Text("Text"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 253 | Tool(PathForSource(ctx, "Tool")) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 254 | |
| 255 | rule.Command(). |
| 256 | Text("command2"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 257 | Input(PathForSource(ctx, "input2")). |
| 258 | Output(PathForOutput(ctx, "output2")). |
| 259 | Tool(PathForSource(ctx, "tool2")) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 260 | |
| 261 | // Test updates to the first command after the second command has been started |
| 262 | cmd.Text("after command2") |
| 263 | // Test updating a command when the previous update did not replace the cmd variable |
| 264 | cmd.Text("old cmd") |
| 265 | |
| 266 | // Test a command that uses the output of a previous command as an input |
| 267 | rule.Command(). |
| 268 | Text("command3"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 269 | Input(PathForSource(ctx, "input3")). |
| 270 | Input(PathForOutput(ctx, "output2")). |
| 271 | Output(PathForOutput(ctx, "output3")) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 272 | |
| 273 | wantCommands := []string{ |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 274 | "Flag FlagWithArg=arg FlagWithInput=input FlagWithOutput=out/output Input out/Output Text Tool after command2 old cmd", |
| 275 | "command2 input2 out/output2 tool2", |
| 276 | "command3 input3 out/output2 out/output3", |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 277 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 278 | wantInputs := PathsForSource(ctx, []string{"Implicit", "Input", "input", "input2", "input3"}) |
| 279 | wantOutputs := PathsForOutput(ctx, []string{"ImplicitOutput", "Output", "output", "output2", "output3"}) |
| 280 | wantTools := PathsForSource(ctx, []string{"Tool", "tool2"}) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 281 | |
| 282 | if !reflect.DeepEqual(rule.Commands(), wantCommands) { |
| 283 | t.Errorf("\nwant rule.Commands() = %#v\n got %#v", wantCommands, rule.Commands()) |
| 284 | } |
| 285 | if !reflect.DeepEqual(rule.Inputs(), wantInputs) { |
| 286 | t.Errorf("\nwant rule.Inputs() = %#v\n got %#v", wantInputs, rule.Inputs()) |
| 287 | } |
| 288 | if !reflect.DeepEqual(rule.Outputs(), wantOutputs) { |
| 289 | t.Errorf("\nwant rule.Outputs() = %#v\n got %#v", wantOutputs, rule.Outputs()) |
| 290 | } |
| 291 | if !reflect.DeepEqual(rule.Tools(), wantTools) { |
| 292 | t.Errorf("\nwant rule.Tools() = %#v\n got %#v", wantTools, rule.Tools()) |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | func testRuleBuilderFactory() Module { |
| 297 | module := &testRuleBuilderModule{} |
| 298 | module.AddProperties(&module.properties) |
| 299 | InitAndroidModule(module) |
| 300 | return module |
| 301 | } |
| 302 | |
| 303 | type testRuleBuilderModule struct { |
| 304 | ModuleBase |
| 305 | properties struct { |
| 306 | Src string |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | func (t *testRuleBuilderModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 311 | in := PathForSource(ctx, t.properties.Src) |
| 312 | out := PathForModuleOut(ctx, ctx.ModuleName()) |
| 313 | |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 314 | testRuleBuilder_Build(ctx, in, out) |
| 315 | } |
| 316 | |
| 317 | type testRuleBuilderSingleton struct{} |
| 318 | |
| 319 | func testRuleBuilderSingletonFactory() Singleton { |
| 320 | return &testRuleBuilderSingleton{} |
| 321 | } |
| 322 | |
| 323 | func (t *testRuleBuilderSingleton) GenerateBuildActions(ctx SingletonContext) { |
| 324 | in := PathForSource(ctx, "bar") |
| 325 | out := PathForOutput(ctx, "baz") |
| 326 | testRuleBuilder_Build(ctx, in, out) |
| 327 | } |
| 328 | |
| 329 | func testRuleBuilder_Build(ctx BuilderContext, in Path, out WritablePath) { |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 330 | rule := NewRuleBuilder() |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 331 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 332 | rule.Command().Tool(PathForSource(ctx, "cp")).Input(in).Output(out) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 333 | |
Colin Cross | baa676f | 2019-02-25 14:56:01 -0800 | [diff] [blame] | 334 | rule.Restat() |
| 335 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 336 | rule.Build(pctx, ctx, "rule", "desc") |
| 337 | } |
| 338 | |
| 339 | func TestRuleBuilder_Build(t *testing.T) { |
| 340 | buildDir, err := ioutil.TempDir("", "soong_test_rule_builder") |
| 341 | if err != nil { |
| 342 | t.Fatal(err) |
| 343 | } |
| 344 | defer os.RemoveAll(buildDir) |
| 345 | |
| 346 | bp := ` |
| 347 | rule_builder_test { |
| 348 | name: "foo", |
| 349 | src: "bar", |
| 350 | } |
| 351 | ` |
| 352 | |
| 353 | config := TestConfig(buildDir, nil) |
| 354 | ctx := NewTestContext() |
| 355 | ctx.MockFileSystem(map[string][]byte{ |
| 356 | "Android.bp": []byte(bp), |
| 357 | "bar": nil, |
| 358 | "cp": nil, |
| 359 | }) |
| 360 | ctx.RegisterModuleType("rule_builder_test", ModuleFactoryAdaptor(testRuleBuilderFactory)) |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 361 | ctx.RegisterSingletonType("rule_builder_test", SingletonFactoryAdaptor(testRuleBuilderSingletonFactory)) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 362 | ctx.Register() |
| 363 | |
| 364 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 365 | FailIfErrored(t, errs) |
| 366 | _, errs = ctx.PrepareBuildActions(config) |
| 367 | FailIfErrored(t, errs) |
| 368 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 369 | check := func(t *testing.T, params TestingBuildParams, wantOutput string) { |
| 370 | if len(params.RuleParams.CommandDeps) != 1 || params.RuleParams.CommandDeps[0] != "cp" { |
| 371 | t.Errorf("want RuleParams.CommandDeps = [%q], got %q", "cp", params.RuleParams.CommandDeps) |
| 372 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 373 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 374 | if len(params.Implicits) != 1 || params.Implicits[0].String() != "bar" { |
| 375 | t.Errorf("want Implicits = [%q], got %q", "bar", params.Implicits.Strings()) |
| 376 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 377 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 378 | if len(params.Outputs) != 1 || params.Outputs[0].String() != wantOutput { |
| 379 | t.Errorf("want Outputs = [%q], got %q", wantOutput, params.Outputs.Strings()) |
| 380 | } |
Colin Cross | baa676f | 2019-02-25 14:56:01 -0800 | [diff] [blame] | 381 | |
| 382 | if !params.RuleParams.Restat { |
| 383 | t.Errorf("want RuleParams.Restat = true, got %v", params.RuleParams.Restat) |
| 384 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 387 | t.Run("module", func(t *testing.T) { |
| 388 | check(t, ctx.ModuleForTests("foo", "").Rule("rule"), |
| 389 | filepath.Join(buildDir, ".intermediates", "foo", "foo")) |
| 390 | }) |
| 391 | t.Run("singleton", func(t *testing.T) { |
| 392 | check(t, ctx.SingletonForTests("rule_builder_test").Rule("rule"), |
| 393 | filepath.Join(buildDir, "baz")) |
| 394 | }) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 395 | } |