• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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"
20	"android/soong/cc"
21	"android/soong/java"
22	"android/soong/sh"
23
24	"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) {
33	// CC module types needed as they can be APEX dependencies
34	cc.RegisterCCBuildComponents(ctx)
35
36	ctx.RegisterModuleType("sh_binary", sh.ShBinaryFactory)
37	ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
38	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)
42}
43
44func TestApexBundleSimple(t *testing.T) {
45	runApexTestCase(t, bp2buildTestCase{
46		description:                "apex - example with all props",
47		moduleTypeUnderTest:        "apex",
48		moduleTypeUnderTestFactory: apex.BundleFactory,
49		filesystem:                 map[string]string{},
50		blueprint: `
51apex_key {
52	name: "com.android.apogee.key",
53	public_key: "com.android.apogee.avbpubkey",
54	private_key: "com.android.apogee.pem",
55	bazel_module: { bp2build_available: false },
56}
57
58android_app_certificate {
59	name: "com.android.apogee.certificate",
60	certificate: "com.android.apogee",
61	bazel_module: { bp2build_available: false },
62}
63
64cc_library {
65	name: "native_shared_lib_1",
66	bazel_module: { bp2build_available: false },
67}
68
69cc_library {
70	name: "native_shared_lib_2",
71	bazel_module: { bp2build_available: false },
72}
73
74// TODO(b/194878861): Add bp2build support for prebuilt_etc
75cc_library {
76	name: "pretend_prebuilt_1",
77	bazel_module: { bp2build_available: false },
78}
79
80// TODO(b/194878861): Add bp2build support for prebuilt_etc
81cc_library {
82	name: "pretend_prebuilt_2",
83	bazel_module: { bp2build_available: false },
84}
85
86filegroup {
87	name: "com.android.apogee-file_contexts",
88	srcs: [
89			"com.android.apogee-file_contexts",
90	],
91	bazel_module: { bp2build_available: false },
92}
93
94cc_binary { name: "cc_binary_1", bazel_module: { bp2build_available: false } }
95sh_binary { name: "sh_binary_2", bazel_module: { bp2build_available: false } }
96
97apex {
98	name: "com.android.apogee",
99	manifest: "apogee_manifest.json",
100	androidManifest: "ApogeeAndroidManifest.xml",
101	file_contexts: "com.android.apogee-file_contexts",
102	min_sdk_version: "29",
103	key: "com.android.apogee.key",
104	certificate: "com.android.apogee.certificate",
105	updatable: false,
106	installable: false,
107	compressible: false,
108	native_shared_libs: [
109	    "native_shared_lib_1",
110	    "native_shared_lib_2",
111	],
112	binaries: [
113		"cc_binary_1",
114		"sh_binary_2",
115	],
116	prebuilts: [
117	    "pretend_prebuilt_1",
118	    "pretend_prebuilt_2",
119	],
120}
121`,
122		expectedBazelTargets: []string{
123			makeBazelTarget("apex", "com.android.apogee", attrNameToString{
124				"android_manifest": `"ApogeeAndroidManifest.xml"`,
125				"binaries": `[
126        ":cc_binary_1",
127        ":sh_binary_2",
128    ]`,
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"`,
135				"native_shared_libs_32": `[
136        ":native_shared_lib_1",
137        ":native_shared_lib_2",
138    ]`,
139				"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    })`,
150				"prebuilts": `[
151        ":pretend_prebuilt_1",
152        ":pretend_prebuilt_2",
153    ]`,
154				"updatable":    "False",
155				"compressible": "False",
156			}),
157		}})
158}
159
160func 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
280func TestApexBundleDefaultPropertyValues(t *testing.T) {
281	runApexTestCase(t, bp2buildTestCase{
282		description:                "apex - default property values",
283		moduleTypeUnderTest:        "apex",
284		moduleTypeUnderTestFactory: apex.BundleFactory,
285		filesystem:                 map[string]string{},
286		blueprint: `
287apex {
288	name: "com.android.apogee",
289	manifest: "apogee_manifest.json",
290}
291`,
292		expectedBazelTargets: []string{makeBazelTarget("apex", "com.android.apogee", attrNameToString{
293			"manifest": `"apogee_manifest.json"`,
294		}),
295		}})
296}
297
298func TestApexBundleHasBazelModuleProps(t *testing.T) {
299	runApexTestCase(t, bp2buildTestCase{
300		description:                "apex - has bazel module props",
301		moduleTypeUnderTest:        "apex",
302		moduleTypeUnderTestFactory: apex.BundleFactory,
303		filesystem:                 map[string]string{},
304		blueprint: `
305apex {
306	name: "apogee",
307	manifest: "manifest.json",
308	bazel_module: { bp2build_available: true },
309}
310`,
311		expectedBazelTargets: []string{makeBazelTarget("apex", "apogee", attrNameToString{
312			"manifest": `"manifest.json"`,
313		}),
314		}})
315}
316
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}
366