blob: ffbee8694cc6312d22b82e6e540fd89111fb2fd8 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 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 apex
16
17import (
Jiyong Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070020 "reflect"
Jooyung Han31c470b2019-10-18 16:26:59 +090021 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090022 "strings"
23 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090024
25 "github.com/google/blueprint/proptools"
26
27 "android/soong/android"
28 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090029 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090030)
31
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070032var buildDir string
33
Jooyung Hand3639552019-08-09 12:57:43 +090034// names returns name list from white space separated string
35func names(s string) (ns []string) {
36 for _, n := range strings.Split(s, " ") {
37 if len(n) > 0 {
38 ns = append(ns, n)
39 }
40 }
41 return
42}
43
Jooyung Han344d5432019-08-23 11:17:39 +090044func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
45 t.Helper()
46 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090047 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
48 if len(errs) > 0 {
49 android.FailIfNoMatchingErrors(t, pattern, errs)
50 return
51 }
52 _, errs = ctx.PrepareBuildActions(config)
53 if len(errs) > 0 {
54 android.FailIfNoMatchingErrors(t, pattern, errs)
55 return
56 }
57
58 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
59}
60
Jooyung Han344d5432019-08-23 11:17:39 +090061func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
62 t.Helper()
63 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090064 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
65 android.FailIfErrored(t, errs)
66 _, errs = ctx.PrepareBuildActions(config)
67 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070068 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090069}
70
Jooyung Han344d5432019-08-23 11:17:39 +090071type testCustomizer func(fs map[string][]byte, config android.Config)
72
73func withFiles(files map[string][]byte) testCustomizer {
74 return func(fs map[string][]byte, config android.Config) {
75 for k, v := range files {
76 fs[k] = v
77 }
78 }
79}
80
81func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
82 return func(fs map[string][]byte, config android.Config) {
83 for k, v := range targets {
84 config.Targets[k] = v
85 }
86 }
87}
88
Jooyung Han31c470b2019-10-18 16:26:59 +090089func withBinder32bit(fs map[string][]byte, config android.Config) {
90 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
91}
92
Jooyung Han344d5432019-08-23 11:17:39 +090093func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -070094 config := android.TestArchConfig(buildDir, nil)
95 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
96 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
97 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
98 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
99 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
Jooyung Han344d5432019-08-23 11:17:39 +0900100 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900101
102 ctx := android.NewTestArchContext()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900103 ctx.RegisterModuleType("apex", android.ModuleFactoryAdaptor(BundleFactory))
Alex Light0851b882019-02-07 13:20:53 -0800104 ctx.RegisterModuleType("apex_test", android.ModuleFactoryAdaptor(testApexBundleFactory))
Jooyung Han344d5432019-08-23 11:17:39 +0900105 ctx.RegisterModuleType("apex_vndk", android.ModuleFactoryAdaptor(vndkApexBundleFactory))
Jiyong Parkd1063c12019-07-17 20:08:41 +0900106 ctx.RegisterModuleType("apex_key", android.ModuleFactoryAdaptor(ApexKeyFactory))
Jiyong Park30ca9372019-02-07 16:27:23 +0900107 ctx.RegisterModuleType("apex_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700108 ctx.RegisterModuleType("prebuilt_apex", android.ModuleFactoryAdaptor(PrebuiltFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +0900109
110 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
111 ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(cc.LibrarySharedFactory))
Jiyong Park7e636d02019-01-28 16:16:54 +0900112 ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(cc.LibraryHeaderFactory))
Jooyung Han344d5432019-08-23 11:17:39 +0900113 ctx.RegisterModuleType("cc_prebuilt_library_shared", android.ModuleFactoryAdaptor(cc.PrebuiltSharedLibraryFactory))
114 ctx.RegisterModuleType("cc_prebuilt_library_static", android.ModuleFactoryAdaptor(cc.PrebuiltStaticLibraryFactory))
Jiyong Park16e91a02018-12-20 18:18:08 +0900115 ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(cc.BinaryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +0900116 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
Roland Levillain630846d2019-06-26 12:48:34 +0100117 ctx.RegisterModuleType("cc_test", android.ModuleFactoryAdaptor(cc.TestFactory))
Jiyong Parkda6eb592018-12-19 17:12:36 +0900118 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
Jooyung Han344d5432019-08-23 11:17:39 +0900119 ctx.RegisterModuleType("vndk_prebuilt_shared", android.ModuleFactoryAdaptor(cc.VndkPrebuiltSharedFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +0900120 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
Jiyong Park7c2ee712018-12-07 00:42:25 +0900121 ctx.RegisterModuleType("prebuilt_etc", android.ModuleFactoryAdaptor(android.PrebuiltEtcFactory))
Jiyong Park04480cf2019-02-06 00:16:29 +0900122 ctx.RegisterModuleType("sh_binary", android.ModuleFactoryAdaptor(android.ShBinaryFactory))
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900123 ctx.RegisterModuleType("android_app_certificate", android.ModuleFactoryAdaptor(java.AndroidAppCertificateFactory))
Jiyong Park809bb722019-02-13 21:33:49 +0900124 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Jiyong Park7f7766d2019-07-25 22:02:35 +0900125 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(java.LibraryFactory))
Jiyong Park9e6c2422019-08-09 20:39:45 +0900126 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(java.ImportFactory))
Dario Frenicde2a032019-10-27 00:29:22 +0100127 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(java.SystemModulesFactory))
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900128 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(java.AndroidAppFactory))
Dario Frenicde2a032019-10-27 00:29:22 +0100129 ctx.RegisterModuleType("android_app_import", android.ModuleFactoryAdaptor(java.AndroidAppImportFactory))
Jiyong Park7f7766d2019-07-25 22:02:35 +0900130
Jooyung Han344d5432019-08-23 11:17:39 +0900131 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700132 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
133 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
134 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900135 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900136 ctx.BottomUp("image", cc.ImageMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900137 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900138 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Roland Levillain9b5fde92019-06-28 15:41:19 +0100139 ctx.BottomUp("test_per_src", cc.TestPerSrcMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900140 ctx.BottomUp("version", cc.VersionMutator).Parallel()
141 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
Jooyung Han344d5432019-08-23 11:17:39 +0900142 })
Jooyung Han31c470b2019-10-18 16:26:59 +0900143 ctx.PreDepsMutators(RegisterPreDepsMutators)
144 ctx.PostDepsMutators(RegisterPostDepsMutators)
Jooyung Han344d5432019-08-23 11:17:39 +0900145 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jooyung Han344d5432019-08-23 11:17:39 +0900146 ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel()
147 ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900148 })
149
150 ctx.Register()
151
152 bp = bp + `
153 toolchain_library {
154 name: "libcompiler_rt-extras",
155 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900156 vendor_available: true,
157 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900158 }
159
160 toolchain_library {
161 name: "libatomic",
162 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900163 vendor_available: true,
164 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900165 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900166 }
167
168 toolchain_library {
169 name: "libgcc",
170 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900171 vendor_available: true,
172 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900173 }
174
175 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700176 name: "libgcc_stripped",
177 src: "",
178 vendor_available: true,
179 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900180 native_bridge_supported: true,
Yi Kongacee27c2019-03-29 20:05:14 -0700181 }
182
183 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900184 name: "libclang_rt.builtins-aarch64-android",
185 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900186 vendor_available: true,
187 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900188 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900189 }
190
191 toolchain_library {
192 name: "libclang_rt.builtins-arm-android",
193 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900194 vendor_available: true,
195 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900196 native_bridge_supported: true,
197 }
198
199 toolchain_library {
200 name: "libclang_rt.builtins-x86_64-android",
201 src: "",
202 vendor_available: true,
203 recovery_available: true,
204 native_bridge_supported: true,
205 }
206
207 toolchain_library {
208 name: "libclang_rt.builtins-i686-android",
209 src: "",
210 vendor_available: true,
211 recovery_available: true,
212 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900213 }
214
215 cc_object {
216 name: "crtbegin_so",
217 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900218 vendor_available: true,
219 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900220 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900221 }
222
223 cc_object {
224 name: "crtend_so",
225 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900226 vendor_available: true,
227 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900228 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900229 }
230
Alex Light3d673592019-01-18 14:37:31 -0800231 cc_object {
232 name: "crtbegin_static",
233 stl: "none",
234 }
235
236 cc_object {
237 name: "crtend_android",
238 stl: "none",
239 }
240
Jiyong Parkda6eb592018-12-19 17:12:36 +0900241 llndk_library {
242 name: "libc",
243 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900244 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900245 }
246
247 llndk_library {
248 name: "libm",
249 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900250 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900251 }
252
253 llndk_library {
254 name: "libdl",
255 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900256 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900257 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900258 `
Dario Frenicde2a032019-10-27 00:29:22 +0100259 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900260
Jooyung Han344d5432019-08-23 11:17:39 +0900261 fs := map[string][]byte{
Dario Frenicde2a032019-10-27 00:29:22 +0100262 "Android.bp": []byte(bp),
263 "a.java": nil,
264 "PrebuiltAppFoo.apk": nil,
265 "PrebuiltAppFooPriv.apk": nil,
266 "build/make/target/product/security": nil,
267 "apex_manifest.json": nil,
268 "AndroidManifest.xml": nil,
269 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900270 "system/sepolicy/apex/myapex_keytest-file_contexts": nil,
271 "system/sepolicy/apex/otherapex-file_contexts": nil,
Jooyung Han5c998b92019-06-27 11:30:33 +0900272 "system/sepolicy/apex/commonapex-file_contexts": nil,
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900273 "mylib.cpp": nil,
274 "mylib_common.cpp": nil,
275 "mytest.cpp": nil,
276 "mytest1.cpp": nil,
277 "mytest2.cpp": nil,
278 "mytest3.cpp": nil,
279 "myprebuilt": nil,
280 "my_include": nil,
281 "foo/bar/MyClass.java": nil,
282 "prebuilt.jar": nil,
283 "vendor/foo/devkeys/test.x509.pem": nil,
284 "vendor/foo/devkeys/test.pk8": nil,
285 "testkey.x509.pem": nil,
286 "testkey.pk8": nil,
287 "testkey.override.x509.pem": nil,
288 "testkey.override.pk8": nil,
289 "vendor/foo/devkeys/testkey.avbpubkey": nil,
290 "vendor/foo/devkeys/testkey.pem": nil,
291 "NOTICE": nil,
292 "custom_notice": nil,
293 "testkey2.avbpubkey": nil,
294 "testkey2.pem": nil,
295 "myapex-arm64.apex": nil,
296 "myapex-arm.apex": nil,
297 "frameworks/base/api/current.txt": nil,
Dario Frenicde2a032019-10-27 00:29:22 +0100298 "framework/aidl/a.aidl": nil,
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900299 "build/make/core/proguard.flags": nil,
300 "build/make/core/proguard_basic_keeps.flags": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900301 }
302
303 for _, handler := range handlers {
304 handler(fs, config)
305 }
306
307 ctx.MockFileSystem(fs)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900308
Jooyung Han5c998b92019-06-27 11:30:33 +0900309 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900310}
311
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700312func setUp() {
313 var err error
314 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900315 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700316 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900317 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900318}
319
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700320func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900321 os.RemoveAll(buildDir)
322}
323
324// ensure that 'result' contains 'expected'
325func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900326 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900327 if !strings.Contains(result, expected) {
328 t.Errorf("%q is not found in %q", expected, result)
329 }
330}
331
332// ensures that 'result' does not contain 'notExpected'
333func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900334 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900335 if strings.Contains(result, notExpected) {
336 t.Errorf("%q is found in %q", notExpected, result)
337 }
338}
339
340func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900341 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900342 if !android.InList(expected, result) {
343 t.Errorf("%q is not found in %v", expected, result)
344 }
345}
346
347func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900348 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900349 if android.InList(notExpected, result) {
350 t.Errorf("%q is found in %v", notExpected, result)
351 }
352}
353
Jooyung Hane1633032019-08-01 17:41:43 +0900354func ensureListEmpty(t *testing.T, result []string) {
355 t.Helper()
356 if len(result) > 0 {
357 t.Errorf("%q is expected to be empty", result)
358 }
359}
360
Jiyong Park25fc6a92018-11-18 18:02:45 +0900361// Minimal test
362func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700363 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900364 apex_defaults {
365 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900366 manifest: ":myapex.manifest",
367 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900368 key: "myapex.key",
369 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800370 multilib: {
371 both: {
372 binaries: ["foo",],
373 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900374 },
Jiyong Park9e6c2422019-08-09 20:39:45 +0900375 java_libs: ["myjar", "myprebuiltjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900376 }
377
Jiyong Park30ca9372019-02-07 16:27:23 +0900378 apex {
379 name: "myapex",
380 defaults: ["myapex-defaults"],
381 }
382
Jiyong Park25fc6a92018-11-18 18:02:45 +0900383 apex_key {
384 name: "myapex.key",
385 public_key: "testkey.avbpubkey",
386 private_key: "testkey.pem",
387 }
388
Jiyong Park809bb722019-02-13 21:33:49 +0900389 filegroup {
390 name: "myapex.manifest",
391 srcs: ["apex_manifest.json"],
392 }
393
394 filegroup {
395 name: "myapex.androidmanifest",
396 srcs: ["AndroidManifest.xml"],
397 }
398
Jiyong Park25fc6a92018-11-18 18:02:45 +0900399 cc_library {
400 name: "mylib",
401 srcs: ["mylib.cpp"],
402 shared_libs: ["mylib2"],
403 system_shared_libs: [],
404 stl: "none",
405 }
406
Alex Light3d673592019-01-18 14:37:31 -0800407 cc_binary {
408 name: "foo",
409 srcs: ["mylib.cpp"],
410 compile_multilib: "both",
411 multilib: {
412 lib32: {
413 suffix: "32",
414 },
415 lib64: {
416 suffix: "64",
417 },
418 },
419 symlinks: ["foo_link_"],
420 symlink_preferred_arch: true,
421 system_shared_libs: [],
422 static_executable: true,
423 stl: "none",
424 }
425
Jiyong Park25fc6a92018-11-18 18:02:45 +0900426 cc_library {
427 name: "mylib2",
428 srcs: ["mylib.cpp"],
429 system_shared_libs: [],
430 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900431 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900432 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900433
434 java_library {
435 name: "myjar",
436 srcs: ["foo/bar/MyClass.java"],
437 sdk_version: "none",
438 system_modules: "none",
439 compile_dex: true,
440 static_libs: ["myotherjar"],
441 }
442
443 java_library {
444 name: "myotherjar",
445 srcs: ["foo/bar/MyClass.java"],
446 sdk_version: "none",
447 system_modules: "none",
448 compile_dex: true,
449 }
Jiyong Park9e6c2422019-08-09 20:39:45 +0900450
451 java_import {
452 name: "myprebuiltjar",
453 jars: ["prebuilt.jar"],
454 installable: true,
455 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900456 `)
457
458 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900459
460 optFlags := apexRule.Args["opt_flags"]
461 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700462 // Ensure that the NOTICE output is being packaged as an asset.
463 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900464
Jiyong Park25fc6a92018-11-18 18:02:45 +0900465 copyCmds := apexRule.Args["copy_commands"]
466
467 // Ensure that main rule creates an output
468 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
469
470 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900471 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900472 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900473 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900474
475 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900476 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900477 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900478
479 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800480 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
481 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900482 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900483 ensureContains(t, copyCmds, "image.apex/javalib/myprebuiltjar.jar")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900484 // .. but not for java libs
485 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800486
Jiyong Park7f7766d2019-07-25 22:02:35 +0900487 // Ensure that the platform variant ends with _core_shared or _common
Logan Chien3aeedc92018-12-26 15:32:21 +0800488 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
489 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900490 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
491 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900492 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common")
Alex Light3d673592019-01-18 14:37:31 -0800493
494 // Ensure that all symlinks are present.
495 found_foo_link_64 := false
496 found_foo := false
497 for _, cmd := range strings.Split(copyCmds, " && ") {
498 if strings.HasPrefix(cmd, "ln -s foo64") {
499 if strings.HasSuffix(cmd, "bin/foo") {
500 found_foo = true
501 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
502 found_foo_link_64 = true
503 }
504 }
505 }
506 good := found_foo && found_foo_link_64
507 if !good {
508 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
509 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900510
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700511 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("mergeNoticesRule")
512 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700513 if len(noticeInputs) != 2 {
514 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900515 }
516 ensureListContains(t, noticeInputs, "NOTICE")
517 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800518}
519
520func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700521 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800522 apex {
523 name: "myapex",
524 key: "myapex.key",
525 payload_type: "zip",
526 native_shared_libs: ["mylib"],
527 }
528
529 apex_key {
530 name: "myapex.key",
531 public_key: "testkey.avbpubkey",
532 private_key: "testkey.pem",
533 }
534
535 cc_library {
536 name: "mylib",
537 srcs: ["mylib.cpp"],
538 shared_libs: ["mylib2"],
539 system_shared_libs: [],
540 stl: "none",
541 }
542
543 cc_library {
544 name: "mylib2",
545 srcs: ["mylib.cpp"],
546 system_shared_libs: [],
547 stl: "none",
548 }
549 `)
550
551 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("zipApexRule")
552 copyCmds := zipApexRule.Args["copy_commands"]
553
554 // Ensure that main rule creates an output
555 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
556
557 // Ensure that APEX variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900558 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800559
560 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900561 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800562
563 // Ensure that both direct and indirect deps are copied into apex
564 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
565 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900566}
567
568func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700569 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900570 apex {
571 name: "myapex",
572 key: "myapex.key",
573 native_shared_libs: ["mylib", "mylib3"],
574 }
575
576 apex_key {
577 name: "myapex.key",
578 public_key: "testkey.avbpubkey",
579 private_key: "testkey.pem",
580 }
581
582 cc_library {
583 name: "mylib",
584 srcs: ["mylib.cpp"],
585 shared_libs: ["mylib2", "mylib3"],
586 system_shared_libs: [],
587 stl: "none",
588 }
589
590 cc_library {
591 name: "mylib2",
592 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900593 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900594 system_shared_libs: [],
595 stl: "none",
596 stubs: {
597 versions: ["1", "2", "3"],
598 },
599 }
600
601 cc_library {
602 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900603 srcs: ["mylib.cpp"],
604 shared_libs: ["mylib4"],
605 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900606 stl: "none",
607 stubs: {
608 versions: ["10", "11", "12"],
609 },
610 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900611
612 cc_library {
613 name: "mylib4",
614 srcs: ["mylib.cpp"],
615 system_shared_libs: [],
616 stl: "none",
617 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900618 `)
619
620 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
621 copyCmds := apexRule.Args["copy_commands"]
622
623 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800624 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900625
626 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800627 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900628
629 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800630 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900631
Jiyong Parkda6eb592018-12-19 17:12:36 +0900632 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900633
634 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900635 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900636 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900637 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900638
639 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Jiyong Parkda6eb592018-12-19 17:12:36 +0900640 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900641 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900642 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900643
644 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900645 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900646 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900647
648 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900649 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_3_myapex").Rule("genStubSrc").Args["flags"])
Jiyong Park25fc6a92018-11-18 18:02:45 +0900650}
651
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900652func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700653 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900654 apex {
655 name: "myapex",
656 key: "myapex.key",
657 native_shared_libs: ["mylib"],
658 }
659
660 apex_key {
661 name: "myapex.key",
662 public_key: "testkey.avbpubkey",
663 private_key: "testkey.pem",
664 }
665
666 cc_library {
667 name: "mylib",
668 srcs: ["mylib.cpp"],
669 shared_libs: ["libfoo#10"],
670 system_shared_libs: [],
671 stl: "none",
672 }
673
674 cc_library {
675 name: "libfoo",
676 srcs: ["mylib.cpp"],
677 shared_libs: ["libbar"],
678 system_shared_libs: [],
679 stl: "none",
680 stubs: {
681 versions: ["10", "20", "30"],
682 },
683 }
684
685 cc_library {
686 name: "libbar",
687 srcs: ["mylib.cpp"],
688 system_shared_libs: [],
689 stl: "none",
690 }
691
692 `)
693
694 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
695 copyCmds := apexRule.Args["copy_commands"]
696
697 // Ensure that direct non-stubs dep is always included
698 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
699
700 // Ensure that indirect stubs dep is not included
701 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
702
703 // Ensure that dependency of stubs is not included
704 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
705
Jiyong Parkda6eb592018-12-19 17:12:36 +0900706 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900707
708 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900709 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900710 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900711 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900712
Jiyong Parkda6eb592018-12-19 17:12:36 +0900713 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900714
715 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
716 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
717}
718
Jooyung Hand3639552019-08-09 12:57:43 +0900719func TestApexWithRuntimeLibsDependency(t *testing.T) {
720 /*
721 myapex
722 |
723 v (runtime_libs)
724 mylib ------+------> libfoo [provides stub]
725 |
726 `------> libbar
727 */
728 ctx, _ := testApex(t, `
729 apex {
730 name: "myapex",
731 key: "myapex.key",
732 native_shared_libs: ["mylib"],
733 }
734
735 apex_key {
736 name: "myapex.key",
737 public_key: "testkey.avbpubkey",
738 private_key: "testkey.pem",
739 }
740
741 cc_library {
742 name: "mylib",
743 srcs: ["mylib.cpp"],
744 runtime_libs: ["libfoo", "libbar"],
745 system_shared_libs: [],
746 stl: "none",
747 }
748
749 cc_library {
750 name: "libfoo",
751 srcs: ["mylib.cpp"],
752 system_shared_libs: [],
753 stl: "none",
754 stubs: {
755 versions: ["10", "20", "30"],
756 },
757 }
758
759 cc_library {
760 name: "libbar",
761 srcs: ["mylib.cpp"],
762 system_shared_libs: [],
763 stl: "none",
764 }
765
766 `)
767
768 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
769 copyCmds := apexRule.Args["copy_commands"]
770
771 // Ensure that direct non-stubs dep is always included
772 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
773
774 // Ensure that indirect stubs dep is not included
775 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
776
777 // Ensure that runtime_libs dep in included
778 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
779
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900780 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
781 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
782 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900783
784}
785
Jooyung Han9c80bae2019-08-20 17:30:57 +0900786func TestApexDependencyToLLNDK(t *testing.T) {
787 ctx, _ := testApex(t, `
788 apex {
789 name: "myapex",
790 key: "myapex.key",
791 use_vendor: true,
792 native_shared_libs: ["mylib"],
793 }
794
795 apex_key {
796 name: "myapex.key",
797 public_key: "testkey.avbpubkey",
798 private_key: "testkey.pem",
799 }
800
801 cc_library {
802 name: "mylib",
803 srcs: ["mylib.cpp"],
804 vendor_available: true,
805 shared_libs: ["libbar"],
806 system_shared_libs: [],
807 stl: "none",
808 }
809
810 cc_library {
811 name: "libbar",
812 srcs: ["mylib.cpp"],
813 system_shared_libs: [],
814 stl: "none",
815 }
816
817 llndk_library {
818 name: "libbar",
819 symbol_file: "",
820 }
821
822 `)
823
824 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
825 copyCmds := apexRule.Args["copy_commands"]
826
827 // Ensure that LLNDK dep is not included
828 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
829
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900830 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
831 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900832
833 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900834 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900835
836}
837
Jiyong Park25fc6a92018-11-18 18:02:45 +0900838func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700839 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900840 apex {
841 name: "myapex",
842 key: "myapex.key",
843 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
844 }
845
846 apex_key {
847 name: "myapex.key",
848 public_key: "testkey.avbpubkey",
849 private_key: "testkey.pem",
850 }
851
852 cc_library {
853 name: "mylib",
854 srcs: ["mylib.cpp"],
855 shared_libs: ["libdl#27"],
856 stl: "none",
857 }
858
859 cc_library_shared {
860 name: "mylib_shared",
861 srcs: ["mylib.cpp"],
862 shared_libs: ["libdl#27"],
863 stl: "none",
864 }
865
866 cc_library {
867 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700868 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900869 nocrt: true,
870 system_shared_libs: [],
871 stl: "none",
872 stubs: {
873 versions: ["27", "28", "29"],
874 },
875 }
876
877 cc_library {
878 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700879 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900880 nocrt: true,
881 system_shared_libs: [],
882 stl: "none",
883 stubs: {
884 versions: ["27", "28", "29"],
885 },
886 }
887
888 cc_library {
889 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700890 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900891 nocrt: true,
892 system_shared_libs: [],
893 stl: "none",
894 stubs: {
895 versions: ["27", "28", "29"],
896 },
897 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900898
899 cc_library {
900 name: "libBootstrap",
901 srcs: ["mylib.cpp"],
902 stl: "none",
903 bootstrap: true,
904 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900905 `)
906
907 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
908 copyCmds := apexRule.Args["copy_commands"]
909
910 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800911 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900912 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
913 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900914
915 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900916 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900917
Jiyong Parkda6eb592018-12-19 17:12:36 +0900918 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
919 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
920 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900921
922 // For dependency to libc
923 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900924 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900925 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900926 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900927 // ... Cflags from stub is correctly exported to mylib
928 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
929 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
930
931 // For dependency to libm
932 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900933 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900934 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900935 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900936 // ... and is not compiling with the stub
937 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
938 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
939
940 // For dependency to libdl
941 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900942 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900943 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900944 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
945 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900946 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900947 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900948 // ... Cflags from stub is correctly exported to mylib
949 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
950 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900951
952 // Ensure that libBootstrap is depending on the platform variant of bionic libs
953 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
954 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
955 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
956 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900957}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900958
959func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700960 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +0900961 apex {
962 name: "myapex",
963 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900964 native_shared_libs: ["mylib"],
965 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900966 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900967 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900968 }
969
970 apex_key {
971 name: "myapex.key",
972 public_key: "testkey.avbpubkey",
973 private_key: "testkey.pem",
974 }
975
976 prebuilt_etc {
977 name: "myetc",
978 src: "myprebuilt",
979 sub_dir: "foo/bar",
980 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900981
982 cc_library {
983 name: "mylib",
984 srcs: ["mylib.cpp"],
985 relative_install_path: "foo/bar",
986 system_shared_libs: [],
987 stl: "none",
988 }
989
990 cc_binary {
991 name: "mybin",
992 srcs: ["mylib.cpp"],
993 relative_install_path: "foo/bar",
994 system_shared_libs: [],
995 static_executable: true,
996 stl: "none",
997 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900998 `)
999
1000 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
1001 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1002
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001003 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001004 ensureListContains(t, dirs, "etc")
1005 ensureListContains(t, dirs, "etc/foo")
1006 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001007 ensureListContains(t, dirs, "lib64")
1008 ensureListContains(t, dirs, "lib64/foo")
1009 ensureListContains(t, dirs, "lib64/foo/bar")
1010 ensureListContains(t, dirs, "lib")
1011 ensureListContains(t, dirs, "lib/foo")
1012 ensureListContains(t, dirs, "lib/foo/bar")
1013
Jiyong Parkbd13e442019-03-15 18:10:35 +09001014 ensureListContains(t, dirs, "bin")
1015 ensureListContains(t, dirs, "bin/foo")
1016 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001017}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001018
1019func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001020 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001021 apex {
1022 name: "myapex",
1023 key: "myapex.key",
1024 native_shared_libs: ["mylib"],
1025 use_vendor: true,
1026 }
1027
1028 apex_key {
1029 name: "myapex.key",
1030 public_key: "testkey.avbpubkey",
1031 private_key: "testkey.pem",
1032 }
1033
1034 cc_library {
1035 name: "mylib",
1036 srcs: ["mylib.cpp"],
1037 shared_libs: ["mylib2"],
1038 system_shared_libs: [],
1039 vendor_available: true,
1040 stl: "none",
1041 }
1042
1043 cc_library {
1044 name: "mylib2",
1045 srcs: ["mylib.cpp"],
1046 system_shared_libs: [],
1047 vendor_available: true,
1048 stl: "none",
1049 }
1050 `)
1051
1052 inputsList := []string{}
1053 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
1054 for _, implicit := range i.Implicits {
1055 inputsList = append(inputsList, implicit.String())
1056 }
1057 }
1058 inputsString := strings.Join(inputsList, " ")
1059
1060 // ensure that the apex includes vendor variants of the direct and indirect deps
Inseob Kim64c43952019-08-26 16:52:35 +09001061 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor.VER_shared_myapex/mylib.so")
1062 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor.VER_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001063
1064 // ensure that the apex does not include core variants
1065 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
1066 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
1067}
Jiyong Park16e91a02018-12-20 18:18:08 +09001068
Jooyung Han5c998b92019-06-27 11:30:33 +09001069func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1070 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1071 apex {
1072 name: "myapex",
1073 key: "myapex.key",
1074 native_shared_libs: ["mylib"],
1075 use_vendor: true,
1076 }
1077
1078 apex_key {
1079 name: "myapex.key",
1080 public_key: "testkey.avbpubkey",
1081 private_key: "testkey.pem",
1082 }
1083
1084 cc_library {
1085 name: "mylib",
1086 srcs: ["mylib.cpp"],
1087 system_shared_libs: [],
1088 stl: "none",
1089 }
1090 `)
1091}
1092
Jiyong Park16e91a02018-12-20 18:18:08 +09001093func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001094 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001095 apex {
1096 name: "myapex",
1097 key: "myapex.key",
1098 native_shared_libs: ["mylib"],
1099 }
1100
1101 apex_key {
1102 name: "myapex.key",
1103 public_key: "testkey.avbpubkey",
1104 private_key: "testkey.pem",
1105 }
1106
1107 cc_library {
1108 name: "mylib",
1109 srcs: ["mylib.cpp"],
1110 system_shared_libs: [],
1111 stl: "none",
1112 stubs: {
1113 versions: ["1", "2", "3"],
1114 },
1115 }
1116
1117 cc_binary {
1118 name: "not_in_apex",
1119 srcs: ["mylib.cpp"],
1120 static_libs: ["mylib"],
1121 static_executable: true,
1122 system_shared_libs: [],
1123 stl: "none",
1124 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001125 `)
1126
1127 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
1128
1129 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +08001130 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001131}
Jiyong Park9335a262018-12-24 11:31:58 +09001132
1133func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001134 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001135 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001136 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001137 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001138 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001139 native_shared_libs: ["mylib"],
1140 }
1141
1142 cc_library {
1143 name: "mylib",
1144 srcs: ["mylib.cpp"],
1145 system_shared_libs: [],
1146 stl: "none",
1147 }
1148
1149 apex_key {
1150 name: "myapex.key",
1151 public_key: "testkey.avbpubkey",
1152 private_key: "testkey.pem",
1153 }
1154
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001155 android_app_certificate {
1156 name: "myapex.certificate",
1157 certificate: "testkey",
1158 }
1159
1160 android_app_certificate {
1161 name: "myapex.certificate.override",
1162 certificate: "testkey.override",
1163 }
1164
Jiyong Park9335a262018-12-24 11:31:58 +09001165 `)
1166
1167 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001168 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001169
1170 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1171 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1172 "vendor/foo/devkeys/testkey.avbpubkey")
1173 }
1174 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1175 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1176 "vendor/foo/devkeys/testkey.pem")
1177 }
1178
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001179 // check the APK certs. It should be overridden to myapex.certificate.override
1180 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
1181 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001182 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001183 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001184 }
1185}
Jiyong Park58e364a2019-01-19 19:24:06 +09001186
1187func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001188 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001189 apex {
1190 name: "myapex",
1191 key: "myapex.key",
1192 native_shared_libs: ["mylib"],
1193 }
1194
1195 apex {
1196 name: "otherapex",
1197 key: "myapex.key",
1198 native_shared_libs: ["mylib"],
1199 }
1200
1201 apex_key {
1202 name: "myapex.key",
1203 public_key: "testkey.avbpubkey",
1204 private_key: "testkey.pem",
1205 }
1206
1207 cc_library {
1208 name: "mylib",
1209 srcs: ["mylib.cpp"],
1210 system_shared_libs: [],
1211 stl: "none",
1212 }
1213 `)
1214
1215 // non-APEX variant does not have __ANDROID__APEX__ defined
1216 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
1217 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
1218 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
Jooyung Han77988572019-10-18 16:26:16 +09001219 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1220 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001221
1222 // APEX variant has __ANDROID_APEX__=<apexname> defined
1223 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
1224 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
1225 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
Jooyung Han77988572019-10-18 16:26:16 +09001226 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1227 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001228
1229 // APEX variant has __ANDROID_APEX__=<apexname> defined
1230 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
1231 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
1232 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
Jooyung Han77988572019-10-18 16:26:16 +09001233 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1234 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001235}
Jiyong Park7e636d02019-01-28 16:16:54 +09001236
1237func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001238 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001239 apex {
1240 name: "myapex",
1241 key: "myapex.key",
1242 native_shared_libs: ["mylib"],
1243 }
1244
1245 apex_key {
1246 name: "myapex.key",
1247 public_key: "testkey.avbpubkey",
1248 private_key: "testkey.pem",
1249 }
1250
1251 cc_library_headers {
1252 name: "mylib_headers",
1253 export_include_dirs: ["my_include"],
1254 system_shared_libs: [],
1255 stl: "none",
1256 }
1257
1258 cc_library {
1259 name: "mylib",
1260 srcs: ["mylib.cpp"],
1261 system_shared_libs: [],
1262 stl: "none",
1263 header_libs: ["mylib_headers"],
1264 export_header_lib_headers: ["mylib_headers"],
1265 stubs: {
1266 versions: ["1", "2", "3"],
1267 },
1268 }
1269
1270 cc_library {
1271 name: "otherlib",
1272 srcs: ["mylib.cpp"],
1273 system_shared_libs: [],
1274 stl: "none",
1275 shared_libs: ["mylib"],
1276 }
1277 `)
1278
1279 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
1280
1281 // Ensure that the include path of the header lib is exported to 'otherlib'
1282 ensureContains(t, cFlags, "-Imy_include")
1283}
Alex Light9670d332019-01-29 18:07:33 -08001284
Jooyung Han31c470b2019-10-18 16:26:59 +09001285func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1286 t.Helper()
1287 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName).Rule("apexRule")
1288 copyCmds := apexRule.Args["copy_commands"]
1289 imageApexDir := "/image.apex/"
1290 dstFiles := []string{}
1291 for _, cmd := range strings.Split(copyCmds, "&&") {
1292 cmd = strings.TrimSpace(cmd)
1293 if cmd == "" {
1294 continue
1295 }
1296 terms := strings.Split(cmd, " ")
1297 switch terms[0] {
1298 case "mkdir":
1299 case "cp":
1300 if len(terms) != 3 {
1301 t.Fatal("copyCmds contains invalid cp command", cmd)
1302 }
1303 dst := terms[2]
1304 index := strings.Index(dst, imageApexDir)
1305 if index == -1 {
1306 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1307 }
1308 dstFile := dst[index+len(imageApexDir):]
1309 dstFiles = append(dstFiles, dstFile)
1310 default:
1311 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1312 }
1313 }
1314 sort.Strings(dstFiles)
1315 sort.Strings(files)
1316 missing := []string{}
1317 surplus := []string{}
1318 i := 0
1319 j := 0
1320 for i < len(dstFiles) && j < len(files) {
1321 if dstFiles[i] == files[j] {
1322 i++
1323 j++
1324 } else if dstFiles[i] < files[j] {
1325 surplus = append(surplus, dstFiles[i])
1326 i++
1327 } else {
1328 missing = append(missing, files[j])
1329 j++
1330 }
1331 }
1332 if i < len(dstFiles) {
1333 surplus = append(surplus, dstFiles[i:]...)
1334 }
1335 if j < len(files) {
1336 missing = append(missing, files[j:]...)
1337 }
1338
1339 failed := false
1340 if len(surplus) > 0 {
1341 t.Log("surplus files", surplus)
1342 failed = true
1343 }
1344 if len(missing) > 0 {
1345 t.Log("missing files", missing)
1346 failed = true
1347 }
1348 if failed {
1349 t.Fail()
1350 }
1351}
1352
Jooyung Han344d5432019-08-23 11:17:39 +09001353func TestVndkApexCurrent(t *testing.T) {
1354 ctx, _ := testApex(t, `
1355 apex_vndk {
1356 name: "myapex",
1357 key: "myapex.key",
1358 file_contexts: "myapex",
1359 }
1360
1361 apex_key {
1362 name: "myapex.key",
1363 public_key: "testkey.avbpubkey",
1364 private_key: "testkey.pem",
1365 }
1366
1367 cc_library {
1368 name: "libvndk",
1369 srcs: ["mylib.cpp"],
1370 vendor_available: true,
1371 vndk: {
1372 enabled: true,
1373 },
1374 system_shared_libs: [],
1375 stl: "none",
1376 }
1377
1378 cc_library {
1379 name: "libvndksp",
1380 srcs: ["mylib.cpp"],
1381 vendor_available: true,
1382 vndk: {
1383 enabled: true,
1384 support_system_process: true,
1385 },
1386 system_shared_libs: [],
1387 stl: "none",
1388 }
1389 `)
1390
Jooyung Han31c470b2019-10-18 16:26:59 +09001391 ensureExactContents(t, ctx, "myapex", []string{
1392 "lib/libvndk.so",
1393 "lib/libvndksp.so",
1394 "lib64/libvndk.so",
1395 "lib64/libvndksp.so",
1396 })
Jooyung Han344d5432019-08-23 11:17:39 +09001397}
1398
1399func TestVndkApexWithPrebuilt(t *testing.T) {
1400 ctx, _ := testApex(t, `
1401 apex_vndk {
1402 name: "myapex",
1403 key: "myapex.key",
1404 file_contexts: "myapex",
1405 }
1406
1407 apex_key {
1408 name: "myapex.key",
1409 public_key: "testkey.avbpubkey",
1410 private_key: "testkey.pem",
1411 }
1412
1413 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001414 name: "libvndk",
1415 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001416 vendor_available: true,
1417 vndk: {
1418 enabled: true,
1419 },
1420 system_shared_libs: [],
1421 stl: "none",
1422 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001423
1424 cc_prebuilt_library_shared {
1425 name: "libvndk.arm",
1426 srcs: ["libvndk.arm.so"],
1427 vendor_available: true,
1428 vndk: {
1429 enabled: true,
1430 },
1431 enabled: false,
1432 arch: {
1433 arm: {
1434 enabled: true,
1435 },
1436 },
1437 system_shared_libs: [],
1438 stl: "none",
1439 }
Jooyung Han344d5432019-08-23 11:17:39 +09001440 `, withFiles(map[string][]byte{
Jooyung Han31c470b2019-10-18 16:26:59 +09001441 "libvndk.so": nil,
1442 "libvndk.arm.so": nil,
Jooyung Han344d5432019-08-23 11:17:39 +09001443 }))
1444
Jooyung Han31c470b2019-10-18 16:26:59 +09001445 ensureExactContents(t, ctx, "myapex", []string{
1446 "lib/libvndk.so",
1447 "lib/libvndk.arm.so",
1448 "lib64/libvndk.so",
1449 })
Jooyung Han344d5432019-08-23 11:17:39 +09001450}
1451
1452func TestVndkApexVersion(t *testing.T) {
1453 ctx, _ := testApex(t, `
1454 apex_vndk {
1455 name: "myapex_v27",
1456 key: "myapex.key",
1457 file_contexts: "myapex",
1458 vndk_version: "27",
1459 }
1460
1461 apex_key {
1462 name: "myapex.key",
1463 public_key: "testkey.avbpubkey",
1464 private_key: "testkey.pem",
1465 }
1466
Jooyung Han31c470b2019-10-18 16:26:59 +09001467 vndk_prebuilt_shared {
1468 name: "libvndk27",
1469 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001470 vendor_available: true,
1471 vndk: {
1472 enabled: true,
1473 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001474 target_arch: "arm64",
1475 arch: {
1476 arm: {
1477 srcs: ["libvndk27_arm.so"],
1478 },
1479 arm64: {
1480 srcs: ["libvndk27_arm64.so"],
1481 },
1482 },
Jooyung Han344d5432019-08-23 11:17:39 +09001483 }
1484
1485 vndk_prebuilt_shared {
1486 name: "libvndk27",
1487 version: "27",
1488 vendor_available: true,
1489 vndk: {
1490 enabled: true,
1491 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001492 target_arch: "x86_64",
1493 arch: {
1494 x86: {
1495 srcs: ["libvndk27_x86.so"],
1496 },
1497 x86_64: {
1498 srcs: ["libvndk27_x86_64.so"],
1499 },
1500 },
1501 }
Jooyung Han344d5432019-08-23 11:17:39 +09001502 `, withFiles(map[string][]byte{
Jooyung Han31c470b2019-10-18 16:26:59 +09001503 "libvndk27_arm.so": nil,
1504 "libvndk27_arm64.so": nil,
1505 "libvndk27_x86.so": nil,
1506 "libvndk27_x86_64.so": nil,
Jooyung Han344d5432019-08-23 11:17:39 +09001507 }))
1508
Jooyung Han31c470b2019-10-18 16:26:59 +09001509 ensureExactContents(t, ctx, "myapex_v27", []string{
1510 "lib/libvndk27_arm.so",
1511 "lib64/libvndk27_arm64.so",
1512 })
Jooyung Han344d5432019-08-23 11:17:39 +09001513}
1514
1515func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1516 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1517 apex_vndk {
1518 name: "myapex_v27",
1519 key: "myapex.key",
1520 file_contexts: "myapex",
1521 vndk_version: "27",
1522 }
1523 apex_vndk {
1524 name: "myapex_v27_other",
1525 key: "myapex.key",
1526 file_contexts: "myapex",
1527 vndk_version: "27",
1528 }
1529
1530 apex_key {
1531 name: "myapex.key",
1532 public_key: "testkey.avbpubkey",
1533 private_key: "testkey.pem",
1534 }
1535
1536 cc_library {
1537 name: "libvndk",
1538 srcs: ["mylib.cpp"],
1539 vendor_available: true,
1540 vndk: {
1541 enabled: true,
1542 },
1543 system_shared_libs: [],
1544 stl: "none",
1545 }
1546
1547 vndk_prebuilt_shared {
1548 name: "libvndk",
1549 version: "27",
1550 vendor_available: true,
1551 vndk: {
1552 enabled: true,
1553 },
1554 srcs: ["libvndk.so"],
1555 }
1556 `, withFiles(map[string][]byte{
1557 "libvndk.so": nil,
1558 }))
1559}
1560
Jooyung Han90eee022019-10-01 20:02:42 +09001561func TestVndkApexNameRule(t *testing.T) {
1562 ctx, _ := testApex(t, `
1563 apex_vndk {
1564 name: "myapex",
1565 key: "myapex.key",
1566 file_contexts: "myapex",
1567 }
1568 apex_vndk {
1569 name: "myapex_v28",
1570 key: "myapex.key",
1571 file_contexts: "myapex",
1572 vndk_version: "28",
1573 }
1574 apex_key {
1575 name: "myapex.key",
1576 public_key: "testkey.avbpubkey",
1577 private_key: "testkey.pem",
1578 }`)
1579
1580 assertApexName := func(expected, moduleName string) {
1581 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName).Module().(*apexBundle)
1582 actual := proptools.String(bundle.properties.Apex_name)
1583 if !reflect.DeepEqual(actual, expected) {
1584 t.Errorf("Got '%v', expected '%v'", actual, expected)
1585 }
1586 }
1587
1588 assertApexName("com.android.vndk.vVER", "myapex")
1589 assertApexName("com.android.vndk.v28", "myapex_v28")
1590}
1591
Jooyung Han344d5432019-08-23 11:17:39 +09001592func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1593 ctx, _ := testApex(t, `
1594 apex_vndk {
1595 name: "myapex",
1596 key: "myapex.key",
1597 file_contexts: "myapex",
1598 }
1599
1600 apex_key {
1601 name: "myapex.key",
1602 public_key: "testkey.avbpubkey",
1603 private_key: "testkey.pem",
1604 }
1605
1606 cc_library {
1607 name: "libvndk",
1608 srcs: ["mylib.cpp"],
1609 vendor_available: true,
1610 native_bridge_supported: true,
1611 host_supported: true,
1612 vndk: {
1613 enabled: true,
1614 },
1615 system_shared_libs: [],
1616 stl: "none",
1617 }
1618 `, withTargets(map[android.OsType][]android.Target{
1619 android.Android: []android.Target{
Colin Cross3b19f5d2019-09-17 14:45:31 -07001620 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1621 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1622 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1623 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
Jooyung Han344d5432019-08-23 11:17:39 +09001624 },
1625 }))
1626
Jooyung Han31c470b2019-10-18 16:26:59 +09001627 ensureExactContents(t, ctx, "myapex", []string{
1628 "lib/libvndk.so",
1629 "lib64/libvndk.so",
1630 })
Jooyung Han344d5432019-08-23 11:17:39 +09001631}
1632
1633func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1634 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1635 apex_vndk {
1636 name: "myapex",
1637 key: "myapex.key",
1638 file_contexts: "myapex",
1639 native_bridge_supported: true,
1640 }
1641
1642 apex_key {
1643 name: "myapex.key",
1644 public_key: "testkey.avbpubkey",
1645 private_key: "testkey.pem",
1646 }
1647
1648 cc_library {
1649 name: "libvndk",
1650 srcs: ["mylib.cpp"],
1651 vendor_available: true,
1652 native_bridge_supported: true,
1653 host_supported: true,
1654 vndk: {
1655 enabled: true,
1656 },
1657 system_shared_libs: [],
1658 stl: "none",
1659 }
1660 `)
1661}
1662
Jooyung Han31c470b2019-10-18 16:26:59 +09001663func TestVndkApexWithBinder32(t *testing.T) {
1664 ctx, _ := testApex(t,
1665 `
1666 apex_vndk {
1667 name: "myapex_v27",
1668 key: "myapex.key",
1669 file_contexts: "myapex",
1670 vndk_version: "27",
1671 }
1672
1673 apex_key {
1674 name: "myapex.key",
1675 public_key: "testkey.avbpubkey",
1676 private_key: "testkey.pem",
1677 }
1678
1679 vndk_prebuilt_shared {
1680 name: "libvndk27",
1681 version: "27",
1682 target_arch: "arm",
1683 vendor_available: true,
1684 vndk: {
1685 enabled: true,
1686 },
1687 arch: {
1688 arm: {
1689 srcs: ["libvndk27.so"],
1690 }
1691 },
1692 }
1693
1694 vndk_prebuilt_shared {
1695 name: "libvndk27",
1696 version: "27",
1697 target_arch: "arm",
1698 binder32bit: true,
1699 vendor_available: true,
1700 vndk: {
1701 enabled: true,
1702 },
1703 arch: {
1704 arm: {
1705 srcs: ["libvndk27binder32.so"],
1706 }
1707 },
1708 }
1709 `,
1710 withFiles(map[string][]byte{
1711 "libvndk27.so": nil,
1712 "libvndk27binder32.so": nil,
1713 }),
1714 withBinder32bit,
1715 withTargets(map[android.OsType][]android.Target{
1716 android.Android: []android.Target{
1717 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1718 },
1719 }),
1720 )
1721
1722 ensureExactContents(t, ctx, "myapex_v27", []string{
1723 "lib/libvndk27binder32.so",
1724 })
1725}
1726
Jooyung Hane1633032019-08-01 17:41:43 +09001727func TestDependenciesInApexManifest(t *testing.T) {
1728 ctx, _ := testApex(t, `
1729 apex {
1730 name: "myapex_nodep",
1731 key: "myapex.key",
1732 native_shared_libs: ["lib_nodep"],
1733 compile_multilib: "both",
1734 file_contexts: "myapex",
1735 }
1736
1737 apex {
1738 name: "myapex_dep",
1739 key: "myapex.key",
1740 native_shared_libs: ["lib_dep"],
1741 compile_multilib: "both",
1742 file_contexts: "myapex",
1743 }
1744
1745 apex {
1746 name: "myapex_provider",
1747 key: "myapex.key",
1748 native_shared_libs: ["libfoo"],
1749 compile_multilib: "both",
1750 file_contexts: "myapex",
1751 }
1752
1753 apex {
1754 name: "myapex_selfcontained",
1755 key: "myapex.key",
1756 native_shared_libs: ["lib_dep", "libfoo"],
1757 compile_multilib: "both",
1758 file_contexts: "myapex",
1759 }
1760
1761 apex_key {
1762 name: "myapex.key",
1763 public_key: "testkey.avbpubkey",
1764 private_key: "testkey.pem",
1765 }
1766
1767 cc_library {
1768 name: "lib_nodep",
1769 srcs: ["mylib.cpp"],
1770 system_shared_libs: [],
1771 stl: "none",
1772 }
1773
1774 cc_library {
1775 name: "lib_dep",
1776 srcs: ["mylib.cpp"],
1777 shared_libs: ["libfoo"],
1778 system_shared_libs: [],
1779 stl: "none",
1780 }
1781
1782 cc_library {
1783 name: "libfoo",
1784 srcs: ["mytest.cpp"],
1785 stubs: {
1786 versions: ["1"],
1787 },
1788 system_shared_libs: [],
1789 stl: "none",
1790 }
1791 `)
1792
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001793 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09001794 var provideNativeLibs, requireNativeLibs []string
1795
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001796 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep").Rule("apexManifestRule")
1797 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1798 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001799 ensureListEmpty(t, provideNativeLibs)
1800 ensureListEmpty(t, requireNativeLibs)
1801
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001802 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep").Rule("apexManifestRule")
1803 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1804 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001805 ensureListEmpty(t, provideNativeLibs)
1806 ensureListContains(t, requireNativeLibs, "libfoo.so")
1807
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001808 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider").Rule("apexManifestRule")
1809 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1810 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001811 ensureListContains(t, provideNativeLibs, "libfoo.so")
1812 ensureListEmpty(t, requireNativeLibs)
1813
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001814 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained").Rule("apexManifestRule")
1815 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1816 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001817 ensureListContains(t, provideNativeLibs, "libfoo.so")
1818 ensureListEmpty(t, requireNativeLibs)
1819}
1820
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001821func TestApexName(t *testing.T) {
1822 ctx, _ := testApex(t, `
1823 apex {
1824 name: "myapex",
1825 key: "myapex.key",
1826 apex_name: "com.android.myapex",
1827 }
1828
1829 apex_key {
1830 name: "myapex.key",
1831 public_key: "testkey.avbpubkey",
1832 private_key: "testkey.pem",
1833 }
1834 `)
1835
1836 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1837 apexManifestRule := module.Rule("apexManifestRule")
1838 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
1839 apexRule := module.Rule("apexRule")
1840 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
1841}
1842
Alex Light0851b882019-02-07 13:20:53 -08001843func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001844 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08001845 apex {
1846 name: "myapex",
1847 key: "myapex.key",
1848 native_shared_libs: ["mylib_common"],
1849 }
1850
1851 apex_key {
1852 name: "myapex.key",
1853 public_key: "testkey.avbpubkey",
1854 private_key: "testkey.pem",
1855 }
1856
1857 cc_library {
1858 name: "mylib_common",
1859 srcs: ["mylib.cpp"],
1860 system_shared_libs: [],
1861 stl: "none",
1862 }
1863 `)
1864
1865 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1866 apexRule := module.Rule("apexRule")
1867 copyCmds := apexRule.Args["copy_commands"]
1868
1869 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
1870 t.Log("Apex was a test apex!")
1871 t.Fail()
1872 }
1873 // Ensure that main rule creates an output
1874 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1875
1876 // Ensure that apex variant is created for the direct dep
1877 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1878
1879 // Ensure that both direct and indirect deps are copied into apex
1880 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1881
1882 // Ensure that the platform variant ends with _core_shared
1883 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1884
1885 if !android.InAnyApex("mylib_common") {
1886 t.Log("Found mylib_common not in any apex!")
1887 t.Fail()
1888 }
1889}
1890
1891func TestTestApex(t *testing.T) {
1892 if android.InAnyApex("mylib_common_test") {
1893 t.Fatal("mylib_common_test must not be used in any other tests since this checks that global state is not updated in an illegal way!")
1894 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001895 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08001896 apex_test {
1897 name: "myapex",
1898 key: "myapex.key",
1899 native_shared_libs: ["mylib_common_test"],
1900 }
1901
1902 apex_key {
1903 name: "myapex.key",
1904 public_key: "testkey.avbpubkey",
1905 private_key: "testkey.pem",
1906 }
1907
1908 cc_library {
1909 name: "mylib_common_test",
1910 srcs: ["mylib.cpp"],
1911 system_shared_libs: [],
1912 stl: "none",
1913 }
1914 `)
1915
1916 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1917 apexRule := module.Rule("apexRule")
1918 copyCmds := apexRule.Args["copy_commands"]
1919
1920 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
1921 t.Log("Apex was not a test apex!")
1922 t.Fail()
1923 }
1924 // Ensure that main rule creates an output
1925 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1926
1927 // Ensure that apex variant is created for the direct dep
1928 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared_myapex")
1929
1930 // Ensure that both direct and indirect deps are copied into apex
1931 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
1932
1933 // Ensure that the platform variant ends with _core_shared
1934 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared")
1935
1936 if android.InAnyApex("mylib_common_test") {
1937 t.Log("Found mylib_common_test in some apex!")
1938 t.Fail()
1939 }
1940}
1941
Alex Light9670d332019-01-29 18:07:33 -08001942func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001943 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08001944 apex {
1945 name: "myapex",
1946 key: "myapex.key",
1947 multilib: {
1948 first: {
1949 native_shared_libs: ["mylib_common"],
1950 }
1951 },
1952 target: {
1953 android: {
1954 multilib: {
1955 first: {
1956 native_shared_libs: ["mylib"],
1957 }
1958 }
1959 },
1960 host: {
1961 multilib: {
1962 first: {
1963 native_shared_libs: ["mylib2"],
1964 }
1965 }
1966 }
1967 }
1968 }
1969
1970 apex_key {
1971 name: "myapex.key",
1972 public_key: "testkey.avbpubkey",
1973 private_key: "testkey.pem",
1974 }
1975
1976 cc_library {
1977 name: "mylib",
1978 srcs: ["mylib.cpp"],
1979 system_shared_libs: [],
1980 stl: "none",
1981 }
1982
1983 cc_library {
1984 name: "mylib_common",
1985 srcs: ["mylib.cpp"],
1986 system_shared_libs: [],
1987 stl: "none",
1988 compile_multilib: "first",
1989 }
1990
1991 cc_library {
1992 name: "mylib2",
1993 srcs: ["mylib.cpp"],
1994 system_shared_libs: [],
1995 stl: "none",
1996 compile_multilib: "first",
1997 }
1998 `)
1999
2000 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
2001 copyCmds := apexRule.Args["copy_commands"]
2002
2003 // Ensure that main rule creates an output
2004 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2005
2006 // Ensure that apex variant is created for the direct dep
2007 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
2008 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
2009 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
2010
2011 // Ensure that both direct and indirect deps are copied into apex
2012 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2013 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2014 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2015
2016 // Ensure that the platform variant ends with _core_shared
2017 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
2018 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
2019 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
2020}
Jiyong Park04480cf2019-02-06 00:16:29 +09002021
2022func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002023 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002024 apex {
2025 name: "myapex",
2026 key: "myapex.key",
2027 binaries: ["myscript"],
2028 }
2029
2030 apex_key {
2031 name: "myapex.key",
2032 public_key: "testkey.avbpubkey",
2033 private_key: "testkey.pem",
2034 }
2035
2036 sh_binary {
2037 name: "myscript",
2038 src: "mylib.cpp",
2039 filename: "myscript.sh",
2040 sub_dir: "script",
2041 }
2042 `)
2043
2044 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
2045 copyCmds := apexRule.Args["copy_commands"]
2046
2047 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2048}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002049
2050func TestApexInProductPartition(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002051 ctx, _ := testApex(t, `
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002052 apex {
2053 name: "myapex",
2054 key: "myapex.key",
2055 native_shared_libs: ["mylib"],
2056 product_specific: true,
2057 }
2058
2059 apex_key {
2060 name: "myapex.key",
2061 public_key: "testkey.avbpubkey",
2062 private_key: "testkey.pem",
2063 product_specific: true,
2064 }
2065
2066 cc_library {
2067 name: "mylib",
2068 srcs: ["mylib.cpp"],
2069 system_shared_libs: [],
2070 stl: "none",
2071 }
2072 `)
2073
2074 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossff6c33d2019-10-02 16:01:35 -07002075 expected := buildDir + "/target/product/test_device/product/apex"
2076 actual := apex.installDir.String()
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002077 if actual != expected {
2078 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2079 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002080}
Jiyong Park67882562019-03-21 01:11:21 +09002081
2082func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002083 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002084 apex_key {
2085 name: "myapex.key",
2086 public_key: ":my.avbpubkey",
2087 private_key: ":my.pem",
2088 product_specific: true,
2089 }
2090
2091 filegroup {
2092 name: "my.avbpubkey",
2093 srcs: ["testkey2.avbpubkey"],
2094 }
2095
2096 filegroup {
2097 name: "my.pem",
2098 srcs: ["testkey2.pem"],
2099 }
2100 `)
2101
2102 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2103 expected_pubkey := "testkey2.avbpubkey"
2104 actual_pubkey := apex_key.public_key_file.String()
2105 if actual_pubkey != expected_pubkey {
2106 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2107 }
2108 expected_privkey := "testkey2.pem"
2109 actual_privkey := apex_key.private_key_file.String()
2110 if actual_privkey != expected_privkey {
2111 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2112 }
2113}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002114
2115func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002116 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002117 prebuilt_apex {
2118 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002119 arch: {
2120 arm64: {
2121 src: "myapex-arm64.apex",
2122 },
2123 arm: {
2124 src: "myapex-arm.apex",
2125 },
2126 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002127 }
2128 `)
2129
2130 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2131
Jiyong Parkc95714e2019-03-29 14:23:10 +09002132 expectedInput := "myapex-arm64.apex"
2133 if prebuilt.inputApex.String() != expectedInput {
2134 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2135 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002136}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002137
2138func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002139 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002140 prebuilt_apex {
2141 name: "myapex",
2142 src: "myapex-arm.apex",
2143 filename: "notmyapex.apex",
2144 }
2145 `)
2146
2147 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2148
2149 expected := "notmyapex.apex"
2150 if p.installFilename != expected {
2151 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2152 }
2153}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002154
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002155func TestPrebuiltOverrides(t *testing.T) {
2156 ctx, config := testApex(t, `
2157 prebuilt_apex {
2158 name: "myapex.prebuilt",
2159 src: "myapex-arm.apex",
2160 overrides: [
2161 "myapex",
2162 ],
2163 }
2164 `)
2165
2166 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2167
2168 expected := []string{"myapex"}
2169 actual := android.AndroidMkEntriesForTest(t, config, "", p).EntryMap["LOCAL_OVERRIDES_PACKAGES"]
2170 if !reflect.DeepEqual(actual, expected) {
2171 t.Errorf("Incorrect LOCAL_OVERRIDES_PACKAGES value '%s', expected '%s'", actual, expected)
2172 }
2173}
2174
Roland Levillain630846d2019-06-26 12:48:34 +01002175func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002176 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002177 apex_test {
2178 name: "myapex",
2179 key: "myapex.key",
2180 tests: [
2181 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002182 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002183 ],
2184 }
2185
2186 apex_key {
2187 name: "myapex.key",
2188 public_key: "testkey.avbpubkey",
2189 private_key: "testkey.pem",
2190 }
2191
2192 cc_test {
2193 name: "mytest",
2194 gtest: false,
2195 srcs: ["mytest.cpp"],
2196 relative_install_path: "test",
2197 system_shared_libs: [],
2198 static_executable: true,
2199 stl: "none",
2200 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002201
2202 cc_test {
2203 name: "mytests",
2204 gtest: false,
2205 srcs: [
2206 "mytest1.cpp",
2207 "mytest2.cpp",
2208 "mytest3.cpp",
2209 ],
2210 test_per_src: true,
2211 relative_install_path: "test",
2212 system_shared_libs: [],
2213 static_executable: true,
2214 stl: "none",
2215 }
Roland Levillain630846d2019-06-26 12:48:34 +01002216 `)
2217
2218 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
2219 copyCmds := apexRule.Args["copy_commands"]
2220
2221 // Ensure that test dep is copied into apex.
2222 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002223
2224 // Ensure that test deps built with `test_per_src` are copied into apex.
2225 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2226 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2227 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002228
2229 // Ensure the module is correctly translated.
2230 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
2231 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2232 name := apexBundle.BaseModuleName()
2233 prefix := "TARGET_"
2234 var builder strings.Builder
2235 data.Custom(&builder, name, prefix, "", data)
2236 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002237 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2238 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2239 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2240 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
2241 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.json.myapex\n")
2242 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002243 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002244}
2245
Jooyung Han5c998b92019-06-27 11:30:33 +09002246func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002247 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002248 apex {
2249 name: "myapex",
2250 key: "myapex.key",
2251 native_shared_libs: ["mylib"],
2252 uses: ["commonapex"],
2253 }
2254
2255 apex {
2256 name: "commonapex",
2257 key: "myapex.key",
2258 native_shared_libs: ["libcommon"],
2259 provide_cpp_shared_libs: true,
2260 }
2261
2262 apex_key {
2263 name: "myapex.key",
2264 public_key: "testkey.avbpubkey",
2265 private_key: "testkey.pem",
2266 }
2267
2268 cc_library {
2269 name: "mylib",
2270 srcs: ["mylib.cpp"],
2271 shared_libs: ["libcommon"],
2272 system_shared_libs: [],
2273 stl: "none",
2274 }
2275
2276 cc_library {
2277 name: "libcommon",
2278 srcs: ["mylib_common.cpp"],
2279 system_shared_libs: [],
2280 stl: "none",
2281 }
2282 `)
2283
2284 module1 := ctx.ModuleForTests("myapex", "android_common_myapex")
2285 apexRule1 := module1.Rule("apexRule")
2286 copyCmds1 := apexRule1.Args["copy_commands"]
2287
2288 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex")
2289 apexRule2 := module2.Rule("apexRule")
2290 copyCmds2 := apexRule2.Args["copy_commands"]
2291
2292 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
2293 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_core_shared_commonapex")
2294 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2295 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2296 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2297}
2298
2299func TestApexUsesFailsIfNotProvided(t *testing.T) {
2300 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2301 apex {
2302 name: "myapex",
2303 key: "myapex.key",
2304 uses: ["commonapex"],
2305 }
2306
2307 apex {
2308 name: "commonapex",
2309 key: "myapex.key",
2310 }
2311
2312 apex_key {
2313 name: "myapex.key",
2314 public_key: "testkey.avbpubkey",
2315 private_key: "testkey.pem",
2316 }
2317 `)
2318 testApexError(t, `uses: "commonapex" is not a provider`, `
2319 apex {
2320 name: "myapex",
2321 key: "myapex.key",
2322 uses: ["commonapex"],
2323 }
2324
2325 cc_library {
2326 name: "commonapex",
2327 system_shared_libs: [],
2328 stl: "none",
2329 }
2330
2331 apex_key {
2332 name: "myapex.key",
2333 public_key: "testkey.avbpubkey",
2334 private_key: "testkey.pem",
2335 }
2336 `)
2337}
2338
2339func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2340 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2341 apex {
2342 name: "myapex",
2343 key: "myapex.key",
2344 use_vendor: true,
2345 uses: ["commonapex"],
2346 }
2347
2348 apex {
2349 name: "commonapex",
2350 key: "myapex.key",
2351 provide_cpp_shared_libs: true,
2352 }
2353
2354 apex_key {
2355 name: "myapex.key",
2356 public_key: "testkey.avbpubkey",
2357 private_key: "testkey.pem",
2358 }
2359 `)
2360}
2361
Jooyung Hand48f3c32019-08-23 11:18:57 +09002362func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2363 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2364 apex {
2365 name: "myapex",
2366 key: "myapex.key",
2367 native_shared_libs: ["libfoo"],
2368 }
2369
2370 apex_key {
2371 name: "myapex.key",
2372 public_key: "testkey.avbpubkey",
2373 private_key: "testkey.pem",
2374 }
2375
2376 cc_library {
2377 name: "libfoo",
2378 stl: "none",
2379 system_shared_libs: [],
2380 enabled: false,
2381 }
2382 `)
2383 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2384 apex {
2385 name: "myapex",
2386 key: "myapex.key",
2387 java_libs: ["myjar"],
2388 }
2389
2390 apex_key {
2391 name: "myapex.key",
2392 public_key: "testkey.avbpubkey",
2393 private_key: "testkey.pem",
2394 }
2395
2396 java_library {
2397 name: "myjar",
2398 srcs: ["foo/bar/MyClass.java"],
2399 sdk_version: "none",
2400 system_modules: "none",
2401 compile_dex: true,
2402 enabled: false,
2403 }
2404 `)
2405}
2406
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002407func TestApexWithApps(t *testing.T) {
2408 ctx, _ := testApex(t, `
2409 apex {
2410 name: "myapex",
2411 key: "myapex.key",
2412 apps: [
2413 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002414 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002415 ],
2416 }
2417
2418 apex_key {
2419 name: "myapex.key",
2420 public_key: "testkey.avbpubkey",
2421 private_key: "testkey.pem",
2422 }
2423
2424 android_app {
2425 name: "AppFoo",
2426 srcs: ["foo/bar/MyClass.java"],
2427 sdk_version: "none",
2428 system_modules: "none",
2429 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002430
2431 android_app {
2432 name: "AppFooPriv",
2433 srcs: ["foo/bar/MyClass.java"],
2434 sdk_version: "none",
2435 system_modules: "none",
2436 privileged: true,
2437 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002438 `)
2439
2440 module := ctx.ModuleForTests("myapex", "android_common_myapex")
2441 apexRule := module.Rule("apexRule")
2442 copyCmds := apexRule.Args["copy_commands"]
2443
2444 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002445 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Dario Frenicde2a032019-10-27 00:29:22 +01002446}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002447
Dario Frenicde2a032019-10-27 00:29:22 +01002448func TestApexWithAppImports(t *testing.T) {
2449 ctx, _ := testApex(t, `
2450 apex {
2451 name: "myapex",
2452 key: "myapex.key",
2453 apps: [
2454 "AppFooPrebuilt",
2455 "AppFooPrivPrebuilt",
2456 ],
2457 }
2458
2459 apex_key {
2460 name: "myapex.key",
2461 public_key: "testkey.avbpubkey",
2462 private_key: "testkey.pem",
2463 }
2464
2465 android_app_import {
2466 name: "AppFooPrebuilt",
2467 apk: "PrebuiltAppFoo.apk",
2468 presigned: true,
2469 dex_preopt: {
2470 enabled: false,
2471 },
2472 }
2473
2474 android_app_import {
2475 name: "AppFooPrivPrebuilt",
2476 apk: "PrebuiltAppFooPriv.apk",
2477 privileged: true,
2478 presigned: true,
2479 dex_preopt: {
2480 enabled: false,
2481 },
2482 }
2483 `)
2484
2485 module := ctx.ModuleForTests("myapex", "android_common_myapex")
2486 apexRule := module.Rule("apexRule")
2487 copyCmds := apexRule.Args["copy_commands"]
2488
2489 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2490 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002491}
2492
Jiyong Park127b40b2019-09-30 16:04:35 +09002493func TestApexAvailable(t *testing.T) {
2494 // libfoo is not available to myapex, but only to otherapex
2495 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2496 apex {
2497 name: "myapex",
2498 key: "myapex.key",
2499 native_shared_libs: ["libfoo"],
2500 }
2501
2502 apex_key {
2503 name: "myapex.key",
2504 public_key: "testkey.avbpubkey",
2505 private_key: "testkey.pem",
2506 }
2507
2508 apex {
2509 name: "otherapex",
2510 key: "otherapex.key",
2511 native_shared_libs: ["libfoo"],
2512 }
2513
2514 apex_key {
2515 name: "otherapex.key",
2516 public_key: "testkey.avbpubkey",
2517 private_key: "testkey.pem",
2518 }
2519
2520 cc_library {
2521 name: "libfoo",
2522 stl: "none",
2523 system_shared_libs: [],
2524 apex_available: ["otherapex"],
2525 }`)
2526
2527 // libbar is an indirect dep
2528 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
2529 apex {
2530 name: "myapex",
2531 key: "myapex.key",
2532 native_shared_libs: ["libfoo"],
2533 }
2534
2535 apex_key {
2536 name: "myapex.key",
2537 public_key: "testkey.avbpubkey",
2538 private_key: "testkey.pem",
2539 }
2540
2541 apex {
2542 name: "otherapex",
2543 key: "otherapex.key",
2544 native_shared_libs: ["libfoo"],
2545 }
2546
2547 apex_key {
2548 name: "otherapex.key",
2549 public_key: "testkey.avbpubkey",
2550 private_key: "testkey.pem",
2551 }
2552
2553 cc_library {
2554 name: "libfoo",
2555 stl: "none",
2556 shared_libs: ["libbar"],
2557 system_shared_libs: [],
2558 apex_available: ["myapex", "otherapex"],
2559 }
2560
2561 cc_library {
2562 name: "libbar",
2563 stl: "none",
2564 system_shared_libs: [],
2565 apex_available: ["otherapex"],
2566 }`)
2567
2568 testApexError(t, "\"otherapex\" is not a valid module name", `
2569 apex {
2570 name: "myapex",
2571 key: "myapex.key",
2572 native_shared_libs: ["libfoo"],
2573 }
2574
2575 apex_key {
2576 name: "myapex.key",
2577 public_key: "testkey.avbpubkey",
2578 private_key: "testkey.pem",
2579 }
2580
2581 cc_library {
2582 name: "libfoo",
2583 stl: "none",
2584 system_shared_libs: [],
2585 apex_available: ["otherapex"],
2586 }`)
2587
2588 ctx, _ := testApex(t, `
2589 apex {
2590 name: "myapex",
2591 key: "myapex.key",
2592 native_shared_libs: ["libfoo", "libbar"],
2593 }
2594
2595 apex_key {
2596 name: "myapex.key",
2597 public_key: "testkey.avbpubkey",
2598 private_key: "testkey.pem",
2599 }
2600
2601 cc_library {
2602 name: "libfoo",
2603 stl: "none",
2604 system_shared_libs: [],
2605 apex_available: ["myapex"],
2606 }
2607
2608 cc_library {
2609 name: "libbar",
2610 stl: "none",
2611 system_shared_libs: [],
2612 apex_available: ["//apex_available:anyapex"],
2613 }`)
2614
2615 // check that libfoo and libbar are created only for myapex, but not for the platform
2616 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared_myapex")
2617 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared")
2618 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_core_shared_myapex")
2619 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_core_shared")
2620
2621 ctx, _ = testApex(t, `
2622 apex {
2623 name: "myapex",
2624 key: "myapex.key",
2625 }
2626
2627 apex_key {
2628 name: "myapex.key",
2629 public_key: "testkey.avbpubkey",
2630 private_key: "testkey.pem",
2631 }
2632
2633 cc_library {
2634 name: "libfoo",
2635 stl: "none",
2636 system_shared_libs: [],
2637 apex_available: ["//apex_available:platform"],
2638 }`)
2639
2640 // check that libfoo is created only for the platform
2641 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared_myapex")
2642 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09002643
2644 ctx, _ = testApex(t, `
2645 apex {
2646 name: "myapex",
2647 key: "myapex.key",
2648 native_shared_libs: ["libfoo"],
2649 }
2650
2651 apex_key {
2652 name: "myapex.key",
2653 public_key: "testkey.avbpubkey",
2654 private_key: "testkey.pem",
2655 }
2656
2657 cc_library {
2658 name: "libfoo",
2659 stl: "none",
2660 system_shared_libs: [],
2661 apex_available: ["myapex"],
2662 static: {
2663 apex_available: ["//apex_available:platform"],
2664 },
2665 }`)
2666
2667 // shared variant of libfoo is only available to myapex
2668 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared_myapex")
2669 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared")
2670 // but the static variant is available to both myapex and the platform
2671 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_static_myapex")
2672 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09002673}
2674
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002675func TestMain(m *testing.M) {
2676 run := func() int {
2677 setUp()
2678 defer tearDown()
2679
2680 return m.Run()
2681 }
2682
2683 os.Exit(run())
2684}