blob: 1b64055f7a9a84b21d8f50bd95ccc9377105daae [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
Cole Faustb85d1a12022-11-08 18:14:01 -0800212 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"] },
Colin Crossf05b0d32022-07-14 18:10:34 -0700372 riscv64: { arch_paths: ["riscv64.txt"] },
Liz Kammerfdd72e62021-10-11 15:41:03 -0400373 },
374 target: {
375 linux: { arch_paths: ["linux.txt"] },
376 bionic: { arch_paths: ["bionic.txt"] },
377 host: { arch_paths: ["host.txt"] },
378 not_windows: { arch_paths: ["not_windows.txt"] },
379 android: { arch_paths: ["android.txt"] },
380 linux_musl: { arch_paths: ["linux_musl.txt"] },
381 musl: { arch_paths: ["musl.txt"] },
382 linux_glibc: { arch_paths: ["linux_glibc.txt"] },
383 glibc: { arch_paths: ["glibc.txt"] },
384 linux_bionic: { arch_paths: ["linux_bionic.txt"] },
385 darwin: { arch_paths: ["darwin.txt"] },
386 windows: { arch_paths: ["windows.txt"] },
387 },
388 multilib: {
389 lib32: { arch_paths: ["lib32.txt"] },
390 lib64: { arch_paths: ["lib64.txt"] },
Liz Kammer4562a3b2021-04-21 18:15:34 -0400391 },
392 bazel_module: { bp2build_available: true },
393}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000394 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000395 MakeBazelTarget("custom", "arch_paths", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500396 "arch_paths": `select({
Liz Kammerfdd72e62021-10-11 15:41:03 -0400397 "//build/bazel/platforms/arch:arm": [
398 "arm.txt",
399 "lib32.txt",
400 ],
401 "//build/bazel/platforms/arch:arm64": [
402 "arm64.txt",
403 "lib64.txt",
404 ],
Colin Crossf05b0d32022-07-14 18:10:34 -0700405 "//build/bazel/platforms/arch:riscv64": [
406 "riscv64.txt",
407 "lib64.txt",
408 ],
Liz Kammerfdd72e62021-10-11 15:41:03 -0400409 "//build/bazel/platforms/arch:x86": [
410 "x86.txt",
411 "lib32.txt",
412 ],
413 "//build/bazel/platforms/arch:x86_64": [
414 "x86_64.txt",
415 "lib64.txt",
416 ],
417 "//conditions:default": [],
418 }) + select({
419 "//build/bazel/platforms/os:android": [
420 "linux.txt",
421 "bionic.txt",
422 "android.txt",
423 ],
424 "//build/bazel/platforms/os:darwin": [
425 "host.txt",
426 "darwin.txt",
427 "not_windows.txt",
428 ],
Liz Kammerfdd72e62021-10-11 15:41:03 -0400429 "//build/bazel/platforms/os:linux_bionic": [
430 "host.txt",
431 "linux.txt",
432 "bionic.txt",
433 "linux_bionic.txt",
434 "not_windows.txt",
435 ],
Colin Cross133782e2022-12-20 15:29:31 -0800436 "//build/bazel/platforms/os:linux_glibc": [
437 "host.txt",
438 "linux.txt",
439 "glibc.txt",
440 "linux_glibc.txt",
441 "not_windows.txt",
442 ],
Liz Kammerfdd72e62021-10-11 15:41:03 -0400443 "//build/bazel/platforms/os:linux_musl": [
444 "host.txt",
445 "linux.txt",
446 "musl.txt",
447 "linux_musl.txt",
448 "not_windows.txt",
449 ],
450 "//build/bazel/platforms/os:windows": [
451 "host.txt",
452 "windows.txt",
453 ],
Liz Kammer4562a3b2021-04-21 18:15:34 -0400454 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500455 })`,
456 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400457 },
458 },
459 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000460 Description: "arch-variant deps",
461 Blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400462 name: "has_dep",
463 arch: {
464 x86: {
465 arch_paths: [":dep"],
466 },
467 },
468 bazel_module: { bp2build_available: true },
469}
470
471custom {
472 name: "dep",
473 arch_paths: ["abc"],
474 bazel_module: { bp2build_available: true },
475}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000476 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000477 MakeBazelTarget("custom", "dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500478 "arch_paths": `["abc"]`,
479 }),
Alixe06d75b2022-08-31 18:28:19 +0000480 MakeBazelTarget("custom", "has_dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500481 "arch_paths": `select({
Liz Kammer4562a3b2021-04-21 18:15:34 -0400482 "//build/bazel/platforms/arch:x86": [":dep"],
483 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500484 })`,
485 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400486 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000487 },
Liz Kammer32a03392021-09-14 11:17:21 -0400488 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000489 Description: "embedded props",
490 Blueprint: `custom {
Liz Kammer32a03392021-09-14 11:17:21 -0400491 name: "embedded_props",
492 embedded_prop: "abc",
493 bazel_module: { bp2build_available: true },
494}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000495 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000496 MakeBazelTarget("custom", "embedded_props", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500497 "embedded_attr": `"abc"`,
498 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400499 },
500 },
501 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000502 Description: "ptr to embedded props",
503 Blueprint: `custom {
Liz Kammer32a03392021-09-14 11:17:21 -0400504 name: "ptr_to_embedded_props",
505 other_embedded_prop: "abc",
506 bazel_module: { bp2build_available: true },
507}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000508 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000509 MakeBazelTarget("custom", "ptr_to_embedded_props", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500510 "other_embedded_attr": `"abc"`,
511 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400512 },
513 },
Jingwen Chen73850672020-12-14 08:25:34 -0500514 }
515
516 dir := "."
517 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000518 t.Run(testCase.Description, func(t *testing.T) {
519 config := android.TestConfig(buildDir, nil, testCase.Blueprint, nil)
Liz Kammerfdd72e62021-10-11 15:41:03 -0400520 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500521
Liz Kammerfdd72e62021-10-11 15:41:03 -0400522 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500523
Liz Kammerfdd72e62021-10-11 15:41:03 -0400524 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
525 if errored(t, testCase, errs) {
526 return
527 }
528 _, errs = ctx.ResolveDependencies(config)
529 if errored(t, testCase, errs) {
530 return
531 }
Jingwen Chen73850672020-12-14 08:25:34 -0500532
Cole Faustb85d1a12022-11-08 18:14:01 -0800533 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammerfdd72e62021-10-11 15:41:03 -0400534 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
535 android.FailIfErrored(t, err)
Jingwen Chen164e0862021-02-19 00:48:40 -0500536
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000537 if actualCount, expectedCount := len(bazelTargets), len(testCase.ExpectedBazelTargets); actualCount != expectedCount {
538 t.Errorf("Expected %d bazel target (%s),\ngot %d (%s)", expectedCount, testCase.ExpectedBazelTargets, actualCount, bazelTargets)
Liz Kammerfdd72e62021-10-11 15:41:03 -0400539 } else {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000540 for i, expectedBazelTarget := range testCase.ExpectedBazelTargets {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400541 actualBazelTarget := bazelTargets[i]
542 if actualBazelTarget.content != expectedBazelTarget {
543 t.Errorf(
544 "Expected generated Bazel target to be '%s', got '%s'",
545 expectedBazelTarget,
546 actualBazelTarget.content,
547 )
548 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400549 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500550 }
Liz Kammerfdd72e62021-10-11 15:41:03 -0400551 })
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800552 }
553}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500554
Liz Kammerdfeb1202022-05-13 17:20:20 -0400555func TestBp2buildHostAndDevice(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000556 testCases := []Bp2buildTestCase{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400557 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000558 Description: "host and device, device only",
559 ModuleTypeUnderTest: "custom",
560 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
561 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400562 name: "foo",
563 bazel_module: { bp2build_available: true },
564}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000565 ExpectedBazelTargets: []string{
566 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400567 },
568 },
569 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000570 Description: "host and device, both",
571 ModuleTypeUnderTest: "custom",
572 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
573 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400574 name: "foo",
575 host_supported: true,
576 bazel_module: { bp2build_available: true },
577}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000578 ExpectedBazelTargets: []string{
579 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400580 },
581 },
582 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000583 Description: "host and device, host explicitly disabled",
584 ModuleTypeUnderTest: "custom",
585 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
586 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400587 name: "foo",
588 host_supported: false,
589 bazel_module: { bp2build_available: true },
590}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000591 ExpectedBazelTargets: []string{
592 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400593 },
594 },
595 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000596 Description: "host and device, neither",
597 ModuleTypeUnderTest: "custom",
598 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
599 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400600 name: "foo",
601 host_supported: false,
602 device_supported: false,
603 bazel_module: { bp2build_available: true },
604}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000605 ExpectedBazelTargets: []string{
606 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400607 "target_compatible_with": `["@platforms//:incompatible"]`,
608 }),
609 },
610 },
611 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000612 Description: "host and device, neither, cannot override with product_var",
613 ModuleTypeUnderTest: "custom",
614 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
615 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400616 name: "foo",
617 host_supported: false,
618 device_supported: false,
619 product_variables: { unbundled_build: { enabled: true } },
620 bazel_module: { bp2build_available: true },
621}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000622 ExpectedBazelTargets: []string{
623 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400624 "target_compatible_with": `["@platforms//:incompatible"]`,
625 }),
626 },
627 },
628 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000629 Description: "host and device, both, disabled overrided with product_var",
630 ModuleTypeUnderTest: "custom",
631 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
632 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400633 name: "foo",
634 host_supported: true,
635 device_supported: true,
636 enabled: false,
637 product_variables: { unbundled_build: { enabled: true } },
638 bazel_module: { bp2build_available: true },
639}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000640 ExpectedBazelTargets: []string{
641 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400642 "target_compatible_with": `["//build/bazel/product_variables:unbundled_build"]`,
643 }),
644 },
645 },
646 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000647 Description: "host and device, neither, cannot override with arch enabled",
648 ModuleTypeUnderTest: "custom",
649 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
650 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400651 name: "foo",
652 host_supported: false,
653 device_supported: false,
654 arch: { x86: { enabled: true } },
655 bazel_module: { bp2build_available: true },
656}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000657 ExpectedBazelTargets: []string{
658 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400659 "target_compatible_with": `["@platforms//:incompatible"]`,
660 }),
661 },
662 },
663 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000664 Description: "host and device, host only",
665 ModuleTypeUnderTest: "custom",
666 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
667 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400668 name: "foo",
669 host_supported: true,
670 device_supported: false,
671 bazel_module: { bp2build_available: true },
672}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000673 ExpectedBazelTargets: []string{
674 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400675 },
676 },
677 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000678 Description: "host only",
679 ModuleTypeUnderTest: "custom",
680 ModuleTypeUnderTestFactory: customModuleFactoryHostSupported,
681 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400682 name: "foo",
683 bazel_module: { bp2build_available: true },
684}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000685 ExpectedBazelTargets: []string{
686 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400687 },
688 },
689 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000690 Description: "device only",
691 ModuleTypeUnderTest: "custom",
692 ModuleTypeUnderTestFactory: customModuleFactoryDeviceSupported,
693 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400694 name: "foo",
695 bazel_module: { bp2build_available: true },
696}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000697 ExpectedBazelTargets: []string{
698 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400699 },
700 },
701 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000702 Description: "host and device default, default",
703 ModuleTypeUnderTest: "custom",
704 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
705 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400706 name: "foo",
707 bazel_module: { bp2build_available: true },
708}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000709 ExpectedBazelTargets: []string{
710 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400711 },
712 },
713 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000714 Description: "host and device default, device only",
715 ModuleTypeUnderTest: "custom",
716 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
717 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400718 name: "foo",
719 host_supported: false,
720 bazel_module: { bp2build_available: true },
721}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000722 ExpectedBazelTargets: []string{
723 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400724 },
725 },
726 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000727 Description: "host and device default, host only",
728 ModuleTypeUnderTest: "custom",
729 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
730 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400731 name: "foo",
732 device_supported: false,
733 bazel_module: { bp2build_available: true },
734}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000735 ExpectedBazelTargets: []string{
736 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400737 },
738 },
739 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000740 Description: "host and device default, neither",
741 ModuleTypeUnderTest: "custom",
742 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
743 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400744 name: "foo",
745 host_supported: false,
746 device_supported: false,
747 bazel_module: { bp2build_available: true },
748}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000749 ExpectedBazelTargets: []string{
750 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400751 "target_compatible_with": `["@platforms//:incompatible"]`,
752 }),
753 },
754 },
755 }
756
757 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000758 t.Run(tc.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +0000759 RunBp2BuildTestCaseSimple(t, tc)
Liz Kammerdfeb1202022-05-13 17:20:20 -0400760 })
761 }
762}
763
Jingwen Chen40067de2021-01-26 21:58:43 -0500764func TestLoadStatements(t *testing.T) {
765 testCases := []struct {
766 bazelTargets BazelTargets
767 expectedLoadStatements string
768 }{
769 {
770 bazelTargets: BazelTargets{
771 BazelTarget{
772 name: "foo",
773 ruleClass: "cc_library",
774 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
775 },
776 },
777 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
778 },
779 {
780 bazelTargets: BazelTargets{
781 BazelTarget{
782 name: "foo",
783 ruleClass: "cc_library",
784 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
785 },
786 BazelTarget{
787 name: "bar",
788 ruleClass: "cc_library",
789 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
790 },
791 },
792 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
793 },
794 {
795 bazelTargets: BazelTargets{
796 BazelTarget{
797 name: "foo",
798 ruleClass: "cc_library",
799 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
800 },
801 BazelTarget{
802 name: "bar",
803 ruleClass: "cc_binary",
804 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
805 },
806 },
807 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
808 },
809 {
810 bazelTargets: BazelTargets{
811 BazelTarget{
812 name: "foo",
813 ruleClass: "cc_library",
814 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
815 },
816 BazelTarget{
817 name: "bar",
818 ruleClass: "cc_binary",
819 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
820 },
821 BazelTarget{
822 name: "baz",
823 ruleClass: "java_binary",
824 bzlLoadLocation: "//build/bazel/rules:java.bzl",
825 },
826 },
827 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
828load("//build/bazel/rules:java.bzl", "java_binary")`,
829 },
830 {
831 bazelTargets: BazelTargets{
832 BazelTarget{
833 name: "foo",
834 ruleClass: "cc_binary",
835 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
836 },
837 BazelTarget{
838 name: "bar",
839 ruleClass: "java_binary",
840 bzlLoadLocation: "//build/bazel/rules:java.bzl",
841 },
842 BazelTarget{
843 name: "baz",
844 ruleClass: "genrule",
845 // Note: no bzlLoadLocation for native rules
846 },
847 },
848 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
849load("//build/bazel/rules:java.bzl", "java_binary")`,
850 },
851 }
852
853 for _, testCase := range testCases {
854 actual := testCase.bazelTargets.LoadStatements()
855 expected := testCase.expectedLoadStatements
856 if actual != expected {
857 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
858 }
859 }
860
861}
862
863func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
864 testCases := []struct {
865 bp string
866 expectedBazelTarget string
867 expectedBazelTargetCount int
868 expectedLoadStatements string
869 }{
870 {
871 bp: `custom {
872 name: "bar",
Liz Kammerdfeb1202022-05-13 17:20:20 -0400873 host_supported: true,
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400874 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500875 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500876}`,
877 expectedBazelTarget: `my_library(
878 name = "bar",
879)
880
Jingwen Chen40067de2021-01-26 21:58:43 -0500881proto_library(
882 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400883)
884
885my_proto_library(
886 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500887)`,
888 expectedBazelTargetCount: 3,
889 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
890load("//build/bazel/rules:rules.bzl", "my_library")`,
891 },
892 }
893
894 dir := "."
895 for _, testCase := range testCases {
896 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
897 ctx := android.NewTestContext(config)
Liz Kammerdfeb1202022-05-13 17:20:20 -0400898 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
Jingwen Chen40067de2021-01-26 21:58:43 -0500899 ctx.RegisterForBazelConversion()
900
901 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
902 android.FailIfErrored(t, errs)
903 _, errs = ctx.ResolveDependencies(config)
904 android.FailIfErrored(t, errs)
905
Cole Faustb85d1a12022-11-08 18:14:01 -0800906 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammer6eff3232021-08-26 08:37:59 -0400907 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
908 android.FailIfErrored(t, err)
Jingwen Chen40067de2021-01-26 21:58:43 -0500909 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
910 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
911 }
912
913 actualBazelTargets := bazelTargets.String()
914 if actualBazelTargets != testCase.expectedBazelTarget {
915 t.Errorf(
916 "Expected generated Bazel target to be '%s', got '%s'",
917 testCase.expectedBazelTarget,
918 actualBazelTargets,
919 )
920 }
921
922 actualLoadStatements := bazelTargets.LoadStatements()
923 if actualLoadStatements != testCase.expectedLoadStatements {
924 t.Errorf(
925 "Expected generated load statements to be '%s', got '%s'",
926 testCase.expectedLoadStatements,
927 actualLoadStatements,
928 )
929 }
930 }
931}
932
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500933func TestModuleTypeBp2Build(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000934 testCases := []Bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500935 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000936 Description: "filegroup with does not specify srcs",
937 ModuleTypeUnderTest: "filegroup",
938 ModuleTypeUnderTestFactory: android.FileGroupFactory,
939 Blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500940 name: "fg_foo",
941 bazel_module: { bp2build_available: true },
942}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000943 ExpectedBazelTargets: []string{
944 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerebfcf672021-02-16 15:00:05 -0500945 },
946 },
947 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000948 Description: "filegroup with no srcs",
949 ModuleTypeUnderTest: "filegroup",
950 ModuleTypeUnderTestFactory: android.FileGroupFactory,
951 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500952 name: "fg_foo",
953 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500954 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500955}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000956 ExpectedBazelTargets: []string{
957 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammer356f7d42021-01-26 09:18:53 -0500958 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500959 },
960 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000961 Description: "filegroup with srcs",
962 ModuleTypeUnderTest: "filegroup",
963 ModuleTypeUnderTestFactory: android.FileGroupFactory,
964 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500965 name: "fg_foo",
966 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500967 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500968}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000969 ExpectedBazelTargets: []string{
970 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500971 "srcs": `[
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500972 "a",
973 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500974 ]`,
975 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500976 },
977 },
978 {
Usta Shresthad5580312022-09-23 16:46:38 -0400979 Description: "filegroup with dot-slash-prefixed srcs",
980 ModuleTypeUnderTest: "filegroup",
981 ModuleTypeUnderTestFactory: android.FileGroupFactory,
982 Blueprint: `filegroup {
983 name: "fg_foo",
984 srcs: ["./a", "./b"],
985 bazel_module: { bp2build_available: true },
986}`,
987 ExpectedBazelTargets: []string{
988 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
989 "srcs": `[
990 "a",
991 "b",
992 ]`,
993 }),
994 },
995 },
996 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000997 Description: "filegroup with excludes srcs",
998 ModuleTypeUnderTest: "filegroup",
999 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1000 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -05001001 name: "fg_foo",
1002 srcs: ["a", "b"],
1003 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001004 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -05001005}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001006 ExpectedBazelTargets: []string{
1007 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001008 "srcs": `["b"]`,
1009 }),
Liz Kammer356f7d42021-01-26 09:18:53 -05001010 },
1011 },
1012 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001013 Description: "depends_on_other_dir_module",
1014 ModuleTypeUnderTest: "filegroup",
1015 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1016 Blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001017 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -05001018 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +00001019 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -05001020 "c",
1021 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001022 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -05001023}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001024 Filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -05001025 "other/Android.bp": `filegroup {
1026 name: "foo",
1027 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -04001028 bazel_module: { bp2build_available: true },
1029}`,
1030 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001031 ExpectedBazelTargets: []string{
1032 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001033 "srcs": `[
1034 "//other:foo",
1035 "c",
1036 ]`,
1037 }),
1038 },
Liz Kammer6eff3232021-08-26 08:37:59 -04001039 },
1040 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001041 Description: "depends_on_other_unconverted_module_error",
1042 ModuleTypeUnderTest: "filegroup",
1043 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1044 UnconvertedDepsMode: errorModulesUnconvertedDeps,
1045 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001046 name: "foobar",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001047 srcs: [
1048 ":foo",
1049 "c",
1050 ],
1051 bazel_module: { bp2build_available: true },
1052}`,
Sasha Smundakf2bb26f2022-08-04 11:28:15 -07001053 ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on unconverted modules: foo`),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001054 Filesystem: map[string]string{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001055 "other/Android.bp": `filegroup {
1056 name: "foo",
1057 srcs: ["a", "b"],
1058}`,
1059 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001060 },
Usta Shrestha808bc712022-09-23 23:18:18 -04001061 {
1062 Description: "depends_on_other_missing_module_error",
1063 ModuleTypeUnderTest: "filegroup",
1064 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1065 UnconvertedDepsMode: errorModulesUnconvertedDeps,
1066 Blueprint: `filegroup {
1067 name: "foobar",
1068 srcs: [
1069 "c",
1070 "//other:foo",
1071 "//other:goo",
1072 ],
1073 bazel_module: { bp2build_available: true },
1074}`,
1075 ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on missing modules: //other:goo`),
1076 Filesystem: map[string]string{"other/Android.bp": `filegroup {
1077 name: "foo",
1078 srcs: ["a"],
1079 bazel_module: { bp2build_available: true },
1080}
1081`,
1082 },
1083 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001084 }
1085
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001086 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001087 t.Run(testCase.Description, func(t *testing.T) {
1088 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
Liz Kammer6eff3232021-08-26 08:37:59 -04001089 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001090 }
1091}
Jingwen Chen041b1842021-02-01 00:23:25 -05001092
Jingwen Chen12b4c272021-03-10 02:05:59 -05001093func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001094 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001095 moduleTypeUnderTest string
1096 moduleTypeUnderTestFactory android.ModuleFactory
1097 bp string
1098 expectedCount int
1099 description string
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001100 }{
1101 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001102 description: "explicitly unavailable",
1103 moduleTypeUnderTest: "filegroup",
1104 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001105 bp: `filegroup {
1106 name: "foo",
1107 srcs: ["a", "b"],
1108 bazel_module: { bp2build_available: false },
1109}`,
1110 expectedCount: 0,
1111 },
1112 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001113 description: "implicitly unavailable",
1114 moduleTypeUnderTest: "filegroup",
1115 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001116 bp: `filegroup {
1117 name: "foo",
1118 srcs: ["a", "b"],
1119}`,
1120 expectedCount: 0,
1121 },
1122 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001123 description: "explicitly available",
1124 moduleTypeUnderTest: "filegroup",
1125 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001126 bp: `filegroup {
1127 name: "foo",
1128 srcs: ["a", "b"],
1129 bazel_module: { bp2build_available: true },
1130}`,
1131 expectedCount: 1,
1132 },
1133 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001134 description: "generates more than 1 target if needed",
1135 moduleTypeUnderTest: "custom",
Liz Kammerdfeb1202022-05-13 17:20:20 -04001136 moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001137 bp: `custom {
1138 name: "foo",
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001139 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001140 bazel_module: { bp2build_available: true },
1141}`,
1142 expectedCount: 3,
1143 },
1144 }
1145
1146 dir := "."
1147 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -04001148 t.Run(testCase.description, func(t *testing.T) {
1149 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1150 ctx := android.NewTestContext(config)
1151 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001152 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001153
Liz Kammer2ada09a2021-08-11 00:17:36 -04001154 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1155 android.FailIfErrored(t, errs)
1156 _, errs = ctx.ResolveDependencies(config)
1157 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001158
Cole Faustb85d1a12022-11-08 18:14:01 -08001159 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammer6eff3232021-08-26 08:37:59 -04001160 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1161 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001162 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1163 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1164 }
1165 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001166 }
1167}
Liz Kammerba3ea162021-02-17 13:22:03 -05001168
Jingwen Chen12b4c272021-03-10 02:05:59 -05001169func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1170 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001171 moduleTypeUnderTest string
1172 moduleTypeUnderTestFactory android.ModuleFactory
1173 expectedCount map[string]int
1174 description string
Sam Delmerico24c56032022-03-28 19:53:03 +00001175 bp2buildConfig allowlists.Bp2BuildConfig
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001176 checkDir string
1177 fs map[string]string
MarkDacek9c094ca2023-03-16 19:15:19 +00001178 forceEnabledModules []string
1179 expectedErrorMessages []string
Jingwen Chen12b4c272021-03-10 02:05:59 -05001180 }{
1181 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001182 description: "test bp2build config package and subpackages config",
1183 moduleTypeUnderTest: "filegroup",
1184 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001185 expectedCount: map[string]int{
1186 "migrated": 1,
1187 "migrated/but_not_really": 0,
1188 "migrated/but_not_really/but_really": 1,
1189 "not_migrated": 0,
1190 "also_not_migrated": 0,
1191 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001192 bp2buildConfig: allowlists.Bp2BuildConfig{
1193 "migrated": allowlists.Bp2BuildDefaultTrueRecursively,
1194 "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
1195 "not_migrated": allowlists.Bp2BuildDefaultFalse,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001196 },
1197 fs: map[string]string{
1198 "migrated/Android.bp": `filegroup { name: "a" }`,
1199 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1200 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1201 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1202 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1203 },
1204 },
1205 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001206 description: "test bp2build config opt-in and opt-out",
1207 moduleTypeUnderTest: "filegroup",
1208 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001209 expectedCount: map[string]int{
1210 "package-opt-in": 2,
1211 "package-opt-in/subpackage": 0,
1212 "package-opt-out": 1,
1213 "package-opt-out/subpackage": 0,
1214 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001215 bp2buildConfig: allowlists.Bp2BuildConfig{
1216 "package-opt-in": allowlists.Bp2BuildDefaultFalse,
1217 "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001218 },
1219 fs: map[string]string{
1220 "package-opt-in/Android.bp": `
1221filegroup { name: "opt-in-a" }
1222filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1223filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1224`,
1225
1226 "package-opt-in/subpackage/Android.bp": `
1227filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1228`,
1229
1230 "package-opt-out/Android.bp": `
1231filegroup { name: "opt-out-a" }
1232filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1233filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1234`,
1235
1236 "package-opt-out/subpackage/Android.bp": `
1237filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1238filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1239`,
1240 },
1241 },
MarkDacek9c094ca2023-03-16 19:15:19 +00001242 {
1243 description: "test force-enabled errors out",
1244 moduleTypeUnderTest: "filegroup",
1245 moduleTypeUnderTestFactory: android.FileGroupFactory,
1246 expectedCount: map[string]int{
1247 "migrated": 0,
1248 "not_migrated": 0,
1249 },
1250 bp2buildConfig: allowlists.Bp2BuildConfig{
1251 "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
1252 "not_migrated": allowlists.Bp2BuildDefaultFalse,
1253 },
1254 fs: map[string]string{
1255 "migrated/Android.bp": `filegroup { name: "a" }`,
1256 },
1257 forceEnabledModules: []string{"a"},
1258 expectedErrorMessages: []string{"Force Enabled Module a not converted"},
1259 },
Jingwen Chen12b4c272021-03-10 02:05:59 -05001260 }
1261
1262 dir := "."
1263 for _, testCase := range testCases {
1264 fs := make(map[string][]byte)
1265 toParse := []string{
1266 "Android.bp",
1267 }
1268 for f, content := range testCase.fs {
1269 if strings.HasSuffix(f, "Android.bp") {
1270 toParse = append(toParse, f)
1271 }
1272 fs[f] = []byte(content)
1273 }
1274 config := android.TestConfig(buildDir, nil, "", fs)
MarkDacek9c094ca2023-03-16 19:15:19 +00001275 config.AddForceEnabledModules(testCase.forceEnabledModules)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001276 ctx := android.NewTestContext(config)
1277 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Sam Delmerico24c56032022-03-28 19:53:03 +00001278 allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig)
1279 ctx.RegisterBp2BuildConfig(allowlist)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001280 ctx.RegisterForBazelConversion()
1281
1282 _, errs := ctx.ParseFileList(dir, toParse)
1283 android.FailIfErrored(t, errs)
1284 _, errs = ctx.ResolveDependencies(config)
1285 android.FailIfErrored(t, errs)
1286
Cole Faustb85d1a12022-11-08 18:14:01 -08001287 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Jingwen Chen12b4c272021-03-10 02:05:59 -05001288
1289 // For each directory, test that the expected number of generated targets is correct.
1290 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -04001291 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
MarkDacek9c094ca2023-03-16 19:15:19 +00001292 android.CheckErrorsAgainstExpectations(t, err, testCase.expectedErrorMessages)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001293 if actualCount := len(bazelTargets); actualCount != expectedCount {
1294 t.Fatalf(
1295 "%s: Expected %d bazel target for %s package, got %d",
1296 testCase.description,
1297 expectedCount,
1298 dir,
1299 actualCount)
1300 }
1301
1302 }
1303 }
1304}
1305
Liz Kammerba3ea162021-02-17 13:22:03 -05001306func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001307 testCases := []Bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -05001308 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001309 Description: "filegroup bazel_module.label",
1310 ModuleTypeUnderTest: "filegroup",
1311 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1312 Blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -05001313 name: "fg_foo",
1314 bazel_module: { label: "//other:fg_foo" },
1315}`,
Cole Faustea602c52022-08-31 14:48:26 -07001316 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001317 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001318 "other/BUILD.bazel": `// BUILD file`,
1319 },
1320 },
1321 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001322 Description: "multiple bazel_module.label same BUILD",
1323 ModuleTypeUnderTest: "filegroup",
1324 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1325 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001326 name: "fg_foo",
1327 bazel_module: { label: "//other:fg_foo" },
1328 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001329
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001330 filegroup {
1331 name: "foo",
1332 bazel_module: { label: "//other:foo" },
1333 }`,
Cole Faustea602c52022-08-31 14:48:26 -07001334 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001335 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001336 "other/BUILD.bazel": `// BUILD file`,
1337 },
1338 },
1339 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001340 Description: "filegroup bazel_module.label and bp2build in subdir",
1341 ModuleTypeUnderTest: "filegroup",
1342 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1343 Dir: "other",
1344 Blueprint: ``,
1345 Filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001346 "other/Android.bp": `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001347 name: "fg_foo",
1348 bazel_module: {
1349 bp2build_available: true,
1350 },
1351 }
1352 filegroup {
1353 name: "fg_bar",
1354 bazel_module: {
1355 label: "//other:fg_bar"
1356 },
1357 }`,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001358 "other/BUILD.bazel": `// definition for fg_bar`,
1359 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001360 ExpectedBazelTargets: []string{
1361 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001362 },
1363 },
1364 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001365 Description: "filegroup bazel_module.label and filegroup bp2build",
1366 ModuleTypeUnderTest: "filegroup",
1367 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001368
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001369 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001370 "other/BUILD.bazel": `// BUILD file`,
1371 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001372 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001373 name: "fg_foo",
1374 bazel_module: {
1375 label: "//other:fg_foo",
1376 },
1377 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001378
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001379 filegroup {
1380 name: "fg_bar",
1381 bazel_module: {
1382 bp2build_available: true,
1383 },
1384 }`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001385 ExpectedBazelTargets: []string{
1386 MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001387 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001388 },
1389 }
1390
1391 dir := "."
1392 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001393 t.Run(testCase.Description, func(t *testing.T) {
Jingwen Chen49109762021-05-25 05:16:48 +00001394 fs := make(map[string][]byte)
1395 toParse := []string{
1396 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001397 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001398 for f, content := range testCase.Filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001399 if strings.HasSuffix(f, "Android.bp") {
1400 toParse = append(toParse, f)
1401 }
1402 fs[f] = []byte(content)
1403 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001404 config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001405 ctx := android.NewTestContext(config)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001406 ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001407 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001408
Jingwen Chen49109762021-05-25 05:16:48 +00001409 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001410 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001411 return
1412 }
1413 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001414 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001415 return
1416 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001417
Jingwen Chen49109762021-05-25 05:16:48 +00001418 checkDir := dir
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001419 if testCase.Dir != "" {
1420 checkDir = testCase.Dir
Jingwen Chen49109762021-05-25 05:16:48 +00001421 }
Cole Faustb85d1a12022-11-08 18:14:01 -08001422 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Liz Kammer6eff3232021-08-26 08:37:59 -04001423 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1424 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001425 bazelTargets.sort()
1426 actualCount := len(bazelTargets)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001427 expectedCount := len(testCase.ExpectedBazelTargets)
Jingwen Chen49109762021-05-25 05:16:48 +00001428 if actualCount != expectedCount {
1429 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1430 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001431 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001432 actualContent := target.content
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001433 expectedContent := testCase.ExpectedBazelTargets[i]
Jingwen Chen49109762021-05-25 05:16:48 +00001434 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001435 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001436 "Expected generated Bazel target to be '%s', got '%s'",
1437 expectedContent,
1438 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001439 )
1440 }
1441 }
Jingwen Chen49109762021-05-25 05:16:48 +00001442 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001443 }
1444}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001445
Jingwen Chen0eeaeb82022-09-21 10:27:42 +00001446func TestGlob(t *testing.T) {
1447 testCases := []Bp2buildTestCase{
1448 {
1449 Description: "filegroup with glob",
1450 ModuleTypeUnderTest: "filegroup",
1451 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1452 Blueprint: `filegroup {
1453 name: "fg_foo",
1454 srcs: ["**/*.txt"],
1455 bazel_module: { bp2build_available: true },
1456}`,
1457 ExpectedBazelTargets: []string{
1458 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1459 "srcs": `[
1460 "other/a.txt",
1461 "other/b.txt",
1462 "other/subdir/a.txt",
1463 ]`,
1464 }),
1465 },
1466 Filesystem: map[string]string{
1467 "other/a.txt": "",
1468 "other/b.txt": "",
1469 "other/subdir/a.txt": "",
1470 "other/file": "",
1471 },
1472 },
1473 {
1474 Description: "filegroup with glob in subdir",
1475 ModuleTypeUnderTest: "filegroup",
1476 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1477 Dir: "other",
1478 Filesystem: map[string]string{
1479 "other/Android.bp": `filegroup {
1480 name: "fg_foo",
1481 srcs: ["**/*.txt"],
1482 bazel_module: { bp2build_available: true },
1483}`,
1484 "other/a.txt": "",
1485 "other/b.txt": "",
1486 "other/subdir/a.txt": "",
1487 "other/file": "",
1488 },
1489 ExpectedBazelTargets: []string{
1490 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1491 "srcs": `[
1492 "a.txt",
1493 "b.txt",
1494 "subdir/a.txt",
1495 ]`,
1496 }),
1497 },
1498 },
1499 {
1500 Description: "filegroup with glob with no kept BUILD files",
1501 ModuleTypeUnderTest: "filegroup",
1502 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1503 KeepBuildFileForDirs: []string{
1504 // empty
1505 },
1506 Blueprint: `filegroup {
1507 name: "fg_foo",
1508 srcs: ["**/*.txt"],
1509 bazel_module: { bp2build_available: true },
1510}`,
1511 Filesystem: map[string]string{
1512 "a.txt": "",
1513 "b.txt": "",
1514 "foo/BUILD": "",
1515 "foo/a.txt": "",
1516 "foo/bar/BUILD": "",
1517 "foo/bar/b.txt": "",
1518 },
1519 ExpectedBazelTargets: []string{
1520 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1521 "srcs": `[
1522 "a.txt",
1523 "b.txt",
1524 "foo/a.txt",
1525 "foo/bar/b.txt",
1526 ]`,
1527 }),
1528 },
1529 },
1530 {
1531 Description: "filegroup with glob with kept BUILD file",
1532 ModuleTypeUnderTest: "filegroup",
1533 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1534 KeepBuildFileForDirs: []string{
1535 "foo",
1536 },
1537 Blueprint: `filegroup {
1538 name: "fg_foo",
1539 srcs: ["**/*.txt"],
1540 bazel_module: { bp2build_available: true },
1541}`,
1542 Filesystem: map[string]string{
1543 "a.txt": "",
1544 "b.txt": "",
1545 "foo/BUILD": "",
1546 "foo/a.txt": "",
1547 "foo/bar/BUILD": "",
1548 "foo/bar/b.txt": "",
1549 },
1550 ExpectedBazelTargets: []string{
1551 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1552 "srcs": `[
1553 "a.txt",
1554 "b.txt",
1555 "//foo:a.txt",
1556 "//foo:bar/b.txt",
1557 ]`,
1558 }),
1559 },
1560 },
1561 {
1562 Description: "filegroup with glob with kept BUILD.bazel file",
1563 ModuleTypeUnderTest: "filegroup",
1564 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1565 KeepBuildFileForDirs: []string{
1566 "foo",
1567 },
1568 Blueprint: `filegroup {
1569 name: "fg_foo",
1570 srcs: ["**/*.txt"],
1571 bazel_module: { bp2build_available: true },
1572}`,
1573 Filesystem: map[string]string{
1574 "a.txt": "",
1575 "b.txt": "",
1576 "foo/BUILD.bazel": "",
1577 "foo/a.txt": "",
1578 "foo/bar/BUILD.bazel": "",
1579 "foo/bar/b.txt": "",
1580 },
1581 ExpectedBazelTargets: []string{
1582 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1583 "srcs": `[
1584 "a.txt",
1585 "b.txt",
1586 "//foo:a.txt",
1587 "//foo:bar/b.txt",
1588 ]`,
1589 }),
1590 },
1591 },
1592 {
1593 Description: "filegroup with glob with Android.bp file as boundary",
1594 ModuleTypeUnderTest: "filegroup",
1595 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1596 Blueprint: `filegroup {
1597 name: "fg_foo",
1598 srcs: ["**/*.txt"],
1599 bazel_module: { bp2build_available: true },
1600}`,
1601 Filesystem: map[string]string{
1602 "a.txt": "",
1603 "b.txt": "",
1604 "foo/Android.bp": "",
1605 "foo/a.txt": "",
1606 "foo/bar/Android.bp": "",
1607 "foo/bar/b.txt": "",
1608 },
1609 ExpectedBazelTargets: []string{
1610 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1611 "srcs": `[
1612 "a.txt",
1613 "b.txt",
1614 "//foo:a.txt",
1615 "//foo/bar:b.txt",
1616 ]`,
1617 }),
1618 },
1619 },
1620 {
1621 Description: "filegroup with glob in subdir with kept BUILD and BUILD.bazel file",
1622 ModuleTypeUnderTest: "filegroup",
1623 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1624 Dir: "other",
1625 KeepBuildFileForDirs: []string{
1626 "other/foo",
1627 "other/foo/bar",
1628 // deliberately not other/foo/baz/BUILD.
1629 },
1630 Filesystem: map[string]string{
1631 "other/Android.bp": `filegroup {
1632 name: "fg_foo",
1633 srcs: ["**/*.txt"],
1634 bazel_module: { bp2build_available: true },
1635}`,
1636 "other/a.txt": "",
1637 "other/b.txt": "",
1638 "other/foo/BUILD": "",
1639 "other/foo/a.txt": "",
1640 "other/foo/bar/BUILD.bazel": "",
1641 "other/foo/bar/b.txt": "",
1642 "other/foo/baz/BUILD": "",
1643 "other/foo/baz/c.txt": "",
1644 },
1645 ExpectedBazelTargets: []string{
1646 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1647 "srcs": `[
1648 "a.txt",
1649 "b.txt",
1650 "//other/foo:a.txt",
1651 "//other/foo/bar:b.txt",
1652 "//other/foo:baz/c.txt",
1653 ]`,
1654 }),
1655 },
1656 },
1657 }
1658
1659 for _, testCase := range testCases {
1660 t.Run(testCase.Description, func(t *testing.T) {
1661 RunBp2BuildTestCaseSimple(t, testCase)
1662 })
1663 }
1664}
1665
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001666func TestGlobExcludeSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001667 testCases := []Bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001668 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001669 Description: "filegroup top level exclude_srcs",
1670 ModuleTypeUnderTest: "filegroup",
1671 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1672 Blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001673 name: "fg_foo",
1674 srcs: ["**/*.txt"],
1675 exclude_srcs: ["c.txt"],
1676 bazel_module: { bp2build_available: true },
1677}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001678 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001679 "a.txt": "",
1680 "b.txt": "",
1681 "c.txt": "",
1682 "dir/Android.bp": "",
1683 "dir/e.txt": "",
1684 "dir/f.txt": "",
1685 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001686 ExpectedBazelTargets: []string{
1687 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001688 "srcs": `[
1689 "a.txt",
1690 "b.txt",
1691 "//dir:e.txt",
1692 "//dir:f.txt",
1693 ]`,
1694 }),
1695 },
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001696 },
1697 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001698 Description: "filegroup in subdir exclude_srcs",
1699 ModuleTypeUnderTest: "filegroup",
1700 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1701 Blueprint: "",
1702 Dir: "dir",
1703 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001704 "dir/Android.bp": `filegroup {
1705 name: "fg_foo",
1706 srcs: ["**/*.txt"],
1707 exclude_srcs: ["b.txt"],
1708 bazel_module: { bp2build_available: true },
1709}
1710`,
1711 "dir/a.txt": "",
1712 "dir/b.txt": "",
1713 "dir/subdir/Android.bp": "",
1714 "dir/subdir/e.txt": "",
1715 "dir/subdir/f.txt": "",
1716 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001717 ExpectedBazelTargets: []string{
1718 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001719 "srcs": `[
Liz Kammer9abd62d2021-05-21 08:37:59 -04001720 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001721 "//dir/subdir:e.txt",
1722 "//dir/subdir:f.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001723 ]`,
1724 }),
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001725 },
1726 },
1727 }
1728
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001729 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001730 t.Run(testCase.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001731 RunBp2BuildTestCaseSimple(t, testCase)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001732 })
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001733 }
1734}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001735
1736func TestCommonBp2BuildModuleAttrs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001737 testCases := []Bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001738 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001739 Description: "Required into data test",
1740 ModuleTypeUnderTest: "filegroup",
1741 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1742 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001743filegroup {
1744 name: "fg_foo",
1745 required: ["reqd"],
1746 bazel_module: { bp2build_available: true },
1747}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001748 ExpectedBazelTargets: []string{
1749 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001750 "data": `[":reqd"]`,
1751 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001752 },
1753 },
1754 {
Jingwen Chena5ecb372022-09-21 09:05:37 +00001755 Description: "Required into data test, cyclic self reference is filtered out",
1756 ModuleTypeUnderTest: "filegroup",
1757 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1758 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
1759filegroup {
1760 name: "fg_foo",
1761 required: ["reqd", "fg_foo"],
1762 bazel_module: { bp2build_available: true },
1763}`,
1764 ExpectedBazelTargets: []string{
1765 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1766 "data": `[":reqd"]`,
1767 }),
1768 },
1769 },
1770 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001771 Description: "Required via arch into data test",
1772 ModuleTypeUnderTest: "python_library",
1773 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1774 Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") +
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001775 simpleModuleDoNotConvertBp2build("python_library", "reqdarm") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001776python_library {
1777 name: "fg_foo",
1778 arch: {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001779 arm: {
1780 required: ["reqdarm"],
1781 },
1782 x86: {
1783 required: ["reqdx86"],
1784 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001785 },
1786 bazel_module: { bp2build_available: true },
1787}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001788 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001789 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001790 "data": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001791 "//build/bazel/platforms/arch:arm": [":reqdarm"],
1792 "//build/bazel/platforms/arch:x86": [":reqdx86"],
1793 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001794 })`,
1795 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001796 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001797 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001798 },
1799 },
1800 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001801 Description: "Required appended to data test",
1802 ModuleTypeUnderTest: "python_library",
1803 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1804 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001805 "data.bin": "",
1806 "src.py": "",
1807 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001808 Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001809python_library {
1810 name: "fg_foo",
1811 data: ["data.bin"],
1812 required: ["reqd"],
1813 bazel_module: { bp2build_available: true },
1814}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001815 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001816 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001817 "data": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001818 "data.bin",
1819 ":reqd",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001820 ]`,
1821 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001822 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001823 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001824 },
1825 },
1826 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001827 Description: "All props-to-attrs at once together test",
1828 ModuleTypeUnderTest: "filegroup",
1829 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1830 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001831filegroup {
1832 name: "fg_foo",
1833 required: ["reqd"],
1834 bazel_module: { bp2build_available: true },
1835}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001836 ExpectedBazelTargets: []string{
1837 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001838 "data": `[":reqd"]`,
1839 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001840 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001841 },
1842 }
1843
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001844 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001845 t.Run(tc.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001846 RunBp2BuildTestCaseSimple(t, tc)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001847 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001848 }
1849}
Sasha Smundak05b0ba62022-09-26 18:15:45 -07001850
1851func TestLicensesAttrConversion(t *testing.T) {
1852 RunBp2BuildTestCase(t,
1853 func(ctx android.RegistrationContext) {
1854 ctx.RegisterModuleType("license", android.LicenseFactory)
1855 },
1856 Bp2buildTestCase{
1857 Description: "Test that licenses: attribute is converted",
1858 ModuleTypeUnderTest: "filegroup",
1859 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1860 Blueprint: `
1861license {
1862 name: "my_license",
1863}
1864filegroup {
1865 name: "my_filegroup",
1866 licenses: ["my_license"],
1867}
1868`,
1869 ExpectedBazelTargets: []string{
1870 MakeBazelTargetNoRestrictions("filegroup", "my_filegroup", AttrNameToString{
1871 "applicable_licenses": `[":my_license"]`,
1872 }),
1873 MakeBazelTargetNoRestrictions("android_license", "my_license", AttrNameToString{}),
1874 },
1875 })
1876}
Spandan Das5af0bd32022-09-28 20:43:08 +00001877
1878func TestGenerateApiBazelTargets(t *testing.T) {
1879 bp := `
1880 custom {
1881 name: "foo",
1882 api: "foo.txt",
1883 }
1884 `
1885 expectedBazelTarget := MakeBazelTarget(
1886 "custom_api_contribution",
1887 "foo",
1888 AttrNameToString{
1889 "api": `"foo.txt"`,
1890 },
1891 )
1892 registerCustomModule := func(ctx android.RegistrationContext) {
1893 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
1894 }
1895 RunApiBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
1896 Blueprint: bp,
1897 ExpectedBazelTargets: []string{expectedBazelTarget},
1898 Description: "Generating API contribution Bazel targets for custom module",
1899 })
1900}
Spandan Das6a448ec2023-04-19 17:36:12 +00001901
1902func TestGenerateConfigSetting(t *testing.T) {
1903 bp := `
1904 custom {
1905 name: "foo",
1906 test_config_setting: true,
1907 }
1908 `
1909 expectedBazelTargets := []string{
1910 MakeBazelTargetNoRestrictions(
1911 "config_setting",
1912 "foo_config_setting",
1913 AttrNameToString{
1914 "flag_values": `{
1915 "//build/bazel/rules/my_string_setting": "foo",
1916 }`,
1917 },
1918 ),
1919 MakeBazelTarget(
1920 "custom",
1921 "foo",
1922 AttrNameToString{},
1923 ),
1924 }
1925 registerCustomModule := func(ctx android.RegistrationContext) {
1926 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
1927 }
1928 RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
1929 Blueprint: bp,
1930 ExpectedBazelTargets: expectedBazelTargets,
1931 Description: "Generating API contribution Bazel targets for custom module",
1932 })
1933}