blob: 0ca5c4e85d52656ee2486202201c8ae9f13a4b5a [file] [log] [blame]
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -05001// Copyright 2022 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 (
Chris Parsonscd209032023-09-19 01:12:48 +000018 "testing"
19
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -050020 "android/soong/android"
21 "android/soong/java"
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -050022)
23
Romain Jobredeauxafc5d272023-09-21 11:07:30 -040024func runAndroidLibraryImportTestWithRegistrationCtxFunc(t *testing.T, registrationCtxFunc func(ctx android.RegistrationContext), tc Bp2buildTestCase) {
25 t.Helper()
26 (&tc).ModuleTypeUnderTest = "android_library_import"
27 (&tc).ModuleTypeUnderTestFactory = java.AARImportFactory
28 RunBp2BuildTestCase(t, registrationCtxFunc, tc)
29}
30
31func runAndroidLibraryImportTest(t *testing.T, tc Bp2buildTestCase) {
32 runAndroidLibraryImportTestWithRegistrationCtxFunc(t, func(ctx android.RegistrationContext) {}, tc)
33}
34
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -050035func TestConvertAndroidLibrary(t *testing.T) {
36 t.Helper()
37 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
38 Description: "Android Library - simple example",
39 ModuleTypeUnderTest: "android_library",
40 ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
41 Filesystem: map[string]string{
42 "lib.java": "",
43 "arm.java": "",
44 "x86.java": "",
45 "res/res.png": "",
46 "manifest/AndroidManifest.xml": "",
47 },
Chris Parsonscd209032023-09-19 01:12:48 +000048 StubbedBuildDefinitions: []string{"static_lib_dep"},
49 Blueprint: simpleModule("android_library", "static_lib_dep") + `
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -050050android_library {
Liz Kammer02914402023-08-07 13:38:18 -040051 name: "TestLib",
52 srcs: ["lib.java"],
53 arch: {
54 arm: {
55 srcs: ["arm.java"],
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -050056 },
Liz Kammer02914402023-08-07 13:38:18 -040057 x86: {
58 srcs: ["x86.java"],
59 }
60 },
61 manifest: "manifest/AndroidManifest.xml",
62 static_libs: ["static_lib_dep"],
63 sdk_version: "current",
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -050064}
65`,
66 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000067 MakeBazelTarget(
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -050068 "android_library",
69 "TestLib",
70 AttrNameToString{
71 "srcs": `["lib.java"] + select({
72 "//build/bazel/platforms/arch:arm": ["arm.java"],
73 "//build/bazel/platforms/arch:x86": ["x86.java"],
74 "//conditions:default": [],
75 })`,
76 "manifest": `"manifest/AndroidManifest.xml"`,
77 "resource_files": `["res/res.png"]`,
78 "deps": `[":static_lib_dep"]`,
79 "exports": `[":static_lib_dep"]`,
Liz Kammer02914402023-08-07 13:38:18 -040080 "sdk_version": `"current"`, // use as default
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -050081 }),
Liz Kammer02914402023-08-07 13:38:18 -040082 MakeNeverlinkDuplicateTarget("android_library", "TestLib"),
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -050083 }})
84}
85
86func TestConvertAndroidLibraryWithNoSources(t *testing.T) {
87 t.Helper()
88 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
89 Description: "Android Library - modules with deps must have sources",
90 ModuleTypeUnderTest: "android_library",
91 ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
92 Filesystem: map[string]string{
93 "res/res.png": "",
94 "AndroidManifest.xml": "",
95 },
Chris Parsonscd209032023-09-19 01:12:48 +000096 Blueprint: simpleModule("android_library", "lib_dep") + `
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -050097android_library {
Liz Kammer02914402023-08-07 13:38:18 -040098 name: "TestLib",
99 srcs: [],
100 manifest: "AndroidManifest.xml",
101 libs: ["lib_dep"],
102 sdk_version: "current",
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -0500103}
104`,
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -0500105 ExpectedBazelTargets: []string{},
106 })
107}
108
109func TestConvertAndroidLibraryImport(t *testing.T) {
Romain Jobredeauxafc5d272023-09-21 11:07:30 -0400110 runAndroidLibraryImportTestWithRegistrationCtxFunc(t,
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -0500111 func(ctx android.RegistrationContext) {
112 ctx.RegisterModuleType("android_library", java.AndroidLibraryFactory)
113 },
114 Bp2buildTestCase{
Romain Jobredeauxafc5d272023-09-21 11:07:30 -0400115 Description: "Android Library Import",
Chris Parsons0c4de1f2023-09-21 20:36:35 +0000116 StubbedBuildDefinitions: []string{"static_lib_dep", "static_import_dep", "static_import_dep-neverlink"},
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -0500117 // Bazel's aar_import can only export *_import targets, so we expect
118 // only "static_import_dep" in exports, but both "static_lib_dep" and
119 // "static_import_dep" in deps
Chris Parsonscd209032023-09-19 01:12:48 +0000120 Blueprint: simpleModule("android_library", "static_lib_dep") + `
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -0500121android_library_import {
122 name: "TestImport",
123 aars: ["import.aar"],
124 static_libs: ["static_lib_dep", "static_import_dep"],
Liz Kammer02914402023-08-07 13:38:18 -0400125 sdk_version: "current",
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -0500126}
Chris Parsonscd209032023-09-19 01:12:48 +0000127
Chris Parsonscd209032023-09-19 01:12:48 +0000128android_library_import {
129 name: "static_import_dep",
Chris Parsons0c4de1f2023-09-21 20:36:35 +0000130 aars: ["import.aar"],
Chris Parsonscd209032023-09-19 01:12:48 +0000131}
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -0500132`,
133 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000134 MakeBazelTarget(
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -0500135 "aar_import",
136 "TestImport",
137 AttrNameToString{
138 "aar": `"import.aar"`,
139 "deps": `[
140 ":static_lib_dep",
141 ":static_import_dep",
142 ]`,
Liz Kammer02914402023-08-07 13:38:18 -0400143 "exports": `[":static_import_dep"]`,
144 "sdk_version": `"current"`, // use as default
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -0500145 },
146 ),
Alix14101de2023-01-06 03:42:07 +0000147 MakeNeverlinkDuplicateTarget("android_library", "TestImport"),
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -0500148 },
149 },
150 )
151}
Alix36795a72023-01-20 16:10:52 +0000152
153func TestConvertAndroidLibraryKotlin(t *testing.T) {
154 t.Helper()
155 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
156 Description: "Android Library with .kt srcs and common_srcs attribute",
157 ModuleTypeUnderTest: "android_library",
158 ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
159 Filesystem: map[string]string{
160 "AndroidManifest.xml": "",
161 },
162 Blueprint: `
163android_library {
Liz Kammer02914402023-08-07 13:38:18 -0400164 name: "TestLib",
165 srcs: ["a.java", "b.kt"],
166 common_srcs: ["c.kt"],
167 sdk_version: "current",
Alix36795a72023-01-20 16:10:52 +0000168}
169`,
170 ExpectedBazelTargets: []string{
171 MakeBazelTarget(
172 "android_library",
173 "TestLib",
174 AttrNameToString{
175 "srcs": `[
176 "a.java",
177 "b.kt",
178 ]`,
179 "common_srcs": `["c.kt"]`,
180 "manifest": `"AndroidManifest.xml"`,
181 "resource_files": `[]`,
Liz Kammer02914402023-08-07 13:38:18 -0400182 "sdk_version": `"current"`, // use as default
Alix36795a72023-01-20 16:10:52 +0000183 }),
184 MakeNeverlinkDuplicateTarget("android_library", "TestLib"),
185 }})
186}
Alixf848bf82023-03-06 19:43:55 +0000187
188func TestConvertAndroidLibraryKotlinCflags(t *testing.T) {
189 t.Helper()
190 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
191 Description: "Android Library with .kt srcs and kotlincflags ",
192 ModuleTypeUnderTest: "android_library",
193 ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
194 Filesystem: map[string]string{
195 "AndroidManifest.xml": "",
196 },
197 Blueprint: `
198android_library {
Liz Kammer02914402023-08-07 13:38:18 -0400199 name: "TestLib",
200 srcs: ["a.java", "b.kt"],
201 kotlincflags: ["-flag1", "-flag2"],
202 sdk_version: "current",
Alixf848bf82023-03-06 19:43:55 +0000203}
204`,
205 ExpectedBazelTargets: []string{
206 MakeBazelTarget(
207 "android_library",
208 "TestLib",
209 AttrNameToString{
210 "srcs": `[
211 "a.java",
212 "b.kt",
213 ]`,
214 "kotlincflags": `[
215 "-flag1",
216 "-flag2",
217 ]`,
218 "manifest": `"AndroidManifest.xml"`,
219 "resource_files": `[]`,
Liz Kammer02914402023-08-07 13:38:18 -0400220 "sdk_version": `"current"`, // use as default
Alixf848bf82023-03-06 19:43:55 +0000221 }),
222 MakeNeverlinkDuplicateTarget("android_library", "TestLib"),
223 }})
224}
Romain Jobredeauxafc5d272023-09-21 11:07:30 -0400225
226func TestAarImportFailsToConvertNoAars(t *testing.T) {
227 runAndroidLibraryImportTest(t,
228 Bp2buildTestCase{
229 Description: "Android Library Import with no aars does not convert.",
230 Blueprint: `
231android_library_import {
232 name: "no_aar_import",
233}
234`,
235 ExpectedBazelTargets: []string{},
236 })
237}