blob: 3fa1e2acf707cf5ce9b3173008533b5ea9f674ad [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 {
30 bp: `custom {
31 name: "foo",
32}
33 `,
34 expectedBazelTarget: `soong_module(
35 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050036 soong_module_name = "foo",
37 soong_module_type = "custom",
38 soong_module_variant = "",
39 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080040 ],
41)`,
42 },
43 {
44 bp: `custom {
45 name: "foo",
46 ramdisk: true,
47}
48 `,
49 expectedBazelTarget: `soong_module(
50 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050051 soong_module_name = "foo",
52 soong_module_type = "custom",
53 soong_module_variant = "",
54 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080055 ],
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 Chen288e2ba2021-01-25 04:36:04 -050067 soong_module_name = "foo",
68 soong_module_type = "custom",
69 soong_module_variant = "",
70 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080071 ],
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 Chen288e2ba2021-01-25 04:36:04 -050083 soong_module_name = "foo",
84 soong_module_type = "custom",
85 soong_module_variant = "",
86 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080087 ],
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 Chen288e2ba2021-01-25 04:36:04 -0500101 soong_module_name = "foo",
102 soong_module_type = "custom",
103 soong_module_variant = "",
104 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800105 ],
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 Chen288e2ba2021-01-25 04:36:04 -0500129 soong_module_name = "foo",
130 soong_module_type = "custom",
131 soong_module_variant = "",
132 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800133 ],
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 Chen288e2ba2021-01-25 04:36:04 -0500167 soong_module_name = "foo",
168 soong_module_type = "custom",
169 soong_module_variant = "",
170 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800171 ],
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 Chen164e0862021-02-19 00:48:40 -0500197
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800198 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 Chen164e0862021-02-19 00:48:40 -0500206 codegenCtx := NewCodegenContext(config, *ctx.Context, QueryView)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500207 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen4e4756d2021-01-24 21:13:13 -0500208 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
209 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800210 }
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 Chen73850672020-12-14 08:25:34 -0500217 actualBazelTarget.content,
218 )
219 }
220 }
221}
222
223func 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 Chen77e8b7b2021-02-05 03:03:24 -0500233 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500234}`,
235 expectedBazelTarget: `custom(
236 name = "foo",
237 string_list_prop = [
238 "a",
239 "b",
240 ],
241 string_prop = "a",
242)`,
243 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000244 {
245 bp: `custom {
246 name: "control_characters",
247 string_list_prop: ["\t", "\n"],
248 string_prop: "a\t\n\r",
249 bazel_module: { bp2build_available: true },
250}`,
251 expectedBazelTarget: `custom(
252 name = "control_characters",
253 string_list_prop = [
254 "\t",
255 "\n",
256 ],
257 string_prop = "a\t\n\r",
258)`,
259 },
Jingwen Chen73850672020-12-14 08:25:34 -0500260 }
261
262 dir := "."
263 for _, testCase := range testCases {
264 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
265 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500266
Jingwen Chen73850672020-12-14 08:25:34 -0500267 ctx.RegisterModuleType("custom", customModuleFactory)
268 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator)
269 ctx.RegisterForBazelConversion()
270
271 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
Liz Kammer356f7d42021-01-26 09:18:53 -0500272 if Errored(t, "", errs) {
273 continue
274 }
Jingwen Chen73850672020-12-14 08:25:34 -0500275 _, errs = ctx.ResolveDependencies(config)
Liz Kammer356f7d42021-01-26 09:18:53 -0500276 if Errored(t, "", errs) {
277 continue
278 }
Jingwen Chen73850672020-12-14 08:25:34 -0500279
Jingwen Chen164e0862021-02-19 00:48:40 -0500280 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500281 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen164e0862021-02-19 00:48:40 -0500282
Jingwen Chen4e4756d2021-01-24 21:13:13 -0500283 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
Liz Kammer356f7d42021-01-26 09:18:53 -0500284 t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount)
285 } else {
286 actualBazelTarget := bazelTargets[0]
287 if actualBazelTarget.content != testCase.expectedBazelTarget {
288 t.Errorf(
289 "Expected generated Bazel target to be '%s', got '%s'",
290 testCase.expectedBazelTarget,
291 actualBazelTarget.content,
292 )
293 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800294 }
295 }
296}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500297
Jingwen Chen40067de2021-01-26 21:58:43 -0500298func TestLoadStatements(t *testing.T) {
299 testCases := []struct {
300 bazelTargets BazelTargets
301 expectedLoadStatements string
302 }{
303 {
304 bazelTargets: BazelTargets{
305 BazelTarget{
306 name: "foo",
307 ruleClass: "cc_library",
308 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
309 },
310 },
311 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
312 },
313 {
314 bazelTargets: BazelTargets{
315 BazelTarget{
316 name: "foo",
317 ruleClass: "cc_library",
318 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
319 },
320 BazelTarget{
321 name: "bar",
322 ruleClass: "cc_library",
323 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
324 },
325 },
326 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
327 },
328 {
329 bazelTargets: BazelTargets{
330 BazelTarget{
331 name: "foo",
332 ruleClass: "cc_library",
333 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
334 },
335 BazelTarget{
336 name: "bar",
337 ruleClass: "cc_binary",
338 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
339 },
340 },
341 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
342 },
343 {
344 bazelTargets: BazelTargets{
345 BazelTarget{
346 name: "foo",
347 ruleClass: "cc_library",
348 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
349 },
350 BazelTarget{
351 name: "bar",
352 ruleClass: "cc_binary",
353 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
354 },
355 BazelTarget{
356 name: "baz",
357 ruleClass: "java_binary",
358 bzlLoadLocation: "//build/bazel/rules:java.bzl",
359 },
360 },
361 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
362load("//build/bazel/rules:java.bzl", "java_binary")`,
363 },
364 {
365 bazelTargets: BazelTargets{
366 BazelTarget{
367 name: "foo",
368 ruleClass: "cc_binary",
369 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
370 },
371 BazelTarget{
372 name: "bar",
373 ruleClass: "java_binary",
374 bzlLoadLocation: "//build/bazel/rules:java.bzl",
375 },
376 BazelTarget{
377 name: "baz",
378 ruleClass: "genrule",
379 // Note: no bzlLoadLocation for native rules
380 },
381 },
382 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
383load("//build/bazel/rules:java.bzl", "java_binary")`,
384 },
385 }
386
387 for _, testCase := range testCases {
388 actual := testCase.bazelTargets.LoadStatements()
389 expected := testCase.expectedLoadStatements
390 if actual != expected {
391 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
392 }
393 }
394
395}
396
397func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
398 testCases := []struct {
399 bp string
400 expectedBazelTarget string
401 expectedBazelTargetCount int
402 expectedLoadStatements string
403 }{
404 {
405 bp: `custom {
406 name: "bar",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500407 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500408}`,
409 expectedBazelTarget: `my_library(
410 name = "bar",
411)
412
413my_proto_library(
414 name = "bar_my_proto_library_deps",
415)
416
417proto_library(
418 name = "bar_proto_library_deps",
419)`,
420 expectedBazelTargetCount: 3,
421 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
422load("//build/bazel/rules:rules.bzl", "my_library")`,
423 },
424 }
425
426 dir := "."
427 for _, testCase := range testCases {
428 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
429 ctx := android.NewTestContext(config)
430 ctx.RegisterModuleType("custom", customModuleFactory)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500431 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark)
Jingwen Chen40067de2021-01-26 21:58:43 -0500432 ctx.RegisterForBazelConversion()
433
434 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
435 android.FailIfErrored(t, errs)
436 _, errs = ctx.ResolveDependencies(config)
437 android.FailIfErrored(t, errs)
438
Jingwen Chen164e0862021-02-19 00:48:40 -0500439 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500440 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen40067de2021-01-26 21:58:43 -0500441 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
442 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
443 }
444
445 actualBazelTargets := bazelTargets.String()
446 if actualBazelTargets != testCase.expectedBazelTarget {
447 t.Errorf(
448 "Expected generated Bazel target to be '%s', got '%s'",
449 testCase.expectedBazelTarget,
450 actualBazelTargets,
451 )
452 }
453
454 actualLoadStatements := bazelTargets.LoadStatements()
455 if actualLoadStatements != testCase.expectedLoadStatements {
456 t.Errorf(
457 "Expected generated load statements to be '%s', got '%s'",
458 testCase.expectedLoadStatements,
459 actualLoadStatements,
460 )
461 }
462 }
463}
464
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500465func TestModuleTypeBp2Build(t *testing.T) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500466 otherGenruleBp := map[string]string{
467 "other/Android.bp": `genrule {
468 name: "foo.tool",
469 out: ["foo_tool.out"],
470 srcs: ["foo_tool.in"],
471 cmd: "cp $(in) $(out)",
472}
473genrule {
474 name: "other.tool",
475 out: ["other_tool.out"],
476 srcs: ["other_tool.in"],
477 cmd: "cp $(in) $(out)",
478}`,
479 }
480
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500481 testCases := []struct {
Liz Kammer356f7d42021-01-26 09:18:53 -0500482 description string
Jingwen Chena42d6412021-01-26 21:57:27 -0500483 moduleTypeUnderTest string
484 moduleTypeUnderTestFactory android.ModuleFactory
485 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
Liz Kammer356f7d42021-01-26 09:18:53 -0500486 preArchMutators []android.RegisterMutatorFunc
487 depsMutators []android.RegisterMutatorFunc
Jingwen Chena42d6412021-01-26 21:57:27 -0500488 bp string
Liz Kammer356f7d42021-01-26 09:18:53 -0500489 expectedBazelTargets []string
490 fs map[string]string
491 dir string
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500492 }{
493 {
Liz Kammerebfcf672021-02-16 15:00:05 -0500494 description: "filegroup with does not specify srcs",
495 moduleTypeUnderTest: "filegroup",
496 moduleTypeUnderTestFactory: android.FileGroupFactory,
497 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
498 bp: `filegroup {
499 name: "fg_foo",
500 bazel_module: { bp2build_available: true },
501}`,
502 expectedBazelTargets: []string{
503 `filegroup(
504 name = "fg_foo",
505)`,
506 },
507 },
508 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500509 description: "filegroup with no srcs",
510 moduleTypeUnderTest: "filegroup",
511 moduleTypeUnderTestFactory: android.FileGroupFactory,
512 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500513 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500514 name: "fg_foo",
515 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500516 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500517}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500518 expectedBazelTargets: []string{
519 `filegroup(
520 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500521 srcs = [
522 ],
523)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500524 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500525 },
526 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500527 description: "filegroup with srcs",
528 moduleTypeUnderTest: "filegroup",
529 moduleTypeUnderTestFactory: android.FileGroupFactory,
530 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500531 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500532 name: "fg_foo",
533 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500534 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500535}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500536 expectedBazelTargets: []string{`filegroup(
537 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500538 srcs = [
539 "a",
540 "b",
541 ],
542)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500543 },
544 },
545 {
546 description: "filegroup with excludes srcs",
547 moduleTypeUnderTest: "filegroup",
548 moduleTypeUnderTestFactory: android.FileGroupFactory,
549 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
550 bp: `filegroup {
551 name: "fg_foo",
552 srcs: ["a", "b"],
553 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500554 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500555}`,
556 expectedBazelTargets: []string{`filegroup(
557 name = "fg_foo",
558 srcs = [
559 "b",
560 ],
561)`,
562 },
563 },
564 {
565 description: "filegroup with glob",
566 moduleTypeUnderTest: "filegroup",
567 moduleTypeUnderTestFactory: android.FileGroupFactory,
568 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
569 bp: `filegroup {
570 name: "foo",
571 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500572 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500573}`,
574 expectedBazelTargets: []string{`filegroup(
575 name = "foo",
576 srcs = [
577 "other/a.txt",
578 "other/b.txt",
579 "other/subdir/a.txt",
580 ],
581)`,
582 },
583 fs: map[string]string{
584 "other/a.txt": "",
585 "other/b.txt": "",
586 "other/subdir/a.txt": "",
587 "other/file": "",
588 },
589 },
590 {
591 description: "filegroup with glob in subdir",
592 moduleTypeUnderTest: "filegroup",
593 moduleTypeUnderTestFactory: android.FileGroupFactory,
594 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
595 bp: `filegroup {
596 name: "foo",
597 srcs: ["a.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500598 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500599}`,
600 dir: "other",
601 expectedBazelTargets: []string{`filegroup(
602 name = "fg_foo",
603 srcs = [
604 "a.txt",
605 "b.txt",
606 "subdir/a.txt",
607 ],
608)`,
609 },
610 fs: map[string]string{
611 "other/Android.bp": `filegroup {
612 name: "fg_foo",
613 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500614 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500615}`,
616 "other/a.txt": "",
617 "other/b.txt": "",
618 "other/subdir/a.txt": "",
619 "other/file": "",
620 },
621 },
622 {
623 description: "depends_on_other_dir_module",
624 moduleTypeUnderTest: "filegroup",
625 moduleTypeUnderTestFactory: android.FileGroupFactory,
626 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
627 bp: `filegroup {
628 name: "foobar",
629 srcs: [
630 ":foo",
631 "c",
632 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500633 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500634}`,
635 expectedBazelTargets: []string{`filegroup(
636 name = "foobar",
637 srcs = [
638 "//other:foo",
639 "c",
640 ],
641)`,
642 },
643 fs: map[string]string{
644 "other/Android.bp": `filegroup {
645 name: "foo",
646 srcs: ["a", "b"],
647}`,
648 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500649 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500650 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500651 description: "genrule with command line variable replacements",
652 moduleTypeUnderTest: "genrule",
653 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
654 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500655 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen316e07c2020-12-14 09:09:52 -0500656 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500657 name: "foo.tool",
658 out: ["foo_tool.out"],
659 srcs: ["foo_tool.in"],
660 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500661 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500662}
663
664genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500665 name: "foo",
666 out: ["foo.out"],
667 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500668 tools: [":foo.tool"],
669 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500670 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500671}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500672 expectedBazelTargets: []string{
673 `genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500674 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500675 cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500676 outs = [
677 "foo.out",
678 ],
679 srcs = [
680 "foo.in",
681 ],
682 tools = [
683 ":foo.tool",
684 ],
685)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500686 `genrule(
687 name = "foo.tool",
688 cmd = "cp $(SRCS) $(OUTS)",
689 outs = [
690 "foo_tool.out",
691 ],
692 srcs = [
693 "foo_tool.in",
694 ],
695)`,
696 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500697 },
698 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500699 description: "genrule using $(locations :label)",
700 moduleTypeUnderTest: "genrule",
701 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
702 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500703 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen316e07c2020-12-14 09:09:52 -0500704 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500705 name: "foo.tools",
706 out: ["foo_tool.out", "foo_tool2.out"],
707 srcs: ["foo_tool.in"],
708 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500709 bazel_module: { bp2build_available: true },
710}
Liz Kammer356f7d42021-01-26 09:18:53 -0500711
712genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500713 name: "foo",
714 out: ["foo.out"],
715 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500716 tools: [":foo.tools"],
717 cmd: "$(locations :foo.tools) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500718 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500719}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500720 expectedBazelTargets: []string{`genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500721 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500722 cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)",
723 outs = [
724 "foo.out",
725 ],
726 srcs = [
727 "foo.in",
728 ],
729 tools = [
730 ":foo.tools",
731 ],
732)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500733 `genrule(
734 name = "foo.tools",
735 cmd = "cp $(SRCS) $(OUTS)",
736 outs = [
737 "foo_tool.out",
738 "foo_tool2.out",
739 ],
740 srcs = [
741 "foo_tool.in",
742 ],
743)`,
744 },
745 },
746 {
747 description: "genrule using $(locations //absolute:label)",
748 moduleTypeUnderTest: "genrule",
749 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
750 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
751 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
752 bp: `genrule {
753 name: "foo",
754 out: ["foo.out"],
755 srcs: ["foo.in"],
756 tool_files: [":foo.tool"],
757 cmd: "$(locations :foo.tool) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500758 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500759}`,
760 expectedBazelTargets: []string{`genrule(
761 name = "foo",
762 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
763 outs = [
764 "foo.out",
765 ],
766 srcs = [
767 "foo.in",
768 ],
769 tools = [
770 "//other:foo.tool",
771 ],
772)`,
773 },
774 fs: otherGenruleBp,
775 },
776 {
777 description: "genrule srcs using $(locations //absolute:label)",
778 moduleTypeUnderTest: "genrule",
779 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
780 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
781 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
782 bp: `genrule {
783 name: "foo",
784 out: ["foo.out"],
785 srcs: [":other.tool"],
786 tool_files: [":foo.tool"],
787 cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500788 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500789}`,
790 expectedBazelTargets: []string{`genrule(
791 name = "foo",
792 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)",
793 outs = [
794 "foo.out",
795 ],
796 srcs = [
797 "//other:other.tool",
798 ],
799 tools = [
800 "//other:foo.tool",
801 ],
802)`,
803 },
804 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500805 },
806 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500807 description: "genrule using $(location) label should substitute first tool label automatically",
808 moduleTypeUnderTest: "genrule",
809 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
810 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500811 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500812 bp: `genrule {
813 name: "foo",
814 out: ["foo.out"],
815 srcs: ["foo.in"],
816 tool_files: [":foo.tool", ":other.tool"],
817 cmd: "$(location) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500818 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500819}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500820 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500821 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500822 cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500823 outs = [
824 "foo.out",
825 ],
826 srcs = [
827 "foo.in",
828 ],
829 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500830 "//other:foo.tool",
831 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500832 ],
833)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500834 },
835 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500836 },
837 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500838 description: "genrule using $(locations) label should substitute first tool label automatically",
839 moduleTypeUnderTest: "genrule",
840 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
841 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500842 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500843 bp: `genrule {
844 name: "foo",
845 out: ["foo.out"],
846 srcs: ["foo.in"],
847 tools: [":foo.tool", ":other.tool"],
848 cmd: "$(locations) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500849 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500850}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500851 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500852 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500853 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500854 outs = [
855 "foo.out",
856 ],
857 srcs = [
858 "foo.in",
859 ],
860 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500861 "//other:foo.tool",
862 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500863 ],
864)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500865 },
866 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500867 },
868 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500869 description: "genrule without tools or tool_files can convert successfully",
870 moduleTypeUnderTest: "genrule",
871 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
872 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500873 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500874 bp: `genrule {
875 name: "foo",
876 out: ["foo.out"],
877 srcs: ["foo.in"],
878 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500879 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500880}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500881 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500882 name = "foo",
883 cmd = "cp $(SRCS) $(OUTS)",
884 outs = [
885 "foo.out",
886 ],
887 srcs = [
888 "foo.in",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500889 ],
890)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500891 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500892 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500893 }
894
895 dir := "."
896 for _, testCase := range testCases {
Liz Kammer356f7d42021-01-26 09:18:53 -0500897 fs := make(map[string][]byte)
898 toParse := []string{
899 "Android.bp",
900 }
901 for f, content := range testCase.fs {
902 if strings.HasSuffix(f, "Android.bp") {
903 toParse = append(toParse, f)
904 }
905 fs[f] = []byte(content)
906 }
907 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500908 ctx := android.NewTestContext(config)
909 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer356f7d42021-01-26 09:18:53 -0500910 for _, m := range testCase.depsMutators {
911 ctx.DepsBp2BuildMutators(m)
912 }
Jingwen Chena42d6412021-01-26 21:57:27 -0500913 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500914 ctx.RegisterForBazelConversion()
915
Liz Kammer356f7d42021-01-26 09:18:53 -0500916 _, errs := ctx.ParseFileList(dir, toParse)
917 if Errored(t, testCase.description, errs) {
918 continue
919 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500920 _, errs = ctx.ResolveDependencies(config)
Liz Kammer356f7d42021-01-26 09:18:53 -0500921 if Errored(t, testCase.description, errs) {
922 continue
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500923 }
924
Liz Kammer356f7d42021-01-26 09:18:53 -0500925 checkDir := dir
926 if testCase.dir != "" {
927 checkDir = testCase.dir
928 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500929
930 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500931 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
Liz Kammer356f7d42021-01-26 09:18:53 -0500932 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
933 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
934 } else {
935 for i, target := range bazelTargets {
936 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
937 t.Errorf(
938 "%s: Expected generated Bazel target to be '%s', got '%s'",
939 testCase.description,
940 w,
941 g,
942 )
943 }
944 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500945 }
946 }
947}
Jingwen Chen041b1842021-02-01 00:23:25 -0500948
Liz Kammer356f7d42021-01-26 09:18:53 -0500949func Errored(t *testing.T, desc string, errs []error) bool {
950 t.Helper()
951 if len(errs) > 0 {
952 for _, err := range errs {
953 t.Errorf("%s: %s", desc, err)
954 }
955 return true
956 }
957 return false
958}
959
Jingwen Chen041b1842021-02-01 00:23:25 -0500960type bp2buildMutator = func(android.TopDownMutatorContext)
961
962func TestBp2BuildInlinesDefaults(t *testing.T) {
963 testCases := []struct {
964 moduleTypesUnderTest map[string]android.ModuleFactory
965 bp2buildMutatorsUnderTest map[string]bp2buildMutator
966 bp string
967 expectedBazelTarget string
968 description string
969 }{
970 {
971 moduleTypesUnderTest: map[string]android.ModuleFactory{
972 "genrule": genrule.GenRuleFactory,
973 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
974 },
975 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
976 "genrule": genrule.GenruleBp2Build,
977 },
978 bp: `genrule_defaults {
979 name: "gen_defaults",
980 cmd: "do-something $(in) $(out)",
981}
982genrule {
983 name: "gen",
984 out: ["out"],
985 srcs: ["in1"],
986 defaults: ["gen_defaults"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500987 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -0500988}
989`,
990 expectedBazelTarget: `genrule(
991 name = "gen",
992 cmd = "do-something $(SRCS) $(OUTS)",
993 outs = [
994 "out",
995 ],
996 srcs = [
997 "in1",
998 ],
999)`,
1000 description: "genrule applies properties from a genrule_defaults dependency if not specified",
1001 },
1002 {
1003 moduleTypesUnderTest: map[string]android.ModuleFactory{
1004 "genrule": genrule.GenRuleFactory,
1005 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1006 },
1007 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1008 "genrule": genrule.GenruleBp2Build,
1009 },
1010 bp: `genrule_defaults {
1011 name: "gen_defaults",
1012 out: ["out-from-defaults"],
1013 srcs: ["in-from-defaults"],
1014 cmd: "cmd-from-defaults",
1015}
1016genrule {
1017 name: "gen",
1018 out: ["out"],
1019 srcs: ["in1"],
1020 defaults: ["gen_defaults"],
1021 cmd: "do-something $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001022 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001023}
1024`,
1025 expectedBazelTarget: `genrule(
1026 name = "gen",
1027 cmd = "do-something $(SRCS) $(OUTS)",
1028 outs = [
1029 "out-from-defaults",
1030 "out",
1031 ],
1032 srcs = [
1033 "in-from-defaults",
1034 "in1",
1035 ],
1036)`,
1037 description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
1038 },
1039 {
1040 moduleTypesUnderTest: map[string]android.ModuleFactory{
1041 "genrule": genrule.GenRuleFactory,
1042 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1043 },
1044 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1045 "genrule": genrule.GenruleBp2Build,
1046 },
1047 bp: `genrule_defaults {
1048 name: "gen_defaults1",
1049 cmd: "cp $(in) $(out)",
1050}
1051
1052genrule_defaults {
1053 name: "gen_defaults2",
1054 srcs: ["in1"],
1055}
1056
1057genrule {
1058 name: "gen",
1059 out: ["out"],
1060 defaults: ["gen_defaults1", "gen_defaults2"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001061 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001062}
1063`,
1064 expectedBazelTarget: `genrule(
1065 name = "gen",
1066 cmd = "cp $(SRCS) $(OUTS)",
1067 outs = [
1068 "out",
1069 ],
1070 srcs = [
1071 "in1",
1072 ],
1073)`,
1074 description: "genrule applies properties from list of genrule_defaults",
1075 },
1076 {
1077 moduleTypesUnderTest: map[string]android.ModuleFactory{
1078 "genrule": genrule.GenRuleFactory,
1079 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1080 },
1081 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1082 "genrule": genrule.GenruleBp2Build,
1083 },
1084 bp: `genrule_defaults {
1085 name: "gen_defaults1",
1086 defaults: ["gen_defaults2"],
1087 cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
1088}
1089
1090genrule_defaults {
1091 name: "gen_defaults2",
1092 defaults: ["gen_defaults3"],
1093 cmd: "cmd2 $(in) $(out)",
1094 out: ["out-from-2"],
1095 srcs: ["in1"],
1096}
1097
1098genrule_defaults {
1099 name: "gen_defaults3",
1100 out: ["out-from-3"],
1101 srcs: ["srcs-from-3"],
1102}
1103
1104genrule {
1105 name: "gen",
1106 out: ["out"],
1107 defaults: ["gen_defaults1"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001108 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001109}
1110`,
1111 expectedBazelTarget: `genrule(
1112 name = "gen",
1113 cmd = "cmd1 $(SRCS) $(OUTS)",
1114 outs = [
1115 "out-from-3",
1116 "out-from-2",
1117 "out",
1118 ],
1119 srcs = [
1120 "srcs-from-3",
1121 "in1",
1122 ],
1123)`,
1124 description: "genrule applies properties from genrule_defaults transitively",
1125 },
1126 }
1127
1128 dir := "."
1129 for _, testCase := range testCases {
1130 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1131 ctx := android.NewTestContext(config)
1132 for m, factory := range testCase.moduleTypesUnderTest {
1133 ctx.RegisterModuleType(m, factory)
1134 }
1135 for mutator, f := range testCase.bp2buildMutatorsUnderTest {
1136 ctx.RegisterBp2BuildMutator(mutator, f)
1137 }
1138 ctx.RegisterForBazelConversion()
1139
1140 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1141 android.FailIfErrored(t, errs)
1142 _, errs = ctx.ResolveDependencies(config)
1143 android.FailIfErrored(t, errs)
1144
Jingwen Chen164e0862021-02-19 00:48:40 -05001145 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -05001146 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen041b1842021-02-01 00:23:25 -05001147 if actualCount := len(bazelTargets); actualCount != 1 {
1148 t.Fatalf("%s: Expected 1 bazel target, got %d", testCase.description, actualCount)
1149 }
1150
1151 actualBazelTarget := bazelTargets[0]
1152 if actualBazelTarget.content != testCase.expectedBazelTarget {
1153 t.Errorf(
1154 "%s: Expected generated Bazel target to be '%s', got '%s'",
1155 testCase.description,
1156 testCase.expectedBazelTarget,
1157 actualBazelTarget.content,
1158 )
1159 }
1160 }
1161}
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001162
Jingwen Chen12b4c272021-03-10 02:05:59 -05001163func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001164 testCases := []struct {
1165 moduleTypeUnderTest string
1166 moduleTypeUnderTestFactory android.ModuleFactory
1167 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1168 bp string
1169 expectedCount int
1170 description string
1171 }{
1172 {
1173 description: "explicitly unavailable",
1174 moduleTypeUnderTest: "filegroup",
1175 moduleTypeUnderTestFactory: android.FileGroupFactory,
1176 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1177 bp: `filegroup {
1178 name: "foo",
1179 srcs: ["a", "b"],
1180 bazel_module: { bp2build_available: false },
1181}`,
1182 expectedCount: 0,
1183 },
1184 {
1185 description: "implicitly unavailable",
1186 moduleTypeUnderTest: "filegroup",
1187 moduleTypeUnderTestFactory: android.FileGroupFactory,
1188 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1189 bp: `filegroup {
1190 name: "foo",
1191 srcs: ["a", "b"],
1192}`,
1193 expectedCount: 0,
1194 },
1195 {
1196 description: "explicitly available",
1197 moduleTypeUnderTest: "filegroup",
1198 moduleTypeUnderTestFactory: android.FileGroupFactory,
1199 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1200 bp: `filegroup {
1201 name: "foo",
1202 srcs: ["a", "b"],
1203 bazel_module: { bp2build_available: true },
1204}`,
1205 expectedCount: 1,
1206 },
1207 {
1208 description: "generates more than 1 target if needed",
1209 moduleTypeUnderTest: "custom",
1210 moduleTypeUnderTestFactory: customModuleFactory,
1211 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
1212 bp: `custom {
1213 name: "foo",
1214 bazel_module: { bp2build_available: true },
1215}`,
1216 expectedCount: 3,
1217 },
1218 }
1219
1220 dir := "."
1221 for _, testCase := range testCases {
1222 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1223 ctx := android.NewTestContext(config)
1224 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1225 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1226 ctx.RegisterForBazelConversion()
1227
1228 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1229 android.FailIfErrored(t, errs)
1230 _, errs = ctx.ResolveDependencies(config)
1231 android.FailIfErrored(t, errs)
1232
Jingwen Chen164e0862021-02-19 00:48:40 -05001233 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -05001234 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001235 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1236 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1237 }
1238 }
1239}
Liz Kammerba3ea162021-02-17 13:22:03 -05001240
Jingwen Chen12b4c272021-03-10 02:05:59 -05001241func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1242 testCases := []struct {
1243 moduleTypeUnderTest string
1244 moduleTypeUnderTestFactory android.ModuleFactory
1245 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1246 expectedCount map[string]int
1247 description string
1248 bp2buildConfig android.Bp2BuildConfig
1249 checkDir string
1250 fs map[string]string
1251 }{
1252 {
1253 description: "test bp2build config package and subpackages config",
1254 moduleTypeUnderTest: "filegroup",
1255 moduleTypeUnderTestFactory: android.FileGroupFactory,
1256 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1257 expectedCount: map[string]int{
1258 "migrated": 1,
1259 "migrated/but_not_really": 0,
1260 "migrated/but_not_really/but_really": 1,
1261 "not_migrated": 0,
1262 "also_not_migrated": 0,
1263 },
1264 bp2buildConfig: android.Bp2BuildConfig{
1265 "migrated": android.Bp2BuildDefaultTrueRecursively,
1266 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
1267 "not_migrated": android.Bp2BuildDefaultFalse,
1268 },
1269 fs: map[string]string{
1270 "migrated/Android.bp": `filegroup { name: "a" }`,
1271 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1272 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1273 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1274 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1275 },
1276 },
1277 {
1278 description: "test bp2build config opt-in and opt-out",
1279 moduleTypeUnderTest: "filegroup",
1280 moduleTypeUnderTestFactory: android.FileGroupFactory,
1281 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1282 expectedCount: map[string]int{
1283 "package-opt-in": 2,
1284 "package-opt-in/subpackage": 0,
1285 "package-opt-out": 1,
1286 "package-opt-out/subpackage": 0,
1287 },
1288 bp2buildConfig: android.Bp2BuildConfig{
1289 "package-opt-in": android.Bp2BuildDefaultFalse,
1290 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
1291 },
1292 fs: map[string]string{
1293 "package-opt-in/Android.bp": `
1294filegroup { name: "opt-in-a" }
1295filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1296filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1297`,
1298
1299 "package-opt-in/subpackage/Android.bp": `
1300filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1301`,
1302
1303 "package-opt-out/Android.bp": `
1304filegroup { name: "opt-out-a" }
1305filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1306filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1307`,
1308
1309 "package-opt-out/subpackage/Android.bp": `
1310filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1311filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1312`,
1313 },
1314 },
1315 }
1316
1317 dir := "."
1318 for _, testCase := range testCases {
1319 fs := make(map[string][]byte)
1320 toParse := []string{
1321 "Android.bp",
1322 }
1323 for f, content := range testCase.fs {
1324 if strings.HasSuffix(f, "Android.bp") {
1325 toParse = append(toParse, f)
1326 }
1327 fs[f] = []byte(content)
1328 }
1329 config := android.TestConfig(buildDir, nil, "", fs)
1330 ctx := android.NewTestContext(config)
1331 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1332 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1333 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
1334 ctx.RegisterForBazelConversion()
1335
1336 _, errs := ctx.ParseFileList(dir, toParse)
1337 android.FailIfErrored(t, errs)
1338 _, errs = ctx.ResolveDependencies(config)
1339 android.FailIfErrored(t, errs)
1340
1341 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1342
1343 // For each directory, test that the expected number of generated targets is correct.
1344 for dir, expectedCount := range testCase.expectedCount {
1345 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
1346 if actualCount := len(bazelTargets); actualCount != expectedCount {
1347 t.Fatalf(
1348 "%s: Expected %d bazel target for %s package, got %d",
1349 testCase.description,
1350 expectedCount,
1351 dir,
1352 actualCount)
1353 }
1354
1355 }
1356 }
1357}
1358
Liz Kammerba3ea162021-02-17 13:22:03 -05001359func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
1360 testCases := []struct {
1361 description string
1362 moduleTypeUnderTest string
1363 moduleTypeUnderTestFactory android.ModuleFactory
1364 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
1365 preArchMutators []android.RegisterMutatorFunc
1366 depsMutators []android.RegisterMutatorFunc
1367 bp string
1368 expectedBazelTargets []string
1369 fs map[string]string
1370 dir string
1371 }{
1372 {
1373 description: "filegroup bazel_module.label",
1374 moduleTypeUnderTest: "filegroup",
1375 moduleTypeUnderTestFactory: android.FileGroupFactory,
1376 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1377 bp: `filegroup {
1378 name: "fg_foo",
1379 bazel_module: { label: "//other:fg_foo" },
1380}`,
1381 expectedBazelTargets: []string{
1382 `// BUILD file`,
1383 },
1384 fs: map[string]string{
1385 "other/BUILD.bazel": `// BUILD file`,
1386 },
1387 },
1388 {
1389 description: "multiple bazel_module.label same BUILD",
1390 moduleTypeUnderTest: "filegroup",
1391 moduleTypeUnderTestFactory: android.FileGroupFactory,
1392 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1393 bp: `filegroup {
1394 name: "fg_foo",
1395 bazel_module: { label: "//other:fg_foo" },
1396}
1397
1398filegroup {
1399 name: "foo",
1400 bazel_module: { label: "//other:foo" },
1401}`,
1402 expectedBazelTargets: []string{
1403 `// BUILD file`,
1404 },
1405 fs: map[string]string{
1406 "other/BUILD.bazel": `// BUILD file`,
1407 },
1408 },
1409 {
1410 description: "filegroup bazel_module.label and bp2build",
1411 moduleTypeUnderTest: "filegroup",
1412 moduleTypeUnderTestFactory: android.FileGroupFactory,
1413 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1414 bp: `filegroup {
1415 name: "fg_foo",
1416 bazel_module: {
1417 label: "//other:fg_foo",
1418 bp2build_available: true,
1419 },
1420}`,
1421 expectedBazelTargets: []string{
1422 `filegroup(
1423 name = "fg_foo",
1424)`,
1425 `// BUILD file`,
1426 },
1427 fs: map[string]string{
1428 "other/BUILD.bazel": `// BUILD file`,
1429 },
1430 },
1431 {
1432 description: "filegroup bazel_module.label and filegroup bp2build",
1433 moduleTypeUnderTest: "filegroup",
1434 moduleTypeUnderTestFactory: android.FileGroupFactory,
1435 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1436 bp: `filegroup {
1437 name: "fg_foo",
1438 bazel_module: {
1439 label: "//other:fg_foo",
1440 },
1441}
1442
1443filegroup {
1444 name: "fg_bar",
1445 bazel_module: {
1446 bp2build_available: true,
1447 },
1448}`,
1449 expectedBazelTargets: []string{
1450 `filegroup(
1451 name = "fg_bar",
1452)`,
1453 `// BUILD file`,
1454 },
1455 fs: map[string]string{
1456 "other/BUILD.bazel": `// BUILD file`,
1457 },
1458 },
1459 }
1460
1461 dir := "."
1462 for _, testCase := range testCases {
1463 fs := make(map[string][]byte)
1464 toParse := []string{
1465 "Android.bp",
1466 }
1467 for f, content := range testCase.fs {
1468 if strings.HasSuffix(f, "Android.bp") {
1469 toParse = append(toParse, f)
1470 }
1471 fs[f] = []byte(content)
1472 }
1473 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
1474 ctx := android.NewTestContext(config)
1475 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1476 for _, m := range testCase.depsMutators {
1477 ctx.DepsBp2BuildMutators(m)
1478 }
1479 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1480 ctx.RegisterForBazelConversion()
1481
1482 _, errs := ctx.ParseFileList(dir, toParse)
1483 if Errored(t, testCase.description, errs) {
1484 continue
1485 }
1486 _, errs = ctx.ResolveDependencies(config)
1487 if Errored(t, testCase.description, errs) {
1488 continue
1489 }
1490
1491 checkDir := dir
1492 if testCase.dir != "" {
1493 checkDir = testCase.dir
1494 }
1495 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1496 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
1497 t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets)
1498 } else {
1499 for i, target := range bazelTargets {
1500 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
1501 t.Errorf(
1502 "%s: Expected generated Bazel target to be '%s', got '%s'",
1503 testCase.description,
1504 w,
1505 g,
1506 )
1507 }
1508 }
1509 }
1510 }
1511}