blob: 90571893c24ecd625fc5898abe1c00b66164226b [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,
Wei Lif034cb42022-01-19 15:54:31 -0800107 compressible: false,
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400108 native_shared_libs: [
109 "native_shared_lib_1",
110 "native_shared_lib_2",
111 ],
112 binaries: [
Jingwen Chenb07c9012021-12-08 10:05:45 +0000113 "cc_binary_1",
114 "sh_binary_2",
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400115 ],
Rupert Shuttleworth9447e1e2021-07-28 05:53:42 -0400116 prebuilts: [
117 "pretend_prebuilt_1",
118 "pretend_prebuilt_2",
119 ],
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400120}
121`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500122 expectedBazelTargets: []string{
123 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
124 "android_manifest": `"ApogeeAndroidManifest.xml"`,
125 "binaries": `[
Jingwen Chenb07c9012021-12-08 10:05:45 +0000126 ":cc_binary_1",
127 ":sh_binary_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500128 ]`,
129 "certificate": `":com.android.apogee.certificate"`,
130 "file_contexts": `":com.android.apogee-file_contexts"`,
131 "installable": "False",
132 "key": `":com.android.apogee.key"`,
133 "manifest": `"apogee_manifest.json"`,
134 "min_sdk_version": `"29"`,
Yu Liu4ae55d12022-01-05 17:17:23 -0800135 "native_shared_libs_32": `[
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400136 ":native_shared_lib_1",
137 ":native_shared_lib_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500138 ]`,
Yu Liu4ae55d12022-01-05 17:17:23 -0800139 "native_shared_libs_64": `select({
140 "//build/bazel/platforms/arch:arm64": [
141 ":native_shared_lib_1",
142 ":native_shared_lib_2",
143 ],
144 "//build/bazel/platforms/arch:x86_64": [
145 ":native_shared_lib_1",
146 ":native_shared_lib_2",
147 ],
148 "//conditions:default": [],
149 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500150 "prebuilts": `[
Rupert Shuttleworth9447e1e2021-07-28 05:53:42 -0400151 ":pretend_prebuilt_1",
152 ":pretend_prebuilt_2",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500153 ]`,
Wei Lif034cb42022-01-19 15:54:31 -0800154 "updatable": "False",
155 "compressible": "False",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500156 }),
157 }})
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400158}
159
Yu Liu4ae55d12022-01-05 17:17:23 -0800160func TestApexBundleCompileMultilibBoth(t *testing.T) {
161 runApexTestCase(t, bp2buildTestCase{
162 description: "apex - example with compile_multilib=both",
163 moduleTypeUnderTest: "apex",
164 moduleTypeUnderTestFactory: apex.BundleFactory,
165 filesystem: map[string]string{},
166 blueprint: createMultilibBlueprint("both"),
167 expectedBazelTargets: []string{
168 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
169 "native_shared_libs_32": `[
170 ":native_shared_lib_1",
171 ":native_shared_lib_3",
172 ] + select({
173 "//build/bazel/platforms/arch:arm": [":native_shared_lib_2"],
174 "//build/bazel/platforms/arch:x86": [":native_shared_lib_2"],
175 "//conditions:default": [],
176 })`,
177 "native_shared_libs_64": `select({
178 "//build/bazel/platforms/arch:arm64": [
179 ":native_shared_lib_1",
180 ":native_shared_lib_4",
181 ":native_shared_lib_2",
182 ],
183 "//build/bazel/platforms/arch:x86_64": [
184 ":native_shared_lib_1",
185 ":native_shared_lib_4",
186 ":native_shared_lib_2",
187 ],
188 "//conditions:default": [],
189 })`,
190 }),
191 }})
192}
193
194func TestApexBundleCompileMultilibFirst(t *testing.T) {
195 runApexTestCase(t, bp2buildTestCase{
196 description: "apex - example with compile_multilib=first",
197 moduleTypeUnderTest: "apex",
198 moduleTypeUnderTestFactory: apex.BundleFactory,
199 filesystem: map[string]string{},
200 blueprint: createMultilibBlueprint("first"),
201 expectedBazelTargets: []string{
202 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
203 "native_shared_libs_32": `select({
204 "//build/bazel/platforms/arch:arm": [
205 ":native_shared_lib_1",
206 ":native_shared_lib_3",
207 ":native_shared_lib_2",
208 ],
209 "//build/bazel/platforms/arch:x86": [
210 ":native_shared_lib_1",
211 ":native_shared_lib_3",
212 ":native_shared_lib_2",
213 ],
214 "//conditions:default": [],
215 })`,
216 "native_shared_libs_64": `select({
217 "//build/bazel/platforms/arch:arm64": [
218 ":native_shared_lib_1",
219 ":native_shared_lib_4",
220 ":native_shared_lib_2",
221 ],
222 "//build/bazel/platforms/arch:x86_64": [
223 ":native_shared_lib_1",
224 ":native_shared_lib_4",
225 ":native_shared_lib_2",
226 ],
227 "//conditions:default": [],
228 })`,
229 }),
230 }})
231}
232
233func TestApexBundleCompileMultilib32(t *testing.T) {
234 runApexTestCase(t, bp2buildTestCase{
235 description: "apex - example with compile_multilib=32",
236 moduleTypeUnderTest: "apex",
237 moduleTypeUnderTestFactory: apex.BundleFactory,
238 filesystem: map[string]string{},
239 blueprint: createMultilibBlueprint("32"),
240 expectedBazelTargets: []string{
241 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
242 "native_shared_libs_32": `[
243 ":native_shared_lib_1",
244 ":native_shared_lib_3",
245 ] + select({
246 "//build/bazel/platforms/arch:arm": [":native_shared_lib_2"],
247 "//build/bazel/platforms/arch:x86": [":native_shared_lib_2"],
248 "//conditions:default": [],
249 })`,
250 }),
251 }})
252}
253
254func TestApexBundleCompileMultilib64(t *testing.T) {
255 runApexTestCase(t, bp2buildTestCase{
256 description: "apex - example with compile_multilib=64",
257 moduleTypeUnderTest: "apex",
258 moduleTypeUnderTestFactory: apex.BundleFactory,
259 filesystem: map[string]string{},
260 blueprint: createMultilibBlueprint("64"),
261 expectedBazelTargets: []string{
262 makeBazelTarget("apex", "com.android.apogee", attrNameToString{
263 "native_shared_libs_64": `select({
264 "//build/bazel/platforms/arch:arm64": [
265 ":native_shared_lib_1",
266 ":native_shared_lib_4",
267 ":native_shared_lib_2",
268 ],
269 "//build/bazel/platforms/arch:x86_64": [
270 ":native_shared_lib_1",
271 ":native_shared_lib_4",
272 ":native_shared_lib_2",
273 ],
274 "//conditions:default": [],
275 })`,
276 }),
277 }})
278}
279
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400280func TestApexBundleDefaultPropertyValues(t *testing.T) {
281 runApexTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400282 description: "apex - default property values",
283 moduleTypeUnderTest: "apex",
284 moduleTypeUnderTestFactory: apex.BundleFactory,
285 filesystem: map[string]string{},
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400286 blueprint: `
287apex {
288 name: "com.android.apogee",
289 manifest: "apogee_manifest.json",
290}
291`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500292 expectedBazelTargets: []string{makeBazelTarget("apex", "com.android.apogee", attrNameToString{
293 "manifest": `"apogee_manifest.json"`,
294 }),
295 }})
Rupert Shuttlewortha9d76dd2021-07-02 07:17:16 -0400296}
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000297
298func TestApexBundleHasBazelModuleProps(t *testing.T) {
299 runApexTestCase(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400300 description: "apex - has bazel module props",
301 moduleTypeUnderTest: "apex",
302 moduleTypeUnderTestFactory: apex.BundleFactory,
303 filesystem: map[string]string{},
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000304 blueprint: `
305apex {
306 name: "apogee",
307 manifest: "manifest.json",
308 bazel_module: { bp2build_available: true },
309}
310`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500311 expectedBazelTargets: []string{makeBazelTarget("apex", "apogee", attrNameToString{
312 "manifest": `"manifest.json"`,
313 }),
314 }})
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000315}
Yu Liu4ae55d12022-01-05 17:17:23 -0800316
317func createMultilibBlueprint(compile_multilib string) string {
318 return `
319cc_library {
320 name: "native_shared_lib_1",
321 bazel_module: { bp2build_available: false },
322}
323
324cc_library {
325 name: "native_shared_lib_2",
326 bazel_module: { bp2build_available: false },
327}
328
329cc_library {
330 name: "native_shared_lib_3",
331 bazel_module: { bp2build_available: false },
332}
333
334cc_library {
335 name: "native_shared_lib_4",
336 bazel_module: { bp2build_available: false },
337}
338
339apex {
340 name: "com.android.apogee",
341 compile_multilib: "` + compile_multilib + `",
342 multilib: {
343 both: {
344 native_shared_libs: [
345 "native_shared_lib_1",
346 ],
347 },
348 first: {
349 native_shared_libs: [
350 "native_shared_lib_2",
351 ],
352 },
353 lib32: {
354 native_shared_libs: [
355 "native_shared_lib_3",
356 ],
357 },
358 lib64: {
359 native_shared_libs: [
360 "native_shared_lib_4",
361 ],
362 },
363 },
364}`
365}