Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 1 | // Copyright 2018 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package genrule |
| 16 | |
| 17 | import ( |
| 18 | "io/ioutil" |
| 19 | "os" |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 20 | "reflect" |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 21 | "strings" |
| 22 | "testing" |
| 23 | |
| 24 | "android/soong/android" |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 25 | |
| 26 | "github.com/google/blueprint/proptools" |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | var buildDir string |
| 30 | |
| 31 | func setUp() { |
| 32 | var err error |
Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 33 | buildDir, err = ioutil.TempDir("", "genrule_test") |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 34 | if err != nil { |
| 35 | panic(err) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func tearDown() { |
| 40 | os.RemoveAll(buildDir) |
| 41 | } |
| 42 | |
| 43 | func TestMain(m *testing.M) { |
| 44 | run := func() int { |
| 45 | setUp() |
| 46 | defer tearDown() |
| 47 | |
| 48 | return m.Run() |
| 49 | } |
| 50 | |
| 51 | os.Exit(run()) |
| 52 | } |
| 53 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 54 | func testContext(config android.Config) *android.TestContext { |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 55 | |
| 56 | ctx := android.NewTestArchContext() |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 57 | ctx.RegisterModuleType("filegroup", android.FileGroupFactory) |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 58 | ctx.RegisterModuleType("tool", toolFactory) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 59 | |
| 60 | registerGenruleBuildComponents(ctx) |
| 61 | |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 62 | ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 63 | ctx.Register(config) |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 64 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 65 | return ctx |
| 66 | } |
| 67 | |
| 68 | func testConfig(bp string, fs map[string][]byte) android.Config { |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 69 | bp += ` |
| 70 | tool { |
| 71 | name: "tool", |
| 72 | } |
| 73 | |
| 74 | filegroup { |
| 75 | name: "tool_files", |
| 76 | srcs: [ |
| 77 | "tool_file1", |
| 78 | "tool_file2", |
| 79 | ], |
| 80 | } |
| 81 | |
| 82 | filegroup { |
| 83 | name: "1tool_file", |
| 84 | srcs: [ |
| 85 | "tool_file1", |
| 86 | ], |
| 87 | } |
| 88 | |
| 89 | filegroup { |
| 90 | name: "ins", |
| 91 | srcs: [ |
| 92 | "in1", |
| 93 | "in2", |
| 94 | ], |
| 95 | } |
| 96 | |
| 97 | filegroup { |
| 98 | name: "1in", |
| 99 | srcs: [ |
| 100 | "in1", |
| 101 | ], |
| 102 | } |
| 103 | |
| 104 | filegroup { |
| 105 | name: "empty", |
| 106 | } |
| 107 | ` |
| 108 | |
| 109 | mockFS := map[string][]byte{ |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 110 | "tool": nil, |
| 111 | "tool_file1": nil, |
| 112 | "tool_file2": nil, |
| 113 | "in1": nil, |
| 114 | "in2": nil, |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 115 | "in1.txt": nil, |
| 116 | "in2.txt": nil, |
| 117 | "in3.txt": nil, |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | for k, v := range fs { |
| 121 | mockFS[k] = v |
| 122 | } |
| 123 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 124 | return android.TestArchConfig(buildDir, nil, bp, mockFS) |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | func TestGenruleCmd(t *testing.T) { |
| 128 | testcases := []struct { |
| 129 | name string |
| 130 | prop string |
| 131 | |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 132 | allowMissingDependencies bool |
| 133 | |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 134 | err string |
| 135 | expect string |
| 136 | }{ |
| 137 | { |
| 138 | name: "empty location tool", |
| 139 | prop: ` |
| 140 | tools: ["tool"], |
| 141 | out: ["out"], |
| 142 | cmd: "$(location) > $(out)", |
| 143 | `, |
| 144 | expect: "out/tool > __SBOX_OUT_FILES__", |
| 145 | }, |
| 146 | { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 147 | name: "empty location tool2", |
| 148 | prop: ` |
| 149 | tools: [":tool"], |
| 150 | out: ["out"], |
| 151 | cmd: "$(location) > $(out)", |
| 152 | `, |
| 153 | expect: "out/tool > __SBOX_OUT_FILES__", |
| 154 | }, |
| 155 | { |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 156 | name: "empty location tool file", |
| 157 | prop: ` |
| 158 | tool_files: ["tool_file1"], |
| 159 | out: ["out"], |
| 160 | cmd: "$(location) > $(out)", |
| 161 | `, |
| 162 | expect: "tool_file1 > __SBOX_OUT_FILES__", |
| 163 | }, |
| 164 | { |
| 165 | name: "empty location tool file fg", |
| 166 | prop: ` |
| 167 | tool_files: [":1tool_file"], |
| 168 | out: ["out"], |
| 169 | cmd: "$(location) > $(out)", |
| 170 | `, |
| 171 | expect: "tool_file1 > __SBOX_OUT_FILES__", |
| 172 | }, |
| 173 | { |
| 174 | name: "empty location tool and tool file", |
| 175 | prop: ` |
| 176 | tools: ["tool"], |
| 177 | tool_files: ["tool_file1"], |
| 178 | out: ["out"], |
| 179 | cmd: "$(location) > $(out)", |
| 180 | `, |
| 181 | expect: "out/tool > __SBOX_OUT_FILES__", |
| 182 | }, |
| 183 | { |
| 184 | name: "tool", |
| 185 | prop: ` |
| 186 | tools: ["tool"], |
| 187 | out: ["out"], |
| 188 | cmd: "$(location tool) > $(out)", |
| 189 | `, |
| 190 | expect: "out/tool > __SBOX_OUT_FILES__", |
| 191 | }, |
| 192 | { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 193 | name: "tool2", |
| 194 | prop: ` |
| 195 | tools: [":tool"], |
| 196 | out: ["out"], |
| 197 | cmd: "$(location :tool) > $(out)", |
| 198 | `, |
| 199 | expect: "out/tool > __SBOX_OUT_FILES__", |
| 200 | }, |
| 201 | { |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 202 | name: "tool file", |
| 203 | prop: ` |
| 204 | tool_files: ["tool_file1"], |
| 205 | out: ["out"], |
| 206 | cmd: "$(location tool_file1) > $(out)", |
| 207 | `, |
| 208 | expect: "tool_file1 > __SBOX_OUT_FILES__", |
| 209 | }, |
| 210 | { |
| 211 | name: "tool file fg", |
| 212 | prop: ` |
| 213 | tool_files: [":1tool_file"], |
| 214 | out: ["out"], |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 215 | cmd: "$(location :1tool_file) > $(out)", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 216 | `, |
| 217 | expect: "tool_file1 > __SBOX_OUT_FILES__", |
| 218 | }, |
| 219 | { |
| 220 | name: "tool files", |
| 221 | prop: ` |
| 222 | tool_files: [":tool_files"], |
| 223 | out: ["out"], |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 224 | cmd: "$(locations :tool_files) > $(out)", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 225 | `, |
| 226 | expect: "tool_file1 tool_file2 > __SBOX_OUT_FILES__", |
| 227 | }, |
| 228 | { |
| 229 | name: "in1", |
| 230 | prop: ` |
| 231 | srcs: ["in1"], |
| 232 | out: ["out"], |
| 233 | cmd: "cat $(in) > $(out)", |
| 234 | `, |
| 235 | expect: "cat ${in} > __SBOX_OUT_FILES__", |
| 236 | }, |
| 237 | { |
| 238 | name: "in1 fg", |
| 239 | prop: ` |
| 240 | srcs: [":1in"], |
| 241 | out: ["out"], |
| 242 | cmd: "cat $(in) > $(out)", |
| 243 | `, |
| 244 | expect: "cat ${in} > __SBOX_OUT_FILES__", |
| 245 | }, |
| 246 | { |
| 247 | name: "ins", |
| 248 | prop: ` |
| 249 | srcs: ["in1", "in2"], |
| 250 | out: ["out"], |
| 251 | cmd: "cat $(in) > $(out)", |
| 252 | `, |
| 253 | expect: "cat ${in} > __SBOX_OUT_FILES__", |
| 254 | }, |
| 255 | { |
| 256 | name: "ins fg", |
| 257 | prop: ` |
| 258 | srcs: [":ins"], |
| 259 | out: ["out"], |
| 260 | cmd: "cat $(in) > $(out)", |
| 261 | `, |
| 262 | expect: "cat ${in} > __SBOX_OUT_FILES__", |
| 263 | }, |
| 264 | { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 265 | name: "location in1", |
| 266 | prop: ` |
| 267 | srcs: ["in1"], |
| 268 | out: ["out"], |
| 269 | cmd: "cat $(location in1) > $(out)", |
| 270 | `, |
| 271 | expect: "cat in1 > __SBOX_OUT_FILES__", |
| 272 | }, |
| 273 | { |
| 274 | name: "location in1 fg", |
| 275 | prop: ` |
| 276 | srcs: [":1in"], |
| 277 | out: ["out"], |
| 278 | cmd: "cat $(location :1in) > $(out)", |
| 279 | `, |
| 280 | expect: "cat in1 > __SBOX_OUT_FILES__", |
| 281 | }, |
| 282 | { |
| 283 | name: "location ins", |
| 284 | prop: ` |
| 285 | srcs: ["in1", "in2"], |
| 286 | out: ["out"], |
| 287 | cmd: "cat $(location in1) > $(out)", |
| 288 | `, |
| 289 | expect: "cat in1 > __SBOX_OUT_FILES__", |
| 290 | }, |
| 291 | { |
| 292 | name: "location ins fg", |
| 293 | prop: ` |
| 294 | srcs: [":ins"], |
| 295 | out: ["out"], |
| 296 | cmd: "cat $(locations :ins) > $(out)", |
| 297 | `, |
| 298 | expect: "cat in1 in2 > __SBOX_OUT_FILES__", |
| 299 | }, |
| 300 | { |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 301 | name: "outs", |
| 302 | prop: ` |
| 303 | out: ["out", "out2"], |
| 304 | cmd: "echo foo > $(out)", |
| 305 | `, |
| 306 | expect: "echo foo > __SBOX_OUT_FILES__", |
| 307 | }, |
| 308 | { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 309 | name: "location out", |
| 310 | prop: ` |
| 311 | out: ["out", "out2"], |
| 312 | cmd: "echo foo > $(location out2)", |
| 313 | `, |
| 314 | expect: "echo foo > __SBOX_OUT_DIR__/out2", |
| 315 | }, |
| 316 | { |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 317 | name: "depfile", |
| 318 | prop: ` |
| 319 | out: ["out"], |
| 320 | depfile: true, |
| 321 | cmd: "echo foo > $(out) && touch $(depfile)", |
| 322 | `, |
| 323 | expect: "echo foo > __SBOX_OUT_FILES__ && touch __SBOX_DEPFILE__", |
| 324 | }, |
| 325 | { |
| 326 | name: "gendir", |
| 327 | prop: ` |
| 328 | out: ["out"], |
| 329 | cmd: "echo foo > $(genDir)/foo && cp $(genDir)/foo $(out)", |
| 330 | `, |
| 331 | expect: "echo foo > __SBOX_OUT_DIR__/foo && cp __SBOX_OUT_DIR__/foo __SBOX_OUT_FILES__", |
| 332 | }, |
| 333 | |
| 334 | { |
| 335 | name: "error empty location", |
| 336 | prop: ` |
| 337 | out: ["out"], |
| 338 | cmd: "$(location) > $(out)", |
| 339 | `, |
| 340 | err: "at least one `tools` or `tool_files` is required if $(location) is used", |
| 341 | }, |
| 342 | { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 343 | name: "error empty location no files", |
| 344 | prop: ` |
| 345 | tool_files: [":empty"], |
| 346 | out: ["out"], |
| 347 | cmd: "$(location) > $(out)", |
| 348 | `, |
| 349 | err: `default label ":empty" has no files`, |
| 350 | }, |
| 351 | { |
| 352 | name: "error empty location multiple files", |
| 353 | prop: ` |
| 354 | tool_files: [":tool_files"], |
| 355 | out: ["out"], |
| 356 | cmd: "$(location) > $(out)", |
| 357 | `, |
| 358 | err: `default label ":tool_files" has multiple files`, |
| 359 | }, |
| 360 | { |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 361 | name: "error location", |
| 362 | prop: ` |
| 363 | out: ["out"], |
| 364 | cmd: "echo foo > $(location missing)", |
| 365 | `, |
| 366 | err: `unknown location label "missing"`, |
| 367 | }, |
| 368 | { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 369 | name: "error locations", |
| 370 | prop: ` |
| 371 | out: ["out"], |
| 372 | cmd: "echo foo > $(locations missing)", |
| 373 | `, |
| 374 | err: `unknown locations label "missing"`, |
| 375 | }, |
| 376 | { |
| 377 | name: "error location no files", |
| 378 | prop: ` |
| 379 | out: ["out"], |
| 380 | srcs: [":empty"], |
| 381 | cmd: "echo $(location :empty) > $(out)", |
| 382 | `, |
| 383 | err: `label ":empty" has no files`, |
| 384 | }, |
| 385 | { |
| 386 | name: "error locations no files", |
| 387 | prop: ` |
| 388 | out: ["out"], |
| 389 | srcs: [":empty"], |
| 390 | cmd: "echo $(locations :empty) > $(out)", |
| 391 | `, |
| 392 | err: `label ":empty" has no files`, |
| 393 | }, |
| 394 | { |
| 395 | name: "error location multiple files", |
| 396 | prop: ` |
| 397 | out: ["out"], |
| 398 | srcs: [":ins"], |
| 399 | cmd: "echo $(location :ins) > $(out)", |
| 400 | `, |
| 401 | err: `label ":ins" has multiple files`, |
| 402 | }, |
| 403 | { |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 404 | name: "error variable", |
| 405 | prop: ` |
| 406 | out: ["out"], |
| 407 | srcs: ["in1"], |
| 408 | cmd: "echo $(foo) > $(out)", |
| 409 | `, |
| 410 | err: `unknown variable '$(foo)'`, |
| 411 | }, |
| 412 | { |
| 413 | name: "error depfile", |
| 414 | prop: ` |
| 415 | out: ["out"], |
| 416 | cmd: "echo foo > $(out) && touch $(depfile)", |
| 417 | `, |
| 418 | err: "$(depfile) used without depfile property", |
| 419 | }, |
| 420 | { |
| 421 | name: "error no depfile", |
| 422 | prop: ` |
| 423 | out: ["out"], |
| 424 | depfile: true, |
| 425 | cmd: "echo foo > $(out)", |
| 426 | `, |
| 427 | err: "specified depfile=true but did not include a reference to '${depfile}' in cmd", |
| 428 | }, |
| 429 | { |
| 430 | name: "error no out", |
| 431 | prop: ` |
| 432 | cmd: "echo foo > $(out)", |
| 433 | `, |
| 434 | err: "must have at least one output file", |
| 435 | }, |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 436 | { |
| 437 | name: "srcs allow missing dependencies", |
| 438 | prop: ` |
| 439 | srcs: [":missing"], |
| 440 | out: ["out"], |
| 441 | cmd: "cat $(location :missing) > $(out)", |
| 442 | `, |
| 443 | |
| 444 | allowMissingDependencies: true, |
| 445 | |
| 446 | expect: "cat ***missing srcs :missing*** > __SBOX_OUT_FILES__", |
| 447 | }, |
| 448 | { |
| 449 | name: "tool allow missing dependencies", |
| 450 | prop: ` |
| 451 | tools: [":missing"], |
| 452 | out: ["out"], |
| 453 | cmd: "$(location :missing) > $(out)", |
| 454 | `, |
| 455 | |
| 456 | allowMissingDependencies: true, |
| 457 | |
| 458 | expect: "***missing tool :missing*** > __SBOX_OUT_FILES__", |
| 459 | }, |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | for _, test := range testcases { |
| 463 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 464 | bp := "genrule {\n" |
| 465 | bp += "name: \"gen\",\n" |
| 466 | bp += test.prop |
| 467 | bp += "}\n" |
| 468 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 469 | config := testConfig(bp, nil) |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 470 | config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(test.allowMissingDependencies) |
| 471 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 472 | ctx := testContext(config) |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 473 | ctx.SetAllowMissingDependencies(test.allowMissingDependencies) |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 474 | |
| 475 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 476 | if errs == nil { |
| 477 | _, errs = ctx.PrepareBuildActions(config) |
| 478 | } |
| 479 | if errs == nil && test.err != "" { |
| 480 | t.Fatalf("want error %q, got no error", test.err) |
| 481 | } else if errs != nil && test.err == "" { |
| 482 | android.FailIfErrored(t, errs) |
| 483 | } else if test.err != "" { |
| 484 | if len(errs) != 1 { |
| 485 | t.Errorf("want 1 error, got %d errors:", len(errs)) |
| 486 | for _, err := range errs { |
| 487 | t.Errorf(" %s", err.Error()) |
| 488 | } |
| 489 | t.FailNow() |
| 490 | } |
| 491 | if !strings.Contains(errs[0].Error(), test.err) { |
| 492 | t.Fatalf("want %q, got %q", test.err, errs[0].Error()) |
| 493 | } |
| 494 | return |
| 495 | } |
| 496 | |
| 497 | gen := ctx.ModuleForTests("gen", "").Module().(*Module) |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 498 | if g, w := gen.rawCommands[0], "'"+test.expect+"'"; w != g { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 499 | t.Errorf("want %q, got %q", w, g) |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 500 | } |
| 501 | }) |
| 502 | } |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Bill Peckham | c087be1 | 2020-02-13 15:55:10 -0800 | [diff] [blame] | 505 | func TestGenruleHashInputs(t *testing.T) { |
| 506 | |
| 507 | // The basic idea here is to verify that the sbox command (which is |
| 508 | // in the Command field of the generate rule) contains a hash of the |
| 509 | // inputs, but only if $(in) is not referenced in the genrule cmd |
| 510 | // property. |
| 511 | |
| 512 | // By including a hash of the inputs, we cause the rule to re-run if |
| 513 | // the list of inputs changes (because the sbox command changes). |
| 514 | |
| 515 | // However, if the genrule cmd property already contains $(in), then |
| 516 | // the dependency is already expressed, so we don't need to include the |
| 517 | // hash in that case. |
| 518 | |
| 519 | bp := ` |
| 520 | genrule { |
| 521 | name: "hash0", |
| 522 | srcs: ["in1.txt", "in2.txt"], |
| 523 | out: ["out"], |
| 524 | cmd: "echo foo > $(out)", |
| 525 | } |
| 526 | genrule { |
| 527 | name: "hash1", |
| 528 | srcs: ["*.txt"], |
| 529 | out: ["out"], |
| 530 | cmd: "echo bar > $(out)", |
| 531 | } |
| 532 | genrule { |
| 533 | name: "hash2", |
| 534 | srcs: ["*.txt"], |
| 535 | out: ["out"], |
| 536 | cmd: "echo $(in) > $(out)", |
| 537 | } |
| 538 | ` |
| 539 | testcases := []struct { |
| 540 | name string |
| 541 | expectedHash string |
| 542 | }{ |
| 543 | { |
| 544 | name: "hash0", |
| 545 | // sha256 value obtained from: echo -n 'in1.txtin2.txt' | sha256sum |
| 546 | expectedHash: "031097e11e0a8c822c960eb9742474f46336360a515744000d086d94335a9cb9", |
| 547 | }, |
| 548 | { |
| 549 | name: "hash1", |
| 550 | // sha256 value obtained from: echo -n 'in1.txtin2.txtin3.txt' | sha256sum |
| 551 | expectedHash: "de5d22a4a7ab50d250cc59fcdf7a7e0775790d270bfca3a7a9e1f18a70dd996c", |
| 552 | }, |
| 553 | { |
| 554 | name: "hash2", |
| 555 | // $(in) is present, option should not appear |
| 556 | expectedHash: "", |
| 557 | }, |
| 558 | } |
| 559 | |
| 560 | config := testConfig(bp, nil) |
| 561 | ctx := testContext(config) |
| 562 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 563 | if errs == nil { |
| 564 | _, errs = ctx.PrepareBuildActions(config) |
| 565 | } |
| 566 | if errs != nil { |
| 567 | t.Fatal(errs) |
| 568 | } |
| 569 | |
| 570 | for _, test := range testcases { |
| 571 | t.Run(test.name, func(t *testing.T) { |
| 572 | gen := ctx.ModuleForTests(test.name, "") |
| 573 | command := gen.Rule("generator").RuleParams.Command |
| 574 | |
| 575 | if len(test.expectedHash) > 0 { |
| 576 | // We add spaces before and after to make sure that |
| 577 | // this option doesn't abutt another sbox option. |
| 578 | expectedInputHashOption := " --input-hash " + test.expectedHash + " " |
| 579 | |
| 580 | if !strings.Contains(command, expectedInputHashOption) { |
| 581 | t.Errorf("Expected command \"%s\" to contain \"%s\"", command, expectedInputHashOption) |
| 582 | } |
| 583 | } else { |
| 584 | if strings.Contains(command, "--input-hash") { |
| 585 | t.Errorf("Unexpected \"--input-hash\" found in command: \"%s\"", command) |
| 586 | } |
| 587 | } |
| 588 | }) |
| 589 | } |
| 590 | } |
| 591 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 592 | func TestGenSrcs(t *testing.T) { |
| 593 | testcases := []struct { |
| 594 | name string |
| 595 | prop string |
| 596 | |
| 597 | allowMissingDependencies bool |
| 598 | |
| 599 | err string |
| 600 | cmds []string |
| 601 | deps []string |
| 602 | files []string |
| 603 | }{ |
| 604 | { |
| 605 | name: "gensrcs", |
| 606 | prop: ` |
| 607 | tools: ["tool"], |
| 608 | srcs: ["in1.txt", "in2.txt"], |
| 609 | cmd: "$(location) $(in) > $(out)", |
| 610 | `, |
| 611 | cmds: []string{ |
| 612 | "'bash -c '\\''out/tool in1.txt > __SBOX_OUT_DIR__/in1.h'\\'' && bash -c '\\''out/tool in2.txt > __SBOX_OUT_DIR__/in2.h'\\'''", |
| 613 | }, |
| 614 | deps: []string{buildDir + "/.intermediates/gen/gen/gensrcs/in1.h", buildDir + "/.intermediates/gen/gen/gensrcs/in2.h"}, |
| 615 | files: []string{buildDir + "/.intermediates/gen/gen/gensrcs/in1.h", buildDir + "/.intermediates/gen/gen/gensrcs/in2.h"}, |
| 616 | }, |
| 617 | { |
| 618 | name: "shards", |
| 619 | prop: ` |
| 620 | tools: ["tool"], |
| 621 | srcs: ["in1.txt", "in2.txt", "in3.txt"], |
| 622 | cmd: "$(location) $(in) > $(out)", |
| 623 | shard_size: 2, |
| 624 | `, |
| 625 | cmds: []string{ |
| 626 | "'bash -c '\\''out/tool in1.txt > __SBOX_OUT_DIR__/in1.h'\\'' && bash -c '\\''out/tool in2.txt > __SBOX_OUT_DIR__/in2.h'\\'''", |
| 627 | "'bash -c '\\''out/tool in3.txt > __SBOX_OUT_DIR__/in3.h'\\'''", |
| 628 | }, |
| 629 | deps: []string{buildDir + "/.intermediates/gen/gen/gensrcs/in1.h", buildDir + "/.intermediates/gen/gen/gensrcs/in2.h", buildDir + "/.intermediates/gen/gen/gensrcs/in3.h"}, |
| 630 | files: []string{buildDir + "/.intermediates/gen/gen/gensrcs/in1.h", buildDir + "/.intermediates/gen/gen/gensrcs/in2.h", buildDir + "/.intermediates/gen/gen/gensrcs/in3.h"}, |
| 631 | }, |
| 632 | } |
| 633 | |
| 634 | for _, test := range testcases { |
| 635 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 636 | bp := "gensrcs {\n" |
| 637 | bp += `name: "gen",` + "\n" |
| 638 | bp += `output_extension: "h",` + "\n" |
| 639 | bp += test.prop |
| 640 | bp += "}\n" |
| 641 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 642 | config := testConfig(bp, nil) |
| 643 | ctx := testContext(config) |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 644 | |
| 645 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 646 | if errs == nil { |
| 647 | _, errs = ctx.PrepareBuildActions(config) |
| 648 | } |
| 649 | if errs == nil && test.err != "" { |
| 650 | t.Fatalf("want error %q, got no error", test.err) |
| 651 | } else if errs != nil && test.err == "" { |
| 652 | android.FailIfErrored(t, errs) |
| 653 | } else if test.err != "" { |
| 654 | if len(errs) != 1 { |
| 655 | t.Errorf("want 1 error, got %d errors:", len(errs)) |
| 656 | for _, err := range errs { |
| 657 | t.Errorf(" %s", err.Error()) |
| 658 | } |
| 659 | t.FailNow() |
| 660 | } |
| 661 | if !strings.Contains(errs[0].Error(), test.err) { |
| 662 | t.Fatalf("want %q, got %q", test.err, errs[0].Error()) |
| 663 | } |
| 664 | return |
| 665 | } |
| 666 | |
| 667 | gen := ctx.ModuleForTests("gen", "").Module().(*Module) |
| 668 | if g, w := gen.rawCommands, test.cmds; !reflect.DeepEqual(w, g) { |
| 669 | t.Errorf("want %q, got %q", w, g) |
| 670 | } |
| 671 | |
| 672 | if g, w := gen.outputDeps.Strings(), test.deps; !reflect.DeepEqual(w, g) { |
| 673 | t.Errorf("want deps %q, got %q", w, g) |
| 674 | } |
| 675 | |
| 676 | if g, w := gen.outputFiles.Strings(), test.files; !reflect.DeepEqual(w, g) { |
| 677 | t.Errorf("want files %q, got %q", w, g) |
| 678 | } |
| 679 | }) |
| 680 | } |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 681 | |
| 682 | } |
| 683 | |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 684 | func TestGenruleDefaults(t *testing.T) { |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 685 | bp := ` |
| 686 | genrule_defaults { |
| 687 | name: "gen_defaults1", |
| 688 | cmd: "cp $(in) $(out)", |
| 689 | } |
| 690 | |
| 691 | genrule_defaults { |
| 692 | name: "gen_defaults2", |
| 693 | srcs: ["in1"], |
| 694 | } |
| 695 | |
| 696 | genrule { |
| 697 | name: "gen", |
| 698 | out: ["out"], |
| 699 | defaults: ["gen_defaults1", "gen_defaults2"], |
| 700 | } |
| 701 | ` |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 702 | config := testConfig(bp, nil) |
| 703 | ctx := testContext(config) |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 704 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 705 | if errs == nil { |
| 706 | _, errs = ctx.PrepareBuildActions(config) |
| 707 | } |
| 708 | if errs != nil { |
| 709 | t.Fatal(errs) |
| 710 | } |
| 711 | gen := ctx.ModuleForTests("gen", "").Module().(*Module) |
| 712 | |
| 713 | expectedCmd := "'cp ${in} __SBOX_OUT_FILES__'" |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 714 | if gen.rawCommands[0] != expectedCmd { |
| 715 | t.Errorf("Expected cmd: %q, actual: %q", expectedCmd, gen.rawCommands[0]) |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | expectedSrcs := []string{"in1"} |
| 719 | if !reflect.DeepEqual(expectedSrcs, gen.properties.Srcs) { |
| 720 | t.Errorf("Expected srcs: %q, actual: %q", expectedSrcs, gen.properties.Srcs) |
| 721 | } |
| 722 | } |
| 723 | |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 724 | type testTool struct { |
| 725 | android.ModuleBase |
| 726 | outputFile android.Path |
| 727 | } |
| 728 | |
| 729 | func toolFactory() android.Module { |
| 730 | module := &testTool{} |
| 731 | android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst) |
| 732 | return module |
| 733 | } |
| 734 | |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 735 | func (t *testTool) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 736 | t.outputFile = android.PathForTesting("out", ctx.ModuleName()) |
| 737 | } |
| 738 | |
| 739 | func (t *testTool) HostToolPath() android.OptionalPath { |
| 740 | return android.OptionalPathForPath(t.outputFile) |
| 741 | } |
| 742 | |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 743 | var _ android.HostToolProvider = (*testTool)(nil) |