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 | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame^] | 18 | "crypto/sha256" |
| 19 | "encoding/hex" |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 20 | "fmt" |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 21 | "path/filepath" |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 22 | "regexp" |
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 | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 31 | func builderContext() BuilderContext { |
| 32 | return BuilderContextForTesting(TestConfig("out", nil, "", map[string][]byte{ |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 33 | "ld": nil, |
| 34 | "a.o": nil, |
| 35 | "b.o": nil, |
| 36 | "cp": nil, |
| 37 | "a": nil, |
| 38 | "b": nil, |
| 39 | "ls": nil, |
Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame] | 40 | "ln": nil, |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 41 | "turbine": nil, |
| 42 | "java": nil, |
| 43 | "javac": nil, |
| 44 | })) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 47 | func ExampleRuleBuilder() { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 48 | ctx := builderContext() |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 49 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 50 | rule := NewRuleBuilder(pctx, ctx) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 51 | |
| 52 | rule.Command(). |
| 53 | Tool(PathForSource(ctx, "ld")). |
| 54 | Inputs(PathsForTesting("a.o", "b.o")). |
| 55 | FlagWithOutput("-o ", PathForOutput(ctx, "linked")) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 56 | rule.Command().Text("echo success") |
| 57 | |
| 58 | // To add the command to the build graph: |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 59 | // rule.Build("link", "link") |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 60 | |
| 61 | fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && ")) |
| 62 | fmt.Printf("tools: %q\n", rule.Tools()) |
| 63 | fmt.Printf("inputs: %q\n", rule.Inputs()) |
| 64 | fmt.Printf("outputs: %q\n", rule.Outputs()) |
| 65 | |
| 66 | // Output: |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 67 | // commands: "ld a.o b.o -o out/linked && echo success" |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 68 | // tools: ["ld"] |
| 69 | // inputs: ["a.o" "b.o"] |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 70 | // outputs: ["out/linked"] |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame] | 73 | func ExampleRuleBuilder_SymlinkOutputs() { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 74 | ctx := builderContext() |
Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame] | 75 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 76 | rule := NewRuleBuilder(pctx, ctx) |
Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame] | 77 | |
| 78 | rule.Command(). |
| 79 | Tool(PathForSource(ctx, "ln")). |
| 80 | FlagWithInput("-s ", PathForTesting("a.o")). |
| 81 | SymlinkOutput(PathForOutput(ctx, "a")) |
| 82 | rule.Command().Text("cp out/a out/b"). |
| 83 | ImplicitSymlinkOutput(PathForOutput(ctx, "b")) |
| 84 | |
| 85 | fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && ")) |
| 86 | fmt.Printf("tools: %q\n", rule.Tools()) |
| 87 | fmt.Printf("inputs: %q\n", rule.Inputs()) |
| 88 | fmt.Printf("outputs: %q\n", rule.Outputs()) |
| 89 | fmt.Printf("symlink_outputs: %q\n", rule.SymlinkOutputs()) |
| 90 | |
| 91 | // Output: |
| 92 | // commands: "ln -s a.o out/a && cp out/a out/b" |
| 93 | // tools: ["ln"] |
| 94 | // inputs: ["a.o"] |
| 95 | // outputs: ["out/a" "out/b"] |
| 96 | // symlink_outputs: ["out/a" "out/b"] |
| 97 | } |
| 98 | |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 99 | func ExampleRuleBuilder_Temporary() { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 100 | ctx := builderContext() |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 101 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 102 | rule := NewRuleBuilder(pctx, ctx) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 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 | |
| 114 | fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && ")) |
| 115 | fmt.Printf("tools: %q\n", rule.Tools()) |
| 116 | fmt.Printf("inputs: %q\n", rule.Inputs()) |
| 117 | fmt.Printf("outputs: %q\n", rule.Outputs()) |
| 118 | |
| 119 | // Output: |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 120 | // commands: "cp a out/b && cp out/b out/c" |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 121 | // tools: ["cp"] |
| 122 | // inputs: ["a"] |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 123 | // outputs: ["out/c"] |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | func ExampleRuleBuilder_DeleteTemporaryFiles() { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 127 | ctx := builderContext() |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 128 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 129 | rule := NewRuleBuilder(pctx, ctx) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 130 | |
| 131 | rule.Command(). |
| 132 | Tool(PathForSource(ctx, "cp")). |
| 133 | Input(PathForSource(ctx, "a")). |
| 134 | Output(PathForOutput(ctx, "b")) |
| 135 | rule.Command(). |
| 136 | Tool(PathForSource(ctx, "cp")). |
| 137 | Input(PathForOutput(ctx, "b")). |
| 138 | Output(PathForOutput(ctx, "c")) |
| 139 | rule.Temporary(PathForOutput(ctx, "b")) |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 140 | rule.DeleteTemporaryFiles() |
| 141 | |
| 142 | fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && ")) |
| 143 | fmt.Printf("tools: %q\n", rule.Tools()) |
| 144 | fmt.Printf("inputs: %q\n", rule.Inputs()) |
| 145 | fmt.Printf("outputs: %q\n", rule.Outputs()) |
| 146 | |
| 147 | // Output: |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 148 | // 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] | 149 | // tools: ["cp"] |
| 150 | // inputs: ["a"] |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 151 | // outputs: ["out/c"] |
Colin Cross | 5cb5b09 | 2019-02-02 21:25:18 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 154 | func ExampleRuleBuilder_Installs() { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 155 | ctx := builderContext() |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 156 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 157 | rule := NewRuleBuilder(pctx, ctx) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 158 | |
| 159 | out := PathForOutput(ctx, "linked") |
| 160 | |
| 161 | rule.Command(). |
| 162 | Tool(PathForSource(ctx, "ld")). |
| 163 | Inputs(PathsForTesting("a.o", "b.o")). |
| 164 | FlagWithOutput("-o ", out) |
| 165 | rule.Install(out, "/bin/linked") |
| 166 | rule.Install(out, "/sbin/linked") |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 167 | |
| 168 | fmt.Printf("rule.Installs().String() = %q\n", rule.Installs().String()) |
| 169 | |
| 170 | // Output: |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 171 | // rule.Installs().String() = "out/linked:/bin/linked out/linked:/sbin/linked" |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 174 | func ExampleRuleBuilderCommand() { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 175 | ctx := builderContext() |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 176 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 177 | rule := NewRuleBuilder(pctx, ctx) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 178 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 179 | // chained |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 180 | rule.Command(). |
| 181 | Tool(PathForSource(ctx, "ld")). |
| 182 | Inputs(PathsForTesting("a.o", "b.o")). |
| 183 | FlagWithOutput("-o ", PathForOutput(ctx, "linked")) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 184 | |
| 185 | // unchained |
| 186 | cmd := rule.Command() |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 187 | cmd.Tool(PathForSource(ctx, "ld")) |
| 188 | cmd.Inputs(PathsForTesting("a.o", "b.o")) |
| 189 | cmd.FlagWithOutput("-o ", PathForOutput(ctx, "linked")) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 190 | |
| 191 | // mixed: |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 192 | cmd = rule.Command().Tool(PathForSource(ctx, "ld")) |
| 193 | cmd.Inputs(PathsForTesting("a.o", "b.o")) |
| 194 | cmd.FlagWithOutput("-o ", PathForOutput(ctx, "linked")) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | func ExampleRuleBuilderCommand_Flag() { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 198 | ctx := builderContext() |
| 199 | fmt.Println(NewRuleBuilder(pctx, ctx).Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 200 | Tool(PathForSource(ctx, "ls")).Flag("-l")) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 201 | // Output: |
| 202 | // ls -l |
| 203 | } |
| 204 | |
Colin Cross | 92b7d58 | 2019-03-29 15:32:51 -0700 | [diff] [blame] | 205 | func ExampleRuleBuilderCommand_Flags() { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 206 | ctx := builderContext() |
| 207 | fmt.Println(NewRuleBuilder(pctx, ctx).Command(). |
Colin Cross | 92b7d58 | 2019-03-29 15:32:51 -0700 | [diff] [blame] | 208 | Tool(PathForSource(ctx, "ls")).Flags([]string{"-l", "-a"})) |
| 209 | // Output: |
| 210 | // ls -l -a |
| 211 | } |
| 212 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 213 | func ExampleRuleBuilderCommand_FlagWithArg() { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 214 | ctx := builderContext() |
| 215 | fmt.Println(NewRuleBuilder(pctx, ctx).Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 216 | Tool(PathForSource(ctx, "ls")). |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 217 | FlagWithArg("--sort=", "time")) |
| 218 | // Output: |
| 219 | // ls --sort=time |
| 220 | } |
| 221 | |
Colin Cross | c7ed004 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 222 | func ExampleRuleBuilderCommand_FlagForEachArg() { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 223 | ctx := builderContext() |
| 224 | fmt.Println(NewRuleBuilder(pctx, ctx).Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 225 | Tool(PathForSource(ctx, "ls")). |
Colin Cross | c7ed004 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 226 | FlagForEachArg("--sort=", []string{"time", "size"})) |
| 227 | // Output: |
| 228 | // ls --sort=time --sort=size |
| 229 | } |
| 230 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 231 | func ExampleRuleBuilderCommand_FlagForEachInput() { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 232 | ctx := builderContext() |
| 233 | fmt.Println(NewRuleBuilder(pctx, ctx).Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 234 | Tool(PathForSource(ctx, "turbine")). |
| 235 | FlagForEachInput("--classpath ", PathsForTesting("a.jar", "b.jar"))) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 236 | // Output: |
| 237 | // turbine --classpath a.jar --classpath b.jar |
| 238 | } |
| 239 | |
| 240 | func ExampleRuleBuilderCommand_FlagWithInputList() { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 241 | ctx := builderContext() |
| 242 | fmt.Println(NewRuleBuilder(pctx, ctx).Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 243 | Tool(PathForSource(ctx, "java")). |
| 244 | FlagWithInputList("-classpath=", PathsForTesting("a.jar", "b.jar"), ":")) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 245 | // Output: |
| 246 | // java -classpath=a.jar:b.jar |
| 247 | } |
| 248 | |
| 249 | func ExampleRuleBuilderCommand_FlagWithInput() { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 250 | ctx := builderContext() |
| 251 | fmt.Println(NewRuleBuilder(pctx, ctx).Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 252 | Tool(PathForSource(ctx, "java")). |
| 253 | FlagWithInput("-classpath=", PathForSource(ctx, "a"))) |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 254 | // Output: |
| 255 | // java -classpath=a |
| 256 | } |
| 257 | |
| 258 | func ExampleRuleBuilderCommand_FlagWithList() { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 259 | ctx := builderContext() |
| 260 | fmt.Println(NewRuleBuilder(pctx, ctx).Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 261 | Tool(PathForSource(ctx, "ls")). |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 262 | FlagWithList("--sort=", []string{"time", "size"}, ",")) |
| 263 | // Output: |
| 264 | // ls --sort=time,size |
| 265 | } |
| 266 | |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 267 | func ExampleRuleBuilderCommand_FlagWithRspFileInputList() { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 268 | ctx := builderContext() |
| 269 | fmt.Println(NewRuleBuilder(pctx, ctx).Command(). |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 270 | Tool(PathForSource(ctx, "javac")). |
Colin Cross | 70c4741 | 2021-03-12 17:48:14 -0800 | [diff] [blame] | 271 | FlagWithRspFileInputList("@", PathForOutput(ctx, "foo.rsp"), PathsForTesting("a.java", "b.java")). |
| 272 | String()) |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 273 | // Output: |
Colin Cross | 70c4741 | 2021-03-12 17:48:14 -0800 | [diff] [blame] | 274 | // javac @out/foo.rsp |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | func ExampleRuleBuilderCommand_String() { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 278 | ctx := builderContext() |
| 279 | fmt.Println(NewRuleBuilder(pctx, ctx).Command(). |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 280 | Text("FOO=foo"). |
| 281 | Text("echo $FOO"). |
| 282 | String()) |
| 283 | // Output: |
| 284 | // FOO=foo echo $FOO |
| 285 | } |
| 286 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 287 | func TestRuleBuilder(t *testing.T) { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 288 | fs := map[string][]byte{ |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 289 | "dep_fixer": nil, |
| 290 | "input": nil, |
| 291 | "Implicit": nil, |
| 292 | "Input": nil, |
| 293 | "OrderOnly": nil, |
| 294 | "OrderOnlys": nil, |
| 295 | "Tool": nil, |
| 296 | "input2": nil, |
| 297 | "tool2": nil, |
| 298 | "input3": nil, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 301 | pathCtx := PathContextForTesting(TestConfig("out_local", nil, "", fs)) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 302 | ctx := builderContextForTests{ |
| 303 | PathContext: pathCtx, |
| 304 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 305 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 306 | addCommands := func(rule *RuleBuilder) { |
| 307 | cmd := rule.Command(). |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 308 | DepFile(PathForOutput(ctx, "module/DepFile")). |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 309 | Flag("Flag"). |
| 310 | FlagWithArg("FlagWithArg=", "arg"). |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 311 | FlagWithDepFile("FlagWithDepFile=", PathForOutput(ctx, "module/depfile")). |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 312 | FlagWithInput("FlagWithInput=", PathForSource(ctx, "input")). |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 313 | FlagWithOutput("FlagWithOutput=", PathForOutput(ctx, "module/output")). |
| 314 | FlagWithRspFileInputList("FlagWithRspFileInputList=", PathForOutput(ctx, "rsp"), |
| 315 | Paths{ |
| 316 | PathForSource(ctx, "RspInput"), |
| 317 | PathForOutput(ctx, "other/RspOutput2"), |
| 318 | }). |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 319 | Implicit(PathForSource(ctx, "Implicit")). |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 320 | ImplicitDepFile(PathForOutput(ctx, "module/ImplicitDepFile")). |
| 321 | ImplicitOutput(PathForOutput(ctx, "module/ImplicitOutput")). |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 322 | Input(PathForSource(ctx, "Input")). |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 323 | Output(PathForOutput(ctx, "module/Output")). |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 324 | OrderOnly(PathForSource(ctx, "OrderOnly")). |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 325 | SymlinkOutput(PathForOutput(ctx, "module/SymlinkOutput")). |
| 326 | ImplicitSymlinkOutput(PathForOutput(ctx, "module/ImplicitSymlinkOutput")). |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 327 | Text("Text"). |
| 328 | Tool(PathForSource(ctx, "Tool")) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 329 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 330 | rule.Command(). |
| 331 | Text("command2"). |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 332 | DepFile(PathForOutput(ctx, "module/depfile2")). |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 333 | Input(PathForSource(ctx, "input2")). |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 334 | Output(PathForOutput(ctx, "module/output2")). |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 335 | OrderOnlys(PathsForSource(ctx, []string{"OrderOnlys"})). |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 336 | Tool(PathForSource(ctx, "tool2")) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 337 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 338 | // Test updates to the first command after the second command has been started |
| 339 | cmd.Text("after command2") |
| 340 | // Test updating a command when the previous update did not replace the cmd variable |
| 341 | cmd.Text("old cmd") |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 342 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 343 | // Test a command that uses the output of a previous command as an input |
| 344 | rule.Command(). |
| 345 | Text("command3"). |
| 346 | Input(PathForSource(ctx, "input3")). |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 347 | Input(PathForOutput(ctx, "module/output2")). |
| 348 | Output(PathForOutput(ctx, "module/output3")). |
| 349 | Text(cmd.PathForInput(PathForSource(ctx, "input3"))). |
| 350 | Text(cmd.PathForOutput(PathForOutput(ctx, "module/output2"))) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 351 | } |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 352 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 353 | wantInputs := PathsForSource(ctx, []string{"Implicit", "Input", "input", "input2", "input3"}) |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 354 | wantRspFileInputs := Paths{PathForSource(ctx, "RspInput"), |
| 355 | PathForOutput(ctx, "other/RspOutput2")} |
| 356 | wantOutputs := PathsForOutput(ctx, []string{ |
| 357 | "module/ImplicitOutput", "module/ImplicitSymlinkOutput", "module/Output", "module/SymlinkOutput", |
| 358 | "module/output", "module/output2", "module/output3"}) |
| 359 | wantDepFiles := PathsForOutput(ctx, []string{ |
| 360 | "module/DepFile", "module/depfile", "module/ImplicitDepFile", "module/depfile2"}) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 361 | wantTools := PathsForSource(ctx, []string{"Tool", "tool2"}) |
Colin Cross | da71eda | 2020-02-21 16:55:19 -0800 | [diff] [blame] | 362 | wantOrderOnlys := PathsForSource(ctx, []string{"OrderOnly", "OrderOnlys"}) |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 363 | wantSymlinkOutputs := PathsForOutput(ctx, []string{ |
| 364 | "module/ImplicitSymlinkOutput", "module/SymlinkOutput"}) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 365 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 366 | t.Run("normal", func(t *testing.T) { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 367 | rule := NewRuleBuilder(pctx, ctx) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 368 | addCommands(rule) |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 369 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 370 | wantCommands := []string{ |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 371 | "out_local/module/DepFile Flag FlagWithArg=arg FlagWithDepFile=out_local/module/depfile " + |
| 372 | "FlagWithInput=input FlagWithOutput=out_local/module/output FlagWithRspFileInputList=out_local/rsp " + |
| 373 | "Input out_local/module/Output out_local/module/SymlinkOutput Text Tool after command2 old cmd", |
| 374 | "command2 out_local/module/depfile2 input2 out_local/module/output2 tool2", |
| 375 | "command3 input3 out_local/module/output2 out_local/module/output3 input3 out_local/module/output2", |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 376 | } |
Colin Cross | 1d2cf04 | 2019-03-29 15:33:06 -0700 | [diff] [blame] | 377 | |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 378 | wantDepMergerCommand := "out_local/host/" + ctx.Config().PrebuiltOS() + "/bin/dep_fixer " + |
| 379 | "out_local/module/DepFile out_local/module/depfile out_local/module/ImplicitDepFile out_local/module/depfile2" |
| 380 | |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 381 | AssertDeepEquals(t, "rule.Commands()", wantCommands, rule.Commands()) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 382 | |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 383 | AssertDeepEquals(t, "rule.Inputs()", wantInputs, rule.Inputs()) |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 384 | AssertDeepEquals(t, "rule.RspfileInputs()", wantRspFileInputs, rule.RspFileInputs()) |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 385 | AssertDeepEquals(t, "rule.Outputs()", wantOutputs, rule.Outputs()) |
| 386 | AssertDeepEquals(t, "rule.SymlinkOutputs()", wantSymlinkOutputs, rule.SymlinkOutputs()) |
| 387 | AssertDeepEquals(t, "rule.DepFiles()", wantDepFiles, rule.DepFiles()) |
| 388 | AssertDeepEquals(t, "rule.Tools()", wantTools, rule.Tools()) |
| 389 | AssertDeepEquals(t, "rule.OrderOnlys()", wantOrderOnlys, rule.OrderOnlys()) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 390 | |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 391 | AssertSame(t, "rule.depFileMergerCmd()", wantDepMergerCommand, rule.depFileMergerCmd(rule.DepFiles()).String()) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 392 | }) |
| 393 | |
| 394 | t.Run("sbox", func(t *testing.T) { |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 395 | rule := NewRuleBuilder(pctx, ctx).Sbox(PathForOutput(ctx, "module"), |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 396 | PathForOutput(ctx, "sbox.textproto")) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 397 | addCommands(rule) |
| 398 | |
| 399 | wantCommands := []string{ |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 400 | "__SBOX_SANDBOX_DIR__/out/DepFile Flag FlagWithArg=arg FlagWithDepFile=__SBOX_SANDBOX_DIR__/out/depfile " + |
| 401 | "FlagWithInput=input FlagWithOutput=__SBOX_SANDBOX_DIR__/out/output " + |
| 402 | "FlagWithRspFileInputList=out_local/rsp Input __SBOX_SANDBOX_DIR__/out/Output " + |
| 403 | "__SBOX_SANDBOX_DIR__/out/SymlinkOutput Text Tool after command2 old cmd", |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 404 | "command2 __SBOX_SANDBOX_DIR__/out/depfile2 input2 __SBOX_SANDBOX_DIR__/out/output2 tool2", |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 405 | "command3 input3 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/out/output3 input3 __SBOX_SANDBOX_DIR__/out/output2", |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 408 | wantDepMergerCommand := "out_local/host/" + ctx.Config().PrebuiltOS() + "/bin/dep_fixer __SBOX_SANDBOX_DIR__/out/DepFile __SBOX_SANDBOX_DIR__/out/depfile __SBOX_SANDBOX_DIR__/out/ImplicitDepFile __SBOX_SANDBOX_DIR__/out/depfile2" |
| 409 | |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 410 | AssertDeepEquals(t, "rule.Commands()", wantCommands, rule.Commands()) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 411 | |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 412 | AssertDeepEquals(t, "rule.Inputs()", wantInputs, rule.Inputs()) |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 413 | AssertDeepEquals(t, "rule.RspfileInputs()", wantRspFileInputs, rule.RspFileInputs()) |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 414 | AssertDeepEquals(t, "rule.Outputs()", wantOutputs, rule.Outputs()) |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 415 | AssertDeepEquals(t, "rule.SymlinkOutputs()", wantSymlinkOutputs, rule.SymlinkOutputs()) |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 416 | AssertDeepEquals(t, "rule.DepFiles()", wantDepFiles, rule.DepFiles()) |
| 417 | AssertDeepEquals(t, "rule.Tools()", wantTools, rule.Tools()) |
| 418 | AssertDeepEquals(t, "rule.OrderOnlys()", wantOrderOnlys, rule.OrderOnlys()) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 419 | |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 420 | AssertSame(t, "rule.depFileMergerCmd()", wantDepMergerCommand, rule.depFileMergerCmd(rule.DepFiles()).String()) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 421 | }) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 422 | |
| 423 | t.Run("sbox tools", func(t *testing.T) { |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 424 | rule := NewRuleBuilder(pctx, ctx).Sbox(PathForOutput(ctx, "module"), |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 425 | PathForOutput(ctx, "sbox.textproto")).SandboxTools() |
| 426 | addCommands(rule) |
| 427 | |
| 428 | wantCommands := []string{ |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 429 | "__SBOX_SANDBOX_DIR__/out/DepFile Flag FlagWithArg=arg FlagWithDepFile=__SBOX_SANDBOX_DIR__/out/depfile " + |
| 430 | "FlagWithInput=input FlagWithOutput=__SBOX_SANDBOX_DIR__/out/output " + |
| 431 | "FlagWithRspFileInputList=out_local/rsp Input __SBOX_SANDBOX_DIR__/out/Output " + |
| 432 | "__SBOX_SANDBOX_DIR__/out/SymlinkOutput Text __SBOX_SANDBOX_DIR__/tools/src/Tool after command2 old cmd", |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 433 | "command2 __SBOX_SANDBOX_DIR__/out/depfile2 input2 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/tools/src/tool2", |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 434 | "command3 input3 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/out/output3 input3 __SBOX_SANDBOX_DIR__/out/output2", |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | wantDepMergerCommand := "__SBOX_SANDBOX_DIR__/tools/out/bin/dep_fixer __SBOX_SANDBOX_DIR__/out/DepFile __SBOX_SANDBOX_DIR__/out/depfile __SBOX_SANDBOX_DIR__/out/ImplicitDepFile __SBOX_SANDBOX_DIR__/out/depfile2" |
| 438 | |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 439 | AssertDeepEquals(t, "rule.Commands()", wantCommands, rule.Commands()) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 440 | |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 441 | AssertDeepEquals(t, "rule.Inputs()", wantInputs, rule.Inputs()) |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 442 | AssertDeepEquals(t, "rule.RspfileInputs()", wantRspFileInputs, rule.RspFileInputs()) |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 443 | AssertDeepEquals(t, "rule.Outputs()", wantOutputs, rule.Outputs()) |
Colin Cross | ab020a7 | 2021-03-12 17:52:23 -0800 | [diff] [blame] | 444 | AssertDeepEquals(t, "rule.SymlinkOutputs()", wantSymlinkOutputs, rule.SymlinkOutputs()) |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 445 | AssertDeepEquals(t, "rule.DepFiles()", wantDepFiles, rule.DepFiles()) |
| 446 | AssertDeepEquals(t, "rule.Tools()", wantTools, rule.Tools()) |
| 447 | AssertDeepEquals(t, "rule.OrderOnlys()", wantOrderOnlys, rule.OrderOnlys()) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 448 | |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 449 | AssertSame(t, "rule.depFileMergerCmd()", wantDepMergerCommand, rule.depFileMergerCmd(rule.DepFiles()).String()) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 450 | }) |
Colin Cross | 045bfd9 | 2021-03-24 16:38:03 -0700 | [diff] [blame] | 451 | |
| 452 | t.Run("sbox inputs", func(t *testing.T) { |
| 453 | rule := NewRuleBuilder(pctx, ctx).Sbox(PathForOutput(ctx, "module"), |
| 454 | PathForOutput(ctx, "sbox.textproto")).SandboxInputs() |
| 455 | addCommands(rule) |
| 456 | |
| 457 | wantCommands := []string{ |
| 458 | "__SBOX_SANDBOX_DIR__/out/DepFile Flag FlagWithArg=arg FlagWithDepFile=__SBOX_SANDBOX_DIR__/out/depfile " + |
| 459 | "FlagWithInput=input FlagWithOutput=__SBOX_SANDBOX_DIR__/out/output " + |
| 460 | "FlagWithRspFileInputList=__SBOX_SANDBOX_DIR__/out/rsp Input __SBOX_SANDBOX_DIR__/out/Output " + |
| 461 | "__SBOX_SANDBOX_DIR__/out/SymlinkOutput Text __SBOX_SANDBOX_DIR__/tools/src/Tool after command2 old cmd", |
| 462 | "command2 __SBOX_SANDBOX_DIR__/out/depfile2 input2 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/tools/src/tool2", |
| 463 | "command3 input3 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/out/output3 input3 __SBOX_SANDBOX_DIR__/out/output2", |
| 464 | } |
| 465 | |
| 466 | wantDepMergerCommand := "__SBOX_SANDBOX_DIR__/tools/out/bin/dep_fixer __SBOX_SANDBOX_DIR__/out/DepFile __SBOX_SANDBOX_DIR__/out/depfile __SBOX_SANDBOX_DIR__/out/ImplicitDepFile __SBOX_SANDBOX_DIR__/out/depfile2" |
| 467 | |
| 468 | AssertDeepEquals(t, "rule.Commands()", wantCommands, rule.Commands()) |
| 469 | |
| 470 | AssertDeepEquals(t, "rule.Inputs()", wantInputs, rule.Inputs()) |
| 471 | AssertDeepEquals(t, "rule.RspfileInputs()", wantRspFileInputs, rule.RspFileInputs()) |
| 472 | AssertDeepEquals(t, "rule.Outputs()", wantOutputs, rule.Outputs()) |
| 473 | AssertDeepEquals(t, "rule.SymlinkOutputs()", wantSymlinkOutputs, rule.SymlinkOutputs()) |
| 474 | AssertDeepEquals(t, "rule.DepFiles()", wantDepFiles, rule.DepFiles()) |
| 475 | AssertDeepEquals(t, "rule.Tools()", wantTools, rule.Tools()) |
| 476 | AssertDeepEquals(t, "rule.OrderOnlys()", wantOrderOnlys, rule.OrderOnlys()) |
| 477 | |
| 478 | AssertSame(t, "rule.depFileMergerCmd()", wantDepMergerCommand, rule.depFileMergerCmd(rule.DepFiles()).String()) |
| 479 | }) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | func testRuleBuilderFactory() Module { |
| 483 | module := &testRuleBuilderModule{} |
| 484 | module.AddProperties(&module.properties) |
| 485 | InitAndroidModule(module) |
| 486 | return module |
| 487 | } |
| 488 | |
| 489 | type testRuleBuilderModule struct { |
| 490 | ModuleBase |
| 491 | properties struct { |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 492 | Srcs []string |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 493 | |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 494 | Restat bool |
| 495 | Sbox bool |
| 496 | Sbox_inputs bool |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | func (t *testRuleBuilderModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 501 | in := PathsForSource(ctx, t.properties.Srcs) |
Colin Cross | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame^] | 502 | implicit := PathForSource(ctx, "implicit") |
| 503 | orderOnly := PathForSource(ctx, "orderonly") |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 504 | out := PathForModuleOut(ctx, "gen", ctx.ModuleName()) |
| 505 | outDep := PathForModuleOut(ctx, "gen", ctx.ModuleName()+".d") |
| 506 | outDir := PathForModuleOut(ctx, "gen") |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 507 | rspFile := PathForModuleOut(ctx, "rsp") |
| 508 | rspFile2 := PathForModuleOut(ctx, "rsp2") |
| 509 | rspFileContents := PathsForSource(ctx, []string{"rsp_in"}) |
| 510 | rspFileContents2 := PathsForSource(ctx, []string{"rsp_in2"}) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 511 | manifestPath := PathForModuleOut(ctx, "sbox.textproto") |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 512 | |
Colin Cross | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame^] | 513 | testRuleBuilder_Build(ctx, in, implicit, orderOnly, out, outDep, outDir, |
| 514 | manifestPath, t.properties.Restat, t.properties.Sbox, t.properties.Sbox_inputs, |
| 515 | rspFile, rspFileContents, rspFile2, rspFileContents2) |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | type testRuleBuilderSingleton struct{} |
| 519 | |
| 520 | func testRuleBuilderSingletonFactory() Singleton { |
| 521 | return &testRuleBuilderSingleton{} |
| 522 | } |
| 523 | |
| 524 | func (t *testRuleBuilderSingleton) GenerateBuildActions(ctx SingletonContext) { |
Colin Cross | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame^] | 525 | in := PathsForSource(ctx, []string{"in"}) |
| 526 | implicit := PathForSource(ctx, "implicit") |
| 527 | orderOnly := PathForSource(ctx, "orderonly") |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 528 | out := PathForOutput(ctx, "singleton/gen/baz") |
| 529 | outDep := PathForOutput(ctx, "singleton/gen/baz.d") |
| 530 | outDir := PathForOutput(ctx, "singleton/gen") |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 531 | rspFile := PathForOutput(ctx, "singleton/rsp") |
| 532 | rspFile2 := PathForOutput(ctx, "singleton/rsp2") |
| 533 | rspFileContents := PathsForSource(ctx, []string{"rsp_in"}) |
| 534 | rspFileContents2 := PathsForSource(ctx, []string{"rsp_in2"}) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 535 | manifestPath := PathForOutput(ctx, "singleton/sbox.textproto") |
Colin Cross | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame^] | 536 | |
| 537 | testRuleBuilder_Build(ctx, in, implicit, orderOnly, out, outDep, outDir, |
| 538 | manifestPath, true, false, false, |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 539 | rspFile, rspFileContents, rspFile2, rspFileContents2) |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Colin Cross | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame^] | 542 | func testRuleBuilder_Build(ctx BuilderContext, in Paths, implicit, orderOnly Path, |
| 543 | out, outDep, outDir, manifestPath WritablePath, |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 544 | restat, sbox, sboxInputs bool, |
| 545 | rspFile WritablePath, rspFileContents Paths, rspFile2 WritablePath, rspFileContents2 Paths) { |
| 546 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 547 | rule := NewRuleBuilder(pctx, ctx) |
Colin Cross | 786cd6d | 2019-02-01 16:41:11 -0800 | [diff] [blame] | 548 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 549 | if sbox { |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 550 | rule.Sbox(outDir, manifestPath) |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 551 | if sboxInputs { |
| 552 | rule.SandboxInputs() |
| 553 | } |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 554 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 555 | |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 556 | rule.Command(). |
| 557 | Tool(PathForSource(ctx, "cp")). |
| 558 | Inputs(in). |
Colin Cross | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame^] | 559 | Implicit(implicit). |
| 560 | OrderOnly(orderOnly). |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 561 | Output(out). |
| 562 | ImplicitDepFile(outDep). |
| 563 | FlagWithRspFileInputList("@", rspFile, rspFileContents). |
| 564 | FlagWithRspFileInputList("@", rspFile2, rspFileContents2) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 565 | |
| 566 | if restat { |
| 567 | rule.Restat() |
| 568 | } |
Colin Cross | baa676f | 2019-02-25 14:56:01 -0800 | [diff] [blame] | 569 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 570 | rule.Build("rule", "desc") |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 571 | } |
| 572 | |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 573 | var prepareForRuleBuilderTest = FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 574 | ctx.RegisterModuleType("rule_builder_test", testRuleBuilderFactory) |
| 575 | ctx.RegisterSingletonType("rule_builder_test", testRuleBuilderSingletonFactory) |
| 576 | }) |
| 577 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 578 | func TestRuleBuilder_Build(t *testing.T) { |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 579 | fs := MockFS{ |
Colin Cross | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame^] | 580 | "in": nil, |
| 581 | "cp": nil, |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 582 | } |
| 583 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 584 | bp := ` |
| 585 | rule_builder_test { |
| 586 | name: "foo", |
Colin Cross | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame^] | 587 | srcs: ["in"], |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 588 | restat: true, |
| 589 | } |
| 590 | rule_builder_test { |
| 591 | name: "foo_sbox", |
Colin Cross | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame^] | 592 | srcs: ["in"], |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 593 | sbox: true, |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 594 | } |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 595 | rule_builder_test { |
| 596 | name: "foo_sbox_inputs", |
Colin Cross | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame^] | 597 | srcs: ["in"], |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 598 | sbox: true, |
| 599 | sbox_inputs: true, |
| 600 | } |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 601 | ` |
| 602 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 603 | result := GroupFixturePreparers( |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 604 | prepareForRuleBuilderTest, |
| 605 | FixtureWithRootAndroidBp(bp), |
| 606 | fs.AddToFixture(), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 607 | ).RunTest(t) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 608 | |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 609 | check := func(t *testing.T, params TestingBuildParams, rspFile2Params TestingBuildParams, |
| 610 | wantCommand, wantOutput, wantDepfile, wantRspFile, wantRspFile2 string, |
| 611 | wantRestat bool, extraImplicits, extraCmdDeps []string) { |
| 612 | |
Dan Willemsen | c89b6f1 | 2019-08-29 14:47:40 -0700 | [diff] [blame] | 613 | t.Helper() |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 614 | command := params.RuleParams.Command |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 615 | re := regexp.MustCompile(" # hash of input list: [a-z0-9]*$") |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 616 | command = re.ReplaceAllLiteralString(command, "") |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 617 | |
| 618 | AssertStringEquals(t, "RuleParams.Command", wantCommand, command) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 619 | |
| 620 | wantDeps := append([]string{"cp"}, extraCmdDeps...) |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 621 | AssertArrayString(t, "RuleParams.CommandDeps", wantDeps, params.RuleParams.CommandDeps) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 622 | |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 623 | AssertBoolEquals(t, "RuleParams.Restat", wantRestat, params.RuleParams.Restat) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 624 | |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 625 | wantInputs := []string{"rsp_in"} |
| 626 | AssertArrayString(t, "Inputs", wantInputs, params.Inputs.Strings()) |
| 627 | |
Colin Cross | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame^] | 628 | wantImplicits := append([]string{"implicit", "in"}, extraImplicits...) |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 629 | // The second rsp file and the files listed in it should be in implicits |
| 630 | wantImplicits = append(wantImplicits, "rsp_in2", wantRspFile2) |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 631 | AssertPathsRelativeToTopEquals(t, "Implicits", wantImplicits, params.Implicits) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 632 | |
Colin Cross | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame^] | 633 | wantOrderOnlys := []string{"orderonly"} |
| 634 | AssertPathsRelativeToTopEquals(t, "OrderOnly", wantOrderOnlys, params.OrderOnly) |
| 635 | |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 636 | wantRspFileContent := "$in" |
| 637 | AssertStringEquals(t, "RspfileContent", wantRspFileContent, params.RuleParams.RspfileContent) |
| 638 | |
| 639 | AssertStringEquals(t, "Rspfile", wantRspFile, params.RuleParams.Rspfile) |
| 640 | |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 641 | AssertPathRelativeToTopEquals(t, "Output", wantOutput, params.Output) |
Colin Cross | baa676f | 2019-02-25 14:56:01 -0800 | [diff] [blame] | 642 | |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 643 | if len(params.ImplicitOutputs) != 0 { |
| 644 | t.Errorf("want ImplicitOutputs = [], got %q", params.ImplicitOutputs.Strings()) |
| 645 | } |
| 646 | |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 647 | AssertPathRelativeToTopEquals(t, "Depfile", wantDepfile, params.Depfile) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 648 | |
| 649 | if params.Deps != blueprint.DepsGCC { |
| 650 | t.Errorf("want Deps = %q, got %q", blueprint.DepsGCC, params.Deps) |
Colin Cross | baa676f | 2019-02-25 14:56:01 -0800 | [diff] [blame] | 651 | } |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 652 | |
| 653 | rspFile2Content := ContentFromFileRuleForTests(t, rspFile2Params) |
| 654 | AssertStringEquals(t, "rspFile2 content", "rsp_in2\n", rspFile2Content) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 655 | } |
| 656 | |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 657 | t.Run("module", func(t *testing.T) { |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 658 | outFile := "out/soong/.intermediates/foo/gen/foo" |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 659 | rspFile := "out/soong/.intermediates/foo/rsp" |
| 660 | rspFile2 := "out/soong/.intermediates/foo/rsp2" |
| 661 | module := result.ModuleForTests("foo", "") |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 662 | check(t, module.Rule("rule"), module.Output(rspFile2), |
Colin Cross | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame^] | 663 | "cp in "+outFile+" @"+rspFile+" @"+rspFile2, |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 664 | outFile, outFile+".d", rspFile, rspFile2, true, nil, nil) |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 665 | }) |
| 666 | t.Run("sbox", func(t *testing.T) { |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 667 | outDir := "out/soong/.intermediates/foo_sbox" |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 668 | outFile := filepath.Join(outDir, "gen/foo_sbox") |
| 669 | depFile := filepath.Join(outDir, "gen/foo_sbox.d") |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 670 | rspFile := filepath.Join(outDir, "rsp") |
| 671 | rspFile2 := filepath.Join(outDir, "rsp2") |
| 672 | manifest := filepath.Join(outDir, "sbox.textproto") |
| 673 | sbox := filepath.Join("out", "soong", "host", result.Config.PrebuiltOS(), "bin/sbox") |
| 674 | sandboxPath := shared.TempDirForOutDir("out/soong") |
| 675 | |
| 676 | cmd := `rm -rf ` + outDir + `/gen && ` + |
| 677 | sbox + ` --sandbox-path ` + sandboxPath + ` --manifest ` + manifest |
| 678 | module := result.ModuleForTests("foo_sbox", "") |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 679 | check(t, module.Output("gen/foo_sbox"), module.Output(rspFile2), |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 680 | cmd, outFile, depFile, rspFile, rspFile2, false, []string{manifest}, []string{sbox}) |
| 681 | }) |
| 682 | t.Run("sbox_inputs", func(t *testing.T) { |
| 683 | outDir := "out/soong/.intermediates/foo_sbox_inputs" |
| 684 | outFile := filepath.Join(outDir, "gen/foo_sbox_inputs") |
| 685 | depFile := filepath.Join(outDir, "gen/foo_sbox_inputs.d") |
| 686 | rspFile := filepath.Join(outDir, "rsp") |
| 687 | rspFile2 := filepath.Join(outDir, "rsp2") |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 688 | manifest := filepath.Join(outDir, "sbox.textproto") |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 689 | sbox := filepath.Join("out", "soong", "host", result.Config.PrebuiltOS(), "bin/sbox") |
| 690 | sandboxPath := shared.TempDirForOutDir("out/soong") |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 691 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 692 | cmd := `rm -rf ` + outDir + `/gen && ` + |
| 693 | sbox + ` --sandbox-path ` + sandboxPath + ` --manifest ` + manifest |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 694 | |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 695 | module := result.ModuleForTests("foo_sbox_inputs", "") |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 696 | check(t, module.Output("gen/foo_sbox_inputs"), module.Output(rspFile2), |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 697 | cmd, outFile, depFile, rspFile, rspFile2, false, []string{manifest}, []string{sbox}) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 698 | }) |
| 699 | t.Run("singleton", func(t *testing.T) { |
Paul Duffin | 709e0e3 | 2021-03-22 10:09:02 +0000 | [diff] [blame] | 700 | outFile := filepath.Join("out/soong/singleton/gen/baz") |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 701 | rspFile := filepath.Join("out/soong/singleton/rsp") |
| 702 | rspFile2 := filepath.Join("out/soong/singleton/rsp2") |
| 703 | singleton := result.SingletonForTests("rule_builder_test") |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 704 | check(t, singleton.Rule("rule"), singleton.Output(rspFile2), |
Colin Cross | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame^] | 705 | "cp in "+outFile+" @"+rspFile+" @"+rspFile2, |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 706 | outFile, outFile+".d", rspFile, rspFile2, true, nil, nil) |
Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 707 | }) |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 708 | } |
Colin Cross | 0cb0d7b | 2019-07-11 10:59:15 -0700 | [diff] [blame] | 709 | |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 710 | func TestRuleBuilderHashInputs(t *testing.T) { |
| 711 | // The basic idea here is to verify that the command (in the case of a |
| 712 | // non-sbox rule) or the sbox textproto manifest contain a hash of the |
| 713 | // inputs. |
| 714 | |
| 715 | // By including a hash of the inputs, we cause the rule to re-run if |
| 716 | // the list of inputs changes because the command line or a dependency |
| 717 | // changes. |
| 718 | |
Colin Cross | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame^] | 719 | hashOf := func(s string) string { |
| 720 | sum := sha256.Sum256([]byte(s)) |
| 721 | return hex.EncodeToString(sum[:]) |
| 722 | } |
| 723 | |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 724 | bp := ` |
| 725 | rule_builder_test { |
| 726 | name: "hash0", |
| 727 | srcs: ["in1.txt", "in2.txt"], |
| 728 | } |
| 729 | rule_builder_test { |
| 730 | name: "hash0_sbox", |
| 731 | srcs: ["in1.txt", "in2.txt"], |
| 732 | sbox: true, |
| 733 | } |
| 734 | rule_builder_test { |
| 735 | name: "hash1", |
| 736 | srcs: ["in1.txt", "in2.txt", "in3.txt"], |
| 737 | } |
| 738 | rule_builder_test { |
| 739 | name: "hash1_sbox", |
| 740 | srcs: ["in1.txt", "in2.txt", "in3.txt"], |
| 741 | sbox: true, |
| 742 | } |
| 743 | ` |
| 744 | testcases := []struct { |
| 745 | name string |
| 746 | expectedHash string |
| 747 | }{ |
| 748 | { |
Colin Cross | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame^] | 749 | name: "hash0", |
| 750 | expectedHash: hashOf("implicit\nin1.txt\nin2.txt"), |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 751 | }, |
| 752 | { |
Colin Cross | da6401b | 2021-04-21 11:32:19 -0700 | [diff] [blame^] | 753 | name: "hash1", |
| 754 | expectedHash: hashOf("implicit\nin1.txt\nin2.txt\nin3.txt"), |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 755 | }, |
| 756 | } |
| 757 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 758 | result := GroupFixturePreparers( |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 759 | prepareForRuleBuilderTest, |
| 760 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 761 | ).RunTest(t) |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 762 | |
| 763 | for _, test := range testcases { |
| 764 | t.Run(test.name, func(t *testing.T) { |
| 765 | t.Run("sbox", func(t *testing.T) { |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 766 | gen := result.ModuleForTests(test.name+"_sbox", "") |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 767 | manifest := RuleBuilderSboxProtoForTests(t, gen.Output("sbox.textproto")) |
| 768 | hash := manifest.Commands[0].GetInputHash() |
| 769 | |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 770 | AssertStringEquals(t, "hash", test.expectedHash, hash) |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 771 | }) |
| 772 | t.Run("", func(t *testing.T) { |
Paul Duffin | d250ff6 | 2021-03-16 21:51:29 +0000 | [diff] [blame] | 773 | gen := result.ModuleForTests(test.name+"", "") |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 774 | command := gen.Output("gen/" + test.name).RuleParams.Command |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 775 | if g, w := command, " # hash of input list: "+test.expectedHash; !strings.HasSuffix(g, w) { |
| 776 | t.Errorf("Expected command line to end with %q, got %q", w, g) |
| 777 | } |
| 778 | }) |
| 779 | }) |
| 780 | } |
| 781 | } |