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 | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 20 | "strings" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | func TestGenerateSoongModuleTargets(t *testing.T) { |
| 25 | testCases := []struct { |
| 26 | bp string |
| 27 | expectedBazelTarget string |
| 28 | }{ |
| 29 | { |
| 30 | bp: `custom { |
| 31 | name: "foo", |
| 32 | } |
| 33 | `, |
| 34 | expectedBazelTarget: `soong_module( |
| 35 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 36 | soong_module_name = "foo", |
| 37 | soong_module_type = "custom", |
| 38 | soong_module_variant = "", |
| 39 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 40 | ], |
| 41 | )`, |
| 42 | }, |
| 43 | { |
| 44 | bp: `custom { |
| 45 | name: "foo", |
| 46 | ramdisk: true, |
| 47 | } |
| 48 | `, |
| 49 | expectedBazelTarget: `soong_module( |
| 50 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 51 | soong_module_name = "foo", |
| 52 | soong_module_type = "custom", |
| 53 | soong_module_variant = "", |
| 54 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 55 | ], |
| 56 | ramdisk = True, |
| 57 | )`, |
| 58 | }, |
| 59 | { |
| 60 | bp: `custom { |
| 61 | name: "foo", |
| 62 | owner: "a_string_with\"quotes\"_and_\\backslashes\\\\", |
| 63 | } |
| 64 | `, |
| 65 | expectedBazelTarget: `soong_module( |
| 66 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 67 | soong_module_name = "foo", |
| 68 | soong_module_type = "custom", |
| 69 | soong_module_variant = "", |
| 70 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 71 | ], |
| 72 | owner = "a_string_with\"quotes\"_and_\\backslashes\\\\", |
| 73 | )`, |
| 74 | }, |
| 75 | { |
| 76 | bp: `custom { |
| 77 | name: "foo", |
| 78 | required: ["bar"], |
| 79 | } |
| 80 | `, |
| 81 | expectedBazelTarget: `soong_module( |
| 82 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 83 | soong_module_name = "foo", |
| 84 | soong_module_type = "custom", |
| 85 | soong_module_variant = "", |
| 86 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 87 | ], |
| 88 | required = [ |
| 89 | "bar", |
| 90 | ], |
| 91 | )`, |
| 92 | }, |
| 93 | { |
| 94 | bp: `custom { |
| 95 | name: "foo", |
| 96 | target_required: ["qux", "bazqux"], |
| 97 | } |
| 98 | `, |
| 99 | expectedBazelTarget: `soong_module( |
| 100 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 101 | soong_module_name = "foo", |
| 102 | soong_module_type = "custom", |
| 103 | soong_module_variant = "", |
| 104 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 105 | ], |
| 106 | target_required = [ |
| 107 | "qux", |
| 108 | "bazqux", |
| 109 | ], |
| 110 | )`, |
| 111 | }, |
| 112 | { |
| 113 | bp: `custom { |
| 114 | name: "foo", |
| 115 | dist: { |
| 116 | targets: ["goal_foo"], |
| 117 | tag: ".foo", |
| 118 | }, |
| 119 | dists: [ |
| 120 | { |
| 121 | targets: ["goal_bar"], |
| 122 | tag: ".bar", |
| 123 | }, |
| 124 | ], |
| 125 | } |
| 126 | `, |
| 127 | expectedBazelTarget: `soong_module( |
| 128 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 129 | soong_module_name = "foo", |
| 130 | soong_module_type = "custom", |
| 131 | soong_module_variant = "", |
| 132 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 133 | ], |
| 134 | dist = { |
| 135 | "tag": ".foo", |
| 136 | "targets": [ |
| 137 | "goal_foo", |
| 138 | ], |
| 139 | }, |
| 140 | dists = [ |
| 141 | { |
| 142 | "tag": ".bar", |
| 143 | "targets": [ |
| 144 | "goal_bar", |
| 145 | ], |
| 146 | }, |
| 147 | ], |
| 148 | )`, |
| 149 | }, |
| 150 | { |
| 151 | bp: `custom { |
| 152 | name: "foo", |
| 153 | required: ["bar"], |
| 154 | target_required: ["qux", "bazqux"], |
| 155 | ramdisk: true, |
| 156 | owner: "custom_owner", |
| 157 | dists: [ |
| 158 | { |
| 159 | tag: ".tag", |
| 160 | targets: ["my_goal"], |
| 161 | }, |
| 162 | ], |
| 163 | } |
| 164 | `, |
| 165 | expectedBazelTarget: `soong_module( |
| 166 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 167 | soong_module_name = "foo", |
| 168 | soong_module_type = "custom", |
| 169 | soong_module_variant = "", |
| 170 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 171 | ], |
| 172 | dists = [ |
| 173 | { |
| 174 | "tag": ".tag", |
| 175 | "targets": [ |
| 176 | "my_goal", |
| 177 | ], |
| 178 | }, |
| 179 | ], |
| 180 | owner = "custom_owner", |
| 181 | ramdisk = True, |
| 182 | required = [ |
| 183 | "bar", |
| 184 | ], |
| 185 | target_required = [ |
| 186 | "qux", |
| 187 | "bazqux", |
| 188 | ], |
| 189 | )`, |
| 190 | }, |
| 191 | } |
| 192 | |
| 193 | dir := "." |
| 194 | for _, testCase := range testCases { |
| 195 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 196 | ctx := android.NewTestContext(config) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 197 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 198 | ctx.RegisterModuleType("custom", customModuleFactory) |
| 199 | ctx.Register() |
| 200 | |
| 201 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 202 | android.FailIfErrored(t, errs) |
| 203 | _, errs = ctx.PrepareBuildActions(config) |
| 204 | android.FailIfErrored(t, errs) |
| 205 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 206 | codegenCtx := NewCodegenContext(config, *ctx.Context, QueryView) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame^] | 207 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
Jingwen Chen | 4e4756d | 2021-01-24 21:13:13 -0500 | [diff] [blame] | 208 | if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount { |
| 209 | t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | actualBazelTarget := bazelTargets[0] |
| 213 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 214 | t.Errorf( |
| 215 | "Expected generated Bazel target to be '%s', got '%s'", |
| 216 | testCase.expectedBazelTarget, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 217 | actualBazelTarget.content, |
| 218 | ) |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | func TestGenerateBazelTargetModules(t *testing.T) { |
| 224 | testCases := []struct { |
| 225 | bp string |
| 226 | expectedBazelTarget string |
| 227 | }{ |
| 228 | { |
| 229 | bp: `custom { |
| 230 | name: "foo", |
| 231 | string_list_prop: ["a", "b"], |
| 232 | string_prop: "a", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 233 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 234 | }`, |
| 235 | expectedBazelTarget: `custom( |
| 236 | name = "foo", |
| 237 | string_list_prop = [ |
| 238 | "a", |
| 239 | "b", |
| 240 | ], |
| 241 | string_prop = "a", |
| 242 | )`, |
| 243 | }, |
| 244 | } |
| 245 | |
| 246 | dir := "." |
| 247 | for _, testCase := range testCases { |
| 248 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 249 | ctx := android.NewTestContext(config) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 250 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 251 | ctx.RegisterModuleType("custom", customModuleFactory) |
| 252 | ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator) |
| 253 | ctx.RegisterForBazelConversion() |
| 254 | |
| 255 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 256 | if Errored(t, "", errs) { |
| 257 | continue |
| 258 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 259 | _, errs = ctx.ResolveDependencies(config) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 260 | if Errored(t, "", errs) { |
| 261 | continue |
| 262 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 263 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 264 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame^] | 265 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 266 | |
Jingwen Chen | 4e4756d | 2021-01-24 21:13:13 -0500 | [diff] [blame] | 267 | if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 268 | t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount) |
| 269 | } else { |
| 270 | actualBazelTarget := bazelTargets[0] |
| 271 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 272 | t.Errorf( |
| 273 | "Expected generated Bazel target to be '%s', got '%s'", |
| 274 | testCase.expectedBazelTarget, |
| 275 | actualBazelTarget.content, |
| 276 | ) |
| 277 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 281 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 282 | func TestLoadStatements(t *testing.T) { |
| 283 | testCases := []struct { |
| 284 | bazelTargets BazelTargets |
| 285 | expectedLoadStatements string |
| 286 | }{ |
| 287 | { |
| 288 | bazelTargets: BazelTargets{ |
| 289 | BazelTarget{ |
| 290 | name: "foo", |
| 291 | ruleClass: "cc_library", |
| 292 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 293 | }, |
| 294 | }, |
| 295 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 296 | }, |
| 297 | { |
| 298 | bazelTargets: BazelTargets{ |
| 299 | BazelTarget{ |
| 300 | name: "foo", |
| 301 | ruleClass: "cc_library", |
| 302 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 303 | }, |
| 304 | BazelTarget{ |
| 305 | name: "bar", |
| 306 | ruleClass: "cc_library", |
| 307 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 308 | }, |
| 309 | }, |
| 310 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 311 | }, |
| 312 | { |
| 313 | bazelTargets: BazelTargets{ |
| 314 | BazelTarget{ |
| 315 | name: "foo", |
| 316 | ruleClass: "cc_library", |
| 317 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 318 | }, |
| 319 | BazelTarget{ |
| 320 | name: "bar", |
| 321 | ruleClass: "cc_binary", |
| 322 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 323 | }, |
| 324 | }, |
| 325 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`, |
| 326 | }, |
| 327 | { |
| 328 | bazelTargets: BazelTargets{ |
| 329 | BazelTarget{ |
| 330 | name: "foo", |
| 331 | ruleClass: "cc_library", |
| 332 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 333 | }, |
| 334 | BazelTarget{ |
| 335 | name: "bar", |
| 336 | ruleClass: "cc_binary", |
| 337 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 338 | }, |
| 339 | BazelTarget{ |
| 340 | name: "baz", |
| 341 | ruleClass: "java_binary", |
| 342 | bzlLoadLocation: "//build/bazel/rules:java.bzl", |
| 343 | }, |
| 344 | }, |
| 345 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library") |
| 346 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 347 | }, |
| 348 | { |
| 349 | bazelTargets: BazelTargets{ |
| 350 | BazelTarget{ |
| 351 | name: "foo", |
| 352 | ruleClass: "cc_binary", |
| 353 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 354 | }, |
| 355 | BazelTarget{ |
| 356 | name: "bar", |
| 357 | ruleClass: "java_binary", |
| 358 | bzlLoadLocation: "//build/bazel/rules:java.bzl", |
| 359 | }, |
| 360 | BazelTarget{ |
| 361 | name: "baz", |
| 362 | ruleClass: "genrule", |
| 363 | // Note: no bzlLoadLocation for native rules |
| 364 | }, |
| 365 | }, |
| 366 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary") |
| 367 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 368 | }, |
| 369 | } |
| 370 | |
| 371 | for _, testCase := range testCases { |
| 372 | actual := testCase.bazelTargets.LoadStatements() |
| 373 | expected := testCase.expectedLoadStatements |
| 374 | if actual != expected { |
| 375 | t.Fatalf("Expected load statements to be %s, got %s", expected, actual) |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | } |
| 380 | |
| 381 | func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) { |
| 382 | testCases := []struct { |
| 383 | bp string |
| 384 | expectedBazelTarget string |
| 385 | expectedBazelTargetCount int |
| 386 | expectedLoadStatements string |
| 387 | }{ |
| 388 | { |
| 389 | bp: `custom { |
| 390 | name: "bar", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 391 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 392 | }`, |
| 393 | expectedBazelTarget: `my_library( |
| 394 | name = "bar", |
| 395 | ) |
| 396 | |
| 397 | my_proto_library( |
| 398 | name = "bar_my_proto_library_deps", |
| 399 | ) |
| 400 | |
| 401 | proto_library( |
| 402 | name = "bar_proto_library_deps", |
| 403 | )`, |
| 404 | expectedBazelTargetCount: 3, |
| 405 | expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library") |
| 406 | load("//build/bazel/rules:rules.bzl", "my_library")`, |
| 407 | }, |
| 408 | } |
| 409 | |
| 410 | dir := "." |
| 411 | for _, testCase := range testCases { |
| 412 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 413 | ctx := android.NewTestContext(config) |
| 414 | ctx.RegisterModuleType("custom", customModuleFactory) |
| 415 | ctx.RegisterBp2BuildMutator("custom_starlark", customBp2BuildMutatorFromStarlark) |
| 416 | ctx.RegisterForBazelConversion() |
| 417 | |
| 418 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 419 | android.FailIfErrored(t, errs) |
| 420 | _, errs = ctx.ResolveDependencies(config) |
| 421 | android.FailIfErrored(t, errs) |
| 422 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 423 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame^] | 424 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 425 | if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount { |
| 426 | t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount) |
| 427 | } |
| 428 | |
| 429 | actualBazelTargets := bazelTargets.String() |
| 430 | if actualBazelTargets != testCase.expectedBazelTarget { |
| 431 | t.Errorf( |
| 432 | "Expected generated Bazel target to be '%s', got '%s'", |
| 433 | testCase.expectedBazelTarget, |
| 434 | actualBazelTargets, |
| 435 | ) |
| 436 | } |
| 437 | |
| 438 | actualLoadStatements := bazelTargets.LoadStatements() |
| 439 | if actualLoadStatements != testCase.expectedLoadStatements { |
| 440 | t.Errorf( |
| 441 | "Expected generated load statements to be '%s', got '%s'", |
| 442 | testCase.expectedLoadStatements, |
| 443 | actualLoadStatements, |
| 444 | ) |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 449 | func TestModuleTypeBp2Build(t *testing.T) { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 450 | otherGenruleBp := map[string]string{ |
| 451 | "other/Android.bp": `genrule { |
| 452 | name: "foo.tool", |
| 453 | out: ["foo_tool.out"], |
| 454 | srcs: ["foo_tool.in"], |
| 455 | cmd: "cp $(in) $(out)", |
| 456 | } |
| 457 | genrule { |
| 458 | name: "other.tool", |
| 459 | out: ["other_tool.out"], |
| 460 | srcs: ["other_tool.in"], |
| 461 | cmd: "cp $(in) $(out)", |
| 462 | }`, |
| 463 | } |
| 464 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 465 | testCases := []struct { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 466 | description string |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 467 | moduleTypeUnderTest string |
| 468 | moduleTypeUnderTestFactory android.ModuleFactory |
| 469 | moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 470 | preArchMutators []android.RegisterMutatorFunc |
| 471 | depsMutators []android.RegisterMutatorFunc |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 472 | bp string |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 473 | expectedBazelTargets []string |
| 474 | fs map[string]string |
| 475 | dir string |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 476 | }{ |
| 477 | { |
Liz Kammer | ebfcf67 | 2021-02-16 15:00:05 -0500 | [diff] [blame] | 478 | description: "filegroup with does not specify srcs", |
| 479 | moduleTypeUnderTest: "filegroup", |
| 480 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 481 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 482 | bp: `filegroup { |
| 483 | name: "fg_foo", |
| 484 | bazel_module: { bp2build_available: true }, |
| 485 | }`, |
| 486 | expectedBazelTargets: []string{ |
| 487 | `filegroup( |
| 488 | name = "fg_foo", |
| 489 | )`, |
| 490 | }, |
| 491 | }, |
| 492 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 493 | description: "filegroup with no srcs", |
| 494 | moduleTypeUnderTest: "filegroup", |
| 495 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 496 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 497 | bp: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 498 | name: "fg_foo", |
| 499 | srcs: [], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 500 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 501 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 502 | expectedBazelTargets: []string{ |
| 503 | `filegroup( |
| 504 | name = "fg_foo", |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 505 | srcs = [ |
| 506 | ], |
| 507 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 508 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 509 | }, |
| 510 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 511 | description: "filegroup with srcs", |
| 512 | moduleTypeUnderTest: "filegroup", |
| 513 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 514 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 515 | bp: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 516 | name: "fg_foo", |
| 517 | srcs: ["a", "b"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 518 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 519 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 520 | expectedBazelTargets: []string{`filegroup( |
| 521 | name = "fg_foo", |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 522 | srcs = [ |
| 523 | "a", |
| 524 | "b", |
| 525 | ], |
| 526 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 527 | }, |
| 528 | }, |
| 529 | { |
| 530 | description: "filegroup with excludes srcs", |
| 531 | moduleTypeUnderTest: "filegroup", |
| 532 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 533 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 534 | bp: `filegroup { |
| 535 | name: "fg_foo", |
| 536 | srcs: ["a", "b"], |
| 537 | exclude_srcs: ["a"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 538 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 539 | }`, |
| 540 | expectedBazelTargets: []string{`filegroup( |
| 541 | name = "fg_foo", |
| 542 | srcs = [ |
| 543 | "b", |
| 544 | ], |
| 545 | )`, |
| 546 | }, |
| 547 | }, |
| 548 | { |
| 549 | description: "filegroup with glob", |
| 550 | moduleTypeUnderTest: "filegroup", |
| 551 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 552 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 553 | bp: `filegroup { |
| 554 | name: "foo", |
| 555 | srcs: ["**/*.txt"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 556 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 557 | }`, |
| 558 | expectedBazelTargets: []string{`filegroup( |
| 559 | name = "foo", |
| 560 | srcs = [ |
| 561 | "other/a.txt", |
| 562 | "other/b.txt", |
| 563 | "other/subdir/a.txt", |
| 564 | ], |
| 565 | )`, |
| 566 | }, |
| 567 | fs: map[string]string{ |
| 568 | "other/a.txt": "", |
| 569 | "other/b.txt": "", |
| 570 | "other/subdir/a.txt": "", |
| 571 | "other/file": "", |
| 572 | }, |
| 573 | }, |
| 574 | { |
| 575 | description: "filegroup with glob in subdir", |
| 576 | moduleTypeUnderTest: "filegroup", |
| 577 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 578 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 579 | bp: `filegroup { |
| 580 | name: "foo", |
| 581 | srcs: ["a.txt"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 582 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 583 | }`, |
| 584 | dir: "other", |
| 585 | expectedBazelTargets: []string{`filegroup( |
| 586 | name = "fg_foo", |
| 587 | srcs = [ |
| 588 | "a.txt", |
| 589 | "b.txt", |
| 590 | "subdir/a.txt", |
| 591 | ], |
| 592 | )`, |
| 593 | }, |
| 594 | fs: map[string]string{ |
| 595 | "other/Android.bp": `filegroup { |
| 596 | name: "fg_foo", |
| 597 | srcs: ["**/*.txt"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 598 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 599 | }`, |
| 600 | "other/a.txt": "", |
| 601 | "other/b.txt": "", |
| 602 | "other/subdir/a.txt": "", |
| 603 | "other/file": "", |
| 604 | }, |
| 605 | }, |
| 606 | { |
| 607 | description: "depends_on_other_dir_module", |
| 608 | moduleTypeUnderTest: "filegroup", |
| 609 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 610 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 611 | bp: `filegroup { |
| 612 | name: "foobar", |
| 613 | srcs: [ |
| 614 | ":foo", |
| 615 | "c", |
| 616 | ], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 617 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 618 | }`, |
| 619 | expectedBazelTargets: []string{`filegroup( |
| 620 | name = "foobar", |
| 621 | srcs = [ |
| 622 | "//other:foo", |
| 623 | "c", |
| 624 | ], |
| 625 | )`, |
| 626 | }, |
| 627 | fs: map[string]string{ |
| 628 | "other/Android.bp": `filegroup { |
| 629 | name: "foo", |
| 630 | srcs: ["a", "b"], |
| 631 | }`, |
| 632 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 633 | }, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 634 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 635 | description: "genrule with command line variable replacements", |
| 636 | moduleTypeUnderTest: "genrule", |
| 637 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 638 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 639 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 640 | bp: `genrule { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 641 | name: "foo.tool", |
| 642 | out: ["foo_tool.out"], |
| 643 | srcs: ["foo_tool.in"], |
| 644 | cmd: "cp $(in) $(out)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 645 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | genrule { |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 649 | name: "foo", |
| 650 | out: ["foo.out"], |
| 651 | srcs: ["foo.in"], |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 652 | tools: [":foo.tool"], |
| 653 | cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 654 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 655 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 656 | expectedBazelTargets: []string{ |
| 657 | `genrule( |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 658 | name = "foo", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 659 | cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 660 | outs = [ |
| 661 | "foo.out", |
| 662 | ], |
| 663 | srcs = [ |
| 664 | "foo.in", |
| 665 | ], |
| 666 | tools = [ |
| 667 | ":foo.tool", |
| 668 | ], |
| 669 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 670 | `genrule( |
| 671 | name = "foo.tool", |
| 672 | cmd = "cp $(SRCS) $(OUTS)", |
| 673 | outs = [ |
| 674 | "foo_tool.out", |
| 675 | ], |
| 676 | srcs = [ |
| 677 | "foo_tool.in", |
| 678 | ], |
| 679 | )`, |
| 680 | }, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 681 | }, |
| 682 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 683 | description: "genrule using $(locations :label)", |
| 684 | moduleTypeUnderTest: "genrule", |
| 685 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 686 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 687 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 688 | bp: `genrule { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 689 | name: "foo.tools", |
| 690 | out: ["foo_tool.out", "foo_tool2.out"], |
| 691 | srcs: ["foo_tool.in"], |
| 692 | cmd: "cp $(in) $(out)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 693 | bazel_module: { bp2build_available: true }, |
| 694 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 695 | |
| 696 | genrule { |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 697 | name: "foo", |
| 698 | out: ["foo.out"], |
| 699 | srcs: ["foo.in"], |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 700 | tools: [":foo.tools"], |
| 701 | cmd: "$(locations :foo.tools) -s $(out) $(in)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 702 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 703 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 704 | expectedBazelTargets: []string{`genrule( |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 705 | name = "foo", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 706 | cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)", |
| 707 | outs = [ |
| 708 | "foo.out", |
| 709 | ], |
| 710 | srcs = [ |
| 711 | "foo.in", |
| 712 | ], |
| 713 | tools = [ |
| 714 | ":foo.tools", |
| 715 | ], |
| 716 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 717 | `genrule( |
| 718 | name = "foo.tools", |
| 719 | cmd = "cp $(SRCS) $(OUTS)", |
| 720 | outs = [ |
| 721 | "foo_tool.out", |
| 722 | "foo_tool2.out", |
| 723 | ], |
| 724 | srcs = [ |
| 725 | "foo_tool.in", |
| 726 | ], |
| 727 | )`, |
| 728 | }, |
| 729 | }, |
| 730 | { |
| 731 | description: "genrule using $(locations //absolute:label)", |
| 732 | moduleTypeUnderTest: "genrule", |
| 733 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 734 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
| 735 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
| 736 | bp: `genrule { |
| 737 | name: "foo", |
| 738 | out: ["foo.out"], |
| 739 | srcs: ["foo.in"], |
| 740 | tool_files: [":foo.tool"], |
| 741 | cmd: "$(locations :foo.tool) -s $(out) $(in)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 742 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 743 | }`, |
| 744 | expectedBazelTargets: []string{`genrule( |
| 745 | name = "foo", |
| 746 | cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)", |
| 747 | outs = [ |
| 748 | "foo.out", |
| 749 | ], |
| 750 | srcs = [ |
| 751 | "foo.in", |
| 752 | ], |
| 753 | tools = [ |
| 754 | "//other:foo.tool", |
| 755 | ], |
| 756 | )`, |
| 757 | }, |
| 758 | fs: otherGenruleBp, |
| 759 | }, |
| 760 | { |
| 761 | description: "genrule srcs using $(locations //absolute:label)", |
| 762 | moduleTypeUnderTest: "genrule", |
| 763 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 764 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
| 765 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
| 766 | bp: `genrule { |
| 767 | name: "foo", |
| 768 | out: ["foo.out"], |
| 769 | srcs: [":other.tool"], |
| 770 | tool_files: [":foo.tool"], |
| 771 | cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 772 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 773 | }`, |
| 774 | expectedBazelTargets: []string{`genrule( |
| 775 | name = "foo", |
| 776 | cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)", |
| 777 | outs = [ |
| 778 | "foo.out", |
| 779 | ], |
| 780 | srcs = [ |
| 781 | "//other:other.tool", |
| 782 | ], |
| 783 | tools = [ |
| 784 | "//other:foo.tool", |
| 785 | ], |
| 786 | )`, |
| 787 | }, |
| 788 | fs: otherGenruleBp, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 789 | }, |
| 790 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 791 | description: "genrule using $(location) label should substitute first tool label automatically", |
| 792 | moduleTypeUnderTest: "genrule", |
| 793 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 794 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 795 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 796 | bp: `genrule { |
| 797 | name: "foo", |
| 798 | out: ["foo.out"], |
| 799 | srcs: ["foo.in"], |
| 800 | tool_files: [":foo.tool", ":other.tool"], |
| 801 | cmd: "$(location) -s $(out) $(in)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 802 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 803 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 804 | expectedBazelTargets: []string{`genrule( |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 805 | name = "foo", |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 806 | cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 807 | outs = [ |
| 808 | "foo.out", |
| 809 | ], |
| 810 | srcs = [ |
| 811 | "foo.in", |
| 812 | ], |
| 813 | tools = [ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 814 | "//other:foo.tool", |
| 815 | "//other:other.tool", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 816 | ], |
| 817 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 818 | }, |
| 819 | fs: otherGenruleBp, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 820 | }, |
| 821 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 822 | description: "genrule using $(locations) label should substitute first tool label automatically", |
| 823 | moduleTypeUnderTest: "genrule", |
| 824 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 825 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 826 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 827 | bp: `genrule { |
| 828 | name: "foo", |
| 829 | out: ["foo.out"], |
| 830 | srcs: ["foo.in"], |
| 831 | tools: [":foo.tool", ":other.tool"], |
| 832 | cmd: "$(locations) -s $(out) $(in)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 833 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 834 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 835 | expectedBazelTargets: []string{`genrule( |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 836 | name = "foo", |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 837 | cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 838 | outs = [ |
| 839 | "foo.out", |
| 840 | ], |
| 841 | srcs = [ |
| 842 | "foo.in", |
| 843 | ], |
| 844 | tools = [ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 845 | "//other:foo.tool", |
| 846 | "//other:other.tool", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 847 | ], |
| 848 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 849 | }, |
| 850 | fs: otherGenruleBp, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 851 | }, |
| 852 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 853 | description: "genrule without tools or tool_files can convert successfully", |
| 854 | moduleTypeUnderTest: "genrule", |
| 855 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 856 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 857 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 858 | bp: `genrule { |
| 859 | name: "foo", |
| 860 | out: ["foo.out"], |
| 861 | srcs: ["foo.in"], |
| 862 | cmd: "cp $(in) $(out)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 863 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 864 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 865 | expectedBazelTargets: []string{`genrule( |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 866 | name = "foo", |
| 867 | cmd = "cp $(SRCS) $(OUTS)", |
| 868 | outs = [ |
| 869 | "foo.out", |
| 870 | ], |
| 871 | srcs = [ |
| 872 | "foo.in", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 873 | ], |
| 874 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 875 | }, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 876 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | dir := "." |
| 880 | for _, testCase := range testCases { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 881 | fs := make(map[string][]byte) |
| 882 | toParse := []string{ |
| 883 | "Android.bp", |
| 884 | } |
| 885 | for f, content := range testCase.fs { |
| 886 | if strings.HasSuffix(f, "Android.bp") { |
| 887 | toParse = append(toParse, f) |
| 888 | } |
| 889 | fs[f] = []byte(content) |
| 890 | } |
| 891 | config := android.TestConfig(buildDir, nil, testCase.bp, fs) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 892 | ctx := android.NewTestContext(config) |
| 893 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 894 | for _, m := range testCase.depsMutators { |
| 895 | ctx.DepsBp2BuildMutators(m) |
| 896 | } |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 897 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 898 | ctx.RegisterForBazelConversion() |
| 899 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 900 | _, errs := ctx.ParseFileList(dir, toParse) |
| 901 | if Errored(t, testCase.description, errs) { |
| 902 | continue |
| 903 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 904 | _, errs = ctx.ResolveDependencies(config) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 905 | if Errored(t, testCase.description, errs) { |
| 906 | continue |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 907 | } |
| 908 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 909 | checkDir := dir |
| 910 | if testCase.dir != "" { |
| 911 | checkDir = testCase.dir |
| 912 | } |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 913 | |
| 914 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame^] | 915 | bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 916 | if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount { |
| 917 | t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount) |
| 918 | } else { |
| 919 | for i, target := range bazelTargets { |
| 920 | if w, g := testCase.expectedBazelTargets[i], target.content; w != g { |
| 921 | t.Errorf( |
| 922 | "%s: Expected generated Bazel target to be '%s', got '%s'", |
| 923 | testCase.description, |
| 924 | w, |
| 925 | g, |
| 926 | ) |
| 927 | } |
| 928 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | } |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 932 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 933 | func Errored(t *testing.T, desc string, errs []error) bool { |
| 934 | t.Helper() |
| 935 | if len(errs) > 0 { |
| 936 | for _, err := range errs { |
| 937 | t.Errorf("%s: %s", desc, err) |
| 938 | } |
| 939 | return true |
| 940 | } |
| 941 | return false |
| 942 | } |
| 943 | |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 944 | type bp2buildMutator = func(android.TopDownMutatorContext) |
| 945 | |
| 946 | func TestBp2BuildInlinesDefaults(t *testing.T) { |
| 947 | testCases := []struct { |
| 948 | moduleTypesUnderTest map[string]android.ModuleFactory |
| 949 | bp2buildMutatorsUnderTest map[string]bp2buildMutator |
| 950 | bp string |
| 951 | expectedBazelTarget string |
| 952 | description string |
| 953 | }{ |
| 954 | { |
| 955 | moduleTypesUnderTest: map[string]android.ModuleFactory{ |
| 956 | "genrule": genrule.GenRuleFactory, |
| 957 | "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() }, |
| 958 | }, |
| 959 | bp2buildMutatorsUnderTest: map[string]bp2buildMutator{ |
| 960 | "genrule": genrule.GenruleBp2Build, |
| 961 | }, |
| 962 | bp: `genrule_defaults { |
| 963 | name: "gen_defaults", |
| 964 | cmd: "do-something $(in) $(out)", |
| 965 | } |
| 966 | genrule { |
| 967 | name: "gen", |
| 968 | out: ["out"], |
| 969 | srcs: ["in1"], |
| 970 | defaults: ["gen_defaults"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 971 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 972 | } |
| 973 | `, |
| 974 | expectedBazelTarget: `genrule( |
| 975 | name = "gen", |
| 976 | cmd = "do-something $(SRCS) $(OUTS)", |
| 977 | outs = [ |
| 978 | "out", |
| 979 | ], |
| 980 | srcs = [ |
| 981 | "in1", |
| 982 | ], |
| 983 | )`, |
| 984 | description: "genrule applies properties from a genrule_defaults dependency if not specified", |
| 985 | }, |
| 986 | { |
| 987 | moduleTypesUnderTest: map[string]android.ModuleFactory{ |
| 988 | "genrule": genrule.GenRuleFactory, |
| 989 | "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() }, |
| 990 | }, |
| 991 | bp2buildMutatorsUnderTest: map[string]bp2buildMutator{ |
| 992 | "genrule": genrule.GenruleBp2Build, |
| 993 | }, |
| 994 | bp: `genrule_defaults { |
| 995 | name: "gen_defaults", |
| 996 | out: ["out-from-defaults"], |
| 997 | srcs: ["in-from-defaults"], |
| 998 | cmd: "cmd-from-defaults", |
| 999 | } |
| 1000 | genrule { |
| 1001 | name: "gen", |
| 1002 | out: ["out"], |
| 1003 | srcs: ["in1"], |
| 1004 | defaults: ["gen_defaults"], |
| 1005 | cmd: "do-something $(in) $(out)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1006 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1007 | } |
| 1008 | `, |
| 1009 | expectedBazelTarget: `genrule( |
| 1010 | name = "gen", |
| 1011 | cmd = "do-something $(SRCS) $(OUTS)", |
| 1012 | outs = [ |
| 1013 | "out-from-defaults", |
| 1014 | "out", |
| 1015 | ], |
| 1016 | srcs = [ |
| 1017 | "in-from-defaults", |
| 1018 | "in1", |
| 1019 | ], |
| 1020 | )`, |
| 1021 | description: "genrule does merges properties from a genrule_defaults dependency, latest-first", |
| 1022 | }, |
| 1023 | { |
| 1024 | moduleTypesUnderTest: map[string]android.ModuleFactory{ |
| 1025 | "genrule": genrule.GenRuleFactory, |
| 1026 | "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() }, |
| 1027 | }, |
| 1028 | bp2buildMutatorsUnderTest: map[string]bp2buildMutator{ |
| 1029 | "genrule": genrule.GenruleBp2Build, |
| 1030 | }, |
| 1031 | bp: `genrule_defaults { |
| 1032 | name: "gen_defaults1", |
| 1033 | cmd: "cp $(in) $(out)", |
| 1034 | } |
| 1035 | |
| 1036 | genrule_defaults { |
| 1037 | name: "gen_defaults2", |
| 1038 | srcs: ["in1"], |
| 1039 | } |
| 1040 | |
| 1041 | genrule { |
| 1042 | name: "gen", |
| 1043 | out: ["out"], |
| 1044 | defaults: ["gen_defaults1", "gen_defaults2"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1045 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1046 | } |
| 1047 | `, |
| 1048 | expectedBazelTarget: `genrule( |
| 1049 | name = "gen", |
| 1050 | cmd = "cp $(SRCS) $(OUTS)", |
| 1051 | outs = [ |
| 1052 | "out", |
| 1053 | ], |
| 1054 | srcs = [ |
| 1055 | "in1", |
| 1056 | ], |
| 1057 | )`, |
| 1058 | description: "genrule applies properties from list of genrule_defaults", |
| 1059 | }, |
| 1060 | { |
| 1061 | moduleTypesUnderTest: map[string]android.ModuleFactory{ |
| 1062 | "genrule": genrule.GenRuleFactory, |
| 1063 | "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() }, |
| 1064 | }, |
| 1065 | bp2buildMutatorsUnderTest: map[string]bp2buildMutator{ |
| 1066 | "genrule": genrule.GenruleBp2Build, |
| 1067 | }, |
| 1068 | bp: `genrule_defaults { |
| 1069 | name: "gen_defaults1", |
| 1070 | defaults: ["gen_defaults2"], |
| 1071 | cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value. |
| 1072 | } |
| 1073 | |
| 1074 | genrule_defaults { |
| 1075 | name: "gen_defaults2", |
| 1076 | defaults: ["gen_defaults3"], |
| 1077 | cmd: "cmd2 $(in) $(out)", |
| 1078 | out: ["out-from-2"], |
| 1079 | srcs: ["in1"], |
| 1080 | } |
| 1081 | |
| 1082 | genrule_defaults { |
| 1083 | name: "gen_defaults3", |
| 1084 | out: ["out-from-3"], |
| 1085 | srcs: ["srcs-from-3"], |
| 1086 | } |
| 1087 | |
| 1088 | genrule { |
| 1089 | name: "gen", |
| 1090 | out: ["out"], |
| 1091 | defaults: ["gen_defaults1"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1092 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1093 | } |
| 1094 | `, |
| 1095 | expectedBazelTarget: `genrule( |
| 1096 | name = "gen", |
| 1097 | cmd = "cmd1 $(SRCS) $(OUTS)", |
| 1098 | outs = [ |
| 1099 | "out-from-3", |
| 1100 | "out-from-2", |
| 1101 | "out", |
| 1102 | ], |
| 1103 | srcs = [ |
| 1104 | "srcs-from-3", |
| 1105 | "in1", |
| 1106 | ], |
| 1107 | )`, |
| 1108 | description: "genrule applies properties from genrule_defaults transitively", |
| 1109 | }, |
| 1110 | } |
| 1111 | |
| 1112 | dir := "." |
| 1113 | for _, testCase := range testCases { |
| 1114 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 1115 | ctx := android.NewTestContext(config) |
| 1116 | for m, factory := range testCase.moduleTypesUnderTest { |
| 1117 | ctx.RegisterModuleType(m, factory) |
| 1118 | } |
| 1119 | for mutator, f := range testCase.bp2buildMutatorsUnderTest { |
| 1120 | ctx.RegisterBp2BuildMutator(mutator, f) |
| 1121 | } |
| 1122 | ctx.RegisterForBazelConversion() |
| 1123 | |
| 1124 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 1125 | android.FailIfErrored(t, errs) |
| 1126 | _, errs = ctx.ResolveDependencies(config) |
| 1127 | android.FailIfErrored(t, errs) |
| 1128 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 1129 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame^] | 1130 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1131 | if actualCount := len(bazelTargets); actualCount != 1 { |
| 1132 | t.Fatalf("%s: Expected 1 bazel target, got %d", testCase.description, actualCount) |
| 1133 | } |
| 1134 | |
| 1135 | actualBazelTarget := bazelTargets[0] |
| 1136 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 1137 | t.Errorf( |
| 1138 | "%s: Expected generated Bazel target to be '%s', got '%s'", |
| 1139 | testCase.description, |
| 1140 | testCase.expectedBazelTarget, |
| 1141 | actualBazelTarget.content, |
| 1142 | ) |
| 1143 | } |
| 1144 | } |
| 1145 | } |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1146 | |
| 1147 | func TestAllowlistingBp2buildTargets(t *testing.T) { |
| 1148 | testCases := []struct { |
| 1149 | moduleTypeUnderTest string |
| 1150 | moduleTypeUnderTestFactory android.ModuleFactory |
| 1151 | moduleTypeUnderTestBp2BuildMutator bp2buildMutator |
| 1152 | bp string |
| 1153 | expectedCount int |
| 1154 | description string |
| 1155 | }{ |
| 1156 | { |
| 1157 | description: "explicitly unavailable", |
| 1158 | moduleTypeUnderTest: "filegroup", |
| 1159 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1160 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 1161 | bp: `filegroup { |
| 1162 | name: "foo", |
| 1163 | srcs: ["a", "b"], |
| 1164 | bazel_module: { bp2build_available: false }, |
| 1165 | }`, |
| 1166 | expectedCount: 0, |
| 1167 | }, |
| 1168 | { |
| 1169 | description: "implicitly unavailable", |
| 1170 | moduleTypeUnderTest: "filegroup", |
| 1171 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1172 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 1173 | bp: `filegroup { |
| 1174 | name: "foo", |
| 1175 | srcs: ["a", "b"], |
| 1176 | }`, |
| 1177 | expectedCount: 0, |
| 1178 | }, |
| 1179 | { |
| 1180 | description: "explicitly available", |
| 1181 | moduleTypeUnderTest: "filegroup", |
| 1182 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1183 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 1184 | bp: `filegroup { |
| 1185 | name: "foo", |
| 1186 | srcs: ["a", "b"], |
| 1187 | bazel_module: { bp2build_available: true }, |
| 1188 | }`, |
| 1189 | expectedCount: 1, |
| 1190 | }, |
| 1191 | { |
| 1192 | description: "generates more than 1 target if needed", |
| 1193 | moduleTypeUnderTest: "custom", |
| 1194 | moduleTypeUnderTestFactory: customModuleFactory, |
| 1195 | moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark, |
| 1196 | bp: `custom { |
| 1197 | name: "foo", |
| 1198 | bazel_module: { bp2build_available: true }, |
| 1199 | }`, |
| 1200 | expectedCount: 3, |
| 1201 | }, |
| 1202 | } |
| 1203 | |
| 1204 | dir := "." |
| 1205 | for _, testCase := range testCases { |
| 1206 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 1207 | ctx := android.NewTestContext(config) |
| 1208 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
| 1209 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
| 1210 | ctx.RegisterForBazelConversion() |
| 1211 | |
| 1212 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 1213 | android.FailIfErrored(t, errs) |
| 1214 | _, errs = ctx.ResolveDependencies(config) |
| 1215 | android.FailIfErrored(t, errs) |
| 1216 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 1217 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame^] | 1218 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1219 | if actualCount := len(bazelTargets); actualCount != testCase.expectedCount { |
| 1220 | t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount) |
| 1221 | } |
| 1222 | } |
| 1223 | } |