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