blob: f809bcc07a6e2413f3b82069469eed2d97ef6901 [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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000273 "//build/bazel_common_rules/platforms/arch:arm": "ARM",
274 "//build/bazel_common_rules/platforms/arch:arm64": "ARM64",
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000275 "//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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000385 "//build/bazel_common_rules/platforms/arch:arm": [
Liz Kammerfdd72e62021-10-11 15:41:03 -0400386 "arm.txt",
387 "lib32.txt",
388 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000389 "//build/bazel_common_rules/platforms/arch:arm64": [
Liz Kammerfdd72e62021-10-11 15:41:03 -0400390 "arm64.txt",
391 "lib64.txt",
392 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000393 "//build/bazel_common_rules/platforms/arch:riscv64": [
Colin Crossf05b0d32022-07-14 18:10:34 -0700394 "riscv64.txt",
395 "lib64.txt",
396 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000397 "//build/bazel_common_rules/platforms/arch:x86": [
Liz Kammerfdd72e62021-10-11 15:41:03 -0400398 "x86.txt",
399 "lib32.txt",
400 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000401 "//build/bazel_common_rules/platforms/arch:x86_64": [
Liz Kammerfdd72e62021-10-11 15:41:03 -0400402 "x86_64.txt",
403 "lib64.txt",
404 ],
405 "//conditions:default": [],
406 }) + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000407 "//build/bazel_common_rules/platforms/os:android": [
Liz Kammerfdd72e62021-10-11 15:41:03 -0400408 "linux.txt",
409 "bionic.txt",
410 "android.txt",
411 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000412 "//build/bazel_common_rules/platforms/os:darwin": [
Liz Kammerfdd72e62021-10-11 15:41:03 -0400413 "host.txt",
414 "darwin.txt",
415 "not_windows.txt",
416 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000417 "//build/bazel_common_rules/platforms/os:linux_bionic": [
Liz Kammerfdd72e62021-10-11 15:41:03 -0400418 "host.txt",
419 "linux.txt",
420 "bionic.txt",
421 "linux_bionic.txt",
422 "not_windows.txt",
423 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000424 "//build/bazel_common_rules/platforms/os:linux_glibc": [
Colin Cross133782e2022-12-20 15:29:31 -0800425 "host.txt",
426 "linux.txt",
427 "glibc.txt",
428 "linux_glibc.txt",
429 "not_windows.txt",
430 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000431 "//build/bazel_common_rules/platforms/os:linux_musl": [
Liz Kammerfdd72e62021-10-11 15:41:03 -0400432 "host.txt",
433 "linux.txt",
434 "musl.txt",
435 "linux_musl.txt",
436 "not_windows.txt",
437 ],
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000438 "//build/bazel_common_rules/platforms/os:windows": [
Liz Kammerfdd72e62021-10-11 15:41:03 -0400439 "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({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000470 "//build/bazel_common_rules/platforms/arch:x86": [":dep"],
Liz Kammer4562a3b2021-04-21 18:15:34 -0400471 "//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 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001061 }
1062
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001063 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001064 t.Run(testCase.Description, func(t *testing.T) {
1065 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
Liz Kammer6eff3232021-08-26 08:37:59 -04001066 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001067 }
1068}
Jingwen Chen041b1842021-02-01 00:23:25 -05001069
Jingwen Chen12b4c272021-03-10 02:05:59 -05001070func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001071 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001072 moduleTypeUnderTest string
1073 moduleTypeUnderTestFactory android.ModuleFactory
1074 bp string
1075 expectedCount int
1076 description string
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001077 }{
1078 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001079 description: "explicitly unavailable",
1080 moduleTypeUnderTest: "filegroup",
1081 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001082 bp: `filegroup {
1083 name: "foo",
1084 srcs: ["a", "b"],
1085 bazel_module: { bp2build_available: false },
1086}`,
1087 expectedCount: 0,
1088 },
1089 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001090 description: "implicitly unavailable",
1091 moduleTypeUnderTest: "filegroup",
1092 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001093 bp: `filegroup {
1094 name: "foo",
1095 srcs: ["a", "b"],
1096}`,
1097 expectedCount: 0,
1098 },
1099 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001100 description: "explicitly available",
1101 moduleTypeUnderTest: "filegroup",
1102 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001103 bp: `filegroup {
1104 name: "foo",
1105 srcs: ["a", "b"],
1106 bazel_module: { bp2build_available: true },
1107}`,
1108 expectedCount: 1,
1109 },
1110 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001111 description: "generates more than 1 target if needed",
1112 moduleTypeUnderTest: "custom",
Liz Kammerdfeb1202022-05-13 17:20:20 -04001113 moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001114 bp: `custom {
1115 name: "foo",
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001116 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001117 bazel_module: { bp2build_available: true },
1118}`,
1119 expectedCount: 3,
1120 },
1121 }
1122
1123 dir := "."
1124 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -04001125 t.Run(testCase.description, func(t *testing.T) {
1126 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1127 ctx := android.NewTestContext(config)
1128 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001129 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001130
Liz Kammer2ada09a2021-08-11 00:17:36 -04001131 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1132 android.FailIfErrored(t, errs)
1133 _, errs = ctx.ResolveDependencies(config)
1134 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001135
Cole Faustb85d1a12022-11-08 18:14:01 -08001136 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammer6eff3232021-08-26 08:37:59 -04001137 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1138 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001139 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1140 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1141 }
1142 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001143 }
1144}
Liz Kammerba3ea162021-02-17 13:22:03 -05001145
Jingwen Chen12b4c272021-03-10 02:05:59 -05001146func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1147 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001148 moduleTypeUnderTest string
1149 moduleTypeUnderTestFactory android.ModuleFactory
1150 expectedCount map[string]int
1151 description string
Sam Delmerico24c56032022-03-28 19:53:03 +00001152 bp2buildConfig allowlists.Bp2BuildConfig
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001153 checkDir string
1154 fs map[string]string
MarkDacek9c094ca2023-03-16 19:15:19 +00001155 forceEnabledModules []string
1156 expectedErrorMessages []string
Jingwen Chen12b4c272021-03-10 02:05:59 -05001157 }{
1158 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001159 description: "test bp2build config package and subpackages config",
1160 moduleTypeUnderTest: "filegroup",
1161 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001162 expectedCount: map[string]int{
1163 "migrated": 1,
1164 "migrated/but_not_really": 0,
1165 "migrated/but_not_really/but_really": 1,
1166 "not_migrated": 0,
1167 "also_not_migrated": 0,
1168 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001169 bp2buildConfig: allowlists.Bp2BuildConfig{
1170 "migrated": allowlists.Bp2BuildDefaultTrueRecursively,
1171 "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
1172 "not_migrated": allowlists.Bp2BuildDefaultFalse,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001173 },
1174 fs: map[string]string{
1175 "migrated/Android.bp": `filegroup { name: "a" }`,
1176 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1177 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1178 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1179 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1180 },
1181 },
1182 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001183 description: "test bp2build config opt-in and opt-out",
1184 moduleTypeUnderTest: "filegroup",
1185 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001186 expectedCount: map[string]int{
1187 "package-opt-in": 2,
1188 "package-opt-in/subpackage": 0,
1189 "package-opt-out": 1,
1190 "package-opt-out/subpackage": 0,
1191 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001192 bp2buildConfig: allowlists.Bp2BuildConfig{
1193 "package-opt-in": allowlists.Bp2BuildDefaultFalse,
1194 "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001195 },
1196 fs: map[string]string{
1197 "package-opt-in/Android.bp": `
1198filegroup { name: "opt-in-a" }
1199filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1200filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1201`,
1202
1203 "package-opt-in/subpackage/Android.bp": `
1204filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1205`,
1206
1207 "package-opt-out/Android.bp": `
1208filegroup { name: "opt-out-a" }
1209filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1210filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1211`,
1212
1213 "package-opt-out/subpackage/Android.bp": `
1214filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1215filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1216`,
1217 },
1218 },
MarkDacek9c094ca2023-03-16 19:15:19 +00001219 {
1220 description: "test force-enabled errors out",
1221 moduleTypeUnderTest: "filegroup",
1222 moduleTypeUnderTestFactory: android.FileGroupFactory,
1223 expectedCount: map[string]int{
1224 "migrated": 0,
1225 "not_migrated": 0,
1226 },
1227 bp2buildConfig: allowlists.Bp2BuildConfig{
1228 "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
1229 "not_migrated": allowlists.Bp2BuildDefaultFalse,
1230 },
1231 fs: map[string]string{
1232 "migrated/Android.bp": `filegroup { name: "a" }`,
1233 },
1234 forceEnabledModules: []string{"a"},
1235 expectedErrorMessages: []string{"Force Enabled Module a not converted"},
1236 },
Jingwen Chen12b4c272021-03-10 02:05:59 -05001237 }
1238
1239 dir := "."
1240 for _, testCase := range testCases {
1241 fs := make(map[string][]byte)
1242 toParse := []string{
1243 "Android.bp",
1244 }
1245 for f, content := range testCase.fs {
1246 if strings.HasSuffix(f, "Android.bp") {
1247 toParse = append(toParse, f)
1248 }
1249 fs[f] = []byte(content)
1250 }
1251 config := android.TestConfig(buildDir, nil, "", fs)
MarkDacek9c094ca2023-03-16 19:15:19 +00001252 config.AddForceEnabledModules(testCase.forceEnabledModules)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001253 ctx := android.NewTestContext(config)
1254 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Sam Delmerico24c56032022-03-28 19:53:03 +00001255 allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig)
1256 ctx.RegisterBp2BuildConfig(allowlist)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001257 ctx.RegisterForBazelConversion()
1258
1259 _, errs := ctx.ParseFileList(dir, toParse)
1260 android.FailIfErrored(t, errs)
1261 _, errs = ctx.ResolveDependencies(config)
1262 android.FailIfErrored(t, errs)
1263
Cole Faustb85d1a12022-11-08 18:14:01 -08001264 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Jingwen Chen12b4c272021-03-10 02:05:59 -05001265
1266 // For each directory, test that the expected number of generated targets is correct.
1267 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -04001268 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
MarkDacek9c094ca2023-03-16 19:15:19 +00001269 android.CheckErrorsAgainstExpectations(t, err, testCase.expectedErrorMessages)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001270 if actualCount := len(bazelTargets); actualCount != expectedCount {
1271 t.Fatalf(
1272 "%s: Expected %d bazel target for %s package, got %d",
1273 testCase.description,
1274 expectedCount,
1275 dir,
1276 actualCount)
1277 }
1278
1279 }
1280 }
1281}
1282
Liz Kammerba3ea162021-02-17 13:22:03 -05001283func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001284 testCases := []Bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -05001285 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001286 Description: "filegroup bazel_module.label",
1287 ModuleTypeUnderTest: "filegroup",
1288 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1289 Blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -05001290 name: "fg_foo",
1291 bazel_module: { label: "//other:fg_foo" },
1292}`,
Cole Faustea602c52022-08-31 14:48:26 -07001293 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001294 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001295 "other/BUILD.bazel": `// BUILD file`,
1296 },
1297 },
1298 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001299 Description: "multiple bazel_module.label same BUILD",
1300 ModuleTypeUnderTest: "filegroup",
1301 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1302 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001303 name: "fg_foo",
1304 bazel_module: { label: "//other:fg_foo" },
1305 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001306
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001307 filegroup {
1308 name: "foo",
1309 bazel_module: { label: "//other:foo" },
1310 }`,
Cole Faustea602c52022-08-31 14:48:26 -07001311 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001312 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001313 "other/BUILD.bazel": `// BUILD file`,
1314 },
1315 },
1316 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001317 Description: "filegroup bazel_module.label and bp2build in subdir",
1318 ModuleTypeUnderTest: "filegroup",
1319 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1320 Dir: "other",
1321 Blueprint: ``,
1322 Filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001323 "other/Android.bp": `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001324 name: "fg_foo",
1325 bazel_module: {
1326 bp2build_available: true,
1327 },
1328 }
1329 filegroup {
1330 name: "fg_bar",
1331 bazel_module: {
1332 label: "//other:fg_bar"
1333 },
1334 }`,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001335 "other/BUILD.bazel": `// definition for fg_bar`,
1336 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001337 ExpectedBazelTargets: []string{
1338 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001339 },
1340 },
1341 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001342 Description: "filegroup bazel_module.label and filegroup bp2build",
1343 ModuleTypeUnderTest: "filegroup",
1344 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001345
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001346 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001347 "other/BUILD.bazel": `// BUILD file`,
1348 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001349 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001350 name: "fg_foo",
1351 bazel_module: {
1352 label: "//other:fg_foo",
1353 },
1354 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001355
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001356 filegroup {
1357 name: "fg_bar",
1358 bazel_module: {
1359 bp2build_available: true,
1360 },
1361 }`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001362 ExpectedBazelTargets: []string{
1363 MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001364 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001365 },
1366 }
1367
1368 dir := "."
1369 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001370 t.Run(testCase.Description, func(t *testing.T) {
Jingwen Chen49109762021-05-25 05:16:48 +00001371 fs := make(map[string][]byte)
1372 toParse := []string{
1373 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001374 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001375 for f, content := range testCase.Filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001376 if strings.HasSuffix(f, "Android.bp") {
1377 toParse = append(toParse, f)
1378 }
1379 fs[f] = []byte(content)
1380 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001381 config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001382 ctx := android.NewTestContext(config)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001383 ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001384 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001385
Jingwen Chen49109762021-05-25 05:16:48 +00001386 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001387 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001388 return
1389 }
1390 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001391 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001392 return
1393 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001394
Jingwen Chen49109762021-05-25 05:16:48 +00001395 checkDir := dir
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001396 if testCase.Dir != "" {
1397 checkDir = testCase.Dir
Jingwen Chen49109762021-05-25 05:16:48 +00001398 }
Cole Faustb85d1a12022-11-08 18:14:01 -08001399 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammer6eff3232021-08-26 08:37:59 -04001400 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1401 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001402 bazelTargets.sort()
1403 actualCount := len(bazelTargets)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001404 expectedCount := len(testCase.ExpectedBazelTargets)
Jingwen Chen49109762021-05-25 05:16:48 +00001405 if actualCount != expectedCount {
1406 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1407 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001408 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001409 actualContent := target.content
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001410 expectedContent := testCase.ExpectedBazelTargets[i]
Jingwen Chen49109762021-05-25 05:16:48 +00001411 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001412 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001413 "Expected generated Bazel target to be '%s', got '%s'",
1414 expectedContent,
1415 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001416 )
1417 }
1418 }
Jingwen Chen49109762021-05-25 05:16:48 +00001419 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001420 }
1421}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001422
Jingwen Chen0eeaeb82022-09-21 10:27:42 +00001423func TestGlob(t *testing.T) {
1424 testCases := []Bp2buildTestCase{
1425 {
1426 Description: "filegroup with glob",
1427 ModuleTypeUnderTest: "filegroup",
1428 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1429 Blueprint: `filegroup {
1430 name: "fg_foo",
1431 srcs: ["**/*.txt"],
1432 bazel_module: { bp2build_available: true },
1433}`,
1434 ExpectedBazelTargets: []string{
1435 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1436 "srcs": `[
1437 "other/a.txt",
1438 "other/b.txt",
1439 "other/subdir/a.txt",
1440 ]`,
1441 }),
1442 },
1443 Filesystem: map[string]string{
1444 "other/a.txt": "",
1445 "other/b.txt": "",
1446 "other/subdir/a.txt": "",
1447 "other/file": "",
1448 },
1449 },
1450 {
1451 Description: "filegroup with glob in subdir",
1452 ModuleTypeUnderTest: "filegroup",
1453 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1454 Dir: "other",
1455 Filesystem: map[string]string{
1456 "other/Android.bp": `filegroup {
1457 name: "fg_foo",
1458 srcs: ["**/*.txt"],
1459 bazel_module: { bp2build_available: true },
1460}`,
1461 "other/a.txt": "",
1462 "other/b.txt": "",
1463 "other/subdir/a.txt": "",
1464 "other/file": "",
1465 },
1466 ExpectedBazelTargets: []string{
1467 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1468 "srcs": `[
1469 "a.txt",
1470 "b.txt",
1471 "subdir/a.txt",
1472 ]`,
1473 }),
1474 },
1475 },
1476 {
1477 Description: "filegroup with glob with no kept BUILD files",
1478 ModuleTypeUnderTest: "filegroup",
1479 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1480 KeepBuildFileForDirs: []string{
1481 // empty
1482 },
1483 Blueprint: `filegroup {
1484 name: "fg_foo",
1485 srcs: ["**/*.txt"],
1486 bazel_module: { bp2build_available: true },
1487}`,
1488 Filesystem: map[string]string{
1489 "a.txt": "",
1490 "b.txt": "",
1491 "foo/BUILD": "",
1492 "foo/a.txt": "",
1493 "foo/bar/BUILD": "",
1494 "foo/bar/b.txt": "",
1495 },
1496 ExpectedBazelTargets: []string{
1497 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1498 "srcs": `[
1499 "a.txt",
1500 "b.txt",
1501 "foo/a.txt",
1502 "foo/bar/b.txt",
1503 ]`,
1504 }),
1505 },
1506 },
1507 {
1508 Description: "filegroup with glob with kept BUILD file",
1509 ModuleTypeUnderTest: "filegroup",
1510 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1511 KeepBuildFileForDirs: []string{
1512 "foo",
1513 },
1514 Blueprint: `filegroup {
1515 name: "fg_foo",
1516 srcs: ["**/*.txt"],
1517 bazel_module: { bp2build_available: true },
1518}`,
1519 Filesystem: map[string]string{
1520 "a.txt": "",
1521 "b.txt": "",
1522 "foo/BUILD": "",
1523 "foo/a.txt": "",
1524 "foo/bar/BUILD": "",
1525 "foo/bar/b.txt": "",
1526 },
1527 ExpectedBazelTargets: []string{
1528 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1529 "srcs": `[
1530 "a.txt",
1531 "b.txt",
1532 "//foo:a.txt",
1533 "//foo:bar/b.txt",
1534 ]`,
1535 }),
1536 },
1537 },
1538 {
1539 Description: "filegroup with glob with kept BUILD.bazel file",
1540 ModuleTypeUnderTest: "filegroup",
1541 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1542 KeepBuildFileForDirs: []string{
1543 "foo",
1544 },
1545 Blueprint: `filegroup {
1546 name: "fg_foo",
1547 srcs: ["**/*.txt"],
1548 bazel_module: { bp2build_available: true },
1549}`,
1550 Filesystem: map[string]string{
1551 "a.txt": "",
1552 "b.txt": "",
1553 "foo/BUILD.bazel": "",
1554 "foo/a.txt": "",
1555 "foo/bar/BUILD.bazel": "",
1556 "foo/bar/b.txt": "",
1557 },
1558 ExpectedBazelTargets: []string{
1559 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1560 "srcs": `[
1561 "a.txt",
1562 "b.txt",
1563 "//foo:a.txt",
1564 "//foo:bar/b.txt",
1565 ]`,
1566 }),
1567 },
1568 },
1569 {
1570 Description: "filegroup with glob with Android.bp file as boundary",
1571 ModuleTypeUnderTest: "filegroup",
1572 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1573 Blueprint: `filegroup {
1574 name: "fg_foo",
1575 srcs: ["**/*.txt"],
1576 bazel_module: { bp2build_available: true },
1577}`,
1578 Filesystem: map[string]string{
1579 "a.txt": "",
1580 "b.txt": "",
1581 "foo/Android.bp": "",
1582 "foo/a.txt": "",
1583 "foo/bar/Android.bp": "",
1584 "foo/bar/b.txt": "",
1585 },
1586 ExpectedBazelTargets: []string{
1587 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1588 "srcs": `[
1589 "a.txt",
1590 "b.txt",
1591 "//foo:a.txt",
1592 "//foo/bar:b.txt",
1593 ]`,
1594 }),
1595 },
1596 },
1597 {
1598 Description: "filegroup with glob in subdir with kept BUILD and BUILD.bazel file",
1599 ModuleTypeUnderTest: "filegroup",
1600 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1601 Dir: "other",
1602 KeepBuildFileForDirs: []string{
1603 "other/foo",
1604 "other/foo/bar",
1605 // deliberately not other/foo/baz/BUILD.
1606 },
1607 Filesystem: map[string]string{
1608 "other/Android.bp": `filegroup {
1609 name: "fg_foo",
1610 srcs: ["**/*.txt"],
1611 bazel_module: { bp2build_available: true },
1612}`,
1613 "other/a.txt": "",
1614 "other/b.txt": "",
1615 "other/foo/BUILD": "",
1616 "other/foo/a.txt": "",
1617 "other/foo/bar/BUILD.bazel": "",
1618 "other/foo/bar/b.txt": "",
1619 "other/foo/baz/BUILD": "",
1620 "other/foo/baz/c.txt": "",
1621 },
1622 ExpectedBazelTargets: []string{
1623 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1624 "srcs": `[
1625 "a.txt",
1626 "b.txt",
1627 "//other/foo:a.txt",
1628 "//other/foo/bar:b.txt",
1629 "//other/foo:baz/c.txt",
1630 ]`,
1631 }),
1632 },
1633 },
1634 }
1635
1636 for _, testCase := range testCases {
1637 t.Run(testCase.Description, func(t *testing.T) {
1638 RunBp2BuildTestCaseSimple(t, testCase)
1639 })
1640 }
1641}
1642
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001643func TestGlobExcludeSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001644 testCases := []Bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001645 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001646 Description: "filegroup top level exclude_srcs",
1647 ModuleTypeUnderTest: "filegroup",
1648 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1649 Blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001650 name: "fg_foo",
1651 srcs: ["**/*.txt"],
1652 exclude_srcs: ["c.txt"],
1653 bazel_module: { bp2build_available: true },
1654}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001655 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001656 "a.txt": "",
1657 "b.txt": "",
1658 "c.txt": "",
1659 "dir/Android.bp": "",
1660 "dir/e.txt": "",
1661 "dir/f.txt": "",
1662 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001663 ExpectedBazelTargets: []string{
1664 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001665 "srcs": `[
1666 "a.txt",
1667 "b.txt",
1668 "//dir:e.txt",
1669 "//dir:f.txt",
1670 ]`,
1671 }),
1672 },
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001673 },
1674 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001675 Description: "filegroup in subdir exclude_srcs",
1676 ModuleTypeUnderTest: "filegroup",
1677 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1678 Blueprint: "",
1679 Dir: "dir",
1680 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001681 "dir/Android.bp": `filegroup {
1682 name: "fg_foo",
1683 srcs: ["**/*.txt"],
1684 exclude_srcs: ["b.txt"],
1685 bazel_module: { bp2build_available: true },
1686}
1687`,
1688 "dir/a.txt": "",
1689 "dir/b.txt": "",
1690 "dir/subdir/Android.bp": "",
1691 "dir/subdir/e.txt": "",
1692 "dir/subdir/f.txt": "",
1693 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001694 ExpectedBazelTargets: []string{
1695 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001696 "srcs": `[
Liz Kammer9abd62d2021-05-21 08:37:59 -04001697 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001698 "//dir/subdir:e.txt",
1699 "//dir/subdir:f.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001700 ]`,
1701 }),
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001702 },
1703 },
1704 }
1705
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001706 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001707 t.Run(testCase.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001708 RunBp2BuildTestCaseSimple(t, testCase)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001709 })
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001710 }
1711}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001712
1713func TestCommonBp2BuildModuleAttrs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001714 testCases := []Bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001715 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001716 Description: "Required into data test",
1717 ModuleTypeUnderTest: "filegroup",
1718 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001719 StubbedBuildDefinitions: []string{"reqd"},
1720 Blueprint: simpleModule("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001721filegroup {
1722 name: "fg_foo",
1723 required: ["reqd"],
1724 bazel_module: { bp2build_available: true },
1725}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001726 ExpectedBazelTargets: []string{
1727 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001728 "data": `[":reqd"]`,
1729 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001730 },
1731 },
1732 {
Jingwen Chena5ecb372022-09-21 09:05:37 +00001733 Description: "Required into data test, cyclic self reference is filtered out",
1734 ModuleTypeUnderTest: "filegroup",
1735 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001736 StubbedBuildDefinitions: []string{"reqd"},
1737 Blueprint: simpleModule("filegroup", "reqd") + `
Jingwen Chena5ecb372022-09-21 09:05:37 +00001738filegroup {
1739 name: "fg_foo",
1740 required: ["reqd", "fg_foo"],
1741 bazel_module: { bp2build_available: true },
1742}`,
1743 ExpectedBazelTargets: []string{
1744 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1745 "data": `[":reqd"]`,
1746 }),
1747 },
1748 },
1749 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001750 Description: "Required via arch into data test",
1751 ModuleTypeUnderTest: "python_library",
1752 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001753 StubbedBuildDefinitions: []string{"reqdx86", "reqdarm"},
1754 Blueprint: simpleModule("python_library", "reqdx86") +
1755 simpleModule("python_library", "reqdarm") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001756python_library {
1757 name: "fg_foo",
1758 arch: {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001759 arm: {
1760 required: ["reqdarm"],
1761 },
1762 x86: {
1763 required: ["reqdx86"],
1764 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001765 },
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": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +00001771 "//build/bazel_common_rules/platforms/arch:arm": [":reqdarm"],
1772 "//build/bazel_common_rules/platforms/arch:x86": [":reqdx86"],
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001773 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001774 })`,
1775 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001776 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001777 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001778 },
1779 },
1780 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001781 Description: "Required appended to data test",
1782 ModuleTypeUnderTest: "python_library",
1783 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1784 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001785 "data.bin": "",
1786 "src.py": "",
1787 },
Chris Parsonscd209032023-09-19 01:12:48 +00001788 StubbedBuildDefinitions: []string{"reqd"},
1789 Blueprint: simpleModule("python_library", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001790python_library {
1791 name: "fg_foo",
1792 data: ["data.bin"],
1793 required: ["reqd"],
1794 bazel_module: { bp2build_available: true },
1795}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001796 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001797 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001798 "data": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001799 "data.bin",
1800 ":reqd",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001801 ]`,
1802 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001803 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001804 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001805 },
1806 },
1807 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001808 Description: "All props-to-attrs at once together test",
1809 ModuleTypeUnderTest: "filegroup",
1810 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001811 StubbedBuildDefinitions: []string{"reqd"},
1812 Blueprint: simpleModule("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001813filegroup {
1814 name: "fg_foo",
1815 required: ["reqd"],
1816 bazel_module: { bp2build_available: true },
1817}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001818 ExpectedBazelTargets: []string{
1819 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001820 "data": `[":reqd"]`,
1821 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001822 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001823 },
1824 }
1825
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001826 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001827 t.Run(tc.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001828 RunBp2BuildTestCaseSimple(t, tc)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001829 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001830 }
1831}
Sasha Smundak05b0ba62022-09-26 18:15:45 -07001832
1833func TestLicensesAttrConversion(t *testing.T) {
1834 RunBp2BuildTestCase(t,
1835 func(ctx android.RegistrationContext) {
1836 ctx.RegisterModuleType("license", android.LicenseFactory)
1837 },
1838 Bp2buildTestCase{
1839 Description: "Test that licenses: attribute is converted",
1840 ModuleTypeUnderTest: "filegroup",
1841 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1842 Blueprint: `
1843license {
1844 name: "my_license",
1845}
1846filegroup {
1847 name: "my_filegroup",
1848 licenses: ["my_license"],
1849}
1850`,
1851 ExpectedBazelTargets: []string{
1852 MakeBazelTargetNoRestrictions("filegroup", "my_filegroup", AttrNameToString{
1853 "applicable_licenses": `[":my_license"]`,
1854 }),
1855 MakeBazelTargetNoRestrictions("android_license", "my_license", AttrNameToString{}),
1856 },
1857 })
1858}
Spandan Das5af0bd32022-09-28 20:43:08 +00001859
Spandan Das6a448ec2023-04-19 17:36:12 +00001860func TestGenerateConfigSetting(t *testing.T) {
1861 bp := `
1862 custom {
1863 name: "foo",
1864 test_config_setting: true,
1865 }
1866 `
1867 expectedBazelTargets := []string{
1868 MakeBazelTargetNoRestrictions(
1869 "config_setting",
1870 "foo_config_setting",
1871 AttrNameToString{
1872 "flag_values": `{
1873 "//build/bazel/rules/my_string_setting": "foo",
1874 }`,
1875 },
1876 ),
1877 MakeBazelTarget(
1878 "custom",
1879 "foo",
1880 AttrNameToString{},
1881 ),
1882 }
1883 registerCustomModule := func(ctx android.RegistrationContext) {
1884 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
1885 }
1886 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
1887 Blueprint: bp,
1888 ExpectedBazelTargets: expectedBazelTargets,
1889 Description: "Generating API contribution Bazel targets for custom module",
1890 })
1891}
Spandan Das921af322023-04-26 02:56:37 +00001892
1893// If values of all keys in an axis are equal to //conditions:default, drop the axis and print the common value
1894func TestPrettyPrintSelectMapEqualValues(t *testing.T) {
1895 lla := bazel.LabelListAttribute{
1896 Value: bazel.LabelList{},
1897 }
1898 libFooImplLabel := bazel.Label{
1899 Label: ":libfoo.impl",
1900 }
Spandan Das6d4d9da2023-04-18 06:20:40 +00001901 lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidPlatform, bazel.MakeLabelList([]bazel.Label{libFooImplLabel}))
Spandan Das921af322023-04-26 02:56:37 +00001902 lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.MakeLabelList([]bazel.Label{libFooImplLabel}))
1903 actual, _ := prettyPrintAttribute(lla, 0)
1904 android.AssertStringEquals(t, "Print the common value if all keys in an axis have the same value", `[":libfoo.impl"]`, actual)
1905}
Spandan Das3131d672023-08-03 22:33:47 +00001906
Chris Parsons5011e612023-09-13 23:33:20 +00001907func TestAlreadyPresentBuildTarget(t *testing.T) {
1908 bp := `
1909 custom {
1910 name: "foo",
1911 }
1912 custom {
1913 name: "bar",
1914 }
1915 `
1916 alreadyPresentBuildFile :=
1917 MakeBazelTarget(
1918 "custom",
1919 "foo",
1920 AttrNameToString{},
1921 )
1922 expectedBazelTargets := []string{
1923 MakeBazelTarget(
1924 "custom",
1925 "bar",
1926 AttrNameToString{},
1927 ),
1928 }
1929 registerCustomModule := func(ctx android.RegistrationContext) {
1930 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
1931 }
1932 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
1933 AlreadyExistingBuildContents: alreadyPresentBuildFile,
1934 Blueprint: bp,
1935 ExpectedBazelTargets: expectedBazelTargets,
1936 Description: "Not duplicating work for an already-present BUILD target.",
1937 })
1938}
1939
Chris Parsons0c4de1f2023-09-21 20:36:35 +00001940func TestAlreadyPresentOneToManyBuildTarget(t *testing.T) {
1941 bp := `
1942 custom {
1943 name: "foo",
1944 one_to_many_prop: true,
1945 }
1946 custom {
1947 name: "bar",
1948 }
1949 `
1950 alreadyPresentBuildFile :=
1951 MakeBazelTarget(
1952 "custom",
1953 // one_to_many_prop ensures that foo generates "foo_proto_library_deps".
1954 "foo_proto_library_deps",
1955 AttrNameToString{},
1956 )
1957 expectedBazelTargets := []string{
1958 MakeBazelTarget(
1959 "custom",
1960 "bar",
1961 AttrNameToString{},
1962 ),
1963 }
1964 registerCustomModule := func(ctx android.RegistrationContext) {
1965 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
1966 }
1967 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
1968 AlreadyExistingBuildContents: alreadyPresentBuildFile,
1969 Blueprint: bp,
1970 ExpectedBazelTargets: expectedBazelTargets,
1971 Description: "Not duplicating work for an already-present BUILD target (different generated name)",
1972 })
1973}
1974
Chris Parsons5011e612023-09-13 23:33:20 +00001975// Verifies that if a module is defined in pkg1/Android.bp, that a target present
1976// in pkg2/BUILD.bazel does not result in the module being labeled "already defined
1977// in a BUILD file".
1978func TestBuildTargetPresentOtherDirectory(t *testing.T) {
1979 bp := `
1980 custom {
1981 name: "foo",
1982 }
1983 `
1984 expectedBazelTargets := []string{
1985 MakeBazelTarget(
1986 "custom",
1987 "foo",
1988 AttrNameToString{},
1989 ),
1990 }
1991 registerCustomModule := func(ctx android.RegistrationContext) {
1992 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
1993 }
1994 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
1995 KeepBuildFileForDirs: []string{"other_pkg"},
1996 Filesystem: map[string]string{
1997 "other_pkg/BUILD.bazel": MakeBazelTarget("custom", "foo", AttrNameToString{}),
1998 },
1999 Blueprint: bp,
2000 ExpectedBazelTargets: expectedBazelTargets,
2001 Description: "Not treating a BUILD target as the bazel definition for a module in another package",
2002 })
2003}
2004
Spandan Das3131d672023-08-03 22:33:47 +00002005// If CommonAttributes.Dir is set, the bazel target should be created in that dir
2006func TestCreateBazelTargetInDifferentDir(t *testing.T) {
2007 t.Parallel()
2008 bp := `
2009 custom {
2010 name: "foo",
2011 dir: "subdir",
2012 }
2013 `
2014 registerCustomModule := func(ctx android.RegistrationContext) {
2015 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2016 }
2017 // Check that foo is not created in root dir
2018 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2019 Description: "foo is not created in root dir because it sets dir explicitly",
2020 Blueprint: bp,
2021 Filesystem: map[string]string{
2022 "subdir/Android.bp": "",
2023 },
2024 ExpectedBazelTargets: []string{},
2025 })
2026 // Check that foo is created in `subdir`
2027 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2028 Description: "foo is created in `subdir` because it sets dir explicitly",
2029 Blueprint: bp,
2030 Filesystem: map[string]string{
2031 "subdir/Android.bp": "",
2032 },
2033 Dir: "subdir",
2034 ExpectedBazelTargets: []string{
2035 MakeBazelTarget("custom", "foo", AttrNameToString{}),
2036 },
2037 })
2038 // Check that we cannot create target in different dir if it is does not an Android.bp
2039 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2040 Description: "foo cannot be created in `subdir` because it does not contain an Android.bp file",
2041 Blueprint: bp,
2042 Dir: "subdir",
2043 ExpectedErr: fmt.Errorf("Cannot use ca.Dir to create a BazelTarget in dir: subdir since it does not contain an Android.bp file"),
2044 })
2045
2046}
Chris Parsons5f1b3c72023-09-28 20:41:03 +00002047
2048func TestBp2buildDepsMutator_missingTransitiveDep(t *testing.T) {
2049 bp := `
2050 custom {
2051 name: "foo",
2052 }
2053
2054 custom {
2055 name: "has_deps",
2056 arch_paths: [":has_missing_dep", ":foo"],
2057 }
2058
2059 custom {
2060 name: "has_missing_dep",
2061 arch_paths: [":missing"],
2062 }
2063 `
2064 expectedBazelTargets := []string{
2065 MakeBazelTarget(
2066 "custom",
2067 "foo",
2068 AttrNameToString{},
2069 ),
2070 }
2071 registerCustomModule := func(ctx android.RegistrationContext) {
2072 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2073 }
2074 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2075 Blueprint: bp,
2076 ExpectedBazelTargets: expectedBazelTargets,
2077 Description: "Skipping conversion of a target with missing transitive dep",
Chris Parsons5f1b3c72023-09-28 20:41:03 +00002078 })
2079}
2080
2081func TestBp2buildDepsMutator_missingDirectDep(t *testing.T) {
2082 bp := `
2083 custom {
2084 name: "foo",
2085 arch_paths: [":exists"],
2086 }
2087 custom {
2088 name: "exists",
2089 }
2090
2091 custom {
2092 name: "has_missing_dep",
2093 arch_paths: [":missing"],
2094 }
2095 `
2096 expectedBazelTargets := []string{
2097 MakeBazelTarget(
2098 "custom",
2099 "foo",
2100 AttrNameToString{"arch_paths": `[":exists"]`},
2101 ),
2102 MakeBazelTarget(
2103 "custom",
2104 "exists",
2105 AttrNameToString{},
2106 ),
2107 }
2108 registerCustomModule := func(ctx android.RegistrationContext) {
2109 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2110 }
2111 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2112 Blueprint: bp,
2113 ExpectedBazelTargets: expectedBazelTargets,
2114 Description: "Skipping conversion of a target with missing direct dep",
Chris Parsons5f1b3c72023-09-28 20:41:03 +00002115 })
2116}
2117
2118func TestBp2buildDepsMutator_unconvertedDirectDep(t *testing.T) {
2119 bp := `
2120 custom {
2121 name: "has_unconverted_dep",
2122 arch_paths: [":unconvertible"],
2123 }
2124
2125 custom {
2126 name: "unconvertible",
2127 does_not_convert_to_bazel: true
2128 }
2129 `
2130 registerCustomModule := func(ctx android.RegistrationContext) {
2131 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2132 }
2133 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2134 Blueprint: bp,
2135 ExpectedBazelTargets: []string{},
2136 Description: "Skipping conversion of a target with unconverted direct dep",
Chris Parsons5f1b3c72023-09-28 20:41:03 +00002137 })
2138}
2139
2140func TestBp2buildDepsMutator_unconvertedTransitiveDep(t *testing.T) {
2141 bp := `
2142 custom {
2143 name: "foo",
2144 arch_paths: [":has_unconverted_dep", ":bar"],
2145 }
2146
2147 custom {
2148 name: "bar",
2149 }
2150
2151 custom {
2152 name: "has_unconverted_dep",
2153 arch_paths: [":unconvertible"],
2154 }
2155
2156 custom {
2157 name: "unconvertible",
2158 does_not_convert_to_bazel: true
2159 }
2160 `
2161 expectedBazelTargets := []string{
2162 MakeBazelTarget(
2163 "custom",
2164 "bar",
2165 AttrNameToString{},
2166 ),
2167 }
2168 registerCustomModule := func(ctx android.RegistrationContext) {
2169 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2170 }
2171 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2172 Blueprint: bp,
2173 ExpectedBazelTargets: expectedBazelTargets,
2174 Description: "Skipping conversion of a target with unconverted transitive dep",
Chris Parsons5f1b3c72023-09-28 20:41:03 +00002175 })
2176}
2177
2178func TestBp2buildDepsMutator_alreadyExistsBuildDeps(t *testing.T) {
2179 bp := `
2180 custom {
2181 name: "foo",
2182 arch_paths: [":bar"],
2183 }
2184 custom {
2185 name: "bar",
2186 arch_paths: [":checked_in"],
2187 }
2188 custom {
2189 name: "checked_in",
2190 arch_paths: [":checked_in"],
2191 does_not_convert_to_bazel: true
2192 }
2193 `
2194 expectedBazelTargets := []string{
2195 MakeBazelTarget(
2196 "custom",
2197 "foo",
2198 AttrNameToString{"arch_paths": `[":bar"]`},
2199 ),
2200 MakeBazelTarget(
2201 "custom",
2202 "bar",
2203 AttrNameToString{"arch_paths": `[":checked_in"]`},
2204 ),
2205 }
2206 registerCustomModule := func(ctx android.RegistrationContext) {
2207 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2208 }
2209 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2210 StubbedBuildDefinitions: []string{"checked_in"},
2211 Blueprint: bp,
2212 ExpectedBazelTargets: expectedBazelTargets,
2213 Description: "Convert target with already-existing build dep",
Chris Parsons5f1b3c72023-09-28 20:41:03 +00002214 })
2215}
2216
2217// Tests that deps of libc are always considered valid for libc. This circumvents
2218// an issue that, in a variantless graph (such as bp2build's), libc has the
2219// unique predicament that it depends on itself.
2220func TestBp2buildDepsMutator_depOnLibc(t *testing.T) {
2221 bp := `
2222 custom {
2223 name: "foo",
2224 arch_paths: [":libc"],
2225 }
2226 custom {
2227 name: "libc",
2228 arch_paths: [":libc_dep"],
2229 }
2230 custom {
2231 name: "libc_dep",
2232 does_not_convert_to_bazel: true
2233 }
2234 `
2235 expectedBazelTargets := []string{
2236 MakeBazelTarget(
2237 "custom",
2238 "foo",
2239 AttrNameToString{"arch_paths": `[":libc"]`},
2240 ),
2241 MakeBazelTarget(
2242 "custom",
2243 "libc",
2244 AttrNameToString{"arch_paths": `[":libc_dep"]`},
2245 ),
2246 }
2247 registerCustomModule := func(ctx android.RegistrationContext) {
2248 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2249 }
2250 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2251 StubbedBuildDefinitions: []string{"checked_in"},
2252 Blueprint: bp,
2253 ExpectedBazelTargets: expectedBazelTargets,
2254 Description: "Convert target with dep on libc",
Chris Parsons5f1b3c72023-09-28 20:41:03 +00002255 })
2256}