blob: e127fd542f3c1e482fe9ff81423c28046eb68bef [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{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400643 "target_compatible_with": `["//build/bazel/product_variables:unbundled_build"]`,
644 }),
645 },
646 },
647 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000648 Description: "host and device, neither, cannot override with arch enabled",
649 ModuleTypeUnderTest: "custom",
650 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
651 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400652 name: "foo",
653 host_supported: false,
654 device_supported: false,
655 arch: { x86: { enabled: true } },
656 bazel_module: { bp2build_available: true },
657}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000658 ExpectedBazelTargets: []string{
659 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400660 "target_compatible_with": `["@platforms//:incompatible"]`,
661 }),
662 },
663 },
664 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000665 Description: "host and device, host only",
666 ModuleTypeUnderTest: "custom",
667 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
668 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400669 name: "foo",
670 host_supported: true,
671 device_supported: false,
672 bazel_module: { bp2build_available: true },
673}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000674 ExpectedBazelTargets: []string{
675 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400676 },
677 },
678 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000679 Description: "host only",
680 ModuleTypeUnderTest: "custom",
681 ModuleTypeUnderTestFactory: customModuleFactoryHostSupported,
682 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400683 name: "foo",
684 bazel_module: { bp2build_available: true },
685}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000686 ExpectedBazelTargets: []string{
687 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400688 },
689 },
690 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000691 Description: "device only",
692 ModuleTypeUnderTest: "custom",
693 ModuleTypeUnderTestFactory: customModuleFactoryDeviceSupported,
694 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400695 name: "foo",
696 bazel_module: { bp2build_available: true },
697}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000698 ExpectedBazelTargets: []string{
699 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400700 },
701 },
702 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000703 Description: "host and device default, default",
704 ModuleTypeUnderTest: "custom",
705 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
706 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400707 name: "foo",
708 bazel_module: { bp2build_available: true },
709}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000710 ExpectedBazelTargets: []string{
711 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400712 },
713 },
714 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000715 Description: "host and device default, device only",
716 ModuleTypeUnderTest: "custom",
717 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
718 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400719 name: "foo",
720 host_supported: false,
721 bazel_module: { bp2build_available: true },
722}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000723 ExpectedBazelTargets: []string{
724 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400725 },
726 },
727 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000728 Description: "host and device default, host only",
729 ModuleTypeUnderTest: "custom",
730 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
731 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400732 name: "foo",
733 device_supported: false,
734 bazel_module: { bp2build_available: true },
735}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000736 ExpectedBazelTargets: []string{
737 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400738 },
739 },
740 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000741 Description: "host and device default, neither",
742 ModuleTypeUnderTest: "custom",
743 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
744 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400745 name: "foo",
746 host_supported: false,
747 device_supported: false,
748 bazel_module: { bp2build_available: true },
749}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000750 ExpectedBazelTargets: []string{
751 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400752 "target_compatible_with": `["@platforms//:incompatible"]`,
753 }),
754 },
755 },
756 }
757
758 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000759 t.Run(tc.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +0000760 RunBp2BuildTestCaseSimple(t, tc)
Liz Kammerdfeb1202022-05-13 17:20:20 -0400761 })
762 }
763}
764
Jingwen Chen40067de2021-01-26 21:58:43 -0500765func TestLoadStatements(t *testing.T) {
766 testCases := []struct {
767 bazelTargets BazelTargets
768 expectedLoadStatements string
769 }{
770 {
771 bazelTargets: BazelTargets{
772 BazelTarget{
773 name: "foo",
774 ruleClass: "cc_library",
775 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
776 },
777 },
778 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
779 },
780 {
781 bazelTargets: BazelTargets{
782 BazelTarget{
783 name: "foo",
784 ruleClass: "cc_library",
785 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
786 },
787 BazelTarget{
788 name: "bar",
789 ruleClass: "cc_library",
790 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
791 },
792 },
793 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
794 },
795 {
796 bazelTargets: BazelTargets{
797 BazelTarget{
798 name: "foo",
799 ruleClass: "cc_library",
800 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
801 },
802 BazelTarget{
803 name: "bar",
804 ruleClass: "cc_binary",
805 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
806 },
807 },
808 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
809 },
810 {
811 bazelTargets: BazelTargets{
812 BazelTarget{
813 name: "foo",
814 ruleClass: "cc_library",
815 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
816 },
817 BazelTarget{
818 name: "bar",
819 ruleClass: "cc_binary",
820 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
821 },
822 BazelTarget{
823 name: "baz",
824 ruleClass: "java_binary",
825 bzlLoadLocation: "//build/bazel/rules:java.bzl",
826 },
827 },
828 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
829load("//build/bazel/rules:java.bzl", "java_binary")`,
830 },
831 {
832 bazelTargets: BazelTargets{
833 BazelTarget{
834 name: "foo",
835 ruleClass: "cc_binary",
836 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
837 },
838 BazelTarget{
839 name: "bar",
840 ruleClass: "java_binary",
841 bzlLoadLocation: "//build/bazel/rules:java.bzl",
842 },
843 BazelTarget{
844 name: "baz",
845 ruleClass: "genrule",
846 // Note: no bzlLoadLocation for native rules
847 },
848 },
849 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
850load("//build/bazel/rules:java.bzl", "java_binary")`,
851 },
852 }
853
854 for _, testCase := range testCases {
855 actual := testCase.bazelTargets.LoadStatements()
856 expected := testCase.expectedLoadStatements
857 if actual != expected {
858 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
859 }
860 }
861
862}
863
864func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
865 testCases := []struct {
866 bp string
867 expectedBazelTarget string
868 expectedBazelTargetCount int
869 expectedLoadStatements string
870 }{
871 {
872 bp: `custom {
873 name: "bar",
Liz Kammerdfeb1202022-05-13 17:20:20 -0400874 host_supported: true,
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400875 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500876 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500877}`,
878 expectedBazelTarget: `my_library(
879 name = "bar",
880)
881
Jingwen Chen40067de2021-01-26 21:58:43 -0500882proto_library(
883 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400884)
885
886my_proto_library(
887 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500888)`,
889 expectedBazelTargetCount: 3,
890 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
891load("//build/bazel/rules:rules.bzl", "my_library")`,
892 },
893 }
894
895 dir := "."
896 for _, testCase := range testCases {
897 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
898 ctx := android.NewTestContext(config)
Liz Kammerdfeb1202022-05-13 17:20:20 -0400899 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
Jingwen Chen40067de2021-01-26 21:58:43 -0500900 ctx.RegisterForBazelConversion()
901
902 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
903 android.FailIfErrored(t, errs)
904 _, errs = ctx.ResolveDependencies(config)
905 android.FailIfErrored(t, errs)
906
Cole Faustb85d1a12022-11-08 18:14:01 -0800907 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammer6eff3232021-08-26 08:37:59 -0400908 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
909 android.FailIfErrored(t, err)
Jingwen Chen40067de2021-01-26 21:58:43 -0500910 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
911 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
912 }
913
914 actualBazelTargets := bazelTargets.String()
915 if actualBazelTargets != testCase.expectedBazelTarget {
916 t.Errorf(
917 "Expected generated Bazel target to be '%s', got '%s'",
918 testCase.expectedBazelTarget,
919 actualBazelTargets,
920 )
921 }
922
923 actualLoadStatements := bazelTargets.LoadStatements()
924 if actualLoadStatements != testCase.expectedLoadStatements {
925 t.Errorf(
926 "Expected generated load statements to be '%s', got '%s'",
927 testCase.expectedLoadStatements,
928 actualLoadStatements,
929 )
930 }
931 }
932}
933
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500934func TestModuleTypeBp2Build(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000935 testCases := []Bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500936 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000937 Description: "filegroup with does not specify srcs",
938 ModuleTypeUnderTest: "filegroup",
939 ModuleTypeUnderTestFactory: android.FileGroupFactory,
940 Blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500941 name: "fg_foo",
942 bazel_module: { bp2build_available: true },
943}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000944 ExpectedBazelTargets: []string{
945 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerebfcf672021-02-16 15:00:05 -0500946 },
947 },
948 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000949 Description: "filegroup with no srcs",
950 ModuleTypeUnderTest: "filegroup",
951 ModuleTypeUnderTestFactory: android.FileGroupFactory,
952 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500953 name: "fg_foo",
954 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500955 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500956}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000957 ExpectedBazelTargets: []string{
958 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammer356f7d42021-01-26 09:18:53 -0500959 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500960 },
961 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000962 Description: "filegroup with srcs",
963 ModuleTypeUnderTest: "filegroup",
964 ModuleTypeUnderTestFactory: android.FileGroupFactory,
965 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500966 name: "fg_foo",
967 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500968 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500969}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000970 ExpectedBazelTargets: []string{
971 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500972 "srcs": `[
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500973 "a",
974 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500975 ]`,
976 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500977 },
978 },
979 {
Usta Shresthad5580312022-09-23 16:46:38 -0400980 Description: "filegroup with dot-slash-prefixed srcs",
981 ModuleTypeUnderTest: "filegroup",
982 ModuleTypeUnderTestFactory: android.FileGroupFactory,
983 Blueprint: `filegroup {
984 name: "fg_foo",
985 srcs: ["./a", "./b"],
986 bazel_module: { bp2build_available: true },
987}`,
988 ExpectedBazelTargets: []string{
989 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
990 "srcs": `[
991 "a",
992 "b",
993 ]`,
994 }),
995 },
996 },
997 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000998 Description: "filegroup with excludes srcs",
999 ModuleTypeUnderTest: "filegroup",
1000 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1001 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -05001002 name: "fg_foo",
1003 srcs: ["a", "b"],
1004 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001005 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -05001006}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001007 ExpectedBazelTargets: []string{
1008 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001009 "srcs": `["b"]`,
1010 }),
Liz Kammer356f7d42021-01-26 09:18:53 -05001011 },
1012 },
1013 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001014 Description: "depends_on_other_dir_module",
1015 ModuleTypeUnderTest: "filegroup",
1016 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1017 Blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001018 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -05001019 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +00001020 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -05001021 "c",
1022 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001023 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -05001024}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001025 Filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -05001026 "other/Android.bp": `filegroup {
1027 name: "foo",
1028 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -04001029 bazel_module: { bp2build_available: true },
1030}`,
1031 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001032 ExpectedBazelTargets: []string{
1033 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001034 "srcs": `[
1035 "//other:foo",
1036 "c",
1037 ]`,
1038 }),
1039 },
Liz Kammer6eff3232021-08-26 08:37:59 -04001040 },
1041 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001042 Description: "depends_on_other_unconverted_module_error",
1043 ModuleTypeUnderTest: "filegroup",
1044 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1045 UnconvertedDepsMode: errorModulesUnconvertedDeps,
1046 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001047 name: "foobar",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001048 srcs: [
1049 ":foo",
1050 "c",
1051 ],
1052 bazel_module: { bp2build_available: true },
1053}`,
Sasha Smundakf2bb26f2022-08-04 11:28:15 -07001054 ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on unconverted modules: foo`),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001055 Filesystem: map[string]string{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001056 "other/Android.bp": `filegroup {
1057 name: "foo",
1058 srcs: ["a", "b"],
1059}`,
1060 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001061 },
Usta Shrestha808bc712022-09-23 23:18:18 -04001062 {
1063 Description: "depends_on_other_missing_module_error",
1064 ModuleTypeUnderTest: "filegroup",
1065 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1066 UnconvertedDepsMode: errorModulesUnconvertedDeps,
1067 Blueprint: `filegroup {
1068 name: "foobar",
1069 srcs: [
1070 "c",
1071 "//other:foo",
1072 "//other:goo",
1073 ],
1074 bazel_module: { bp2build_available: true },
1075}`,
1076 ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on missing modules: //other:goo`),
1077 Filesystem: map[string]string{"other/Android.bp": `filegroup {
1078 name: "foo",
1079 srcs: ["a"],
1080 bazel_module: { bp2build_available: true },
1081}
1082`,
1083 },
1084 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001085 }
1086
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001087 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001088 t.Run(testCase.Description, func(t *testing.T) {
1089 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
Liz Kammer6eff3232021-08-26 08:37:59 -04001090 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001091 }
1092}
Jingwen Chen041b1842021-02-01 00:23:25 -05001093
Jingwen Chen12b4c272021-03-10 02:05:59 -05001094func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001095 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001096 moduleTypeUnderTest string
1097 moduleTypeUnderTestFactory android.ModuleFactory
1098 bp string
1099 expectedCount int
1100 description string
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001101 }{
1102 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001103 description: "explicitly unavailable",
1104 moduleTypeUnderTest: "filegroup",
1105 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001106 bp: `filegroup {
1107 name: "foo",
1108 srcs: ["a", "b"],
1109 bazel_module: { bp2build_available: false },
1110}`,
1111 expectedCount: 0,
1112 },
1113 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001114 description: "implicitly unavailable",
1115 moduleTypeUnderTest: "filegroup",
1116 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001117 bp: `filegroup {
1118 name: "foo",
1119 srcs: ["a", "b"],
1120}`,
1121 expectedCount: 0,
1122 },
1123 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001124 description: "explicitly available",
1125 moduleTypeUnderTest: "filegroup",
1126 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001127 bp: `filegroup {
1128 name: "foo",
1129 srcs: ["a", "b"],
1130 bazel_module: { bp2build_available: true },
1131}`,
1132 expectedCount: 1,
1133 },
1134 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001135 description: "generates more than 1 target if needed",
1136 moduleTypeUnderTest: "custom",
Liz Kammerdfeb1202022-05-13 17:20:20 -04001137 moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001138 bp: `custom {
1139 name: "foo",
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001140 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001141 bazel_module: { bp2build_available: true },
1142}`,
1143 expectedCount: 3,
1144 },
1145 }
1146
1147 dir := "."
1148 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -04001149 t.Run(testCase.description, func(t *testing.T) {
1150 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1151 ctx := android.NewTestContext(config)
1152 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001153 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001154
Liz Kammer2ada09a2021-08-11 00:17:36 -04001155 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1156 android.FailIfErrored(t, errs)
1157 _, errs = ctx.ResolveDependencies(config)
1158 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001159
Cole Faustb85d1a12022-11-08 18:14:01 -08001160 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammer6eff3232021-08-26 08:37:59 -04001161 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1162 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001163 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1164 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1165 }
1166 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001167 }
1168}
Liz Kammerba3ea162021-02-17 13:22:03 -05001169
Jingwen Chen12b4c272021-03-10 02:05:59 -05001170func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1171 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001172 moduleTypeUnderTest string
1173 moduleTypeUnderTestFactory android.ModuleFactory
1174 expectedCount map[string]int
1175 description string
Sam Delmerico24c56032022-03-28 19:53:03 +00001176 bp2buildConfig allowlists.Bp2BuildConfig
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001177 checkDir string
1178 fs map[string]string
MarkDacek9c094ca2023-03-16 19:15:19 +00001179 forceEnabledModules []string
1180 expectedErrorMessages []string
Jingwen Chen12b4c272021-03-10 02:05:59 -05001181 }{
1182 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001183 description: "test bp2build config package and subpackages config",
1184 moduleTypeUnderTest: "filegroup",
1185 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001186 expectedCount: map[string]int{
1187 "migrated": 1,
1188 "migrated/but_not_really": 0,
1189 "migrated/but_not_really/but_really": 1,
1190 "not_migrated": 0,
1191 "also_not_migrated": 0,
1192 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001193 bp2buildConfig: allowlists.Bp2BuildConfig{
1194 "migrated": allowlists.Bp2BuildDefaultTrueRecursively,
1195 "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
1196 "not_migrated": allowlists.Bp2BuildDefaultFalse,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001197 },
1198 fs: map[string]string{
1199 "migrated/Android.bp": `filegroup { name: "a" }`,
1200 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1201 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1202 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1203 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1204 },
1205 },
1206 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001207 description: "test bp2build config opt-in and opt-out",
1208 moduleTypeUnderTest: "filegroup",
1209 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001210 expectedCount: map[string]int{
1211 "package-opt-in": 2,
1212 "package-opt-in/subpackage": 0,
1213 "package-opt-out": 1,
1214 "package-opt-out/subpackage": 0,
1215 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001216 bp2buildConfig: allowlists.Bp2BuildConfig{
1217 "package-opt-in": allowlists.Bp2BuildDefaultFalse,
1218 "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001219 },
1220 fs: map[string]string{
1221 "package-opt-in/Android.bp": `
1222filegroup { name: "opt-in-a" }
1223filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1224filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1225`,
1226
1227 "package-opt-in/subpackage/Android.bp": `
1228filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1229`,
1230
1231 "package-opt-out/Android.bp": `
1232filegroup { name: "opt-out-a" }
1233filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1234filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1235`,
1236
1237 "package-opt-out/subpackage/Android.bp": `
1238filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1239filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1240`,
1241 },
1242 },
MarkDacek9c094ca2023-03-16 19:15:19 +00001243 {
1244 description: "test force-enabled errors out",
1245 moduleTypeUnderTest: "filegroup",
1246 moduleTypeUnderTestFactory: android.FileGroupFactory,
1247 expectedCount: map[string]int{
1248 "migrated": 0,
1249 "not_migrated": 0,
1250 },
1251 bp2buildConfig: allowlists.Bp2BuildConfig{
1252 "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
1253 "not_migrated": allowlists.Bp2BuildDefaultFalse,
1254 },
1255 fs: map[string]string{
1256 "migrated/Android.bp": `filegroup { name: "a" }`,
1257 },
1258 forceEnabledModules: []string{"a"},
1259 expectedErrorMessages: []string{"Force Enabled Module a not converted"},
1260 },
Jingwen Chen12b4c272021-03-10 02:05:59 -05001261 }
1262
1263 dir := "."
1264 for _, testCase := range testCases {
1265 fs := make(map[string][]byte)
1266 toParse := []string{
1267 "Android.bp",
1268 }
1269 for f, content := range testCase.fs {
1270 if strings.HasSuffix(f, "Android.bp") {
1271 toParse = append(toParse, f)
1272 }
1273 fs[f] = []byte(content)
1274 }
1275 config := android.TestConfig(buildDir, nil, "", fs)
MarkDacek9c094ca2023-03-16 19:15:19 +00001276 config.AddForceEnabledModules(testCase.forceEnabledModules)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001277 ctx := android.NewTestContext(config)
1278 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Sam Delmerico24c56032022-03-28 19:53:03 +00001279 allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig)
1280 ctx.RegisterBp2BuildConfig(allowlist)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001281 ctx.RegisterForBazelConversion()
1282
1283 _, errs := ctx.ParseFileList(dir, toParse)
1284 android.FailIfErrored(t, errs)
1285 _, errs = ctx.ResolveDependencies(config)
1286 android.FailIfErrored(t, errs)
1287
Cole Faustb85d1a12022-11-08 18:14:01 -08001288 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Jingwen Chen12b4c272021-03-10 02:05:59 -05001289
1290 // For each directory, test that the expected number of generated targets is correct.
1291 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -04001292 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
MarkDacek9c094ca2023-03-16 19:15:19 +00001293 android.CheckErrorsAgainstExpectations(t, err, testCase.expectedErrorMessages)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001294 if actualCount := len(bazelTargets); actualCount != expectedCount {
1295 t.Fatalf(
1296 "%s: Expected %d bazel target for %s package, got %d",
1297 testCase.description,
1298 expectedCount,
1299 dir,
1300 actualCount)
1301 }
1302
1303 }
1304 }
1305}
1306
Liz Kammerba3ea162021-02-17 13:22:03 -05001307func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001308 testCases := []Bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -05001309 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001310 Description: "filegroup bazel_module.label",
1311 ModuleTypeUnderTest: "filegroup",
1312 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1313 Blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -05001314 name: "fg_foo",
1315 bazel_module: { label: "//other:fg_foo" },
1316}`,
Cole Faustea602c52022-08-31 14:48:26 -07001317 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001318 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001319 "other/BUILD.bazel": `// BUILD file`,
1320 },
1321 },
1322 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001323 Description: "multiple bazel_module.label same BUILD",
1324 ModuleTypeUnderTest: "filegroup",
1325 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1326 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001327 name: "fg_foo",
1328 bazel_module: { label: "//other:fg_foo" },
1329 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001330
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001331 filegroup {
1332 name: "foo",
1333 bazel_module: { label: "//other:foo" },
1334 }`,
Cole Faustea602c52022-08-31 14:48:26 -07001335 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001336 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001337 "other/BUILD.bazel": `// BUILD file`,
1338 },
1339 },
1340 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001341 Description: "filegroup bazel_module.label and bp2build in subdir",
1342 ModuleTypeUnderTest: "filegroup",
1343 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1344 Dir: "other",
1345 Blueprint: ``,
1346 Filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001347 "other/Android.bp": `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001348 name: "fg_foo",
1349 bazel_module: {
1350 bp2build_available: true,
1351 },
1352 }
1353 filegroup {
1354 name: "fg_bar",
1355 bazel_module: {
1356 label: "//other:fg_bar"
1357 },
1358 }`,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001359 "other/BUILD.bazel": `// definition for fg_bar`,
1360 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001361 ExpectedBazelTargets: []string{
1362 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001363 },
1364 },
1365 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001366 Description: "filegroup bazel_module.label and filegroup bp2build",
1367 ModuleTypeUnderTest: "filegroup",
1368 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001369
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001370 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001371 "other/BUILD.bazel": `// BUILD file`,
1372 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001373 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001374 name: "fg_foo",
1375 bazel_module: {
1376 label: "//other:fg_foo",
1377 },
1378 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001379
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001380 filegroup {
1381 name: "fg_bar",
1382 bazel_module: {
1383 bp2build_available: true,
1384 },
1385 }`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001386 ExpectedBazelTargets: []string{
1387 MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001388 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001389 },
1390 }
1391
1392 dir := "."
1393 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001394 t.Run(testCase.Description, func(t *testing.T) {
Jingwen Chen49109762021-05-25 05:16:48 +00001395 fs := make(map[string][]byte)
1396 toParse := []string{
1397 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001398 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001399 for f, content := range testCase.Filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001400 if strings.HasSuffix(f, "Android.bp") {
1401 toParse = append(toParse, f)
1402 }
1403 fs[f] = []byte(content)
1404 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001405 config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001406 ctx := android.NewTestContext(config)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001407 ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001408 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001409
Jingwen Chen49109762021-05-25 05:16:48 +00001410 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001411 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001412 return
1413 }
1414 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001415 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001416 return
1417 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001418
Jingwen Chen49109762021-05-25 05:16:48 +00001419 checkDir := dir
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001420 if testCase.Dir != "" {
1421 checkDir = testCase.Dir
Jingwen Chen49109762021-05-25 05:16:48 +00001422 }
Cole Faustb85d1a12022-11-08 18:14:01 -08001423 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammer6eff3232021-08-26 08:37:59 -04001424 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1425 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001426 bazelTargets.sort()
1427 actualCount := len(bazelTargets)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001428 expectedCount := len(testCase.ExpectedBazelTargets)
Jingwen Chen49109762021-05-25 05:16:48 +00001429 if actualCount != expectedCount {
1430 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1431 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001432 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001433 actualContent := target.content
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001434 expectedContent := testCase.ExpectedBazelTargets[i]
Jingwen Chen49109762021-05-25 05:16:48 +00001435 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001436 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001437 "Expected generated Bazel target to be '%s', got '%s'",
1438 expectedContent,
1439 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001440 )
1441 }
1442 }
Jingwen Chen49109762021-05-25 05:16:48 +00001443 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001444 }
1445}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001446
Jingwen Chen0eeaeb82022-09-21 10:27:42 +00001447func TestGlob(t *testing.T) {
1448 testCases := []Bp2buildTestCase{
1449 {
1450 Description: "filegroup with glob",
1451 ModuleTypeUnderTest: "filegroup",
1452 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1453 Blueprint: `filegroup {
1454 name: "fg_foo",
1455 srcs: ["**/*.txt"],
1456 bazel_module: { bp2build_available: true },
1457}`,
1458 ExpectedBazelTargets: []string{
1459 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1460 "srcs": `[
1461 "other/a.txt",
1462 "other/b.txt",
1463 "other/subdir/a.txt",
1464 ]`,
1465 }),
1466 },
1467 Filesystem: map[string]string{
1468 "other/a.txt": "",
1469 "other/b.txt": "",
1470 "other/subdir/a.txt": "",
1471 "other/file": "",
1472 },
1473 },
1474 {
1475 Description: "filegroup with glob in subdir",
1476 ModuleTypeUnderTest: "filegroup",
1477 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1478 Dir: "other",
1479 Filesystem: map[string]string{
1480 "other/Android.bp": `filegroup {
1481 name: "fg_foo",
1482 srcs: ["**/*.txt"],
1483 bazel_module: { bp2build_available: true },
1484}`,
1485 "other/a.txt": "",
1486 "other/b.txt": "",
1487 "other/subdir/a.txt": "",
1488 "other/file": "",
1489 },
1490 ExpectedBazelTargets: []string{
1491 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1492 "srcs": `[
1493 "a.txt",
1494 "b.txt",
1495 "subdir/a.txt",
1496 ]`,
1497 }),
1498 },
1499 },
1500 {
1501 Description: "filegroup with glob with no kept BUILD files",
1502 ModuleTypeUnderTest: "filegroup",
1503 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1504 KeepBuildFileForDirs: []string{
1505 // empty
1506 },
1507 Blueprint: `filegroup {
1508 name: "fg_foo",
1509 srcs: ["**/*.txt"],
1510 bazel_module: { bp2build_available: true },
1511}`,
1512 Filesystem: map[string]string{
1513 "a.txt": "",
1514 "b.txt": "",
1515 "foo/BUILD": "",
1516 "foo/a.txt": "",
1517 "foo/bar/BUILD": "",
1518 "foo/bar/b.txt": "",
1519 },
1520 ExpectedBazelTargets: []string{
1521 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1522 "srcs": `[
1523 "a.txt",
1524 "b.txt",
1525 "foo/a.txt",
1526 "foo/bar/b.txt",
1527 ]`,
1528 }),
1529 },
1530 },
1531 {
1532 Description: "filegroup with glob with kept BUILD file",
1533 ModuleTypeUnderTest: "filegroup",
1534 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1535 KeepBuildFileForDirs: []string{
1536 "foo",
1537 },
1538 Blueprint: `filegroup {
1539 name: "fg_foo",
1540 srcs: ["**/*.txt"],
1541 bazel_module: { bp2build_available: true },
1542}`,
1543 Filesystem: map[string]string{
1544 "a.txt": "",
1545 "b.txt": "",
1546 "foo/BUILD": "",
1547 "foo/a.txt": "",
1548 "foo/bar/BUILD": "",
1549 "foo/bar/b.txt": "",
1550 },
1551 ExpectedBazelTargets: []string{
1552 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1553 "srcs": `[
1554 "a.txt",
1555 "b.txt",
1556 "//foo:a.txt",
1557 "//foo:bar/b.txt",
1558 ]`,
1559 }),
1560 },
1561 },
1562 {
1563 Description: "filegroup with glob with kept BUILD.bazel file",
1564 ModuleTypeUnderTest: "filegroup",
1565 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1566 KeepBuildFileForDirs: []string{
1567 "foo",
1568 },
1569 Blueprint: `filegroup {
1570 name: "fg_foo",
1571 srcs: ["**/*.txt"],
1572 bazel_module: { bp2build_available: true },
1573}`,
1574 Filesystem: map[string]string{
1575 "a.txt": "",
1576 "b.txt": "",
1577 "foo/BUILD.bazel": "",
1578 "foo/a.txt": "",
1579 "foo/bar/BUILD.bazel": "",
1580 "foo/bar/b.txt": "",
1581 },
1582 ExpectedBazelTargets: []string{
1583 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1584 "srcs": `[
1585 "a.txt",
1586 "b.txt",
1587 "//foo:a.txt",
1588 "//foo:bar/b.txt",
1589 ]`,
1590 }),
1591 },
1592 },
1593 {
1594 Description: "filegroup with glob with Android.bp file as boundary",
1595 ModuleTypeUnderTest: "filegroup",
1596 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1597 Blueprint: `filegroup {
1598 name: "fg_foo",
1599 srcs: ["**/*.txt"],
1600 bazel_module: { bp2build_available: true },
1601}`,
1602 Filesystem: map[string]string{
1603 "a.txt": "",
1604 "b.txt": "",
1605 "foo/Android.bp": "",
1606 "foo/a.txt": "",
1607 "foo/bar/Android.bp": "",
1608 "foo/bar/b.txt": "",
1609 },
1610 ExpectedBazelTargets: []string{
1611 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1612 "srcs": `[
1613 "a.txt",
1614 "b.txt",
1615 "//foo:a.txt",
1616 "//foo/bar:b.txt",
1617 ]`,
1618 }),
1619 },
1620 },
1621 {
1622 Description: "filegroup with glob in subdir with kept BUILD and BUILD.bazel file",
1623 ModuleTypeUnderTest: "filegroup",
1624 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1625 Dir: "other",
1626 KeepBuildFileForDirs: []string{
1627 "other/foo",
1628 "other/foo/bar",
1629 // deliberately not other/foo/baz/BUILD.
1630 },
1631 Filesystem: map[string]string{
1632 "other/Android.bp": `filegroup {
1633 name: "fg_foo",
1634 srcs: ["**/*.txt"],
1635 bazel_module: { bp2build_available: true },
1636}`,
1637 "other/a.txt": "",
1638 "other/b.txt": "",
1639 "other/foo/BUILD": "",
1640 "other/foo/a.txt": "",
1641 "other/foo/bar/BUILD.bazel": "",
1642 "other/foo/bar/b.txt": "",
1643 "other/foo/baz/BUILD": "",
1644 "other/foo/baz/c.txt": "",
1645 },
1646 ExpectedBazelTargets: []string{
1647 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1648 "srcs": `[
1649 "a.txt",
1650 "b.txt",
1651 "//other/foo:a.txt",
1652 "//other/foo/bar:b.txt",
1653 "//other/foo:baz/c.txt",
1654 ]`,
1655 }),
1656 },
1657 },
1658 }
1659
1660 for _, testCase := range testCases {
1661 t.Run(testCase.Description, func(t *testing.T) {
1662 RunBp2BuildTestCaseSimple(t, testCase)
1663 })
1664 }
1665}
1666
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001667func TestGlobExcludeSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001668 testCases := []Bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001669 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001670 Description: "filegroup top level exclude_srcs",
1671 ModuleTypeUnderTest: "filegroup",
1672 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1673 Blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001674 name: "fg_foo",
1675 srcs: ["**/*.txt"],
1676 exclude_srcs: ["c.txt"],
1677 bazel_module: { bp2build_available: true },
1678}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001679 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001680 "a.txt": "",
1681 "b.txt": "",
1682 "c.txt": "",
1683 "dir/Android.bp": "",
1684 "dir/e.txt": "",
1685 "dir/f.txt": "",
1686 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001687 ExpectedBazelTargets: []string{
1688 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001689 "srcs": `[
1690 "a.txt",
1691 "b.txt",
1692 "//dir:e.txt",
1693 "//dir:f.txt",
1694 ]`,
1695 }),
1696 },
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001697 },
1698 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001699 Description: "filegroup in subdir exclude_srcs",
1700 ModuleTypeUnderTest: "filegroup",
1701 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1702 Blueprint: "",
1703 Dir: "dir",
1704 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001705 "dir/Android.bp": `filegroup {
1706 name: "fg_foo",
1707 srcs: ["**/*.txt"],
1708 exclude_srcs: ["b.txt"],
1709 bazel_module: { bp2build_available: true },
1710}
1711`,
1712 "dir/a.txt": "",
1713 "dir/b.txt": "",
1714 "dir/subdir/Android.bp": "",
1715 "dir/subdir/e.txt": "",
1716 "dir/subdir/f.txt": "",
1717 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001718 ExpectedBazelTargets: []string{
1719 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001720 "srcs": `[
Liz Kammer9abd62d2021-05-21 08:37:59 -04001721 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001722 "//dir/subdir:e.txt",
1723 "//dir/subdir:f.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001724 ]`,
1725 }),
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001726 },
1727 },
1728 }
1729
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001730 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001731 t.Run(testCase.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001732 RunBp2BuildTestCaseSimple(t, testCase)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001733 })
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001734 }
1735}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001736
1737func TestCommonBp2BuildModuleAttrs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001738 testCases := []Bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001739 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001740 Description: "Required into data test",
1741 ModuleTypeUnderTest: "filegroup",
1742 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1743 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001744filegroup {
1745 name: "fg_foo",
1746 required: ["reqd"],
1747 bazel_module: { bp2build_available: true },
1748}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001749 ExpectedBazelTargets: []string{
1750 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001751 "data": `[":reqd"]`,
1752 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001753 },
1754 },
1755 {
Jingwen Chena5ecb372022-09-21 09:05:37 +00001756 Description: "Required into data test, cyclic self reference is filtered out",
1757 ModuleTypeUnderTest: "filegroup",
1758 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1759 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
1760filegroup {
1761 name: "fg_foo",
1762 required: ["reqd", "fg_foo"],
1763 bazel_module: { bp2build_available: true },
1764}`,
1765 ExpectedBazelTargets: []string{
1766 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1767 "data": `[":reqd"]`,
1768 }),
1769 },
1770 },
1771 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001772 Description: "Required via arch into data test",
1773 ModuleTypeUnderTest: "python_library",
1774 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1775 Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") +
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001776 simpleModuleDoNotConvertBp2build("python_library", "reqdarm") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001777python_library {
1778 name: "fg_foo",
1779 arch: {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001780 arm: {
1781 required: ["reqdarm"],
1782 },
1783 x86: {
1784 required: ["reqdx86"],
1785 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001786 },
1787 bazel_module: { bp2build_available: true },
1788}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001789 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001790 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001791 "data": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001792 "//build/bazel/platforms/arch:arm": [":reqdarm"],
1793 "//build/bazel/platforms/arch:x86": [":reqdx86"],
1794 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001795 })`,
1796 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001797 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001798 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001799 },
1800 },
1801 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001802 Description: "Required appended to data test",
1803 ModuleTypeUnderTest: "python_library",
1804 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1805 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001806 "data.bin": "",
1807 "src.py": "",
1808 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001809 Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001810python_library {
1811 name: "fg_foo",
1812 data: ["data.bin"],
1813 required: ["reqd"],
1814 bazel_module: { bp2build_available: true },
1815}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001816 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001817 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001818 "data": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001819 "data.bin",
1820 ":reqd",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001821 ]`,
1822 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001823 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001824 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001825 },
1826 },
1827 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001828 Description: "All props-to-attrs at once together test",
1829 ModuleTypeUnderTest: "filegroup",
1830 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1831 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001832filegroup {
1833 name: "fg_foo",
1834 required: ["reqd"],
1835 bazel_module: { bp2build_available: true },
1836}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001837 ExpectedBazelTargets: []string{
1838 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001839 "data": `[":reqd"]`,
1840 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001841 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001842 },
1843 }
1844
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001845 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001846 t.Run(tc.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001847 RunBp2BuildTestCaseSimple(t, tc)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001848 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001849 }
1850}
Sasha Smundak05b0ba62022-09-26 18:15:45 -07001851
1852func TestLicensesAttrConversion(t *testing.T) {
1853 RunBp2BuildTestCase(t,
1854 func(ctx android.RegistrationContext) {
1855 ctx.RegisterModuleType("license", android.LicenseFactory)
1856 },
1857 Bp2buildTestCase{
1858 Description: "Test that licenses: attribute is converted",
1859 ModuleTypeUnderTest: "filegroup",
1860 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1861 Blueprint: `
1862license {
1863 name: "my_license",
1864}
1865filegroup {
1866 name: "my_filegroup",
1867 licenses: ["my_license"],
1868}
1869`,
1870 ExpectedBazelTargets: []string{
1871 MakeBazelTargetNoRestrictions("filegroup", "my_filegroup", AttrNameToString{
1872 "applicable_licenses": `[":my_license"]`,
1873 }),
1874 MakeBazelTargetNoRestrictions("android_license", "my_license", AttrNameToString{}),
1875 },
1876 })
1877}
Spandan Das5af0bd32022-09-28 20:43:08 +00001878
1879func TestGenerateApiBazelTargets(t *testing.T) {
1880 bp := `
1881 custom {
1882 name: "foo",
1883 api: "foo.txt",
1884 }
1885 `
1886 expectedBazelTarget := MakeBazelTarget(
1887 "custom_api_contribution",
1888 "foo",
1889 AttrNameToString{
1890 "api": `"foo.txt"`,
1891 },
1892 )
1893 registerCustomModule := func(ctx android.RegistrationContext) {
1894 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
1895 }
1896 RunApiBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
1897 Blueprint: bp,
1898 ExpectedBazelTargets: []string{expectedBazelTarget},
1899 Description: "Generating API contribution Bazel targets for custom module",
1900 })
1901}
Spandan Das6a448ec2023-04-19 17:36:12 +00001902
1903func TestGenerateConfigSetting(t *testing.T) {
1904 bp := `
1905 custom {
1906 name: "foo",
1907 test_config_setting: true,
1908 }
1909 `
1910 expectedBazelTargets := []string{
1911 MakeBazelTargetNoRestrictions(
1912 "config_setting",
1913 "foo_config_setting",
1914 AttrNameToString{
1915 "flag_values": `{
1916 "//build/bazel/rules/my_string_setting": "foo",
1917 }`,
1918 },
1919 ),
1920 MakeBazelTarget(
1921 "custom",
1922 "foo",
1923 AttrNameToString{},
1924 ),
1925 }
1926 registerCustomModule := func(ctx android.RegistrationContext) {
1927 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
1928 }
1929 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
1930 Blueprint: bp,
1931 ExpectedBazelTargets: expectedBazelTargets,
1932 Description: "Generating API contribution Bazel targets for custom module",
1933 })
1934}
Spandan Das921af322023-04-26 02:56:37 +00001935
1936// If values of all keys in an axis are equal to //conditions:default, drop the axis and print the common value
1937func TestPrettyPrintSelectMapEqualValues(t *testing.T) {
1938 lla := bazel.LabelListAttribute{
1939 Value: bazel.LabelList{},
1940 }
1941 libFooImplLabel := bazel.Label{
1942 Label: ":libfoo.impl",
1943 }
Spandan Das6d4d9da2023-04-18 06:20:40 +00001944 lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidPlatform, bazel.MakeLabelList([]bazel.Label{libFooImplLabel}))
Spandan Das921af322023-04-26 02:56:37 +00001945 lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.MakeLabelList([]bazel.Label{libFooImplLabel}))
1946 actual, _ := prettyPrintAttribute(lla, 0)
1947 android.AssertStringEquals(t, "Print the common value if all keys in an axis have the same value", `[":libfoo.impl"]`, actual)
1948}