blob: 3887c5da928083f0efb3c0c2adfe500650077e47 [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{
776 name: "foo",
777 ruleClass: "cc_library",
778 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
779 },
780 },
781 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
782 },
783 {
784 bazelTargets: BazelTargets{
785 BazelTarget{
786 name: "foo",
787 ruleClass: "cc_library",
788 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
789 },
790 BazelTarget{
791 name: "bar",
792 ruleClass: "cc_library",
793 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
794 },
795 },
796 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
797 },
798 {
799 bazelTargets: BazelTargets{
800 BazelTarget{
801 name: "foo",
802 ruleClass: "cc_library",
803 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
804 },
805 BazelTarget{
806 name: "bar",
807 ruleClass: "cc_binary",
808 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
809 },
810 },
811 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
812 },
813 {
814 bazelTargets: BazelTargets{
815 BazelTarget{
816 name: "foo",
817 ruleClass: "cc_library",
818 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
819 },
820 BazelTarget{
821 name: "bar",
822 ruleClass: "cc_binary",
823 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
824 },
825 BazelTarget{
826 name: "baz",
827 ruleClass: "java_binary",
828 bzlLoadLocation: "//build/bazel/rules:java.bzl",
829 },
830 },
831 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
832load("//build/bazel/rules:java.bzl", "java_binary")`,
833 },
834 {
835 bazelTargets: BazelTargets{
836 BazelTarget{
837 name: "foo",
838 ruleClass: "cc_binary",
839 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
840 },
841 BazelTarget{
842 name: "bar",
843 ruleClass: "java_binary",
844 bzlLoadLocation: "//build/bazel/rules:java.bzl",
845 },
846 BazelTarget{
847 name: "baz",
848 ruleClass: "genrule",
849 // Note: no bzlLoadLocation for native rules
850 },
851 },
852 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
853load("//build/bazel/rules:java.bzl", "java_binary")`,
854 },
855 }
856
857 for _, testCase := range testCases {
858 actual := testCase.bazelTargets.LoadStatements()
859 expected := testCase.expectedLoadStatements
860 if actual != expected {
861 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
862 }
863 }
864
865}
866
867func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
868 testCases := []struct {
869 bp string
870 expectedBazelTarget string
871 expectedBazelTargetCount int
872 expectedLoadStatements string
873 }{
874 {
875 bp: `custom {
876 name: "bar",
Liz Kammerdfeb1202022-05-13 17:20:20 -0400877 host_supported: true,
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400878 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500879 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500880}`,
881 expectedBazelTarget: `my_library(
882 name = "bar",
883)
884
Jingwen Chen40067de2021-01-26 21:58:43 -0500885proto_library(
886 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400887)
888
889my_proto_library(
890 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500891)`,
892 expectedBazelTargetCount: 3,
893 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
894load("//build/bazel/rules:rules.bzl", "my_library")`,
895 },
896 }
897
898 dir := "."
899 for _, testCase := range testCases {
900 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
901 ctx := android.NewTestContext(config)
Liz Kammerdfeb1202022-05-13 17:20:20 -0400902 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
Jingwen Chen40067de2021-01-26 21:58:43 -0500903 ctx.RegisterForBazelConversion()
904
905 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
906 android.FailIfErrored(t, errs)
907 _, errs = ctx.ResolveDependencies(config)
908 android.FailIfErrored(t, errs)
909
Cole Faustb85d1a12022-11-08 18:14:01 -0800910 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammer6eff3232021-08-26 08:37:59 -0400911 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
912 android.FailIfErrored(t, err)
Jingwen Chen40067de2021-01-26 21:58:43 -0500913 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
914 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
915 }
916
917 actualBazelTargets := bazelTargets.String()
918 if actualBazelTargets != testCase.expectedBazelTarget {
919 t.Errorf(
920 "Expected generated Bazel target to be '%s', got '%s'",
921 testCase.expectedBazelTarget,
922 actualBazelTargets,
923 )
924 }
925
926 actualLoadStatements := bazelTargets.LoadStatements()
927 if actualLoadStatements != testCase.expectedLoadStatements {
928 t.Errorf(
929 "Expected generated load statements to be '%s', got '%s'",
930 testCase.expectedLoadStatements,
931 actualLoadStatements,
932 )
933 }
934 }
935}
936
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500937func TestModuleTypeBp2Build(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000938 testCases := []Bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500939 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000940 Description: "filegroup with does not specify srcs",
941 ModuleTypeUnderTest: "filegroup",
942 ModuleTypeUnderTestFactory: android.FileGroupFactory,
943 Blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500944 name: "fg_foo",
945 bazel_module: { bp2build_available: true },
946}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000947 ExpectedBazelTargets: []string{
948 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerebfcf672021-02-16 15:00:05 -0500949 },
950 },
951 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000952 Description: "filegroup with no srcs",
953 ModuleTypeUnderTest: "filegroup",
954 ModuleTypeUnderTestFactory: android.FileGroupFactory,
955 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500956 name: "fg_foo",
957 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500958 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500959}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000960 ExpectedBazelTargets: []string{
961 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammer356f7d42021-01-26 09:18:53 -0500962 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500963 },
964 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000965 Description: "filegroup with srcs",
966 ModuleTypeUnderTest: "filegroup",
967 ModuleTypeUnderTestFactory: android.FileGroupFactory,
968 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500969 name: "fg_foo",
970 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500971 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500972}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000973 ExpectedBazelTargets: []string{
974 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500975 "srcs": `[
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500976 "a",
977 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500978 ]`,
979 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500980 },
981 },
982 {
Usta Shresthad5580312022-09-23 16:46:38 -0400983 Description: "filegroup with dot-slash-prefixed srcs",
984 ModuleTypeUnderTest: "filegroup",
985 ModuleTypeUnderTestFactory: android.FileGroupFactory,
986 Blueprint: `filegroup {
987 name: "fg_foo",
988 srcs: ["./a", "./b"],
989 bazel_module: { bp2build_available: true },
990}`,
991 ExpectedBazelTargets: []string{
992 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
993 "srcs": `[
994 "a",
995 "b",
996 ]`,
997 }),
998 },
999 },
1000 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001001 Description: "filegroup with excludes srcs",
1002 ModuleTypeUnderTest: "filegroup",
1003 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1004 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -05001005 name: "fg_foo",
1006 srcs: ["a", "b"],
1007 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001008 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -05001009}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001010 ExpectedBazelTargets: []string{
1011 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001012 "srcs": `["b"]`,
1013 }),
Liz Kammer356f7d42021-01-26 09:18:53 -05001014 },
1015 },
1016 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001017 Description: "depends_on_other_dir_module",
1018 ModuleTypeUnderTest: "filegroup",
1019 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1020 Blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001021 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -05001022 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +00001023 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -05001024 "c",
1025 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001026 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -05001027}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001028 Filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -05001029 "other/Android.bp": `filegroup {
1030 name: "foo",
1031 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -04001032 bazel_module: { bp2build_available: true },
1033}`,
1034 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001035 ExpectedBazelTargets: []string{
1036 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001037 "srcs": `[
1038 "//other:foo",
1039 "c",
1040 ]`,
1041 }),
1042 },
Liz Kammer6eff3232021-08-26 08:37:59 -04001043 },
1044 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001045 Description: "depends_on_other_unconverted_module_error",
1046 ModuleTypeUnderTest: "filegroup",
1047 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1048 UnconvertedDepsMode: errorModulesUnconvertedDeps,
1049 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001050 name: "foobar",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001051 srcs: [
1052 ":foo",
1053 "c",
1054 ],
1055 bazel_module: { bp2build_available: true },
1056}`,
Sasha Smundakf2bb26f2022-08-04 11:28:15 -07001057 ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on unconverted modules: foo`),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001058 Filesystem: map[string]string{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001059 "other/Android.bp": `filegroup {
1060 name: "foo",
1061 srcs: ["a", "b"],
1062}`,
1063 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001064 },
Usta Shrestha808bc712022-09-23 23:18:18 -04001065 {
1066 Description: "depends_on_other_missing_module_error",
1067 ModuleTypeUnderTest: "filegroup",
1068 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1069 UnconvertedDepsMode: errorModulesUnconvertedDeps,
1070 Blueprint: `filegroup {
1071 name: "foobar",
1072 srcs: [
1073 "c",
1074 "//other:foo",
1075 "//other:goo",
1076 ],
1077 bazel_module: { bp2build_available: true },
1078}`,
1079 ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on missing modules: //other:goo`),
1080 Filesystem: map[string]string{"other/Android.bp": `filegroup {
1081 name: "foo",
1082 srcs: ["a"],
1083 bazel_module: { bp2build_available: true },
1084}
1085`,
1086 },
1087 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001088 }
1089
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001090 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001091 t.Run(testCase.Description, func(t *testing.T) {
1092 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
Liz Kammer6eff3232021-08-26 08:37:59 -04001093 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001094 }
1095}
Jingwen Chen041b1842021-02-01 00:23:25 -05001096
Jingwen Chen12b4c272021-03-10 02:05:59 -05001097func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001098 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001099 moduleTypeUnderTest string
1100 moduleTypeUnderTestFactory android.ModuleFactory
1101 bp string
1102 expectedCount int
1103 description string
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001104 }{
1105 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001106 description: "explicitly unavailable",
1107 moduleTypeUnderTest: "filegroup",
1108 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001109 bp: `filegroup {
1110 name: "foo",
1111 srcs: ["a", "b"],
1112 bazel_module: { bp2build_available: false },
1113}`,
1114 expectedCount: 0,
1115 },
1116 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001117 description: "implicitly unavailable",
1118 moduleTypeUnderTest: "filegroup",
1119 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001120 bp: `filegroup {
1121 name: "foo",
1122 srcs: ["a", "b"],
1123}`,
1124 expectedCount: 0,
1125 },
1126 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001127 description: "explicitly available",
1128 moduleTypeUnderTest: "filegroup",
1129 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001130 bp: `filegroup {
1131 name: "foo",
1132 srcs: ["a", "b"],
1133 bazel_module: { bp2build_available: true },
1134}`,
1135 expectedCount: 1,
1136 },
1137 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001138 description: "generates more than 1 target if needed",
1139 moduleTypeUnderTest: "custom",
Liz Kammerdfeb1202022-05-13 17:20:20 -04001140 moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001141 bp: `custom {
1142 name: "foo",
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001143 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001144 bazel_module: { bp2build_available: true },
1145}`,
1146 expectedCount: 3,
1147 },
1148 }
1149
1150 dir := "."
1151 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -04001152 t.Run(testCase.description, func(t *testing.T) {
1153 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1154 ctx := android.NewTestContext(config)
1155 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001156 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001157
Liz Kammer2ada09a2021-08-11 00:17:36 -04001158 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1159 android.FailIfErrored(t, errs)
1160 _, errs = ctx.ResolveDependencies(config)
1161 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001162
Cole Faustb85d1a12022-11-08 18:14:01 -08001163 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammer6eff3232021-08-26 08:37:59 -04001164 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1165 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001166 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1167 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1168 }
1169 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001170 }
1171}
Liz Kammerba3ea162021-02-17 13:22:03 -05001172
Jingwen Chen12b4c272021-03-10 02:05:59 -05001173func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1174 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001175 moduleTypeUnderTest string
1176 moduleTypeUnderTestFactory android.ModuleFactory
1177 expectedCount map[string]int
1178 description string
Sam Delmerico24c56032022-03-28 19:53:03 +00001179 bp2buildConfig allowlists.Bp2BuildConfig
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001180 checkDir string
1181 fs map[string]string
MarkDacek9c094ca2023-03-16 19:15:19 +00001182 forceEnabledModules []string
1183 expectedErrorMessages []string
Jingwen Chen12b4c272021-03-10 02:05:59 -05001184 }{
1185 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001186 description: "test bp2build config package and subpackages config",
1187 moduleTypeUnderTest: "filegroup",
1188 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001189 expectedCount: map[string]int{
1190 "migrated": 1,
1191 "migrated/but_not_really": 0,
1192 "migrated/but_not_really/but_really": 1,
1193 "not_migrated": 0,
1194 "also_not_migrated": 0,
1195 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001196 bp2buildConfig: allowlists.Bp2BuildConfig{
1197 "migrated": allowlists.Bp2BuildDefaultTrueRecursively,
1198 "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
1199 "not_migrated": allowlists.Bp2BuildDefaultFalse,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001200 },
1201 fs: map[string]string{
1202 "migrated/Android.bp": `filegroup { name: "a" }`,
1203 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1204 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1205 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1206 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1207 },
1208 },
1209 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001210 description: "test bp2build config opt-in and opt-out",
1211 moduleTypeUnderTest: "filegroup",
1212 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001213 expectedCount: map[string]int{
1214 "package-opt-in": 2,
1215 "package-opt-in/subpackage": 0,
1216 "package-opt-out": 1,
1217 "package-opt-out/subpackage": 0,
1218 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001219 bp2buildConfig: allowlists.Bp2BuildConfig{
1220 "package-opt-in": allowlists.Bp2BuildDefaultFalse,
1221 "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001222 },
1223 fs: map[string]string{
1224 "package-opt-in/Android.bp": `
1225filegroup { name: "opt-in-a" }
1226filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1227filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1228`,
1229
1230 "package-opt-in/subpackage/Android.bp": `
1231filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1232`,
1233
1234 "package-opt-out/Android.bp": `
1235filegroup { name: "opt-out-a" }
1236filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1237filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1238`,
1239
1240 "package-opt-out/subpackage/Android.bp": `
1241filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1242filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1243`,
1244 },
1245 },
MarkDacek9c094ca2023-03-16 19:15:19 +00001246 {
1247 description: "test force-enabled errors out",
1248 moduleTypeUnderTest: "filegroup",
1249 moduleTypeUnderTestFactory: android.FileGroupFactory,
1250 expectedCount: map[string]int{
1251 "migrated": 0,
1252 "not_migrated": 0,
1253 },
1254 bp2buildConfig: allowlists.Bp2BuildConfig{
1255 "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
1256 "not_migrated": allowlists.Bp2BuildDefaultFalse,
1257 },
1258 fs: map[string]string{
1259 "migrated/Android.bp": `filegroup { name: "a" }`,
1260 },
1261 forceEnabledModules: []string{"a"},
1262 expectedErrorMessages: []string{"Force Enabled Module a not converted"},
1263 },
Jingwen Chen12b4c272021-03-10 02:05:59 -05001264 }
1265
1266 dir := "."
1267 for _, testCase := range testCases {
1268 fs := make(map[string][]byte)
1269 toParse := []string{
1270 "Android.bp",
1271 }
1272 for f, content := range testCase.fs {
1273 if strings.HasSuffix(f, "Android.bp") {
1274 toParse = append(toParse, f)
1275 }
1276 fs[f] = []byte(content)
1277 }
1278 config := android.TestConfig(buildDir, nil, "", fs)
MarkDacek9c094ca2023-03-16 19:15:19 +00001279 config.AddForceEnabledModules(testCase.forceEnabledModules)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001280 ctx := android.NewTestContext(config)
1281 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Sam Delmerico24c56032022-03-28 19:53:03 +00001282 allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig)
1283 ctx.RegisterBp2BuildConfig(allowlist)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001284 ctx.RegisterForBazelConversion()
1285
1286 _, errs := ctx.ParseFileList(dir, toParse)
1287 android.FailIfErrored(t, errs)
1288 _, errs = ctx.ResolveDependencies(config)
1289 android.FailIfErrored(t, errs)
1290
Cole Faustb85d1a12022-11-08 18:14:01 -08001291 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Jingwen Chen12b4c272021-03-10 02:05:59 -05001292
1293 // For each directory, test that the expected number of generated targets is correct.
1294 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -04001295 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
MarkDacek9c094ca2023-03-16 19:15:19 +00001296 android.CheckErrorsAgainstExpectations(t, err, testCase.expectedErrorMessages)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001297 if actualCount := len(bazelTargets); actualCount != expectedCount {
1298 t.Fatalf(
1299 "%s: Expected %d bazel target for %s package, got %d",
1300 testCase.description,
1301 expectedCount,
1302 dir,
1303 actualCount)
1304 }
1305
1306 }
1307 }
1308}
1309
Liz Kammerba3ea162021-02-17 13:22:03 -05001310func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001311 testCases := []Bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -05001312 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001313 Description: "filegroup bazel_module.label",
1314 ModuleTypeUnderTest: "filegroup",
1315 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1316 Blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -05001317 name: "fg_foo",
1318 bazel_module: { label: "//other:fg_foo" },
1319}`,
Cole Faustea602c52022-08-31 14:48:26 -07001320 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001321 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001322 "other/BUILD.bazel": `// BUILD file`,
1323 },
1324 },
1325 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001326 Description: "multiple bazel_module.label same BUILD",
1327 ModuleTypeUnderTest: "filegroup",
1328 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1329 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001330 name: "fg_foo",
1331 bazel_module: { label: "//other:fg_foo" },
1332 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001333
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001334 filegroup {
1335 name: "foo",
1336 bazel_module: { label: "//other:foo" },
1337 }`,
Cole Faustea602c52022-08-31 14:48:26 -07001338 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001339 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001340 "other/BUILD.bazel": `// BUILD file`,
1341 },
1342 },
1343 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001344 Description: "filegroup bazel_module.label and bp2build in subdir",
1345 ModuleTypeUnderTest: "filegroup",
1346 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1347 Dir: "other",
1348 Blueprint: ``,
1349 Filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001350 "other/Android.bp": `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001351 name: "fg_foo",
1352 bazel_module: {
1353 bp2build_available: true,
1354 },
1355 }
1356 filegroup {
1357 name: "fg_bar",
1358 bazel_module: {
1359 label: "//other:fg_bar"
1360 },
1361 }`,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001362 "other/BUILD.bazel": `// definition for fg_bar`,
1363 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001364 ExpectedBazelTargets: []string{
1365 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001366 },
1367 },
1368 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001369 Description: "filegroup bazel_module.label and filegroup bp2build",
1370 ModuleTypeUnderTest: "filegroup",
1371 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001372
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001373 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001374 "other/BUILD.bazel": `// BUILD file`,
1375 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001376 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001377 name: "fg_foo",
1378 bazel_module: {
1379 label: "//other:fg_foo",
1380 },
1381 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001382
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001383 filegroup {
1384 name: "fg_bar",
1385 bazel_module: {
1386 bp2build_available: true,
1387 },
1388 }`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001389 ExpectedBazelTargets: []string{
1390 MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001391 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001392 },
1393 }
1394
1395 dir := "."
1396 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001397 t.Run(testCase.Description, func(t *testing.T) {
Jingwen Chen49109762021-05-25 05:16:48 +00001398 fs := make(map[string][]byte)
1399 toParse := []string{
1400 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001401 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001402 for f, content := range testCase.Filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001403 if strings.HasSuffix(f, "Android.bp") {
1404 toParse = append(toParse, f)
1405 }
1406 fs[f] = []byte(content)
1407 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001408 config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001409 ctx := android.NewTestContext(config)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001410 ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001411 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001412
Jingwen Chen49109762021-05-25 05:16:48 +00001413 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001414 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001415 return
1416 }
1417 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001418 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001419 return
1420 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001421
Jingwen Chen49109762021-05-25 05:16:48 +00001422 checkDir := dir
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001423 if testCase.Dir != "" {
1424 checkDir = testCase.Dir
Jingwen Chen49109762021-05-25 05:16:48 +00001425 }
Cole Faustb85d1a12022-11-08 18:14:01 -08001426 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammer6eff3232021-08-26 08:37:59 -04001427 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1428 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001429 bazelTargets.sort()
1430 actualCount := len(bazelTargets)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001431 expectedCount := len(testCase.ExpectedBazelTargets)
Jingwen Chen49109762021-05-25 05:16:48 +00001432 if actualCount != expectedCount {
1433 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1434 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001435 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001436 actualContent := target.content
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001437 expectedContent := testCase.ExpectedBazelTargets[i]
Jingwen Chen49109762021-05-25 05:16:48 +00001438 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001439 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001440 "Expected generated Bazel target to be '%s', got '%s'",
1441 expectedContent,
1442 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001443 )
1444 }
1445 }
Jingwen Chen49109762021-05-25 05:16:48 +00001446 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001447 }
1448}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001449
Jingwen Chen0eeaeb82022-09-21 10:27:42 +00001450func TestGlob(t *testing.T) {
1451 testCases := []Bp2buildTestCase{
1452 {
1453 Description: "filegroup with glob",
1454 ModuleTypeUnderTest: "filegroup",
1455 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1456 Blueprint: `filegroup {
1457 name: "fg_foo",
1458 srcs: ["**/*.txt"],
1459 bazel_module: { bp2build_available: true },
1460}`,
1461 ExpectedBazelTargets: []string{
1462 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1463 "srcs": `[
1464 "other/a.txt",
1465 "other/b.txt",
1466 "other/subdir/a.txt",
1467 ]`,
1468 }),
1469 },
1470 Filesystem: map[string]string{
1471 "other/a.txt": "",
1472 "other/b.txt": "",
1473 "other/subdir/a.txt": "",
1474 "other/file": "",
1475 },
1476 },
1477 {
1478 Description: "filegroup with glob in subdir",
1479 ModuleTypeUnderTest: "filegroup",
1480 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1481 Dir: "other",
1482 Filesystem: map[string]string{
1483 "other/Android.bp": `filegroup {
1484 name: "fg_foo",
1485 srcs: ["**/*.txt"],
1486 bazel_module: { bp2build_available: true },
1487}`,
1488 "other/a.txt": "",
1489 "other/b.txt": "",
1490 "other/subdir/a.txt": "",
1491 "other/file": "",
1492 },
1493 ExpectedBazelTargets: []string{
1494 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1495 "srcs": `[
1496 "a.txt",
1497 "b.txt",
1498 "subdir/a.txt",
1499 ]`,
1500 }),
1501 },
1502 },
1503 {
1504 Description: "filegroup with glob with no kept BUILD files",
1505 ModuleTypeUnderTest: "filegroup",
1506 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1507 KeepBuildFileForDirs: []string{
1508 // empty
1509 },
1510 Blueprint: `filegroup {
1511 name: "fg_foo",
1512 srcs: ["**/*.txt"],
1513 bazel_module: { bp2build_available: true },
1514}`,
1515 Filesystem: map[string]string{
1516 "a.txt": "",
1517 "b.txt": "",
1518 "foo/BUILD": "",
1519 "foo/a.txt": "",
1520 "foo/bar/BUILD": "",
1521 "foo/bar/b.txt": "",
1522 },
1523 ExpectedBazelTargets: []string{
1524 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1525 "srcs": `[
1526 "a.txt",
1527 "b.txt",
1528 "foo/a.txt",
1529 "foo/bar/b.txt",
1530 ]`,
1531 }),
1532 },
1533 },
1534 {
1535 Description: "filegroup with glob with kept BUILD file",
1536 ModuleTypeUnderTest: "filegroup",
1537 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1538 KeepBuildFileForDirs: []string{
1539 "foo",
1540 },
1541 Blueprint: `filegroup {
1542 name: "fg_foo",
1543 srcs: ["**/*.txt"],
1544 bazel_module: { bp2build_available: true },
1545}`,
1546 Filesystem: map[string]string{
1547 "a.txt": "",
1548 "b.txt": "",
1549 "foo/BUILD": "",
1550 "foo/a.txt": "",
1551 "foo/bar/BUILD": "",
1552 "foo/bar/b.txt": "",
1553 },
1554 ExpectedBazelTargets: []string{
1555 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1556 "srcs": `[
1557 "a.txt",
1558 "b.txt",
1559 "//foo:a.txt",
1560 "//foo:bar/b.txt",
1561 ]`,
1562 }),
1563 },
1564 },
1565 {
1566 Description: "filegroup with glob with kept BUILD.bazel file",
1567 ModuleTypeUnderTest: "filegroup",
1568 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1569 KeepBuildFileForDirs: []string{
1570 "foo",
1571 },
1572 Blueprint: `filegroup {
1573 name: "fg_foo",
1574 srcs: ["**/*.txt"],
1575 bazel_module: { bp2build_available: true },
1576}`,
1577 Filesystem: map[string]string{
1578 "a.txt": "",
1579 "b.txt": "",
1580 "foo/BUILD.bazel": "",
1581 "foo/a.txt": "",
1582 "foo/bar/BUILD.bazel": "",
1583 "foo/bar/b.txt": "",
1584 },
1585 ExpectedBazelTargets: []string{
1586 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1587 "srcs": `[
1588 "a.txt",
1589 "b.txt",
1590 "//foo:a.txt",
1591 "//foo:bar/b.txt",
1592 ]`,
1593 }),
1594 },
1595 },
1596 {
1597 Description: "filegroup with glob with Android.bp file as boundary",
1598 ModuleTypeUnderTest: "filegroup",
1599 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1600 Blueprint: `filegroup {
1601 name: "fg_foo",
1602 srcs: ["**/*.txt"],
1603 bazel_module: { bp2build_available: true },
1604}`,
1605 Filesystem: map[string]string{
1606 "a.txt": "",
1607 "b.txt": "",
1608 "foo/Android.bp": "",
1609 "foo/a.txt": "",
1610 "foo/bar/Android.bp": "",
1611 "foo/bar/b.txt": "",
1612 },
1613 ExpectedBazelTargets: []string{
1614 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1615 "srcs": `[
1616 "a.txt",
1617 "b.txt",
1618 "//foo:a.txt",
1619 "//foo/bar:b.txt",
1620 ]`,
1621 }),
1622 },
1623 },
1624 {
1625 Description: "filegroup with glob in subdir with kept BUILD and BUILD.bazel file",
1626 ModuleTypeUnderTest: "filegroup",
1627 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1628 Dir: "other",
1629 KeepBuildFileForDirs: []string{
1630 "other/foo",
1631 "other/foo/bar",
1632 // deliberately not other/foo/baz/BUILD.
1633 },
1634 Filesystem: map[string]string{
1635 "other/Android.bp": `filegroup {
1636 name: "fg_foo",
1637 srcs: ["**/*.txt"],
1638 bazel_module: { bp2build_available: true },
1639}`,
1640 "other/a.txt": "",
1641 "other/b.txt": "",
1642 "other/foo/BUILD": "",
1643 "other/foo/a.txt": "",
1644 "other/foo/bar/BUILD.bazel": "",
1645 "other/foo/bar/b.txt": "",
1646 "other/foo/baz/BUILD": "",
1647 "other/foo/baz/c.txt": "",
1648 },
1649 ExpectedBazelTargets: []string{
1650 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1651 "srcs": `[
1652 "a.txt",
1653 "b.txt",
1654 "//other/foo:a.txt",
1655 "//other/foo/bar:b.txt",
1656 "//other/foo:baz/c.txt",
1657 ]`,
1658 }),
1659 },
1660 },
1661 }
1662
1663 for _, testCase := range testCases {
1664 t.Run(testCase.Description, func(t *testing.T) {
1665 RunBp2BuildTestCaseSimple(t, testCase)
1666 })
1667 }
1668}
1669
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001670func TestGlobExcludeSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001671 testCases := []Bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001672 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001673 Description: "filegroup top level exclude_srcs",
1674 ModuleTypeUnderTest: "filegroup",
1675 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1676 Blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001677 name: "fg_foo",
1678 srcs: ["**/*.txt"],
1679 exclude_srcs: ["c.txt"],
1680 bazel_module: { bp2build_available: true },
1681}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001682 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001683 "a.txt": "",
1684 "b.txt": "",
1685 "c.txt": "",
1686 "dir/Android.bp": "",
1687 "dir/e.txt": "",
1688 "dir/f.txt": "",
1689 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001690 ExpectedBazelTargets: []string{
1691 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001692 "srcs": `[
1693 "a.txt",
1694 "b.txt",
1695 "//dir:e.txt",
1696 "//dir:f.txt",
1697 ]`,
1698 }),
1699 },
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001700 },
1701 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001702 Description: "filegroup in subdir exclude_srcs",
1703 ModuleTypeUnderTest: "filegroup",
1704 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1705 Blueprint: "",
1706 Dir: "dir",
1707 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001708 "dir/Android.bp": `filegroup {
1709 name: "fg_foo",
1710 srcs: ["**/*.txt"],
1711 exclude_srcs: ["b.txt"],
1712 bazel_module: { bp2build_available: true },
1713}
1714`,
1715 "dir/a.txt": "",
1716 "dir/b.txt": "",
1717 "dir/subdir/Android.bp": "",
1718 "dir/subdir/e.txt": "",
1719 "dir/subdir/f.txt": "",
1720 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001721 ExpectedBazelTargets: []string{
1722 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001723 "srcs": `[
Liz Kammer9abd62d2021-05-21 08:37:59 -04001724 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001725 "//dir/subdir:e.txt",
1726 "//dir/subdir:f.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001727 ]`,
1728 }),
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001729 },
1730 },
1731 }
1732
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001733 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001734 t.Run(testCase.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001735 RunBp2BuildTestCaseSimple(t, testCase)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001736 })
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001737 }
1738}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001739
1740func TestCommonBp2BuildModuleAttrs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001741 testCases := []Bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001742 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001743 Description: "Required into data test",
1744 ModuleTypeUnderTest: "filegroup",
1745 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1746 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001747filegroup {
1748 name: "fg_foo",
1749 required: ["reqd"],
1750 bazel_module: { bp2build_available: true },
1751}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001752 ExpectedBazelTargets: []string{
1753 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001754 "data": `[":reqd"]`,
1755 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001756 },
1757 },
1758 {
Jingwen Chena5ecb372022-09-21 09:05:37 +00001759 Description: "Required into data test, cyclic self reference is filtered out",
1760 ModuleTypeUnderTest: "filegroup",
1761 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1762 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
1763filegroup {
1764 name: "fg_foo",
1765 required: ["reqd", "fg_foo"],
1766 bazel_module: { bp2build_available: true },
1767}`,
1768 ExpectedBazelTargets: []string{
1769 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1770 "data": `[":reqd"]`,
1771 }),
1772 },
1773 },
1774 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001775 Description: "Required via arch into data test",
1776 ModuleTypeUnderTest: "python_library",
1777 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1778 Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") +
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001779 simpleModuleDoNotConvertBp2build("python_library", "reqdarm") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001780python_library {
1781 name: "fg_foo",
1782 arch: {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001783 arm: {
1784 required: ["reqdarm"],
1785 },
1786 x86: {
1787 required: ["reqdx86"],
1788 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001789 },
1790 bazel_module: { bp2build_available: true },
1791}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001792 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001793 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001794 "data": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001795 "//build/bazel/platforms/arch:arm": [":reqdarm"],
1796 "//build/bazel/platforms/arch:x86": [":reqdx86"],
1797 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001798 })`,
1799 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001800 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001801 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001802 },
1803 },
1804 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001805 Description: "Required appended to data test",
1806 ModuleTypeUnderTest: "python_library",
1807 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1808 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001809 "data.bin": "",
1810 "src.py": "",
1811 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001812 Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001813python_library {
1814 name: "fg_foo",
1815 data: ["data.bin"],
1816 required: ["reqd"],
1817 bazel_module: { bp2build_available: true },
1818}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001819 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001820 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001821 "data": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001822 "data.bin",
1823 ":reqd",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001824 ]`,
1825 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001826 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001827 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001828 },
1829 },
1830 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001831 Description: "All props-to-attrs at once together test",
1832 ModuleTypeUnderTest: "filegroup",
1833 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1834 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001835filegroup {
1836 name: "fg_foo",
1837 required: ["reqd"],
1838 bazel_module: { bp2build_available: true },
1839}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001840 ExpectedBazelTargets: []string{
1841 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001842 "data": `[":reqd"]`,
1843 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001844 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001845 },
1846 }
1847
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001848 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001849 t.Run(tc.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001850 RunBp2BuildTestCaseSimple(t, tc)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001851 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001852 }
1853}
Sasha Smundak05b0ba62022-09-26 18:15:45 -07001854
1855func TestLicensesAttrConversion(t *testing.T) {
1856 RunBp2BuildTestCase(t,
1857 func(ctx android.RegistrationContext) {
1858 ctx.RegisterModuleType("license", android.LicenseFactory)
1859 },
1860 Bp2buildTestCase{
1861 Description: "Test that licenses: attribute is converted",
1862 ModuleTypeUnderTest: "filegroup",
1863 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1864 Blueprint: `
1865license {
1866 name: "my_license",
1867}
1868filegroup {
1869 name: "my_filegroup",
1870 licenses: ["my_license"],
1871}
1872`,
1873 ExpectedBazelTargets: []string{
1874 MakeBazelTargetNoRestrictions("filegroup", "my_filegroup", AttrNameToString{
1875 "applicable_licenses": `[":my_license"]`,
1876 }),
1877 MakeBazelTargetNoRestrictions("android_license", "my_license", AttrNameToString{}),
1878 },
1879 })
1880}
Spandan Das5af0bd32022-09-28 20:43:08 +00001881
Spandan Das6a448ec2023-04-19 17:36:12 +00001882func TestGenerateConfigSetting(t *testing.T) {
1883 bp := `
1884 custom {
1885 name: "foo",
1886 test_config_setting: true,
1887 }
1888 `
1889 expectedBazelTargets := []string{
1890 MakeBazelTargetNoRestrictions(
1891 "config_setting",
1892 "foo_config_setting",
1893 AttrNameToString{
1894 "flag_values": `{
1895 "//build/bazel/rules/my_string_setting": "foo",
1896 }`,
1897 },
1898 ),
1899 MakeBazelTarget(
1900 "custom",
1901 "foo",
1902 AttrNameToString{},
1903 ),
1904 }
1905 registerCustomModule := func(ctx android.RegistrationContext) {
1906 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
1907 }
1908 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
1909 Blueprint: bp,
1910 ExpectedBazelTargets: expectedBazelTargets,
1911 Description: "Generating API contribution Bazel targets for custom module",
1912 })
1913}
Spandan Das921af322023-04-26 02:56:37 +00001914
1915// If values of all keys in an axis are equal to //conditions:default, drop the axis and print the common value
1916func TestPrettyPrintSelectMapEqualValues(t *testing.T) {
1917 lla := bazel.LabelListAttribute{
1918 Value: bazel.LabelList{},
1919 }
1920 libFooImplLabel := bazel.Label{
1921 Label: ":libfoo.impl",
1922 }
Spandan Das6d4d9da2023-04-18 06:20:40 +00001923 lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidPlatform, bazel.MakeLabelList([]bazel.Label{libFooImplLabel}))
Spandan Das921af322023-04-26 02:56:37 +00001924 lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.MakeLabelList([]bazel.Label{libFooImplLabel}))
1925 actual, _ := prettyPrintAttribute(lla, 0)
1926 android.AssertStringEquals(t, "Print the common value if all keys in an axis have the same value", `[":libfoo.impl"]`, actual)
1927}
Spandan Das3131d672023-08-03 22:33:47 +00001928
1929// If CommonAttributes.Dir is set, the bazel target should be created in that dir
1930func TestCreateBazelTargetInDifferentDir(t *testing.T) {
1931 t.Parallel()
1932 bp := `
1933 custom {
1934 name: "foo",
1935 dir: "subdir",
1936 }
1937 `
1938 registerCustomModule := func(ctx android.RegistrationContext) {
1939 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
1940 }
1941 // Check that foo is not created in root dir
1942 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
1943 Description: "foo is not created in root dir because it sets dir explicitly",
1944 Blueprint: bp,
1945 Filesystem: map[string]string{
1946 "subdir/Android.bp": "",
1947 },
1948 ExpectedBazelTargets: []string{},
1949 })
1950 // Check that foo is created in `subdir`
1951 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
1952 Description: "foo is created in `subdir` because it sets dir explicitly",
1953 Blueprint: bp,
1954 Filesystem: map[string]string{
1955 "subdir/Android.bp": "",
1956 },
1957 Dir: "subdir",
1958 ExpectedBazelTargets: []string{
1959 MakeBazelTarget("custom", "foo", AttrNameToString{}),
1960 },
1961 })
1962 // Check that we cannot create target in different dir if it is does not an Android.bp
1963 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
1964 Description: "foo cannot be created in `subdir` because it does not contain an Android.bp file",
1965 Blueprint: bp,
1966 Dir: "subdir",
1967 ExpectedErr: fmt.Errorf("Cannot use ca.Dir to create a BazelTarget in dir: subdir since it does not contain an Android.bp file"),
1968 })
1969
1970}