blob: 9f4f7c1b3b941a2e77f86158e069cf20912e9fde [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"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000024 "android/soong/python"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080025)
26
27func TestGenerateSoongModuleTargets(t *testing.T) {
28 testCases := []struct {
Liz Kammerd366c902021-06-03 13:43:01 -040029 description string
Liz Kammer2dd9ca42020-11-25 16:06:39 -080030 bp string
31 expectedBazelTarget string
32 }{
33 {
Liz Kammerd366c902021-06-03 13:43:01 -040034 description: "only name",
Jingwen Chenb4628eb2021-04-08 14:40:57 +000035 bp: `custom { name: "foo" }
Liz Kammerd366c902021-06-03 13:43:01 -040036 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080037 expectedBazelTarget: `soong_module(
38 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050039 soong_module_name = "foo",
40 soong_module_type = "custom",
41 soong_module_variant = "",
42 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080043 ],
Liz Kammerd366c902021-06-03 13:43:01 -040044 bool_prop = False,
Liz Kammer46fb7ab2021-12-01 10:09:34 -050045 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080046)`,
47 },
48 {
Liz Kammerd366c902021-06-03 13:43:01 -040049 description: "handles bool",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080050 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040051 name: "foo",
52 bool_prop: true,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080053}
Liz Kammerd366c902021-06-03 13:43:01 -040054 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080055 expectedBazelTarget: `soong_module(
56 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050057 soong_module_name = "foo",
58 soong_module_type = "custom",
59 soong_module_variant = "",
60 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080061 ],
Liz Kammerd366c902021-06-03 13:43:01 -040062 bool_prop = True,
Liz Kammer46fb7ab2021-12-01 10:09:34 -050063 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080064)`,
65 },
66 {
Liz Kammerd366c902021-06-03 13:43:01 -040067 description: "string escaping",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080068 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040069 name: "foo",
70 owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080071}
Liz Kammerd366c902021-06-03 13:43:01 -040072 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080073 expectedBazelTarget: `soong_module(
74 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050075 soong_module_name = "foo",
76 soong_module_type = "custom",
77 soong_module_variant = "",
78 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080079 ],
Liz Kammerd366c902021-06-03 13:43:01 -040080 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080081 owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
Liz Kammer46fb7ab2021-12-01 10:09:34 -050082 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080083)`,
84 },
85 {
Liz Kammerd366c902021-06-03 13:43:01 -040086 description: "single item string list",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080087 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040088 name: "foo",
89 required: ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -080090}
Liz Kammerd366c902021-06-03 13:43:01 -040091 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080092 expectedBazelTarget: `soong_module(
93 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050094 soong_module_name = "foo",
95 soong_module_type = "custom",
96 soong_module_variant = "",
97 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080098 ],
Liz Kammerd366c902021-06-03 13:43:01 -040099 bool_prop = False,
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000100 required = ["bar"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500101 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800102)`,
103 },
104 {
Liz Kammerd366c902021-06-03 13:43:01 -0400105 description: "list of strings",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800106 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400107 name: "foo",
108 target_required: ["qux", "bazqux"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800109}
Liz Kammerd366c902021-06-03 13:43:01 -0400110 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800111 expectedBazelTarget: `soong_module(
112 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500113 soong_module_name = "foo",
114 soong_module_type = "custom",
115 soong_module_variant = "",
116 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800117 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400118 bool_prop = False,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500119 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800120 target_required = [
121 "qux",
122 "bazqux",
123 ],
124)`,
125 },
126 {
Liz Kammerd366c902021-06-03 13:43:01 -0400127 description: "dist/dists",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800128 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400129 name: "foo",
130 dist: {
131 targets: ["goal_foo"],
132 tag: ".foo",
133 },
134 dists: [{
135 targets: ["goal_bar"],
136 tag: ".bar",
137 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800138}
Liz Kammerd366c902021-06-03 13:43:01 -0400139 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800140 expectedBazelTarget: `soong_module(
141 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500142 soong_module_name = "foo",
143 soong_module_type = "custom",
144 soong_module_variant = "",
145 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800146 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400147 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800148 dist = {
149 "tag": ".foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000150 "targets": ["goal_foo"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800151 },
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000152 dists = [{
153 "tag": ".bar",
154 "targets": ["goal_bar"],
155 }],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500156 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800157)`,
158 },
159 {
Liz Kammerd366c902021-06-03 13:43:01 -0400160 description: "put it together",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800161 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400162 name: "foo",
163 required: ["bar"],
164 target_required: ["qux", "bazqux"],
165 bool_prop: true,
166 owner: "custom_owner",
167 dists: [
168 {
169 tag: ".tag",
170 targets: ["my_goal"],
171 },
172 ],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800173}
Liz Kammerd366c902021-06-03 13:43:01 -0400174 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800175 expectedBazelTarget: `soong_module(
176 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500177 soong_module_name = "foo",
178 soong_module_type = "custom",
179 soong_module_variant = "",
180 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800181 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400182 bool_prop = True,
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000183 dists = [{
184 "tag": ".tag",
185 "targets": ["my_goal"],
186 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800187 owner = "custom_owner",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000188 required = ["bar"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500189 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800190 target_required = [
191 "qux",
192 "bazqux",
193 ],
194)`,
195 },
196 }
197
198 dir := "."
199 for _, testCase := range testCases {
Liz Kammerd366c902021-06-03 13:43:01 -0400200 t.Run(testCase.description, func(t *testing.T) {
201 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
202 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500203
Liz Kammerdfeb1202022-05-13 17:20:20 -0400204 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
Liz Kammerd366c902021-06-03 13:43:01 -0400205 ctx.Register()
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800206
Liz Kammerd366c902021-06-03 13:43:01 -0400207 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
208 android.FailIfErrored(t, errs)
209 _, errs = ctx.PrepareBuildActions(config)
210 android.FailIfErrored(t, errs)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800211
Liz Kammerd366c902021-06-03 13:43:01 -0400212 codegenCtx := NewCodegenContext(config, *ctx.Context, QueryView)
Liz Kammer6eff3232021-08-26 08:37:59 -0400213 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
214 android.FailIfErrored(t, err)
Liz Kammerd366c902021-06-03 13:43:01 -0400215 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
216 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
217 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800218
Liz Kammerd366c902021-06-03 13:43:01 -0400219 actualBazelTarget := bazelTargets[0]
220 if actualBazelTarget.content != testCase.expectedBazelTarget {
221 t.Errorf(
222 "Expected generated Bazel target to be '%s', got '%s'",
223 testCase.expectedBazelTarget,
224 actualBazelTarget.content,
225 )
226 }
227 })
Jingwen Chen73850672020-12-14 08:25:34 -0500228 }
229}
230
231func TestGenerateBazelTargetModules(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000232 testCases := []Bp2buildTestCase{
Jingwen Chen73850672020-12-14 08:25:34 -0500233 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000234 Description: "string prop empty",
235 Blueprint: `custom {
236 name: "foo",
237 string_literal_prop: "",
238 bazel_module: { bp2build_available: true },
239}`,
240 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000241 MakeBazelTarget("custom", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000242 "string_literal_prop": `""`,
243 }),
244 },
245 },
246 {
247 Description: `string prop "PROP"`,
248 Blueprint: `custom {
249 name: "foo",
250 string_literal_prop: "PROP",
251 bazel_module: { bp2build_available: true },
252}`,
253 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000254 MakeBazelTarget("custom", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000255 "string_literal_prop": `"PROP"`,
256 }),
257 },
258 },
259 {
260 Description: `string prop arch variant`,
261 Blueprint: `custom {
262 name: "foo",
263 arch: {
264 arm: { string_literal_prop: "ARM" },
265 arm64: { string_literal_prop: "ARM64" },
266 },
267 bazel_module: { bp2build_available: true },
268}`,
269 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000270 MakeBazelTarget("custom", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000271 "string_literal_prop": `select({
272 "//build/bazel/platforms/arch:arm": "ARM",
273 "//build/bazel/platforms/arch:arm64": "ARM64",
274 "//conditions:default": None,
275 })`,
276 }),
277 },
278 },
279 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000280 Description: "string ptr props",
281 Blueprint: `custom {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500282 name: "foo",
283 string_ptr_prop: "",
284 bazel_module: { bp2build_available: true },
285}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000286 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000287 MakeBazelTarget("custom", "foo", AttrNameToString{
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500288 "string_ptr_prop": `""`,
289 }),
290 },
291 },
292 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000293 Description: "string list props",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000294 Blueprint: `custom {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400295 name: "foo",
Jingwen Chen73850672020-12-14 08:25:34 -0500296 string_list_prop: ["a", "b"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500297 string_ptr_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500298 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500299}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000300 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000301 MakeBazelTarget("custom", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500302 "string_list_prop": `[
Jingwen Chen73850672020-12-14 08:25:34 -0500303 "a",
304 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500305 ]`,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500306 "string_ptr_prop": `"a"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500307 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400308 },
Jingwen Chen73850672020-12-14 08:25:34 -0500309 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000310 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000311 Description: "control characters",
312 Blueprint: `custom {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500313 name: "foo",
Jingwen Chen58a12b82021-03-30 13:08:36 +0000314 string_list_prop: ["\t", "\n"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500315 string_ptr_prop: "a\t\n\r",
Jingwen Chen58a12b82021-03-30 13:08:36 +0000316 bazel_module: { bp2build_available: true },
317}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000318 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000319 MakeBazelTarget("custom", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500320 "string_list_prop": `[
Jingwen Chen58a12b82021-03-30 13:08:36 +0000321 "\t",
322 "\n",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500323 ]`,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500324 "string_ptr_prop": `"a\t\n\r"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500325 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400326 },
327 },
328 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000329 Description: "handles dep",
330 Blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400331 name: "has_dep",
332 arch_paths: [":dep"],
333 bazel_module: { bp2build_available: true },
334}
335
336custom {
337 name: "dep",
338 arch_paths: ["abc"],
339 bazel_module: { bp2build_available: true },
340}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000341 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000342 MakeBazelTarget("custom", "dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500343 "arch_paths": `["abc"]`,
344 }),
Alixe06d75b2022-08-31 18:28:19 +0000345 MakeBazelTarget("custom", "has_dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500346 "arch_paths": `[":dep"]`,
347 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400348 },
349 },
350 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000351 Description: "non-existent dep",
352 Blueprint: `custom {
Liz Kammerdaa09ef2021-12-15 15:35:38 -0500353 name: "has_dep",
354 arch_paths: [":dep"],
355 bazel_module: { bp2build_available: true },
356}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000357 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000358 MakeBazelTarget("custom", "has_dep", AttrNameToString{
Liz Kammerdaa09ef2021-12-15 15:35:38 -0500359 "arch_paths": `[":dep__BP2BUILD__MISSING__DEP"]`,
360 }),
361 },
362 },
363 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000364 Description: "arch-variant srcs",
365 Blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400366 name: "arch_paths",
367 arch: {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400368 x86: { arch_paths: ["x86.txt"] },
369 x86_64: { arch_paths: ["x86_64.txt"] },
370 arm: { arch_paths: ["arm.txt"] },
371 arm64: { arch_paths: ["arm64.txt"] },
372 },
373 target: {
374 linux: { arch_paths: ["linux.txt"] },
375 bionic: { arch_paths: ["bionic.txt"] },
376 host: { arch_paths: ["host.txt"] },
377 not_windows: { arch_paths: ["not_windows.txt"] },
378 android: { arch_paths: ["android.txt"] },
379 linux_musl: { arch_paths: ["linux_musl.txt"] },
380 musl: { arch_paths: ["musl.txt"] },
381 linux_glibc: { arch_paths: ["linux_glibc.txt"] },
382 glibc: { arch_paths: ["glibc.txt"] },
383 linux_bionic: { arch_paths: ["linux_bionic.txt"] },
384 darwin: { arch_paths: ["darwin.txt"] },
385 windows: { arch_paths: ["windows.txt"] },
386 },
387 multilib: {
388 lib32: { arch_paths: ["lib32.txt"] },
389 lib64: { arch_paths: ["lib64.txt"] },
Liz Kammer4562a3b2021-04-21 18:15:34 -0400390 },
391 bazel_module: { bp2build_available: true },
392}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000393 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000394 MakeBazelTarget("custom", "arch_paths", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500395 "arch_paths": `select({
Liz Kammerfdd72e62021-10-11 15:41:03 -0400396 "//build/bazel/platforms/arch:arm": [
397 "arm.txt",
398 "lib32.txt",
399 ],
400 "//build/bazel/platforms/arch:arm64": [
401 "arm64.txt",
402 "lib64.txt",
403 ],
404 "//build/bazel/platforms/arch:x86": [
405 "x86.txt",
406 "lib32.txt",
407 ],
408 "//build/bazel/platforms/arch:x86_64": [
409 "x86_64.txt",
410 "lib64.txt",
411 ],
412 "//conditions:default": [],
413 }) + select({
414 "//build/bazel/platforms/os:android": [
415 "linux.txt",
416 "bionic.txt",
417 "android.txt",
418 ],
419 "//build/bazel/platforms/os:darwin": [
420 "host.txt",
421 "darwin.txt",
422 "not_windows.txt",
423 ],
424 "//build/bazel/platforms/os:linux": [
425 "host.txt",
426 "linux.txt",
427 "glibc.txt",
428 "linux_glibc.txt",
429 "not_windows.txt",
430 ],
431 "//build/bazel/platforms/os:linux_bionic": [
432 "host.txt",
433 "linux.txt",
434 "bionic.txt",
435 "linux_bionic.txt",
436 "not_windows.txt",
437 ],
438 "//build/bazel/platforms/os:linux_musl": [
439 "host.txt",
440 "linux.txt",
441 "musl.txt",
442 "linux_musl.txt",
443 "not_windows.txt",
444 ],
445 "//build/bazel/platforms/os:windows": [
446 "host.txt",
447 "windows.txt",
448 ],
Liz Kammer4562a3b2021-04-21 18:15:34 -0400449 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500450 })`,
451 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400452 },
453 },
454 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000455 Description: "arch-variant deps",
456 Blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400457 name: "has_dep",
458 arch: {
459 x86: {
460 arch_paths: [":dep"],
461 },
462 },
463 bazel_module: { bp2build_available: true },
464}
465
466custom {
467 name: "dep",
468 arch_paths: ["abc"],
469 bazel_module: { bp2build_available: true },
470}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000471 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000472 MakeBazelTarget("custom", "dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500473 "arch_paths": `["abc"]`,
474 }),
Alixe06d75b2022-08-31 18:28:19 +0000475 MakeBazelTarget("custom", "has_dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500476 "arch_paths": `select({
Liz Kammer4562a3b2021-04-21 18:15:34 -0400477 "//build/bazel/platforms/arch:x86": [":dep"],
478 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500479 })`,
480 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400481 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000482 },
Liz Kammer32a03392021-09-14 11:17:21 -0400483 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000484 Description: "embedded props",
485 Blueprint: `custom {
Liz Kammer32a03392021-09-14 11:17:21 -0400486 name: "embedded_props",
487 embedded_prop: "abc",
488 bazel_module: { bp2build_available: true },
489}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000490 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000491 MakeBazelTarget("custom", "embedded_props", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500492 "embedded_attr": `"abc"`,
493 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400494 },
495 },
496 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000497 Description: "ptr to embedded props",
498 Blueprint: `custom {
Liz Kammer32a03392021-09-14 11:17:21 -0400499 name: "ptr_to_embedded_props",
500 other_embedded_prop: "abc",
501 bazel_module: { bp2build_available: true },
502}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000503 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000504 MakeBazelTarget("custom", "ptr_to_embedded_props", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500505 "other_embedded_attr": `"abc"`,
506 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400507 },
508 },
Jingwen Chen73850672020-12-14 08:25:34 -0500509 }
510
511 dir := "."
512 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000513 t.Run(testCase.Description, func(t *testing.T) {
514 config := android.TestConfig(buildDir, nil, testCase.Blueprint, nil)
Liz Kammerfdd72e62021-10-11 15:41:03 -0400515 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500516
Liz Kammerfdd72e62021-10-11 15:41:03 -0400517 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500518
Liz Kammerfdd72e62021-10-11 15:41:03 -0400519 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
520 if errored(t, testCase, errs) {
521 return
522 }
523 _, errs = ctx.ResolveDependencies(config)
524 if errored(t, testCase, errs) {
525 return
526 }
Jingwen Chen73850672020-12-14 08:25:34 -0500527
Liz Kammerfdd72e62021-10-11 15:41:03 -0400528 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
529 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
530 android.FailIfErrored(t, err)
Jingwen Chen164e0862021-02-19 00:48:40 -0500531
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000532 if actualCount, expectedCount := len(bazelTargets), len(testCase.ExpectedBazelTargets); actualCount != expectedCount {
533 t.Errorf("Expected %d bazel target (%s),\ngot %d (%s)", expectedCount, testCase.ExpectedBazelTargets, actualCount, bazelTargets)
Liz Kammerfdd72e62021-10-11 15:41:03 -0400534 } else {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000535 for i, expectedBazelTarget := range testCase.ExpectedBazelTargets {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400536 actualBazelTarget := bazelTargets[i]
537 if actualBazelTarget.content != expectedBazelTarget {
538 t.Errorf(
539 "Expected generated Bazel target to be '%s', got '%s'",
540 expectedBazelTarget,
541 actualBazelTarget.content,
542 )
543 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400544 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500545 }
Liz Kammerfdd72e62021-10-11 15:41:03 -0400546 })
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800547 }
548}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500549
Liz Kammerdfeb1202022-05-13 17:20:20 -0400550func TestBp2buildHostAndDevice(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000551 testCases := []Bp2buildTestCase{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400552 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000553 Description: "host and device, device only",
554 ModuleTypeUnderTest: "custom",
555 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
556 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400557 name: "foo",
558 bazel_module: { bp2build_available: true },
559}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000560 ExpectedBazelTargets: []string{
561 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400562 },
563 },
564 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000565 Description: "host and device, both",
566 ModuleTypeUnderTest: "custom",
567 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
568 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400569 name: "foo",
570 host_supported: true,
571 bazel_module: { bp2build_available: true },
572}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000573 ExpectedBazelTargets: []string{
574 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400575 },
576 },
577 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000578 Description: "host and device, host explicitly disabled",
579 ModuleTypeUnderTest: "custom",
580 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
581 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400582 name: "foo",
583 host_supported: false,
584 bazel_module: { bp2build_available: true },
585}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000586 ExpectedBazelTargets: []string{
587 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400588 },
589 },
590 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000591 Description: "host and device, neither",
592 ModuleTypeUnderTest: "custom",
593 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
594 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400595 name: "foo",
596 host_supported: false,
597 device_supported: false,
598 bazel_module: { bp2build_available: true },
599}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000600 ExpectedBazelTargets: []string{
601 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400602 "target_compatible_with": `["@platforms//:incompatible"]`,
603 }),
604 },
605 },
606 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000607 Description: "host and device, neither, cannot override with product_var",
608 ModuleTypeUnderTest: "custom",
609 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
610 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400611 name: "foo",
612 host_supported: false,
613 device_supported: false,
614 product_variables: { unbundled_build: { enabled: true } },
615 bazel_module: { bp2build_available: true },
616}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000617 ExpectedBazelTargets: []string{
618 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400619 "target_compatible_with": `["@platforms//:incompatible"]`,
620 }),
621 },
622 },
623 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000624 Description: "host and device, both, disabled overrided with product_var",
625 ModuleTypeUnderTest: "custom",
626 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
627 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400628 name: "foo",
629 host_supported: true,
630 device_supported: true,
631 enabled: false,
632 product_variables: { unbundled_build: { enabled: true } },
633 bazel_module: { bp2build_available: true },
634}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000635 ExpectedBazelTargets: []string{
636 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400637 "target_compatible_with": `["//build/bazel/product_variables:unbundled_build"]`,
638 }),
639 },
640 },
641 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000642 Description: "host and device, neither, cannot override with arch enabled",
643 ModuleTypeUnderTest: "custom",
644 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
645 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400646 name: "foo",
647 host_supported: false,
648 device_supported: false,
649 arch: { x86: { enabled: true } },
650 bazel_module: { bp2build_available: true },
651}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000652 ExpectedBazelTargets: []string{
653 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400654 "target_compatible_with": `["@platforms//:incompatible"]`,
655 }),
656 },
657 },
658 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000659 Description: "host and device, host only",
660 ModuleTypeUnderTest: "custom",
661 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
662 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400663 name: "foo",
664 host_supported: true,
665 device_supported: false,
666 bazel_module: { bp2build_available: true },
667}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000668 ExpectedBazelTargets: []string{
669 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400670 },
671 },
672 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000673 Description: "host only",
674 ModuleTypeUnderTest: "custom",
675 ModuleTypeUnderTestFactory: customModuleFactoryHostSupported,
676 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400677 name: "foo",
678 bazel_module: { bp2build_available: true },
679}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000680 ExpectedBazelTargets: []string{
681 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400682 },
683 },
684 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000685 Description: "device only",
686 ModuleTypeUnderTest: "custom",
687 ModuleTypeUnderTestFactory: customModuleFactoryDeviceSupported,
688 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400689 name: "foo",
690 bazel_module: { bp2build_available: true },
691}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000692 ExpectedBazelTargets: []string{
693 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400694 },
695 },
696 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000697 Description: "host and device default, default",
698 ModuleTypeUnderTest: "custom",
699 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
700 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400701 name: "foo",
702 bazel_module: { bp2build_available: true },
703}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000704 ExpectedBazelTargets: []string{
705 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400706 },
707 },
708 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000709 Description: "host and device default, device only",
710 ModuleTypeUnderTest: "custom",
711 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
712 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400713 name: "foo",
714 host_supported: false,
715 bazel_module: { bp2build_available: true },
716}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000717 ExpectedBazelTargets: []string{
718 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400719 },
720 },
721 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000722 Description: "host and device default, host only",
723 ModuleTypeUnderTest: "custom",
724 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
725 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400726 name: "foo",
727 device_supported: false,
728 bazel_module: { bp2build_available: true },
729}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000730 ExpectedBazelTargets: []string{
731 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400732 },
733 },
734 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000735 Description: "host and device default, neither",
736 ModuleTypeUnderTest: "custom",
737 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
738 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400739 name: "foo",
740 host_supported: false,
741 device_supported: false,
742 bazel_module: { bp2build_available: true },
743}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000744 ExpectedBazelTargets: []string{
745 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400746 "target_compatible_with": `["@platforms//:incompatible"]`,
747 }),
748 },
749 },
750 }
751
752 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000753 t.Run(tc.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +0000754 RunBp2BuildTestCaseSimple(t, tc)
Liz Kammerdfeb1202022-05-13 17:20:20 -0400755 })
756 }
757}
758
Jingwen Chen40067de2021-01-26 21:58:43 -0500759func TestLoadStatements(t *testing.T) {
760 testCases := []struct {
761 bazelTargets BazelTargets
762 expectedLoadStatements string
763 }{
764 {
765 bazelTargets: BazelTargets{
766 BazelTarget{
767 name: "foo",
768 ruleClass: "cc_library",
769 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
770 },
771 },
772 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
773 },
774 {
775 bazelTargets: BazelTargets{
776 BazelTarget{
777 name: "foo",
778 ruleClass: "cc_library",
779 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
780 },
781 BazelTarget{
782 name: "bar",
783 ruleClass: "cc_library",
784 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
785 },
786 },
787 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
788 },
789 {
790 bazelTargets: BazelTargets{
791 BazelTarget{
792 name: "foo",
793 ruleClass: "cc_library",
794 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
795 },
796 BazelTarget{
797 name: "bar",
798 ruleClass: "cc_binary",
799 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
800 },
801 },
802 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
803 },
804 {
805 bazelTargets: BazelTargets{
806 BazelTarget{
807 name: "foo",
808 ruleClass: "cc_library",
809 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
810 },
811 BazelTarget{
812 name: "bar",
813 ruleClass: "cc_binary",
814 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
815 },
816 BazelTarget{
817 name: "baz",
818 ruleClass: "java_binary",
819 bzlLoadLocation: "//build/bazel/rules:java.bzl",
820 },
821 },
822 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
823load("//build/bazel/rules:java.bzl", "java_binary")`,
824 },
825 {
826 bazelTargets: BazelTargets{
827 BazelTarget{
828 name: "foo",
829 ruleClass: "cc_binary",
830 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
831 },
832 BazelTarget{
833 name: "bar",
834 ruleClass: "java_binary",
835 bzlLoadLocation: "//build/bazel/rules:java.bzl",
836 },
837 BazelTarget{
838 name: "baz",
839 ruleClass: "genrule",
840 // Note: no bzlLoadLocation for native rules
841 },
842 },
843 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
844load("//build/bazel/rules:java.bzl", "java_binary")`,
845 },
846 }
847
848 for _, testCase := range testCases {
849 actual := testCase.bazelTargets.LoadStatements()
850 expected := testCase.expectedLoadStatements
851 if actual != expected {
852 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
853 }
854 }
855
856}
857
858func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
859 testCases := []struct {
860 bp string
861 expectedBazelTarget string
862 expectedBazelTargetCount int
863 expectedLoadStatements string
864 }{
865 {
866 bp: `custom {
867 name: "bar",
Liz Kammerdfeb1202022-05-13 17:20:20 -0400868 host_supported: true,
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400869 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500870 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500871}`,
872 expectedBazelTarget: `my_library(
873 name = "bar",
874)
875
Jingwen Chen40067de2021-01-26 21:58:43 -0500876proto_library(
877 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400878)
879
880my_proto_library(
881 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500882)`,
883 expectedBazelTargetCount: 3,
884 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
885load("//build/bazel/rules:rules.bzl", "my_library")`,
886 },
887 }
888
889 dir := "."
890 for _, testCase := range testCases {
891 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
892 ctx := android.NewTestContext(config)
Liz Kammerdfeb1202022-05-13 17:20:20 -0400893 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
Jingwen Chen40067de2021-01-26 21:58:43 -0500894 ctx.RegisterForBazelConversion()
895
896 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
897 android.FailIfErrored(t, errs)
898 _, errs = ctx.ResolveDependencies(config)
899 android.FailIfErrored(t, errs)
900
Jingwen Chen164e0862021-02-19 00:48:40 -0500901 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400902 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
903 android.FailIfErrored(t, err)
Jingwen Chen40067de2021-01-26 21:58:43 -0500904 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
905 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
906 }
907
908 actualBazelTargets := bazelTargets.String()
909 if actualBazelTargets != testCase.expectedBazelTarget {
910 t.Errorf(
911 "Expected generated Bazel target to be '%s', got '%s'",
912 testCase.expectedBazelTarget,
913 actualBazelTargets,
914 )
915 }
916
917 actualLoadStatements := bazelTargets.LoadStatements()
918 if actualLoadStatements != testCase.expectedLoadStatements {
919 t.Errorf(
920 "Expected generated load statements to be '%s', got '%s'",
921 testCase.expectedLoadStatements,
922 actualLoadStatements,
923 )
924 }
925 }
926}
927
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500928func TestModuleTypeBp2Build(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000929 testCases := []Bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500930 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000931 Description: "filegroup with does not specify srcs",
932 ModuleTypeUnderTest: "filegroup",
933 ModuleTypeUnderTestFactory: android.FileGroupFactory,
934 Blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500935 name: "fg_foo",
936 bazel_module: { bp2build_available: true },
937}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000938 ExpectedBazelTargets: []string{
939 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerebfcf672021-02-16 15:00:05 -0500940 },
941 },
942 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000943 Description: "filegroup with no srcs",
944 ModuleTypeUnderTest: "filegroup",
945 ModuleTypeUnderTestFactory: android.FileGroupFactory,
946 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500947 name: "fg_foo",
948 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500949 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500950}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000951 ExpectedBazelTargets: []string{
952 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammer356f7d42021-01-26 09:18:53 -0500953 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500954 },
955 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000956 Description: "filegroup with srcs",
957 ModuleTypeUnderTest: "filegroup",
958 ModuleTypeUnderTestFactory: android.FileGroupFactory,
959 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500960 name: "fg_foo",
961 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500962 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500963}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000964 ExpectedBazelTargets: []string{
965 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500966 "srcs": `[
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500967 "a",
968 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500969 ]`,
970 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500971 },
972 },
973 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000974 Description: "filegroup with excludes srcs",
975 ModuleTypeUnderTest: "filegroup",
976 ModuleTypeUnderTestFactory: android.FileGroupFactory,
977 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500978 name: "fg_foo",
979 srcs: ["a", "b"],
980 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500981 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500982}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000983 ExpectedBazelTargets: []string{
984 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500985 "srcs": `["b"]`,
986 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500987 },
988 },
989 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000990 Description: "depends_on_other_dir_module",
991 ModuleTypeUnderTest: "filegroup",
992 ModuleTypeUnderTestFactory: android.FileGroupFactory,
993 Blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500994 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500995 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000996 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500997 "c",
998 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500999 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -05001000}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001001 Filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -05001002 "other/Android.bp": `filegroup {
1003 name: "foo",
1004 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -04001005 bazel_module: { bp2build_available: true },
1006}`,
1007 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001008 ExpectedBazelTargets: []string{
1009 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001010 "srcs": `[
1011 "//other:foo",
1012 "c",
1013 ]`,
1014 }),
1015 },
Liz Kammer6eff3232021-08-26 08:37:59 -04001016 },
1017 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001018 Description: "depends_on_other_unconverted_module_error",
1019 ModuleTypeUnderTest: "filegroup",
1020 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1021 UnconvertedDepsMode: errorModulesUnconvertedDeps,
1022 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001023 name: "foobar",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001024 srcs: [
1025 ":foo",
1026 "c",
1027 ],
1028 bazel_module: { bp2build_available: true },
1029}`,
Sasha Smundakf2bb26f2022-08-04 11:28:15 -07001030 ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on unconverted modules: foo`),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001031 Filesystem: map[string]string{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001032 "other/Android.bp": `filegroup {
1033 name: "foo",
1034 srcs: ["a", "b"],
1035}`,
1036 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001037 },
1038 }
1039
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001040 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001041 t.Run(testCase.Description, func(t *testing.T) {
1042 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
Liz Kammer6eff3232021-08-26 08:37:59 -04001043 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001044 }
1045}
Jingwen Chen041b1842021-02-01 00:23:25 -05001046
1047type bp2buildMutator = func(android.TopDownMutatorContext)
1048
Jingwen Chen12b4c272021-03-10 02:05:59 -05001049func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001050 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001051 moduleTypeUnderTest string
1052 moduleTypeUnderTestFactory android.ModuleFactory
1053 bp string
1054 expectedCount int
1055 description string
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001056 }{
1057 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001058 description: "explicitly unavailable",
1059 moduleTypeUnderTest: "filegroup",
1060 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001061 bp: `filegroup {
1062 name: "foo",
1063 srcs: ["a", "b"],
1064 bazel_module: { bp2build_available: false },
1065}`,
1066 expectedCount: 0,
1067 },
1068 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001069 description: "implicitly unavailable",
1070 moduleTypeUnderTest: "filegroup",
1071 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001072 bp: `filegroup {
1073 name: "foo",
1074 srcs: ["a", "b"],
1075}`,
1076 expectedCount: 0,
1077 },
1078 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001079 description: "explicitly available",
1080 moduleTypeUnderTest: "filegroup",
1081 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001082 bp: `filegroup {
1083 name: "foo",
1084 srcs: ["a", "b"],
1085 bazel_module: { bp2build_available: true },
1086}`,
1087 expectedCount: 1,
1088 },
1089 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001090 description: "generates more than 1 target if needed",
1091 moduleTypeUnderTest: "custom",
Liz Kammerdfeb1202022-05-13 17:20:20 -04001092 moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001093 bp: `custom {
1094 name: "foo",
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001095 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001096 bazel_module: { bp2build_available: true },
1097}`,
1098 expectedCount: 3,
1099 },
1100 }
1101
1102 dir := "."
1103 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -04001104 t.Run(testCase.description, func(t *testing.T) {
1105 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1106 ctx := android.NewTestContext(config)
1107 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001108 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001109
Liz Kammer2ada09a2021-08-11 00:17:36 -04001110 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1111 android.FailIfErrored(t, errs)
1112 _, errs = ctx.ResolveDependencies(config)
1113 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001114
Liz Kammer2ada09a2021-08-11 00:17:36 -04001115 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -04001116 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1117 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001118 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1119 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1120 }
1121 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001122 }
1123}
Liz Kammerba3ea162021-02-17 13:22:03 -05001124
Jingwen Chen12b4c272021-03-10 02:05:59 -05001125func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1126 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001127 moduleTypeUnderTest string
1128 moduleTypeUnderTestFactory android.ModuleFactory
1129 expectedCount map[string]int
1130 description string
Sam Delmerico24c56032022-03-28 19:53:03 +00001131 bp2buildConfig allowlists.Bp2BuildConfig
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001132 checkDir string
1133 fs map[string]string
Jingwen Chen12b4c272021-03-10 02:05:59 -05001134 }{
1135 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001136 description: "test bp2build config package and subpackages config",
1137 moduleTypeUnderTest: "filegroup",
1138 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001139 expectedCount: map[string]int{
1140 "migrated": 1,
1141 "migrated/but_not_really": 0,
1142 "migrated/but_not_really/but_really": 1,
1143 "not_migrated": 0,
1144 "also_not_migrated": 0,
1145 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001146 bp2buildConfig: allowlists.Bp2BuildConfig{
1147 "migrated": allowlists.Bp2BuildDefaultTrueRecursively,
1148 "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
1149 "not_migrated": allowlists.Bp2BuildDefaultFalse,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001150 },
1151 fs: map[string]string{
1152 "migrated/Android.bp": `filegroup { name: "a" }`,
1153 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1154 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1155 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1156 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1157 },
1158 },
1159 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001160 description: "test bp2build config opt-in and opt-out",
1161 moduleTypeUnderTest: "filegroup",
1162 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001163 expectedCount: map[string]int{
1164 "package-opt-in": 2,
1165 "package-opt-in/subpackage": 0,
1166 "package-opt-out": 1,
1167 "package-opt-out/subpackage": 0,
1168 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001169 bp2buildConfig: allowlists.Bp2BuildConfig{
1170 "package-opt-in": allowlists.Bp2BuildDefaultFalse,
1171 "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001172 },
1173 fs: map[string]string{
1174 "package-opt-in/Android.bp": `
1175filegroup { name: "opt-in-a" }
1176filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1177filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1178`,
1179
1180 "package-opt-in/subpackage/Android.bp": `
1181filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1182`,
1183
1184 "package-opt-out/Android.bp": `
1185filegroup { name: "opt-out-a" }
1186filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1187filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1188`,
1189
1190 "package-opt-out/subpackage/Android.bp": `
1191filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1192filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1193`,
1194 },
1195 },
1196 }
1197
1198 dir := "."
1199 for _, testCase := range testCases {
1200 fs := make(map[string][]byte)
1201 toParse := []string{
1202 "Android.bp",
1203 }
1204 for f, content := range testCase.fs {
1205 if strings.HasSuffix(f, "Android.bp") {
1206 toParse = append(toParse, f)
1207 }
1208 fs[f] = []byte(content)
1209 }
1210 config := android.TestConfig(buildDir, nil, "", fs)
1211 ctx := android.NewTestContext(config)
1212 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Sam Delmerico24c56032022-03-28 19:53:03 +00001213 allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig)
1214 ctx.RegisterBp2BuildConfig(allowlist)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001215 ctx.RegisterForBazelConversion()
1216
1217 _, errs := ctx.ParseFileList(dir, toParse)
1218 android.FailIfErrored(t, errs)
1219 _, errs = ctx.ResolveDependencies(config)
1220 android.FailIfErrored(t, errs)
1221
1222 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1223
1224 // For each directory, test that the expected number of generated targets is correct.
1225 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -04001226 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1227 android.FailIfErrored(t, err)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001228 if actualCount := len(bazelTargets); actualCount != expectedCount {
1229 t.Fatalf(
1230 "%s: Expected %d bazel target for %s package, got %d",
1231 testCase.description,
1232 expectedCount,
1233 dir,
1234 actualCount)
1235 }
1236
1237 }
1238 }
1239}
1240
Liz Kammerba3ea162021-02-17 13:22:03 -05001241func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001242 testCases := []Bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -05001243 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001244 Description: "filegroup bazel_module.label",
1245 ModuleTypeUnderTest: "filegroup",
1246 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1247 Blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -05001248 name: "fg_foo",
1249 bazel_module: { label: "//other:fg_foo" },
1250}`,
Cole Faustea602c52022-08-31 14:48:26 -07001251 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001252 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001253 "other/BUILD.bazel": `// BUILD file`,
1254 },
1255 },
1256 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001257 Description: "multiple bazel_module.label same BUILD",
1258 ModuleTypeUnderTest: "filegroup",
1259 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1260 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001261 name: "fg_foo",
1262 bazel_module: { label: "//other:fg_foo" },
1263 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001264
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001265 filegroup {
1266 name: "foo",
1267 bazel_module: { label: "//other:foo" },
1268 }`,
Cole Faustea602c52022-08-31 14:48:26 -07001269 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001270 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001271 "other/BUILD.bazel": `// BUILD file`,
1272 },
1273 },
1274 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001275 Description: "filegroup bazel_module.label and bp2build in subdir",
1276 ModuleTypeUnderTest: "filegroup",
1277 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1278 Dir: "other",
1279 Blueprint: ``,
1280 Filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001281 "other/Android.bp": `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001282 name: "fg_foo",
1283 bazel_module: {
1284 bp2build_available: true,
1285 },
1286 }
1287 filegroup {
1288 name: "fg_bar",
1289 bazel_module: {
1290 label: "//other:fg_bar"
1291 },
1292 }`,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001293 "other/BUILD.bazel": `// definition for fg_bar`,
1294 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001295 ExpectedBazelTargets: []string{
1296 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001297 },
1298 },
1299 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001300 Description: "filegroup bazel_module.label and filegroup bp2build",
1301 ModuleTypeUnderTest: "filegroup",
1302 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001303
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001304 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001305 "other/BUILD.bazel": `// BUILD file`,
1306 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001307 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001308 name: "fg_foo",
1309 bazel_module: {
1310 label: "//other:fg_foo",
1311 },
1312 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001313
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001314 filegroup {
1315 name: "fg_bar",
1316 bazel_module: {
1317 bp2build_available: true,
1318 },
1319 }`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001320 ExpectedBazelTargets: []string{
1321 MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001322 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001323 },
1324 }
1325
1326 dir := "."
1327 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001328 t.Run(testCase.Description, func(t *testing.T) {
Jingwen Chen49109762021-05-25 05:16:48 +00001329 fs := make(map[string][]byte)
1330 toParse := []string{
1331 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001332 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001333 for f, content := range testCase.Filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001334 if strings.HasSuffix(f, "Android.bp") {
1335 toParse = append(toParse, f)
1336 }
1337 fs[f] = []byte(content)
1338 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001339 config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001340 ctx := android.NewTestContext(config)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001341 ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001342 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001343
Jingwen Chen49109762021-05-25 05:16:48 +00001344 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001345 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001346 return
1347 }
1348 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001349 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001350 return
1351 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001352
Jingwen Chen49109762021-05-25 05:16:48 +00001353 checkDir := dir
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001354 if testCase.Dir != "" {
1355 checkDir = testCase.Dir
Jingwen Chen49109762021-05-25 05:16:48 +00001356 }
Liz Kammer6eff3232021-08-26 08:37:59 -04001357 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1358 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1359 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001360 bazelTargets.sort()
1361 actualCount := len(bazelTargets)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001362 expectedCount := len(testCase.ExpectedBazelTargets)
Jingwen Chen49109762021-05-25 05:16:48 +00001363 if actualCount != expectedCount {
1364 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1365 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001366 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001367 actualContent := target.content
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001368 expectedContent := testCase.ExpectedBazelTargets[i]
Jingwen Chen49109762021-05-25 05:16:48 +00001369 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001370 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001371 "Expected generated Bazel target to be '%s', got '%s'",
1372 expectedContent,
1373 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001374 )
1375 }
1376 }
Jingwen Chen49109762021-05-25 05:16:48 +00001377 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001378 }
1379}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001380
Jingwen Chen0eeaeb82022-09-21 10:27:42 +00001381func TestGlob(t *testing.T) {
1382 testCases := []Bp2buildTestCase{
1383 {
1384 Description: "filegroup with glob",
1385 ModuleTypeUnderTest: "filegroup",
1386 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1387 Blueprint: `filegroup {
1388 name: "fg_foo",
1389 srcs: ["**/*.txt"],
1390 bazel_module: { bp2build_available: true },
1391}`,
1392 ExpectedBazelTargets: []string{
1393 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1394 "srcs": `[
1395 "other/a.txt",
1396 "other/b.txt",
1397 "other/subdir/a.txt",
1398 ]`,
1399 }),
1400 },
1401 Filesystem: map[string]string{
1402 "other/a.txt": "",
1403 "other/b.txt": "",
1404 "other/subdir/a.txt": "",
1405 "other/file": "",
1406 },
1407 },
1408 {
1409 Description: "filegroup with glob in subdir",
1410 ModuleTypeUnderTest: "filegroup",
1411 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1412 Dir: "other",
1413 Filesystem: map[string]string{
1414 "other/Android.bp": `filegroup {
1415 name: "fg_foo",
1416 srcs: ["**/*.txt"],
1417 bazel_module: { bp2build_available: true },
1418}`,
1419 "other/a.txt": "",
1420 "other/b.txt": "",
1421 "other/subdir/a.txt": "",
1422 "other/file": "",
1423 },
1424 ExpectedBazelTargets: []string{
1425 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1426 "srcs": `[
1427 "a.txt",
1428 "b.txt",
1429 "subdir/a.txt",
1430 ]`,
1431 }),
1432 },
1433 },
1434 {
1435 Description: "filegroup with glob with no kept BUILD files",
1436 ModuleTypeUnderTest: "filegroup",
1437 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1438 KeepBuildFileForDirs: []string{
1439 // empty
1440 },
1441 Blueprint: `filegroup {
1442 name: "fg_foo",
1443 srcs: ["**/*.txt"],
1444 bazel_module: { bp2build_available: true },
1445}`,
1446 Filesystem: map[string]string{
1447 "a.txt": "",
1448 "b.txt": "",
1449 "foo/BUILD": "",
1450 "foo/a.txt": "",
1451 "foo/bar/BUILD": "",
1452 "foo/bar/b.txt": "",
1453 },
1454 ExpectedBazelTargets: []string{
1455 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1456 "srcs": `[
1457 "a.txt",
1458 "b.txt",
1459 "foo/a.txt",
1460 "foo/bar/b.txt",
1461 ]`,
1462 }),
1463 },
1464 },
1465 {
1466 Description: "filegroup with glob with kept BUILD file",
1467 ModuleTypeUnderTest: "filegroup",
1468 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1469 KeepBuildFileForDirs: []string{
1470 "foo",
1471 },
1472 Blueprint: `filegroup {
1473 name: "fg_foo",
1474 srcs: ["**/*.txt"],
1475 bazel_module: { bp2build_available: true },
1476}`,
1477 Filesystem: map[string]string{
1478 "a.txt": "",
1479 "b.txt": "",
1480 "foo/BUILD": "",
1481 "foo/a.txt": "",
1482 "foo/bar/BUILD": "",
1483 "foo/bar/b.txt": "",
1484 },
1485 ExpectedBazelTargets: []string{
1486 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1487 "srcs": `[
1488 "a.txt",
1489 "b.txt",
1490 "//foo:a.txt",
1491 "//foo:bar/b.txt",
1492 ]`,
1493 }),
1494 },
1495 },
1496 {
1497 Description: "filegroup with glob with kept BUILD.bazel file",
1498 ModuleTypeUnderTest: "filegroup",
1499 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1500 KeepBuildFileForDirs: []string{
1501 "foo",
1502 },
1503 Blueprint: `filegroup {
1504 name: "fg_foo",
1505 srcs: ["**/*.txt"],
1506 bazel_module: { bp2build_available: true },
1507}`,
1508 Filesystem: map[string]string{
1509 "a.txt": "",
1510 "b.txt": "",
1511 "foo/BUILD.bazel": "",
1512 "foo/a.txt": "",
1513 "foo/bar/BUILD.bazel": "",
1514 "foo/bar/b.txt": "",
1515 },
1516 ExpectedBazelTargets: []string{
1517 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1518 "srcs": `[
1519 "a.txt",
1520 "b.txt",
1521 "//foo:a.txt",
1522 "//foo:bar/b.txt",
1523 ]`,
1524 }),
1525 },
1526 },
1527 {
1528 Description: "filegroup with glob with Android.bp file as boundary",
1529 ModuleTypeUnderTest: "filegroup",
1530 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1531 Blueprint: `filegroup {
1532 name: "fg_foo",
1533 srcs: ["**/*.txt"],
1534 bazel_module: { bp2build_available: true },
1535}`,
1536 Filesystem: map[string]string{
1537 "a.txt": "",
1538 "b.txt": "",
1539 "foo/Android.bp": "",
1540 "foo/a.txt": "",
1541 "foo/bar/Android.bp": "",
1542 "foo/bar/b.txt": "",
1543 },
1544 ExpectedBazelTargets: []string{
1545 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1546 "srcs": `[
1547 "a.txt",
1548 "b.txt",
1549 "//foo:a.txt",
1550 "//foo/bar:b.txt",
1551 ]`,
1552 }),
1553 },
1554 },
1555 {
1556 Description: "filegroup with glob in subdir with kept BUILD and BUILD.bazel file",
1557 ModuleTypeUnderTest: "filegroup",
1558 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1559 Dir: "other",
1560 KeepBuildFileForDirs: []string{
1561 "other/foo",
1562 "other/foo/bar",
1563 // deliberately not other/foo/baz/BUILD.
1564 },
1565 Filesystem: map[string]string{
1566 "other/Android.bp": `filegroup {
1567 name: "fg_foo",
1568 srcs: ["**/*.txt"],
1569 bazel_module: { bp2build_available: true },
1570}`,
1571 "other/a.txt": "",
1572 "other/b.txt": "",
1573 "other/foo/BUILD": "",
1574 "other/foo/a.txt": "",
1575 "other/foo/bar/BUILD.bazel": "",
1576 "other/foo/bar/b.txt": "",
1577 "other/foo/baz/BUILD": "",
1578 "other/foo/baz/c.txt": "",
1579 },
1580 ExpectedBazelTargets: []string{
1581 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1582 "srcs": `[
1583 "a.txt",
1584 "b.txt",
1585 "//other/foo:a.txt",
1586 "//other/foo/bar:b.txt",
1587 "//other/foo:baz/c.txt",
1588 ]`,
1589 }),
1590 },
1591 },
1592 }
1593
1594 for _, testCase := range testCases {
1595 t.Run(testCase.Description, func(t *testing.T) {
1596 RunBp2BuildTestCaseSimple(t, testCase)
1597 })
1598 }
1599}
1600
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001601func TestGlobExcludeSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001602 testCases := []Bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001603 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001604 Description: "filegroup top level exclude_srcs",
1605 ModuleTypeUnderTest: "filegroup",
1606 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1607 Blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001608 name: "fg_foo",
1609 srcs: ["**/*.txt"],
1610 exclude_srcs: ["c.txt"],
1611 bazel_module: { bp2build_available: true },
1612}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001613 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001614 "a.txt": "",
1615 "b.txt": "",
1616 "c.txt": "",
1617 "dir/Android.bp": "",
1618 "dir/e.txt": "",
1619 "dir/f.txt": "",
1620 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001621 ExpectedBazelTargets: []string{
1622 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001623 "srcs": `[
1624 "a.txt",
1625 "b.txt",
1626 "//dir:e.txt",
1627 "//dir:f.txt",
1628 ]`,
1629 }),
1630 },
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001631 },
1632 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001633 Description: "filegroup in subdir exclude_srcs",
1634 ModuleTypeUnderTest: "filegroup",
1635 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1636 Blueprint: "",
1637 Dir: "dir",
1638 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001639 "dir/Android.bp": `filegroup {
1640 name: "fg_foo",
1641 srcs: ["**/*.txt"],
1642 exclude_srcs: ["b.txt"],
1643 bazel_module: { bp2build_available: true },
1644}
1645`,
1646 "dir/a.txt": "",
1647 "dir/b.txt": "",
1648 "dir/subdir/Android.bp": "",
1649 "dir/subdir/e.txt": "",
1650 "dir/subdir/f.txt": "",
1651 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001652 ExpectedBazelTargets: []string{
1653 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001654 "srcs": `[
Liz Kammer9abd62d2021-05-21 08:37:59 -04001655 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001656 "//dir/subdir:e.txt",
1657 "//dir/subdir:f.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001658 ]`,
1659 }),
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001660 },
1661 },
1662 }
1663
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001664 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001665 t.Run(testCase.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001666 RunBp2BuildTestCaseSimple(t, testCase)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001667 })
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001668 }
1669}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001670
1671func TestCommonBp2BuildModuleAttrs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001672 testCases := []Bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001673 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001674 Description: "Required into data test",
1675 ModuleTypeUnderTest: "filegroup",
1676 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1677 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001678filegroup {
1679 name: "fg_foo",
1680 required: ["reqd"],
1681 bazel_module: { bp2build_available: true },
1682}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001683 ExpectedBazelTargets: []string{
1684 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001685 "data": `[":reqd"]`,
1686 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001687 },
1688 },
1689 {
Jingwen Chena5ecb372022-09-21 09:05:37 +00001690 Description: "Required into data test, cyclic self reference is filtered out",
1691 ModuleTypeUnderTest: "filegroup",
1692 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1693 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
1694filegroup {
1695 name: "fg_foo",
1696 required: ["reqd", "fg_foo"],
1697 bazel_module: { bp2build_available: true },
1698}`,
1699 ExpectedBazelTargets: []string{
1700 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1701 "data": `[":reqd"]`,
1702 }),
1703 },
1704 },
1705 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001706 Description: "Required via arch into data test",
1707 ModuleTypeUnderTest: "python_library",
1708 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1709 Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") +
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001710 simpleModuleDoNotConvertBp2build("python_library", "reqdarm") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001711python_library {
1712 name: "fg_foo",
1713 arch: {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001714 arm: {
1715 required: ["reqdarm"],
1716 },
1717 x86: {
1718 required: ["reqdx86"],
1719 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001720 },
1721 bazel_module: { bp2build_available: true },
1722}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001723 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001724 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001725 "data": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001726 "//build/bazel/platforms/arch:arm": [":reqdarm"],
1727 "//build/bazel/platforms/arch:x86": [":reqdx86"],
1728 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001729 })`,
1730 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001731 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001732 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001733 },
1734 },
1735 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001736 Description: "Required appended to data test",
1737 ModuleTypeUnderTest: "python_library",
1738 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1739 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001740 "data.bin": "",
1741 "src.py": "",
1742 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001743 Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001744python_library {
1745 name: "fg_foo",
1746 data: ["data.bin"],
1747 required: ["reqd"],
1748 bazel_module: { bp2build_available: true },
1749}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001750 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001751 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001752 "data": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001753 "data.bin",
1754 ":reqd",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001755 ]`,
1756 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001757 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001758 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001759 },
1760 },
1761 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001762 Description: "All props-to-attrs at once together test",
1763 ModuleTypeUnderTest: "filegroup",
1764 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1765 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001766filegroup {
1767 name: "fg_foo",
1768 required: ["reqd"],
1769 bazel_module: { bp2build_available: true },
1770}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001771 ExpectedBazelTargets: []string{
1772 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001773 "data": `[":reqd"]`,
1774 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001775 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001776 },
1777 }
1778
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001779 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001780 t.Run(tc.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001781 RunBp2BuildTestCaseSimple(t, tc)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001782 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001783 }
1784}