blob: dd28c3c94f3d906232335950ac37d65d3b344bb4 [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: "filegroup with glob",
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: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500996 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500997}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000998 ExpectedBazelTargets: []string{
999 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001000 "srcs": `[
Liz Kammer356f7d42021-01-26 09:18:53 -05001001 "other/a.txt",
1002 "other/b.txt",
1003 "other/subdir/a.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001004 ]`,
1005 }),
Liz Kammer356f7d42021-01-26 09:18:53 -05001006 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001007 Filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -05001008 "other/a.txt": "",
1009 "other/b.txt": "",
1010 "other/subdir/a.txt": "",
1011 "other/file": "",
1012 },
1013 },
1014 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001015 Description: "filegroup with glob in subdir",
1016 ModuleTypeUnderTest: "filegroup",
1017 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1018 Dir: "other",
1019 Filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -05001020 "other/Android.bp": `filegroup {
1021 name: "fg_foo",
1022 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001023 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -05001024}`,
1025 "other/a.txt": "",
1026 "other/b.txt": "",
1027 "other/subdir/a.txt": "",
1028 "other/file": "",
1029 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001030 ExpectedBazelTargets: []string{
1031 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001032 "srcs": `[
1033 "a.txt",
1034 "b.txt",
1035 "subdir/a.txt",
1036 ]`,
1037 }),
1038 },
Liz Kammer356f7d42021-01-26 09:18:53 -05001039 },
1040 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001041 Description: "depends_on_other_dir_module",
1042 ModuleTypeUnderTest: "filegroup",
1043 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1044 Blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001045 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -05001046 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +00001047 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -05001048 "c",
1049 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001050 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -05001051}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001052 Filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -05001053 "other/Android.bp": `filegroup {
1054 name: "foo",
1055 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -04001056 bazel_module: { bp2build_available: true },
1057}`,
1058 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001059 ExpectedBazelTargets: []string{
1060 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001061 "srcs": `[
1062 "//other:foo",
1063 "c",
1064 ]`,
1065 }),
1066 },
Liz Kammer6eff3232021-08-26 08:37:59 -04001067 },
1068 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001069 Description: "depends_on_other_unconverted_module_error",
1070 ModuleTypeUnderTest: "filegroup",
1071 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1072 UnconvertedDepsMode: errorModulesUnconvertedDeps,
1073 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001074 name: "foobar",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001075 srcs: [
1076 ":foo",
1077 "c",
1078 ],
1079 bazel_module: { bp2build_available: true },
1080}`,
Sasha Smundakf2bb26f2022-08-04 11:28:15 -07001081 ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on unconverted modules: foo`),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001082 Filesystem: map[string]string{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001083 "other/Android.bp": `filegroup {
1084 name: "foo",
1085 srcs: ["a", "b"],
1086}`,
1087 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001088 },
1089 }
1090
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001091 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001092 t.Run(testCase.Description, func(t *testing.T) {
1093 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
Liz Kammer6eff3232021-08-26 08:37:59 -04001094 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001095 }
1096}
Jingwen Chen041b1842021-02-01 00:23:25 -05001097
1098type bp2buildMutator = func(android.TopDownMutatorContext)
1099
Jingwen Chen12b4c272021-03-10 02:05:59 -05001100func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001101 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001102 moduleTypeUnderTest string
1103 moduleTypeUnderTestFactory android.ModuleFactory
1104 bp string
1105 expectedCount int
1106 description string
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001107 }{
1108 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001109 description: "explicitly unavailable",
1110 moduleTypeUnderTest: "filegroup",
1111 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001112 bp: `filegroup {
1113 name: "foo",
1114 srcs: ["a", "b"],
1115 bazel_module: { bp2build_available: false },
1116}`,
1117 expectedCount: 0,
1118 },
1119 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001120 description: "implicitly unavailable",
1121 moduleTypeUnderTest: "filegroup",
1122 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001123 bp: `filegroup {
1124 name: "foo",
1125 srcs: ["a", "b"],
1126}`,
1127 expectedCount: 0,
1128 },
1129 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001130 description: "explicitly available",
1131 moduleTypeUnderTest: "filegroup",
1132 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001133 bp: `filegroup {
1134 name: "foo",
1135 srcs: ["a", "b"],
1136 bazel_module: { bp2build_available: true },
1137}`,
1138 expectedCount: 1,
1139 },
1140 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001141 description: "generates more than 1 target if needed",
1142 moduleTypeUnderTest: "custom",
Liz Kammerdfeb1202022-05-13 17:20:20 -04001143 moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001144 bp: `custom {
1145 name: "foo",
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001146 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001147 bazel_module: { bp2build_available: true },
1148}`,
1149 expectedCount: 3,
1150 },
1151 }
1152
1153 dir := "."
1154 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -04001155 t.Run(testCase.description, func(t *testing.T) {
1156 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1157 ctx := android.NewTestContext(config)
1158 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001159 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001160
Liz Kammer2ada09a2021-08-11 00:17:36 -04001161 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1162 android.FailIfErrored(t, errs)
1163 _, errs = ctx.ResolveDependencies(config)
1164 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001165
Liz Kammer2ada09a2021-08-11 00:17:36 -04001166 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -04001167 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1168 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001169 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1170 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1171 }
1172 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001173 }
1174}
Liz Kammerba3ea162021-02-17 13:22:03 -05001175
Jingwen Chen12b4c272021-03-10 02:05:59 -05001176func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1177 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001178 moduleTypeUnderTest string
1179 moduleTypeUnderTestFactory android.ModuleFactory
1180 expectedCount map[string]int
1181 description string
Sam Delmerico24c56032022-03-28 19:53:03 +00001182 bp2buildConfig allowlists.Bp2BuildConfig
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001183 checkDir string
1184 fs map[string]string
Jingwen Chen12b4c272021-03-10 02:05:59 -05001185 }{
1186 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001187 description: "test bp2build config package and subpackages config",
1188 moduleTypeUnderTest: "filegroup",
1189 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001190 expectedCount: map[string]int{
1191 "migrated": 1,
1192 "migrated/but_not_really": 0,
1193 "migrated/but_not_really/but_really": 1,
1194 "not_migrated": 0,
1195 "also_not_migrated": 0,
1196 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001197 bp2buildConfig: allowlists.Bp2BuildConfig{
1198 "migrated": allowlists.Bp2BuildDefaultTrueRecursively,
1199 "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
1200 "not_migrated": allowlists.Bp2BuildDefaultFalse,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001201 },
1202 fs: map[string]string{
1203 "migrated/Android.bp": `filegroup { name: "a" }`,
1204 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1205 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1206 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1207 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1208 },
1209 },
1210 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001211 description: "test bp2build config opt-in and opt-out",
1212 moduleTypeUnderTest: "filegroup",
1213 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001214 expectedCount: map[string]int{
1215 "package-opt-in": 2,
1216 "package-opt-in/subpackage": 0,
1217 "package-opt-out": 1,
1218 "package-opt-out/subpackage": 0,
1219 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001220 bp2buildConfig: allowlists.Bp2BuildConfig{
1221 "package-opt-in": allowlists.Bp2BuildDefaultFalse,
1222 "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001223 },
1224 fs: map[string]string{
1225 "package-opt-in/Android.bp": `
1226filegroup { name: "opt-in-a" }
1227filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1228filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1229`,
1230
1231 "package-opt-in/subpackage/Android.bp": `
1232filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1233`,
1234
1235 "package-opt-out/Android.bp": `
1236filegroup { name: "opt-out-a" }
1237filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1238filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1239`,
1240
1241 "package-opt-out/subpackage/Android.bp": `
1242filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1243filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1244`,
1245 },
1246 },
1247 }
1248
1249 dir := "."
1250 for _, testCase := range testCases {
1251 fs := make(map[string][]byte)
1252 toParse := []string{
1253 "Android.bp",
1254 }
1255 for f, content := range testCase.fs {
1256 if strings.HasSuffix(f, "Android.bp") {
1257 toParse = append(toParse, f)
1258 }
1259 fs[f] = []byte(content)
1260 }
1261 config := android.TestConfig(buildDir, nil, "", fs)
1262 ctx := android.NewTestContext(config)
1263 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Sam Delmerico24c56032022-03-28 19:53:03 +00001264 allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig)
1265 ctx.RegisterBp2BuildConfig(allowlist)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001266 ctx.RegisterForBazelConversion()
1267
1268 _, errs := ctx.ParseFileList(dir, toParse)
1269 android.FailIfErrored(t, errs)
1270 _, errs = ctx.ResolveDependencies(config)
1271 android.FailIfErrored(t, errs)
1272
1273 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1274
1275 // For each directory, test that the expected number of generated targets is correct.
1276 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -04001277 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1278 android.FailIfErrored(t, err)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001279 if actualCount := len(bazelTargets); actualCount != expectedCount {
1280 t.Fatalf(
1281 "%s: Expected %d bazel target for %s package, got %d",
1282 testCase.description,
1283 expectedCount,
1284 dir,
1285 actualCount)
1286 }
1287
1288 }
1289 }
1290}
1291
Liz Kammerba3ea162021-02-17 13:22:03 -05001292func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001293 testCases := []Bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -05001294 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001295 Description: "filegroup bazel_module.label",
1296 ModuleTypeUnderTest: "filegroup",
1297 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1298 Blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -05001299 name: "fg_foo",
1300 bazel_module: { label: "//other:fg_foo" },
1301}`,
Cole Faustea602c52022-08-31 14:48:26 -07001302 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001303 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001304 "other/BUILD.bazel": `// BUILD file`,
1305 },
1306 },
1307 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001308 Description: "multiple bazel_module.label same BUILD",
1309 ModuleTypeUnderTest: "filegroup",
1310 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1311 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001312 name: "fg_foo",
1313 bazel_module: { label: "//other:fg_foo" },
1314 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001315
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001316 filegroup {
1317 name: "foo",
1318 bazel_module: { label: "//other:foo" },
1319 }`,
Cole Faustea602c52022-08-31 14:48:26 -07001320 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001321 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001322 "other/BUILD.bazel": `// BUILD file`,
1323 },
1324 },
1325 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001326 Description: "filegroup bazel_module.label and bp2build in subdir",
1327 ModuleTypeUnderTest: "filegroup",
1328 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1329 Dir: "other",
1330 Blueprint: ``,
1331 Filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001332 "other/Android.bp": `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001333 name: "fg_foo",
1334 bazel_module: {
1335 bp2build_available: true,
1336 },
1337 }
1338 filegroup {
1339 name: "fg_bar",
1340 bazel_module: {
1341 label: "//other:fg_bar"
1342 },
1343 }`,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001344 "other/BUILD.bazel": `// definition for fg_bar`,
1345 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001346 ExpectedBazelTargets: []string{
1347 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001348 },
1349 },
1350 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001351 Description: "filegroup bazel_module.label and filegroup bp2build",
1352 ModuleTypeUnderTest: "filegroup",
1353 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001354
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001355 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001356 "other/BUILD.bazel": `// BUILD file`,
1357 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001358 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001359 name: "fg_foo",
1360 bazel_module: {
1361 label: "//other:fg_foo",
1362 },
1363 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001364
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001365 filegroup {
1366 name: "fg_bar",
1367 bazel_module: {
1368 bp2build_available: true,
1369 },
1370 }`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001371 ExpectedBazelTargets: []string{
1372 MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001373 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001374 },
1375 }
1376
1377 dir := "."
1378 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001379 t.Run(testCase.Description, func(t *testing.T) {
Jingwen Chen49109762021-05-25 05:16:48 +00001380 fs := make(map[string][]byte)
1381 toParse := []string{
1382 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001383 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001384 for f, content := range testCase.Filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001385 if strings.HasSuffix(f, "Android.bp") {
1386 toParse = append(toParse, f)
1387 }
1388 fs[f] = []byte(content)
1389 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001390 config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001391 ctx := android.NewTestContext(config)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001392 ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001393 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001394
Jingwen Chen49109762021-05-25 05:16:48 +00001395 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001396 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001397 return
1398 }
1399 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001400 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001401 return
1402 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001403
Jingwen Chen49109762021-05-25 05:16:48 +00001404 checkDir := dir
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001405 if testCase.Dir != "" {
1406 checkDir = testCase.Dir
Jingwen Chen49109762021-05-25 05:16:48 +00001407 }
Liz Kammer6eff3232021-08-26 08:37:59 -04001408 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1409 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1410 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001411 bazelTargets.sort()
1412 actualCount := len(bazelTargets)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001413 expectedCount := len(testCase.ExpectedBazelTargets)
Jingwen Chen49109762021-05-25 05:16:48 +00001414 if actualCount != expectedCount {
1415 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1416 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001417 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001418 actualContent := target.content
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001419 expectedContent := testCase.ExpectedBazelTargets[i]
Jingwen Chen49109762021-05-25 05:16:48 +00001420 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001421 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001422 "Expected generated Bazel target to be '%s', got '%s'",
1423 expectedContent,
1424 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001425 )
1426 }
1427 }
Jingwen Chen49109762021-05-25 05:16:48 +00001428 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001429 }
1430}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001431
1432func TestGlobExcludeSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001433 testCases := []Bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001434 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001435 Description: "filegroup top level exclude_srcs",
1436 ModuleTypeUnderTest: "filegroup",
1437 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1438 Blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001439 name: "fg_foo",
1440 srcs: ["**/*.txt"],
1441 exclude_srcs: ["c.txt"],
1442 bazel_module: { bp2build_available: true },
1443}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001444 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001445 "a.txt": "",
1446 "b.txt": "",
1447 "c.txt": "",
1448 "dir/Android.bp": "",
1449 "dir/e.txt": "",
1450 "dir/f.txt": "",
1451 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001452 ExpectedBazelTargets: []string{
1453 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001454 "srcs": `[
1455 "a.txt",
1456 "b.txt",
1457 "//dir:e.txt",
1458 "//dir:f.txt",
1459 ]`,
1460 }),
1461 },
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001462 },
1463 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001464 Description: "filegroup in subdir exclude_srcs",
1465 ModuleTypeUnderTest: "filegroup",
1466 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1467 Blueprint: "",
1468 Dir: "dir",
1469 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001470 "dir/Android.bp": `filegroup {
1471 name: "fg_foo",
1472 srcs: ["**/*.txt"],
1473 exclude_srcs: ["b.txt"],
1474 bazel_module: { bp2build_available: true },
1475}
1476`,
1477 "dir/a.txt": "",
1478 "dir/b.txt": "",
1479 "dir/subdir/Android.bp": "",
1480 "dir/subdir/e.txt": "",
1481 "dir/subdir/f.txt": "",
1482 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001483 ExpectedBazelTargets: []string{
1484 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001485 "srcs": `[
Liz Kammer9abd62d2021-05-21 08:37:59 -04001486 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001487 "//dir/subdir:e.txt",
1488 "//dir/subdir:f.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001489 ]`,
1490 }),
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001491 },
1492 },
1493 }
1494
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001495 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001496 t.Run(testCase.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001497 RunBp2BuildTestCaseSimple(t, testCase)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001498 })
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001499 }
1500}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001501
1502func TestCommonBp2BuildModuleAttrs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001503 testCases := []Bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001504 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001505 Description: "Required into data test",
1506 ModuleTypeUnderTest: "filegroup",
1507 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1508 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001509filegroup {
1510 name: "fg_foo",
1511 required: ["reqd"],
1512 bazel_module: { bp2build_available: true },
1513}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001514 ExpectedBazelTargets: []string{
1515 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001516 "data": `[":reqd"]`,
1517 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001518 },
1519 },
1520 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001521 Description: "Required via arch into data test",
1522 ModuleTypeUnderTest: "python_library",
1523 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1524 Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") +
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001525 simpleModuleDoNotConvertBp2build("python_library", "reqdarm") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001526python_library {
1527 name: "fg_foo",
1528 arch: {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001529 arm: {
1530 required: ["reqdarm"],
1531 },
1532 x86: {
1533 required: ["reqdx86"],
1534 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001535 },
1536 bazel_module: { bp2build_available: true },
1537}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001538 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001539 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001540 "data": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001541 "//build/bazel/platforms/arch:arm": [":reqdarm"],
1542 "//build/bazel/platforms/arch:x86": [":reqdx86"],
1543 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001544 })`,
1545 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001546 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001547 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001548 },
1549 },
1550 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001551 Description: "Required appended to data test",
1552 ModuleTypeUnderTest: "python_library",
1553 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1554 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001555 "data.bin": "",
1556 "src.py": "",
1557 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001558 Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001559python_library {
1560 name: "fg_foo",
1561 data: ["data.bin"],
1562 required: ["reqd"],
1563 bazel_module: { bp2build_available: true },
1564}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001565 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001566 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001567 "data": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001568 "data.bin",
1569 ":reqd",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001570 ]`,
1571 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001572 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001573 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001574 },
1575 },
1576 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001577 Description: "All props-to-attrs at once together test",
1578 ModuleTypeUnderTest: "filegroup",
1579 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1580 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001581filegroup {
1582 name: "fg_foo",
1583 required: ["reqd"],
1584 bazel_module: { bp2build_available: true },
1585}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001586 ExpectedBazelTargets: []string{
1587 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001588 "data": `[":reqd"]`,
1589 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001590 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001591 },
1592 }
1593
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001594 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001595 t.Run(tc.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001596 RunBp2BuildTestCaseSimple(t, tc)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001597 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001598 }
1599}