blob: 21d7062fadb3f4b9c2772e5d4d6cdd371020837d [file] [log] [blame]
Liz Kammer2dd9ca42020-11-25 16:06:39 -08001// 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
15package bp2build
16
17import (
18 "android/soong/android"
Jingwen Chen316e07c2020-12-14 09:09:52 -050019 "android/soong/genrule"
Liz Kammer356f7d42021-01-26 09:18:53 -050020 "strings"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080021 "testing"
22)
23
24func TestGenerateSoongModuleTargets(t *testing.T) {
25 testCases := []struct {
26 bp string
27 expectedBazelTarget string
28 }{
29 {
Jingwen Chenb4628eb2021-04-08 14:40:57 +000030 bp: `custom { name: "foo" }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080031 `,
32 expectedBazelTarget: `soong_module(
33 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050034 soong_module_name = "foo",
35 soong_module_type = "custom",
36 soong_module_variant = "",
37 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080038 ],
39)`,
40 },
41 {
42 bp: `custom {
43 name: "foo",
44 ramdisk: true,
45}
46 `,
47 expectedBazelTarget: `soong_module(
48 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050049 soong_module_name = "foo",
50 soong_module_type = "custom",
51 soong_module_variant = "",
52 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080053 ],
54 ramdisk = True,
55)`,
56 },
57 {
58 bp: `custom {
59 name: "foo",
60 owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
61}
62 `,
63 expectedBazelTarget: `soong_module(
64 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050065 soong_module_name = "foo",
66 soong_module_type = "custom",
67 soong_module_variant = "",
68 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080069 ],
70 owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
71)`,
72 },
73 {
74 bp: `custom {
75 name: "foo",
76 required: ["bar"],
77}
78 `,
79 expectedBazelTarget: `soong_module(
80 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050081 soong_module_name = "foo",
82 soong_module_type = "custom",
83 soong_module_variant = "",
84 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080085 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +000086 required = ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -080087)`,
88 },
89 {
90 bp: `custom {
91 name: "foo",
92 target_required: ["qux", "bazqux"],
93}
94 `,
95 expectedBazelTarget: `soong_module(
96 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050097 soong_module_name = "foo",
98 soong_module_type = "custom",
99 soong_module_variant = "",
100 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800101 ],
102 target_required = [
103 "qux",
104 "bazqux",
105 ],
106)`,
107 },
108 {
109 bp: `custom {
110 name: "foo",
111 dist: {
112 targets: ["goal_foo"],
113 tag: ".foo",
114 },
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000115 dists: [{
116 targets: ["goal_bar"],
117 tag: ".bar",
118 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800119}
120 `,
121 expectedBazelTarget: `soong_module(
122 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500123 soong_module_name = "foo",
124 soong_module_type = "custom",
125 soong_module_variant = "",
126 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800127 ],
128 dist = {
129 "tag": ".foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000130 "targets": ["goal_foo"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800131 },
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000132 dists = [{
133 "tag": ".bar",
134 "targets": ["goal_bar"],
135 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800136)`,
137 },
138 {
139 bp: `custom {
140 name: "foo",
141 required: ["bar"],
142 target_required: ["qux", "bazqux"],
143 ramdisk: true,
144 owner: "custom_owner",
145 dists: [
146 {
147 tag: ".tag",
148 targets: ["my_goal"],
149 },
150 ],
151}
152 `,
153 expectedBazelTarget: `soong_module(
154 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500155 soong_module_name = "foo",
156 soong_module_type = "custom",
157 soong_module_variant = "",
158 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800159 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000160 dists = [{
161 "tag": ".tag",
162 "targets": ["my_goal"],
163 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800164 owner = "custom_owner",
165 ramdisk = True,
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000166 required = ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800167 target_required = [
168 "qux",
169 "bazqux",
170 ],
171)`,
172 },
173 }
174
175 dir := "."
176 for _, testCase := range testCases {
177 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
178 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500179
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800180 ctx.RegisterModuleType("custom", customModuleFactory)
181 ctx.Register()
182
183 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
184 android.FailIfErrored(t, errs)
185 _, errs = ctx.PrepareBuildActions(config)
186 android.FailIfErrored(t, errs)
187
Jingwen Chen164e0862021-02-19 00:48:40 -0500188 codegenCtx := NewCodegenContext(config, *ctx.Context, QueryView)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500189 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen4e4756d2021-01-24 21:13:13 -0500190 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
191 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800192 }
193
194 actualBazelTarget := bazelTargets[0]
195 if actualBazelTarget.content != testCase.expectedBazelTarget {
196 t.Errorf(
197 "Expected generated Bazel target to be '%s', got '%s'",
198 testCase.expectedBazelTarget,
Jingwen Chen73850672020-12-14 08:25:34 -0500199 actualBazelTarget.content,
200 )
201 }
202 }
203}
204
205func TestGenerateBazelTargetModules(t *testing.T) {
206 testCases := []struct {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400207 name string
208 bp string
209 expectedBazelTargets []string
Jingwen Chen73850672020-12-14 08:25:34 -0500210 }{
211 {
212 bp: `custom {
213 name: "foo",
214 string_list_prop: ["a", "b"],
215 string_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500216 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500217}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400218 expectedBazelTargets: []string{`custom(
Jingwen Chen73850672020-12-14 08:25:34 -0500219 name = "foo",
220 string_list_prop = [
221 "a",
222 "b",
223 ],
224 string_prop = "a",
225)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400226 },
Jingwen Chen73850672020-12-14 08:25:34 -0500227 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000228 {
229 bp: `custom {
230 name: "control_characters",
231 string_list_prop: ["\t", "\n"],
232 string_prop: "a\t\n\r",
233 bazel_module: { bp2build_available: true },
234}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400235 expectedBazelTargets: []string{`custom(
Jingwen Chen58a12b82021-03-30 13:08:36 +0000236 name = "control_characters",
237 string_list_prop = [
238 "\t",
239 "\n",
240 ],
241 string_prop = "a\t\n\r",
242)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400243 },
244 },
245 {
246 bp: `custom {
247 name: "has_dep",
248 arch_paths: [":dep"],
249 bazel_module: { bp2build_available: true },
250}
251
252custom {
253 name: "dep",
254 arch_paths: ["abc"],
255 bazel_module: { bp2build_available: true },
256}`,
257 expectedBazelTargets: []string{`custom(
258 name = "dep",
259 arch_paths = ["abc"],
260)`,
261 `custom(
262 name = "has_dep",
263 arch_paths = [":dep"],
264)`,
265 },
266 },
267 {
268 bp: `custom {
269 name: "arch_paths",
270 arch: {
271 x86: {
272 arch_paths: ["abc"],
273 },
274 },
275 bazel_module: { bp2build_available: true },
276}`,
277 expectedBazelTargets: []string{`custom(
278 name = "arch_paths",
279 arch_paths = select({
280 "//build/bazel/platforms/arch:x86": ["abc"],
281 "//conditions:default": [],
282 }),
283)`,
284 },
285 },
286 {
287 bp: `custom {
288 name: "has_dep",
289 arch: {
290 x86: {
291 arch_paths: [":dep"],
292 },
293 },
294 bazel_module: { bp2build_available: true },
295}
296
297custom {
298 name: "dep",
299 arch_paths: ["abc"],
300 bazel_module: { bp2build_available: true },
301}`,
302 expectedBazelTargets: []string{`custom(
303 name = "dep",
304 arch_paths = ["abc"],
305)`,
306 `custom(
307 name = "has_dep",
308 arch_paths = select({
309 "//build/bazel/platforms/arch:x86": [":dep"],
310 "//conditions:default": [],
311 }),
312)`,
313 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000314 },
Jingwen Chen73850672020-12-14 08:25:34 -0500315 }
316
317 dir := "."
318 for _, testCase := range testCases {
319 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
320 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500321
Jingwen Chen73850672020-12-14 08:25:34 -0500322 ctx.RegisterModuleType("custom", customModuleFactory)
323 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator)
324 ctx.RegisterForBazelConversion()
325
326 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
Liz Kammer356f7d42021-01-26 09:18:53 -0500327 if Errored(t, "", errs) {
328 continue
329 }
Jingwen Chen73850672020-12-14 08:25:34 -0500330 _, errs = ctx.ResolveDependencies(config)
Liz Kammer356f7d42021-01-26 09:18:53 -0500331 if Errored(t, "", errs) {
332 continue
333 }
Jingwen Chen73850672020-12-14 08:25:34 -0500334
Jingwen Chen164e0862021-02-19 00:48:40 -0500335 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500336 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen164e0862021-02-19 00:48:40 -0500337
Liz Kammer4562a3b2021-04-21 18:15:34 -0400338 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
Liz Kammer356f7d42021-01-26 09:18:53 -0500339 t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount)
340 } else {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400341 for i, expectedBazelTarget := range testCase.expectedBazelTargets {
342 actualBazelTarget := bazelTargets[i]
343 if actualBazelTarget.content != expectedBazelTarget {
344 t.Errorf(
345 "Expected generated Bazel target to be '%s', got '%s'",
346 expectedBazelTarget,
347 actualBazelTarget.content,
348 )
349 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500350 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800351 }
352 }
353}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500354
Jingwen Chen40067de2021-01-26 21:58:43 -0500355func TestLoadStatements(t *testing.T) {
356 testCases := []struct {
357 bazelTargets BazelTargets
358 expectedLoadStatements string
359 }{
360 {
361 bazelTargets: BazelTargets{
362 BazelTarget{
363 name: "foo",
364 ruleClass: "cc_library",
365 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
366 },
367 },
368 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
369 },
370 {
371 bazelTargets: BazelTargets{
372 BazelTarget{
373 name: "foo",
374 ruleClass: "cc_library",
375 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
376 },
377 BazelTarget{
378 name: "bar",
379 ruleClass: "cc_library",
380 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
381 },
382 },
383 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
384 },
385 {
386 bazelTargets: BazelTargets{
387 BazelTarget{
388 name: "foo",
389 ruleClass: "cc_library",
390 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
391 },
392 BazelTarget{
393 name: "bar",
394 ruleClass: "cc_binary",
395 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
396 },
397 },
398 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
399 },
400 {
401 bazelTargets: BazelTargets{
402 BazelTarget{
403 name: "foo",
404 ruleClass: "cc_library",
405 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
406 },
407 BazelTarget{
408 name: "bar",
409 ruleClass: "cc_binary",
410 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
411 },
412 BazelTarget{
413 name: "baz",
414 ruleClass: "java_binary",
415 bzlLoadLocation: "//build/bazel/rules:java.bzl",
416 },
417 },
418 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
419load("//build/bazel/rules:java.bzl", "java_binary")`,
420 },
421 {
422 bazelTargets: BazelTargets{
423 BazelTarget{
424 name: "foo",
425 ruleClass: "cc_binary",
426 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
427 },
428 BazelTarget{
429 name: "bar",
430 ruleClass: "java_binary",
431 bzlLoadLocation: "//build/bazel/rules:java.bzl",
432 },
433 BazelTarget{
434 name: "baz",
435 ruleClass: "genrule",
436 // Note: no bzlLoadLocation for native rules
437 },
438 },
439 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
440load("//build/bazel/rules:java.bzl", "java_binary")`,
441 },
442 }
443
444 for _, testCase := range testCases {
445 actual := testCase.bazelTargets.LoadStatements()
446 expected := testCase.expectedLoadStatements
447 if actual != expected {
448 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
449 }
450 }
451
452}
453
454func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
455 testCases := []struct {
456 bp string
457 expectedBazelTarget string
458 expectedBazelTargetCount int
459 expectedLoadStatements string
460 }{
461 {
462 bp: `custom {
463 name: "bar",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500464 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500465}`,
466 expectedBazelTarget: `my_library(
467 name = "bar",
468)
469
470my_proto_library(
471 name = "bar_my_proto_library_deps",
472)
473
474proto_library(
475 name = "bar_proto_library_deps",
476)`,
477 expectedBazelTargetCount: 3,
478 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
479load("//build/bazel/rules:rules.bzl", "my_library")`,
480 },
481 }
482
483 dir := "."
484 for _, testCase := range testCases {
485 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
486 ctx := android.NewTestContext(config)
487 ctx.RegisterModuleType("custom", customModuleFactory)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500488 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark)
Jingwen Chen40067de2021-01-26 21:58:43 -0500489 ctx.RegisterForBazelConversion()
490
491 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
492 android.FailIfErrored(t, errs)
493 _, errs = ctx.ResolveDependencies(config)
494 android.FailIfErrored(t, errs)
495
Jingwen Chen164e0862021-02-19 00:48:40 -0500496 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500497 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen40067de2021-01-26 21:58:43 -0500498 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
499 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
500 }
501
502 actualBazelTargets := bazelTargets.String()
503 if actualBazelTargets != testCase.expectedBazelTarget {
504 t.Errorf(
505 "Expected generated Bazel target to be '%s', got '%s'",
506 testCase.expectedBazelTarget,
507 actualBazelTargets,
508 )
509 }
510
511 actualLoadStatements := bazelTargets.LoadStatements()
512 if actualLoadStatements != testCase.expectedLoadStatements {
513 t.Errorf(
514 "Expected generated load statements to be '%s', got '%s'",
515 testCase.expectedLoadStatements,
516 actualLoadStatements,
517 )
518 }
519 }
520}
521
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500522func TestModuleTypeBp2Build(t *testing.T) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500523 otherGenruleBp := map[string]string{
524 "other/Android.bp": `genrule {
525 name: "foo.tool",
526 out: ["foo_tool.out"],
527 srcs: ["foo_tool.in"],
528 cmd: "cp $(in) $(out)",
529}
530genrule {
531 name: "other.tool",
532 out: ["other_tool.out"],
533 srcs: ["other_tool.in"],
534 cmd: "cp $(in) $(out)",
535}`,
536 }
537
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500538 testCases := []struct {
Liz Kammer356f7d42021-01-26 09:18:53 -0500539 description string
Jingwen Chena42d6412021-01-26 21:57:27 -0500540 moduleTypeUnderTest string
541 moduleTypeUnderTestFactory android.ModuleFactory
542 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
Liz Kammer356f7d42021-01-26 09:18:53 -0500543 preArchMutators []android.RegisterMutatorFunc
544 depsMutators []android.RegisterMutatorFunc
Jingwen Chena42d6412021-01-26 21:57:27 -0500545 bp string
Liz Kammer356f7d42021-01-26 09:18:53 -0500546 expectedBazelTargets []string
547 fs map[string]string
548 dir string
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500549 }{
550 {
Liz Kammerebfcf672021-02-16 15:00:05 -0500551 description: "filegroup with does not specify srcs",
552 moduleTypeUnderTest: "filegroup",
553 moduleTypeUnderTestFactory: android.FileGroupFactory,
554 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
555 bp: `filegroup {
556 name: "fg_foo",
557 bazel_module: { bp2build_available: true },
558}`,
559 expectedBazelTargets: []string{
560 `filegroup(
561 name = "fg_foo",
562)`,
563 },
564 },
565 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500566 description: "filegroup with no srcs",
567 moduleTypeUnderTest: "filegroup",
568 moduleTypeUnderTestFactory: android.FileGroupFactory,
569 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500570 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500571 name: "fg_foo",
572 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500573 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500574}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500575 expectedBazelTargets: []string{
576 `filegroup(
577 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500578)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500579 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500580 },
581 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500582 description: "filegroup with srcs",
583 moduleTypeUnderTest: "filegroup",
584 moduleTypeUnderTestFactory: android.FileGroupFactory,
585 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500586 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500587 name: "fg_foo",
588 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500589 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500590}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500591 expectedBazelTargets: []string{`filegroup(
592 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500593 srcs = [
594 "a",
595 "b",
596 ],
597)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500598 },
599 },
600 {
601 description: "filegroup with excludes srcs",
602 moduleTypeUnderTest: "filegroup",
603 moduleTypeUnderTestFactory: android.FileGroupFactory,
604 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
605 bp: `filegroup {
606 name: "fg_foo",
607 srcs: ["a", "b"],
608 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500609 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500610}`,
611 expectedBazelTargets: []string{`filegroup(
612 name = "fg_foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000613 srcs = ["b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500614)`,
615 },
616 },
617 {
618 description: "filegroup with glob",
619 moduleTypeUnderTest: "filegroup",
620 moduleTypeUnderTestFactory: android.FileGroupFactory,
621 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
622 bp: `filegroup {
623 name: "foo",
624 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500625 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500626}`,
627 expectedBazelTargets: []string{`filegroup(
628 name = "foo",
629 srcs = [
630 "other/a.txt",
631 "other/b.txt",
632 "other/subdir/a.txt",
633 ],
634)`,
635 },
636 fs: map[string]string{
637 "other/a.txt": "",
638 "other/b.txt": "",
639 "other/subdir/a.txt": "",
640 "other/file": "",
641 },
642 },
643 {
644 description: "filegroup with glob in subdir",
645 moduleTypeUnderTest: "filegroup",
646 moduleTypeUnderTestFactory: android.FileGroupFactory,
647 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
648 bp: `filegroup {
649 name: "foo",
650 srcs: ["a.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500651 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500652}`,
653 dir: "other",
654 expectedBazelTargets: []string{`filegroup(
655 name = "fg_foo",
656 srcs = [
657 "a.txt",
658 "b.txt",
659 "subdir/a.txt",
660 ],
661)`,
662 },
663 fs: map[string]string{
664 "other/Android.bp": `filegroup {
665 name: "fg_foo",
666 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500667 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500668}`,
669 "other/a.txt": "",
670 "other/b.txt": "",
671 "other/subdir/a.txt": "",
672 "other/file": "",
673 },
674 },
675 {
676 description: "depends_on_other_dir_module",
677 moduleTypeUnderTest: "filegroup",
678 moduleTypeUnderTestFactory: android.FileGroupFactory,
679 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
680 bp: `filegroup {
681 name: "foobar",
682 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000683 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500684 "c",
685 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500686 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500687}`,
688 expectedBazelTargets: []string{`filegroup(
689 name = "foobar",
690 srcs = [
691 "//other:foo",
692 "c",
693 ],
694)`,
695 },
696 fs: map[string]string{
697 "other/Android.bp": `filegroup {
698 name: "foo",
699 srcs: ["a", "b"],
700}`,
701 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500702 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500703 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500704 description: "genrule with command line variable replacements",
705 moduleTypeUnderTest: "genrule",
706 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
707 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500708 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen316e07c2020-12-14 09:09:52 -0500709 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500710 name: "foo.tool",
711 out: ["foo_tool.out"],
712 srcs: ["foo_tool.in"],
713 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500714 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500715}
716
717genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500718 name: "foo",
719 out: ["foo.out"],
720 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500721 tools: [":foo.tool"],
722 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500723 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500724}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500725 expectedBazelTargets: []string{
726 `genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500727 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500728 cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000729 outs = ["foo.out"],
730 srcs = ["foo.in"],
731 tools = [":foo.tool"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500732)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500733 `genrule(
734 name = "foo.tool",
735 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000736 outs = ["foo_tool.out"],
737 srcs = ["foo_tool.in"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500738)`,
739 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500740 },
741 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500742 description: "genrule using $(locations :label)",
743 moduleTypeUnderTest: "genrule",
744 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
745 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500746 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen316e07c2020-12-14 09:09:52 -0500747 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500748 name: "foo.tools",
749 out: ["foo_tool.out", "foo_tool2.out"],
750 srcs: ["foo_tool.in"],
751 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500752 bazel_module: { bp2build_available: true },
753}
Liz Kammer356f7d42021-01-26 09:18:53 -0500754
755genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500756 name: "foo",
757 out: ["foo.out"],
758 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500759 tools: [":foo.tools"],
760 cmd: "$(locations :foo.tools) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500761 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500762}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500763 expectedBazelTargets: []string{`genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500764 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500765 cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000766 outs = ["foo.out"],
767 srcs = ["foo.in"],
768 tools = [":foo.tools"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500769)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500770 `genrule(
771 name = "foo.tools",
772 cmd = "cp $(SRCS) $(OUTS)",
773 outs = [
774 "foo_tool.out",
775 "foo_tool2.out",
776 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000777 srcs = ["foo_tool.in"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500778)`,
779 },
780 },
781 {
782 description: "genrule using $(locations //absolute:label)",
783 moduleTypeUnderTest: "genrule",
784 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
785 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
786 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
787 bp: `genrule {
788 name: "foo",
789 out: ["foo.out"],
790 srcs: ["foo.in"],
791 tool_files: [":foo.tool"],
792 cmd: "$(locations :foo.tool) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500793 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500794}`,
795 expectedBazelTargets: []string{`genrule(
796 name = "foo",
797 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000798 outs = ["foo.out"],
799 srcs = ["foo.in"],
800 tools = ["//other:foo.tool"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500801)`,
802 },
803 fs: otherGenruleBp,
804 },
805 {
806 description: "genrule srcs using $(locations //absolute:label)",
807 moduleTypeUnderTest: "genrule",
808 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
809 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
810 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
811 bp: `genrule {
812 name: "foo",
813 out: ["foo.out"],
814 srcs: [":other.tool"],
815 tool_files: [":foo.tool"],
816 cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500817 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500818}`,
819 expectedBazelTargets: []string{`genrule(
820 name = "foo",
821 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000822 outs = ["foo.out"],
823 srcs = ["//other:other.tool"],
824 tools = ["//other:foo.tool"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500825)`,
826 },
827 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500828 },
829 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500830 description: "genrule using $(location) label should substitute first tool label automatically",
831 moduleTypeUnderTest: "genrule",
832 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
833 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500834 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500835 bp: `genrule {
836 name: "foo",
837 out: ["foo.out"],
838 srcs: ["foo.in"],
839 tool_files: [":foo.tool", ":other.tool"],
840 cmd: "$(location) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500841 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500842}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500843 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500844 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500845 cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000846 outs = ["foo.out"],
847 srcs = ["foo.in"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500848 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500849 "//other:foo.tool",
850 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500851 ],
852)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500853 },
854 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500855 },
856 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500857 description: "genrule using $(locations) label should substitute first tool label automatically",
858 moduleTypeUnderTest: "genrule",
859 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
860 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500861 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500862 bp: `genrule {
863 name: "foo",
864 out: ["foo.out"],
865 srcs: ["foo.in"],
866 tools: [":foo.tool", ":other.tool"],
867 cmd: "$(locations) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500868 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500869}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500870 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500871 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500872 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000873 outs = ["foo.out"],
874 srcs = ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500875 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500876 "//other:foo.tool",
877 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500878 ],
879)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500880 },
881 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500882 },
883 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500884 description: "genrule without tools or tool_files can convert successfully",
885 moduleTypeUnderTest: "genrule",
886 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
887 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500888 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500889 bp: `genrule {
890 name: "foo",
891 out: ["foo.out"],
892 srcs: ["foo.in"],
893 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500894 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500895}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500896 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500897 name = "foo",
898 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000899 outs = ["foo.out"],
900 srcs = ["foo.in"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500901)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500902 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500903 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500904 }
905
906 dir := "."
907 for _, testCase := range testCases {
Liz Kammer356f7d42021-01-26 09:18:53 -0500908 fs := make(map[string][]byte)
909 toParse := []string{
910 "Android.bp",
911 }
912 for f, content := range testCase.fs {
913 if strings.HasSuffix(f, "Android.bp") {
914 toParse = append(toParse, f)
915 }
916 fs[f] = []byte(content)
917 }
918 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500919 ctx := android.NewTestContext(config)
920 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer356f7d42021-01-26 09:18:53 -0500921 for _, m := range testCase.depsMutators {
922 ctx.DepsBp2BuildMutators(m)
923 }
Jingwen Chena42d6412021-01-26 21:57:27 -0500924 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500925 ctx.RegisterForBazelConversion()
926
Liz Kammer356f7d42021-01-26 09:18:53 -0500927 _, errs := ctx.ParseFileList(dir, toParse)
928 if Errored(t, testCase.description, errs) {
929 continue
930 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500931 _, errs = ctx.ResolveDependencies(config)
Liz Kammer356f7d42021-01-26 09:18:53 -0500932 if Errored(t, testCase.description, errs) {
933 continue
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500934 }
935
Liz Kammer356f7d42021-01-26 09:18:53 -0500936 checkDir := dir
937 if testCase.dir != "" {
938 checkDir = testCase.dir
939 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500940
941 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500942 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
Liz Kammer356f7d42021-01-26 09:18:53 -0500943 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
944 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
945 } else {
946 for i, target := range bazelTargets {
947 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
948 t.Errorf(
949 "%s: Expected generated Bazel target to be '%s', got '%s'",
950 testCase.description,
951 w,
952 g,
953 )
954 }
955 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500956 }
957 }
958}
Jingwen Chen041b1842021-02-01 00:23:25 -0500959
Liz Kammer356f7d42021-01-26 09:18:53 -0500960func Errored(t *testing.T, desc string, errs []error) bool {
961 t.Helper()
962 if len(errs) > 0 {
963 for _, err := range errs {
964 t.Errorf("%s: %s", desc, err)
965 }
966 return true
967 }
968 return false
969}
970
Jingwen Chen041b1842021-02-01 00:23:25 -0500971type bp2buildMutator = func(android.TopDownMutatorContext)
972
973func TestBp2BuildInlinesDefaults(t *testing.T) {
974 testCases := []struct {
975 moduleTypesUnderTest map[string]android.ModuleFactory
976 bp2buildMutatorsUnderTest map[string]bp2buildMutator
977 bp string
978 expectedBazelTarget string
979 description string
980 }{
981 {
982 moduleTypesUnderTest: map[string]android.ModuleFactory{
983 "genrule": genrule.GenRuleFactory,
984 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
985 },
986 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
987 "genrule": genrule.GenruleBp2Build,
988 },
989 bp: `genrule_defaults {
990 name: "gen_defaults",
991 cmd: "do-something $(in) $(out)",
992}
993genrule {
994 name: "gen",
995 out: ["out"],
996 srcs: ["in1"],
997 defaults: ["gen_defaults"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500998 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -0500999}
1000`,
1001 expectedBazelTarget: `genrule(
1002 name = "gen",
1003 cmd = "do-something $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +00001004 outs = ["out"],
1005 srcs = ["in1"],
Jingwen Chen041b1842021-02-01 00:23:25 -05001006)`,
1007 description: "genrule applies properties from a genrule_defaults dependency if not specified",
1008 },
1009 {
1010 moduleTypesUnderTest: map[string]android.ModuleFactory{
1011 "genrule": genrule.GenRuleFactory,
1012 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1013 },
1014 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1015 "genrule": genrule.GenruleBp2Build,
1016 },
1017 bp: `genrule_defaults {
1018 name: "gen_defaults",
1019 out: ["out-from-defaults"],
1020 srcs: ["in-from-defaults"],
1021 cmd: "cmd-from-defaults",
1022}
1023genrule {
1024 name: "gen",
1025 out: ["out"],
1026 srcs: ["in1"],
1027 defaults: ["gen_defaults"],
1028 cmd: "do-something $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001029 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001030}
1031`,
1032 expectedBazelTarget: `genrule(
1033 name = "gen",
1034 cmd = "do-something $(SRCS) $(OUTS)",
1035 outs = [
1036 "out-from-defaults",
1037 "out",
1038 ],
1039 srcs = [
1040 "in-from-defaults",
1041 "in1",
1042 ],
1043)`,
1044 description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
1045 },
1046 {
1047 moduleTypesUnderTest: map[string]android.ModuleFactory{
1048 "genrule": genrule.GenRuleFactory,
1049 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1050 },
1051 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1052 "genrule": genrule.GenruleBp2Build,
1053 },
1054 bp: `genrule_defaults {
1055 name: "gen_defaults1",
1056 cmd: "cp $(in) $(out)",
1057}
1058
1059genrule_defaults {
1060 name: "gen_defaults2",
1061 srcs: ["in1"],
1062}
1063
1064genrule {
1065 name: "gen",
1066 out: ["out"],
1067 defaults: ["gen_defaults1", "gen_defaults2"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001068 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001069}
1070`,
1071 expectedBazelTarget: `genrule(
1072 name = "gen",
1073 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +00001074 outs = ["out"],
1075 srcs = ["in1"],
Jingwen Chen041b1842021-02-01 00:23:25 -05001076)`,
1077 description: "genrule applies properties from list of genrule_defaults",
1078 },
1079 {
1080 moduleTypesUnderTest: map[string]android.ModuleFactory{
1081 "genrule": genrule.GenRuleFactory,
1082 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1083 },
1084 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1085 "genrule": genrule.GenruleBp2Build,
1086 },
1087 bp: `genrule_defaults {
1088 name: "gen_defaults1",
1089 defaults: ["gen_defaults2"],
1090 cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
1091}
1092
1093genrule_defaults {
1094 name: "gen_defaults2",
1095 defaults: ["gen_defaults3"],
1096 cmd: "cmd2 $(in) $(out)",
1097 out: ["out-from-2"],
1098 srcs: ["in1"],
1099}
1100
1101genrule_defaults {
1102 name: "gen_defaults3",
1103 out: ["out-from-3"],
1104 srcs: ["srcs-from-3"],
1105}
1106
1107genrule {
1108 name: "gen",
1109 out: ["out"],
1110 defaults: ["gen_defaults1"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001111 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001112}
1113`,
1114 expectedBazelTarget: `genrule(
1115 name = "gen",
1116 cmd = "cmd1 $(SRCS) $(OUTS)",
1117 outs = [
1118 "out-from-3",
1119 "out-from-2",
1120 "out",
1121 ],
1122 srcs = [
Jingwen Chen041b1842021-02-01 00:23:25 -05001123 "in1",
Jingwen Chen07027912021-03-15 06:02:43 -04001124 "srcs-from-3",
Jingwen Chen041b1842021-02-01 00:23:25 -05001125 ],
1126)`,
1127 description: "genrule applies properties from genrule_defaults transitively",
1128 },
1129 }
1130
1131 dir := "."
1132 for _, testCase := range testCases {
1133 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1134 ctx := android.NewTestContext(config)
1135 for m, factory := range testCase.moduleTypesUnderTest {
1136 ctx.RegisterModuleType(m, factory)
1137 }
1138 for mutator, f := range testCase.bp2buildMutatorsUnderTest {
1139 ctx.RegisterBp2BuildMutator(mutator, f)
1140 }
1141 ctx.RegisterForBazelConversion()
1142
1143 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1144 android.FailIfErrored(t, errs)
1145 _, errs = ctx.ResolveDependencies(config)
1146 android.FailIfErrored(t, errs)
1147
Jingwen Chen164e0862021-02-19 00:48:40 -05001148 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -05001149 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen041b1842021-02-01 00:23:25 -05001150 if actualCount := len(bazelTargets); actualCount != 1 {
1151 t.Fatalf("%s: Expected 1 bazel target, got %d", testCase.description, actualCount)
1152 }
1153
1154 actualBazelTarget := bazelTargets[0]
1155 if actualBazelTarget.content != testCase.expectedBazelTarget {
1156 t.Errorf(
1157 "%s: Expected generated Bazel target to be '%s', got '%s'",
1158 testCase.description,
1159 testCase.expectedBazelTarget,
1160 actualBazelTarget.content,
1161 )
1162 }
1163 }
1164}
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001165
Jingwen Chen12b4c272021-03-10 02:05:59 -05001166func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001167 testCases := []struct {
1168 moduleTypeUnderTest string
1169 moduleTypeUnderTestFactory android.ModuleFactory
1170 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1171 bp string
1172 expectedCount int
1173 description string
1174 }{
1175 {
1176 description: "explicitly unavailable",
1177 moduleTypeUnderTest: "filegroup",
1178 moduleTypeUnderTestFactory: android.FileGroupFactory,
1179 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1180 bp: `filegroup {
1181 name: "foo",
1182 srcs: ["a", "b"],
1183 bazel_module: { bp2build_available: false },
1184}`,
1185 expectedCount: 0,
1186 },
1187 {
1188 description: "implicitly unavailable",
1189 moduleTypeUnderTest: "filegroup",
1190 moduleTypeUnderTestFactory: android.FileGroupFactory,
1191 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1192 bp: `filegroup {
1193 name: "foo",
1194 srcs: ["a", "b"],
1195}`,
1196 expectedCount: 0,
1197 },
1198 {
1199 description: "explicitly available",
1200 moduleTypeUnderTest: "filegroup",
1201 moduleTypeUnderTestFactory: android.FileGroupFactory,
1202 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1203 bp: `filegroup {
1204 name: "foo",
1205 srcs: ["a", "b"],
1206 bazel_module: { bp2build_available: true },
1207}`,
1208 expectedCount: 1,
1209 },
1210 {
1211 description: "generates more than 1 target if needed",
1212 moduleTypeUnderTest: "custom",
1213 moduleTypeUnderTestFactory: customModuleFactory,
1214 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
1215 bp: `custom {
1216 name: "foo",
1217 bazel_module: { bp2build_available: true },
1218}`,
1219 expectedCount: 3,
1220 },
1221 }
1222
1223 dir := "."
1224 for _, testCase := range testCases {
1225 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1226 ctx := android.NewTestContext(config)
1227 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1228 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1229 ctx.RegisterForBazelConversion()
1230
1231 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1232 android.FailIfErrored(t, errs)
1233 _, errs = ctx.ResolveDependencies(config)
1234 android.FailIfErrored(t, errs)
1235
Jingwen Chen164e0862021-02-19 00:48:40 -05001236 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -05001237 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001238 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1239 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1240 }
1241 }
1242}
Liz Kammerba3ea162021-02-17 13:22:03 -05001243
Jingwen Chen12b4c272021-03-10 02:05:59 -05001244func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1245 testCases := []struct {
1246 moduleTypeUnderTest string
1247 moduleTypeUnderTestFactory android.ModuleFactory
1248 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1249 expectedCount map[string]int
1250 description string
1251 bp2buildConfig android.Bp2BuildConfig
1252 checkDir string
1253 fs map[string]string
1254 }{
1255 {
1256 description: "test bp2build config package and subpackages config",
1257 moduleTypeUnderTest: "filegroup",
1258 moduleTypeUnderTestFactory: android.FileGroupFactory,
1259 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1260 expectedCount: map[string]int{
1261 "migrated": 1,
1262 "migrated/but_not_really": 0,
1263 "migrated/but_not_really/but_really": 1,
1264 "not_migrated": 0,
1265 "also_not_migrated": 0,
1266 },
1267 bp2buildConfig: android.Bp2BuildConfig{
1268 "migrated": android.Bp2BuildDefaultTrueRecursively,
1269 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
1270 "not_migrated": android.Bp2BuildDefaultFalse,
1271 },
1272 fs: map[string]string{
1273 "migrated/Android.bp": `filegroup { name: "a" }`,
1274 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1275 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1276 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1277 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1278 },
1279 },
1280 {
1281 description: "test bp2build config opt-in and opt-out",
1282 moduleTypeUnderTest: "filegroup",
1283 moduleTypeUnderTestFactory: android.FileGroupFactory,
1284 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1285 expectedCount: map[string]int{
1286 "package-opt-in": 2,
1287 "package-opt-in/subpackage": 0,
1288 "package-opt-out": 1,
1289 "package-opt-out/subpackage": 0,
1290 },
1291 bp2buildConfig: android.Bp2BuildConfig{
1292 "package-opt-in": android.Bp2BuildDefaultFalse,
1293 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
1294 },
1295 fs: map[string]string{
1296 "package-opt-in/Android.bp": `
1297filegroup { name: "opt-in-a" }
1298filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1299filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1300`,
1301
1302 "package-opt-in/subpackage/Android.bp": `
1303filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1304`,
1305
1306 "package-opt-out/Android.bp": `
1307filegroup { name: "opt-out-a" }
1308filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1309filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1310`,
1311
1312 "package-opt-out/subpackage/Android.bp": `
1313filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1314filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1315`,
1316 },
1317 },
1318 }
1319
1320 dir := "."
1321 for _, testCase := range testCases {
1322 fs := make(map[string][]byte)
1323 toParse := []string{
1324 "Android.bp",
1325 }
1326 for f, content := range testCase.fs {
1327 if strings.HasSuffix(f, "Android.bp") {
1328 toParse = append(toParse, f)
1329 }
1330 fs[f] = []byte(content)
1331 }
1332 config := android.TestConfig(buildDir, nil, "", fs)
1333 ctx := android.NewTestContext(config)
1334 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1335 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1336 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
1337 ctx.RegisterForBazelConversion()
1338
1339 _, errs := ctx.ParseFileList(dir, toParse)
1340 android.FailIfErrored(t, errs)
1341 _, errs = ctx.ResolveDependencies(config)
1342 android.FailIfErrored(t, errs)
1343
1344 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1345
1346 // For each directory, test that the expected number of generated targets is correct.
1347 for dir, expectedCount := range testCase.expectedCount {
1348 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
1349 if actualCount := len(bazelTargets); actualCount != expectedCount {
1350 t.Fatalf(
1351 "%s: Expected %d bazel target for %s package, got %d",
1352 testCase.description,
1353 expectedCount,
1354 dir,
1355 actualCount)
1356 }
1357
1358 }
1359 }
1360}
1361
Liz Kammerba3ea162021-02-17 13:22:03 -05001362func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
1363 testCases := []struct {
1364 description string
1365 moduleTypeUnderTest string
1366 moduleTypeUnderTestFactory android.ModuleFactory
1367 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
1368 preArchMutators []android.RegisterMutatorFunc
1369 depsMutators []android.RegisterMutatorFunc
1370 bp string
1371 expectedBazelTargets []string
1372 fs map[string]string
1373 dir string
1374 }{
1375 {
1376 description: "filegroup bazel_module.label",
1377 moduleTypeUnderTest: "filegroup",
1378 moduleTypeUnderTestFactory: android.FileGroupFactory,
1379 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1380 bp: `filegroup {
1381 name: "fg_foo",
1382 bazel_module: { label: "//other:fg_foo" },
1383}`,
1384 expectedBazelTargets: []string{
1385 `// BUILD file`,
1386 },
1387 fs: map[string]string{
1388 "other/BUILD.bazel": `// BUILD file`,
1389 },
1390 },
1391 {
1392 description: "multiple bazel_module.label same BUILD",
1393 moduleTypeUnderTest: "filegroup",
1394 moduleTypeUnderTestFactory: android.FileGroupFactory,
1395 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1396 bp: `filegroup {
1397 name: "fg_foo",
1398 bazel_module: { label: "//other:fg_foo" },
1399}
1400
1401filegroup {
1402 name: "foo",
1403 bazel_module: { label: "//other:foo" },
1404}`,
1405 expectedBazelTargets: []string{
1406 `// BUILD file`,
1407 },
1408 fs: map[string]string{
1409 "other/BUILD.bazel": `// BUILD file`,
1410 },
1411 },
1412 {
1413 description: "filegroup bazel_module.label and bp2build",
1414 moduleTypeUnderTest: "filegroup",
1415 moduleTypeUnderTestFactory: android.FileGroupFactory,
1416 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1417 bp: `filegroup {
1418 name: "fg_foo",
1419 bazel_module: {
1420 label: "//other:fg_foo",
1421 bp2build_available: true,
1422 },
1423}`,
1424 expectedBazelTargets: []string{
1425 `filegroup(
1426 name = "fg_foo",
1427)`,
1428 `// BUILD file`,
1429 },
1430 fs: map[string]string{
1431 "other/BUILD.bazel": `// BUILD file`,
1432 },
1433 },
1434 {
1435 description: "filegroup bazel_module.label and filegroup bp2build",
1436 moduleTypeUnderTest: "filegroup",
1437 moduleTypeUnderTestFactory: android.FileGroupFactory,
1438 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1439 bp: `filegroup {
1440 name: "fg_foo",
1441 bazel_module: {
1442 label: "//other:fg_foo",
1443 },
1444}
1445
1446filegroup {
1447 name: "fg_bar",
1448 bazel_module: {
1449 bp2build_available: true,
1450 },
1451}`,
1452 expectedBazelTargets: []string{
1453 `filegroup(
1454 name = "fg_bar",
1455)`,
1456 `// BUILD file`,
1457 },
1458 fs: map[string]string{
1459 "other/BUILD.bazel": `// BUILD file`,
1460 },
1461 },
1462 }
1463
1464 dir := "."
1465 for _, testCase := range testCases {
1466 fs := make(map[string][]byte)
1467 toParse := []string{
1468 "Android.bp",
1469 }
1470 for f, content := range testCase.fs {
1471 if strings.HasSuffix(f, "Android.bp") {
1472 toParse = append(toParse, f)
1473 }
1474 fs[f] = []byte(content)
1475 }
1476 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
1477 ctx := android.NewTestContext(config)
1478 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1479 for _, m := range testCase.depsMutators {
1480 ctx.DepsBp2BuildMutators(m)
1481 }
1482 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1483 ctx.RegisterForBazelConversion()
1484
1485 _, errs := ctx.ParseFileList(dir, toParse)
1486 if Errored(t, testCase.description, errs) {
1487 continue
1488 }
1489 _, errs = ctx.ResolveDependencies(config)
1490 if Errored(t, testCase.description, errs) {
1491 continue
1492 }
1493
1494 checkDir := dir
1495 if testCase.dir != "" {
1496 checkDir = testCase.dir
1497 }
1498 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1499 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
1500 t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets)
1501 } else {
1502 for i, target := range bazelTargets {
1503 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
1504 t.Errorf(
1505 "%s: Expected generated Bazel target to be '%s', got '%s'",
1506 testCase.description,
1507 w,
1508 g,
1509 )
1510 }
1511 }
1512 }
1513 }
1514}