blob: 09d9dc1038deb7e1ebad54a8b932a83ab6c1399e [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 (
18 "android/soong/android"
19 "android/soong/java"
20 "fmt"
21
22 "testing"
23)
24
25func TestConvertAndroidLibrary(t *testing.T) {
26 t.Helper()
27 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
28 Description: "Android Library - simple example",
29 ModuleTypeUnderTest: "android_library",
30 ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
31 Filesystem: map[string]string{
32 "lib.java": "",
33 "arm.java": "",
34 "x86.java": "",
35 "res/res.png": "",
36 "manifest/AndroidManifest.xml": "",
37 },
38 Blueprint: simpleModuleDoNotConvertBp2build("android_library", "static_lib_dep") + `
39android_library {
40 name: "TestLib",
41 srcs: ["lib.java"],
42 arch: {
43 arm: {
44 srcs: ["arm.java"],
45 },
46 x86: {
47 srcs: ["x86.java"],
48 }
49 },
50 manifest: "manifest/AndroidManifest.xml",
51 static_libs: ["static_lib_dep"],
52 java_version: "7",
53}
54`,
55 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000056 MakeBazelTarget(
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -050057 "android_library",
58 "TestLib",
59 AttrNameToString{
60 "srcs": `["lib.java"] + select({
61 "//build/bazel/platforms/arch:arm": ["arm.java"],
62 "//build/bazel/platforms/arch:x86": ["x86.java"],
63 "//conditions:default": [],
64 })`,
65 "manifest": `"manifest/AndroidManifest.xml"`,
66 "resource_files": `["res/res.png"]`,
67 "deps": `[":static_lib_dep"]`,
68 "exports": `[":static_lib_dep"]`,
Romain Jobredeaux2eef2e12023-02-24 12:07:08 -050069 "java_version": `"7"`,
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -050070 }),
Romain Jobredeaux2eef2e12023-02-24 12:07:08 -050071 MakeNeverlinkDuplicateTargetWithAttrs(
72 "android_library",
73 "TestLib",
74 AttrNameToString{"java_version": `"7"`}),
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -050075 }})
76}
77
78func TestConvertAndroidLibraryWithNoSources(t *testing.T) {
79 t.Helper()
80 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
81 Description: "Android Library - modules with deps must have sources",
82 ModuleTypeUnderTest: "android_library",
83 ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
84 Filesystem: map[string]string{
85 "res/res.png": "",
86 "AndroidManifest.xml": "",
87 },
88 Blueprint: simpleModuleDoNotConvertBp2build("android_library", "lib_dep") + `
89android_library {
90 name: "TestLib",
91 srcs: [],
92 manifest: "AndroidManifest.xml",
93 libs: ["lib_dep"],
94}
95`,
96 ExpectedErr: fmt.Errorf("Module has direct dependencies but no sources. Bazel will not allow this."),
97 ExpectedBazelTargets: []string{},
98 })
99}
100
101func TestConvertAndroidLibraryImport(t *testing.T) {
102 t.Helper()
103 RunBp2BuildTestCase(
104 t,
105 func(ctx android.RegistrationContext) {
106 ctx.RegisterModuleType("android_library", java.AndroidLibraryFactory)
107 },
108 Bp2buildTestCase{
109 Description: "Android Library Import",
110 ModuleTypeUnderTest: "android_library_import",
111 ModuleTypeUnderTestFactory: java.AARImportFactory,
112 Filesystem: map[string]string{
113 "import.aar": "",
114 },
115 // Bazel's aar_import can only export *_import targets, so we expect
116 // only "static_import_dep" in exports, but both "static_lib_dep" and
117 // "static_import_dep" in deps
118 Blueprint: simpleModuleDoNotConvertBp2build("android_library", "static_lib_dep") +
119 simpleModuleDoNotConvertBp2build("android_library_import", "static_import_dep") + `
120android_library_import {
121 name: "TestImport",
122 aars: ["import.aar"],
123 static_libs: ["static_lib_dep", "static_import_dep"],
124}
125`,
126 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000127 MakeBazelTarget(
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -0500128 "aar_import",
129 "TestImport",
130 AttrNameToString{
131 "aar": `"import.aar"`,
132 "deps": `[
133 ":static_lib_dep",
134 ":static_import_dep",
135 ]`,
136 "exports": `[":static_import_dep"]`,
137 },
138 ),
Alix14101de2023-01-06 03:42:07 +0000139 MakeNeverlinkDuplicateTarget("android_library", "TestImport"),
Romain Jobredeauxc9b2bba2022-02-15 09:35:07 -0500140 },
141 },
142 )
143}
Alix36795a72023-01-20 16:10:52 +0000144
145func TestConvertAndroidLibraryKotlin(t *testing.T) {
146 t.Helper()
147 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
148 Description: "Android Library with .kt srcs and common_srcs attribute",
149 ModuleTypeUnderTest: "android_library",
150 ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
151 Filesystem: map[string]string{
152 "AndroidManifest.xml": "",
153 },
154 Blueprint: `
155android_library {
156 name: "TestLib",
157 srcs: ["a.java", "b.kt"],
158 common_srcs: ["c.kt"],
159}
160`,
161 ExpectedBazelTargets: []string{
162 MakeBazelTarget(
163 "android_library",
164 "TestLib",
165 AttrNameToString{
166 "srcs": `[
167 "a.java",
168 "b.kt",
169 ]`,
170 "common_srcs": `["c.kt"]`,
171 "manifest": `"AndroidManifest.xml"`,
172 "resource_files": `[]`,
173 }),
174 MakeNeverlinkDuplicateTarget("android_library", "TestLib"),
175 }})
176}
Alixf848bf82023-03-06 19:43:55 +0000177
178func TestConvertAndroidLibraryKotlinCflags(t *testing.T) {
179 t.Helper()
180 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
181 Description: "Android Library with .kt srcs and kotlincflags ",
182 ModuleTypeUnderTest: "android_library",
183 ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
184 Filesystem: map[string]string{
185 "AndroidManifest.xml": "",
186 },
187 Blueprint: `
188android_library {
189 name: "TestLib",
190 srcs: ["a.java", "b.kt"],
191 kotlincflags: ["-flag1", "-flag2"],
192}
193`,
194 ExpectedBazelTargets: []string{
195 MakeBazelTarget(
196 "android_library",
197 "TestLib",
198 AttrNameToString{
199 "srcs": `[
200 "a.java",
201 "b.kt",
202 ]`,
203 "kotlincflags": `[
204 "-flag1",
205 "-flag2",
206 ]`,
207 "manifest": `"AndroidManifest.xml"`,
208 "resource_files": `[]`,
209 }),
210 MakeNeverlinkDuplicateTarget("android_library", "TestLib"),
211 }})
212}