Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1 | // Copyright 2020 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 bp2build |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 19 | "android/soong/genrule" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | func TestGenerateSoongModuleTargets(t *testing.T) { |
| 24 | testCases := []struct { |
| 25 | bp string |
| 26 | expectedBazelTarget string |
| 27 | }{ |
| 28 | { |
| 29 | bp: `custom { |
| 30 | name: "foo", |
| 31 | } |
| 32 | `, |
| 33 | expectedBazelTarget: `soong_module( |
| 34 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 35 | soong_module_name = "foo", |
| 36 | soong_module_type = "custom", |
| 37 | soong_module_variant = "", |
| 38 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 39 | ], |
| 40 | )`, |
| 41 | }, |
| 42 | { |
| 43 | bp: `custom { |
| 44 | name: "foo", |
| 45 | ramdisk: true, |
| 46 | } |
| 47 | `, |
| 48 | expectedBazelTarget: `soong_module( |
| 49 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 50 | soong_module_name = "foo", |
| 51 | soong_module_type = "custom", |
| 52 | soong_module_variant = "", |
| 53 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 54 | ], |
| 55 | ramdisk = True, |
| 56 | )`, |
| 57 | }, |
| 58 | { |
| 59 | bp: `custom { |
| 60 | name: "foo", |
| 61 | owner: "a_string_with\"quotes\"_and_\\backslashes\\\\", |
| 62 | } |
| 63 | `, |
| 64 | expectedBazelTarget: `soong_module( |
| 65 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 66 | soong_module_name = "foo", |
| 67 | soong_module_type = "custom", |
| 68 | soong_module_variant = "", |
| 69 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 70 | ], |
| 71 | owner = "a_string_with\"quotes\"_and_\\backslashes\\\\", |
| 72 | )`, |
| 73 | }, |
| 74 | { |
| 75 | bp: `custom { |
| 76 | name: "foo", |
| 77 | required: ["bar"], |
| 78 | } |
| 79 | `, |
| 80 | expectedBazelTarget: `soong_module( |
| 81 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 82 | soong_module_name = "foo", |
| 83 | soong_module_type = "custom", |
| 84 | soong_module_variant = "", |
| 85 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 86 | ], |
| 87 | required = [ |
| 88 | "bar", |
| 89 | ], |
| 90 | )`, |
| 91 | }, |
| 92 | { |
| 93 | bp: `custom { |
| 94 | name: "foo", |
| 95 | target_required: ["qux", "bazqux"], |
| 96 | } |
| 97 | `, |
| 98 | expectedBazelTarget: `soong_module( |
| 99 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 100 | soong_module_name = "foo", |
| 101 | soong_module_type = "custom", |
| 102 | soong_module_variant = "", |
| 103 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 104 | ], |
| 105 | target_required = [ |
| 106 | "qux", |
| 107 | "bazqux", |
| 108 | ], |
| 109 | )`, |
| 110 | }, |
| 111 | { |
| 112 | bp: `custom { |
| 113 | name: "foo", |
| 114 | dist: { |
| 115 | targets: ["goal_foo"], |
| 116 | tag: ".foo", |
| 117 | }, |
| 118 | dists: [ |
| 119 | { |
| 120 | targets: ["goal_bar"], |
| 121 | tag: ".bar", |
| 122 | }, |
| 123 | ], |
| 124 | } |
| 125 | `, |
| 126 | expectedBazelTarget: `soong_module( |
| 127 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 128 | soong_module_name = "foo", |
| 129 | soong_module_type = "custom", |
| 130 | soong_module_variant = "", |
| 131 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 132 | ], |
| 133 | dist = { |
| 134 | "tag": ".foo", |
| 135 | "targets": [ |
| 136 | "goal_foo", |
| 137 | ], |
| 138 | }, |
| 139 | dists = [ |
| 140 | { |
| 141 | "tag": ".bar", |
| 142 | "targets": [ |
| 143 | "goal_bar", |
| 144 | ], |
| 145 | }, |
| 146 | ], |
| 147 | )`, |
| 148 | }, |
| 149 | { |
| 150 | bp: `custom { |
| 151 | name: "foo", |
| 152 | required: ["bar"], |
| 153 | target_required: ["qux", "bazqux"], |
| 154 | ramdisk: true, |
| 155 | owner: "custom_owner", |
| 156 | dists: [ |
| 157 | { |
| 158 | tag: ".tag", |
| 159 | targets: ["my_goal"], |
| 160 | }, |
| 161 | ], |
| 162 | } |
| 163 | `, |
| 164 | expectedBazelTarget: `soong_module( |
| 165 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 166 | soong_module_name = "foo", |
| 167 | soong_module_type = "custom", |
| 168 | soong_module_variant = "", |
| 169 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 170 | ], |
| 171 | dists = [ |
| 172 | { |
| 173 | "tag": ".tag", |
| 174 | "targets": [ |
| 175 | "my_goal", |
| 176 | ], |
| 177 | }, |
| 178 | ], |
| 179 | owner = "custom_owner", |
| 180 | ramdisk = True, |
| 181 | required = [ |
| 182 | "bar", |
| 183 | ], |
| 184 | target_required = [ |
| 185 | "qux", |
| 186 | "bazqux", |
| 187 | ], |
| 188 | )`, |
| 189 | }, |
| 190 | } |
| 191 | |
| 192 | dir := "." |
| 193 | for _, testCase := range testCases { |
| 194 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 195 | ctx := android.NewTestContext(config) |
| 196 | ctx.RegisterModuleType("custom", customModuleFactory) |
| 197 | ctx.Register() |
| 198 | |
| 199 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 200 | android.FailIfErrored(t, errs) |
| 201 | _, errs = ctx.PrepareBuildActions(config) |
| 202 | android.FailIfErrored(t, errs) |
| 203 | |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 204 | bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, QueryView)[dir] |
Jingwen Chen | 4e4756d | 2021-01-24 21:13:13 -0500 | [diff] [blame] | 205 | if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount { |
| 206 | t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | actualBazelTarget := bazelTargets[0] |
| 210 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 211 | t.Errorf( |
| 212 | "Expected generated Bazel target to be '%s', got '%s'", |
| 213 | testCase.expectedBazelTarget, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 214 | actualBazelTarget.content, |
| 215 | ) |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | func TestGenerateBazelTargetModules(t *testing.T) { |
| 221 | testCases := []struct { |
| 222 | bp string |
| 223 | expectedBazelTarget string |
| 224 | }{ |
| 225 | { |
| 226 | bp: `custom { |
| 227 | name: "foo", |
| 228 | string_list_prop: ["a", "b"], |
| 229 | string_prop: "a", |
| 230 | }`, |
| 231 | expectedBazelTarget: `custom( |
| 232 | name = "foo", |
| 233 | string_list_prop = [ |
| 234 | "a", |
| 235 | "b", |
| 236 | ], |
| 237 | string_prop = "a", |
| 238 | )`, |
| 239 | }, |
| 240 | } |
| 241 | |
| 242 | dir := "." |
| 243 | for _, testCase := range testCases { |
| 244 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 245 | ctx := android.NewTestContext(config) |
| 246 | ctx.RegisterModuleType("custom", customModuleFactory) |
| 247 | ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator) |
| 248 | ctx.RegisterForBazelConversion() |
| 249 | |
| 250 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 251 | android.FailIfErrored(t, errs) |
| 252 | _, errs = ctx.ResolveDependencies(config) |
| 253 | android.FailIfErrored(t, errs) |
| 254 | |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 255 | bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, Bp2Build)[dir] |
Jingwen Chen | 4e4756d | 2021-01-24 21:13:13 -0500 | [diff] [blame] | 256 | if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount { |
| 257 | t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | actualBazelTarget := bazelTargets[0] |
| 261 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 262 | t.Errorf( |
| 263 | "Expected generated Bazel target to be '%s', got '%s'", |
| 264 | testCase.expectedBazelTarget, |
| 265 | actualBazelTarget.content, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 266 | ) |
| 267 | } |
| 268 | } |
| 269 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 270 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame^] | 271 | func TestLoadStatements(t *testing.T) { |
| 272 | testCases := []struct { |
| 273 | bazelTargets BazelTargets |
| 274 | expectedLoadStatements string |
| 275 | }{ |
| 276 | { |
| 277 | bazelTargets: BazelTargets{ |
| 278 | BazelTarget{ |
| 279 | name: "foo", |
| 280 | ruleClass: "cc_library", |
| 281 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 282 | }, |
| 283 | }, |
| 284 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 285 | }, |
| 286 | { |
| 287 | bazelTargets: BazelTargets{ |
| 288 | BazelTarget{ |
| 289 | name: "foo", |
| 290 | ruleClass: "cc_library", |
| 291 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 292 | }, |
| 293 | BazelTarget{ |
| 294 | name: "bar", |
| 295 | ruleClass: "cc_library", |
| 296 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 297 | }, |
| 298 | }, |
| 299 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 300 | }, |
| 301 | { |
| 302 | bazelTargets: BazelTargets{ |
| 303 | BazelTarget{ |
| 304 | name: "foo", |
| 305 | ruleClass: "cc_library", |
| 306 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 307 | }, |
| 308 | BazelTarget{ |
| 309 | name: "bar", |
| 310 | ruleClass: "cc_binary", |
| 311 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 312 | }, |
| 313 | }, |
| 314 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`, |
| 315 | }, |
| 316 | { |
| 317 | bazelTargets: BazelTargets{ |
| 318 | BazelTarget{ |
| 319 | name: "foo", |
| 320 | ruleClass: "cc_library", |
| 321 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 322 | }, |
| 323 | BazelTarget{ |
| 324 | name: "bar", |
| 325 | ruleClass: "cc_binary", |
| 326 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 327 | }, |
| 328 | BazelTarget{ |
| 329 | name: "baz", |
| 330 | ruleClass: "java_binary", |
| 331 | bzlLoadLocation: "//build/bazel/rules:java.bzl", |
| 332 | }, |
| 333 | }, |
| 334 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library") |
| 335 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 336 | }, |
| 337 | { |
| 338 | bazelTargets: BazelTargets{ |
| 339 | BazelTarget{ |
| 340 | name: "foo", |
| 341 | ruleClass: "cc_binary", |
| 342 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 343 | }, |
| 344 | BazelTarget{ |
| 345 | name: "bar", |
| 346 | ruleClass: "java_binary", |
| 347 | bzlLoadLocation: "//build/bazel/rules:java.bzl", |
| 348 | }, |
| 349 | BazelTarget{ |
| 350 | name: "baz", |
| 351 | ruleClass: "genrule", |
| 352 | // Note: no bzlLoadLocation for native rules |
| 353 | }, |
| 354 | }, |
| 355 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary") |
| 356 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 357 | }, |
| 358 | } |
| 359 | |
| 360 | for _, testCase := range testCases { |
| 361 | actual := testCase.bazelTargets.LoadStatements() |
| 362 | expected := testCase.expectedLoadStatements |
| 363 | if actual != expected { |
| 364 | t.Fatalf("Expected load statements to be %s, got %s", expected, actual) |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | } |
| 369 | |
| 370 | func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) { |
| 371 | testCases := []struct { |
| 372 | bp string |
| 373 | expectedBazelTarget string |
| 374 | expectedBazelTargetCount int |
| 375 | expectedLoadStatements string |
| 376 | }{ |
| 377 | { |
| 378 | bp: `custom { |
| 379 | name: "bar", |
| 380 | }`, |
| 381 | expectedBazelTarget: `my_library( |
| 382 | name = "bar", |
| 383 | ) |
| 384 | |
| 385 | my_proto_library( |
| 386 | name = "bar_my_proto_library_deps", |
| 387 | ) |
| 388 | |
| 389 | proto_library( |
| 390 | name = "bar_proto_library_deps", |
| 391 | )`, |
| 392 | expectedBazelTargetCount: 3, |
| 393 | expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library") |
| 394 | load("//build/bazel/rules:rules.bzl", "my_library")`, |
| 395 | }, |
| 396 | } |
| 397 | |
| 398 | dir := "." |
| 399 | for _, testCase := range testCases { |
| 400 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 401 | ctx := android.NewTestContext(config) |
| 402 | ctx.RegisterModuleType("custom", customModuleFactory) |
| 403 | ctx.RegisterBp2BuildMutator("custom_starlark", customBp2BuildMutatorFromStarlark) |
| 404 | ctx.RegisterForBazelConversion() |
| 405 | |
| 406 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 407 | android.FailIfErrored(t, errs) |
| 408 | _, errs = ctx.ResolveDependencies(config) |
| 409 | android.FailIfErrored(t, errs) |
| 410 | |
| 411 | bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, Bp2Build)[dir] |
| 412 | if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount { |
| 413 | t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount) |
| 414 | } |
| 415 | |
| 416 | actualBazelTargets := bazelTargets.String() |
| 417 | if actualBazelTargets != testCase.expectedBazelTarget { |
| 418 | t.Errorf( |
| 419 | "Expected generated Bazel target to be '%s', got '%s'", |
| 420 | testCase.expectedBazelTarget, |
| 421 | actualBazelTargets, |
| 422 | ) |
| 423 | } |
| 424 | |
| 425 | actualLoadStatements := bazelTargets.LoadStatements() |
| 426 | if actualLoadStatements != testCase.expectedLoadStatements { |
| 427 | t.Errorf( |
| 428 | "Expected generated load statements to be '%s', got '%s'", |
| 429 | testCase.expectedLoadStatements, |
| 430 | actualLoadStatements, |
| 431 | ) |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 436 | func TestModuleTypeBp2Build(t *testing.T) { |
| 437 | testCases := []struct { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 438 | moduleTypeUnderTest string |
| 439 | moduleTypeUnderTestFactory android.ModuleFactory |
| 440 | moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext) |
| 441 | bp string |
| 442 | expectedBazelTarget string |
| 443 | description string |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 444 | }{ |
| 445 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 446 | description: "filegroup with no srcs", |
| 447 | moduleTypeUnderTest: "filegroup", |
| 448 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 449 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 450 | bp: `filegroup { |
| 451 | name: "foo", |
| 452 | srcs: [], |
| 453 | }`, |
| 454 | expectedBazelTarget: `filegroup( |
| 455 | name = "foo", |
| 456 | srcs = [ |
| 457 | ], |
| 458 | )`, |
| 459 | }, |
| 460 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 461 | description: "filegroup with srcs", |
| 462 | moduleTypeUnderTest: "filegroup", |
| 463 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 464 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 465 | bp: `filegroup { |
| 466 | name: "foo", |
| 467 | srcs: ["a", "b"], |
| 468 | }`, |
| 469 | expectedBazelTarget: `filegroup( |
| 470 | name = "foo", |
| 471 | srcs = [ |
| 472 | "a", |
| 473 | "b", |
| 474 | ], |
| 475 | )`, |
| 476 | }, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 477 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 478 | description: "genrule with command line variable replacements", |
| 479 | moduleTypeUnderTest: "genrule", |
| 480 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 481 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 482 | bp: `genrule { |
| 483 | name: "foo", |
| 484 | out: ["foo.out"], |
| 485 | srcs: ["foo.in"], |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 486 | tools: [":foo.tool"], |
| 487 | cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 488 | }`, |
| 489 | expectedBazelTarget: `genrule( |
| 490 | name = "foo", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 491 | cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 492 | outs = [ |
| 493 | "foo.out", |
| 494 | ], |
| 495 | srcs = [ |
| 496 | "foo.in", |
| 497 | ], |
| 498 | tools = [ |
| 499 | ":foo.tool", |
| 500 | ], |
| 501 | )`, |
| 502 | }, |
| 503 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 504 | description: "genrule using $(locations :label)", |
| 505 | moduleTypeUnderTest: "genrule", |
| 506 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 507 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 508 | bp: `genrule { |
| 509 | name: "foo", |
| 510 | out: ["foo.out"], |
| 511 | srcs: ["foo.in"], |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 512 | tools: [":foo.tools"], |
| 513 | cmd: "$(locations :foo.tools) -s $(out) $(in)", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 514 | }`, |
| 515 | expectedBazelTarget: `genrule( |
| 516 | name = "foo", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 517 | cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)", |
| 518 | outs = [ |
| 519 | "foo.out", |
| 520 | ], |
| 521 | srcs = [ |
| 522 | "foo.in", |
| 523 | ], |
| 524 | tools = [ |
| 525 | ":foo.tools", |
| 526 | ], |
| 527 | )`, |
| 528 | }, |
| 529 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 530 | description: "genrule using $(location) label should substitute first tool label automatically", |
| 531 | moduleTypeUnderTest: "genrule", |
| 532 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 533 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 534 | bp: `genrule { |
| 535 | name: "foo", |
| 536 | out: ["foo.out"], |
| 537 | srcs: ["foo.in"], |
| 538 | tool_files: [":foo.tool", ":other.tool"], |
| 539 | cmd: "$(location) -s $(out) $(in)", |
| 540 | }`, |
| 541 | expectedBazelTarget: `genrule( |
| 542 | name = "foo", |
| 543 | cmd = "$(location :foo.tool) -s $(OUTS) $(SRCS)", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 544 | outs = [ |
| 545 | "foo.out", |
| 546 | ], |
| 547 | srcs = [ |
| 548 | "foo.in", |
| 549 | ], |
| 550 | tools = [ |
| 551 | ":foo.tool", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 552 | ":other.tool", |
| 553 | ], |
| 554 | )`, |
| 555 | }, |
| 556 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 557 | description: "genrule using $(locations) label should substitute first tool label automatically", |
| 558 | moduleTypeUnderTest: "genrule", |
| 559 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 560 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 561 | bp: `genrule { |
| 562 | name: "foo", |
| 563 | out: ["foo.out"], |
| 564 | srcs: ["foo.in"], |
| 565 | tools: [":foo.tool", ":other.tool"], |
| 566 | cmd: "$(locations) -s $(out) $(in)", |
| 567 | }`, |
| 568 | expectedBazelTarget: `genrule( |
| 569 | name = "foo", |
| 570 | cmd = "$(locations :foo.tool) -s $(OUTS) $(SRCS)", |
| 571 | outs = [ |
| 572 | "foo.out", |
| 573 | ], |
| 574 | srcs = [ |
| 575 | "foo.in", |
| 576 | ], |
| 577 | tools = [ |
| 578 | ":foo.tool", |
| 579 | ":other.tool", |
| 580 | ], |
| 581 | )`, |
| 582 | }, |
| 583 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 584 | description: "genrule without tools or tool_files can convert successfully", |
| 585 | moduleTypeUnderTest: "genrule", |
| 586 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 587 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 588 | bp: `genrule { |
| 589 | name: "foo", |
| 590 | out: ["foo.out"], |
| 591 | srcs: ["foo.in"], |
| 592 | cmd: "cp $(in) $(out)", |
| 593 | }`, |
| 594 | expectedBazelTarget: `genrule( |
| 595 | name = "foo", |
| 596 | cmd = "cp $(SRCS) $(OUTS)", |
| 597 | outs = [ |
| 598 | "foo.out", |
| 599 | ], |
| 600 | srcs = [ |
| 601 | "foo.in", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 602 | ], |
| 603 | )`, |
| 604 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | dir := "." |
| 608 | for _, testCase := range testCases { |
| 609 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 610 | ctx := android.NewTestContext(config) |
| 611 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 612 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 613 | ctx.RegisterForBazelConversion() |
| 614 | |
| 615 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 616 | android.FailIfErrored(t, errs) |
| 617 | _, errs = ctx.ResolveDependencies(config) |
| 618 | android.FailIfErrored(t, errs) |
| 619 | |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 620 | bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, Bp2Build)[dir] |
Jingwen Chen | 4e4756d | 2021-01-24 21:13:13 -0500 | [diff] [blame] | 621 | if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount { |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 622 | t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | actualBazelTarget := bazelTargets[0] |
| 626 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 627 | t.Errorf( |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 628 | "%s: Expected generated Bazel target to be '%s', got '%s'", |
| 629 | testCase.description, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 630 | testCase.expectedBazelTarget, |
| 631 | actualBazelTarget.content, |
| 632 | ) |
| 633 | } |
| 634 | } |
| 635 | } |