blob: dc56a17b0e99180b60b834d88ab5ea00b058e07d [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: "non-existent dep",
353 Blueprint: `custom {
Liz Kammerdaa09ef2021-12-15 15:35:38 -0500354 name: "has_dep",
355 arch_paths: [":dep"],
356 bazel_module: { bp2build_available: true },
357}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000358 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000359 MakeBazelTarget("custom", "has_dep", AttrNameToString{
Liz Kammerdaa09ef2021-12-15 15:35:38 -0500360 "arch_paths": `[":dep__BP2BUILD__MISSING__DEP"]`,
361 }),
362 },
363 },
364 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000365 Description: "arch-variant srcs",
366 Blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400367 name: "arch_paths",
368 arch: {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400369 x86: { arch_paths: ["x86.txt"] },
370 x86_64: { arch_paths: ["x86_64.txt"] },
371 arm: { arch_paths: ["arm.txt"] },
372 arm64: { arch_paths: ["arm64.txt"] },
Colin Crossf05b0d32022-07-14 18:10:34 -0700373 riscv64: { arch_paths: ["riscv64.txt"] },
Liz Kammerfdd72e62021-10-11 15:41:03 -0400374 },
375 target: {
376 linux: { arch_paths: ["linux.txt"] },
377 bionic: { arch_paths: ["bionic.txt"] },
378 host: { arch_paths: ["host.txt"] },
379 not_windows: { arch_paths: ["not_windows.txt"] },
380 android: { arch_paths: ["android.txt"] },
381 linux_musl: { arch_paths: ["linux_musl.txt"] },
382 musl: { arch_paths: ["musl.txt"] },
383 linux_glibc: { arch_paths: ["linux_glibc.txt"] },
384 glibc: { arch_paths: ["glibc.txt"] },
385 linux_bionic: { arch_paths: ["linux_bionic.txt"] },
386 darwin: { arch_paths: ["darwin.txt"] },
387 windows: { arch_paths: ["windows.txt"] },
388 },
389 multilib: {
390 lib32: { arch_paths: ["lib32.txt"] },
391 lib64: { arch_paths: ["lib64.txt"] },
Liz Kammer4562a3b2021-04-21 18:15:34 -0400392 },
393 bazel_module: { bp2build_available: true },
394}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000395 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000396 MakeBazelTarget("custom", "arch_paths", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500397 "arch_paths": `select({
Liz Kammerfdd72e62021-10-11 15:41:03 -0400398 "//build/bazel/platforms/arch:arm": [
399 "arm.txt",
400 "lib32.txt",
401 ],
402 "//build/bazel/platforms/arch:arm64": [
403 "arm64.txt",
404 "lib64.txt",
405 ],
Colin Crossf05b0d32022-07-14 18:10:34 -0700406 "//build/bazel/platforms/arch:riscv64": [
407 "riscv64.txt",
408 "lib64.txt",
409 ],
Liz Kammerfdd72e62021-10-11 15:41:03 -0400410 "//build/bazel/platforms/arch:x86": [
411 "x86.txt",
412 "lib32.txt",
413 ],
414 "//build/bazel/platforms/arch:x86_64": [
415 "x86_64.txt",
416 "lib64.txt",
417 ],
418 "//conditions:default": [],
419 }) + select({
420 "//build/bazel/platforms/os:android": [
421 "linux.txt",
422 "bionic.txt",
423 "android.txt",
424 ],
425 "//build/bazel/platforms/os:darwin": [
426 "host.txt",
427 "darwin.txt",
428 "not_windows.txt",
429 ],
Liz Kammerfdd72e62021-10-11 15:41:03 -0400430 "//build/bazel/platforms/os:linux_bionic": [
431 "host.txt",
432 "linux.txt",
433 "bionic.txt",
434 "linux_bionic.txt",
435 "not_windows.txt",
436 ],
Colin Cross133782e2022-12-20 15:29:31 -0800437 "//build/bazel/platforms/os:linux_glibc": [
438 "host.txt",
439 "linux.txt",
440 "glibc.txt",
441 "linux_glibc.txt",
442 "not_windows.txt",
443 ],
Liz Kammerfdd72e62021-10-11 15:41:03 -0400444 "//build/bazel/platforms/os:linux_musl": [
445 "host.txt",
446 "linux.txt",
447 "musl.txt",
448 "linux_musl.txt",
449 "not_windows.txt",
450 ],
451 "//build/bazel/platforms/os:windows": [
452 "host.txt",
453 "windows.txt",
454 ],
Liz Kammer4562a3b2021-04-21 18:15:34 -0400455 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500456 })`,
457 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400458 },
459 },
460 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000461 Description: "arch-variant deps",
462 Blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400463 name: "has_dep",
464 arch: {
465 x86: {
466 arch_paths: [":dep"],
467 },
468 },
469 bazel_module: { bp2build_available: true },
470}
471
472custom {
473 name: "dep",
474 arch_paths: ["abc"],
475 bazel_module: { bp2build_available: true },
476}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000477 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000478 MakeBazelTarget("custom", "dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500479 "arch_paths": `["abc"]`,
480 }),
Alixe06d75b2022-08-31 18:28:19 +0000481 MakeBazelTarget("custom", "has_dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500482 "arch_paths": `select({
Liz Kammer4562a3b2021-04-21 18:15:34 -0400483 "//build/bazel/platforms/arch:x86": [":dep"],
484 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500485 })`,
486 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400487 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000488 },
Liz Kammer32a03392021-09-14 11:17:21 -0400489 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000490 Description: "embedded props",
491 Blueprint: `custom {
Liz Kammer32a03392021-09-14 11:17:21 -0400492 name: "embedded_props",
493 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", "embedded_props", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500498 "embedded_attr": `"abc"`,
499 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400500 },
501 },
502 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000503 Description: "ptr to embedded props",
504 Blueprint: `custom {
Liz Kammer32a03392021-09-14 11:17:21 -0400505 name: "ptr_to_embedded_props",
506 other_embedded_prop: "abc",
507 bazel_module: { bp2build_available: true },
508}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000509 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000510 MakeBazelTarget("custom", "ptr_to_embedded_props", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500511 "other_embedded_attr": `"abc"`,
512 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400513 },
514 },
Jingwen Chen73850672020-12-14 08:25:34 -0500515 }
516
517 dir := "."
518 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000519 t.Run(testCase.Description, func(t *testing.T) {
520 config := android.TestConfig(buildDir, nil, testCase.Blueprint, nil)
Liz Kammerfdd72e62021-10-11 15:41:03 -0400521 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500522
Liz Kammerfdd72e62021-10-11 15:41:03 -0400523 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500524
Liz Kammerfdd72e62021-10-11 15:41:03 -0400525 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
526 if errored(t, testCase, errs) {
527 return
528 }
529 _, errs = ctx.ResolveDependencies(config)
530 if errored(t, testCase, errs) {
531 return
532 }
Jingwen Chen73850672020-12-14 08:25:34 -0500533
Cole Faustb85d1a12022-11-08 18:14:01 -0800534 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammerfdd72e62021-10-11 15:41:03 -0400535 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
536 android.FailIfErrored(t, err)
Jingwen Chen164e0862021-02-19 00:48:40 -0500537
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000538 if actualCount, expectedCount := len(bazelTargets), len(testCase.ExpectedBazelTargets); actualCount != expectedCount {
539 t.Errorf("Expected %d bazel target (%s),\ngot %d (%s)", expectedCount, testCase.ExpectedBazelTargets, actualCount, bazelTargets)
Liz Kammerfdd72e62021-10-11 15:41:03 -0400540 } else {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000541 for i, expectedBazelTarget := range testCase.ExpectedBazelTargets {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400542 actualBazelTarget := bazelTargets[i]
543 if actualBazelTarget.content != expectedBazelTarget {
544 t.Errorf(
545 "Expected generated Bazel target to be '%s', got '%s'",
546 expectedBazelTarget,
547 actualBazelTarget.content,
548 )
549 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400550 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500551 }
Liz Kammerfdd72e62021-10-11 15:41:03 -0400552 })
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800553 }
554}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500555
Liz Kammerdfeb1202022-05-13 17:20:20 -0400556func TestBp2buildHostAndDevice(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000557 testCases := []Bp2buildTestCase{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400558 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000559 Description: "host and device, device only",
560 ModuleTypeUnderTest: "custom",
561 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
562 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400563 name: "foo",
564 bazel_module: { bp2build_available: true },
565}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000566 ExpectedBazelTargets: []string{
567 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400568 },
569 },
570 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000571 Description: "host and device, both",
572 ModuleTypeUnderTest: "custom",
573 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
574 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400575 name: "foo",
576 host_supported: true,
577 bazel_module: { bp2build_available: true },
578}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000579 ExpectedBazelTargets: []string{
580 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400581 },
582 },
583 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000584 Description: "host and device, host explicitly disabled",
585 ModuleTypeUnderTest: "custom",
586 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
587 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400588 name: "foo",
589 host_supported: false,
590 bazel_module: { bp2build_available: true },
591}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000592 ExpectedBazelTargets: []string{
593 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400594 },
595 },
596 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000597 Description: "host and device, neither",
598 ModuleTypeUnderTest: "custom",
599 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
600 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400601 name: "foo",
602 host_supported: false,
603 device_supported: false,
604 bazel_module: { bp2build_available: true },
605}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000606 ExpectedBazelTargets: []string{
607 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400608 "target_compatible_with": `["@platforms//:incompatible"]`,
609 }),
610 },
611 },
612 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000613 Description: "host and device, neither, cannot override with product_var",
614 ModuleTypeUnderTest: "custom",
615 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
616 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400617 name: "foo",
618 host_supported: false,
619 device_supported: false,
620 product_variables: { unbundled_build: { enabled: true } },
621 bazel_module: { bp2build_available: true },
622}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000623 ExpectedBazelTargets: []string{
624 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400625 "target_compatible_with": `["@platforms//:incompatible"]`,
626 }),
627 },
628 },
629 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000630 Description: "host and device, both, disabled overrided with product_var",
631 ModuleTypeUnderTest: "custom",
632 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
633 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400634 name: "foo",
635 host_supported: true,
636 device_supported: true,
637 enabled: false,
638 product_variables: { unbundled_build: { enabled: true } },
639 bazel_module: { bp2build_available: true },
640}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000641 ExpectedBazelTargets: []string{
642 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Cole Faust87c0c332023-07-31 12:10:12 -0700643 "target_compatible_with": `select({
644 "//build/bazel/product_config/config_settings:unbundled_build": [],
645 "//conditions:default": ["@platforms//:incompatible"],
646 })`,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400647 }),
648 },
649 },
650 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000651 Description: "host and device, neither, cannot override with arch enabled",
652 ModuleTypeUnderTest: "custom",
653 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
654 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400655 name: "foo",
656 host_supported: false,
657 device_supported: false,
658 arch: { x86: { enabled: true } },
659 bazel_module: { bp2build_available: true },
660}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000661 ExpectedBazelTargets: []string{
662 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400663 "target_compatible_with": `["@platforms//:incompatible"]`,
664 }),
665 },
666 },
667 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000668 Description: "host and device, host only",
669 ModuleTypeUnderTest: "custom",
670 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
671 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400672 name: "foo",
673 host_supported: true,
674 device_supported: false,
675 bazel_module: { bp2build_available: true },
676}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000677 ExpectedBazelTargets: []string{
678 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400679 },
680 },
681 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000682 Description: "host only",
683 ModuleTypeUnderTest: "custom",
684 ModuleTypeUnderTestFactory: customModuleFactoryHostSupported,
685 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400686 name: "foo",
687 bazel_module: { bp2build_available: true },
688}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000689 ExpectedBazelTargets: []string{
690 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400691 },
692 },
693 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000694 Description: "device only",
695 ModuleTypeUnderTest: "custom",
696 ModuleTypeUnderTestFactory: customModuleFactoryDeviceSupported,
697 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400698 name: "foo",
699 bazel_module: { bp2build_available: true },
700}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000701 ExpectedBazelTargets: []string{
702 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400703 },
704 },
705 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000706 Description: "host and device default, default",
707 ModuleTypeUnderTest: "custom",
708 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
709 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400710 name: "foo",
711 bazel_module: { bp2build_available: true },
712}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000713 ExpectedBazelTargets: []string{
714 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400715 },
716 },
717 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000718 Description: "host and device default, device only",
719 ModuleTypeUnderTest: "custom",
720 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
721 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400722 name: "foo",
723 host_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.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400728 },
729 },
730 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000731 Description: "host and device default, host only",
732 ModuleTypeUnderTest: "custom",
733 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
734 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400735 name: "foo",
736 device_supported: false,
737 bazel_module: { bp2build_available: true },
738}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000739 ExpectedBazelTargets: []string{
740 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400741 },
742 },
743 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000744 Description: "host and device default, neither",
745 ModuleTypeUnderTest: "custom",
746 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
747 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400748 name: "foo",
749 host_supported: false,
750 device_supported: false,
751 bazel_module: { bp2build_available: true },
752}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000753 ExpectedBazelTargets: []string{
754 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400755 "target_compatible_with": `["@platforms//:incompatible"]`,
756 }),
757 },
758 },
759 }
760
761 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000762 t.Run(tc.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +0000763 RunBp2BuildTestCaseSimple(t, tc)
Liz Kammerdfeb1202022-05-13 17:20:20 -0400764 })
765 }
766}
767
Jingwen Chen40067de2021-01-26 21:58:43 -0500768func TestLoadStatements(t *testing.T) {
769 testCases := []struct {
770 bazelTargets BazelTargets
771 expectedLoadStatements string
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 },
784 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
785 },
786 {
787 bazelTargets: BazelTargets{
788 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700789 name: "foo",
790 ruleClass: "cc_library",
791 loads: []BazelLoad{{
792 file: "//build/bazel/rules:cc.bzl",
793 symbols: []BazelLoadSymbol{{symbol: "cc_library"}},
794 }},
Jingwen Chen40067de2021-01-26 21:58:43 -0500795 },
796 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700797 name: "bar",
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 },
805 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
806 },
807 {
808 bazelTargets: BazelTargets{
809 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700810 name: "foo",
811 ruleClass: "cc_library",
812 loads: []BazelLoad{{
813 file: "//build/bazel/rules:cc.bzl",
814 symbols: []BazelLoadSymbol{{symbol: "cc_library"}},
815 }},
Jingwen Chen40067de2021-01-26 21:58:43 -0500816 },
817 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700818 name: "bar",
819 ruleClass: "cc_binary",
820 loads: []BazelLoad{{
821 file: "//build/bazel/rules:cc.bzl",
822 symbols: []BazelLoadSymbol{{symbol: "cc_binary"}},
823 }},
Jingwen Chen40067de2021-01-26 21:58:43 -0500824 },
825 },
826 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
827 },
828 {
829 bazelTargets: BazelTargets{
830 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700831 name: "foo",
832 ruleClass: "cc_library",
833 loads: []BazelLoad{{
834 file: "//build/bazel/rules:cc.bzl",
835 symbols: []BazelLoadSymbol{{symbol: "cc_library"}},
836 }},
Jingwen Chen40067de2021-01-26 21:58:43 -0500837 },
838 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700839 name: "bar",
840 ruleClass: "cc_binary",
841 loads: []BazelLoad{{
842 file: "//build/bazel/rules:cc.bzl",
843 symbols: []BazelLoadSymbol{{symbol: "cc_binary"}},
844 }},
Jingwen Chen40067de2021-01-26 21:58:43 -0500845 },
846 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700847 name: "baz",
848 ruleClass: "java_binary",
849 loads: []BazelLoad{{
850 file: "//build/bazel/rules:java.bzl",
851 symbols: []BazelLoadSymbol{{symbol: "java_binary"}},
852 }},
Jingwen Chen40067de2021-01-26 21:58:43 -0500853 },
854 },
855 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
856load("//build/bazel/rules:java.bzl", "java_binary")`,
857 },
858 {
859 bazelTargets: BazelTargets{
860 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700861 name: "foo",
862 ruleClass: "cc_binary",
863 loads: []BazelLoad{{
864 file: "//build/bazel/rules:cc.bzl",
865 symbols: []BazelLoadSymbol{{symbol: "cc_binary"}},
866 }},
Jingwen Chen40067de2021-01-26 21:58:43 -0500867 },
868 BazelTarget{
Cole Faustb4cb0c82023-09-14 15:16:58 -0700869 name: "bar",
870 ruleClass: "java_binary",
871 loads: []BazelLoad{{
872 file: "//build/bazel/rules:java.bzl",
873 symbols: []BazelLoadSymbol{{symbol: "java_binary"}},
874 }},
Jingwen Chen40067de2021-01-26 21:58:43 -0500875 },
876 BazelTarget{
877 name: "baz",
878 ruleClass: "genrule",
Cole Faustb4cb0c82023-09-14 15:16:58 -0700879 // Note: no loads for native rules
Jingwen Chen40067de2021-01-26 21:58:43 -0500880 },
881 },
882 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
883load("//build/bazel/rules:java.bzl", "java_binary")`,
884 },
885 }
886
887 for _, testCase := range testCases {
888 actual := testCase.bazelTargets.LoadStatements()
889 expected := testCase.expectedLoadStatements
890 if actual != expected {
891 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
892 }
893 }
894
895}
896
897func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
898 testCases := []struct {
899 bp string
900 expectedBazelTarget string
901 expectedBazelTargetCount int
902 expectedLoadStatements string
903 }{
904 {
905 bp: `custom {
906 name: "bar",
Liz Kammerdfeb1202022-05-13 17:20:20 -0400907 host_supported: true,
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400908 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500909 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500910}`,
911 expectedBazelTarget: `my_library(
912 name = "bar",
913)
914
Jingwen Chen40067de2021-01-26 21:58:43 -0500915proto_library(
916 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400917)
918
919my_proto_library(
920 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500921)`,
922 expectedBazelTargetCount: 3,
923 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
924load("//build/bazel/rules:rules.bzl", "my_library")`,
925 },
926 }
927
928 dir := "."
929 for _, testCase := range testCases {
930 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
931 ctx := android.NewTestContext(config)
Liz Kammerdfeb1202022-05-13 17:20:20 -0400932 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
Jingwen Chen40067de2021-01-26 21:58:43 -0500933 ctx.RegisterForBazelConversion()
934
935 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
936 android.FailIfErrored(t, errs)
937 _, errs = ctx.ResolveDependencies(config)
938 android.FailIfErrored(t, errs)
939
Cole Faustb85d1a12022-11-08 18:14:01 -0800940 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammer6eff3232021-08-26 08:37:59 -0400941 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
942 android.FailIfErrored(t, err)
Jingwen Chen40067de2021-01-26 21:58:43 -0500943 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
944 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
945 }
946
947 actualBazelTargets := bazelTargets.String()
948 if actualBazelTargets != testCase.expectedBazelTarget {
949 t.Errorf(
950 "Expected generated Bazel target to be '%s', got '%s'",
951 testCase.expectedBazelTarget,
952 actualBazelTargets,
953 )
954 }
955
956 actualLoadStatements := bazelTargets.LoadStatements()
957 if actualLoadStatements != testCase.expectedLoadStatements {
958 t.Errorf(
959 "Expected generated load statements to be '%s', got '%s'",
960 testCase.expectedLoadStatements,
961 actualLoadStatements,
962 )
963 }
964 }
965}
966
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500967func TestModuleTypeBp2Build(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000968 testCases := []Bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500969 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000970 Description: "filegroup with does not specify srcs",
971 ModuleTypeUnderTest: "filegroup",
972 ModuleTypeUnderTestFactory: android.FileGroupFactory,
973 Blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500974 name: "fg_foo",
975 bazel_module: { bp2build_available: true },
976}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000977 ExpectedBazelTargets: []string{
978 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerebfcf672021-02-16 15:00:05 -0500979 },
980 },
981 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000982 Description: "filegroup with no srcs",
983 ModuleTypeUnderTest: "filegroup",
984 ModuleTypeUnderTestFactory: android.FileGroupFactory,
985 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500986 name: "fg_foo",
987 srcs: [],
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 Kammer356f7d42021-01-26 09:18:53 -0500992 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500993 },
994 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000995 Description: "filegroup with srcs",
996 ModuleTypeUnderTest: "filegroup",
997 ModuleTypeUnderTestFactory: android.FileGroupFactory,
998 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500999 name: "fg_foo",
1000 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001001 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001002}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001003 ExpectedBazelTargets: []string{
1004 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001005 "srcs": `[
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001006 "a",
1007 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001008 ]`,
1009 }),
Liz Kammer356f7d42021-01-26 09:18:53 -05001010 },
1011 },
1012 {
Usta Shresthad5580312022-09-23 16:46:38 -04001013 Description: "filegroup with dot-slash-prefixed srcs",
1014 ModuleTypeUnderTest: "filegroup",
1015 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1016 Blueprint: `filegroup {
1017 name: "fg_foo",
1018 srcs: ["./a", "./b"],
1019 bazel_module: { bp2build_available: true },
1020}`,
1021 ExpectedBazelTargets: []string{
1022 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1023 "srcs": `[
1024 "a",
1025 "b",
1026 ]`,
1027 }),
1028 },
1029 },
1030 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001031 Description: "filegroup with excludes srcs",
1032 ModuleTypeUnderTest: "filegroup",
1033 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1034 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -05001035 name: "fg_foo",
1036 srcs: ["a", "b"],
1037 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001038 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -05001039}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001040 ExpectedBazelTargets: []string{
1041 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001042 "srcs": `["b"]`,
1043 }),
Liz Kammer356f7d42021-01-26 09:18:53 -05001044 },
1045 },
1046 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001047 Description: "depends_on_other_dir_module",
1048 ModuleTypeUnderTest: "filegroup",
1049 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1050 Blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001051 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -05001052 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +00001053 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -05001054 "c",
1055 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001056 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -05001057}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001058 Filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -05001059 "other/Android.bp": `filegroup {
1060 name: "foo",
1061 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -04001062 bazel_module: { bp2build_available: true },
1063}`,
1064 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001065 ExpectedBazelTargets: []string{
1066 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001067 "srcs": `[
1068 "//other:foo",
1069 "c",
1070 ]`,
1071 }),
1072 },
Liz Kammer6eff3232021-08-26 08:37:59 -04001073 },
1074 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001075 Description: "depends_on_other_unconverted_module_error",
1076 ModuleTypeUnderTest: "filegroup",
1077 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1078 UnconvertedDepsMode: errorModulesUnconvertedDeps,
1079 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001080 name: "foobar",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001081 srcs: [
1082 ":foo",
1083 "c",
1084 ],
1085 bazel_module: { bp2build_available: true },
1086}`,
Sasha Smundakf2bb26f2022-08-04 11:28:15 -07001087 ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on unconverted modules: foo`),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001088 Filesystem: map[string]string{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001089 "other/Android.bp": `filegroup {
1090 name: "foo",
1091 srcs: ["a", "b"],
1092}`,
1093 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001094 },
Usta Shrestha808bc712022-09-23 23:18:18 -04001095 {
1096 Description: "depends_on_other_missing_module_error",
1097 ModuleTypeUnderTest: "filegroup",
1098 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1099 UnconvertedDepsMode: errorModulesUnconvertedDeps,
1100 Blueprint: `filegroup {
1101 name: "foobar",
1102 srcs: [
1103 "c",
1104 "//other:foo",
1105 "//other:goo",
1106 ],
1107 bazel_module: { bp2build_available: true },
1108}`,
1109 ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on missing modules: //other:goo`),
1110 Filesystem: map[string]string{"other/Android.bp": `filegroup {
1111 name: "foo",
1112 srcs: ["a"],
1113 bazel_module: { bp2build_available: true },
1114}
1115`,
1116 },
1117 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001118 }
1119
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001120 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001121 t.Run(testCase.Description, func(t *testing.T) {
1122 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
Liz Kammer6eff3232021-08-26 08:37:59 -04001123 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001124 }
1125}
Jingwen Chen041b1842021-02-01 00:23:25 -05001126
Jingwen Chen12b4c272021-03-10 02:05:59 -05001127func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001128 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001129 moduleTypeUnderTest string
1130 moduleTypeUnderTestFactory android.ModuleFactory
1131 bp string
1132 expectedCount int
1133 description string
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001134 }{
1135 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001136 description: "explicitly unavailable",
1137 moduleTypeUnderTest: "filegroup",
1138 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001139 bp: `filegroup {
1140 name: "foo",
1141 srcs: ["a", "b"],
1142 bazel_module: { bp2build_available: false },
1143}`,
1144 expectedCount: 0,
1145 },
1146 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001147 description: "implicitly unavailable",
1148 moduleTypeUnderTest: "filegroup",
1149 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001150 bp: `filegroup {
1151 name: "foo",
1152 srcs: ["a", "b"],
1153}`,
1154 expectedCount: 0,
1155 },
1156 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001157 description: "explicitly available",
1158 moduleTypeUnderTest: "filegroup",
1159 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001160 bp: `filegroup {
1161 name: "foo",
1162 srcs: ["a", "b"],
1163 bazel_module: { bp2build_available: true },
1164}`,
1165 expectedCount: 1,
1166 },
1167 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001168 description: "generates more than 1 target if needed",
1169 moduleTypeUnderTest: "custom",
Liz Kammerdfeb1202022-05-13 17:20:20 -04001170 moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001171 bp: `custom {
1172 name: "foo",
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001173 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001174 bazel_module: { bp2build_available: true },
1175}`,
1176 expectedCount: 3,
1177 },
1178 }
1179
1180 dir := "."
1181 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -04001182 t.Run(testCase.description, func(t *testing.T) {
1183 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1184 ctx := android.NewTestContext(config)
1185 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001186 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001187
Liz Kammer2ada09a2021-08-11 00:17:36 -04001188 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1189 android.FailIfErrored(t, errs)
1190 _, errs = ctx.ResolveDependencies(config)
1191 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001192
Cole Faustb85d1a12022-11-08 18:14:01 -08001193 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammer6eff3232021-08-26 08:37:59 -04001194 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1195 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001196 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1197 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1198 }
1199 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001200 }
1201}
Liz Kammerba3ea162021-02-17 13:22:03 -05001202
Jingwen Chen12b4c272021-03-10 02:05:59 -05001203func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1204 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001205 moduleTypeUnderTest string
1206 moduleTypeUnderTestFactory android.ModuleFactory
1207 expectedCount map[string]int
1208 description string
Sam Delmerico24c56032022-03-28 19:53:03 +00001209 bp2buildConfig allowlists.Bp2BuildConfig
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001210 checkDir string
1211 fs map[string]string
MarkDacek9c094ca2023-03-16 19:15:19 +00001212 forceEnabledModules []string
1213 expectedErrorMessages []string
Jingwen Chen12b4c272021-03-10 02:05:59 -05001214 }{
1215 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001216 description: "test bp2build config package and subpackages config",
1217 moduleTypeUnderTest: "filegroup",
1218 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001219 expectedCount: map[string]int{
1220 "migrated": 1,
1221 "migrated/but_not_really": 0,
1222 "migrated/but_not_really/but_really": 1,
1223 "not_migrated": 0,
1224 "also_not_migrated": 0,
1225 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001226 bp2buildConfig: allowlists.Bp2BuildConfig{
1227 "migrated": allowlists.Bp2BuildDefaultTrueRecursively,
1228 "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
1229 "not_migrated": allowlists.Bp2BuildDefaultFalse,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001230 },
1231 fs: map[string]string{
1232 "migrated/Android.bp": `filegroup { name: "a" }`,
1233 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1234 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1235 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1236 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1237 },
1238 },
1239 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001240 description: "test bp2build config opt-in and opt-out",
1241 moduleTypeUnderTest: "filegroup",
1242 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001243 expectedCount: map[string]int{
1244 "package-opt-in": 2,
1245 "package-opt-in/subpackage": 0,
1246 "package-opt-out": 1,
1247 "package-opt-out/subpackage": 0,
1248 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001249 bp2buildConfig: allowlists.Bp2BuildConfig{
1250 "package-opt-in": allowlists.Bp2BuildDefaultFalse,
1251 "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001252 },
1253 fs: map[string]string{
1254 "package-opt-in/Android.bp": `
1255filegroup { name: "opt-in-a" }
1256filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1257filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1258`,
1259
1260 "package-opt-in/subpackage/Android.bp": `
1261filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1262`,
1263
1264 "package-opt-out/Android.bp": `
1265filegroup { name: "opt-out-a" }
1266filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1267filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1268`,
1269
1270 "package-opt-out/subpackage/Android.bp": `
1271filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1272filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1273`,
1274 },
1275 },
MarkDacek9c094ca2023-03-16 19:15:19 +00001276 {
1277 description: "test force-enabled errors out",
1278 moduleTypeUnderTest: "filegroup",
1279 moduleTypeUnderTestFactory: android.FileGroupFactory,
1280 expectedCount: map[string]int{
1281 "migrated": 0,
1282 "not_migrated": 0,
1283 },
1284 bp2buildConfig: allowlists.Bp2BuildConfig{
1285 "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
1286 "not_migrated": allowlists.Bp2BuildDefaultFalse,
1287 },
1288 fs: map[string]string{
1289 "migrated/Android.bp": `filegroup { name: "a" }`,
1290 },
1291 forceEnabledModules: []string{"a"},
1292 expectedErrorMessages: []string{"Force Enabled Module a not converted"},
1293 },
Jingwen Chen12b4c272021-03-10 02:05:59 -05001294 }
1295
1296 dir := "."
1297 for _, testCase := range testCases {
1298 fs := make(map[string][]byte)
1299 toParse := []string{
1300 "Android.bp",
1301 }
1302 for f, content := range testCase.fs {
1303 if strings.HasSuffix(f, "Android.bp") {
1304 toParse = append(toParse, f)
1305 }
1306 fs[f] = []byte(content)
1307 }
1308 config := android.TestConfig(buildDir, nil, "", fs)
MarkDacek9c094ca2023-03-16 19:15:19 +00001309 config.AddForceEnabledModules(testCase.forceEnabledModules)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001310 ctx := android.NewTestContext(config)
1311 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Sam Delmerico24c56032022-03-28 19:53:03 +00001312 allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig)
1313 ctx.RegisterBp2BuildConfig(allowlist)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001314 ctx.RegisterForBazelConversion()
1315
1316 _, errs := ctx.ParseFileList(dir, toParse)
1317 android.FailIfErrored(t, errs)
1318 _, errs = ctx.ResolveDependencies(config)
1319 android.FailIfErrored(t, errs)
1320
Cole Faustb85d1a12022-11-08 18:14:01 -08001321 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Jingwen Chen12b4c272021-03-10 02:05:59 -05001322
1323 // For each directory, test that the expected number of generated targets is correct.
1324 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -04001325 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
MarkDacek9c094ca2023-03-16 19:15:19 +00001326 android.CheckErrorsAgainstExpectations(t, err, testCase.expectedErrorMessages)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001327 if actualCount := len(bazelTargets); actualCount != expectedCount {
1328 t.Fatalf(
1329 "%s: Expected %d bazel target for %s package, got %d",
1330 testCase.description,
1331 expectedCount,
1332 dir,
1333 actualCount)
1334 }
1335
1336 }
1337 }
1338}
1339
Liz Kammerba3ea162021-02-17 13:22:03 -05001340func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001341 testCases := []Bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -05001342 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001343 Description: "filegroup bazel_module.label",
1344 ModuleTypeUnderTest: "filegroup",
1345 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1346 Blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -05001347 name: "fg_foo",
1348 bazel_module: { label: "//other:fg_foo" },
1349}`,
Cole Faustea602c52022-08-31 14:48:26 -07001350 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001351 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001352 "other/BUILD.bazel": `// BUILD file`,
1353 },
1354 },
1355 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001356 Description: "multiple bazel_module.label same BUILD",
1357 ModuleTypeUnderTest: "filegroup",
1358 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1359 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001360 name: "fg_foo",
1361 bazel_module: { label: "//other:fg_foo" },
1362 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001363
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001364 filegroup {
1365 name: "foo",
1366 bazel_module: { label: "//other:foo" },
1367 }`,
Cole Faustea602c52022-08-31 14:48:26 -07001368 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001369 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001370 "other/BUILD.bazel": `// BUILD file`,
1371 },
1372 },
1373 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001374 Description: "filegroup bazel_module.label and bp2build in subdir",
1375 ModuleTypeUnderTest: "filegroup",
1376 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1377 Dir: "other",
1378 Blueprint: ``,
1379 Filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001380 "other/Android.bp": `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001381 name: "fg_foo",
1382 bazel_module: {
1383 bp2build_available: true,
1384 },
1385 }
1386 filegroup {
1387 name: "fg_bar",
1388 bazel_module: {
1389 label: "//other:fg_bar"
1390 },
1391 }`,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001392 "other/BUILD.bazel": `// definition for fg_bar`,
1393 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001394 ExpectedBazelTargets: []string{
1395 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001396 },
1397 },
1398 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001399 Description: "filegroup bazel_module.label and filegroup bp2build",
1400 ModuleTypeUnderTest: "filegroup",
1401 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001402
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001403 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001404 "other/BUILD.bazel": `// BUILD file`,
1405 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001406 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001407 name: "fg_foo",
1408 bazel_module: {
1409 label: "//other:fg_foo",
1410 },
1411 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001412
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001413 filegroup {
1414 name: "fg_bar",
1415 bazel_module: {
1416 bp2build_available: true,
1417 },
1418 }`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001419 ExpectedBazelTargets: []string{
1420 MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001421 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001422 },
1423 }
1424
1425 dir := "."
1426 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001427 t.Run(testCase.Description, func(t *testing.T) {
Jingwen Chen49109762021-05-25 05:16:48 +00001428 fs := make(map[string][]byte)
1429 toParse := []string{
1430 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001431 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001432 for f, content := range testCase.Filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001433 if strings.HasSuffix(f, "Android.bp") {
1434 toParse = append(toParse, f)
1435 }
1436 fs[f] = []byte(content)
1437 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001438 config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001439 ctx := android.NewTestContext(config)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001440 ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001441 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001442
Jingwen Chen49109762021-05-25 05:16:48 +00001443 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001444 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001445 return
1446 }
1447 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001448 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001449 return
1450 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001451
Jingwen Chen49109762021-05-25 05:16:48 +00001452 checkDir := dir
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001453 if testCase.Dir != "" {
1454 checkDir = testCase.Dir
Jingwen Chen49109762021-05-25 05:16:48 +00001455 }
Cole Faustb85d1a12022-11-08 18:14:01 -08001456 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammer6eff3232021-08-26 08:37:59 -04001457 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1458 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001459 bazelTargets.sort()
1460 actualCount := len(bazelTargets)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001461 expectedCount := len(testCase.ExpectedBazelTargets)
Jingwen Chen49109762021-05-25 05:16:48 +00001462 if actualCount != expectedCount {
1463 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1464 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001465 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001466 actualContent := target.content
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001467 expectedContent := testCase.ExpectedBazelTargets[i]
Jingwen Chen49109762021-05-25 05:16:48 +00001468 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001469 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001470 "Expected generated Bazel target to be '%s', got '%s'",
1471 expectedContent,
1472 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001473 )
1474 }
1475 }
Jingwen Chen49109762021-05-25 05:16:48 +00001476 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001477 }
1478}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001479
Jingwen Chen0eeaeb82022-09-21 10:27:42 +00001480func TestGlob(t *testing.T) {
1481 testCases := []Bp2buildTestCase{
1482 {
1483 Description: "filegroup with glob",
1484 ModuleTypeUnderTest: "filegroup",
1485 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1486 Blueprint: `filegroup {
1487 name: "fg_foo",
1488 srcs: ["**/*.txt"],
1489 bazel_module: { bp2build_available: true },
1490}`,
1491 ExpectedBazelTargets: []string{
1492 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1493 "srcs": `[
1494 "other/a.txt",
1495 "other/b.txt",
1496 "other/subdir/a.txt",
1497 ]`,
1498 }),
1499 },
1500 Filesystem: map[string]string{
1501 "other/a.txt": "",
1502 "other/b.txt": "",
1503 "other/subdir/a.txt": "",
1504 "other/file": "",
1505 },
1506 },
1507 {
1508 Description: "filegroup with glob in subdir",
1509 ModuleTypeUnderTest: "filegroup",
1510 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1511 Dir: "other",
1512 Filesystem: map[string]string{
1513 "other/Android.bp": `filegroup {
1514 name: "fg_foo",
1515 srcs: ["**/*.txt"],
1516 bazel_module: { bp2build_available: true },
1517}`,
1518 "other/a.txt": "",
1519 "other/b.txt": "",
1520 "other/subdir/a.txt": "",
1521 "other/file": "",
1522 },
1523 ExpectedBazelTargets: []string{
1524 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1525 "srcs": `[
1526 "a.txt",
1527 "b.txt",
1528 "subdir/a.txt",
1529 ]`,
1530 }),
1531 },
1532 },
1533 {
1534 Description: "filegroup with glob with no kept BUILD files",
1535 ModuleTypeUnderTest: "filegroup",
1536 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1537 KeepBuildFileForDirs: []string{
1538 // empty
1539 },
1540 Blueprint: `filegroup {
1541 name: "fg_foo",
1542 srcs: ["**/*.txt"],
1543 bazel_module: { bp2build_available: true },
1544}`,
1545 Filesystem: map[string]string{
1546 "a.txt": "",
1547 "b.txt": "",
1548 "foo/BUILD": "",
1549 "foo/a.txt": "",
1550 "foo/bar/BUILD": "",
1551 "foo/bar/b.txt": "",
1552 },
1553 ExpectedBazelTargets: []string{
1554 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1555 "srcs": `[
1556 "a.txt",
1557 "b.txt",
1558 "foo/a.txt",
1559 "foo/bar/b.txt",
1560 ]`,
1561 }),
1562 },
1563 },
1564 {
1565 Description: "filegroup with glob with kept BUILD file",
1566 ModuleTypeUnderTest: "filegroup",
1567 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1568 KeepBuildFileForDirs: []string{
1569 "foo",
1570 },
1571 Blueprint: `filegroup {
1572 name: "fg_foo",
1573 srcs: ["**/*.txt"],
1574 bazel_module: { bp2build_available: true },
1575}`,
1576 Filesystem: map[string]string{
1577 "a.txt": "",
1578 "b.txt": "",
1579 "foo/BUILD": "",
1580 "foo/a.txt": "",
1581 "foo/bar/BUILD": "",
1582 "foo/bar/b.txt": "",
1583 },
1584 ExpectedBazelTargets: []string{
1585 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1586 "srcs": `[
1587 "a.txt",
1588 "b.txt",
1589 "//foo:a.txt",
1590 "//foo:bar/b.txt",
1591 ]`,
1592 }),
1593 },
1594 },
1595 {
1596 Description: "filegroup with glob with kept BUILD.bazel file",
1597 ModuleTypeUnderTest: "filegroup",
1598 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1599 KeepBuildFileForDirs: []string{
1600 "foo",
1601 },
1602 Blueprint: `filegroup {
1603 name: "fg_foo",
1604 srcs: ["**/*.txt"],
1605 bazel_module: { bp2build_available: true },
1606}`,
1607 Filesystem: map[string]string{
1608 "a.txt": "",
1609 "b.txt": "",
1610 "foo/BUILD.bazel": "",
1611 "foo/a.txt": "",
1612 "foo/bar/BUILD.bazel": "",
1613 "foo/bar/b.txt": "",
1614 },
1615 ExpectedBazelTargets: []string{
1616 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1617 "srcs": `[
1618 "a.txt",
1619 "b.txt",
1620 "//foo:a.txt",
1621 "//foo:bar/b.txt",
1622 ]`,
1623 }),
1624 },
1625 },
1626 {
1627 Description: "filegroup with glob with Android.bp file as boundary",
1628 ModuleTypeUnderTest: "filegroup",
1629 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1630 Blueprint: `filegroup {
1631 name: "fg_foo",
1632 srcs: ["**/*.txt"],
1633 bazel_module: { bp2build_available: true },
1634}`,
1635 Filesystem: map[string]string{
1636 "a.txt": "",
1637 "b.txt": "",
1638 "foo/Android.bp": "",
1639 "foo/a.txt": "",
1640 "foo/bar/Android.bp": "",
1641 "foo/bar/b.txt": "",
1642 },
1643 ExpectedBazelTargets: []string{
1644 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1645 "srcs": `[
1646 "a.txt",
1647 "b.txt",
1648 "//foo:a.txt",
1649 "//foo/bar:b.txt",
1650 ]`,
1651 }),
1652 },
1653 },
1654 {
1655 Description: "filegroup with glob in subdir with kept BUILD and BUILD.bazel file",
1656 ModuleTypeUnderTest: "filegroup",
1657 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1658 Dir: "other",
1659 KeepBuildFileForDirs: []string{
1660 "other/foo",
1661 "other/foo/bar",
1662 // deliberately not other/foo/baz/BUILD.
1663 },
1664 Filesystem: map[string]string{
1665 "other/Android.bp": `filegroup {
1666 name: "fg_foo",
1667 srcs: ["**/*.txt"],
1668 bazel_module: { bp2build_available: true },
1669}`,
1670 "other/a.txt": "",
1671 "other/b.txt": "",
1672 "other/foo/BUILD": "",
1673 "other/foo/a.txt": "",
1674 "other/foo/bar/BUILD.bazel": "",
1675 "other/foo/bar/b.txt": "",
1676 "other/foo/baz/BUILD": "",
1677 "other/foo/baz/c.txt": "",
1678 },
1679 ExpectedBazelTargets: []string{
1680 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1681 "srcs": `[
1682 "a.txt",
1683 "b.txt",
1684 "//other/foo:a.txt",
1685 "//other/foo/bar:b.txt",
1686 "//other/foo:baz/c.txt",
1687 ]`,
1688 }),
1689 },
1690 },
1691 }
1692
1693 for _, testCase := range testCases {
1694 t.Run(testCase.Description, func(t *testing.T) {
1695 RunBp2BuildTestCaseSimple(t, testCase)
1696 })
1697 }
1698}
1699
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001700func TestGlobExcludeSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001701 testCases := []Bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001702 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001703 Description: "filegroup top level exclude_srcs",
1704 ModuleTypeUnderTest: "filegroup",
1705 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1706 Blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001707 name: "fg_foo",
1708 srcs: ["**/*.txt"],
1709 exclude_srcs: ["c.txt"],
1710 bazel_module: { bp2build_available: true },
1711}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001712 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001713 "a.txt": "",
1714 "b.txt": "",
1715 "c.txt": "",
1716 "dir/Android.bp": "",
1717 "dir/e.txt": "",
1718 "dir/f.txt": "",
1719 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001720 ExpectedBazelTargets: []string{
1721 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001722 "srcs": `[
1723 "a.txt",
1724 "b.txt",
1725 "//dir:e.txt",
1726 "//dir:f.txt",
1727 ]`,
1728 }),
1729 },
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001730 },
1731 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001732 Description: "filegroup in subdir exclude_srcs",
1733 ModuleTypeUnderTest: "filegroup",
1734 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1735 Blueprint: "",
1736 Dir: "dir",
1737 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001738 "dir/Android.bp": `filegroup {
1739 name: "fg_foo",
1740 srcs: ["**/*.txt"],
1741 exclude_srcs: ["b.txt"],
1742 bazel_module: { bp2build_available: true },
1743}
1744`,
1745 "dir/a.txt": "",
1746 "dir/b.txt": "",
1747 "dir/subdir/Android.bp": "",
1748 "dir/subdir/e.txt": "",
1749 "dir/subdir/f.txt": "",
1750 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001751 ExpectedBazelTargets: []string{
1752 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001753 "srcs": `[
Liz Kammer9abd62d2021-05-21 08:37:59 -04001754 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001755 "//dir/subdir:e.txt",
1756 "//dir/subdir:f.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001757 ]`,
1758 }),
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001759 },
1760 },
1761 }
1762
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001763 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001764 t.Run(testCase.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001765 RunBp2BuildTestCaseSimple(t, testCase)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001766 })
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001767 }
1768}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001769
1770func TestCommonBp2BuildModuleAttrs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001771 testCases := []Bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001772 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001773 Description: "Required into data test",
1774 ModuleTypeUnderTest: "filegroup",
1775 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001776 StubbedBuildDefinitions: []string{"reqd"},
1777 Blueprint: simpleModule("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001778filegroup {
1779 name: "fg_foo",
1780 required: ["reqd"],
1781 bazel_module: { bp2build_available: true },
1782}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001783 ExpectedBazelTargets: []string{
1784 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001785 "data": `[":reqd"]`,
1786 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001787 },
1788 },
1789 {
Jingwen Chena5ecb372022-09-21 09:05:37 +00001790 Description: "Required into data test, cyclic self reference is filtered out",
1791 ModuleTypeUnderTest: "filegroup",
1792 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001793 StubbedBuildDefinitions: []string{"reqd"},
1794 Blueprint: simpleModule("filegroup", "reqd") + `
Jingwen Chena5ecb372022-09-21 09:05:37 +00001795filegroup {
1796 name: "fg_foo",
1797 required: ["reqd", "fg_foo"],
1798 bazel_module: { bp2build_available: true },
1799}`,
1800 ExpectedBazelTargets: []string{
1801 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1802 "data": `[":reqd"]`,
1803 }),
1804 },
1805 },
1806 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001807 Description: "Required via arch into data test",
1808 ModuleTypeUnderTest: "python_library",
1809 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001810 StubbedBuildDefinitions: []string{"reqdx86", "reqdarm"},
1811 Blueprint: simpleModule("python_library", "reqdx86") +
1812 simpleModule("python_library", "reqdarm") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001813python_library {
1814 name: "fg_foo",
1815 arch: {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001816 arm: {
1817 required: ["reqdarm"],
1818 },
1819 x86: {
1820 required: ["reqdx86"],
1821 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001822 },
1823 bazel_module: { bp2build_available: true },
1824}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001825 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001826 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001827 "data": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001828 "//build/bazel/platforms/arch:arm": [":reqdarm"],
1829 "//build/bazel/platforms/arch:x86": [":reqdx86"],
1830 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001831 })`,
1832 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001833 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001834 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001835 },
1836 },
1837 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001838 Description: "Required appended to data test",
1839 ModuleTypeUnderTest: "python_library",
1840 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1841 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001842 "data.bin": "",
1843 "src.py": "",
1844 },
Chris Parsonscd209032023-09-19 01:12:48 +00001845 StubbedBuildDefinitions: []string{"reqd"},
1846 Blueprint: simpleModule("python_library", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001847python_library {
1848 name: "fg_foo",
1849 data: ["data.bin"],
1850 required: ["reqd"],
1851 bazel_module: { bp2build_available: true },
1852}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001853 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001854 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001855 "data": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001856 "data.bin",
1857 ":reqd",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001858 ]`,
1859 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001860 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001861 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001862 },
1863 },
1864 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001865 Description: "All props-to-attrs at once together test",
1866 ModuleTypeUnderTest: "filegroup",
1867 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Chris Parsonscd209032023-09-19 01:12:48 +00001868 StubbedBuildDefinitions: []string{"reqd"},
1869 Blueprint: simpleModule("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001870filegroup {
1871 name: "fg_foo",
1872 required: ["reqd"],
1873 bazel_module: { bp2build_available: true },
1874}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001875 ExpectedBazelTargets: []string{
1876 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001877 "data": `[":reqd"]`,
1878 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001879 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001880 },
1881 }
1882
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001883 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001884 t.Run(tc.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001885 RunBp2BuildTestCaseSimple(t, tc)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001886 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001887 }
1888}
Sasha Smundak05b0ba62022-09-26 18:15:45 -07001889
1890func TestLicensesAttrConversion(t *testing.T) {
1891 RunBp2BuildTestCase(t,
1892 func(ctx android.RegistrationContext) {
1893 ctx.RegisterModuleType("license", android.LicenseFactory)
1894 },
1895 Bp2buildTestCase{
1896 Description: "Test that licenses: attribute is converted",
1897 ModuleTypeUnderTest: "filegroup",
1898 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1899 Blueprint: `
1900license {
1901 name: "my_license",
1902}
1903filegroup {
1904 name: "my_filegroup",
1905 licenses: ["my_license"],
1906}
1907`,
1908 ExpectedBazelTargets: []string{
1909 MakeBazelTargetNoRestrictions("filegroup", "my_filegroup", AttrNameToString{
1910 "applicable_licenses": `[":my_license"]`,
1911 }),
1912 MakeBazelTargetNoRestrictions("android_license", "my_license", AttrNameToString{}),
1913 },
1914 })
1915}
Spandan Das5af0bd32022-09-28 20:43:08 +00001916
Spandan Das6a448ec2023-04-19 17:36:12 +00001917func TestGenerateConfigSetting(t *testing.T) {
1918 bp := `
1919 custom {
1920 name: "foo",
1921 test_config_setting: true,
1922 }
1923 `
1924 expectedBazelTargets := []string{
1925 MakeBazelTargetNoRestrictions(
1926 "config_setting",
1927 "foo_config_setting",
1928 AttrNameToString{
1929 "flag_values": `{
1930 "//build/bazel/rules/my_string_setting": "foo",
1931 }`,
1932 },
1933 ),
1934 MakeBazelTarget(
1935 "custom",
1936 "foo",
1937 AttrNameToString{},
1938 ),
1939 }
1940 registerCustomModule := func(ctx android.RegistrationContext) {
1941 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
1942 }
1943 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
1944 Blueprint: bp,
1945 ExpectedBazelTargets: expectedBazelTargets,
1946 Description: "Generating API contribution Bazel targets for custom module",
1947 })
1948}
Spandan Das921af322023-04-26 02:56:37 +00001949
1950// If values of all keys in an axis are equal to //conditions:default, drop the axis and print the common value
1951func TestPrettyPrintSelectMapEqualValues(t *testing.T) {
1952 lla := bazel.LabelListAttribute{
1953 Value: bazel.LabelList{},
1954 }
1955 libFooImplLabel := bazel.Label{
1956 Label: ":libfoo.impl",
1957 }
Spandan Das6d4d9da2023-04-18 06:20:40 +00001958 lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidPlatform, bazel.MakeLabelList([]bazel.Label{libFooImplLabel}))
Spandan Das921af322023-04-26 02:56:37 +00001959 lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.MakeLabelList([]bazel.Label{libFooImplLabel}))
1960 actual, _ := prettyPrintAttribute(lla, 0)
1961 android.AssertStringEquals(t, "Print the common value if all keys in an axis have the same value", `[":libfoo.impl"]`, actual)
1962}
Spandan Das3131d672023-08-03 22:33:47 +00001963
Chris Parsons5011e612023-09-13 23:33:20 +00001964func TestAlreadyPresentBuildTarget(t *testing.T) {
1965 bp := `
1966 custom {
1967 name: "foo",
1968 }
1969 custom {
1970 name: "bar",
1971 }
1972 `
1973 alreadyPresentBuildFile :=
1974 MakeBazelTarget(
1975 "custom",
1976 "foo",
1977 AttrNameToString{},
1978 )
1979 expectedBazelTargets := []string{
1980 MakeBazelTarget(
1981 "custom",
1982 "bar",
1983 AttrNameToString{},
1984 ),
1985 }
1986 registerCustomModule := func(ctx android.RegistrationContext) {
1987 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
1988 }
1989 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
1990 AlreadyExistingBuildContents: alreadyPresentBuildFile,
1991 Blueprint: bp,
1992 ExpectedBazelTargets: expectedBazelTargets,
1993 Description: "Not duplicating work for an already-present BUILD target.",
1994 })
1995}
1996
Chris Parsons0c4de1f2023-09-21 20:36:35 +00001997func TestAlreadyPresentOneToManyBuildTarget(t *testing.T) {
1998 bp := `
1999 custom {
2000 name: "foo",
2001 one_to_many_prop: true,
2002 }
2003 custom {
2004 name: "bar",
2005 }
2006 `
2007 alreadyPresentBuildFile :=
2008 MakeBazelTarget(
2009 "custom",
2010 // one_to_many_prop ensures that foo generates "foo_proto_library_deps".
2011 "foo_proto_library_deps",
2012 AttrNameToString{},
2013 )
2014 expectedBazelTargets := []string{
2015 MakeBazelTarget(
2016 "custom",
2017 "bar",
2018 AttrNameToString{},
2019 ),
2020 }
2021 registerCustomModule := func(ctx android.RegistrationContext) {
2022 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2023 }
2024 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2025 AlreadyExistingBuildContents: alreadyPresentBuildFile,
2026 Blueprint: bp,
2027 ExpectedBazelTargets: expectedBazelTargets,
2028 Description: "Not duplicating work for an already-present BUILD target (different generated name)",
2029 })
2030}
2031
Chris Parsons5011e612023-09-13 23:33:20 +00002032// Verifies that if a module is defined in pkg1/Android.bp, that a target present
2033// in pkg2/BUILD.bazel does not result in the module being labeled "already defined
2034// in a BUILD file".
2035func TestBuildTargetPresentOtherDirectory(t *testing.T) {
2036 bp := `
2037 custom {
2038 name: "foo",
2039 }
2040 `
2041 expectedBazelTargets := []string{
2042 MakeBazelTarget(
2043 "custom",
2044 "foo",
2045 AttrNameToString{},
2046 ),
2047 }
2048 registerCustomModule := func(ctx android.RegistrationContext) {
2049 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2050 }
2051 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2052 KeepBuildFileForDirs: []string{"other_pkg"},
2053 Filesystem: map[string]string{
2054 "other_pkg/BUILD.bazel": MakeBazelTarget("custom", "foo", AttrNameToString{}),
2055 },
2056 Blueprint: bp,
2057 ExpectedBazelTargets: expectedBazelTargets,
2058 Description: "Not treating a BUILD target as the bazel definition for a module in another package",
2059 })
2060}
2061
Spandan Das3131d672023-08-03 22:33:47 +00002062// If CommonAttributes.Dir is set, the bazel target should be created in that dir
2063func TestCreateBazelTargetInDifferentDir(t *testing.T) {
2064 t.Parallel()
2065 bp := `
2066 custom {
2067 name: "foo",
2068 dir: "subdir",
2069 }
2070 `
2071 registerCustomModule := func(ctx android.RegistrationContext) {
2072 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2073 }
2074 // Check that foo is not created in root dir
2075 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2076 Description: "foo is not created in root dir because it sets dir explicitly",
2077 Blueprint: bp,
2078 Filesystem: map[string]string{
2079 "subdir/Android.bp": "",
2080 },
2081 ExpectedBazelTargets: []string{},
2082 })
2083 // Check that foo is created in `subdir`
2084 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2085 Description: "foo is created in `subdir` because it sets dir explicitly",
2086 Blueprint: bp,
2087 Filesystem: map[string]string{
2088 "subdir/Android.bp": "",
2089 },
2090 Dir: "subdir",
2091 ExpectedBazelTargets: []string{
2092 MakeBazelTarget("custom", "foo", AttrNameToString{}),
2093 },
2094 })
2095 // Check that we cannot create target in different dir if it is does not an Android.bp
2096 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2097 Description: "foo cannot be created in `subdir` because it does not contain an Android.bp file",
2098 Blueprint: bp,
2099 Dir: "subdir",
2100 ExpectedErr: fmt.Errorf("Cannot use ca.Dir to create a BazelTarget in dir: subdir since it does not contain an Android.bp file"),
2101 })
2102
2103}
Chris Parsons5f1b3c72023-09-28 20:41:03 +00002104
2105func TestBp2buildDepsMutator_missingTransitiveDep(t *testing.T) {
2106 bp := `
2107 custom {
2108 name: "foo",
2109 }
2110
2111 custom {
2112 name: "has_deps",
2113 arch_paths: [":has_missing_dep", ":foo"],
2114 }
2115
2116 custom {
2117 name: "has_missing_dep",
2118 arch_paths: [":missing"],
2119 }
2120 `
2121 expectedBazelTargets := []string{
2122 MakeBazelTarget(
2123 "custom",
2124 "foo",
2125 AttrNameToString{},
2126 ),
2127 }
2128 registerCustomModule := func(ctx android.RegistrationContext) {
2129 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2130 }
2131 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2132 Blueprint: bp,
2133 ExpectedBazelTargets: expectedBazelTargets,
2134 Description: "Skipping conversion of a target with missing transitive dep",
2135 DepsMutator: true,
2136 })
2137}
2138
2139func TestBp2buildDepsMutator_missingDirectDep(t *testing.T) {
2140 bp := `
2141 custom {
2142 name: "foo",
2143 arch_paths: [":exists"],
2144 }
2145 custom {
2146 name: "exists",
2147 }
2148
2149 custom {
2150 name: "has_missing_dep",
2151 arch_paths: [":missing"],
2152 }
2153 `
2154 expectedBazelTargets := []string{
2155 MakeBazelTarget(
2156 "custom",
2157 "foo",
2158 AttrNameToString{"arch_paths": `[":exists"]`},
2159 ),
2160 MakeBazelTarget(
2161 "custom",
2162 "exists",
2163 AttrNameToString{},
2164 ),
2165 }
2166 registerCustomModule := func(ctx android.RegistrationContext) {
2167 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2168 }
2169 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2170 Blueprint: bp,
2171 ExpectedBazelTargets: expectedBazelTargets,
2172 Description: "Skipping conversion of a target with missing direct dep",
2173 DepsMutator: true,
2174 })
2175}
2176
2177func TestBp2buildDepsMutator_unconvertedDirectDep(t *testing.T) {
2178 bp := `
2179 custom {
2180 name: "has_unconverted_dep",
2181 arch_paths: [":unconvertible"],
2182 }
2183
2184 custom {
2185 name: "unconvertible",
2186 does_not_convert_to_bazel: true
2187 }
2188 `
2189 registerCustomModule := func(ctx android.RegistrationContext) {
2190 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2191 }
2192 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2193 Blueprint: bp,
2194 ExpectedBazelTargets: []string{},
2195 Description: "Skipping conversion of a target with unconverted direct dep",
2196 DepsMutator: true,
2197 })
2198}
2199
2200func TestBp2buildDepsMutator_unconvertedTransitiveDep(t *testing.T) {
2201 bp := `
2202 custom {
2203 name: "foo",
2204 arch_paths: [":has_unconverted_dep", ":bar"],
2205 }
2206
2207 custom {
2208 name: "bar",
2209 }
2210
2211 custom {
2212 name: "has_unconverted_dep",
2213 arch_paths: [":unconvertible"],
2214 }
2215
2216 custom {
2217 name: "unconvertible",
2218 does_not_convert_to_bazel: true
2219 }
2220 `
2221 expectedBazelTargets := []string{
2222 MakeBazelTarget(
2223 "custom",
2224 "bar",
2225 AttrNameToString{},
2226 ),
2227 }
2228 registerCustomModule := func(ctx android.RegistrationContext) {
2229 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2230 }
2231 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2232 Blueprint: bp,
2233 ExpectedBazelTargets: expectedBazelTargets,
2234 Description: "Skipping conversion of a target with unconverted transitive dep",
2235 DepsMutator: true,
2236 })
2237}
2238
2239func TestBp2buildDepsMutator_alreadyExistsBuildDeps(t *testing.T) {
2240 bp := `
2241 custom {
2242 name: "foo",
2243 arch_paths: [":bar"],
2244 }
2245 custom {
2246 name: "bar",
2247 arch_paths: [":checked_in"],
2248 }
2249 custom {
2250 name: "checked_in",
2251 arch_paths: [":checked_in"],
2252 does_not_convert_to_bazel: true
2253 }
2254 `
2255 expectedBazelTargets := []string{
2256 MakeBazelTarget(
2257 "custom",
2258 "foo",
2259 AttrNameToString{"arch_paths": `[":bar"]`},
2260 ),
2261 MakeBazelTarget(
2262 "custom",
2263 "bar",
2264 AttrNameToString{"arch_paths": `[":checked_in"]`},
2265 ),
2266 }
2267 registerCustomModule := func(ctx android.RegistrationContext) {
2268 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2269 }
2270 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2271 StubbedBuildDefinitions: []string{"checked_in"},
2272 Blueprint: bp,
2273 ExpectedBazelTargets: expectedBazelTargets,
2274 Description: "Convert target with already-existing build dep",
2275 DepsMutator: true,
2276 })
2277}
2278
2279// Tests that deps of libc are always considered valid for libc. This circumvents
2280// an issue that, in a variantless graph (such as bp2build's), libc has the
2281// unique predicament that it depends on itself.
2282func TestBp2buildDepsMutator_depOnLibc(t *testing.T) {
2283 bp := `
2284 custom {
2285 name: "foo",
2286 arch_paths: [":libc"],
2287 }
2288 custom {
2289 name: "libc",
2290 arch_paths: [":libc_dep"],
2291 }
2292 custom {
2293 name: "libc_dep",
2294 does_not_convert_to_bazel: true
2295 }
2296 `
2297 expectedBazelTargets := []string{
2298 MakeBazelTarget(
2299 "custom",
2300 "foo",
2301 AttrNameToString{"arch_paths": `[":libc"]`},
2302 ),
2303 MakeBazelTarget(
2304 "custom",
2305 "libc",
2306 AttrNameToString{"arch_paths": `[":libc_dep"]`},
2307 ),
2308 }
2309 registerCustomModule := func(ctx android.RegistrationContext) {
2310 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
2311 }
2312 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
2313 StubbedBuildDefinitions: []string{"checked_in"},
2314 Blueprint: bp,
2315 ExpectedBazelTargets: expectedBazelTargets,
2316 Description: "Convert target with dep on libc",
2317 DepsMutator: true,
2318 })
2319}