blob: 4b141c954fc42c68f8f7ad7d439551e801e2d87d [file] [log] [blame]
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -04001// Copyright 2021 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/apex"
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040020 "android/soong/cc"
21 "android/soong/java"
Jingwen Chenb07c9012021-12-08 10:05:45 +000022 "android/soong/sh"
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040023
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -040024 "testing"
25)
26
27func runApexTestCase(t *testing.T, tc bp2buildTestCase) {
28 t.Helper()
29 runBp2BuildTestCase(t, registerApexModuleTypes, tc)
30}
31
32func registerApexModuleTypes(ctx android.RegistrationContext) {
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040033 // CC module types needed as they can be APEX dependencies
34 cc.RegisterCCBuildComponents(ctx)
35
Jingwen Chenb07c9012021-12-08 10:05:45 +000036 ctx.RegisterModuleType("sh_binary", sh.ShBinaryFactory)
37 ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040038 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
39 ctx.RegisterModuleType("apex_key", apex.ApexKeyFactory)
40 ctx.RegisterModuleType("android_app_certificate", java.AndroidAppCertificateFactory)
41 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -040042}
43
44func TestApexBundleSimple(t *testing.T) {
45 runApexTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -040046 description: "apex - example with all props",
47 moduleTypeUnderTest: "apex",
48 moduleTypeUnderTestFactory: apex.BundleFactory,
49 filesystem: map[string]string{},
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -040050 blueprint: `
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040051apex_key {
Jingwen Chenb07c9012021-12-08 10:05:45 +000052 name: "com.android.apogee.key",
53 public_key: "com.android.apogee.avbpubkey",
54 private_key: "com.android.apogee.pem",
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040055 bazel_module: { bp2build_available: false },
56}
57
58android_app_certificate {
Jingwen Chenb07c9012021-12-08 10:05:45 +000059 name: "com.android.apogee.certificate",
60 certificate: "com.android.apogee",
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040061 bazel_module: { bp2build_available: false },
62}
63
64cc_library {
Jingwen Chenb07c9012021-12-08 10:05:45 +000065 name: "native_shared_lib_1",
66 bazel_module: { bp2build_available: false },
67}
68
69cc_library {
70 name: "native_shared_lib_2",
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040071 bazel_module: { bp2build_available: false },
72}
73
Rupert Shuttleworth9447e1e2021-07-28 05:53:42 -040074// TODO(b/194878861): Add bp2build support for prebuilt_etc
75cc_library {
Jingwen Chenb07c9012021-12-08 10:05:45 +000076 name: "pretend_prebuilt_1",
77 bazel_module: { bp2build_available: false },
Rupert Shuttleworth9447e1e2021-07-28 05:53:42 -040078}
79
80// TODO(b/194878861): Add bp2build support for prebuilt_etc
81cc_library {
Jingwen Chenb07c9012021-12-08 10:05:45 +000082 name: "pretend_prebuilt_2",
83 bazel_module: { bp2build_available: false },
Rupert Shuttleworth9447e1e2021-07-28 05:53:42 -040084}
85
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040086filegroup {
87 name: "com.android.apogee-file_contexts",
Jingwen Chenb07c9012021-12-08 10:05:45 +000088 srcs: [
89 "com.android.apogee-file_contexts",
90 ],
91 bazel_module: { bp2build_available: false },
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040092}
93
Jingwen Chenb07c9012021-12-08 10:05:45 +000094cc_binary { name: "cc_binary_1", bazel_module: { bp2build_available: false } }
95sh_binary { name: "sh_binary_2", bazel_module: { bp2build_available: false } }
96
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -040097apex {
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040098 name: "com.android.apogee",
99 manifest: "apogee_manifest.json",
100 androidManifest: "ApogeeAndroidManifest.xml",
Jingwen Chenb07c9012021-12-08 10:05:45 +0000101 file_contexts: "com.android.apogee-file_contexts",
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400102 min_sdk_version: "29",
103 key: "com.android.apogee.key",
104 certificate: "com.android.apogee.certificate",
105 updatable: false,
106 installable: false,
107 native_shared_libs: [
108 "native_shared_lib_1",
109 "native_shared_lib_2",
110 ],
111 binaries: [
Jingwen Chenb07c9012021-12-08 10:05:45 +0000112 "cc_binary_1",
113 "sh_binary_2",
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400114 ],
Rupert Shuttleworth9447e1e2021-07-28 05:53:42 -0400115 prebuilts: [
116 "pretend_prebuilt_1",
117 "pretend_prebuilt_2",
118 ],
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400119}
120`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500121 expectedBazelTargets: []string{
122 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
123 "android_manifest": `"ApogeeAndroidManifest.xml"`,
124 "binaries": `[
Jingwen Chenb07c9012021-12-08 10:05:45 +0000125 ":cc_binary_1",
126 ":sh_binary_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500127 ]`,
128 "certificate": `":com.android.apogee.certificate"`,
129 "file_contexts": `":com.android.apogee-file_contexts"`,
130 "installable": "False",
131 "key": `":com.android.apogee.key"`,
132 "manifest": `"apogee_manifest.json"`,
133 "min_sdk_version": `"29"`,
Yu Liu4ae55d12022-01-05 17:17:23 -0800134 "native_shared_libs_32": `[
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400135 ":native_shared_lib_1",
136 ":native_shared_lib_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500137 ]`,
Yu Liu4ae55d12022-01-05 17:17:23 -0800138 "native_shared_libs_64": `select({
139 "//build/bazel/platforms/arch:arm64": [
140 ":native_shared_lib_1",
141 ":native_shared_lib_2",
142 ],
143 "//build/bazel/platforms/arch:x86_64": [
144 ":native_shared_lib_1",
145 ":native_shared_lib_2",
146 ],
147 "//conditions:default": [],
148 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500149 "prebuilts": `[
Rupert Shuttleworth9447e1e2021-07-28 05:53:42 -0400150 ":pretend_prebuilt_1",
151 ":pretend_prebuilt_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500152 ]`,
153 "updatable": "False",
154 }),
155 }})
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400156}
157
Yu Liu4ae55d12022-01-05 17:17:23 -0800158func TestApexBundleCompileMultilibBoth(t *testing.T) {
159 runApexTestCase(t, bp2buildTestCase{
160 description: "apex - example with compile_multilib=both",
161 moduleTypeUnderTest: "apex",
162 moduleTypeUnderTestFactory: apex.BundleFactory,
163 filesystem: map[string]string{},
164 blueprint: createMultilibBlueprint("both"),
165 expectedBazelTargets: []string{
166 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
167 "native_shared_libs_32": `[
168 ":native_shared_lib_1",
169 ":native_shared_lib_3",
170 ] + select({
171 "//build/bazel/platforms/arch:arm": [":native_shared_lib_2"],
172 "//build/bazel/platforms/arch:x86": [":native_shared_lib_2"],
173 "//conditions:default": [],
174 })`,
175 "native_shared_libs_64": `select({
176 "//build/bazel/platforms/arch:arm64": [
177 ":native_shared_lib_1",
178 ":native_shared_lib_4",
179 ":native_shared_lib_2",
180 ],
181 "//build/bazel/platforms/arch:x86_64": [
182 ":native_shared_lib_1",
183 ":native_shared_lib_4",
184 ":native_shared_lib_2",
185 ],
186 "//conditions:default": [],
187 })`,
188 }),
189 }})
190}
191
192func TestApexBundleCompileMultilibFirst(t *testing.T) {
193 runApexTestCase(t, bp2buildTestCase{
194 description: "apex - example with compile_multilib=first",
195 moduleTypeUnderTest: "apex",
196 moduleTypeUnderTestFactory: apex.BundleFactory,
197 filesystem: map[string]string{},
198 blueprint: createMultilibBlueprint("first"),
199 expectedBazelTargets: []string{
200 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
201 "native_shared_libs_32": `select({
202 "//build/bazel/platforms/arch:arm": [
203 ":native_shared_lib_1",
204 ":native_shared_lib_3",
205 ":native_shared_lib_2",
206 ],
207 "//build/bazel/platforms/arch:x86": [
208 ":native_shared_lib_1",
209 ":native_shared_lib_3",
210 ":native_shared_lib_2",
211 ],
212 "//conditions:default": [],
213 })`,
214 "native_shared_libs_64": `select({
215 "//build/bazel/platforms/arch:arm64": [
216 ":native_shared_lib_1",
217 ":native_shared_lib_4",
218 ":native_shared_lib_2",
219 ],
220 "//build/bazel/platforms/arch:x86_64": [
221 ":native_shared_lib_1",
222 ":native_shared_lib_4",
223 ":native_shared_lib_2",
224 ],
225 "//conditions:default": [],
226 })`,
227 }),
228 }})
229}
230
231func TestApexBundleCompileMultilib32(t *testing.T) {
232 runApexTestCase(t, bp2buildTestCase{
233 description: "apex - example with compile_multilib=32",
234 moduleTypeUnderTest: "apex",
235 moduleTypeUnderTestFactory: apex.BundleFactory,
236 filesystem: map[string]string{},
237 blueprint: createMultilibBlueprint("32"),
238 expectedBazelTargets: []string{
239 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
240 "native_shared_libs_32": `[
241 ":native_shared_lib_1",
242 ":native_shared_lib_3",
243 ] + select({
244 "//build/bazel/platforms/arch:arm": [":native_shared_lib_2"],
245 "//build/bazel/platforms/arch:x86": [":native_shared_lib_2"],
246 "//conditions:default": [],
247 })`,
248 }),
249 }})
250}
251
252func TestApexBundleCompileMultilib64(t *testing.T) {
253 runApexTestCase(t, bp2buildTestCase{
254 description: "apex - example with compile_multilib=64",
255 moduleTypeUnderTest: "apex",
256 moduleTypeUnderTestFactory: apex.BundleFactory,
257 filesystem: map[string]string{},
258 blueprint: createMultilibBlueprint("64"),
259 expectedBazelTargets: []string{
260 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
261 "native_shared_libs_64": `select({
262 "//build/bazel/platforms/arch:arm64": [
263 ":native_shared_lib_1",
264 ":native_shared_lib_4",
265 ":native_shared_lib_2",
266 ],
267 "//build/bazel/platforms/arch:x86_64": [
268 ":native_shared_lib_1",
269 ":native_shared_lib_4",
270 ":native_shared_lib_2",
271 ],
272 "//conditions:default": [],
273 })`,
274 }),
275 }})
276}
277
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400278func TestApexBundleDefaultPropertyValues(t *testing.T) {
279 runApexTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400280 description: "apex - default property values",
281 moduleTypeUnderTest: "apex",
282 moduleTypeUnderTestFactory: apex.BundleFactory,
283 filesystem: map[string]string{},
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400284 blueprint: `
285apex {
286 name: "com.android.apogee",
287 manifest: "apogee_manifest.json",
288}
289`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500290 expectedBazelTargets: []string{makeBazelTarget("apex", "com.android.apogee", attrNameToString{
291 "manifest": `"apogee_manifest.json"`,
292 }),
293 }})
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400294}
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000295
296func TestApexBundleHasBazelModuleProps(t *testing.T) {
297 runApexTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400298 description: "apex - has bazel module props",
299 moduleTypeUnderTest: "apex",
300 moduleTypeUnderTestFactory: apex.BundleFactory,
301 filesystem: map[string]string{},
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000302 blueprint: `
303apex {
304 name: "apogee",
305 manifest: "manifest.json",
306 bazel_module: { bp2build_available: true },
307}
308`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500309 expectedBazelTargets: []string{makeBazelTarget("apex", "apogee", attrNameToString{
310 "manifest": `"manifest.json"`,
311 }),
312 }})
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000313}
Yu Liu4ae55d12022-01-05 17:17:23 -0800314
315func createMultilibBlueprint(compile_multilib string) string {
316 return `
317cc_library {
318 name: "native_shared_lib_1",
319 bazel_module: { bp2build_available: false },
320}
321
322cc_library {
323 name: "native_shared_lib_2",
324 bazel_module: { bp2build_available: false },
325}
326
327cc_library {
328 name: "native_shared_lib_3",
329 bazel_module: { bp2build_available: false },
330}
331
332cc_library {
333 name: "native_shared_lib_4",
334 bazel_module: { bp2build_available: false },
335}
336
337apex {
338 name: "com.android.apogee",
339 compile_multilib: "` + compile_multilib + `",
340 multilib: {
341 both: {
342 native_shared_libs: [
343 "native_shared_lib_1",
344 ],
345 },
346 first: {
347 native_shared_libs: [
348 "native_shared_lib_2",
349 ],
350 },
351 lib32: {
352 native_shared_libs: [
353 "native_shared_lib_3",
354 ],
355 },
356 lib64: {
357 native_shared_libs: [
358 "native_shared_lib_4",
359 ],
360 },
361 },
362}`
363}