blob: d36d2a90a40482e994b7517d512411d45fae6545 [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 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000234 Description: "string ptr props",
235 Blueprint: `custom {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500236 name: "foo",
237 string_ptr_prop: "",
238 bazel_module: { bp2build_available: true },
239}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000240 ExpectedBazelTargets: []string{
241 makeBazelTarget("custom", "foo", AttrNameToString{
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500242 "string_ptr_prop": `""`,
243 }),
244 },
245 },
246 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000247 Description: "string props",
248 Blueprint: `custom {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400249 name: "foo",
Jingwen Chen73850672020-12-14 08:25:34 -0500250 string_list_prop: ["a", "b"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500251 string_ptr_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500252 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500253}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000254 ExpectedBazelTargets: []string{
255 makeBazelTarget("custom", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500256 "string_list_prop": `[
Jingwen Chen73850672020-12-14 08:25:34 -0500257 "a",
258 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500259 ]`,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500260 "string_ptr_prop": `"a"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500261 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400262 },
Jingwen Chen73850672020-12-14 08:25:34 -0500263 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000264 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000265 Description: "control characters",
266 Blueprint: `custom {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500267 name: "foo",
Jingwen Chen58a12b82021-03-30 13:08:36 +0000268 string_list_prop: ["\t", "\n"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500269 string_ptr_prop: "a\t\n\r",
Jingwen Chen58a12b82021-03-30 13:08:36 +0000270 bazel_module: { bp2build_available: true },
271}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000272 ExpectedBazelTargets: []string{
273 makeBazelTarget("custom", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500274 "string_list_prop": `[
Jingwen Chen58a12b82021-03-30 13:08:36 +0000275 "\t",
276 "\n",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500277 ]`,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500278 "string_ptr_prop": `"a\t\n\r"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500279 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400280 },
281 },
282 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000283 Description: "handles dep",
284 Blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400285 name: "has_dep",
286 arch_paths: [":dep"],
287 bazel_module: { bp2build_available: true },
288}
289
290custom {
291 name: "dep",
292 arch_paths: ["abc"],
293 bazel_module: { bp2build_available: true },
294}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000295 ExpectedBazelTargets: []string{
296 makeBazelTarget("custom", "dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500297 "arch_paths": `["abc"]`,
298 }),
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000299 makeBazelTarget("custom", "has_dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500300 "arch_paths": `[":dep"]`,
301 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400302 },
303 },
304 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000305 Description: "non-existent dep",
306 Blueprint: `custom {
Liz Kammerdaa09ef2021-12-15 15:35:38 -0500307 name: "has_dep",
308 arch_paths: [":dep"],
309 bazel_module: { bp2build_available: true },
310}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000311 ExpectedBazelTargets: []string{
312 makeBazelTarget("custom", "has_dep", AttrNameToString{
Liz Kammerdaa09ef2021-12-15 15:35:38 -0500313 "arch_paths": `[":dep__BP2BUILD__MISSING__DEP"]`,
314 }),
315 },
316 },
317 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000318 Description: "arch-variant srcs",
319 Blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400320 name: "arch_paths",
321 arch: {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400322 x86: { arch_paths: ["x86.txt"] },
323 x86_64: { arch_paths: ["x86_64.txt"] },
324 arm: { arch_paths: ["arm.txt"] },
325 arm64: { arch_paths: ["arm64.txt"] },
326 },
327 target: {
328 linux: { arch_paths: ["linux.txt"] },
329 bionic: { arch_paths: ["bionic.txt"] },
330 host: { arch_paths: ["host.txt"] },
331 not_windows: { arch_paths: ["not_windows.txt"] },
332 android: { arch_paths: ["android.txt"] },
333 linux_musl: { arch_paths: ["linux_musl.txt"] },
334 musl: { arch_paths: ["musl.txt"] },
335 linux_glibc: { arch_paths: ["linux_glibc.txt"] },
336 glibc: { arch_paths: ["glibc.txt"] },
337 linux_bionic: { arch_paths: ["linux_bionic.txt"] },
338 darwin: { arch_paths: ["darwin.txt"] },
339 windows: { arch_paths: ["windows.txt"] },
340 },
341 multilib: {
342 lib32: { arch_paths: ["lib32.txt"] },
343 lib64: { arch_paths: ["lib64.txt"] },
Liz Kammer4562a3b2021-04-21 18:15:34 -0400344 },
345 bazel_module: { bp2build_available: true },
346}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000347 ExpectedBazelTargets: []string{
348 makeBazelTarget("custom", "arch_paths", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500349 "arch_paths": `select({
Liz Kammerfdd72e62021-10-11 15:41:03 -0400350 "//build/bazel/platforms/arch:arm": [
351 "arm.txt",
352 "lib32.txt",
353 ],
354 "//build/bazel/platforms/arch:arm64": [
355 "arm64.txt",
356 "lib64.txt",
357 ],
358 "//build/bazel/platforms/arch:x86": [
359 "x86.txt",
360 "lib32.txt",
361 ],
362 "//build/bazel/platforms/arch:x86_64": [
363 "x86_64.txt",
364 "lib64.txt",
365 ],
366 "//conditions:default": [],
367 }) + select({
368 "//build/bazel/platforms/os:android": [
369 "linux.txt",
370 "bionic.txt",
371 "android.txt",
372 ],
373 "//build/bazel/platforms/os:darwin": [
374 "host.txt",
375 "darwin.txt",
376 "not_windows.txt",
377 ],
378 "//build/bazel/platforms/os:linux": [
379 "host.txt",
380 "linux.txt",
381 "glibc.txt",
382 "linux_glibc.txt",
383 "not_windows.txt",
384 ],
385 "//build/bazel/platforms/os:linux_bionic": [
386 "host.txt",
387 "linux.txt",
388 "bionic.txt",
389 "linux_bionic.txt",
390 "not_windows.txt",
391 ],
392 "//build/bazel/platforms/os:linux_musl": [
393 "host.txt",
394 "linux.txt",
395 "musl.txt",
396 "linux_musl.txt",
397 "not_windows.txt",
398 ],
399 "//build/bazel/platforms/os:windows": [
400 "host.txt",
401 "windows.txt",
402 ],
Liz Kammer4562a3b2021-04-21 18:15:34 -0400403 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500404 })`,
405 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400406 },
407 },
408 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000409 Description: "arch-variant deps",
410 Blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400411 name: "has_dep",
412 arch: {
413 x86: {
414 arch_paths: [":dep"],
415 },
416 },
417 bazel_module: { bp2build_available: true },
418}
419
420custom {
421 name: "dep",
422 arch_paths: ["abc"],
423 bazel_module: { bp2build_available: true },
424}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000425 ExpectedBazelTargets: []string{
426 makeBazelTarget("custom", "dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500427 "arch_paths": `["abc"]`,
428 }),
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000429 makeBazelTarget("custom", "has_dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500430 "arch_paths": `select({
Liz Kammer4562a3b2021-04-21 18:15:34 -0400431 "//build/bazel/platforms/arch:x86": [":dep"],
432 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500433 })`,
434 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400435 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000436 },
Liz Kammer32a03392021-09-14 11:17:21 -0400437 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000438 Description: "embedded props",
439 Blueprint: `custom {
Liz Kammer32a03392021-09-14 11:17:21 -0400440 name: "embedded_props",
441 embedded_prop: "abc",
442 bazel_module: { bp2build_available: true },
443}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000444 ExpectedBazelTargets: []string{
445 makeBazelTarget("custom", "embedded_props", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500446 "embedded_attr": `"abc"`,
447 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400448 },
449 },
450 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000451 Description: "ptr to embedded props",
452 Blueprint: `custom {
Liz Kammer32a03392021-09-14 11:17:21 -0400453 name: "ptr_to_embedded_props",
454 other_embedded_prop: "abc",
455 bazel_module: { bp2build_available: true },
456}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000457 ExpectedBazelTargets: []string{
458 makeBazelTarget("custom", "ptr_to_embedded_props", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500459 "other_embedded_attr": `"abc"`,
460 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400461 },
462 },
Jingwen Chen73850672020-12-14 08:25:34 -0500463 }
464
465 dir := "."
466 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000467 t.Run(testCase.Description, func(t *testing.T) {
468 config := android.TestConfig(buildDir, nil, testCase.Blueprint, nil)
Liz Kammerfdd72e62021-10-11 15:41:03 -0400469 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500470
Liz Kammerfdd72e62021-10-11 15:41:03 -0400471 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500472
Liz Kammerfdd72e62021-10-11 15:41:03 -0400473 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
474 if errored(t, testCase, errs) {
475 return
476 }
477 _, errs = ctx.ResolveDependencies(config)
478 if errored(t, testCase, errs) {
479 return
480 }
Jingwen Chen73850672020-12-14 08:25:34 -0500481
Liz Kammerfdd72e62021-10-11 15:41:03 -0400482 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
483 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
484 android.FailIfErrored(t, err)
Jingwen Chen164e0862021-02-19 00:48:40 -0500485
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000486 if actualCount, expectedCount := len(bazelTargets), len(testCase.ExpectedBazelTargets); actualCount != expectedCount {
487 t.Errorf("Expected %d bazel target (%s),\ngot %d (%s)", expectedCount, testCase.ExpectedBazelTargets, actualCount, bazelTargets)
Liz Kammerfdd72e62021-10-11 15:41:03 -0400488 } else {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000489 for i, expectedBazelTarget := range testCase.ExpectedBazelTargets {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400490 actualBazelTarget := bazelTargets[i]
491 if actualBazelTarget.content != expectedBazelTarget {
492 t.Errorf(
493 "Expected generated Bazel target to be '%s', got '%s'",
494 expectedBazelTarget,
495 actualBazelTarget.content,
496 )
497 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400498 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500499 }
Liz Kammerfdd72e62021-10-11 15:41:03 -0400500 })
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800501 }
502}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500503
Liz Kammerdfeb1202022-05-13 17:20:20 -0400504func TestBp2buildHostAndDevice(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000505 testCases := []Bp2buildTestCase{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400506 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000507 Description: "host and device, device only",
508 ModuleTypeUnderTest: "custom",
509 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
510 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400511 name: "foo",
512 bazel_module: { bp2build_available: true },
513}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000514 ExpectedBazelTargets: []string{
515 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400516 },
517 },
518 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000519 Description: "host and device, both",
520 ModuleTypeUnderTest: "custom",
521 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
522 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400523 name: "foo",
524 host_supported: true,
525 bazel_module: { bp2build_available: true },
526}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000527 ExpectedBazelTargets: []string{
528 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400529 },
530 },
531 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000532 Description: "host and device, host explicitly disabled",
533 ModuleTypeUnderTest: "custom",
534 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
535 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400536 name: "foo",
537 host_supported: false,
538 bazel_module: { bp2build_available: true },
539}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000540 ExpectedBazelTargets: []string{
541 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400542 },
543 },
544 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000545 Description: "host and device, neither",
546 ModuleTypeUnderTest: "custom",
547 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
548 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400549 name: "foo",
550 host_supported: false,
551 device_supported: false,
552 bazel_module: { bp2build_available: true },
553}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000554 ExpectedBazelTargets: []string{
555 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400556 "target_compatible_with": `["@platforms//:incompatible"]`,
557 }),
558 },
559 },
560 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000561 Description: "host and device, neither, cannot override with product_var",
562 ModuleTypeUnderTest: "custom",
563 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
564 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400565 name: "foo",
566 host_supported: false,
567 device_supported: false,
568 product_variables: { unbundled_build: { enabled: true } },
569 bazel_module: { bp2build_available: true },
570}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000571 ExpectedBazelTargets: []string{
572 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400573 "target_compatible_with": `["@platforms//:incompatible"]`,
574 }),
575 },
576 },
577 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000578 Description: "host and device, both, disabled overrided with product_var",
579 ModuleTypeUnderTest: "custom",
580 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
581 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400582 name: "foo",
583 host_supported: true,
584 device_supported: true,
585 enabled: false,
586 product_variables: { unbundled_build: { enabled: true } },
587 bazel_module: { bp2build_available: true },
588}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000589 ExpectedBazelTargets: []string{
590 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400591 "target_compatible_with": `["//build/bazel/product_variables:unbundled_build"]`,
592 }),
593 },
594 },
595 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000596 Description: "host and device, neither, cannot override with arch enabled",
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 arch: { x86: { enabled: true } },
604 bazel_module: { bp2build_available: true },
605}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000606 ExpectedBazelTargets: []string{
607 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400608 "target_compatible_with": `["@platforms//:incompatible"]`,
609 }),
610 },
611 },
612 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000613 Description: "host and device, host only",
614 ModuleTypeUnderTest: "custom",
615 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
616 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400617 name: "foo",
618 host_supported: true,
619 device_supported: false,
620 bazel_module: { bp2build_available: true },
621}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000622 ExpectedBazelTargets: []string{
623 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400624 },
625 },
626 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000627 Description: "host only",
628 ModuleTypeUnderTest: "custom",
629 ModuleTypeUnderTestFactory: customModuleFactoryHostSupported,
630 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400631 name: "foo",
632 bazel_module: { bp2build_available: true },
633}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000634 ExpectedBazelTargets: []string{
635 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400636 },
637 },
638 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000639 Description: "device only",
640 ModuleTypeUnderTest: "custom",
641 ModuleTypeUnderTestFactory: customModuleFactoryDeviceSupported,
642 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400643 name: "foo",
644 bazel_module: { bp2build_available: true },
645}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000646 ExpectedBazelTargets: []string{
647 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400648 },
649 },
650 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000651 Description: "host and device default, default",
652 ModuleTypeUnderTest: "custom",
653 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
654 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400655 name: "foo",
656 bazel_module: { bp2build_available: true },
657}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000658 ExpectedBazelTargets: []string{
659 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400660 },
661 },
662 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000663 Description: "host and device default, device only",
664 ModuleTypeUnderTest: "custom",
665 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
666 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400667 name: "foo",
668 host_supported: false,
669 bazel_module: { bp2build_available: true },
670}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000671 ExpectedBazelTargets: []string{
672 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400673 },
674 },
675 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000676 Description: "host and device default, host only",
677 ModuleTypeUnderTest: "custom",
678 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
679 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400680 name: "foo",
681 device_supported: false,
682 bazel_module: { bp2build_available: true },
683}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000684 ExpectedBazelTargets: []string{
685 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400686 },
687 },
688 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000689 Description: "host and device default, neither",
690 ModuleTypeUnderTest: "custom",
691 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
692 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400693 name: "foo",
694 host_supported: false,
695 device_supported: false,
696 bazel_module: { bp2build_available: true },
697}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000698 ExpectedBazelTargets: []string{
699 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400700 "target_compatible_with": `["@platforms//:incompatible"]`,
701 }),
702 },
703 },
704 }
705
706 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000707 t.Run(tc.Description, func(t *testing.T) {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400708 runBp2BuildTestCaseSimple(t, tc)
709 })
710 }
711}
712
Jingwen Chen40067de2021-01-26 21:58:43 -0500713func TestLoadStatements(t *testing.T) {
714 testCases := []struct {
715 bazelTargets BazelTargets
716 expectedLoadStatements string
717 }{
718 {
719 bazelTargets: BazelTargets{
720 BazelTarget{
721 name: "foo",
722 ruleClass: "cc_library",
723 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
724 },
725 },
726 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
727 },
728 {
729 bazelTargets: BazelTargets{
730 BazelTarget{
731 name: "foo",
732 ruleClass: "cc_library",
733 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
734 },
735 BazelTarget{
736 name: "bar",
737 ruleClass: "cc_library",
738 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
739 },
740 },
741 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
742 },
743 {
744 bazelTargets: BazelTargets{
745 BazelTarget{
746 name: "foo",
747 ruleClass: "cc_library",
748 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
749 },
750 BazelTarget{
751 name: "bar",
752 ruleClass: "cc_binary",
753 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
754 },
755 },
756 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
757 },
758 {
759 bazelTargets: BazelTargets{
760 BazelTarget{
761 name: "foo",
762 ruleClass: "cc_library",
763 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
764 },
765 BazelTarget{
766 name: "bar",
767 ruleClass: "cc_binary",
768 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
769 },
770 BazelTarget{
771 name: "baz",
772 ruleClass: "java_binary",
773 bzlLoadLocation: "//build/bazel/rules:java.bzl",
774 },
775 },
776 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
777load("//build/bazel/rules:java.bzl", "java_binary")`,
778 },
779 {
780 bazelTargets: BazelTargets{
781 BazelTarget{
782 name: "foo",
783 ruleClass: "cc_binary",
784 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
785 },
786 BazelTarget{
787 name: "bar",
788 ruleClass: "java_binary",
789 bzlLoadLocation: "//build/bazel/rules:java.bzl",
790 },
791 BazelTarget{
792 name: "baz",
793 ruleClass: "genrule",
794 // Note: no bzlLoadLocation for native rules
795 },
796 },
797 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
798load("//build/bazel/rules:java.bzl", "java_binary")`,
799 },
800 }
801
802 for _, testCase := range testCases {
803 actual := testCase.bazelTargets.LoadStatements()
804 expected := testCase.expectedLoadStatements
805 if actual != expected {
806 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
807 }
808 }
809
810}
811
812func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
813 testCases := []struct {
814 bp string
815 expectedBazelTarget string
816 expectedBazelTargetCount int
817 expectedLoadStatements string
818 }{
819 {
820 bp: `custom {
821 name: "bar",
Liz Kammerdfeb1202022-05-13 17:20:20 -0400822 host_supported: true,
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400823 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500824 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500825}`,
826 expectedBazelTarget: `my_library(
827 name = "bar",
828)
829
Jingwen Chen40067de2021-01-26 21:58:43 -0500830proto_library(
831 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400832)
833
834my_proto_library(
835 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500836)`,
837 expectedBazelTargetCount: 3,
838 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
839load("//build/bazel/rules:rules.bzl", "my_library")`,
840 },
841 }
842
843 dir := "."
844 for _, testCase := range testCases {
845 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
846 ctx := android.NewTestContext(config)
Liz Kammerdfeb1202022-05-13 17:20:20 -0400847 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
Jingwen Chen40067de2021-01-26 21:58:43 -0500848 ctx.RegisterForBazelConversion()
849
850 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
851 android.FailIfErrored(t, errs)
852 _, errs = ctx.ResolveDependencies(config)
853 android.FailIfErrored(t, errs)
854
Jingwen Chen164e0862021-02-19 00:48:40 -0500855 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400856 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
857 android.FailIfErrored(t, err)
Jingwen Chen40067de2021-01-26 21:58:43 -0500858 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
859 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
860 }
861
862 actualBazelTargets := bazelTargets.String()
863 if actualBazelTargets != testCase.expectedBazelTarget {
864 t.Errorf(
865 "Expected generated Bazel target to be '%s', got '%s'",
866 testCase.expectedBazelTarget,
867 actualBazelTargets,
868 )
869 }
870
871 actualLoadStatements := bazelTargets.LoadStatements()
872 if actualLoadStatements != testCase.expectedLoadStatements {
873 t.Errorf(
874 "Expected generated load statements to be '%s', got '%s'",
875 testCase.expectedLoadStatements,
876 actualLoadStatements,
877 )
878 }
879 }
880}
881
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500882func TestModuleTypeBp2Build(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000883 testCases := []Bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500884 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000885 Description: "filegroup with does not specify srcs",
886 ModuleTypeUnderTest: "filegroup",
887 ModuleTypeUnderTestFactory: android.FileGroupFactory,
888 Blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500889 name: "fg_foo",
890 bazel_module: { bp2build_available: true },
891}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000892 ExpectedBazelTargets: []string{
893 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerebfcf672021-02-16 15:00:05 -0500894 },
895 },
896 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000897 Description: "filegroup with no srcs",
898 ModuleTypeUnderTest: "filegroup",
899 ModuleTypeUnderTestFactory: android.FileGroupFactory,
900 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500901 name: "fg_foo",
902 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500903 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500904}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000905 ExpectedBazelTargets: []string{
906 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammer356f7d42021-01-26 09:18:53 -0500907 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500908 },
909 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000910 Description: "filegroup with srcs",
911 ModuleTypeUnderTest: "filegroup",
912 ModuleTypeUnderTestFactory: android.FileGroupFactory,
913 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500914 name: "fg_foo",
915 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500916 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500917}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000918 ExpectedBazelTargets: []string{
919 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500920 "srcs": `[
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500921 "a",
922 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500923 ]`,
924 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500925 },
926 },
927 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000928 Description: "filegroup with excludes srcs",
929 ModuleTypeUnderTest: "filegroup",
930 ModuleTypeUnderTestFactory: android.FileGroupFactory,
931 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500932 name: "fg_foo",
933 srcs: ["a", "b"],
934 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500935 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500936}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000937 ExpectedBazelTargets: []string{
938 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500939 "srcs": `["b"]`,
940 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500941 },
942 },
943 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000944 Description: "filegroup with glob",
945 ModuleTypeUnderTest: "filegroup",
946 ModuleTypeUnderTestFactory: android.FileGroupFactory,
947 Blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500948 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500949 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500950 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500951}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000952 ExpectedBazelTargets: []string{
953 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500954 "srcs": `[
Liz Kammer356f7d42021-01-26 09:18:53 -0500955 "other/a.txt",
956 "other/b.txt",
957 "other/subdir/a.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500958 ]`,
959 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500960 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000961 Filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500962 "other/a.txt": "",
963 "other/b.txt": "",
964 "other/subdir/a.txt": "",
965 "other/file": "",
966 },
967 },
968 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000969 Description: "filegroup with glob in subdir",
970 ModuleTypeUnderTest: "filegroup",
971 ModuleTypeUnderTestFactory: android.FileGroupFactory,
972 Dir: "other",
973 Filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500974 "other/Android.bp": `filegroup {
975 name: "fg_foo",
976 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500977 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500978}`,
979 "other/a.txt": "",
980 "other/b.txt": "",
981 "other/subdir/a.txt": "",
982 "other/file": "",
983 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000984 ExpectedBazelTargets: []string{
985 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500986 "srcs": `[
987 "a.txt",
988 "b.txt",
989 "subdir/a.txt",
990 ]`,
991 }),
992 },
Liz Kammer356f7d42021-01-26 09:18:53 -0500993 },
994 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000995 Description: "depends_on_other_dir_module",
996 ModuleTypeUnderTest: "filegroup",
997 ModuleTypeUnderTestFactory: android.FileGroupFactory,
998 Blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500999 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -05001000 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +00001001 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -05001002 "c",
1003 ],
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 Filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -05001007 "other/Android.bp": `filegroup {
1008 name: "foo",
1009 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -04001010 bazel_module: { bp2build_available: true },
1011}`,
1012 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001013 ExpectedBazelTargets: []string{
1014 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001015 "srcs": `[
1016 "//other:foo",
1017 "c",
1018 ]`,
1019 }),
1020 },
Liz Kammer6eff3232021-08-26 08:37:59 -04001021 },
1022 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001023 Description: "depends_on_other_unconverted_module_error",
1024 ModuleTypeUnderTest: "filegroup",
1025 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1026 UnconvertedDepsMode: errorModulesUnconvertedDeps,
1027 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001028 name: "foobar",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001029 srcs: [
1030 ":foo",
1031 "c",
1032 ],
1033 bazel_module: { bp2build_available: true },
1034}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001035 ExpectedErr: fmt.Errorf(`"foobar" depends on unconverted modules: foo`),
1036 Filesystem: map[string]string{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001037 "other/Android.bp": `filegroup {
1038 name: "foo",
1039 srcs: ["a", "b"],
1040}`,
1041 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001042 },
1043 }
1044
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001045 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001046 t.Run(testCase.Description, func(t *testing.T) {
1047 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
Liz Kammer6eff3232021-08-26 08:37:59 -04001048 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001049 }
1050}
Jingwen Chen041b1842021-02-01 00:23:25 -05001051
1052type bp2buildMutator = func(android.TopDownMutatorContext)
1053
Jingwen Chen12b4c272021-03-10 02:05:59 -05001054func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001055 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001056 moduleTypeUnderTest string
1057 moduleTypeUnderTestFactory android.ModuleFactory
1058 bp string
1059 expectedCount int
1060 description string
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001061 }{
1062 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001063 description: "explicitly unavailable",
1064 moduleTypeUnderTest: "filegroup",
1065 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001066 bp: `filegroup {
1067 name: "foo",
1068 srcs: ["a", "b"],
1069 bazel_module: { bp2build_available: false },
1070}`,
1071 expectedCount: 0,
1072 },
1073 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001074 description: "implicitly unavailable",
1075 moduleTypeUnderTest: "filegroup",
1076 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001077 bp: `filegroup {
1078 name: "foo",
1079 srcs: ["a", "b"],
1080}`,
1081 expectedCount: 0,
1082 },
1083 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001084 description: "explicitly available",
1085 moduleTypeUnderTest: "filegroup",
1086 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001087 bp: `filegroup {
1088 name: "foo",
1089 srcs: ["a", "b"],
1090 bazel_module: { bp2build_available: true },
1091}`,
1092 expectedCount: 1,
1093 },
1094 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001095 description: "generates more than 1 target if needed",
1096 moduleTypeUnderTest: "custom",
Liz Kammerdfeb1202022-05-13 17:20:20 -04001097 moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001098 bp: `custom {
1099 name: "foo",
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001100 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001101 bazel_module: { bp2build_available: true },
1102}`,
1103 expectedCount: 3,
1104 },
1105 }
1106
1107 dir := "."
1108 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -04001109 t.Run(testCase.description, func(t *testing.T) {
1110 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1111 ctx := android.NewTestContext(config)
1112 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001113 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001114
Liz Kammer2ada09a2021-08-11 00:17:36 -04001115 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1116 android.FailIfErrored(t, errs)
1117 _, errs = ctx.ResolveDependencies(config)
1118 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001119
Liz Kammer2ada09a2021-08-11 00:17:36 -04001120 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -04001121 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1122 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001123 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1124 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1125 }
1126 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001127 }
1128}
Liz Kammerba3ea162021-02-17 13:22:03 -05001129
Jingwen Chen12b4c272021-03-10 02:05:59 -05001130func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1131 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001132 moduleTypeUnderTest string
1133 moduleTypeUnderTestFactory android.ModuleFactory
1134 expectedCount map[string]int
1135 description string
Sam Delmerico24c56032022-03-28 19:53:03 +00001136 bp2buildConfig allowlists.Bp2BuildConfig
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001137 checkDir string
1138 fs map[string]string
Jingwen Chen12b4c272021-03-10 02:05:59 -05001139 }{
1140 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001141 description: "test bp2build config package and subpackages config",
1142 moduleTypeUnderTest: "filegroup",
1143 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001144 expectedCount: map[string]int{
1145 "migrated": 1,
1146 "migrated/but_not_really": 0,
1147 "migrated/but_not_really/but_really": 1,
1148 "not_migrated": 0,
1149 "also_not_migrated": 0,
1150 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001151 bp2buildConfig: allowlists.Bp2BuildConfig{
1152 "migrated": allowlists.Bp2BuildDefaultTrueRecursively,
1153 "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
1154 "not_migrated": allowlists.Bp2BuildDefaultFalse,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001155 },
1156 fs: map[string]string{
1157 "migrated/Android.bp": `filegroup { name: "a" }`,
1158 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1159 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1160 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1161 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1162 },
1163 },
1164 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001165 description: "test bp2build config opt-in and opt-out",
1166 moduleTypeUnderTest: "filegroup",
1167 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001168 expectedCount: map[string]int{
1169 "package-opt-in": 2,
1170 "package-opt-in/subpackage": 0,
1171 "package-opt-out": 1,
1172 "package-opt-out/subpackage": 0,
1173 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001174 bp2buildConfig: allowlists.Bp2BuildConfig{
1175 "package-opt-in": allowlists.Bp2BuildDefaultFalse,
1176 "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001177 },
1178 fs: map[string]string{
1179 "package-opt-in/Android.bp": `
1180filegroup { name: "opt-in-a" }
1181filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1182filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1183`,
1184
1185 "package-opt-in/subpackage/Android.bp": `
1186filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1187`,
1188
1189 "package-opt-out/Android.bp": `
1190filegroup { name: "opt-out-a" }
1191filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1192filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1193`,
1194
1195 "package-opt-out/subpackage/Android.bp": `
1196filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1197filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1198`,
1199 },
1200 },
1201 }
1202
1203 dir := "."
1204 for _, testCase := range testCases {
1205 fs := make(map[string][]byte)
1206 toParse := []string{
1207 "Android.bp",
1208 }
1209 for f, content := range testCase.fs {
1210 if strings.HasSuffix(f, "Android.bp") {
1211 toParse = append(toParse, f)
1212 }
1213 fs[f] = []byte(content)
1214 }
1215 config := android.TestConfig(buildDir, nil, "", fs)
1216 ctx := android.NewTestContext(config)
1217 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Sam Delmerico24c56032022-03-28 19:53:03 +00001218 allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig)
1219 ctx.RegisterBp2BuildConfig(allowlist)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001220 ctx.RegisterForBazelConversion()
1221
1222 _, errs := ctx.ParseFileList(dir, toParse)
1223 android.FailIfErrored(t, errs)
1224 _, errs = ctx.ResolveDependencies(config)
1225 android.FailIfErrored(t, errs)
1226
1227 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1228
1229 // For each directory, test that the expected number of generated targets is correct.
1230 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -04001231 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1232 android.FailIfErrored(t, err)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001233 if actualCount := len(bazelTargets); actualCount != expectedCount {
1234 t.Fatalf(
1235 "%s: Expected %d bazel target for %s package, got %d",
1236 testCase.description,
1237 expectedCount,
1238 dir,
1239 actualCount)
1240 }
1241
1242 }
1243 }
1244}
1245
Liz Kammerba3ea162021-02-17 13:22:03 -05001246func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001247 testCases := []Bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -05001248 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001249 Description: "filegroup bazel_module.label",
1250 ModuleTypeUnderTest: "filegroup",
1251 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1252 Blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -05001253 name: "fg_foo",
1254 bazel_module: { label: "//other:fg_foo" },
1255}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001256 ExpectedBazelTargets: []string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001257 `// BUILD file`,
1258 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001259 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001260 "other/BUILD.bazel": `// BUILD file`,
1261 },
1262 },
1263 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001264 Description: "multiple bazel_module.label same BUILD",
1265 ModuleTypeUnderTest: "filegroup",
1266 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1267 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001268 name: "fg_foo",
1269 bazel_module: { label: "//other:fg_foo" },
1270 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001271
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001272 filegroup {
1273 name: "foo",
1274 bazel_module: { label: "//other:foo" },
1275 }`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001276 ExpectedBazelTargets: []string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001277 `// BUILD file`,
1278 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001279 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001280 "other/BUILD.bazel": `// BUILD file`,
1281 },
1282 },
1283 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001284 Description: "filegroup bazel_module.label and bp2build in subdir",
1285 ModuleTypeUnderTest: "filegroup",
1286 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1287 Dir: "other",
1288 Blueprint: ``,
1289 Filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001290 "other/Android.bp": `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001291 name: "fg_foo",
1292 bazel_module: {
1293 bp2build_available: true,
1294 },
1295 }
1296 filegroup {
1297 name: "fg_bar",
1298 bazel_module: {
1299 label: "//other:fg_bar"
1300 },
1301 }`,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001302 "other/BUILD.bazel": `// definition for fg_bar`,
1303 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001304 ExpectedBazelTargets: []string{
1305 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001306 `// definition for fg_bar`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001307 },
1308 },
1309 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001310 Description: "filegroup bazel_module.label and filegroup bp2build",
1311 ModuleTypeUnderTest: "filegroup",
1312 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001313
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001314 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001315 "other/BUILD.bazel": `// BUILD file`,
1316 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001317 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001318 name: "fg_foo",
1319 bazel_module: {
1320 label: "//other:fg_foo",
1321 },
1322 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001323
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001324 filegroup {
1325 name: "fg_bar",
1326 bazel_module: {
1327 bp2build_available: true,
1328 },
1329 }`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001330 ExpectedBazelTargets: []string{
1331 MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001332 `// BUILD file`,
1333 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001334 },
1335 }
1336
1337 dir := "."
1338 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001339 t.Run(testCase.Description, func(t *testing.T) {
Jingwen Chen49109762021-05-25 05:16:48 +00001340 fs := make(map[string][]byte)
1341 toParse := []string{
1342 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001343 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001344 for f, content := range testCase.Filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001345 if strings.HasSuffix(f, "Android.bp") {
1346 toParse = append(toParse, f)
1347 }
1348 fs[f] = []byte(content)
1349 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001350 config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001351 ctx := android.NewTestContext(config)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001352 ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001353 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001354
Jingwen Chen49109762021-05-25 05:16:48 +00001355 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001356 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001357 return
1358 }
1359 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001360 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001361 return
1362 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001363
Jingwen Chen49109762021-05-25 05:16:48 +00001364 checkDir := dir
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001365 if testCase.Dir != "" {
1366 checkDir = testCase.Dir
Jingwen Chen49109762021-05-25 05:16:48 +00001367 }
Liz Kammer6eff3232021-08-26 08:37:59 -04001368 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1369 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1370 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001371 bazelTargets.sort()
1372 actualCount := len(bazelTargets)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001373 expectedCount := len(testCase.ExpectedBazelTargets)
Jingwen Chen49109762021-05-25 05:16:48 +00001374 if actualCount != expectedCount {
1375 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1376 }
1377 if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
1378 t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
1379 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001380 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001381 actualContent := target.content
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001382 expectedContent := testCase.ExpectedBazelTargets[i]
Jingwen Chen49109762021-05-25 05:16:48 +00001383 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001384 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001385 "Expected generated Bazel target to be '%s', got '%s'",
1386 expectedContent,
1387 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001388 )
1389 }
1390 }
Jingwen Chen49109762021-05-25 05:16:48 +00001391 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001392 }
1393}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001394
1395func TestGlobExcludeSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001396 testCases := []Bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001397 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001398 Description: "filegroup top level exclude_srcs",
1399 ModuleTypeUnderTest: "filegroup",
1400 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1401 Blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001402 name: "fg_foo",
1403 srcs: ["**/*.txt"],
1404 exclude_srcs: ["c.txt"],
1405 bazel_module: { bp2build_available: true },
1406}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001407 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001408 "a.txt": "",
1409 "b.txt": "",
1410 "c.txt": "",
1411 "dir/Android.bp": "",
1412 "dir/e.txt": "",
1413 "dir/f.txt": "",
1414 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001415 ExpectedBazelTargets: []string{
1416 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001417 "srcs": `[
1418 "a.txt",
1419 "b.txt",
1420 "//dir:e.txt",
1421 "//dir:f.txt",
1422 ]`,
1423 }),
1424 },
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001425 },
1426 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001427 Description: "filegroup in subdir exclude_srcs",
1428 ModuleTypeUnderTest: "filegroup",
1429 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1430 Blueprint: "",
1431 Dir: "dir",
1432 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001433 "dir/Android.bp": `filegroup {
1434 name: "fg_foo",
1435 srcs: ["**/*.txt"],
1436 exclude_srcs: ["b.txt"],
1437 bazel_module: { bp2build_available: true },
1438}
1439`,
1440 "dir/a.txt": "",
1441 "dir/b.txt": "",
1442 "dir/subdir/Android.bp": "",
1443 "dir/subdir/e.txt": "",
1444 "dir/subdir/f.txt": "",
1445 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001446 ExpectedBazelTargets: []string{
1447 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001448 "srcs": `[
Liz Kammer9abd62d2021-05-21 08:37:59 -04001449 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001450 "//dir/subdir:e.txt",
1451 "//dir/subdir:f.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001452 ]`,
1453 }),
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001454 },
1455 },
1456 }
1457
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001458 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001459 t.Run(testCase.Description, func(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001460 runBp2BuildTestCaseSimple(t, testCase)
1461 })
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001462 }
1463}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001464
1465func TestCommonBp2BuildModuleAttrs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001466 testCases := []Bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001467 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001468 Description: "Required into data test",
1469 ModuleTypeUnderTest: "filegroup",
1470 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1471 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001472filegroup {
1473 name: "fg_foo",
1474 required: ["reqd"],
1475 bazel_module: { bp2build_available: true },
1476}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001477 ExpectedBazelTargets: []string{
1478 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001479 "data": `[":reqd"]`,
1480 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001481 },
1482 },
1483 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001484 Description: "Required via arch into data test",
1485 ModuleTypeUnderTest: "python_library",
1486 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1487 Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") +
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001488 simpleModuleDoNotConvertBp2build("python_library", "reqdarm") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001489python_library {
1490 name: "fg_foo",
1491 arch: {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001492 arm: {
1493 required: ["reqdarm"],
1494 },
1495 x86: {
1496 required: ["reqdx86"],
1497 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001498 },
1499 bazel_module: { bp2build_available: true },
1500}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001501 ExpectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001502 makeBazelTarget("py_library", "fg_foo", map[string]string{
1503 "data": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001504 "//build/bazel/platforms/arch:arm": [":reqdarm"],
1505 "//build/bazel/platforms/arch:x86": [":reqdx86"],
1506 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001507 })`,
1508 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001509 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001510 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001511 },
1512 },
1513 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001514 Description: "Required appended to data test",
1515 ModuleTypeUnderTest: "python_library",
1516 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1517 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001518 "data.bin": "",
1519 "src.py": "",
1520 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001521 Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001522python_library {
1523 name: "fg_foo",
1524 data: ["data.bin"],
1525 required: ["reqd"],
1526 bazel_module: { bp2build_available: true },
1527}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001528 ExpectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001529 makeBazelTarget("py_library", "fg_foo", map[string]string{
1530 "data": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001531 "data.bin",
1532 ":reqd",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001533 ]`,
1534 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001535 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001536 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001537 },
1538 },
1539 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001540 Description: "All props-to-attrs at once together test",
1541 ModuleTypeUnderTest: "filegroup",
1542 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1543 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001544filegroup {
1545 name: "fg_foo",
1546 required: ["reqd"],
1547 bazel_module: { bp2build_available: true },
1548}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001549 ExpectedBazelTargets: []string{
1550 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001551 "data": `[":reqd"]`,
1552 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001553 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001554 },
1555 }
1556
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001557 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001558 t.Run(tc.Description, func(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001559 runBp2BuildTestCaseSimple(t, tc)
1560 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001561 }
1562}