blob: 10a13bf4e59251e54cd19ca197c8853542758fad [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 (
Liz Kammer6eff3232021-08-26 08:37:59 -040018 "fmt"
Liz Kammer356f7d42021-01-26 09:18:53 -050019 "strings"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080020 "testing"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000021
22 "android/soong/android"
Sam Delmerico24c56032022-03-28 19:53:03 +000023 "android/soong/android/allowlists"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000024 "android/soong/python"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080025)
26
27func TestGenerateSoongModuleTargets(t *testing.T) {
28 testCases := []struct {
Liz Kammerd366c902021-06-03 13:43:01 -040029 description string
Liz Kammer2dd9ca42020-11-25 16:06:39 -080030 bp string
31 expectedBazelTarget string
32 }{
33 {
Liz Kammerd366c902021-06-03 13:43:01 -040034 description: "only name",
Jingwen Chenb4628eb2021-04-08 14:40:57 +000035 bp: `custom { name: "foo" }
Liz Kammerd366c902021-06-03 13:43:01 -040036 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080037 expectedBazelTarget: `soong_module(
38 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050039 soong_module_name = "foo",
40 soong_module_type = "custom",
41 soong_module_variant = "",
42 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080043 ],
Liz Kammerd366c902021-06-03 13:43:01 -040044 bool_prop = False,
Liz Kammer46fb7ab2021-12-01 10:09:34 -050045 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080046)`,
47 },
48 {
Liz Kammerd366c902021-06-03 13:43:01 -040049 description: "handles bool",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080050 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040051 name: "foo",
52 bool_prop: true,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080053}
Liz Kammerd366c902021-06-03 13:43:01 -040054 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080055 expectedBazelTarget: `soong_module(
56 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050057 soong_module_name = "foo",
58 soong_module_type = "custom",
59 soong_module_variant = "",
60 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080061 ],
Liz Kammerd366c902021-06-03 13:43:01 -040062 bool_prop = True,
Liz Kammer46fb7ab2021-12-01 10:09:34 -050063 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080064)`,
65 },
66 {
Liz Kammerd366c902021-06-03 13:43:01 -040067 description: "string escaping",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080068 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040069 name: "foo",
70 owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080071}
Liz Kammerd366c902021-06-03 13:43:01 -040072 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080073 expectedBazelTarget: `soong_module(
74 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050075 soong_module_name = "foo",
76 soong_module_type = "custom",
77 soong_module_variant = "",
78 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080079 ],
Liz Kammerd366c902021-06-03 13:43:01 -040080 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080081 owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
Liz Kammer46fb7ab2021-12-01 10:09:34 -050082 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080083)`,
84 },
85 {
Liz Kammerd366c902021-06-03 13:43:01 -040086 description: "single item string list",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080087 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040088 name: "foo",
89 required: ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -080090}
Liz Kammerd366c902021-06-03 13:43:01 -040091 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080092 expectedBazelTarget: `soong_module(
93 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050094 soong_module_name = "foo",
95 soong_module_type = "custom",
96 soong_module_variant = "",
97 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080098 ],
Liz Kammerd366c902021-06-03 13:43:01 -040099 bool_prop = False,
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000100 required = ["bar"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500101 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800102)`,
103 },
104 {
Liz Kammerd366c902021-06-03 13:43:01 -0400105 description: "list of strings",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800106 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400107 name: "foo",
108 target_required: ["qux", "bazqux"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800109}
Liz Kammerd366c902021-06-03 13:43:01 -0400110 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800111 expectedBazelTarget: `soong_module(
112 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500113 soong_module_name = "foo",
114 soong_module_type = "custom",
115 soong_module_variant = "",
116 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800117 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400118 bool_prop = False,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500119 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800120 target_required = [
121 "qux",
122 "bazqux",
123 ],
124)`,
125 },
126 {
Liz Kammerd366c902021-06-03 13:43:01 -0400127 description: "dist/dists",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800128 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400129 name: "foo",
130 dist: {
131 targets: ["goal_foo"],
132 tag: ".foo",
133 },
134 dists: [{
135 targets: ["goal_bar"],
136 tag: ".bar",
137 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800138}
Liz Kammerd366c902021-06-03 13:43:01 -0400139 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800140 expectedBazelTarget: `soong_module(
141 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500142 soong_module_name = "foo",
143 soong_module_type = "custom",
144 soong_module_variant = "",
145 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800146 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400147 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800148 dist = {
149 "tag": ".foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000150 "targets": ["goal_foo"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800151 },
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000152 dists = [{
153 "tag": ".bar",
154 "targets": ["goal_bar"],
155 }],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500156 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800157)`,
158 },
159 {
Liz Kammerd366c902021-06-03 13:43:01 -0400160 description: "put it together",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800161 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400162 name: "foo",
163 required: ["bar"],
164 target_required: ["qux", "bazqux"],
165 bool_prop: true,
166 owner: "custom_owner",
167 dists: [
168 {
169 tag: ".tag",
170 targets: ["my_goal"],
171 },
172 ],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800173}
Liz Kammerd366c902021-06-03 13:43:01 -0400174 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800175 expectedBazelTarget: `soong_module(
176 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500177 soong_module_name = "foo",
178 soong_module_type = "custom",
179 soong_module_variant = "",
180 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800181 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400182 bool_prop = True,
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000183 dists = [{
184 "tag": ".tag",
185 "targets": ["my_goal"],
186 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800187 owner = "custom_owner",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000188 required = ["bar"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500189 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800190 target_required = [
191 "qux",
192 "bazqux",
193 ],
194)`,
195 },
196 }
197
198 dir := "."
199 for _, testCase := range testCases {
Liz Kammerd366c902021-06-03 13:43:01 -0400200 t.Run(testCase.description, func(t *testing.T) {
201 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
202 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500203
Liz Kammerdfeb1202022-05-13 17:20:20 -0400204 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
Liz Kammerd366c902021-06-03 13:43:01 -0400205 ctx.Register()
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800206
Liz Kammerd366c902021-06-03 13:43:01 -0400207 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
208 android.FailIfErrored(t, errs)
209 _, errs = ctx.PrepareBuildActions(config)
210 android.FailIfErrored(t, errs)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800211
Liz Kammerd366c902021-06-03 13:43:01 -0400212 codegenCtx := NewCodegenContext(config, *ctx.Context, QueryView)
Liz Kammer6eff3232021-08-26 08:37:59 -0400213 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
214 android.FailIfErrored(t, err)
Liz Kammerd366c902021-06-03 13:43:01 -0400215 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
216 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
217 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800218
Liz Kammerd366c902021-06-03 13:43:01 -0400219 actualBazelTarget := bazelTargets[0]
220 if actualBazelTarget.content != testCase.expectedBazelTarget {
221 t.Errorf(
222 "Expected generated Bazel target to be '%s', got '%s'",
223 testCase.expectedBazelTarget,
224 actualBazelTarget.content,
225 )
226 }
227 })
Jingwen Chen73850672020-12-14 08:25:34 -0500228 }
229}
230
231func TestGenerateBazelTargetModules(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000232 testCases := []Bp2buildTestCase{
Jingwen Chen73850672020-12-14 08:25:34 -0500233 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000234 Description: "string prop empty",
235 Blueprint: `custom {
236 name: "foo",
237 string_literal_prop: "",
238 bazel_module: { bp2build_available: true },
239}`,
240 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000241 MakeBazelTarget("custom", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000242 "string_literal_prop": `""`,
243 }),
244 },
245 },
246 {
247 Description: `string prop "PROP"`,
248 Blueprint: `custom {
249 name: "foo",
250 string_literal_prop: "PROP",
251 bazel_module: { bp2build_available: true },
252}`,
253 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000254 MakeBazelTarget("custom", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000255 "string_literal_prop": `"PROP"`,
256 }),
257 },
258 },
259 {
260 Description: `string prop arch variant`,
261 Blueprint: `custom {
262 name: "foo",
263 arch: {
264 arm: { string_literal_prop: "ARM" },
265 arm64: { string_literal_prop: "ARM64" },
266 },
267 bazel_module: { bp2build_available: true },
268}`,
269 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000270 MakeBazelTarget("custom", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000271 "string_literal_prop": `select({
272 "//build/bazel/platforms/arch:arm": "ARM",
273 "//build/bazel/platforms/arch:arm64": "ARM64",
274 "//conditions:default": None,
275 })`,
276 }),
277 },
278 },
279 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000280 Description: "string ptr props",
281 Blueprint: `custom {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500282 name: "foo",
283 string_ptr_prop: "",
284 bazel_module: { bp2build_available: true },
285}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000286 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000287 MakeBazelTarget("custom", "foo", AttrNameToString{
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500288 "string_ptr_prop": `""`,
289 }),
290 },
291 },
292 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000293 Description: "string list props",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000294 Blueprint: `custom {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400295 name: "foo",
Jingwen Chen73850672020-12-14 08:25:34 -0500296 string_list_prop: ["a", "b"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500297 string_ptr_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500298 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500299}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000300 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000301 MakeBazelTarget("custom", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500302 "string_list_prop": `[
Jingwen Chen73850672020-12-14 08:25:34 -0500303 "a",
304 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500305 ]`,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500306 "string_ptr_prop": `"a"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500307 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400308 },
Jingwen Chen73850672020-12-14 08:25:34 -0500309 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000310 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000311 Description: "control characters",
312 Blueprint: `custom {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500313 name: "foo",
Jingwen Chen58a12b82021-03-30 13:08:36 +0000314 string_list_prop: ["\t", "\n"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500315 string_ptr_prop: "a\t\n\r",
Jingwen Chen58a12b82021-03-30 13:08:36 +0000316 bazel_module: { bp2build_available: true },
317}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000318 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000319 MakeBazelTarget("custom", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500320 "string_list_prop": `[
Jingwen Chen58a12b82021-03-30 13:08:36 +0000321 "\t",
322 "\n",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500323 ]`,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500324 "string_ptr_prop": `"a\t\n\r"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500325 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400326 },
327 },
328 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000329 Description: "handles dep",
330 Blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400331 name: "has_dep",
332 arch_paths: [":dep"],
333 bazel_module: { bp2build_available: true },
334}
335
336custom {
337 name: "dep",
338 arch_paths: ["abc"],
339 bazel_module: { bp2build_available: true },
340}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000341 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000342 MakeBazelTarget("custom", "dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500343 "arch_paths": `["abc"]`,
344 }),
Alixe06d75b2022-08-31 18:28:19 +0000345 MakeBazelTarget("custom", "has_dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500346 "arch_paths": `[":dep"]`,
347 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400348 },
349 },
350 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000351 Description: "non-existent dep",
352 Blueprint: `custom {
Liz Kammerdaa09ef2021-12-15 15:35:38 -0500353 name: "has_dep",
354 arch_paths: [":dep"],
355 bazel_module: { bp2build_available: true },
356}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000357 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000358 MakeBazelTarget("custom", "has_dep", AttrNameToString{
Liz Kammerdaa09ef2021-12-15 15:35:38 -0500359 "arch_paths": `[":dep__BP2BUILD__MISSING__DEP"]`,
360 }),
361 },
362 },
363 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000364 Description: "arch-variant srcs",
365 Blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400366 name: "arch_paths",
367 arch: {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400368 x86: { arch_paths: ["x86.txt"] },
369 x86_64: { arch_paths: ["x86_64.txt"] },
370 arm: { arch_paths: ["arm.txt"] },
371 arm64: { arch_paths: ["arm64.txt"] },
372 },
373 target: {
374 linux: { arch_paths: ["linux.txt"] },
375 bionic: { arch_paths: ["bionic.txt"] },
376 host: { arch_paths: ["host.txt"] },
377 not_windows: { arch_paths: ["not_windows.txt"] },
378 android: { arch_paths: ["android.txt"] },
379 linux_musl: { arch_paths: ["linux_musl.txt"] },
380 musl: { arch_paths: ["musl.txt"] },
381 linux_glibc: { arch_paths: ["linux_glibc.txt"] },
382 glibc: { arch_paths: ["glibc.txt"] },
383 linux_bionic: { arch_paths: ["linux_bionic.txt"] },
384 darwin: { arch_paths: ["darwin.txt"] },
385 windows: { arch_paths: ["windows.txt"] },
386 },
387 multilib: {
388 lib32: { arch_paths: ["lib32.txt"] },
389 lib64: { arch_paths: ["lib64.txt"] },
Liz Kammer4562a3b2021-04-21 18:15:34 -0400390 },
391 bazel_module: { bp2build_available: true },
392}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000393 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000394 MakeBazelTarget("custom", "arch_paths", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500395 "arch_paths": `select({
Liz Kammerfdd72e62021-10-11 15:41:03 -0400396 "//build/bazel/platforms/arch:arm": [
397 "arm.txt",
398 "lib32.txt",
399 ],
400 "//build/bazel/platforms/arch:arm64": [
401 "arm64.txt",
402 "lib64.txt",
403 ],
404 "//build/bazel/platforms/arch:x86": [
405 "x86.txt",
406 "lib32.txt",
407 ],
408 "//build/bazel/platforms/arch:x86_64": [
409 "x86_64.txt",
410 "lib64.txt",
411 ],
412 "//conditions:default": [],
413 }) + select({
414 "//build/bazel/platforms/os:android": [
415 "linux.txt",
416 "bionic.txt",
417 "android.txt",
418 ],
419 "//build/bazel/platforms/os:darwin": [
420 "host.txt",
421 "darwin.txt",
422 "not_windows.txt",
423 ],
424 "//build/bazel/platforms/os:linux": [
425 "host.txt",
426 "linux.txt",
427 "glibc.txt",
428 "linux_glibc.txt",
429 "not_windows.txt",
430 ],
431 "//build/bazel/platforms/os:linux_bionic": [
432 "host.txt",
433 "linux.txt",
434 "bionic.txt",
435 "linux_bionic.txt",
436 "not_windows.txt",
437 ],
438 "//build/bazel/platforms/os:linux_musl": [
439 "host.txt",
440 "linux.txt",
441 "musl.txt",
442 "linux_musl.txt",
443 "not_windows.txt",
444 ],
445 "//build/bazel/platforms/os:windows": [
446 "host.txt",
447 "windows.txt",
448 ],
Liz Kammer4562a3b2021-04-21 18:15:34 -0400449 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500450 })`,
451 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400452 },
453 },
454 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000455 Description: "arch-variant deps",
456 Blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400457 name: "has_dep",
458 arch: {
459 x86: {
460 arch_paths: [":dep"],
461 },
462 },
463 bazel_module: { bp2build_available: true },
464}
465
466custom {
467 name: "dep",
468 arch_paths: ["abc"],
469 bazel_module: { bp2build_available: true },
470}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000471 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000472 MakeBazelTarget("custom", "dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500473 "arch_paths": `["abc"]`,
474 }),
Alixe06d75b2022-08-31 18:28:19 +0000475 MakeBazelTarget("custom", "has_dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500476 "arch_paths": `select({
Liz Kammer4562a3b2021-04-21 18:15:34 -0400477 "//build/bazel/platforms/arch:x86": [":dep"],
478 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500479 })`,
480 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400481 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000482 },
Liz Kammer32a03392021-09-14 11:17:21 -0400483 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000484 Description: "embedded props",
485 Blueprint: `custom {
Liz Kammer32a03392021-09-14 11:17:21 -0400486 name: "embedded_props",
487 embedded_prop: "abc",
488 bazel_module: { bp2build_available: true },
489}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000490 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000491 MakeBazelTarget("custom", "embedded_props", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500492 "embedded_attr": `"abc"`,
493 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400494 },
495 },
496 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000497 Description: "ptr to embedded props",
498 Blueprint: `custom {
Liz Kammer32a03392021-09-14 11:17:21 -0400499 name: "ptr_to_embedded_props",
500 other_embedded_prop: "abc",
501 bazel_module: { bp2build_available: true },
502}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000503 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000504 MakeBazelTarget("custom", "ptr_to_embedded_props", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500505 "other_embedded_attr": `"abc"`,
506 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400507 },
508 },
Jingwen Chen73850672020-12-14 08:25:34 -0500509 }
510
511 dir := "."
512 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000513 t.Run(testCase.Description, func(t *testing.T) {
514 config := android.TestConfig(buildDir, nil, testCase.Blueprint, nil)
Liz Kammerfdd72e62021-10-11 15:41:03 -0400515 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500516
Liz Kammerfdd72e62021-10-11 15:41:03 -0400517 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500518
Liz Kammerfdd72e62021-10-11 15:41:03 -0400519 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
520 if errored(t, testCase, errs) {
521 return
522 }
523 _, errs = ctx.ResolveDependencies(config)
524 if errored(t, testCase, errs) {
525 return
526 }
Jingwen Chen73850672020-12-14 08:25:34 -0500527
Liz Kammerfdd72e62021-10-11 15:41:03 -0400528 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
529 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
530 android.FailIfErrored(t, err)
Jingwen Chen164e0862021-02-19 00:48:40 -0500531
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000532 if actualCount, expectedCount := len(bazelTargets), len(testCase.ExpectedBazelTargets); actualCount != expectedCount {
533 t.Errorf("Expected %d bazel target (%s),\ngot %d (%s)", expectedCount, testCase.ExpectedBazelTargets, actualCount, bazelTargets)
Liz Kammerfdd72e62021-10-11 15:41:03 -0400534 } else {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000535 for i, expectedBazelTarget := range testCase.ExpectedBazelTargets {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400536 actualBazelTarget := bazelTargets[i]
537 if actualBazelTarget.content != expectedBazelTarget {
538 t.Errorf(
539 "Expected generated Bazel target to be '%s', got '%s'",
540 expectedBazelTarget,
541 actualBazelTarget.content,
542 )
543 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400544 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500545 }
Liz Kammerfdd72e62021-10-11 15:41:03 -0400546 })
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800547 }
548}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500549
Liz Kammerdfeb1202022-05-13 17:20:20 -0400550func TestBp2buildHostAndDevice(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000551 testCases := []Bp2buildTestCase{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400552 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000553 Description: "host and device, device only",
554 ModuleTypeUnderTest: "custom",
555 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
556 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400557 name: "foo",
558 bazel_module: { bp2build_available: true },
559}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000560 ExpectedBazelTargets: []string{
561 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400562 },
563 },
564 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000565 Description: "host and device, both",
566 ModuleTypeUnderTest: "custom",
567 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
568 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400569 name: "foo",
570 host_supported: true,
571 bazel_module: { bp2build_available: true },
572}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000573 ExpectedBazelTargets: []string{
574 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400575 },
576 },
577 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000578 Description: "host and device, host explicitly disabled",
579 ModuleTypeUnderTest: "custom",
580 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
581 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400582 name: "foo",
583 host_supported: false,
584 bazel_module: { bp2build_available: true },
585}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000586 ExpectedBazelTargets: []string{
587 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400588 },
589 },
590 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000591 Description: "host and device, neither",
592 ModuleTypeUnderTest: "custom",
593 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
594 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400595 name: "foo",
596 host_supported: false,
597 device_supported: false,
598 bazel_module: { bp2build_available: true },
599}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000600 ExpectedBazelTargets: []string{
601 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400602 "target_compatible_with": `["@platforms//:incompatible"]`,
603 }),
604 },
605 },
606 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000607 Description: "host and device, neither, cannot override with product_var",
608 ModuleTypeUnderTest: "custom",
609 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
610 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400611 name: "foo",
612 host_supported: false,
613 device_supported: false,
614 product_variables: { unbundled_build: { enabled: true } },
615 bazel_module: { bp2build_available: true },
616}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000617 ExpectedBazelTargets: []string{
618 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400619 "target_compatible_with": `["@platforms//:incompatible"]`,
620 }),
621 },
622 },
623 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000624 Description: "host and device, both, disabled overrided with product_var",
625 ModuleTypeUnderTest: "custom",
626 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
627 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400628 name: "foo",
629 host_supported: true,
630 device_supported: true,
631 enabled: false,
632 product_variables: { unbundled_build: { enabled: true } },
633 bazel_module: { bp2build_available: true },
634}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000635 ExpectedBazelTargets: []string{
636 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400637 "target_compatible_with": `["//build/bazel/product_variables:unbundled_build"]`,
638 }),
639 },
640 },
641 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000642 Description: "host and device, neither, cannot override with arch enabled",
643 ModuleTypeUnderTest: "custom",
644 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
645 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400646 name: "foo",
647 host_supported: false,
648 device_supported: false,
649 arch: { x86: { enabled: true } },
650 bazel_module: { bp2build_available: true },
651}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000652 ExpectedBazelTargets: []string{
653 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400654 "target_compatible_with": `["@platforms//:incompatible"]`,
655 }),
656 },
657 },
658 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000659 Description: "host and device, host only",
660 ModuleTypeUnderTest: "custom",
661 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
662 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400663 name: "foo",
664 host_supported: true,
665 device_supported: false,
666 bazel_module: { bp2build_available: true },
667}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000668 ExpectedBazelTargets: []string{
669 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400670 },
671 },
672 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000673 Description: "host only",
674 ModuleTypeUnderTest: "custom",
675 ModuleTypeUnderTestFactory: customModuleFactoryHostSupported,
676 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400677 name: "foo",
678 bazel_module: { bp2build_available: true },
679}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000680 ExpectedBazelTargets: []string{
681 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400682 },
683 },
684 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000685 Description: "device only",
686 ModuleTypeUnderTest: "custom",
687 ModuleTypeUnderTestFactory: customModuleFactoryDeviceSupported,
688 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400689 name: "foo",
690 bazel_module: { bp2build_available: true },
691}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000692 ExpectedBazelTargets: []string{
693 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400694 },
695 },
696 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000697 Description: "host and device default, default",
698 ModuleTypeUnderTest: "custom",
699 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
700 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400701 name: "foo",
702 bazel_module: { bp2build_available: true },
703}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000704 ExpectedBazelTargets: []string{
705 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400706 },
707 },
708 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000709 Description: "host and device default, device only",
710 ModuleTypeUnderTest: "custom",
711 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
712 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400713 name: "foo",
714 host_supported: false,
715 bazel_module: { bp2build_available: true },
716}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000717 ExpectedBazelTargets: []string{
718 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400719 },
720 },
721 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000722 Description: "host and device default, host only",
723 ModuleTypeUnderTest: "custom",
724 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
725 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400726 name: "foo",
727 device_supported: false,
728 bazel_module: { bp2build_available: true },
729}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000730 ExpectedBazelTargets: []string{
731 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400732 },
733 },
734 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000735 Description: "host and device default, neither",
736 ModuleTypeUnderTest: "custom",
737 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
738 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400739 name: "foo",
740 host_supported: false,
741 device_supported: false,
742 bazel_module: { bp2build_available: true },
743}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000744 ExpectedBazelTargets: []string{
745 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400746 "target_compatible_with": `["@platforms//:incompatible"]`,
747 }),
748 },
749 },
750 }
751
752 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000753 t.Run(tc.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +0000754 RunBp2BuildTestCaseSimple(t, tc)
Liz Kammerdfeb1202022-05-13 17:20:20 -0400755 })
756 }
757}
758
Jingwen Chen40067de2021-01-26 21:58:43 -0500759func TestLoadStatements(t *testing.T) {
760 testCases := []struct {
761 bazelTargets BazelTargets
762 expectedLoadStatements string
763 }{
764 {
765 bazelTargets: BazelTargets{
766 BazelTarget{
767 name: "foo",
768 ruleClass: "cc_library",
769 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
770 },
771 },
772 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
773 },
774 {
775 bazelTargets: BazelTargets{
776 BazelTarget{
777 name: "foo",
778 ruleClass: "cc_library",
779 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
780 },
781 BazelTarget{
782 name: "bar",
783 ruleClass: "cc_library",
784 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
785 },
786 },
787 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
788 },
789 {
790 bazelTargets: BazelTargets{
791 BazelTarget{
792 name: "foo",
793 ruleClass: "cc_library",
794 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
795 },
796 BazelTarget{
797 name: "bar",
798 ruleClass: "cc_binary",
799 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
800 },
801 },
802 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
803 },
804 {
805 bazelTargets: BazelTargets{
806 BazelTarget{
807 name: "foo",
808 ruleClass: "cc_library",
809 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
810 },
811 BazelTarget{
812 name: "bar",
813 ruleClass: "cc_binary",
814 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
815 },
816 BazelTarget{
817 name: "baz",
818 ruleClass: "java_binary",
819 bzlLoadLocation: "//build/bazel/rules:java.bzl",
820 },
821 },
822 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
823load("//build/bazel/rules:java.bzl", "java_binary")`,
824 },
825 {
826 bazelTargets: BazelTargets{
827 BazelTarget{
828 name: "foo",
829 ruleClass: "cc_binary",
830 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
831 },
832 BazelTarget{
833 name: "bar",
834 ruleClass: "java_binary",
835 bzlLoadLocation: "//build/bazel/rules:java.bzl",
836 },
837 BazelTarget{
838 name: "baz",
839 ruleClass: "genrule",
840 // Note: no bzlLoadLocation for native rules
841 },
842 },
843 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
844load("//build/bazel/rules:java.bzl", "java_binary")`,
845 },
846 }
847
848 for _, testCase := range testCases {
849 actual := testCase.bazelTargets.LoadStatements()
850 expected := testCase.expectedLoadStatements
851 if actual != expected {
852 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
853 }
854 }
855
856}
857
858func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
859 testCases := []struct {
860 bp string
861 expectedBazelTarget string
862 expectedBazelTargetCount int
863 expectedLoadStatements string
864 }{
865 {
866 bp: `custom {
867 name: "bar",
Liz Kammerdfeb1202022-05-13 17:20:20 -0400868 host_supported: true,
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400869 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500870 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500871}`,
872 expectedBazelTarget: `my_library(
873 name = "bar",
874)
875
Jingwen Chen40067de2021-01-26 21:58:43 -0500876proto_library(
877 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400878)
879
880my_proto_library(
881 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500882)`,
883 expectedBazelTargetCount: 3,
884 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
885load("//build/bazel/rules:rules.bzl", "my_library")`,
886 },
887 }
888
889 dir := "."
890 for _, testCase := range testCases {
891 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
892 ctx := android.NewTestContext(config)
Liz Kammerdfeb1202022-05-13 17:20:20 -0400893 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
Jingwen Chen40067de2021-01-26 21:58:43 -0500894 ctx.RegisterForBazelConversion()
895
896 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
897 android.FailIfErrored(t, errs)
898 _, errs = ctx.ResolveDependencies(config)
899 android.FailIfErrored(t, errs)
900
Jingwen Chen164e0862021-02-19 00:48:40 -0500901 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400902 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
903 android.FailIfErrored(t, err)
Jingwen Chen40067de2021-01-26 21:58:43 -0500904 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
905 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
906 }
907
908 actualBazelTargets := bazelTargets.String()
909 if actualBazelTargets != testCase.expectedBazelTarget {
910 t.Errorf(
911 "Expected generated Bazel target to be '%s', got '%s'",
912 testCase.expectedBazelTarget,
913 actualBazelTargets,
914 )
915 }
916
917 actualLoadStatements := bazelTargets.LoadStatements()
918 if actualLoadStatements != testCase.expectedLoadStatements {
919 t.Errorf(
920 "Expected generated load statements to be '%s', got '%s'",
921 testCase.expectedLoadStatements,
922 actualLoadStatements,
923 )
924 }
925 }
926}
927
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500928func TestModuleTypeBp2Build(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000929 testCases := []Bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500930 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000931 Description: "filegroup with does not specify srcs",
932 ModuleTypeUnderTest: "filegroup",
933 ModuleTypeUnderTestFactory: android.FileGroupFactory,
934 Blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500935 name: "fg_foo",
936 bazel_module: { bp2build_available: true },
937}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000938 ExpectedBazelTargets: []string{
939 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerebfcf672021-02-16 15:00:05 -0500940 },
941 },
942 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000943 Description: "filegroup with no srcs",
944 ModuleTypeUnderTest: "filegroup",
945 ModuleTypeUnderTestFactory: android.FileGroupFactory,
946 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500947 name: "fg_foo",
948 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500949 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500950}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000951 ExpectedBazelTargets: []string{
952 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammer356f7d42021-01-26 09:18:53 -0500953 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500954 },
955 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000956 Description: "filegroup with srcs",
957 ModuleTypeUnderTest: "filegroup",
958 ModuleTypeUnderTestFactory: android.FileGroupFactory,
959 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500960 name: "fg_foo",
961 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500962 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500963}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000964 ExpectedBazelTargets: []string{
965 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500966 "srcs": `[
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500967 "a",
968 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500969 ]`,
970 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500971 },
972 },
973 {
Usta Shresthad5580312022-09-23 16:46:38 -0400974 Description: "filegroup with dot-slash-prefixed srcs",
975 ModuleTypeUnderTest: "filegroup",
976 ModuleTypeUnderTestFactory: android.FileGroupFactory,
977 Blueprint: `filegroup {
978 name: "fg_foo",
979 srcs: ["./a", "./b"],
980 bazel_module: { bp2build_available: true },
981}`,
982 ExpectedBazelTargets: []string{
983 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
984 "srcs": `[
985 "a",
986 "b",
987 ]`,
988 }),
989 },
990 },
991 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000992 Description: "filegroup with excludes srcs",
993 ModuleTypeUnderTest: "filegroup",
994 ModuleTypeUnderTestFactory: android.FileGroupFactory,
995 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500996 name: "fg_foo",
997 srcs: ["a", "b"],
998 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500999 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -05001000}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001001 ExpectedBazelTargets: []string{
1002 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001003 "srcs": `["b"]`,
1004 }),
Liz Kammer356f7d42021-01-26 09:18:53 -05001005 },
1006 },
1007 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001008 Description: "depends_on_other_dir_module",
1009 ModuleTypeUnderTest: "filegroup",
1010 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1011 Blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001012 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -05001013 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +00001014 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -05001015 "c",
1016 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001017 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -05001018}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001019 Filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -05001020 "other/Android.bp": `filegroup {
1021 name: "foo",
1022 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -04001023 bazel_module: { bp2build_available: true },
1024}`,
1025 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001026 ExpectedBazelTargets: []string{
1027 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001028 "srcs": `[
1029 "//other:foo",
1030 "c",
1031 ]`,
1032 }),
1033 },
Liz Kammer6eff3232021-08-26 08:37:59 -04001034 },
1035 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001036 Description: "depends_on_other_unconverted_module_error",
1037 ModuleTypeUnderTest: "filegroup",
1038 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1039 UnconvertedDepsMode: errorModulesUnconvertedDeps,
1040 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001041 name: "foobar",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001042 srcs: [
1043 ":foo",
1044 "c",
1045 ],
1046 bazel_module: { bp2build_available: true },
1047}`,
Sasha Smundakf2bb26f2022-08-04 11:28:15 -07001048 ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on unconverted modules: foo`),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001049 Filesystem: map[string]string{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001050 "other/Android.bp": `filegroup {
1051 name: "foo",
1052 srcs: ["a", "b"],
1053}`,
1054 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001055 },
1056 }
1057
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001058 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001059 t.Run(testCase.Description, func(t *testing.T) {
1060 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
Liz Kammer6eff3232021-08-26 08:37:59 -04001061 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001062 }
1063}
Jingwen Chen041b1842021-02-01 00:23:25 -05001064
1065type bp2buildMutator = func(android.TopDownMutatorContext)
1066
Jingwen Chen12b4c272021-03-10 02:05:59 -05001067func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001068 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001069 moduleTypeUnderTest string
1070 moduleTypeUnderTestFactory android.ModuleFactory
1071 bp string
1072 expectedCount int
1073 description string
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001074 }{
1075 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001076 description: "explicitly unavailable",
1077 moduleTypeUnderTest: "filegroup",
1078 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001079 bp: `filegroup {
1080 name: "foo",
1081 srcs: ["a", "b"],
1082 bazel_module: { bp2build_available: false },
1083}`,
1084 expectedCount: 0,
1085 },
1086 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001087 description: "implicitly unavailable",
1088 moduleTypeUnderTest: "filegroup",
1089 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001090 bp: `filegroup {
1091 name: "foo",
1092 srcs: ["a", "b"],
1093}`,
1094 expectedCount: 0,
1095 },
1096 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001097 description: "explicitly available",
1098 moduleTypeUnderTest: "filegroup",
1099 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001100 bp: `filegroup {
1101 name: "foo",
1102 srcs: ["a", "b"],
1103 bazel_module: { bp2build_available: true },
1104}`,
1105 expectedCount: 1,
1106 },
1107 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001108 description: "generates more than 1 target if needed",
1109 moduleTypeUnderTest: "custom",
Liz Kammerdfeb1202022-05-13 17:20:20 -04001110 moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001111 bp: `custom {
1112 name: "foo",
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001113 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001114 bazel_module: { bp2build_available: true },
1115}`,
1116 expectedCount: 3,
1117 },
1118 }
1119
1120 dir := "."
1121 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -04001122 t.Run(testCase.description, func(t *testing.T) {
1123 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1124 ctx := android.NewTestContext(config)
1125 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001126 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001127
Liz Kammer2ada09a2021-08-11 00:17:36 -04001128 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1129 android.FailIfErrored(t, errs)
1130 _, errs = ctx.ResolveDependencies(config)
1131 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001132
Liz Kammer2ada09a2021-08-11 00:17:36 -04001133 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -04001134 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1135 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001136 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1137 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1138 }
1139 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001140 }
1141}
Liz Kammerba3ea162021-02-17 13:22:03 -05001142
Jingwen Chen12b4c272021-03-10 02:05:59 -05001143func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1144 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001145 moduleTypeUnderTest string
1146 moduleTypeUnderTestFactory android.ModuleFactory
1147 expectedCount map[string]int
1148 description string
Sam Delmerico24c56032022-03-28 19:53:03 +00001149 bp2buildConfig allowlists.Bp2BuildConfig
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001150 checkDir string
1151 fs map[string]string
Jingwen Chen12b4c272021-03-10 02:05:59 -05001152 }{
1153 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001154 description: "test bp2build config package and subpackages config",
1155 moduleTypeUnderTest: "filegroup",
1156 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001157 expectedCount: map[string]int{
1158 "migrated": 1,
1159 "migrated/but_not_really": 0,
1160 "migrated/but_not_really/but_really": 1,
1161 "not_migrated": 0,
1162 "also_not_migrated": 0,
1163 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001164 bp2buildConfig: allowlists.Bp2BuildConfig{
1165 "migrated": allowlists.Bp2BuildDefaultTrueRecursively,
1166 "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
1167 "not_migrated": allowlists.Bp2BuildDefaultFalse,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001168 },
1169 fs: map[string]string{
1170 "migrated/Android.bp": `filegroup { name: "a" }`,
1171 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1172 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1173 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1174 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1175 },
1176 },
1177 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001178 description: "test bp2build config opt-in and opt-out",
1179 moduleTypeUnderTest: "filegroup",
1180 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001181 expectedCount: map[string]int{
1182 "package-opt-in": 2,
1183 "package-opt-in/subpackage": 0,
1184 "package-opt-out": 1,
1185 "package-opt-out/subpackage": 0,
1186 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001187 bp2buildConfig: allowlists.Bp2BuildConfig{
1188 "package-opt-in": allowlists.Bp2BuildDefaultFalse,
1189 "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001190 },
1191 fs: map[string]string{
1192 "package-opt-in/Android.bp": `
1193filegroup { name: "opt-in-a" }
1194filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1195filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1196`,
1197
1198 "package-opt-in/subpackage/Android.bp": `
1199filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1200`,
1201
1202 "package-opt-out/Android.bp": `
1203filegroup { name: "opt-out-a" }
1204filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1205filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1206`,
1207
1208 "package-opt-out/subpackage/Android.bp": `
1209filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1210filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1211`,
1212 },
1213 },
1214 }
1215
1216 dir := "."
1217 for _, testCase := range testCases {
1218 fs := make(map[string][]byte)
1219 toParse := []string{
1220 "Android.bp",
1221 }
1222 for f, content := range testCase.fs {
1223 if strings.HasSuffix(f, "Android.bp") {
1224 toParse = append(toParse, f)
1225 }
1226 fs[f] = []byte(content)
1227 }
1228 config := android.TestConfig(buildDir, nil, "", fs)
1229 ctx := android.NewTestContext(config)
1230 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Sam Delmerico24c56032022-03-28 19:53:03 +00001231 allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig)
1232 ctx.RegisterBp2BuildConfig(allowlist)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001233 ctx.RegisterForBazelConversion()
1234
1235 _, errs := ctx.ParseFileList(dir, toParse)
1236 android.FailIfErrored(t, errs)
1237 _, errs = ctx.ResolveDependencies(config)
1238 android.FailIfErrored(t, errs)
1239
1240 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1241
1242 // For each directory, test that the expected number of generated targets is correct.
1243 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -04001244 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1245 android.FailIfErrored(t, err)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001246 if actualCount := len(bazelTargets); actualCount != expectedCount {
1247 t.Fatalf(
1248 "%s: Expected %d bazel target for %s package, got %d",
1249 testCase.description,
1250 expectedCount,
1251 dir,
1252 actualCount)
1253 }
1254
1255 }
1256 }
1257}
1258
Liz Kammerba3ea162021-02-17 13:22:03 -05001259func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001260 testCases := []Bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -05001261 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001262 Description: "filegroup bazel_module.label",
1263 ModuleTypeUnderTest: "filegroup",
1264 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1265 Blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -05001266 name: "fg_foo",
1267 bazel_module: { label: "//other:fg_foo" },
1268}`,
Cole Faustea602c52022-08-31 14:48:26 -07001269 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001270 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001271 "other/BUILD.bazel": `// BUILD file`,
1272 },
1273 },
1274 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001275 Description: "multiple bazel_module.label same BUILD",
1276 ModuleTypeUnderTest: "filegroup",
1277 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1278 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001279 name: "fg_foo",
1280 bazel_module: { label: "//other:fg_foo" },
1281 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001282
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001283 filegroup {
1284 name: "foo",
1285 bazel_module: { label: "//other:foo" },
1286 }`,
Cole Faustea602c52022-08-31 14:48:26 -07001287 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001288 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001289 "other/BUILD.bazel": `// BUILD file`,
1290 },
1291 },
1292 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001293 Description: "filegroup bazel_module.label and bp2build in subdir",
1294 ModuleTypeUnderTest: "filegroup",
1295 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1296 Dir: "other",
1297 Blueprint: ``,
1298 Filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001299 "other/Android.bp": `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001300 name: "fg_foo",
1301 bazel_module: {
1302 bp2build_available: true,
1303 },
1304 }
1305 filegroup {
1306 name: "fg_bar",
1307 bazel_module: {
1308 label: "//other:fg_bar"
1309 },
1310 }`,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001311 "other/BUILD.bazel": `// definition for fg_bar`,
1312 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001313 ExpectedBazelTargets: []string{
1314 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001315 },
1316 },
1317 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001318 Description: "filegroup bazel_module.label and filegroup bp2build",
1319 ModuleTypeUnderTest: "filegroup",
1320 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001321
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001322 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001323 "other/BUILD.bazel": `// BUILD file`,
1324 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001325 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001326 name: "fg_foo",
1327 bazel_module: {
1328 label: "//other:fg_foo",
1329 },
1330 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001331
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001332 filegroup {
1333 name: "fg_bar",
1334 bazel_module: {
1335 bp2build_available: true,
1336 },
1337 }`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001338 ExpectedBazelTargets: []string{
1339 MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001340 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001341 },
1342 }
1343
1344 dir := "."
1345 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001346 t.Run(testCase.Description, func(t *testing.T) {
Jingwen Chen49109762021-05-25 05:16:48 +00001347 fs := make(map[string][]byte)
1348 toParse := []string{
1349 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001350 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001351 for f, content := range testCase.Filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001352 if strings.HasSuffix(f, "Android.bp") {
1353 toParse = append(toParse, f)
1354 }
1355 fs[f] = []byte(content)
1356 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001357 config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001358 ctx := android.NewTestContext(config)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001359 ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001360 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001361
Jingwen Chen49109762021-05-25 05:16:48 +00001362 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001363 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001364 return
1365 }
1366 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001367 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001368 return
1369 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001370
Jingwen Chen49109762021-05-25 05:16:48 +00001371 checkDir := dir
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001372 if testCase.Dir != "" {
1373 checkDir = testCase.Dir
Jingwen Chen49109762021-05-25 05:16:48 +00001374 }
Liz Kammer6eff3232021-08-26 08:37:59 -04001375 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1376 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1377 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001378 bazelTargets.sort()
1379 actualCount := len(bazelTargets)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001380 expectedCount := len(testCase.ExpectedBazelTargets)
Jingwen Chen49109762021-05-25 05:16:48 +00001381 if actualCount != expectedCount {
1382 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1383 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001384 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001385 actualContent := target.content
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001386 expectedContent := testCase.ExpectedBazelTargets[i]
Jingwen Chen49109762021-05-25 05:16:48 +00001387 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001388 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001389 "Expected generated Bazel target to be '%s', got '%s'",
1390 expectedContent,
1391 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001392 )
1393 }
1394 }
Jingwen Chen49109762021-05-25 05:16:48 +00001395 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001396 }
1397}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001398
Jingwen Chen0eeaeb82022-09-21 10:27:42 +00001399func TestGlob(t *testing.T) {
1400 testCases := []Bp2buildTestCase{
1401 {
1402 Description: "filegroup with glob",
1403 ModuleTypeUnderTest: "filegroup",
1404 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1405 Blueprint: `filegroup {
1406 name: "fg_foo",
1407 srcs: ["**/*.txt"],
1408 bazel_module: { bp2build_available: true },
1409}`,
1410 ExpectedBazelTargets: []string{
1411 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1412 "srcs": `[
1413 "other/a.txt",
1414 "other/b.txt",
1415 "other/subdir/a.txt",
1416 ]`,
1417 }),
1418 },
1419 Filesystem: map[string]string{
1420 "other/a.txt": "",
1421 "other/b.txt": "",
1422 "other/subdir/a.txt": "",
1423 "other/file": "",
1424 },
1425 },
1426 {
1427 Description: "filegroup with glob in subdir",
1428 ModuleTypeUnderTest: "filegroup",
1429 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1430 Dir: "other",
1431 Filesystem: map[string]string{
1432 "other/Android.bp": `filegroup {
1433 name: "fg_foo",
1434 srcs: ["**/*.txt"],
1435 bazel_module: { bp2build_available: true },
1436}`,
1437 "other/a.txt": "",
1438 "other/b.txt": "",
1439 "other/subdir/a.txt": "",
1440 "other/file": "",
1441 },
1442 ExpectedBazelTargets: []string{
1443 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1444 "srcs": `[
1445 "a.txt",
1446 "b.txt",
1447 "subdir/a.txt",
1448 ]`,
1449 }),
1450 },
1451 },
1452 {
1453 Description: "filegroup with glob with no kept BUILD files",
1454 ModuleTypeUnderTest: "filegroup",
1455 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1456 KeepBuildFileForDirs: []string{
1457 // empty
1458 },
1459 Blueprint: `filegroup {
1460 name: "fg_foo",
1461 srcs: ["**/*.txt"],
1462 bazel_module: { bp2build_available: true },
1463}`,
1464 Filesystem: map[string]string{
1465 "a.txt": "",
1466 "b.txt": "",
1467 "foo/BUILD": "",
1468 "foo/a.txt": "",
1469 "foo/bar/BUILD": "",
1470 "foo/bar/b.txt": "",
1471 },
1472 ExpectedBazelTargets: []string{
1473 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1474 "srcs": `[
1475 "a.txt",
1476 "b.txt",
1477 "foo/a.txt",
1478 "foo/bar/b.txt",
1479 ]`,
1480 }),
1481 },
1482 },
1483 {
1484 Description: "filegroup with glob with kept BUILD file",
1485 ModuleTypeUnderTest: "filegroup",
1486 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1487 KeepBuildFileForDirs: []string{
1488 "foo",
1489 },
1490 Blueprint: `filegroup {
1491 name: "fg_foo",
1492 srcs: ["**/*.txt"],
1493 bazel_module: { bp2build_available: true },
1494}`,
1495 Filesystem: map[string]string{
1496 "a.txt": "",
1497 "b.txt": "",
1498 "foo/BUILD": "",
1499 "foo/a.txt": "",
1500 "foo/bar/BUILD": "",
1501 "foo/bar/b.txt": "",
1502 },
1503 ExpectedBazelTargets: []string{
1504 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1505 "srcs": `[
1506 "a.txt",
1507 "b.txt",
1508 "//foo:a.txt",
1509 "//foo:bar/b.txt",
1510 ]`,
1511 }),
1512 },
1513 },
1514 {
1515 Description: "filegroup with glob with kept BUILD.bazel file",
1516 ModuleTypeUnderTest: "filegroup",
1517 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1518 KeepBuildFileForDirs: []string{
1519 "foo",
1520 },
1521 Blueprint: `filegroup {
1522 name: "fg_foo",
1523 srcs: ["**/*.txt"],
1524 bazel_module: { bp2build_available: true },
1525}`,
1526 Filesystem: map[string]string{
1527 "a.txt": "",
1528 "b.txt": "",
1529 "foo/BUILD.bazel": "",
1530 "foo/a.txt": "",
1531 "foo/bar/BUILD.bazel": "",
1532 "foo/bar/b.txt": "",
1533 },
1534 ExpectedBazelTargets: []string{
1535 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1536 "srcs": `[
1537 "a.txt",
1538 "b.txt",
1539 "//foo:a.txt",
1540 "//foo:bar/b.txt",
1541 ]`,
1542 }),
1543 },
1544 },
1545 {
1546 Description: "filegroup with glob with Android.bp file as boundary",
1547 ModuleTypeUnderTest: "filegroup",
1548 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1549 Blueprint: `filegroup {
1550 name: "fg_foo",
1551 srcs: ["**/*.txt"],
1552 bazel_module: { bp2build_available: true },
1553}`,
1554 Filesystem: map[string]string{
1555 "a.txt": "",
1556 "b.txt": "",
1557 "foo/Android.bp": "",
1558 "foo/a.txt": "",
1559 "foo/bar/Android.bp": "",
1560 "foo/bar/b.txt": "",
1561 },
1562 ExpectedBazelTargets: []string{
1563 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1564 "srcs": `[
1565 "a.txt",
1566 "b.txt",
1567 "//foo:a.txt",
1568 "//foo/bar:b.txt",
1569 ]`,
1570 }),
1571 },
1572 },
1573 {
1574 Description: "filegroup with glob in subdir with kept BUILD and BUILD.bazel file",
1575 ModuleTypeUnderTest: "filegroup",
1576 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1577 Dir: "other",
1578 KeepBuildFileForDirs: []string{
1579 "other/foo",
1580 "other/foo/bar",
1581 // deliberately not other/foo/baz/BUILD.
1582 },
1583 Filesystem: map[string]string{
1584 "other/Android.bp": `filegroup {
1585 name: "fg_foo",
1586 srcs: ["**/*.txt"],
1587 bazel_module: { bp2build_available: true },
1588}`,
1589 "other/a.txt": "",
1590 "other/b.txt": "",
1591 "other/foo/BUILD": "",
1592 "other/foo/a.txt": "",
1593 "other/foo/bar/BUILD.bazel": "",
1594 "other/foo/bar/b.txt": "",
1595 "other/foo/baz/BUILD": "",
1596 "other/foo/baz/c.txt": "",
1597 },
1598 ExpectedBazelTargets: []string{
1599 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1600 "srcs": `[
1601 "a.txt",
1602 "b.txt",
1603 "//other/foo:a.txt",
1604 "//other/foo/bar:b.txt",
1605 "//other/foo:baz/c.txt",
1606 ]`,
1607 }),
1608 },
1609 },
1610 }
1611
1612 for _, testCase := range testCases {
1613 t.Run(testCase.Description, func(t *testing.T) {
1614 RunBp2BuildTestCaseSimple(t, testCase)
1615 })
1616 }
1617}
1618
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001619func TestGlobExcludeSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001620 testCases := []Bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001621 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001622 Description: "filegroup top level exclude_srcs",
1623 ModuleTypeUnderTest: "filegroup",
1624 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1625 Blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001626 name: "fg_foo",
1627 srcs: ["**/*.txt"],
1628 exclude_srcs: ["c.txt"],
1629 bazel_module: { bp2build_available: true },
1630}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001631 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001632 "a.txt": "",
1633 "b.txt": "",
1634 "c.txt": "",
1635 "dir/Android.bp": "",
1636 "dir/e.txt": "",
1637 "dir/f.txt": "",
1638 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001639 ExpectedBazelTargets: []string{
1640 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001641 "srcs": `[
1642 "a.txt",
1643 "b.txt",
1644 "//dir:e.txt",
1645 "//dir:f.txt",
1646 ]`,
1647 }),
1648 },
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001649 },
1650 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001651 Description: "filegroup in subdir exclude_srcs",
1652 ModuleTypeUnderTest: "filegroup",
1653 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1654 Blueprint: "",
1655 Dir: "dir",
1656 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001657 "dir/Android.bp": `filegroup {
1658 name: "fg_foo",
1659 srcs: ["**/*.txt"],
1660 exclude_srcs: ["b.txt"],
1661 bazel_module: { bp2build_available: true },
1662}
1663`,
1664 "dir/a.txt": "",
1665 "dir/b.txt": "",
1666 "dir/subdir/Android.bp": "",
1667 "dir/subdir/e.txt": "",
1668 "dir/subdir/f.txt": "",
1669 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001670 ExpectedBazelTargets: []string{
1671 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001672 "srcs": `[
Liz Kammer9abd62d2021-05-21 08:37:59 -04001673 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001674 "//dir/subdir:e.txt",
1675 "//dir/subdir:f.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001676 ]`,
1677 }),
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001678 },
1679 },
1680 }
1681
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001682 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001683 t.Run(testCase.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001684 RunBp2BuildTestCaseSimple(t, testCase)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001685 })
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001686 }
1687}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001688
1689func TestCommonBp2BuildModuleAttrs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001690 testCases := []Bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001691 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001692 Description: "Required into data test",
1693 ModuleTypeUnderTest: "filegroup",
1694 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1695 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001696filegroup {
1697 name: "fg_foo",
1698 required: ["reqd"],
1699 bazel_module: { bp2build_available: true },
1700}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001701 ExpectedBazelTargets: []string{
1702 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001703 "data": `[":reqd"]`,
1704 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001705 },
1706 },
1707 {
Jingwen Chena5ecb372022-09-21 09:05:37 +00001708 Description: "Required into data test, cyclic self reference is filtered out",
1709 ModuleTypeUnderTest: "filegroup",
1710 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1711 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
1712filegroup {
1713 name: "fg_foo",
1714 required: ["reqd", "fg_foo"],
1715 bazel_module: { bp2build_available: true },
1716}`,
1717 ExpectedBazelTargets: []string{
1718 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1719 "data": `[":reqd"]`,
1720 }),
1721 },
1722 },
1723 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001724 Description: "Required via arch into data test",
1725 ModuleTypeUnderTest: "python_library",
1726 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1727 Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") +
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001728 simpleModuleDoNotConvertBp2build("python_library", "reqdarm") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001729python_library {
1730 name: "fg_foo",
1731 arch: {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001732 arm: {
1733 required: ["reqdarm"],
1734 },
1735 x86: {
1736 required: ["reqdx86"],
1737 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001738 },
1739 bazel_module: { bp2build_available: true },
1740}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001741 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001742 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001743 "data": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001744 "//build/bazel/platforms/arch:arm": [":reqdarm"],
1745 "//build/bazel/platforms/arch:x86": [":reqdx86"],
1746 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001747 })`,
1748 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001749 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001750 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001751 },
1752 },
1753 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001754 Description: "Required appended to data test",
1755 ModuleTypeUnderTest: "python_library",
1756 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1757 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001758 "data.bin": "",
1759 "src.py": "",
1760 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001761 Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001762python_library {
1763 name: "fg_foo",
1764 data: ["data.bin"],
1765 required: ["reqd"],
1766 bazel_module: { bp2build_available: true },
1767}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001768 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001769 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001770 "data": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001771 "data.bin",
1772 ":reqd",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001773 ]`,
1774 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001775 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001776 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001777 },
1778 },
1779 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001780 Description: "All props-to-attrs at once together test",
1781 ModuleTypeUnderTest: "filegroup",
1782 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1783 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001784filegroup {
1785 name: "fg_foo",
1786 required: ["reqd"],
1787 bazel_module: { bp2build_available: true },
1788}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001789 ExpectedBazelTargets: []string{
1790 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001791 "data": `[":reqd"]`,
1792 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001793 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001794 },
1795 }
1796
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001797 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001798 t.Run(tc.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001799 RunBp2BuildTestCaseSimple(t, tc)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001800 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001801 }
1802}