blob: 1ede4428a09d38c470e51053741f3fa5a09980cd [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 {
207 bp string
208 expectedBazelTarget string
209 }{
210 {
211 bp: `custom {
212 name: "foo",
213 string_list_prop: ["a", "b"],
214 string_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500215 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500216}`,
217 expectedBazelTarget: `custom(
218 name = "foo",
219 string_list_prop = [
220 "a",
221 "b",
222 ],
223 string_prop = "a",
224)`,
225 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000226 {
227 bp: `custom {
228 name: "control_characters",
229 string_list_prop: ["\t", "\n"],
230 string_prop: "a\t\n\r",
231 bazel_module: { bp2build_available: true },
232}`,
233 expectedBazelTarget: `custom(
234 name = "control_characters",
235 string_list_prop = [
236 "\t",
237 "\n",
238 ],
239 string_prop = "a\t\n\r",
240)`,
241 },
Jingwen Chen73850672020-12-14 08:25:34 -0500242 }
243
244 dir := "."
245 for _, testCase := range testCases {
246 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
247 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500248
Jingwen Chen73850672020-12-14 08:25:34 -0500249 ctx.RegisterModuleType("custom", customModuleFactory)
250 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator)
251 ctx.RegisterForBazelConversion()
252
253 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
Liz Kammer356f7d42021-01-26 09:18:53 -0500254 if Errored(t, "", errs) {
255 continue
256 }
Jingwen Chen73850672020-12-14 08:25:34 -0500257 _, errs = ctx.ResolveDependencies(config)
Liz Kammer356f7d42021-01-26 09:18:53 -0500258 if Errored(t, "", errs) {
259 continue
260 }
Jingwen Chen73850672020-12-14 08:25:34 -0500261
Jingwen Chen164e0862021-02-19 00:48:40 -0500262 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500263 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen164e0862021-02-19 00:48:40 -0500264
Jingwen Chen4e4756d2021-01-24 21:13:13 -0500265 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
Liz Kammer356f7d42021-01-26 09:18:53 -0500266 t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount)
267 } else {
268 actualBazelTarget := bazelTargets[0]
269 if actualBazelTarget.content != testCase.expectedBazelTarget {
270 t.Errorf(
271 "Expected generated Bazel target to be '%s', got '%s'",
272 testCase.expectedBazelTarget,
273 actualBazelTarget.content,
274 )
275 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800276 }
277 }
278}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500279
Jingwen Chen40067de2021-01-26 21:58:43 -0500280func TestLoadStatements(t *testing.T) {
281 testCases := []struct {
282 bazelTargets BazelTargets
283 expectedLoadStatements string
284 }{
285 {
286 bazelTargets: BazelTargets{
287 BazelTarget{
288 name: "foo",
289 ruleClass: "cc_library",
290 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
291 },
292 },
293 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
294 },
295 {
296 bazelTargets: BazelTargets{
297 BazelTarget{
298 name: "foo",
299 ruleClass: "cc_library",
300 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
301 },
302 BazelTarget{
303 name: "bar",
304 ruleClass: "cc_library",
305 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
306 },
307 },
308 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
309 },
310 {
311 bazelTargets: BazelTargets{
312 BazelTarget{
313 name: "foo",
314 ruleClass: "cc_library",
315 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
316 },
317 BazelTarget{
318 name: "bar",
319 ruleClass: "cc_binary",
320 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
321 },
322 },
323 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
324 },
325 {
326 bazelTargets: BazelTargets{
327 BazelTarget{
328 name: "foo",
329 ruleClass: "cc_library",
330 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
331 },
332 BazelTarget{
333 name: "bar",
334 ruleClass: "cc_binary",
335 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
336 },
337 BazelTarget{
338 name: "baz",
339 ruleClass: "java_binary",
340 bzlLoadLocation: "//build/bazel/rules:java.bzl",
341 },
342 },
343 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
344load("//build/bazel/rules:java.bzl", "java_binary")`,
345 },
346 {
347 bazelTargets: BazelTargets{
348 BazelTarget{
349 name: "foo",
350 ruleClass: "cc_binary",
351 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
352 },
353 BazelTarget{
354 name: "bar",
355 ruleClass: "java_binary",
356 bzlLoadLocation: "//build/bazel/rules:java.bzl",
357 },
358 BazelTarget{
359 name: "baz",
360 ruleClass: "genrule",
361 // Note: no bzlLoadLocation for native rules
362 },
363 },
364 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
365load("//build/bazel/rules:java.bzl", "java_binary")`,
366 },
367 }
368
369 for _, testCase := range testCases {
370 actual := testCase.bazelTargets.LoadStatements()
371 expected := testCase.expectedLoadStatements
372 if actual != expected {
373 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
374 }
375 }
376
377}
378
379func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
380 testCases := []struct {
381 bp string
382 expectedBazelTarget string
383 expectedBazelTargetCount int
384 expectedLoadStatements string
385 }{
386 {
387 bp: `custom {
388 name: "bar",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500389 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500390}`,
391 expectedBazelTarget: `my_library(
392 name = "bar",
393)
394
395my_proto_library(
396 name = "bar_my_proto_library_deps",
397)
398
399proto_library(
400 name = "bar_proto_library_deps",
401)`,
402 expectedBazelTargetCount: 3,
403 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
404load("//build/bazel/rules:rules.bzl", "my_library")`,
405 },
406 }
407
408 dir := "."
409 for _, testCase := range testCases {
410 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
411 ctx := android.NewTestContext(config)
412 ctx.RegisterModuleType("custom", customModuleFactory)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500413 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark)
Jingwen Chen40067de2021-01-26 21:58:43 -0500414 ctx.RegisterForBazelConversion()
415
416 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
417 android.FailIfErrored(t, errs)
418 _, errs = ctx.ResolveDependencies(config)
419 android.FailIfErrored(t, errs)
420
Jingwen Chen164e0862021-02-19 00:48:40 -0500421 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500422 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen40067de2021-01-26 21:58:43 -0500423 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
424 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
425 }
426
427 actualBazelTargets := bazelTargets.String()
428 if actualBazelTargets != testCase.expectedBazelTarget {
429 t.Errorf(
430 "Expected generated Bazel target to be '%s', got '%s'",
431 testCase.expectedBazelTarget,
432 actualBazelTargets,
433 )
434 }
435
436 actualLoadStatements := bazelTargets.LoadStatements()
437 if actualLoadStatements != testCase.expectedLoadStatements {
438 t.Errorf(
439 "Expected generated load statements to be '%s', got '%s'",
440 testCase.expectedLoadStatements,
441 actualLoadStatements,
442 )
443 }
444 }
445}
446
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500447func TestModuleTypeBp2Build(t *testing.T) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500448 otherGenruleBp := map[string]string{
449 "other/Android.bp": `genrule {
450 name: "foo.tool",
451 out: ["foo_tool.out"],
452 srcs: ["foo_tool.in"],
453 cmd: "cp $(in) $(out)",
454}
455genrule {
456 name: "other.tool",
457 out: ["other_tool.out"],
458 srcs: ["other_tool.in"],
459 cmd: "cp $(in) $(out)",
460}`,
461 }
462
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500463 testCases := []struct {
Liz Kammer356f7d42021-01-26 09:18:53 -0500464 description string
Jingwen Chena42d6412021-01-26 21:57:27 -0500465 moduleTypeUnderTest string
466 moduleTypeUnderTestFactory android.ModuleFactory
467 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
Liz Kammer356f7d42021-01-26 09:18:53 -0500468 preArchMutators []android.RegisterMutatorFunc
469 depsMutators []android.RegisterMutatorFunc
Jingwen Chena42d6412021-01-26 21:57:27 -0500470 bp string
Liz Kammer356f7d42021-01-26 09:18:53 -0500471 expectedBazelTargets []string
472 fs map[string]string
473 dir string
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500474 }{
475 {
Liz Kammerebfcf672021-02-16 15:00:05 -0500476 description: "filegroup with does not specify srcs",
477 moduleTypeUnderTest: "filegroup",
478 moduleTypeUnderTestFactory: android.FileGroupFactory,
479 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
480 bp: `filegroup {
481 name: "fg_foo",
482 bazel_module: { bp2build_available: true },
483}`,
484 expectedBazelTargets: []string{
485 `filegroup(
486 name = "fg_foo",
487)`,
488 },
489 },
490 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500491 description: "filegroup with no srcs",
492 moduleTypeUnderTest: "filegroup",
493 moduleTypeUnderTestFactory: android.FileGroupFactory,
494 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500495 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500496 name: "fg_foo",
497 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500498 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500499}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500500 expectedBazelTargets: []string{
501 `filegroup(
502 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500503)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500504 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500505 },
506 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500507 description: "filegroup with srcs",
508 moduleTypeUnderTest: "filegroup",
509 moduleTypeUnderTestFactory: android.FileGroupFactory,
510 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500511 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500512 name: "fg_foo",
513 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500514 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500515}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500516 expectedBazelTargets: []string{`filegroup(
517 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500518 srcs = [
519 "a",
520 "b",
521 ],
522)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500523 },
524 },
525 {
526 description: "filegroup with excludes srcs",
527 moduleTypeUnderTest: "filegroup",
528 moduleTypeUnderTestFactory: android.FileGroupFactory,
529 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
530 bp: `filegroup {
531 name: "fg_foo",
532 srcs: ["a", "b"],
533 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500534 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500535}`,
536 expectedBazelTargets: []string{`filegroup(
537 name = "fg_foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000538 srcs = ["b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500539)`,
540 },
541 },
542 {
543 description: "filegroup with glob",
544 moduleTypeUnderTest: "filegroup",
545 moduleTypeUnderTestFactory: android.FileGroupFactory,
546 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
547 bp: `filegroup {
548 name: "foo",
549 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500550 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500551}`,
552 expectedBazelTargets: []string{`filegroup(
553 name = "foo",
554 srcs = [
555 "other/a.txt",
556 "other/b.txt",
557 "other/subdir/a.txt",
558 ],
559)`,
560 },
561 fs: map[string]string{
562 "other/a.txt": "",
563 "other/b.txt": "",
564 "other/subdir/a.txt": "",
565 "other/file": "",
566 },
567 },
568 {
569 description: "filegroup with glob in subdir",
570 moduleTypeUnderTest: "filegroup",
571 moduleTypeUnderTestFactory: android.FileGroupFactory,
572 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
573 bp: `filegroup {
574 name: "foo",
575 srcs: ["a.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500576 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500577}`,
578 dir: "other",
579 expectedBazelTargets: []string{`filegroup(
580 name = "fg_foo",
581 srcs = [
582 "a.txt",
583 "b.txt",
584 "subdir/a.txt",
585 ],
586)`,
587 },
588 fs: map[string]string{
589 "other/Android.bp": `filegroup {
590 name: "fg_foo",
591 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500592 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500593}`,
594 "other/a.txt": "",
595 "other/b.txt": "",
596 "other/subdir/a.txt": "",
597 "other/file": "",
598 },
599 },
600 {
601 description: "depends_on_other_dir_module",
602 moduleTypeUnderTest: "filegroup",
603 moduleTypeUnderTestFactory: android.FileGroupFactory,
604 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
605 bp: `filegroup {
606 name: "foobar",
607 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000608 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500609 "c",
610 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500611 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500612}`,
613 expectedBazelTargets: []string{`filegroup(
614 name = "foobar",
615 srcs = [
616 "//other:foo",
617 "c",
618 ],
619)`,
620 },
621 fs: map[string]string{
622 "other/Android.bp": `filegroup {
623 name: "foo",
624 srcs: ["a", "b"],
625}`,
626 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500627 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500628 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500629 description: "genrule with command line variable replacements",
630 moduleTypeUnderTest: "genrule",
631 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
632 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500633 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen316e07c2020-12-14 09:09:52 -0500634 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500635 name: "foo.tool",
636 out: ["foo_tool.out"],
637 srcs: ["foo_tool.in"],
638 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500639 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500640}
641
642genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500643 name: "foo",
644 out: ["foo.out"],
645 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500646 tools: [":foo.tool"],
647 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500648 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500649}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500650 expectedBazelTargets: []string{
651 `genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500652 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500653 cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000654 outs = ["foo.out"],
655 srcs = ["foo.in"],
656 tools = [":foo.tool"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500657)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500658 `genrule(
659 name = "foo.tool",
660 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000661 outs = ["foo_tool.out"],
662 srcs = ["foo_tool.in"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500663)`,
664 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500665 },
666 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500667 description: "genrule using $(locations :label)",
668 moduleTypeUnderTest: "genrule",
669 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
670 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500671 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen316e07c2020-12-14 09:09:52 -0500672 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500673 name: "foo.tools",
674 out: ["foo_tool.out", "foo_tool2.out"],
675 srcs: ["foo_tool.in"],
676 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500677 bazel_module: { bp2build_available: true },
678}
Liz Kammer356f7d42021-01-26 09:18:53 -0500679
680genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500681 name: "foo",
682 out: ["foo.out"],
683 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500684 tools: [":foo.tools"],
685 cmd: "$(locations :foo.tools) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500686 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500687}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500688 expectedBazelTargets: []string{`genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500689 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500690 cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000691 outs = ["foo.out"],
692 srcs = ["foo.in"],
693 tools = [":foo.tools"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500694)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500695 `genrule(
696 name = "foo.tools",
697 cmd = "cp $(SRCS) $(OUTS)",
698 outs = [
699 "foo_tool.out",
700 "foo_tool2.out",
701 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000702 srcs = ["foo_tool.in"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500703)`,
704 },
705 },
706 {
707 description: "genrule using $(locations //absolute:label)",
708 moduleTypeUnderTest: "genrule",
709 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
710 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
711 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
712 bp: `genrule {
713 name: "foo",
714 out: ["foo.out"],
715 srcs: ["foo.in"],
716 tool_files: [":foo.tool"],
717 cmd: "$(locations :foo.tool) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500718 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500719}`,
720 expectedBazelTargets: []string{`genrule(
721 name = "foo",
722 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000723 outs = ["foo.out"],
724 srcs = ["foo.in"],
725 tools = ["//other:foo.tool"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500726)`,
727 },
728 fs: otherGenruleBp,
729 },
730 {
731 description: "genrule srcs using $(locations //absolute:label)",
732 moduleTypeUnderTest: "genrule",
733 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
734 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
735 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
736 bp: `genrule {
737 name: "foo",
738 out: ["foo.out"],
739 srcs: [":other.tool"],
740 tool_files: [":foo.tool"],
741 cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500742 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500743}`,
744 expectedBazelTargets: []string{`genrule(
745 name = "foo",
746 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000747 outs = ["foo.out"],
748 srcs = ["//other:other.tool"],
749 tools = ["//other:foo.tool"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500750)`,
751 },
752 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500753 },
754 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500755 description: "genrule using $(location) label should substitute first tool label automatically",
756 moduleTypeUnderTest: "genrule",
757 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
758 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500759 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500760 bp: `genrule {
761 name: "foo",
762 out: ["foo.out"],
763 srcs: ["foo.in"],
764 tool_files: [":foo.tool", ":other.tool"],
765 cmd: "$(location) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500766 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500767}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500768 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500769 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500770 cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000771 outs = ["foo.out"],
772 srcs = ["foo.in"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500773 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500774 "//other:foo.tool",
775 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500776 ],
777)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500778 },
779 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500780 },
781 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500782 description: "genrule using $(locations) label should substitute first tool label automatically",
783 moduleTypeUnderTest: "genrule",
784 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
785 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500786 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500787 bp: `genrule {
788 name: "foo",
789 out: ["foo.out"],
790 srcs: ["foo.in"],
791 tools: [":foo.tool", ":other.tool"],
792 cmd: "$(locations) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500793 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500794}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500795 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500796 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500797 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000798 outs = ["foo.out"],
799 srcs = ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500800 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500801 "//other:foo.tool",
802 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500803 ],
804)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500805 },
806 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500807 },
808 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500809 description: "genrule without tools or tool_files can convert successfully",
810 moduleTypeUnderTest: "genrule",
811 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
812 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500813 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500814 bp: `genrule {
815 name: "foo",
816 out: ["foo.out"],
817 srcs: ["foo.in"],
818 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500819 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500820}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500821 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500822 name = "foo",
823 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000824 outs = ["foo.out"],
825 srcs = ["foo.in"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500826)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500827 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500828 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500829 }
830
831 dir := "."
832 for _, testCase := range testCases {
Liz Kammer356f7d42021-01-26 09:18:53 -0500833 fs := make(map[string][]byte)
834 toParse := []string{
835 "Android.bp",
836 }
837 for f, content := range testCase.fs {
838 if strings.HasSuffix(f, "Android.bp") {
839 toParse = append(toParse, f)
840 }
841 fs[f] = []byte(content)
842 }
843 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500844 ctx := android.NewTestContext(config)
845 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer356f7d42021-01-26 09:18:53 -0500846 for _, m := range testCase.depsMutators {
847 ctx.DepsBp2BuildMutators(m)
848 }
Jingwen Chena42d6412021-01-26 21:57:27 -0500849 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500850 ctx.RegisterForBazelConversion()
851
Liz Kammer356f7d42021-01-26 09:18:53 -0500852 _, errs := ctx.ParseFileList(dir, toParse)
853 if Errored(t, testCase.description, errs) {
854 continue
855 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500856 _, errs = ctx.ResolveDependencies(config)
Liz Kammer356f7d42021-01-26 09:18:53 -0500857 if Errored(t, testCase.description, errs) {
858 continue
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500859 }
860
Liz Kammer356f7d42021-01-26 09:18:53 -0500861 checkDir := dir
862 if testCase.dir != "" {
863 checkDir = testCase.dir
864 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500865
866 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500867 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
Liz Kammer356f7d42021-01-26 09:18:53 -0500868 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
869 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
870 } else {
871 for i, target := range bazelTargets {
872 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
873 t.Errorf(
874 "%s: Expected generated Bazel target to be '%s', got '%s'",
875 testCase.description,
876 w,
877 g,
878 )
879 }
880 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500881 }
882 }
883}
Jingwen Chen041b1842021-02-01 00:23:25 -0500884
Liz Kammer356f7d42021-01-26 09:18:53 -0500885func Errored(t *testing.T, desc string, errs []error) bool {
886 t.Helper()
887 if len(errs) > 0 {
888 for _, err := range errs {
889 t.Errorf("%s: %s", desc, err)
890 }
891 return true
892 }
893 return false
894}
895
Jingwen Chen041b1842021-02-01 00:23:25 -0500896type bp2buildMutator = func(android.TopDownMutatorContext)
897
898func TestBp2BuildInlinesDefaults(t *testing.T) {
899 testCases := []struct {
900 moduleTypesUnderTest map[string]android.ModuleFactory
901 bp2buildMutatorsUnderTest map[string]bp2buildMutator
902 bp string
903 expectedBazelTarget string
904 description string
905 }{
906 {
907 moduleTypesUnderTest: map[string]android.ModuleFactory{
908 "genrule": genrule.GenRuleFactory,
909 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
910 },
911 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
912 "genrule": genrule.GenruleBp2Build,
913 },
914 bp: `genrule_defaults {
915 name: "gen_defaults",
916 cmd: "do-something $(in) $(out)",
917}
918genrule {
919 name: "gen",
920 out: ["out"],
921 srcs: ["in1"],
922 defaults: ["gen_defaults"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500923 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -0500924}
925`,
926 expectedBazelTarget: `genrule(
927 name = "gen",
928 cmd = "do-something $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000929 outs = ["out"],
930 srcs = ["in1"],
Jingwen Chen041b1842021-02-01 00:23:25 -0500931)`,
932 description: "genrule applies properties from a genrule_defaults dependency if not specified",
933 },
934 {
935 moduleTypesUnderTest: map[string]android.ModuleFactory{
936 "genrule": genrule.GenRuleFactory,
937 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
938 },
939 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
940 "genrule": genrule.GenruleBp2Build,
941 },
942 bp: `genrule_defaults {
943 name: "gen_defaults",
944 out: ["out-from-defaults"],
945 srcs: ["in-from-defaults"],
946 cmd: "cmd-from-defaults",
947}
948genrule {
949 name: "gen",
950 out: ["out"],
951 srcs: ["in1"],
952 defaults: ["gen_defaults"],
953 cmd: "do-something $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500954 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -0500955}
956`,
957 expectedBazelTarget: `genrule(
958 name = "gen",
959 cmd = "do-something $(SRCS) $(OUTS)",
960 outs = [
961 "out-from-defaults",
962 "out",
963 ],
964 srcs = [
965 "in-from-defaults",
966 "in1",
967 ],
968)`,
969 description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
970 },
971 {
972 moduleTypesUnderTest: map[string]android.ModuleFactory{
973 "genrule": genrule.GenRuleFactory,
974 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
975 },
976 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
977 "genrule": genrule.GenruleBp2Build,
978 },
979 bp: `genrule_defaults {
980 name: "gen_defaults1",
981 cmd: "cp $(in) $(out)",
982}
983
984genrule_defaults {
985 name: "gen_defaults2",
986 srcs: ["in1"],
987}
988
989genrule {
990 name: "gen",
991 out: ["out"],
992 defaults: ["gen_defaults1", "gen_defaults2"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500993 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -0500994}
995`,
996 expectedBazelTarget: `genrule(
997 name = "gen",
998 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000999 outs = ["out"],
1000 srcs = ["in1"],
Jingwen Chen041b1842021-02-01 00:23:25 -05001001)`,
1002 description: "genrule applies properties from list of genrule_defaults",
1003 },
1004 {
1005 moduleTypesUnderTest: map[string]android.ModuleFactory{
1006 "genrule": genrule.GenRuleFactory,
1007 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1008 },
1009 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1010 "genrule": genrule.GenruleBp2Build,
1011 },
1012 bp: `genrule_defaults {
1013 name: "gen_defaults1",
1014 defaults: ["gen_defaults2"],
1015 cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
1016}
1017
1018genrule_defaults {
1019 name: "gen_defaults2",
1020 defaults: ["gen_defaults3"],
1021 cmd: "cmd2 $(in) $(out)",
1022 out: ["out-from-2"],
1023 srcs: ["in1"],
1024}
1025
1026genrule_defaults {
1027 name: "gen_defaults3",
1028 out: ["out-from-3"],
1029 srcs: ["srcs-from-3"],
1030}
1031
1032genrule {
1033 name: "gen",
1034 out: ["out"],
1035 defaults: ["gen_defaults1"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001036 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001037}
1038`,
1039 expectedBazelTarget: `genrule(
1040 name = "gen",
1041 cmd = "cmd1 $(SRCS) $(OUTS)",
1042 outs = [
1043 "out-from-3",
1044 "out-from-2",
1045 "out",
1046 ],
1047 srcs = [
Jingwen Chen041b1842021-02-01 00:23:25 -05001048 "in1",
Jingwen Chen07027912021-03-15 06:02:43 -04001049 "srcs-from-3",
Jingwen Chen041b1842021-02-01 00:23:25 -05001050 ],
1051)`,
1052 description: "genrule applies properties from genrule_defaults transitively",
1053 },
1054 }
1055
1056 dir := "."
1057 for _, testCase := range testCases {
1058 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1059 ctx := android.NewTestContext(config)
1060 for m, factory := range testCase.moduleTypesUnderTest {
1061 ctx.RegisterModuleType(m, factory)
1062 }
1063 for mutator, f := range testCase.bp2buildMutatorsUnderTest {
1064 ctx.RegisterBp2BuildMutator(mutator, f)
1065 }
1066 ctx.RegisterForBazelConversion()
1067
1068 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1069 android.FailIfErrored(t, errs)
1070 _, errs = ctx.ResolveDependencies(config)
1071 android.FailIfErrored(t, errs)
1072
Jingwen Chen164e0862021-02-19 00:48:40 -05001073 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -05001074 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen041b1842021-02-01 00:23:25 -05001075 if actualCount := len(bazelTargets); actualCount != 1 {
1076 t.Fatalf("%s: Expected 1 bazel target, got %d", testCase.description, actualCount)
1077 }
1078
1079 actualBazelTarget := bazelTargets[0]
1080 if actualBazelTarget.content != testCase.expectedBazelTarget {
1081 t.Errorf(
1082 "%s: Expected generated Bazel target to be '%s', got '%s'",
1083 testCase.description,
1084 testCase.expectedBazelTarget,
1085 actualBazelTarget.content,
1086 )
1087 }
1088 }
1089}
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001090
Jingwen Chen12b4c272021-03-10 02:05:59 -05001091func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001092 testCases := []struct {
1093 moduleTypeUnderTest string
1094 moduleTypeUnderTestFactory android.ModuleFactory
1095 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1096 bp string
1097 expectedCount int
1098 description string
1099 }{
1100 {
1101 description: "explicitly unavailable",
1102 moduleTypeUnderTest: "filegroup",
1103 moduleTypeUnderTestFactory: android.FileGroupFactory,
1104 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1105 bp: `filegroup {
1106 name: "foo",
1107 srcs: ["a", "b"],
1108 bazel_module: { bp2build_available: false },
1109}`,
1110 expectedCount: 0,
1111 },
1112 {
1113 description: "implicitly unavailable",
1114 moduleTypeUnderTest: "filegroup",
1115 moduleTypeUnderTestFactory: android.FileGroupFactory,
1116 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1117 bp: `filegroup {
1118 name: "foo",
1119 srcs: ["a", "b"],
1120}`,
1121 expectedCount: 0,
1122 },
1123 {
1124 description: "explicitly available",
1125 moduleTypeUnderTest: "filegroup",
1126 moduleTypeUnderTestFactory: android.FileGroupFactory,
1127 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1128 bp: `filegroup {
1129 name: "foo",
1130 srcs: ["a", "b"],
1131 bazel_module: { bp2build_available: true },
1132}`,
1133 expectedCount: 1,
1134 },
1135 {
1136 description: "generates more than 1 target if needed",
1137 moduleTypeUnderTest: "custom",
1138 moduleTypeUnderTestFactory: customModuleFactory,
1139 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
1140 bp: `custom {
1141 name: "foo",
1142 bazel_module: { bp2build_available: true },
1143}`,
1144 expectedCount: 3,
1145 },
1146 }
1147
1148 dir := "."
1149 for _, testCase := range testCases {
1150 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1151 ctx := android.NewTestContext(config)
1152 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1153 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1154 ctx.RegisterForBazelConversion()
1155
1156 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1157 android.FailIfErrored(t, errs)
1158 _, errs = ctx.ResolveDependencies(config)
1159 android.FailIfErrored(t, errs)
1160
Jingwen Chen164e0862021-02-19 00:48:40 -05001161 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -05001162 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001163 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1164 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1165 }
1166 }
1167}
Liz Kammerba3ea162021-02-17 13:22:03 -05001168
Jingwen Chen12b4c272021-03-10 02:05:59 -05001169func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1170 testCases := []struct {
1171 moduleTypeUnderTest string
1172 moduleTypeUnderTestFactory android.ModuleFactory
1173 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1174 expectedCount map[string]int
1175 description string
1176 bp2buildConfig android.Bp2BuildConfig
1177 checkDir string
1178 fs map[string]string
1179 }{
1180 {
1181 description: "test bp2build config package and subpackages config",
1182 moduleTypeUnderTest: "filegroup",
1183 moduleTypeUnderTestFactory: android.FileGroupFactory,
1184 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1185 expectedCount: map[string]int{
1186 "migrated": 1,
1187 "migrated/but_not_really": 0,
1188 "migrated/but_not_really/but_really": 1,
1189 "not_migrated": 0,
1190 "also_not_migrated": 0,
1191 },
1192 bp2buildConfig: android.Bp2BuildConfig{
1193 "migrated": android.Bp2BuildDefaultTrueRecursively,
1194 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
1195 "not_migrated": android.Bp2BuildDefaultFalse,
1196 },
1197 fs: map[string]string{
1198 "migrated/Android.bp": `filegroup { name: "a" }`,
1199 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1200 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1201 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1202 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1203 },
1204 },
1205 {
1206 description: "test bp2build config opt-in and opt-out",
1207 moduleTypeUnderTest: "filegroup",
1208 moduleTypeUnderTestFactory: android.FileGroupFactory,
1209 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1210 expectedCount: map[string]int{
1211 "package-opt-in": 2,
1212 "package-opt-in/subpackage": 0,
1213 "package-opt-out": 1,
1214 "package-opt-out/subpackage": 0,
1215 },
1216 bp2buildConfig: android.Bp2BuildConfig{
1217 "package-opt-in": android.Bp2BuildDefaultFalse,
1218 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
1219 },
1220 fs: map[string]string{
1221 "package-opt-in/Android.bp": `
1222filegroup { name: "opt-in-a" }
1223filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1224filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1225`,
1226
1227 "package-opt-in/subpackage/Android.bp": `
1228filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1229`,
1230
1231 "package-opt-out/Android.bp": `
1232filegroup { name: "opt-out-a" }
1233filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1234filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1235`,
1236
1237 "package-opt-out/subpackage/Android.bp": `
1238filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1239filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1240`,
1241 },
1242 },
1243 }
1244
1245 dir := "."
1246 for _, testCase := range testCases {
1247 fs := make(map[string][]byte)
1248 toParse := []string{
1249 "Android.bp",
1250 }
1251 for f, content := range testCase.fs {
1252 if strings.HasSuffix(f, "Android.bp") {
1253 toParse = append(toParse, f)
1254 }
1255 fs[f] = []byte(content)
1256 }
1257 config := android.TestConfig(buildDir, nil, "", fs)
1258 ctx := android.NewTestContext(config)
1259 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1260 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1261 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
1262 ctx.RegisterForBazelConversion()
1263
1264 _, errs := ctx.ParseFileList(dir, toParse)
1265 android.FailIfErrored(t, errs)
1266 _, errs = ctx.ResolveDependencies(config)
1267 android.FailIfErrored(t, errs)
1268
1269 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1270
1271 // For each directory, test that the expected number of generated targets is correct.
1272 for dir, expectedCount := range testCase.expectedCount {
1273 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
1274 if actualCount := len(bazelTargets); actualCount != expectedCount {
1275 t.Fatalf(
1276 "%s: Expected %d bazel target for %s package, got %d",
1277 testCase.description,
1278 expectedCount,
1279 dir,
1280 actualCount)
1281 }
1282
1283 }
1284 }
1285}
1286
Liz Kammerba3ea162021-02-17 13:22:03 -05001287func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
1288 testCases := []struct {
1289 description string
1290 moduleTypeUnderTest string
1291 moduleTypeUnderTestFactory android.ModuleFactory
1292 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
1293 preArchMutators []android.RegisterMutatorFunc
1294 depsMutators []android.RegisterMutatorFunc
1295 bp string
1296 expectedBazelTargets []string
1297 fs map[string]string
1298 dir string
1299 }{
1300 {
1301 description: "filegroup bazel_module.label",
1302 moduleTypeUnderTest: "filegroup",
1303 moduleTypeUnderTestFactory: android.FileGroupFactory,
1304 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1305 bp: `filegroup {
1306 name: "fg_foo",
1307 bazel_module: { label: "//other:fg_foo" },
1308}`,
1309 expectedBazelTargets: []string{
1310 `// BUILD file`,
1311 },
1312 fs: map[string]string{
1313 "other/BUILD.bazel": `// BUILD file`,
1314 },
1315 },
1316 {
1317 description: "multiple bazel_module.label same BUILD",
1318 moduleTypeUnderTest: "filegroup",
1319 moduleTypeUnderTestFactory: android.FileGroupFactory,
1320 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1321 bp: `filegroup {
1322 name: "fg_foo",
1323 bazel_module: { label: "//other:fg_foo" },
1324}
1325
1326filegroup {
1327 name: "foo",
1328 bazel_module: { label: "//other:foo" },
1329}`,
1330 expectedBazelTargets: []string{
1331 `// BUILD file`,
1332 },
1333 fs: map[string]string{
1334 "other/BUILD.bazel": `// BUILD file`,
1335 },
1336 },
1337 {
1338 description: "filegroup bazel_module.label and bp2build",
1339 moduleTypeUnderTest: "filegroup",
1340 moduleTypeUnderTestFactory: android.FileGroupFactory,
1341 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1342 bp: `filegroup {
1343 name: "fg_foo",
1344 bazel_module: {
1345 label: "//other:fg_foo",
1346 bp2build_available: true,
1347 },
1348}`,
1349 expectedBazelTargets: []string{
1350 `filegroup(
1351 name = "fg_foo",
1352)`,
1353 `// BUILD file`,
1354 },
1355 fs: map[string]string{
1356 "other/BUILD.bazel": `// BUILD file`,
1357 },
1358 },
1359 {
1360 description: "filegroup bazel_module.label and filegroup bp2build",
1361 moduleTypeUnderTest: "filegroup",
1362 moduleTypeUnderTestFactory: android.FileGroupFactory,
1363 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1364 bp: `filegroup {
1365 name: "fg_foo",
1366 bazel_module: {
1367 label: "//other:fg_foo",
1368 },
1369}
1370
1371filegroup {
1372 name: "fg_bar",
1373 bazel_module: {
1374 bp2build_available: true,
1375 },
1376}`,
1377 expectedBazelTargets: []string{
1378 `filegroup(
1379 name = "fg_bar",
1380)`,
1381 `// BUILD file`,
1382 },
1383 fs: map[string]string{
1384 "other/BUILD.bazel": `// BUILD file`,
1385 },
1386 },
1387 }
1388
1389 dir := "."
1390 for _, testCase := range testCases {
1391 fs := make(map[string][]byte)
1392 toParse := []string{
1393 "Android.bp",
1394 }
1395 for f, content := range testCase.fs {
1396 if strings.HasSuffix(f, "Android.bp") {
1397 toParse = append(toParse, f)
1398 }
1399 fs[f] = []byte(content)
1400 }
1401 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
1402 ctx := android.NewTestContext(config)
1403 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1404 for _, m := range testCase.depsMutators {
1405 ctx.DepsBp2BuildMutators(m)
1406 }
1407 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1408 ctx.RegisterForBazelConversion()
1409
1410 _, errs := ctx.ParseFileList(dir, toParse)
1411 if Errored(t, testCase.description, errs) {
1412 continue
1413 }
1414 _, errs = ctx.ResolveDependencies(config)
1415 if Errored(t, testCase.description, errs) {
1416 continue
1417 }
1418
1419 checkDir := dir
1420 if testCase.dir != "" {
1421 checkDir = testCase.dir
1422 }
1423 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1424 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
1425 t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets)
1426 } else {
1427 for i, target := range bazelTargets {
1428 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
1429 t.Errorf(
1430 "%s: Expected generated Bazel target to be '%s', got '%s'",
1431 testCase.description,
1432 w,
1433 g,
1434 )
1435 }
1436 }
1437 }
1438 }
1439}