blob: 8e51d98035496985b11e87d28ff2e329858e95e4 [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"
Spandan Das921af322023-04-26 02:56:37 +000024 "android/soong/bazel"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000025 "android/soong/python"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080026)
27
28func TestGenerateSoongModuleTargets(t *testing.T) {
29 testCases := []struct {
Liz Kammerd366c902021-06-03 13:43:01 -040030 description string
Liz Kammer2dd9ca42020-11-25 16:06:39 -080031 bp string
32 expectedBazelTarget string
33 }{
34 {
Liz Kammerd366c902021-06-03 13:43:01 -040035 description: "only name",
Jingwen Chenb4628eb2021-04-08 14:40:57 +000036 bp: `custom { name: "foo" }
Liz Kammerd366c902021-06-03 13:43:01 -040037 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080038 expectedBazelTarget: `soong_module(
39 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050040 soong_module_name = "foo",
41 soong_module_type = "custom",
42 soong_module_variant = "",
43 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080044 ],
Liz Kammerd366c902021-06-03 13:43:01 -040045 bool_prop = False,
Liz Kammer46fb7ab2021-12-01 10:09:34 -050046 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080047)`,
48 },
49 {
Liz Kammerd366c902021-06-03 13:43:01 -040050 description: "handles bool",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080051 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040052 name: "foo",
53 bool_prop: true,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080054}
Liz Kammerd366c902021-06-03 13:43:01 -040055 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080056 expectedBazelTarget: `soong_module(
57 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050058 soong_module_name = "foo",
59 soong_module_type = "custom",
60 soong_module_variant = "",
61 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080062 ],
Liz Kammerd366c902021-06-03 13:43:01 -040063 bool_prop = True,
Liz Kammer46fb7ab2021-12-01 10:09:34 -050064 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080065)`,
66 },
67 {
Liz Kammerd366c902021-06-03 13:43:01 -040068 description: "string escaping",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080069 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040070 name: "foo",
71 owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080072}
Liz Kammerd366c902021-06-03 13:43:01 -040073 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080074 expectedBazelTarget: `soong_module(
75 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050076 soong_module_name = "foo",
77 soong_module_type = "custom",
78 soong_module_variant = "",
79 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080080 ],
Liz Kammerd366c902021-06-03 13:43:01 -040081 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080082 owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
Liz Kammer46fb7ab2021-12-01 10:09:34 -050083 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080084)`,
85 },
86 {
Liz Kammerd366c902021-06-03 13:43:01 -040087 description: "single item string list",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080088 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040089 name: "foo",
90 required: ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -080091}
Liz Kammerd366c902021-06-03 13:43:01 -040092 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080093 expectedBazelTarget: `soong_module(
94 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050095 soong_module_name = "foo",
96 soong_module_type = "custom",
97 soong_module_variant = "",
98 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080099 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400100 bool_prop = False,
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000101 required = ["bar"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500102 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800103)`,
104 },
105 {
Liz Kammerd366c902021-06-03 13:43:01 -0400106 description: "list of strings",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800107 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400108 name: "foo",
109 target_required: ["qux", "bazqux"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800110}
Liz Kammerd366c902021-06-03 13:43:01 -0400111 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800112 expectedBazelTarget: `soong_module(
113 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500114 soong_module_name = "foo",
115 soong_module_type = "custom",
116 soong_module_variant = "",
117 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800118 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400119 bool_prop = False,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500120 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800121 target_required = [
122 "qux",
123 "bazqux",
124 ],
125)`,
126 },
127 {
Liz Kammerd366c902021-06-03 13:43:01 -0400128 description: "dist/dists",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800129 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400130 name: "foo",
131 dist: {
132 targets: ["goal_foo"],
133 tag: ".foo",
134 },
135 dists: [{
136 targets: ["goal_bar"],
137 tag: ".bar",
138 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800139}
Liz Kammerd366c902021-06-03 13:43:01 -0400140 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800141 expectedBazelTarget: `soong_module(
142 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500143 soong_module_name = "foo",
144 soong_module_type = "custom",
145 soong_module_variant = "",
146 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800147 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400148 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800149 dist = {
150 "tag": ".foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000151 "targets": ["goal_foo"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800152 },
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000153 dists = [{
154 "tag": ".bar",
155 "targets": ["goal_bar"],
156 }],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500157 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800158)`,
159 },
160 {
Liz Kammerd366c902021-06-03 13:43:01 -0400161 description: "put it together",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800162 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400163 name: "foo",
164 required: ["bar"],
165 target_required: ["qux", "bazqux"],
166 bool_prop: true,
167 owner: "custom_owner",
168 dists: [
169 {
170 tag: ".tag",
171 targets: ["my_goal"],
172 },
173 ],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800174}
Liz Kammerd366c902021-06-03 13:43:01 -0400175 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800176 expectedBazelTarget: `soong_module(
177 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500178 soong_module_name = "foo",
179 soong_module_type = "custom",
180 soong_module_variant = "",
181 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800182 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400183 bool_prop = True,
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000184 dists = [{
185 "tag": ".tag",
186 "targets": ["my_goal"],
187 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800188 owner = "custom_owner",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000189 required = ["bar"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500190 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800191 target_required = [
192 "qux",
193 "bazqux",
194 ],
195)`,
196 },
197 }
198
199 dir := "."
200 for _, testCase := range testCases {
Liz Kammerd366c902021-06-03 13:43:01 -0400201 t.Run(testCase.description, func(t *testing.T) {
202 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
203 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500204
Liz Kammerdfeb1202022-05-13 17:20:20 -0400205 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
Liz Kammerd366c902021-06-03 13:43:01 -0400206 ctx.Register()
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800207
Liz Kammerd366c902021-06-03 13:43:01 -0400208 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
209 android.FailIfErrored(t, errs)
210 _, errs = ctx.PrepareBuildActions(config)
211 android.FailIfErrored(t, errs)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800212
Cole Faustb85d1a12022-11-08 18:14:01 -0800213 codegenCtx := NewCodegenContext(config, ctx.Context, QueryView, "")
Liz Kammer6eff3232021-08-26 08:37:59 -0400214 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
215 android.FailIfErrored(t, err)
Liz Kammerd366c902021-06-03 13:43:01 -0400216 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
217 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
218 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800219
Liz Kammerd366c902021-06-03 13:43:01 -0400220 actualBazelTarget := bazelTargets[0]
221 if actualBazelTarget.content != testCase.expectedBazelTarget {
222 t.Errorf(
223 "Expected generated Bazel target to be '%s', got '%s'",
224 testCase.expectedBazelTarget,
225 actualBazelTarget.content,
226 )
227 }
228 })
Jingwen Chen73850672020-12-14 08:25:34 -0500229 }
230}
231
232func TestGenerateBazelTargetModules(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000233 testCases := []Bp2buildTestCase{
Jingwen Chen73850672020-12-14 08:25:34 -0500234 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000235 Description: "string prop empty",
236 Blueprint: `custom {
237 name: "foo",
238 string_literal_prop: "",
239 bazel_module: { bp2build_available: true },
240}`,
241 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000242 MakeBazelTarget("custom", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000243 "string_literal_prop": `""`,
244 }),
245 },
246 },
247 {
248 Description: `string prop "PROP"`,
249 Blueprint: `custom {
250 name: "foo",
251 string_literal_prop: "PROP",
252 bazel_module: { bp2build_available: true },
253}`,
254 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000255 MakeBazelTarget("custom", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000256 "string_literal_prop": `"PROP"`,
257 }),
258 },
259 },
260 {
261 Description: `string prop arch variant`,
262 Blueprint: `custom {
263 name: "foo",
264 arch: {
265 arm: { string_literal_prop: "ARM" },
266 arm64: { string_literal_prop: "ARM64" },
267 },
268 bazel_module: { bp2build_available: true },
269}`,
270 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000271 MakeBazelTarget("custom", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000272 "string_literal_prop": `select({
273 "//build/bazel/platforms/arch:arm": "ARM",
274 "//build/bazel/platforms/arch:arm64": "ARM64",
275 "//conditions:default": None,
276 })`,
277 }),
278 },
279 },
280 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000281 Description: "string ptr props",
282 Blueprint: `custom {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500283 name: "foo",
284 string_ptr_prop: "",
285 bazel_module: { bp2build_available: true },
286}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000287 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000288 MakeBazelTarget("custom", "foo", AttrNameToString{
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500289 "string_ptr_prop": `""`,
290 }),
291 },
292 },
293 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000294 Description: "string list props",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000295 Blueprint: `custom {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400296 name: "foo",
Jingwen Chen73850672020-12-14 08:25:34 -0500297 string_list_prop: ["a", "b"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500298 string_ptr_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500299 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500300}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000301 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000302 MakeBazelTarget("custom", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500303 "string_list_prop": `[
Jingwen Chen73850672020-12-14 08:25:34 -0500304 "a",
305 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500306 ]`,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500307 "string_ptr_prop": `"a"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500308 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400309 },
Jingwen Chen73850672020-12-14 08:25:34 -0500310 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000311 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000312 Description: "control characters",
313 Blueprint: `custom {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500314 name: "foo",
Jingwen Chen58a12b82021-03-30 13:08:36 +0000315 string_list_prop: ["\t", "\n"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500316 string_ptr_prop: "a\t\n\r",
Jingwen Chen58a12b82021-03-30 13:08:36 +0000317 bazel_module: { bp2build_available: true },
318}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000319 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000320 MakeBazelTarget("custom", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500321 "string_list_prop": `[
Jingwen Chen58a12b82021-03-30 13:08:36 +0000322 "\t",
323 "\n",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500324 ]`,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500325 "string_ptr_prop": `"a\t\n\r"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500326 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400327 },
328 },
329 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000330 Description: "handles dep",
331 Blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400332 name: "has_dep",
333 arch_paths: [":dep"],
334 bazel_module: { bp2build_available: true },
335}
336
337custom {
338 name: "dep",
339 arch_paths: ["abc"],
340 bazel_module: { bp2build_available: true },
341}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000342 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000343 MakeBazelTarget("custom", "dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500344 "arch_paths": `["abc"]`,
345 }),
Alixe06d75b2022-08-31 18:28:19 +0000346 MakeBazelTarget("custom", "has_dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500347 "arch_paths": `[":dep"]`,
348 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400349 },
350 },
351 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000352 Description: "arch-variant srcs",
353 Blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400354 name: "arch_paths",
355 arch: {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400356 x86: { arch_paths: ["x86.txt"] },
357 x86_64: { arch_paths: ["x86_64.txt"] },
358 arm: { arch_paths: ["arm.txt"] },
359 arm64: { arch_paths: ["arm64.txt"] },
Colin Crossf05b0d32022-07-14 18:10:34 -0700360 riscv64: { arch_paths: ["riscv64.txt"] },
Liz Kammerfdd72e62021-10-11 15:41:03 -0400361 },
362 target: {
363 linux: { arch_paths: ["linux.txt"] },
364 bionic: { arch_paths: ["bionic.txt"] },
365 host: { arch_paths: ["host.txt"] },
366 not_windows: { arch_paths: ["not_windows.txt"] },
367 android: { arch_paths: ["android.txt"] },
368 linux_musl: { arch_paths: ["linux_musl.txt"] },
369 musl: { arch_paths: ["musl.txt"] },
370 linux_glibc: { arch_paths: ["linux_glibc.txt"] },
371 glibc: { arch_paths: ["glibc.txt"] },
372 linux_bionic: { arch_paths: ["linux_bionic.txt"] },
373 darwin: { arch_paths: ["darwin.txt"] },
374 windows: { arch_paths: ["windows.txt"] },
375 },
376 multilib: {
377 lib32: { arch_paths: ["lib32.txt"] },
378 lib64: { arch_paths: ["lib64.txt"] },
Liz Kammer4562a3b2021-04-21 18:15:34 -0400379 },
380 bazel_module: { bp2build_available: true },
381}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000382 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000383 MakeBazelTarget("custom", "arch_paths", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500384 "arch_paths": `select({
Liz Kammerfdd72e62021-10-11 15:41:03 -0400385 "//build/bazel/platforms/arch:arm": [
386 "arm.txt",
387 "lib32.txt",
388 ],
389 "//build/bazel/platforms/arch:arm64": [
390 "arm64.txt",
391 "lib64.txt",
392 ],
Colin Crossf05b0d32022-07-14 18:10:34 -0700393 "//build/bazel/platforms/arch:riscv64": [
394 "riscv64.txt",
395 "lib64.txt",
396 ],
Liz Kammerfdd72e62021-10-11 15:41:03 -0400397 "//build/bazel/platforms/arch:x86": [
398 "x86.txt",
399 "lib32.txt",
400 ],
401 "//build/bazel/platforms/arch:x86_64": [
402 "x86_64.txt",
403 "lib64.txt",
404 ],
405 "//conditions:default": [],
406 }) + select({
407 "//build/bazel/platforms/os:android": [
408 "linux.txt",
409 "bionic.txt",
410 "android.txt",
411 ],
412 "//build/bazel/platforms/os:darwin": [
413 "host.txt",
414 "darwin.txt",
415 "not_windows.txt",
416 ],
Liz Kammerfdd72e62021-10-11 15:41:03 -0400417 "//build/bazel/platforms/os:linux_bionic": [
418 "host.txt",
419 "linux.txt",
420 "bionic.txt",
421 "linux_bionic.txt",
422 "not_windows.txt",
423 ],
Colin Cross133782e2022-12-20 15:29:31 -0800424 "//build/bazel/platforms/os:linux_glibc": [
425 "host.txt",
426 "linux.txt",
427 "glibc.txt",
428 "linux_glibc.txt",
429 "not_windows.txt",
430 ],
Liz Kammerfdd72e62021-10-11 15:41:03 -0400431 "//build/bazel/platforms/os:linux_musl": [
432 "host.txt",
433 "linux.txt",
434 "musl.txt",
435 "linux_musl.txt",
436 "not_windows.txt",
437 ],
438 "//build/bazel/platforms/os:windows": [
439 "host.txt",
440 "windows.txt",
441 ],
Liz Kammer4562a3b2021-04-21 18:15:34 -0400442 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500443 })`,
444 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400445 },
446 },
447 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000448 Description: "arch-variant deps",
449 Blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400450 name: "has_dep",
451 arch: {
452 x86: {
453 arch_paths: [":dep"],
454 },
455 },
456 bazel_module: { bp2build_available: true },
457}
458
459custom {
460 name: "dep",
461 arch_paths: ["abc"],
462 bazel_module: { bp2build_available: true },
463}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000464 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000465 MakeBazelTarget("custom", "dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500466 "arch_paths": `["abc"]`,
467 }),
Alixe06d75b2022-08-31 18:28:19 +0000468 MakeBazelTarget("custom", "has_dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500469 "arch_paths": `select({
Liz Kammer4562a3b2021-04-21 18:15:34 -0400470 "//build/bazel/platforms/arch:x86": [":dep"],
471 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500472 })`,
473 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400474 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000475 },
Liz Kammer32a03392021-09-14 11:17:21 -0400476 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000477 Description: "embedded props",
478 Blueprint: `custom {
Liz Kammer32a03392021-09-14 11:17:21 -0400479 name: "embedded_props",
480 embedded_prop: "abc",
481 bazel_module: { bp2build_available: true },
482}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000483 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000484 MakeBazelTarget("custom", "embedded_props", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500485 "embedded_attr": `"abc"`,
486 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400487 },
488 },
489 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000490 Description: "ptr to embedded props",
491 Blueprint: `custom {
Liz Kammer32a03392021-09-14 11:17:21 -0400492 name: "ptr_to_embedded_props",
493 other_embedded_prop: "abc",
494 bazel_module: { bp2build_available: true },
495}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000496 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000497 MakeBazelTarget("custom", "ptr_to_embedded_props", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500498 "other_embedded_attr": `"abc"`,
499 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400500 },
501 },
Jingwen Chen73850672020-12-14 08:25:34 -0500502 }
503
504 dir := "."
505 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000506 t.Run(testCase.Description, func(t *testing.T) {
507 config := android.TestConfig(buildDir, nil, testCase.Blueprint, nil)
Liz Kammerfdd72e62021-10-11 15:41:03 -0400508 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500509
Liz Kammerfdd72e62021-10-11 15:41:03 -0400510 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500511
Liz Kammerfdd72e62021-10-11 15:41:03 -0400512 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
513 if errored(t, testCase, errs) {
514 return
515 }
516 _, errs = ctx.ResolveDependencies(config)
517 if errored(t, testCase, errs) {
518 return
519 }
Jingwen Chen73850672020-12-14 08:25:34 -0500520
Cole Faustb85d1a12022-11-08 18:14:01 -0800521 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammerfdd72e62021-10-11 15:41:03 -0400522 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
523 android.FailIfErrored(t, err)
Jingwen Chen164e0862021-02-19 00:48:40 -0500524
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000525 if actualCount, expectedCount := len(bazelTargets), len(testCase.ExpectedBazelTargets); actualCount != expectedCount {
526 t.Errorf("Expected %d bazel target (%s),\ngot %d (%s)", expectedCount, testCase.ExpectedBazelTargets, actualCount, bazelTargets)
Liz Kammerfdd72e62021-10-11 15:41:03 -0400527 } else {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000528 for i, expectedBazelTarget := range testCase.ExpectedBazelTargets {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400529 actualBazelTarget := bazelTargets[i]
530 if actualBazelTarget.content != expectedBazelTarget {
531 t.Errorf(
532 "Expected generated Bazel target to be '%s', got '%s'",
533 expectedBazelTarget,
534 actualBazelTarget.content,
535 )
536 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400537 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500538 }
Liz Kammerfdd72e62021-10-11 15:41:03 -0400539 })
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800540 }
541}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500542
Liz Kammerdfeb1202022-05-13 17:20:20 -0400543func TestBp2buildHostAndDevice(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000544 testCases := []Bp2buildTestCase{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400545 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000546 Description: "host and device, device only",
547 ModuleTypeUnderTest: "custom",
548 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
549 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400550 name: "foo",
551 bazel_module: { bp2build_available: true },
552}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000553 ExpectedBazelTargets: []string{
554 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400555 },
556 },
557 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000558 Description: "host and device, both",
559 ModuleTypeUnderTest: "custom",
560 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
561 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400562 name: "foo",
563 host_supported: true,
564 bazel_module: { bp2build_available: true },
565}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000566 ExpectedBazelTargets: []string{
567 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400568 },
569 },
570 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000571 Description: "host and device, host explicitly disabled",
572 ModuleTypeUnderTest: "custom",
573 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
574 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400575 name: "foo",
576 host_supported: false,
577 bazel_module: { bp2build_available: true },
578}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000579 ExpectedBazelTargets: []string{
580 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400581 },
582 },
583 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000584 Description: "host and device, neither",
585 ModuleTypeUnderTest: "custom",
586 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
587 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400588 name: "foo",
589 host_supported: false,
590 device_supported: false,
591 bazel_module: { bp2build_available: true },
592}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000593 ExpectedBazelTargets: []string{
594 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400595 "target_compatible_with": `["@platforms//:incompatible"]`,
596 }),
597 },
598 },
599 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000600 Description: "host and device, neither, cannot override with product_var",
601 ModuleTypeUnderTest: "custom",
602 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
603 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400604 name: "foo",
605 host_supported: false,
606 device_supported: false,
607 product_variables: { unbundled_build: { enabled: true } },
608 bazel_module: { bp2build_available: true },
609}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000610 ExpectedBazelTargets: []string{
611 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400612 "target_compatible_with": `["@platforms//:incompatible"]`,
613 }),
614 },
615 },
616 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000617 Description: "host and device, both, disabled overrided with product_var",
618 ModuleTypeUnderTest: "custom",
619 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
620 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400621 name: "foo",
622 host_supported: true,
623 device_supported: true,
624 enabled: false,
625 product_variables: { unbundled_build: { enabled: true } },
626 bazel_module: { bp2build_available: true },
627}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000628 ExpectedBazelTargets: []string{
629 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Cole Faust87c0c332023-07-31 12:10:12 -0700630 "target_compatible_with": `select({
631 "//build/bazel/product_config/config_settings:unbundled_build": [],
632 "//conditions:default": ["@platforms//:incompatible"],
633 })`,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400634 }),
635 },
636 },
637 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000638 Description: "host and device, neither, cannot override with arch enabled",
639 ModuleTypeUnderTest: "custom",
640 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
641 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400642 name: "foo",
643 host_supported: false,
644 device_supported: false,
645 arch: { x86: { enabled: true } },
646 bazel_module: { bp2build_available: true },
647}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000648 ExpectedBazelTargets: []string{
649 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400650 "target_compatible_with": `["@platforms//:incompatible"]`,
651 }),
652 },
653 },
654 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000655 Description: "host and device, host only",
656 ModuleTypeUnderTest: "custom",
657 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
658 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400659 name: "foo",
660 host_supported: true,
661 device_supported: false,
662 bazel_module: { bp2build_available: true },
663}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000664 ExpectedBazelTargets: []string{
665 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400666 },
667 },
668 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000669 Description: "host only",
670 ModuleTypeUnderTest: "custom",
671 ModuleTypeUnderTestFactory: customModuleFactoryHostSupported,
672 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400673 name: "foo",
674 bazel_module: { bp2build_available: true },
675}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000676 ExpectedBazelTargets: []string{
677 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400678 },
679 },
680 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000681 Description: "device only",
682 ModuleTypeUnderTest: "custom",
683 ModuleTypeUnderTestFactory: customModuleFactoryDeviceSupported,
684 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400685 name: "foo",
686 bazel_module: { bp2build_available: true },
687}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000688 ExpectedBazelTargets: []string{
689 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400690 },
691 },
692 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000693 Description: "host and device default, default",
694 ModuleTypeUnderTest: "custom",
695 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
696 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400697 name: "foo",
698 bazel_module: { bp2build_available: true },
699}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000700 ExpectedBazelTargets: []string{
701 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400702 },
703 },
704 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000705 Description: "host and device default, device only",
706 ModuleTypeUnderTest: "custom",
707 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
708 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400709 name: "foo",
710 host_supported: false,
711 bazel_module: { bp2build_available: true },
712}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000713 ExpectedBazelTargets: []string{
714 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400715 },
716 },
717 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000718 Description: "host and device default, host only",
719 ModuleTypeUnderTest: "custom",
720 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
721 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400722 name: "foo",
723 device_supported: false,
724 bazel_module: { bp2build_available: true },
725}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000726 ExpectedBazelTargets: []string{
727 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400728 },
729 },
730 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000731 Description: "host and device default, neither",
732 ModuleTypeUnderTest: "custom",
733 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
734 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400735 name: "foo",
736 host_supported: false,
737 device_supported: false,
738 bazel_module: { bp2build_available: true },
739}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000740 ExpectedBazelTargets: []string{
741 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400742 "target_compatible_with": `["@platforms//:incompatible"]`,
743 }),
744 },
745 },
746 }
747
748 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000749 t.Run(tc.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +0000750 RunBp2BuildTestCaseSimple(t, tc)
Liz Kammerdfeb1202022-05-13 17:20:20 -0400751 })
752 }
753}
754
Jingwen Chen40067de2021-01-26 21:58:43 -0500755func TestLoadStatements(t *testing.T) {
756 testCases := []struct {
757 bazelTargets BazelTargets
758 expectedLoadStatements string
759 }{
760 {
761 bazelTargets: BazelTargets{
762 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700763 name: "foo",
764 ruleClass: "cc_library",
765 loads: []BazelLoad{{
766 file: "//build/bazel/rules:cc.bzl",
767 symbols: []BazelLoadSymbol{{symbol: "cc_library"}},
768 }},
Jingwen Chen40067de2021-01-26 21:58:43 -0500769 },
770 },
771 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
772 },
773 {
774 bazelTargets: BazelTargets{
775 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700776 name: "foo",
777 ruleClass: "cc_library",
778 loads: []BazelLoad{{
779 file: "//build/bazel/rules:cc.bzl",
780 symbols: []BazelLoadSymbol{{symbol: "cc_library"}},
781 }},
Jingwen Chen40067de2021-01-26 21:58:43 -0500782 },
783 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700784 name: "bar",
785 ruleClass: "cc_library",
786 loads: []BazelLoad{{
787 file: "//build/bazel/rules:cc.bzl",
788 symbols: []BazelLoadSymbol{{symbol: "cc_library"}},
789 }},
Jingwen Chen40067de2021-01-26 21:58:43 -0500790 },
791 },
792 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
793 },
794 {
795 bazelTargets: BazelTargets{
796 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700797 name: "foo",
798 ruleClass: "cc_library",
799 loads: []BazelLoad{{
800 file: "//build/bazel/rules:cc.bzl",
801 symbols: []BazelLoadSymbol{{symbol: "cc_library"}},
802 }},
Jingwen Chen40067de2021-01-26 21:58:43 -0500803 },
804 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700805 name: "bar",
806 ruleClass: "cc_binary",
807 loads: []BazelLoad{{
808 file: "//build/bazel/rules:cc.bzl",
809 symbols: []BazelLoadSymbol{{symbol: "cc_binary"}},
810 }},
Jingwen Chen40067de2021-01-26 21:58:43 -0500811 },
812 },
813 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
814 },
815 {
816 bazelTargets: BazelTargets{
817 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700818 name: "foo",
819 ruleClass: "cc_library",
820 loads: []BazelLoad{{
821 file: "//build/bazel/rules:cc.bzl",
822 symbols: []BazelLoadSymbol{{symbol: "cc_library"}},
823 }},
Jingwen Chen40067de2021-01-26 21:58:43 -0500824 },
825 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700826 name: "bar",
827 ruleClass: "cc_binary",
828 loads: []BazelLoad{{
829 file: "//build/bazel/rules:cc.bzl",
830 symbols: []BazelLoadSymbol{{symbol: "cc_binary"}},
831 }},
Jingwen Chen40067de2021-01-26 21:58:43 -0500832 },
833 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700834 name: "baz",
835 ruleClass: "java_binary",
836 loads: []BazelLoad{{
837 file: "//build/bazel/rules:java.bzl",
838 symbols: []BazelLoadSymbol{{symbol: "java_binary"}},
839 }},
Jingwen Chen40067de2021-01-26 21:58:43 -0500840 },
841 },
842 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
843load("//build/bazel/rules:java.bzl", "java_binary")`,
844 },
845 {
846 bazelTargets: BazelTargets{
847 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700848 name: "foo",
849 ruleClass: "cc_binary",
850 loads: []BazelLoad{{
851 file: "//build/bazel/rules:cc.bzl",
852 symbols: []BazelLoadSymbol{{symbol: "cc_binary"}},
853 }},
Jingwen Chen40067de2021-01-26 21:58:43 -0500854 },
855 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700856 name: "bar",
857 ruleClass: "java_binary",
858 loads: []BazelLoad{{
859 file: "//build/bazel/rules:java.bzl",
860 symbols: []BazelLoadSymbol{{symbol: "java_binary"}},
861 }},
Jingwen Chen40067de2021-01-26 21:58:43 -0500862 },
863 BazelTarget{
864 name: "baz",
865 ruleClass: "genrule",
Cole Faustb4cb0c82023-09-14 15:16:58 -0700866 // Note: no loads for native rules
Jingwen Chen40067de2021-01-26 21:58:43 -0500867 },
868 },
869 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
870load("//build/bazel/rules:java.bzl", "java_binary")`,
871 },
872 }
873
874 for _, testCase := range testCases {
875 actual := testCase.bazelTargets.LoadStatements()
876 expected := testCase.expectedLoadStatements
877 if actual != expected {
878 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
879 }
880 }
881
882}
883
884func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
885 testCases := []struct {
886 bp string
887 expectedBazelTarget string
888 expectedBazelTargetCount int
889 expectedLoadStatements string
890 }{
891 {
892 bp: `custom {
893 name: "bar",
Liz Kammerdfeb1202022-05-13 17:20:20 -0400894 host_supported: true,
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400895 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500896 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500897}`,
898 expectedBazelTarget: `my_library(
899 name = "bar",
900)
901
Jingwen Chen40067de2021-01-26 21:58:43 -0500902proto_library(
903 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400904)
905
906my_proto_library(
907 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500908)`,
909 expectedBazelTargetCount: 3,
910 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
911load("//build/bazel/rules:rules.bzl", "my_library")`,
912 },
913 }
914
915 dir := "."
916 for _, testCase := range testCases {
917 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
918 ctx := android.NewTestContext(config)
Liz Kammerdfeb1202022-05-13 17:20:20 -0400919 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
Jingwen Chen40067de2021-01-26 21:58:43 -0500920 ctx.RegisterForBazelConversion()
921
922 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
923 android.FailIfErrored(t, errs)
924 _, errs = ctx.ResolveDependencies(config)
925 android.FailIfErrored(t, errs)
926
Cole Faustb85d1a12022-11-08 18:14:01 -0800927 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammer6eff3232021-08-26 08:37:59 -0400928 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
929 android.FailIfErrored(t, err)
Jingwen Chen40067de2021-01-26 21:58:43 -0500930 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
931 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
932 }
933
934 actualBazelTargets := bazelTargets.String()
935 if actualBazelTargets != testCase.expectedBazelTarget {
936 t.Errorf(
937 "Expected generated Bazel target to be '%s', got '%s'",
938 testCase.expectedBazelTarget,
939 actualBazelTargets,
940 )
941 }
942
943 actualLoadStatements := bazelTargets.LoadStatements()
944 if actualLoadStatements != testCase.expectedLoadStatements {
945 t.Errorf(
946 "Expected generated load statements to be '%s', got '%s'",
947 testCase.expectedLoadStatements,
948 actualLoadStatements,
949 )
950 }
951 }
952}
953
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500954func TestModuleTypeBp2Build(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000955 testCases := []Bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500956 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000957 Description: "filegroup with does not specify srcs",
958 ModuleTypeUnderTest: "filegroup",
959 ModuleTypeUnderTestFactory: android.FileGroupFactory,
960 Blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500961 name: "fg_foo",
962 bazel_module: { bp2build_available: true },
963}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000964 ExpectedBazelTargets: []string{
965 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerebfcf672021-02-16 15:00:05 -0500966 },
967 },
968 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000969 Description: "filegroup with no srcs",
970 ModuleTypeUnderTest: "filegroup",
971 ModuleTypeUnderTestFactory: android.FileGroupFactory,
972 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500973 name: "fg_foo",
974 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500975 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500976}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000977 ExpectedBazelTargets: []string{
978 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammer356f7d42021-01-26 09:18:53 -0500979 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500980 },
981 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000982 Description: "filegroup with srcs",
983 ModuleTypeUnderTest: "filegroup",
984 ModuleTypeUnderTestFactory: android.FileGroupFactory,
985 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500986 name: "fg_foo",
987 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500988 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500989}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000990 ExpectedBazelTargets: []string{
991 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500992 "srcs": `[
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500993 "a",
994 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500995 ]`,
996 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500997 },
998 },
999 {
Usta Shresthad5580312022-09-23 16:46:38 -04001000 Description: "filegroup with dot-slash-prefixed srcs",
1001 ModuleTypeUnderTest: "filegroup",
1002 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1003 Blueprint: `filegroup {
1004 name: "fg_foo",
1005 srcs: ["./a", "./b"],
1006 bazel_module: { bp2build_available: true },
1007}`,
1008 ExpectedBazelTargets: []string{
1009 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1010 "srcs": `[
1011 "a",
1012 "b",
1013 ]`,
1014 }),
1015 },
1016 },
1017 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001018 Description: "filegroup with excludes srcs",
1019 ModuleTypeUnderTest: "filegroup",
1020 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1021 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -05001022 name: "fg_foo",
1023 srcs: ["a", "b"],
1024 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001025 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -05001026}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001027 ExpectedBazelTargets: []string{
1028 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001029 "srcs": `["b"]`,
1030 }),
Liz Kammer356f7d42021-01-26 09:18:53 -05001031 },
1032 },
1033 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001034 Description: "depends_on_other_dir_module",
1035 ModuleTypeUnderTest: "filegroup",
1036 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1037 Blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001038 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -05001039 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +00001040 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -05001041 "c",
1042 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001043 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -05001044}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001045 Filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -05001046 "other/Android.bp": `filegroup {
1047 name: "foo",
1048 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -04001049 bazel_module: { bp2build_available: true },
1050}`,
1051 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001052 ExpectedBazelTargets: []string{
1053 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001054 "srcs": `[
1055 "//other:foo",
1056 "c",
1057 ]`,
1058 }),
1059 },
Liz Kammer6eff3232021-08-26 08:37:59 -04001060 },
1061 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001062 Description: "depends_on_other_unconverted_module_error",
1063 ModuleTypeUnderTest: "filegroup",
1064 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1065 UnconvertedDepsMode: errorModulesUnconvertedDeps,
1066 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001067 name: "foobar",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001068 srcs: [
1069 ":foo",
1070 "c",
1071 ],
1072 bazel_module: { bp2build_available: true },
1073}`,
Sasha Smundakf2bb26f2022-08-04 11:28:15 -07001074 ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on unconverted modules: foo`),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001075 Filesystem: map[string]string{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001076 "other/Android.bp": `filegroup {
1077 name: "foo",
1078 srcs: ["a", "b"],
1079}`,
1080 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001081 },
Usta Shrestha808bc712022-09-23 23:18:18 -04001082 {
1083 Description: "depends_on_other_missing_module_error",
1084 ModuleTypeUnderTest: "filegroup",
1085 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1086 UnconvertedDepsMode: errorModulesUnconvertedDeps,
1087 Blueprint: `filegroup {
1088 name: "foobar",
1089 srcs: [
1090 "c",
1091 "//other:foo",
1092 "//other:goo",
1093 ],
1094 bazel_module: { bp2build_available: true },
1095}`,
1096 ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on missing modules: //other:goo`),
1097 Filesystem: map[string]string{"other/Android.bp": `filegroup {
1098 name: "foo",
1099 srcs: ["a"],
1100 bazel_module: { bp2build_available: true },
1101}
1102`,
1103 },
1104 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001105 }
1106
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001107 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001108 t.Run(testCase.Description, func(t *testing.T) {
1109 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
Liz Kammer6eff3232021-08-26 08:37:59 -04001110 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001111 }
1112}
Jingwen Chen041b1842021-02-01 00:23:25 -05001113
Jingwen Chen12b4c272021-03-10 02:05:59 -05001114func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001115 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001116 moduleTypeUnderTest string
1117 moduleTypeUnderTestFactory android.ModuleFactory
1118 bp string
1119 expectedCount int
1120 description string
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001121 }{
1122 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001123 description: "explicitly unavailable",
1124 moduleTypeUnderTest: "filegroup",
1125 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001126 bp: `filegroup {
1127 name: "foo",
1128 srcs: ["a", "b"],
1129 bazel_module: { bp2build_available: false },
1130}`,
1131 expectedCount: 0,
1132 },
1133 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001134 description: "implicitly unavailable",
1135 moduleTypeUnderTest: "filegroup",
1136 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001137 bp: `filegroup {
1138 name: "foo",
1139 srcs: ["a", "b"],
1140}`,
1141 expectedCount: 0,
1142 },
1143 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001144 description: "explicitly available",
1145 moduleTypeUnderTest: "filegroup",
1146 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001147 bp: `filegroup {
1148 name: "foo",
1149 srcs: ["a", "b"],
1150 bazel_module: { bp2build_available: true },
1151}`,
1152 expectedCount: 1,
1153 },
1154 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001155 description: "generates more than 1 target if needed",
1156 moduleTypeUnderTest: "custom",
Liz Kammerdfeb1202022-05-13 17:20:20 -04001157 moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001158 bp: `custom {
1159 name: "foo",
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001160 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001161 bazel_module: { bp2build_available: true },
1162}`,
1163 expectedCount: 3,
1164 },
1165 }
1166
1167 dir := "."
1168 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -04001169 t.Run(testCase.description, func(t *testing.T) {
1170 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1171 ctx := android.NewTestContext(config)
1172 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001173 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001174
Liz Kammer2ada09a2021-08-11 00:17:36 -04001175 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1176 android.FailIfErrored(t, errs)
1177 _, errs = ctx.ResolveDependencies(config)
1178 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001179
Cole Faustb85d1a12022-11-08 18:14:01 -08001180 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammer6eff3232021-08-26 08:37:59 -04001181 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1182 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001183 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1184 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1185 }
1186 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001187 }
1188}
Liz Kammerba3ea162021-02-17 13:22:03 -05001189
Jingwen Chen12b4c272021-03-10 02:05:59 -05001190func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1191 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001192 moduleTypeUnderTest string
1193 moduleTypeUnderTestFactory android.ModuleFactory
1194 expectedCount map[string]int
1195 description string
Sam Delmerico24c56032022-03-28 19:53:03 +00001196 bp2buildConfig allowlists.Bp2BuildConfig
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001197 checkDir string
1198 fs map[string]string
MarkDacek9c094ca2023-03-16 19:15:19 +00001199 forceEnabledModules []string
1200 expectedErrorMessages []string
Jingwen Chen12b4c272021-03-10 02:05:59 -05001201 }{
1202 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001203 description: "test bp2build config package and subpackages config",
1204 moduleTypeUnderTest: "filegroup",
1205 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001206 expectedCount: map[string]int{
1207 "migrated": 1,
1208 "migrated/but_not_really": 0,
1209 "migrated/but_not_really/but_really": 1,
1210 "not_migrated": 0,
1211 "also_not_migrated": 0,
1212 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001213 bp2buildConfig: allowlists.Bp2BuildConfig{
1214 "migrated": allowlists.Bp2BuildDefaultTrueRecursively,
1215 "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
1216 "not_migrated": allowlists.Bp2BuildDefaultFalse,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001217 },
1218 fs: map[string]string{
1219 "migrated/Android.bp": `filegroup { name: "a" }`,
1220 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1221 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1222 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1223 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1224 },
1225 },
1226 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001227 description: "test bp2build config opt-in and opt-out",
1228 moduleTypeUnderTest: "filegroup",
1229 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001230 expectedCount: map[string]int{
1231 "package-opt-in": 2,
1232 "package-opt-in/subpackage": 0,
1233 "package-opt-out": 1,
1234 "package-opt-out/subpackage": 0,
1235 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001236 bp2buildConfig: allowlists.Bp2BuildConfig{
1237 "package-opt-in": allowlists.Bp2BuildDefaultFalse,
1238 "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001239 },
1240 fs: map[string]string{
1241 "package-opt-in/Android.bp": `
1242filegroup { name: "opt-in-a" }
1243filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1244filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1245`,
1246
1247 "package-opt-in/subpackage/Android.bp": `
1248filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1249`,
1250
1251 "package-opt-out/Android.bp": `
1252filegroup { name: "opt-out-a" }
1253filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1254filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1255`,
1256
1257 "package-opt-out/subpackage/Android.bp": `
1258filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1259filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1260`,
1261 },
1262 },
MarkDacek9c094ca2023-03-16 19:15:19 +00001263 {
1264 description: "test force-enabled errors out",
1265 moduleTypeUnderTest: "filegroup",
1266 moduleTypeUnderTestFactory: android.FileGroupFactory,
1267 expectedCount: map[string]int{
1268 "migrated": 0,
1269 "not_migrated": 0,
1270 },
1271 bp2buildConfig: allowlists.Bp2BuildConfig{
1272 "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
1273 "not_migrated": allowlists.Bp2BuildDefaultFalse,
1274 },
1275 fs: map[string]string{
1276 "migrated/Android.bp": `filegroup { name: "a" }`,
1277 },
1278 forceEnabledModules: []string{"a"},
1279 expectedErrorMessages: []string{"Force Enabled Module a not converted"},
1280 },
Jingwen Chen12b4c272021-03-10 02:05:59 -05001281 }
1282
1283 dir := "."
1284 for _, testCase := range testCases {
1285 fs := make(map[string][]byte)
1286 toParse := []string{
1287 "Android.bp",
1288 }
1289 for f, content := range testCase.fs {
1290 if strings.HasSuffix(f, "Android.bp") {
1291 toParse = append(toParse, f)
1292 }
1293 fs[f] = []byte(content)
1294 }
1295 config := android.TestConfig(buildDir, nil, "", fs)
MarkDacek9c094ca2023-03-16 19:15:19 +00001296 config.AddForceEnabledModules(testCase.forceEnabledModules)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001297 ctx := android.NewTestContext(config)
1298 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Sam Delmerico24c56032022-03-28 19:53:03 +00001299 allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig)
1300 ctx.RegisterBp2BuildConfig(allowlist)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001301 ctx.RegisterForBazelConversion()
1302
1303 _, errs := ctx.ParseFileList(dir, toParse)
1304 android.FailIfErrored(t, errs)
1305 _, errs = ctx.ResolveDependencies(config)
1306 android.FailIfErrored(t, errs)
1307
Cole Faustb85d1a12022-11-08 18:14:01 -08001308 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Jingwen Chen12b4c272021-03-10 02:05:59 -05001309
1310 // For each directory, test that the expected number of generated targets is correct.
1311 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -04001312 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
MarkDacek9c094ca2023-03-16 19:15:19 +00001313 android.CheckErrorsAgainstExpectations(t, err, testCase.expectedErrorMessages)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001314 if actualCount := len(bazelTargets); actualCount != expectedCount {
1315 t.Fatalf(
1316 "%s: Expected %d bazel target for %s package, got %d",
1317 testCase.description,
1318 expectedCount,
1319 dir,
1320 actualCount)
1321 }
1322
1323 }
1324 }
1325}
1326
Liz Kammerba3ea162021-02-17 13:22:03 -05001327func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001328 testCases := []Bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -05001329 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001330 Description: "filegroup bazel_module.label",
1331 ModuleTypeUnderTest: "filegroup",
1332 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1333 Blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -05001334 name: "fg_foo",
1335 bazel_module: { label: "//other:fg_foo" },
1336}`,
Cole Faustea602c52022-08-31 14:48:26 -07001337 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001338 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001339 "other/BUILD.bazel": `// BUILD file`,
1340 },
1341 },
1342 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001343 Description: "multiple bazel_module.label same BUILD",
1344 ModuleTypeUnderTest: "filegroup",
1345 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1346 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001347 name: "fg_foo",
1348 bazel_module: { label: "//other:fg_foo" },
1349 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001350
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001351 filegroup {
1352 name: "foo",
1353 bazel_module: { label: "//other:foo" },
1354 }`,
Cole Faustea602c52022-08-31 14:48:26 -07001355 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001356 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001357 "other/BUILD.bazel": `// BUILD file`,
1358 },
1359 },
1360 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001361 Description: "filegroup bazel_module.label and bp2build in subdir",
1362 ModuleTypeUnderTest: "filegroup",
1363 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1364 Dir: "other",
1365 Blueprint: ``,
1366 Filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001367 "other/Android.bp": `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001368 name: "fg_foo",
1369 bazel_module: {
1370 bp2build_available: true,
1371 },
1372 }
1373 filegroup {
1374 name: "fg_bar",
1375 bazel_module: {
1376 label: "//other:fg_bar"
1377 },
1378 }`,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001379 "other/BUILD.bazel": `// definition for fg_bar`,
1380 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001381 ExpectedBazelTargets: []string{
1382 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001383 },
1384 },
1385 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001386 Description: "filegroup bazel_module.label and filegroup bp2build",
1387 ModuleTypeUnderTest: "filegroup",
1388 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001389
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001390 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001391 "other/BUILD.bazel": `// BUILD file`,
1392 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001393 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001394 name: "fg_foo",
1395 bazel_module: {
1396 label: "//other:fg_foo",
1397 },
1398 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001399
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001400 filegroup {
1401 name: "fg_bar",
1402 bazel_module: {
1403 bp2build_available: true,
1404 },
1405 }`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001406 ExpectedBazelTargets: []string{
1407 MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001408 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001409 },
1410 }
1411
1412 dir := "."
1413 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001414 t.Run(testCase.Description, func(t *testing.T) {
Jingwen Chen49109762021-05-25 05:16:48 +00001415 fs := make(map[string][]byte)
1416 toParse := []string{
1417 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001418 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001419 for f, content := range testCase.Filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001420 if strings.HasSuffix(f, "Android.bp") {
1421 toParse = append(toParse, f)
1422 }
1423 fs[f] = []byte(content)
1424 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001425 config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001426 ctx := android.NewTestContext(config)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001427 ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001428 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001429
Jingwen Chen49109762021-05-25 05:16:48 +00001430 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001431 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001432 return
1433 }
1434 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001435 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001436 return
1437 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001438
Jingwen Chen49109762021-05-25 05:16:48 +00001439 checkDir := dir
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001440 if testCase.Dir != "" {
1441 checkDir = testCase.Dir
Jingwen Chen49109762021-05-25 05:16:48 +00001442 }
Cole Faustb85d1a12022-11-08 18:14:01 -08001443 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammer6eff3232021-08-26 08:37:59 -04001444 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1445 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001446 bazelTargets.sort()
1447 actualCount := len(bazelTargets)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001448 expectedCount := len(testCase.ExpectedBazelTargets)
Jingwen Chen49109762021-05-25 05:16:48 +00001449 if actualCount != expectedCount {
1450 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1451 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001452 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001453 actualContent := target.content
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001454 expectedContent := testCase.ExpectedBazelTargets[i]
Jingwen Chen49109762021-05-25 05:16:48 +00001455 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001456 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001457 "Expected generated Bazel target to be '%s', got '%s'",
1458 expectedContent,
1459 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001460 )
1461 }
1462 }
Jingwen Chen49109762021-05-25 05:16:48 +00001463 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001464 }
1465}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001466
Jingwen Chen0eeaeb82022-09-21 10:27:42 +00001467func TestGlob(t *testing.T) {
1468 testCases := []Bp2buildTestCase{
1469 {
1470 Description: "filegroup with glob",
1471 ModuleTypeUnderTest: "filegroup",
1472 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1473 Blueprint: `filegroup {
1474 name: "fg_foo",
1475 srcs: ["**/*.txt"],
1476 bazel_module: { bp2build_available: true },
1477}`,
1478 ExpectedBazelTargets: []string{
1479 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1480 "srcs": `[
1481 "other/a.txt",
1482 "other/b.txt",
1483 "other/subdir/a.txt",
1484 ]`,
1485 }),
1486 },
1487 Filesystem: map[string]string{
1488 "other/a.txt": "",
1489 "other/b.txt": "",
1490 "other/subdir/a.txt": "",
1491 "other/file": "",
1492 },
1493 },
1494 {
1495 Description: "filegroup with glob in subdir",
1496 ModuleTypeUnderTest: "filegroup",
1497 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1498 Dir: "other",
1499 Filesystem: map[string]string{
1500 "other/Android.bp": `filegroup {
1501 name: "fg_foo",
1502 srcs: ["**/*.txt"],
1503 bazel_module: { bp2build_available: true },
1504}`,
1505 "other/a.txt": "",
1506 "other/b.txt": "",
1507 "other/subdir/a.txt": "",
1508 "other/file": "",
1509 },
1510 ExpectedBazelTargets: []string{
1511 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1512 "srcs": `[
1513 "a.txt",
1514 "b.txt",
1515 "subdir/a.txt",
1516 ]`,
1517 }),
1518 },
1519 },
1520 {
1521 Description: "filegroup with glob with no kept BUILD files",
1522 ModuleTypeUnderTest: "filegroup",
1523 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1524 KeepBuildFileForDirs: []string{
1525 // empty
1526 },
1527 Blueprint: `filegroup {
1528 name: "fg_foo",
1529 srcs: ["**/*.txt"],
1530 bazel_module: { bp2build_available: true },
1531}`,
1532 Filesystem: map[string]string{
1533 "a.txt": "",
1534 "b.txt": "",
1535 "foo/BUILD": "",
1536 "foo/a.txt": "",
1537 "foo/bar/BUILD": "",
1538 "foo/bar/b.txt": "",
1539 },
1540 ExpectedBazelTargets: []string{
1541 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1542 "srcs": `[
1543 "a.txt",
1544 "b.txt",
1545 "foo/a.txt",
1546 "foo/bar/b.txt",
1547 ]`,
1548 }),
1549 },
1550 },
1551 {
1552 Description: "filegroup with glob with kept BUILD file",
1553 ModuleTypeUnderTest: "filegroup",
1554 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1555 KeepBuildFileForDirs: []string{
1556 "foo",
1557 },
1558 Blueprint: `filegroup {
1559 name: "fg_foo",
1560 srcs: ["**/*.txt"],
1561 bazel_module: { bp2build_available: true },
1562}`,
1563 Filesystem: map[string]string{
1564 "a.txt": "",
1565 "b.txt": "",
1566 "foo/BUILD": "",
1567 "foo/a.txt": "",
1568 "foo/bar/BUILD": "",
1569 "foo/bar/b.txt": "",
1570 },
1571 ExpectedBazelTargets: []string{
1572 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1573 "srcs": `[
1574 "a.txt",
1575 "b.txt",
1576 "//foo:a.txt",
1577 "//foo:bar/b.txt",
1578 ]`,
1579 }),
1580 },
1581 },
1582 {
1583 Description: "filegroup with glob with kept BUILD.bazel file",
1584 ModuleTypeUnderTest: "filegroup",
1585 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1586 KeepBuildFileForDirs: []string{
1587 "foo",
1588 },
1589 Blueprint: `filegroup {
1590 name: "fg_foo",
1591 srcs: ["**/*.txt"],
1592 bazel_module: { bp2build_available: true },
1593}`,
1594 Filesystem: map[string]string{
1595 "a.txt": "",
1596 "b.txt": "",
1597 "foo/BUILD.bazel": "",
1598 "foo/a.txt": "",
1599 "foo/bar/BUILD.bazel": "",
1600 "foo/bar/b.txt": "",
1601 },
1602 ExpectedBazelTargets: []string{
1603 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1604 "srcs": `[
1605 "a.txt",
1606 "b.txt",
1607 "//foo:a.txt",
1608 "//foo:bar/b.txt",
1609 ]`,
1610 }),
1611 },
1612 },
1613 {
1614 Description: "filegroup with glob with Android.bp file as boundary",
1615 ModuleTypeUnderTest: "filegroup",
1616 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1617 Blueprint: `filegroup {
1618 name: "fg_foo",
1619 srcs: ["**/*.txt"],
1620 bazel_module: { bp2build_available: true },
1621}`,
1622 Filesystem: map[string]string{
1623 "a.txt": "",
1624 "b.txt": "",
1625 "foo/Android.bp": "",
1626 "foo/a.txt": "",
1627 "foo/bar/Android.bp": "",
1628 "foo/bar/b.txt": "",
1629 },
1630 ExpectedBazelTargets: []string{
1631 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1632 "srcs": `[
1633 "a.txt",
1634 "b.txt",
1635 "//foo:a.txt",
1636 "//foo/bar:b.txt",
1637 ]`,
1638 }),
1639 },
1640 },
1641 {
1642 Description: "filegroup with glob in subdir with kept BUILD and BUILD.bazel file",
1643 ModuleTypeUnderTest: "filegroup",
1644 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1645 Dir: "other",
1646 KeepBuildFileForDirs: []string{
1647 "other/foo",
1648 "other/foo/bar",
1649 // deliberately not other/foo/baz/BUILD.
1650 },
1651 Filesystem: map[string]string{
1652 "other/Android.bp": `filegroup {
1653 name: "fg_foo",
1654 srcs: ["**/*.txt"],
1655 bazel_module: { bp2build_available: true },
1656}`,
1657 "other/a.txt": "",
1658 "other/b.txt": "",
1659 "other/foo/BUILD": "",
1660 "other/foo/a.txt": "",
1661 "other/foo/bar/BUILD.bazel": "",
1662 "other/foo/bar/b.txt": "",
1663 "other/foo/baz/BUILD": "",
1664 "other/foo/baz/c.txt": "",
1665 },
1666 ExpectedBazelTargets: []string{
1667 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1668 "srcs": `[
1669 "a.txt",
1670 "b.txt",
1671 "//other/foo:a.txt",
1672 "//other/foo/bar:b.txt",
1673 "//other/foo:baz/c.txt",
1674 ]`,
1675 }),
1676 },
1677 },
1678 }
1679
1680 for _, testCase := range testCases {
1681 t.Run(testCase.Description, func(t *testing.T) {
1682 RunBp2BuildTestCaseSimple(t, testCase)
1683 })
1684 }
1685}
1686
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001687func TestGlobExcludeSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001688 testCases := []Bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001689 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001690 Description: "filegroup top level exclude_srcs",
1691 ModuleTypeUnderTest: "filegroup",
1692 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1693 Blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001694 name: "fg_foo",
1695 srcs: ["**/*.txt"],
1696 exclude_srcs: ["c.txt"],
1697 bazel_module: { bp2build_available: true },
1698}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001699 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001700 "a.txt": "",
1701 "b.txt": "",
1702 "c.txt": "",
1703 "dir/Android.bp": "",
1704 "dir/e.txt": "",
1705 "dir/f.txt": "",
1706 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001707 ExpectedBazelTargets: []string{
1708 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001709 "srcs": `[
1710 "a.txt",
1711 "b.txt",
1712 "//dir:e.txt",
1713 "//dir:f.txt",
1714 ]`,
1715 }),
1716 },
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001717 },
1718 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001719 Description: "filegroup in subdir exclude_srcs",
1720 ModuleTypeUnderTest: "filegroup",
1721 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1722 Blueprint: "",
1723 Dir: "dir",
1724 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001725 "dir/Android.bp": `filegroup {
1726 name: "fg_foo",
1727 srcs: ["**/*.txt"],
1728 exclude_srcs: ["b.txt"],
1729 bazel_module: { bp2build_available: true },
1730}
1731`,
1732 "dir/a.txt": "",
1733 "dir/b.txt": "",
1734 "dir/subdir/Android.bp": "",
1735 "dir/subdir/e.txt": "",
1736 "dir/subdir/f.txt": "",
1737 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001738 ExpectedBazelTargets: []string{
1739 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001740 "srcs": `[
Liz Kammer9abd62d2021-05-21 08:37:59 -04001741 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001742 "//dir/subdir:e.txt",
1743 "//dir/subdir:f.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001744 ]`,
1745 }),
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001746 },
1747 },
1748 }
1749
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001750 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001751 t.Run(testCase.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001752 RunBp2BuildTestCaseSimple(t, testCase)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001753 })
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001754 }
1755}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001756
1757func TestCommonBp2BuildModuleAttrs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001758 testCases := []Bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001759 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001760 Description: "Required into data test",
1761 ModuleTypeUnderTest: "filegroup",
1762 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001763 StubbedBuildDefinitions: []string{"reqd"},
1764 Blueprint: simpleModule("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001765filegroup {
1766 name: "fg_foo",
1767 required: ["reqd"],
1768 bazel_module: { bp2build_available: true },
1769}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001770 ExpectedBazelTargets: []string{
1771 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001772 "data": `[":reqd"]`,
1773 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001774 },
1775 },
1776 {
Jingwen Chena5ecb372022-09-21 09:05:37 +00001777 Description: "Required into data test, cyclic self reference is filtered out",
1778 ModuleTypeUnderTest: "filegroup",
1779 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001780 StubbedBuildDefinitions: []string{"reqd"},
1781 Blueprint: simpleModule("filegroup", "reqd") + `
Jingwen Chena5ecb372022-09-21 09:05:37 +00001782filegroup {
1783 name: "fg_foo",
1784 required: ["reqd", "fg_foo"],
1785 bazel_module: { bp2build_available: true },
1786}`,
1787 ExpectedBazelTargets: []string{
1788 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1789 "data": `[":reqd"]`,
1790 }),
1791 },
1792 },
1793 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001794 Description: "Required via arch into data test",
1795 ModuleTypeUnderTest: "python_library",
1796 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001797 StubbedBuildDefinitions: []string{"reqdx86", "reqdarm"},
1798 Blueprint: simpleModule("python_library", "reqdx86") +
1799 simpleModule("python_library", "reqdarm") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001800python_library {
1801 name: "fg_foo",
1802 arch: {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001803 arm: {
1804 required: ["reqdarm"],
1805 },
1806 x86: {
1807 required: ["reqdx86"],
1808 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001809 },
1810 bazel_module: { bp2build_available: true },
1811}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001812 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001813 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001814 "data": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001815 "//build/bazel/platforms/arch:arm": [":reqdarm"],
1816 "//build/bazel/platforms/arch:x86": [":reqdx86"],
1817 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001818 })`,
1819 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001820 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001821 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001822 },
1823 },
1824 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001825 Description: "Required appended to data test",
1826 ModuleTypeUnderTest: "python_library",
1827 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1828 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001829 "data.bin": "",
1830 "src.py": "",
1831 },
Chris Parsonscd209032023-09-19 01:12:48 +00001832 StubbedBuildDefinitions: []string{"reqd"},
1833 Blueprint: simpleModule("python_library", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001834python_library {
1835 name: "fg_foo",
1836 data: ["data.bin"],
1837 required: ["reqd"],
1838 bazel_module: { bp2build_available: true },
1839}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001840 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001841 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001842 "data": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001843 "data.bin",
1844 ":reqd",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001845 ]`,
1846 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001847 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001848 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001849 },
1850 },
1851 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001852 Description: "All props-to-attrs at once together test",
1853 ModuleTypeUnderTest: "filegroup",
1854 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001855 StubbedBuildDefinitions: []string{"reqd"},
1856 Blueprint: simpleModule("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001857filegroup {
1858 name: "fg_foo",
1859 required: ["reqd"],
1860 bazel_module: { bp2build_available: true },
1861}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001862 ExpectedBazelTargets: []string{
1863 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001864 "data": `[":reqd"]`,
1865 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001866 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001867 },
1868 }
1869
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001870 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001871 t.Run(tc.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001872 RunBp2BuildTestCaseSimple(t, tc)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001873 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001874 }
1875}
Sasha Smundak05b0ba62022-09-26 18:15:45 -07001876
1877func TestLicensesAttrConversion(t *testing.T) {
1878 RunBp2BuildTestCase(t,
1879 func(ctx android.RegistrationContext) {
1880 ctx.RegisterModuleType("license", android.LicenseFactory)
1881 },
1882 Bp2buildTestCase{
1883 Description: "Test that licenses: attribute is converted",
1884 ModuleTypeUnderTest: "filegroup",
1885 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1886 Blueprint: `
1887license {
1888 name: "my_license",
1889}
1890filegroup {
1891 name: "my_filegroup",
1892 licenses: ["my_license"],
1893}
1894`,
1895 ExpectedBazelTargets: []string{
1896 MakeBazelTargetNoRestrictions("filegroup", "my_filegroup", AttrNameToString{
1897 "applicable_licenses": `[":my_license"]`,
1898 }),
1899 MakeBazelTargetNoRestrictions("android_license", "my_license", AttrNameToString{}),
1900 },
1901 })
1902}
Spandan Das5af0bd32022-09-28 20:43:08 +00001903
Spandan Das6a448ec2023-04-19 17:36:12 +00001904func TestGenerateConfigSetting(t *testing.T) {
1905 bp := `
1906 custom {
1907 name: "foo",
1908 test_config_setting: true,
1909 }
1910 `
1911 expectedBazelTargets := []string{
1912 MakeBazelTargetNoRestrictions(
1913 "config_setting",
1914 "foo_config_setting",
1915 AttrNameToString{
1916 "flag_values": `{
1917 "//build/bazel/rules/my_string_setting": "foo",
1918 }`,
1919 },
1920 ),
1921 MakeBazelTarget(
1922 "custom",
1923 "foo",
1924 AttrNameToString{},
1925 ),
1926 }
1927 registerCustomModule := func(ctx android.RegistrationContext) {
1928 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
1929 }
1930 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
1931 Blueprint: bp,
1932 ExpectedBazelTargets: expectedBazelTargets,
1933 Description: "Generating API contribution Bazel targets for custom module",
1934 })
1935}
Spandan Das921af322023-04-26 02:56:37 +00001936
1937// If values of all keys in an axis are equal to //conditions:default, drop the axis and print the common value
1938func TestPrettyPrintSelectMapEqualValues(t *testing.T) {
1939 lla := bazel.LabelListAttribute{
1940 Value: bazel.LabelList{},
1941 }
1942 libFooImplLabel := bazel.Label{
1943 Label: ":libfoo.impl",
1944 }
Spandan Das6d4d9da2023-04-18 06:20:40 +00001945 lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidPlatform, bazel.MakeLabelList([]bazel.Label{libFooImplLabel}))
Spandan Das921af322023-04-26 02:56:37 +00001946 lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.MakeLabelList([]bazel.Label{libFooImplLabel}))
1947 actual, _ := prettyPrintAttribute(lla, 0)
1948 android.AssertStringEquals(t, "Print the common value if all keys in an axis have the same value", `[":libfoo.impl"]`, actual)
1949}
Spandan Das3131d672023-08-03 22:33:47 +00001950
Chris Parsons5011e612023-09-13 23:33:20 +00001951func TestAlreadyPresentBuildTarget(t *testing.T) {
1952 bp := `
1953 custom {
1954 name: "foo",
1955 }
1956 custom {
1957 name: "bar",
1958 }
1959 `
1960 alreadyPresentBuildFile :=
1961 MakeBazelTarget(
1962 "custom",
1963 "foo",
1964 AttrNameToString{},
1965 )
1966 expectedBazelTargets := []string{
1967 MakeBazelTarget(
1968 "custom",
1969 "bar",
1970 AttrNameToString{},
1971 ),
1972 }
1973 registerCustomModule := func(ctx android.RegistrationContext) {
1974 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
1975 }
1976 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
1977 AlreadyExistingBuildContents: alreadyPresentBuildFile,
1978 Blueprint: bp,
1979 ExpectedBazelTargets: expectedBazelTargets,
1980 Description: "Not duplicating work for an already-present BUILD target.",
1981 })
1982}
1983
Chris Parsons0c4de1f2023-09-21 20:36:35 +00001984func TestAlreadyPresentOneToManyBuildTarget(t *testing.T) {
1985 bp := `
1986 custom {
1987 name: "foo",
1988 one_to_many_prop: true,
1989 }
1990 custom {
1991 name: "bar",
1992 }
1993 `
1994 alreadyPresentBuildFile :=
1995 MakeBazelTarget(
1996 "custom",
1997 // one_to_many_prop ensures that foo generates "foo_proto_library_deps".
1998 "foo_proto_library_deps",
1999 AttrNameToString{},
2000 )
2001 expectedBazelTargets := []string{
2002 MakeBazelTarget(
2003 "custom",
2004 "bar",
2005 AttrNameToString{},
2006 ),
2007 }
2008 registerCustomModule := func(ctx android.RegistrationContext) {
2009 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2010 }
2011 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2012 AlreadyExistingBuildContents: alreadyPresentBuildFile,
2013 Blueprint: bp,
2014 ExpectedBazelTargets: expectedBazelTargets,
2015 Description: "Not duplicating work for an already-present BUILD target (different generated name)",
2016 })
2017}
2018
Chris Parsons5011e612023-09-13 23:33:20 +00002019// Verifies that if a module is defined in pkg1/Android.bp, that a target present
2020// in pkg2/BUILD.bazel does not result in the module being labeled "already defined
2021// in a BUILD file".
2022func TestBuildTargetPresentOtherDirectory(t *testing.T) {
2023 bp := `
2024 custom {
2025 name: "foo",
2026 }
2027 `
2028 expectedBazelTargets := []string{
2029 MakeBazelTarget(
2030 "custom",
2031 "foo",
2032 AttrNameToString{},
2033 ),
2034 }
2035 registerCustomModule := func(ctx android.RegistrationContext) {
2036 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2037 }
2038 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2039 KeepBuildFileForDirs: []string{"other_pkg"},
2040 Filesystem: map[string]string{
2041 "other_pkg/BUILD.bazel": MakeBazelTarget("custom", "foo", AttrNameToString{}),
2042 },
2043 Blueprint: bp,
2044 ExpectedBazelTargets: expectedBazelTargets,
2045 Description: "Not treating a BUILD target as the bazel definition for a module in another package",
2046 })
2047}
2048
Spandan Das3131d672023-08-03 22:33:47 +00002049// If CommonAttributes.Dir is set, the bazel target should be created in that dir
2050func TestCreateBazelTargetInDifferentDir(t *testing.T) {
2051 t.Parallel()
2052 bp := `
2053 custom {
2054 name: "foo",
2055 dir: "subdir",
2056 }
2057 `
2058 registerCustomModule := func(ctx android.RegistrationContext) {
2059 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2060 }
2061 // Check that foo is not created in root dir
2062 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2063 Description: "foo is not created in root dir because it sets dir explicitly",
2064 Blueprint: bp,
2065 Filesystem: map[string]string{
2066 "subdir/Android.bp": "",
2067 },
2068 ExpectedBazelTargets: []string{},
2069 })
2070 // Check that foo is created in `subdir`
2071 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2072 Description: "foo is created in `subdir` because it sets dir explicitly",
2073 Blueprint: bp,
2074 Filesystem: map[string]string{
2075 "subdir/Android.bp": "",
2076 },
2077 Dir: "subdir",
2078 ExpectedBazelTargets: []string{
2079 MakeBazelTarget("custom", "foo", AttrNameToString{}),
2080 },
2081 })
2082 // Check that we cannot create target in different dir if it is does not an Android.bp
2083 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2084 Description: "foo cannot be created in `subdir` because it does not contain an Android.bp file",
2085 Blueprint: bp,
2086 Dir: "subdir",
2087 ExpectedErr: fmt.Errorf("Cannot use ca.Dir to create a BazelTarget in dir: subdir since it does not contain an Android.bp file"),
2088 })
2089
2090}
Chris Parsons5f1b3c72023-09-28 20:41:03 +00002091
2092func TestBp2buildDepsMutator_missingTransitiveDep(t *testing.T) {
2093 bp := `
2094 custom {
2095 name: "foo",
2096 }
2097
2098 custom {
2099 name: "has_deps",
2100 arch_paths: [":has_missing_dep", ":foo"],
2101 }
2102
2103 custom {
2104 name: "has_missing_dep",
2105 arch_paths: [":missing"],
2106 }
2107 `
2108 expectedBazelTargets := []string{
2109 MakeBazelTarget(
2110 "custom",
2111 "foo",
2112 AttrNameToString{},
2113 ),
2114 }
2115 registerCustomModule := func(ctx android.RegistrationContext) {
2116 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2117 }
2118 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2119 Blueprint: bp,
2120 ExpectedBazelTargets: expectedBazelTargets,
2121 Description: "Skipping conversion of a target with missing transitive dep",
2122 DepsMutator: true,
2123 })
2124}
2125
2126func TestBp2buildDepsMutator_missingDirectDep(t *testing.T) {
2127 bp := `
2128 custom {
2129 name: "foo",
2130 arch_paths: [":exists"],
2131 }
2132 custom {
2133 name: "exists",
2134 }
2135
2136 custom {
2137 name: "has_missing_dep",
2138 arch_paths: [":missing"],
2139 }
2140 `
2141 expectedBazelTargets := []string{
2142 MakeBazelTarget(
2143 "custom",
2144 "foo",
2145 AttrNameToString{"arch_paths": `[":exists"]`},
2146 ),
2147 MakeBazelTarget(
2148 "custom",
2149 "exists",
2150 AttrNameToString{},
2151 ),
2152 }
2153 registerCustomModule := func(ctx android.RegistrationContext) {
2154 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2155 }
2156 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2157 Blueprint: bp,
2158 ExpectedBazelTargets: expectedBazelTargets,
2159 Description: "Skipping conversion of a target with missing direct dep",
2160 DepsMutator: true,
2161 })
2162}
2163
2164func TestBp2buildDepsMutator_unconvertedDirectDep(t *testing.T) {
2165 bp := `
2166 custom {
2167 name: "has_unconverted_dep",
2168 arch_paths: [":unconvertible"],
2169 }
2170
2171 custom {
2172 name: "unconvertible",
2173 does_not_convert_to_bazel: true
2174 }
2175 `
2176 registerCustomModule := func(ctx android.RegistrationContext) {
2177 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2178 }
2179 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2180 Blueprint: bp,
2181 ExpectedBazelTargets: []string{},
2182 Description: "Skipping conversion of a target with unconverted direct dep",
2183 DepsMutator: true,
2184 })
2185}
2186
2187func TestBp2buildDepsMutator_unconvertedTransitiveDep(t *testing.T) {
2188 bp := `
2189 custom {
2190 name: "foo",
2191 arch_paths: [":has_unconverted_dep", ":bar"],
2192 }
2193
2194 custom {
2195 name: "bar",
2196 }
2197
2198 custom {
2199 name: "has_unconverted_dep",
2200 arch_paths: [":unconvertible"],
2201 }
2202
2203 custom {
2204 name: "unconvertible",
2205 does_not_convert_to_bazel: true
2206 }
2207 `
2208 expectedBazelTargets := []string{
2209 MakeBazelTarget(
2210 "custom",
2211 "bar",
2212 AttrNameToString{},
2213 ),
2214 }
2215 registerCustomModule := func(ctx android.RegistrationContext) {
2216 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2217 }
2218 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2219 Blueprint: bp,
2220 ExpectedBazelTargets: expectedBazelTargets,
2221 Description: "Skipping conversion of a target with unconverted transitive dep",
2222 DepsMutator: true,
2223 })
2224}
2225
2226func TestBp2buildDepsMutator_alreadyExistsBuildDeps(t *testing.T) {
2227 bp := `
2228 custom {
2229 name: "foo",
2230 arch_paths: [":bar"],
2231 }
2232 custom {
2233 name: "bar",
2234 arch_paths: [":checked_in"],
2235 }
2236 custom {
2237 name: "checked_in",
2238 arch_paths: [":checked_in"],
2239 does_not_convert_to_bazel: true
2240 }
2241 `
2242 expectedBazelTargets := []string{
2243 MakeBazelTarget(
2244 "custom",
2245 "foo",
2246 AttrNameToString{"arch_paths": `[":bar"]`},
2247 ),
2248 MakeBazelTarget(
2249 "custom",
2250 "bar",
2251 AttrNameToString{"arch_paths": `[":checked_in"]`},
2252 ),
2253 }
2254 registerCustomModule := func(ctx android.RegistrationContext) {
2255 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2256 }
2257 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2258 StubbedBuildDefinitions: []string{"checked_in"},
2259 Blueprint: bp,
2260 ExpectedBazelTargets: expectedBazelTargets,
2261 Description: "Convert target with already-existing build dep",
2262 DepsMutator: true,
2263 })
2264}
2265
2266// Tests that deps of libc are always considered valid for libc. This circumvents
2267// an issue that, in a variantless graph (such as bp2build's), libc has the
2268// unique predicament that it depends on itself.
2269func TestBp2buildDepsMutator_depOnLibc(t *testing.T) {
2270 bp := `
2271 custom {
2272 name: "foo",
2273 arch_paths: [":libc"],
2274 }
2275 custom {
2276 name: "libc",
2277 arch_paths: [":libc_dep"],
2278 }
2279 custom {
2280 name: "libc_dep",
2281 does_not_convert_to_bazel: true
2282 }
2283 `
2284 expectedBazelTargets := []string{
2285 MakeBazelTarget(
2286 "custom",
2287 "foo",
2288 AttrNameToString{"arch_paths": `[":libc"]`},
2289 ),
2290 MakeBazelTarget(
2291 "custom",
2292 "libc",
2293 AttrNameToString{"arch_paths": `[":libc_dep"]`},
2294 ),
2295 }
2296 registerCustomModule := func(ctx android.RegistrationContext) {
2297 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2298 }
2299 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2300 StubbedBuildDefinitions: []string{"checked_in"},
2301 Blueprint: bp,
2302 ExpectedBazelTargets: expectedBazelTargets,
2303 Description: "Convert target with dep on libc",
2304 DepsMutator: true,
2305 })
2306}