blob: e2d85aea0289e95a9cb11bc6dfc29de9623bd593 [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"
Jiyong Park25fc6a92018-11-18 18:02:45 +090021 "strings"
22 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090023
24 "github.com/google/blueprint/proptools"
25
26 "android/soong/android"
27 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090028 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090029)
30
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070031var buildDir string
32
Jooyung Hand3639552019-08-09 12:57:43 +090033// names returns name list from white space separated string
34func names(s string) (ns []string) {
35 for _, n := range strings.Split(s, " ") {
36 if len(n) > 0 {
37 ns = append(ns, n)
38 }
39 }
40 return
41}
42
Jooyung Han5c998b92019-06-27 11:30:33 +090043func testApexError(t *testing.T, pattern, bp string) {
44 ctx, config := testApexContext(t, bp)
45 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
46 if len(errs) > 0 {
47 android.FailIfNoMatchingErrors(t, pattern, errs)
48 return
49 }
50 _, errs = ctx.PrepareBuildActions(config)
51 if len(errs) > 0 {
52 android.FailIfNoMatchingErrors(t, pattern, errs)
53 return
54 }
55
56 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
57}
58
Jaewoong Jung22f7d182019-07-16 18:25:41 -070059func testApex(t *testing.T, bp string) (*android.TestContext, android.Config) {
Jooyung Han5c998b92019-06-27 11:30:33 +090060 ctx, config := testApexContext(t, bp)
61 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
62 android.FailIfErrored(t, errs)
63 _, errs = ctx.PrepareBuildActions(config)
64 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070065 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090066}
67
68func testApexContext(t *testing.T, bp string) (*android.TestContext, android.Config) {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -070069 config := android.TestArchConfig(buildDir, nil)
70 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
71 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
72 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
73 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
74 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
Jiyong Park25fc6a92018-11-18 18:02:45 +090075
76 ctx := android.NewTestArchContext()
Alex Light0851b882019-02-07 13:20:53 -080077 ctx.RegisterModuleType("apex", android.ModuleFactoryAdaptor(apexBundleFactory))
78 ctx.RegisterModuleType("apex_test", android.ModuleFactoryAdaptor(testApexBundleFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090079 ctx.RegisterModuleType("apex_key", android.ModuleFactoryAdaptor(apexKeyFactory))
Jiyong Park30ca9372019-02-07 16:27:23 +090080 ctx.RegisterModuleType("apex_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Jaewoong Jung939ebd52019-03-26 15:07:36 -070081 ctx.RegisterModuleType("prebuilt_apex", android.ModuleFactoryAdaptor(PrebuiltFactory))
Jiyong Park30ca9372019-02-07 16:27:23 +090082 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Park25fc6a92018-11-18 18:02:45 +090083
84 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
85 ctx.TopDown("apex_deps", apexDepsMutator)
86 ctx.BottomUp("apex", apexMutator)
Jooyung Han5c998b92019-06-27 11:30:33 +090087 ctx.BottomUp("apex_uses", apexUsesMutator)
Jaewoong Jung939ebd52019-03-26 15:07:36 -070088 ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel()
89 ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090090 })
91
92 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
93 ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(cc.LibrarySharedFactory))
Jiyong Park7e636d02019-01-28 16:16:54 +090094 ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(cc.LibraryHeaderFactory))
Jiyong Park16e91a02018-12-20 18:18:08 +090095 ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(cc.BinaryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090096 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
Roland Levillain630846d2019-06-26 12:48:34 +010097 ctx.RegisterModuleType("cc_test", android.ModuleFactoryAdaptor(cc.TestFactory))
Jiyong Parkda6eb592018-12-19 17:12:36 +090098 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090099 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
Jiyong Park7c2ee712018-12-07 00:42:25 +0900100 ctx.RegisterModuleType("prebuilt_etc", android.ModuleFactoryAdaptor(android.PrebuiltEtcFactory))
Jiyong Park04480cf2019-02-06 00:16:29 +0900101 ctx.RegisterModuleType("sh_binary", android.ModuleFactoryAdaptor(android.ShBinaryFactory))
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900102 ctx.RegisterModuleType("android_app_certificate", android.ModuleFactoryAdaptor(java.AndroidAppCertificateFactory))
Jiyong Park809bb722019-02-13 21:33:49 +0900103 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Jiyong Park7f7766d2019-07-25 22:02:35 +0900104 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(java.LibraryFactory))
Jiyong Park9e6c2422019-08-09 20:39:45 +0900105 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(java.ImportFactory))
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900106 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(java.AndroidAppFactory))
Jiyong Park7f7766d2019-07-25 22:02:35 +0900107
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700108 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
109 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
110 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900111 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900112 ctx.BottomUp("image", cc.ImageMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900113 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900114 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Roland Levillain9b5fde92019-06-28 15:41:19 +0100115 ctx.BottomUp("test_per_src", cc.TestPerSrcMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900116 ctx.BottomUp("version", cc.VersionMutator).Parallel()
117 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
118 })
119
120 ctx.Register()
121
122 bp = bp + `
123 toolchain_library {
124 name: "libcompiler_rt-extras",
125 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900126 vendor_available: true,
127 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900128 }
129
130 toolchain_library {
131 name: "libatomic",
132 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900133 vendor_available: true,
134 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900135 }
136
137 toolchain_library {
138 name: "libgcc",
139 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900140 vendor_available: true,
141 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900142 }
143
144 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700145 name: "libgcc_stripped",
146 src: "",
147 vendor_available: true,
148 recovery_available: true,
149 }
150
151 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900152 name: "libclang_rt.builtins-aarch64-android",
153 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900154 vendor_available: true,
155 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900156 }
157
158 toolchain_library {
159 name: "libclang_rt.builtins-arm-android",
160 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900161 vendor_available: true,
162 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900163 }
164
165 cc_object {
166 name: "crtbegin_so",
167 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900168 vendor_available: true,
169 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900170 }
171
172 cc_object {
173 name: "crtend_so",
174 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900175 vendor_available: true,
176 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900177 }
178
Alex Light3d673592019-01-18 14:37:31 -0800179 cc_object {
180 name: "crtbegin_static",
181 stl: "none",
182 }
183
184 cc_object {
185 name: "crtend_android",
186 stl: "none",
187 }
188
Jiyong Parkda6eb592018-12-19 17:12:36 +0900189 llndk_library {
190 name: "libc",
191 symbol_file: "",
192 }
193
194 llndk_library {
195 name: "libm",
196 symbol_file: "",
197 }
198
199 llndk_library {
200 name: "libdl",
201 symbol_file: "",
202 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900203 `
204
205 ctx.MockFileSystem(map[string][]byte{
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900206 "Android.bp": []byte(bp),
Dan Willemsen412160e2019-04-09 21:36:26 -0700207 "build/make/target/product/security": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900208 "apex_manifest.json": nil,
Jiyong Park809bb722019-02-13 21:33:49 +0900209 "AndroidManifest.xml": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900210 "system/sepolicy/apex/myapex-file_contexts": nil,
211 "system/sepolicy/apex/myapex_keytest-file_contexts": nil,
212 "system/sepolicy/apex/otherapex-file_contexts": nil,
Jooyung Han5c998b92019-06-27 11:30:33 +0900213 "system/sepolicy/apex/commonapex-file_contexts": nil,
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900214 "mylib.cpp": nil,
215 "mylib_common.cpp": nil,
216 "mytest.cpp": nil,
217 "mytest1.cpp": nil,
218 "mytest2.cpp": nil,
219 "mytest3.cpp": nil,
220 "myprebuilt": nil,
221 "my_include": nil,
222 "foo/bar/MyClass.java": nil,
223 "prebuilt.jar": nil,
224 "vendor/foo/devkeys/test.x509.pem": nil,
225 "vendor/foo/devkeys/test.pk8": nil,
226 "testkey.x509.pem": nil,
227 "testkey.pk8": nil,
228 "testkey.override.x509.pem": nil,
229 "testkey.override.pk8": nil,
230 "vendor/foo/devkeys/testkey.avbpubkey": nil,
231 "vendor/foo/devkeys/testkey.pem": nil,
232 "NOTICE": nil,
233 "custom_notice": nil,
234 "testkey2.avbpubkey": nil,
235 "testkey2.pem": nil,
236 "myapex-arm64.apex": nil,
237 "myapex-arm.apex": nil,
238 "frameworks/base/api/current.txt": nil,
239 "build/make/core/proguard.flags": nil,
240 "build/make/core/proguard_basic_keeps.flags": nil,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900241 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900242
Jooyung Han5c998b92019-06-27 11:30:33 +0900243 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900244}
245
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700246func setUp() {
247 var err error
248 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900249 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700250 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900251 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900252}
253
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700254func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900255 os.RemoveAll(buildDir)
256}
257
258// ensure that 'result' contains 'expected'
259func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900260 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900261 if !strings.Contains(result, expected) {
262 t.Errorf("%q is not found in %q", expected, result)
263 }
264}
265
266// ensures that 'result' does not contain 'notExpected'
267func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900268 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900269 if strings.Contains(result, notExpected) {
270 t.Errorf("%q is found in %q", notExpected, result)
271 }
272}
273
274func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900275 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900276 if !android.InList(expected, result) {
277 t.Errorf("%q is not found in %v", expected, result)
278 }
279}
280
281func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900282 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900283 if android.InList(notExpected, result) {
284 t.Errorf("%q is found in %v", notExpected, result)
285 }
286}
287
Jooyung Hane1633032019-08-01 17:41:43 +0900288func ensureListEmpty(t *testing.T, result []string) {
289 t.Helper()
290 if len(result) > 0 {
291 t.Errorf("%q is expected to be empty", result)
292 }
293}
294
Jiyong Park25fc6a92018-11-18 18:02:45 +0900295// Minimal test
296func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700297 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900298 apex_defaults {
299 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900300 manifest: ":myapex.manifest",
301 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900302 key: "myapex.key",
303 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800304 multilib: {
305 both: {
306 binaries: ["foo",],
307 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900308 },
Jiyong Park9e6c2422019-08-09 20:39:45 +0900309 java_libs: ["myjar", "myprebuiltjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900310 }
311
Jiyong Park30ca9372019-02-07 16:27:23 +0900312 apex {
313 name: "myapex",
314 defaults: ["myapex-defaults"],
315 }
316
Jiyong Park25fc6a92018-11-18 18:02:45 +0900317 apex_key {
318 name: "myapex.key",
319 public_key: "testkey.avbpubkey",
320 private_key: "testkey.pem",
321 }
322
Jiyong Park809bb722019-02-13 21:33:49 +0900323 filegroup {
324 name: "myapex.manifest",
325 srcs: ["apex_manifest.json"],
326 }
327
328 filegroup {
329 name: "myapex.androidmanifest",
330 srcs: ["AndroidManifest.xml"],
331 }
332
Jiyong Park25fc6a92018-11-18 18:02:45 +0900333 cc_library {
334 name: "mylib",
335 srcs: ["mylib.cpp"],
336 shared_libs: ["mylib2"],
337 system_shared_libs: [],
338 stl: "none",
339 }
340
Alex Light3d673592019-01-18 14:37:31 -0800341 cc_binary {
342 name: "foo",
343 srcs: ["mylib.cpp"],
344 compile_multilib: "both",
345 multilib: {
346 lib32: {
347 suffix: "32",
348 },
349 lib64: {
350 suffix: "64",
351 },
352 },
353 symlinks: ["foo_link_"],
354 symlink_preferred_arch: true,
355 system_shared_libs: [],
356 static_executable: true,
357 stl: "none",
358 }
359
Jiyong Park25fc6a92018-11-18 18:02:45 +0900360 cc_library {
361 name: "mylib2",
362 srcs: ["mylib.cpp"],
363 system_shared_libs: [],
364 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900365 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900366 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900367
368 java_library {
369 name: "myjar",
370 srcs: ["foo/bar/MyClass.java"],
371 sdk_version: "none",
372 system_modules: "none",
373 compile_dex: true,
374 static_libs: ["myotherjar"],
375 }
376
377 java_library {
378 name: "myotherjar",
379 srcs: ["foo/bar/MyClass.java"],
380 sdk_version: "none",
381 system_modules: "none",
382 compile_dex: true,
383 }
Jiyong Park9e6c2422019-08-09 20:39:45 +0900384
385 java_import {
386 name: "myprebuiltjar",
387 jars: ["prebuilt.jar"],
388 installable: true,
389 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900390 `)
391
392 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900393
394 optFlags := apexRule.Args["opt_flags"]
395 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700396 // Ensure that the NOTICE output is being packaged as an asset.
397 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900398
Jiyong Park25fc6a92018-11-18 18:02:45 +0900399 copyCmds := apexRule.Args["copy_commands"]
400
401 // Ensure that main rule creates an output
402 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
403
404 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900405 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900406 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900407 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900408
409 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900410 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900411 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900412
413 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800414 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
415 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900416 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900417 ensureContains(t, copyCmds, "image.apex/javalib/myprebuiltjar.jar")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900418 // .. but not for java libs
419 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800420
Jiyong Park7f7766d2019-07-25 22:02:35 +0900421 // Ensure that the platform variant ends with _core_shared or _common
Logan Chien3aeedc92018-12-26 15:32:21 +0800422 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
423 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900424 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
425 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900426 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common")
Alex Light3d673592019-01-18 14:37:31 -0800427
428 // Ensure that all symlinks are present.
429 found_foo_link_64 := false
430 found_foo := false
431 for _, cmd := range strings.Split(copyCmds, " && ") {
432 if strings.HasPrefix(cmd, "ln -s foo64") {
433 if strings.HasSuffix(cmd, "bin/foo") {
434 found_foo = true
435 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
436 found_foo_link_64 = true
437 }
438 }
439 }
440 good := found_foo && found_foo_link_64
441 if !good {
442 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
443 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900444
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700445 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("mergeNoticesRule")
446 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700447 if len(noticeInputs) != 2 {
448 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900449 }
450 ensureListContains(t, noticeInputs, "NOTICE")
451 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800452}
453
454func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700455 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800456 apex {
457 name: "myapex",
458 key: "myapex.key",
459 payload_type: "zip",
460 native_shared_libs: ["mylib"],
461 }
462
463 apex_key {
464 name: "myapex.key",
465 public_key: "testkey.avbpubkey",
466 private_key: "testkey.pem",
467 }
468
469 cc_library {
470 name: "mylib",
471 srcs: ["mylib.cpp"],
472 shared_libs: ["mylib2"],
473 system_shared_libs: [],
474 stl: "none",
475 }
476
477 cc_library {
478 name: "mylib2",
479 srcs: ["mylib.cpp"],
480 system_shared_libs: [],
481 stl: "none",
482 }
483 `)
484
485 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("zipApexRule")
486 copyCmds := zipApexRule.Args["copy_commands"]
487
488 // Ensure that main rule creates an output
489 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
490
491 // Ensure that APEX variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900492 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800493
494 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900495 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800496
497 // Ensure that both direct and indirect deps are copied into apex
498 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
499 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900500}
501
502func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700503 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900504 apex {
505 name: "myapex",
506 key: "myapex.key",
507 native_shared_libs: ["mylib", "mylib3"],
508 }
509
510 apex_key {
511 name: "myapex.key",
512 public_key: "testkey.avbpubkey",
513 private_key: "testkey.pem",
514 }
515
516 cc_library {
517 name: "mylib",
518 srcs: ["mylib.cpp"],
519 shared_libs: ["mylib2", "mylib3"],
520 system_shared_libs: [],
521 stl: "none",
522 }
523
524 cc_library {
525 name: "mylib2",
526 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900527 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900528 system_shared_libs: [],
529 stl: "none",
530 stubs: {
531 versions: ["1", "2", "3"],
532 },
533 }
534
535 cc_library {
536 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900537 srcs: ["mylib.cpp"],
538 shared_libs: ["mylib4"],
539 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900540 stl: "none",
541 stubs: {
542 versions: ["10", "11", "12"],
543 },
544 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900545
546 cc_library {
547 name: "mylib4",
548 srcs: ["mylib.cpp"],
549 system_shared_libs: [],
550 stl: "none",
551 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900552 `)
553
554 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
555 copyCmds := apexRule.Args["copy_commands"]
556
557 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800558 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900559
560 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800561 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900562
563 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800564 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900565
Jiyong Parkda6eb592018-12-19 17:12:36 +0900566 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900567
568 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900569 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900570 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900571 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900572
573 // 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 +0900574 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900575 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900576 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900577
578 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900579 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900580 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900581
582 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900583 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 +0900584}
585
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900586func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700587 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900588 apex {
589 name: "myapex",
590 key: "myapex.key",
591 native_shared_libs: ["mylib"],
592 }
593
594 apex_key {
595 name: "myapex.key",
596 public_key: "testkey.avbpubkey",
597 private_key: "testkey.pem",
598 }
599
600 cc_library {
601 name: "mylib",
602 srcs: ["mylib.cpp"],
603 shared_libs: ["libfoo#10"],
604 system_shared_libs: [],
605 stl: "none",
606 }
607
608 cc_library {
609 name: "libfoo",
610 srcs: ["mylib.cpp"],
611 shared_libs: ["libbar"],
612 system_shared_libs: [],
613 stl: "none",
614 stubs: {
615 versions: ["10", "20", "30"],
616 },
617 }
618
619 cc_library {
620 name: "libbar",
621 srcs: ["mylib.cpp"],
622 system_shared_libs: [],
623 stl: "none",
624 }
625
626 `)
627
628 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
629 copyCmds := apexRule.Args["copy_commands"]
630
631 // Ensure that direct non-stubs dep is always included
632 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
633
634 // Ensure that indirect stubs dep is not included
635 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
636
637 // Ensure that dependency of stubs is not included
638 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
639
Jiyong Parkda6eb592018-12-19 17:12:36 +0900640 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900641
642 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900643 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900644 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900645 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900646
Jiyong Parkda6eb592018-12-19 17:12:36 +0900647 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900648
649 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
650 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
651}
652
Jooyung Hand3639552019-08-09 12:57:43 +0900653func TestApexWithRuntimeLibsDependency(t *testing.T) {
654 /*
655 myapex
656 |
657 v (runtime_libs)
658 mylib ------+------> libfoo [provides stub]
659 |
660 `------> libbar
661 */
662 ctx, _ := testApex(t, `
663 apex {
664 name: "myapex",
665 key: "myapex.key",
666 native_shared_libs: ["mylib"],
667 }
668
669 apex_key {
670 name: "myapex.key",
671 public_key: "testkey.avbpubkey",
672 private_key: "testkey.pem",
673 }
674
675 cc_library {
676 name: "mylib",
677 srcs: ["mylib.cpp"],
678 runtime_libs: ["libfoo", "libbar"],
679 system_shared_libs: [],
680 stl: "none",
681 }
682
683 cc_library {
684 name: "libfoo",
685 srcs: ["mylib.cpp"],
686 system_shared_libs: [],
687 stl: "none",
688 stubs: {
689 versions: ["10", "20", "30"],
690 },
691 }
692
693 cc_library {
694 name: "libbar",
695 srcs: ["mylib.cpp"],
696 system_shared_libs: [],
697 stl: "none",
698 }
699
700 `)
701
702 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
703 copyCmds := apexRule.Args["copy_commands"]
704
705 // Ensure that direct non-stubs dep is always included
706 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
707
708 // Ensure that indirect stubs dep is not included
709 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
710
711 // Ensure that runtime_libs dep in included
712 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
713
714 injectRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("injectApexDependency")
715 ensureListEmpty(t, names(injectRule.Args["provideNativeLibs"]))
716 ensureListContains(t, names(injectRule.Args["requireNativeLibs"]), "libfoo.so")
717
718}
719
Jooyung Han9c80bae2019-08-20 17:30:57 +0900720func TestApexDependencyToLLNDK(t *testing.T) {
721 ctx, _ := testApex(t, `
722 apex {
723 name: "myapex",
724 key: "myapex.key",
725 use_vendor: true,
726 native_shared_libs: ["mylib"],
727 }
728
729 apex_key {
730 name: "myapex.key",
731 public_key: "testkey.avbpubkey",
732 private_key: "testkey.pem",
733 }
734
735 cc_library {
736 name: "mylib",
737 srcs: ["mylib.cpp"],
738 vendor_available: true,
739 shared_libs: ["libbar"],
740 system_shared_libs: [],
741 stl: "none",
742 }
743
744 cc_library {
745 name: "libbar",
746 srcs: ["mylib.cpp"],
747 system_shared_libs: [],
748 stl: "none",
749 }
750
751 llndk_library {
752 name: "libbar",
753 symbol_file: "",
754 }
755
756 `)
757
758 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
759 copyCmds := apexRule.Args["copy_commands"]
760
761 // Ensure that LLNDK dep is not included
762 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
763
764 injectRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("injectApexDependency")
765 ensureListEmpty(t, names(injectRule.Args["provideNativeLibs"]))
766
767 // Ensure that LLNDK dep is required
768 ensureListContains(t, names(injectRule.Args["requireNativeLibs"]), "libbar.so")
769
770}
771
Jiyong Park25fc6a92018-11-18 18:02:45 +0900772func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700773 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900774 apex {
775 name: "myapex",
776 key: "myapex.key",
777 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
778 }
779
780 apex_key {
781 name: "myapex.key",
782 public_key: "testkey.avbpubkey",
783 private_key: "testkey.pem",
784 }
785
786 cc_library {
787 name: "mylib",
788 srcs: ["mylib.cpp"],
789 shared_libs: ["libdl#27"],
790 stl: "none",
791 }
792
793 cc_library_shared {
794 name: "mylib_shared",
795 srcs: ["mylib.cpp"],
796 shared_libs: ["libdl#27"],
797 stl: "none",
798 }
799
800 cc_library {
801 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700802 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900803 nocrt: true,
804 system_shared_libs: [],
805 stl: "none",
806 stubs: {
807 versions: ["27", "28", "29"],
808 },
809 }
810
811 cc_library {
812 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700813 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900814 nocrt: true,
815 system_shared_libs: [],
816 stl: "none",
817 stubs: {
818 versions: ["27", "28", "29"],
819 },
820 }
821
822 cc_library {
823 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700824 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900825 nocrt: true,
826 system_shared_libs: [],
827 stl: "none",
828 stubs: {
829 versions: ["27", "28", "29"],
830 },
831 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900832
833 cc_library {
834 name: "libBootstrap",
835 srcs: ["mylib.cpp"],
836 stl: "none",
837 bootstrap: true,
838 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900839 `)
840
841 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
842 copyCmds := apexRule.Args["copy_commands"]
843
844 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800845 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900846 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
847 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900848
849 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900850 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900851
Jiyong Parkda6eb592018-12-19 17:12:36 +0900852 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
853 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
854 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900855
856 // For dependency to libc
857 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900858 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900859 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900860 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900861 // ... Cflags from stub is correctly exported to mylib
862 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
863 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
864
865 // For dependency to libm
866 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900867 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900868 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900869 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900870 // ... and is not compiling with the stub
871 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
872 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
873
874 // For dependency to libdl
875 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900876 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900877 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900878 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
879 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900880 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900881 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900882 // ... Cflags from stub is correctly exported to mylib
883 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
884 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900885
886 // Ensure that libBootstrap is depending on the platform variant of bionic libs
887 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
888 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
889 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
890 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900891}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900892
893func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700894 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +0900895 apex {
896 name: "myapex",
897 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900898 native_shared_libs: ["mylib"],
899 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900900 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900901 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900902 }
903
904 apex_key {
905 name: "myapex.key",
906 public_key: "testkey.avbpubkey",
907 private_key: "testkey.pem",
908 }
909
910 prebuilt_etc {
911 name: "myetc",
912 src: "myprebuilt",
913 sub_dir: "foo/bar",
914 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900915
916 cc_library {
917 name: "mylib",
918 srcs: ["mylib.cpp"],
919 relative_install_path: "foo/bar",
920 system_shared_libs: [],
921 stl: "none",
922 }
923
924 cc_binary {
925 name: "mybin",
926 srcs: ["mylib.cpp"],
927 relative_install_path: "foo/bar",
928 system_shared_libs: [],
929 static_executable: true,
930 stl: "none",
931 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900932 `)
933
934 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
935 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
936
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900937 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +0900938 ensureListContains(t, dirs, "etc")
939 ensureListContains(t, dirs, "etc/foo")
940 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900941 ensureListContains(t, dirs, "lib64")
942 ensureListContains(t, dirs, "lib64/foo")
943 ensureListContains(t, dirs, "lib64/foo/bar")
944 ensureListContains(t, dirs, "lib")
945 ensureListContains(t, dirs, "lib/foo")
946 ensureListContains(t, dirs, "lib/foo/bar")
947
Jiyong Parkbd13e442019-03-15 18:10:35 +0900948 ensureListContains(t, dirs, "bin")
949 ensureListContains(t, dirs, "bin/foo")
950 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +0900951}
Jiyong Parkda6eb592018-12-19 17:12:36 +0900952
953func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700954 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +0900955 apex {
956 name: "myapex",
957 key: "myapex.key",
958 native_shared_libs: ["mylib"],
959 use_vendor: true,
960 }
961
962 apex_key {
963 name: "myapex.key",
964 public_key: "testkey.avbpubkey",
965 private_key: "testkey.pem",
966 }
967
968 cc_library {
969 name: "mylib",
970 srcs: ["mylib.cpp"],
971 shared_libs: ["mylib2"],
972 system_shared_libs: [],
973 vendor_available: true,
974 stl: "none",
975 }
976
977 cc_library {
978 name: "mylib2",
979 srcs: ["mylib.cpp"],
980 system_shared_libs: [],
981 vendor_available: true,
982 stl: "none",
983 }
984 `)
985
986 inputsList := []string{}
987 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
988 for _, implicit := range i.Implicits {
989 inputsList = append(inputsList, implicit.String())
990 }
991 }
992 inputsString := strings.Join(inputsList, " ")
993
994 // ensure that the apex includes vendor variants of the direct and indirect deps
995 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib.so")
996 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib2.so")
997
998 // ensure that the apex does not include core variants
999 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
1000 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
1001}
Jiyong Park16e91a02018-12-20 18:18:08 +09001002
Jooyung Han5c998b92019-06-27 11:30:33 +09001003func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1004 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1005 apex {
1006 name: "myapex",
1007 key: "myapex.key",
1008 native_shared_libs: ["mylib"],
1009 use_vendor: true,
1010 }
1011
1012 apex_key {
1013 name: "myapex.key",
1014 public_key: "testkey.avbpubkey",
1015 private_key: "testkey.pem",
1016 }
1017
1018 cc_library {
1019 name: "mylib",
1020 srcs: ["mylib.cpp"],
1021 system_shared_libs: [],
1022 stl: "none",
1023 }
1024 `)
1025}
1026
Jiyong Park16e91a02018-12-20 18:18:08 +09001027func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001028 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001029 apex {
1030 name: "myapex",
1031 key: "myapex.key",
1032 native_shared_libs: ["mylib"],
1033 }
1034
1035 apex_key {
1036 name: "myapex.key",
1037 public_key: "testkey.avbpubkey",
1038 private_key: "testkey.pem",
1039 }
1040
1041 cc_library {
1042 name: "mylib",
1043 srcs: ["mylib.cpp"],
1044 system_shared_libs: [],
1045 stl: "none",
1046 stubs: {
1047 versions: ["1", "2", "3"],
1048 },
1049 }
1050
1051 cc_binary {
1052 name: "not_in_apex",
1053 srcs: ["mylib.cpp"],
1054 static_libs: ["mylib"],
1055 static_executable: true,
1056 system_shared_libs: [],
1057 stl: "none",
1058 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001059 `)
1060
1061 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
1062
1063 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +08001064 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001065}
Jiyong Park9335a262018-12-24 11:31:58 +09001066
1067func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001068 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001069 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001070 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001071 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001072 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001073 native_shared_libs: ["mylib"],
1074 }
1075
1076 cc_library {
1077 name: "mylib",
1078 srcs: ["mylib.cpp"],
1079 system_shared_libs: [],
1080 stl: "none",
1081 }
1082
1083 apex_key {
1084 name: "myapex.key",
1085 public_key: "testkey.avbpubkey",
1086 private_key: "testkey.pem",
1087 }
1088
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001089 android_app_certificate {
1090 name: "myapex.certificate",
1091 certificate: "testkey",
1092 }
1093
1094 android_app_certificate {
1095 name: "myapex.certificate.override",
1096 certificate: "testkey.override",
1097 }
1098
Jiyong Park9335a262018-12-24 11:31:58 +09001099 `)
1100
1101 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001102 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001103
1104 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1105 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1106 "vendor/foo/devkeys/testkey.avbpubkey")
1107 }
1108 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1109 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1110 "vendor/foo/devkeys/testkey.pem")
1111 }
1112
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001113 // check the APK certs. It should be overridden to myapex.certificate.override
1114 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
1115 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001116 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001117 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001118 }
1119}
Jiyong Park58e364a2019-01-19 19:24:06 +09001120
1121func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001122 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001123 apex {
1124 name: "myapex",
1125 key: "myapex.key",
1126 native_shared_libs: ["mylib"],
1127 }
1128
1129 apex {
1130 name: "otherapex",
1131 key: "myapex.key",
1132 native_shared_libs: ["mylib"],
1133 }
1134
1135 apex_key {
1136 name: "myapex.key",
1137 public_key: "testkey.avbpubkey",
1138 private_key: "testkey.pem",
1139 }
1140
1141 cc_library {
1142 name: "mylib",
1143 srcs: ["mylib.cpp"],
1144 system_shared_libs: [],
1145 stl: "none",
1146 }
1147 `)
1148
1149 // non-APEX variant does not have __ANDROID__APEX__ defined
1150 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
1151 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
1152 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
1153
1154 // APEX variant has __ANDROID_APEX__=<apexname> defined
1155 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
1156 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
1157 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
1158
1159 // APEX variant has __ANDROID_APEX__=<apexname> defined
1160 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
1161 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
1162 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
1163}
Jiyong Park7e636d02019-01-28 16:16:54 +09001164
1165func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001166 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001167 apex {
1168 name: "myapex",
1169 key: "myapex.key",
1170 native_shared_libs: ["mylib"],
1171 }
1172
1173 apex_key {
1174 name: "myapex.key",
1175 public_key: "testkey.avbpubkey",
1176 private_key: "testkey.pem",
1177 }
1178
1179 cc_library_headers {
1180 name: "mylib_headers",
1181 export_include_dirs: ["my_include"],
1182 system_shared_libs: [],
1183 stl: "none",
1184 }
1185
1186 cc_library {
1187 name: "mylib",
1188 srcs: ["mylib.cpp"],
1189 system_shared_libs: [],
1190 stl: "none",
1191 header_libs: ["mylib_headers"],
1192 export_header_lib_headers: ["mylib_headers"],
1193 stubs: {
1194 versions: ["1", "2", "3"],
1195 },
1196 }
1197
1198 cc_library {
1199 name: "otherlib",
1200 srcs: ["mylib.cpp"],
1201 system_shared_libs: [],
1202 stl: "none",
1203 shared_libs: ["mylib"],
1204 }
1205 `)
1206
1207 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
1208
1209 // Ensure that the include path of the header lib is exported to 'otherlib'
1210 ensureContains(t, cFlags, "-Imy_include")
1211}
Alex Light9670d332019-01-29 18:07:33 -08001212
Jooyung Hane1633032019-08-01 17:41:43 +09001213func TestDependenciesInApexManifest(t *testing.T) {
1214 ctx, _ := testApex(t, `
1215 apex {
1216 name: "myapex_nodep",
1217 key: "myapex.key",
1218 native_shared_libs: ["lib_nodep"],
1219 compile_multilib: "both",
1220 file_contexts: "myapex",
1221 }
1222
1223 apex {
1224 name: "myapex_dep",
1225 key: "myapex.key",
1226 native_shared_libs: ["lib_dep"],
1227 compile_multilib: "both",
1228 file_contexts: "myapex",
1229 }
1230
1231 apex {
1232 name: "myapex_provider",
1233 key: "myapex.key",
1234 native_shared_libs: ["libfoo"],
1235 compile_multilib: "both",
1236 file_contexts: "myapex",
1237 }
1238
1239 apex {
1240 name: "myapex_selfcontained",
1241 key: "myapex.key",
1242 native_shared_libs: ["lib_dep", "libfoo"],
1243 compile_multilib: "both",
1244 file_contexts: "myapex",
1245 }
1246
1247 apex_key {
1248 name: "myapex.key",
1249 public_key: "testkey.avbpubkey",
1250 private_key: "testkey.pem",
1251 }
1252
1253 cc_library {
1254 name: "lib_nodep",
1255 srcs: ["mylib.cpp"],
1256 system_shared_libs: [],
1257 stl: "none",
1258 }
1259
1260 cc_library {
1261 name: "lib_dep",
1262 srcs: ["mylib.cpp"],
1263 shared_libs: ["libfoo"],
1264 system_shared_libs: [],
1265 stl: "none",
1266 }
1267
1268 cc_library {
1269 name: "libfoo",
1270 srcs: ["mytest.cpp"],
1271 stubs: {
1272 versions: ["1"],
1273 },
1274 system_shared_libs: [],
1275 stl: "none",
1276 }
1277 `)
1278
Jooyung Hane1633032019-08-01 17:41:43 +09001279 var injectRule android.TestingBuildParams
1280 var provideNativeLibs, requireNativeLibs []string
1281
1282 injectRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep").Rule("injectApexDependency")
1283 provideNativeLibs = names(injectRule.Args["provideNativeLibs"])
1284 requireNativeLibs = names(injectRule.Args["requireNativeLibs"])
1285 ensureListEmpty(t, provideNativeLibs)
1286 ensureListEmpty(t, requireNativeLibs)
1287
1288 injectRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep").Rule("injectApexDependency")
1289 provideNativeLibs = names(injectRule.Args["provideNativeLibs"])
1290 requireNativeLibs = names(injectRule.Args["requireNativeLibs"])
1291 ensureListEmpty(t, provideNativeLibs)
1292 ensureListContains(t, requireNativeLibs, "libfoo.so")
1293
1294 injectRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider").Rule("injectApexDependency")
1295 provideNativeLibs = names(injectRule.Args["provideNativeLibs"])
1296 requireNativeLibs = names(injectRule.Args["requireNativeLibs"])
1297 ensureListContains(t, provideNativeLibs, "libfoo.so")
1298 ensureListEmpty(t, requireNativeLibs)
1299
1300 injectRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained").Rule("injectApexDependency")
1301 provideNativeLibs = names(injectRule.Args["provideNativeLibs"])
1302 requireNativeLibs = names(injectRule.Args["requireNativeLibs"])
1303 ensureListContains(t, provideNativeLibs, "libfoo.so")
1304 ensureListEmpty(t, requireNativeLibs)
1305}
1306
Alex Light0851b882019-02-07 13:20:53 -08001307func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001308 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08001309 apex {
1310 name: "myapex",
1311 key: "myapex.key",
1312 native_shared_libs: ["mylib_common"],
1313 }
1314
1315 apex_key {
1316 name: "myapex.key",
1317 public_key: "testkey.avbpubkey",
1318 private_key: "testkey.pem",
1319 }
1320
1321 cc_library {
1322 name: "mylib_common",
1323 srcs: ["mylib.cpp"],
1324 system_shared_libs: [],
1325 stl: "none",
1326 }
1327 `)
1328
1329 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1330 apexRule := module.Rule("apexRule")
1331 copyCmds := apexRule.Args["copy_commands"]
1332
1333 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
1334 t.Log("Apex was a test apex!")
1335 t.Fail()
1336 }
1337 // Ensure that main rule creates an output
1338 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1339
1340 // Ensure that apex variant is created for the direct dep
1341 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1342
1343 // Ensure that both direct and indirect deps are copied into apex
1344 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1345
1346 // Ensure that the platform variant ends with _core_shared
1347 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1348
1349 if !android.InAnyApex("mylib_common") {
1350 t.Log("Found mylib_common not in any apex!")
1351 t.Fail()
1352 }
1353}
1354
1355func TestTestApex(t *testing.T) {
1356 if android.InAnyApex("mylib_common_test") {
1357 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!")
1358 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001359 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08001360 apex_test {
1361 name: "myapex",
1362 key: "myapex.key",
1363 native_shared_libs: ["mylib_common_test"],
1364 }
1365
1366 apex_key {
1367 name: "myapex.key",
1368 public_key: "testkey.avbpubkey",
1369 private_key: "testkey.pem",
1370 }
1371
1372 cc_library {
1373 name: "mylib_common_test",
1374 srcs: ["mylib.cpp"],
1375 system_shared_libs: [],
1376 stl: "none",
1377 }
1378 `)
1379
1380 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1381 apexRule := module.Rule("apexRule")
1382 copyCmds := apexRule.Args["copy_commands"]
1383
1384 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
1385 t.Log("Apex was not a test apex!")
1386 t.Fail()
1387 }
1388 // Ensure that main rule creates an output
1389 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1390
1391 // Ensure that apex variant is created for the direct dep
1392 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared_myapex")
1393
1394 // Ensure that both direct and indirect deps are copied into apex
1395 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
1396
1397 // Ensure that the platform variant ends with _core_shared
1398 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared")
1399
1400 if android.InAnyApex("mylib_common_test") {
1401 t.Log("Found mylib_common_test in some apex!")
1402 t.Fail()
1403 }
1404}
1405
Alex Light9670d332019-01-29 18:07:33 -08001406func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001407 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08001408 apex {
1409 name: "myapex",
1410 key: "myapex.key",
1411 multilib: {
1412 first: {
1413 native_shared_libs: ["mylib_common"],
1414 }
1415 },
1416 target: {
1417 android: {
1418 multilib: {
1419 first: {
1420 native_shared_libs: ["mylib"],
1421 }
1422 }
1423 },
1424 host: {
1425 multilib: {
1426 first: {
1427 native_shared_libs: ["mylib2"],
1428 }
1429 }
1430 }
1431 }
1432 }
1433
1434 apex_key {
1435 name: "myapex.key",
1436 public_key: "testkey.avbpubkey",
1437 private_key: "testkey.pem",
1438 }
1439
1440 cc_library {
1441 name: "mylib",
1442 srcs: ["mylib.cpp"],
1443 system_shared_libs: [],
1444 stl: "none",
1445 }
1446
1447 cc_library {
1448 name: "mylib_common",
1449 srcs: ["mylib.cpp"],
1450 system_shared_libs: [],
1451 stl: "none",
1452 compile_multilib: "first",
1453 }
1454
1455 cc_library {
1456 name: "mylib2",
1457 srcs: ["mylib.cpp"],
1458 system_shared_libs: [],
1459 stl: "none",
1460 compile_multilib: "first",
1461 }
1462 `)
1463
1464 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1465 copyCmds := apexRule.Args["copy_commands"]
1466
1467 // Ensure that main rule creates an output
1468 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1469
1470 // Ensure that apex variant is created for the direct dep
1471 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
1472 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1473 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
1474
1475 // Ensure that both direct and indirect deps are copied into apex
1476 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1477 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1478 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1479
1480 // Ensure that the platform variant ends with _core_shared
1481 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
1482 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1483 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
1484}
Jiyong Park04480cf2019-02-06 00:16:29 +09001485
1486func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001487 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09001488 apex {
1489 name: "myapex",
1490 key: "myapex.key",
1491 binaries: ["myscript"],
1492 }
1493
1494 apex_key {
1495 name: "myapex.key",
1496 public_key: "testkey.avbpubkey",
1497 private_key: "testkey.pem",
1498 }
1499
1500 sh_binary {
1501 name: "myscript",
1502 src: "mylib.cpp",
1503 filename: "myscript.sh",
1504 sub_dir: "script",
1505 }
1506 `)
1507
1508 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1509 copyCmds := apexRule.Args["copy_commands"]
1510
1511 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
1512}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001513
1514func TestApexInProductPartition(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001515 ctx, _ := testApex(t, `
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001516 apex {
1517 name: "myapex",
1518 key: "myapex.key",
1519 native_shared_libs: ["mylib"],
1520 product_specific: true,
1521 }
1522
1523 apex_key {
1524 name: "myapex.key",
1525 public_key: "testkey.avbpubkey",
1526 private_key: "testkey.pem",
1527 product_specific: true,
1528 }
1529
1530 cc_library {
1531 name: "mylib",
1532 srcs: ["mylib.cpp"],
1533 system_shared_libs: [],
1534 stl: "none",
1535 }
1536 `)
1537
1538 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
1539 expected := "target/product/test_device/product/apex"
1540 actual := apex.installDir.RelPathString()
1541 if actual != expected {
1542 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
1543 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001544}
Jiyong Park67882562019-03-21 01:11:21 +09001545
1546func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001547 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09001548 apex_key {
1549 name: "myapex.key",
1550 public_key: ":my.avbpubkey",
1551 private_key: ":my.pem",
1552 product_specific: true,
1553 }
1554
1555 filegroup {
1556 name: "my.avbpubkey",
1557 srcs: ["testkey2.avbpubkey"],
1558 }
1559
1560 filegroup {
1561 name: "my.pem",
1562 srcs: ["testkey2.pem"],
1563 }
1564 `)
1565
1566 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
1567 expected_pubkey := "testkey2.avbpubkey"
1568 actual_pubkey := apex_key.public_key_file.String()
1569 if actual_pubkey != expected_pubkey {
1570 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
1571 }
1572 expected_privkey := "testkey2.pem"
1573 actual_privkey := apex_key.private_key_file.String()
1574 if actual_privkey != expected_privkey {
1575 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
1576 }
1577}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001578
1579func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001580 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001581 prebuilt_apex {
1582 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09001583 arch: {
1584 arm64: {
1585 src: "myapex-arm64.apex",
1586 },
1587 arm: {
1588 src: "myapex-arm.apex",
1589 },
1590 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001591 }
1592 `)
1593
1594 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1595
Jiyong Parkc95714e2019-03-29 14:23:10 +09001596 expectedInput := "myapex-arm64.apex"
1597 if prebuilt.inputApex.String() != expectedInput {
1598 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
1599 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001600}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001601
1602func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001603 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001604 prebuilt_apex {
1605 name: "myapex",
1606 src: "myapex-arm.apex",
1607 filename: "notmyapex.apex",
1608 }
1609 `)
1610
1611 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1612
1613 expected := "notmyapex.apex"
1614 if p.installFilename != expected {
1615 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
1616 }
1617}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07001618
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001619func TestPrebuiltOverrides(t *testing.T) {
1620 ctx, config := testApex(t, `
1621 prebuilt_apex {
1622 name: "myapex.prebuilt",
1623 src: "myapex-arm.apex",
1624 overrides: [
1625 "myapex",
1626 ],
1627 }
1628 `)
1629
1630 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
1631
1632 expected := []string{"myapex"}
1633 actual := android.AndroidMkEntriesForTest(t, config, "", p).EntryMap["LOCAL_OVERRIDES_PACKAGES"]
1634 if !reflect.DeepEqual(actual, expected) {
1635 t.Errorf("Incorrect LOCAL_OVERRIDES_PACKAGES value '%s', expected '%s'", actual, expected)
1636 }
1637}
1638
Roland Levillain630846d2019-06-26 12:48:34 +01001639func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01001640 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01001641 apex_test {
1642 name: "myapex",
1643 key: "myapex.key",
1644 tests: [
1645 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01001646 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01001647 ],
1648 }
1649
1650 apex_key {
1651 name: "myapex.key",
1652 public_key: "testkey.avbpubkey",
1653 private_key: "testkey.pem",
1654 }
1655
1656 cc_test {
1657 name: "mytest",
1658 gtest: false,
1659 srcs: ["mytest.cpp"],
1660 relative_install_path: "test",
1661 system_shared_libs: [],
1662 static_executable: true,
1663 stl: "none",
1664 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01001665
1666 cc_test {
1667 name: "mytests",
1668 gtest: false,
1669 srcs: [
1670 "mytest1.cpp",
1671 "mytest2.cpp",
1672 "mytest3.cpp",
1673 ],
1674 test_per_src: true,
1675 relative_install_path: "test",
1676 system_shared_libs: [],
1677 static_executable: true,
1678 stl: "none",
1679 }
Roland Levillain630846d2019-06-26 12:48:34 +01001680 `)
1681
1682 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1683 copyCmds := apexRule.Args["copy_commands"]
1684
1685 // Ensure that test dep is copied into apex.
1686 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01001687
1688 // Ensure that test deps built with `test_per_src` are copied into apex.
1689 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
1690 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
1691 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01001692
1693 // Ensure the module is correctly translated.
1694 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
1695 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
1696 name := apexBundle.BaseModuleName()
1697 prefix := "TARGET_"
1698 var builder strings.Builder
1699 data.Custom(&builder, name, prefix, "", data)
1700 androidMk := builder.String()
1701 ensureContains(t, androidMk, "LOCAL_MODULE := myapex.mytest\n")
1702 ensureContains(t, androidMk, "LOCAL_MODULE := myapex.mytest1\n")
1703 ensureContains(t, androidMk, "LOCAL_MODULE := myapex.mytest2\n")
1704 ensureContains(t, androidMk, "LOCAL_MODULE := myapex.mytest3\n")
1705 ensureContains(t, androidMk, "LOCAL_MODULE := myapex.apex_manifest.json\n")
1706 ensureContains(t, androidMk, "LOCAL_MODULE := myapex.apex_pubkey\n")
1707 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01001708}
1709
Jooyung Han5c998b92019-06-27 11:30:33 +09001710func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001711 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09001712 apex {
1713 name: "myapex",
1714 key: "myapex.key",
1715 native_shared_libs: ["mylib"],
1716 uses: ["commonapex"],
1717 }
1718
1719 apex {
1720 name: "commonapex",
1721 key: "myapex.key",
1722 native_shared_libs: ["libcommon"],
1723 provide_cpp_shared_libs: true,
1724 }
1725
1726 apex_key {
1727 name: "myapex.key",
1728 public_key: "testkey.avbpubkey",
1729 private_key: "testkey.pem",
1730 }
1731
1732 cc_library {
1733 name: "mylib",
1734 srcs: ["mylib.cpp"],
1735 shared_libs: ["libcommon"],
1736 system_shared_libs: [],
1737 stl: "none",
1738 }
1739
1740 cc_library {
1741 name: "libcommon",
1742 srcs: ["mylib_common.cpp"],
1743 system_shared_libs: [],
1744 stl: "none",
1745 }
1746 `)
1747
1748 module1 := ctx.ModuleForTests("myapex", "android_common_myapex")
1749 apexRule1 := module1.Rule("apexRule")
1750 copyCmds1 := apexRule1.Args["copy_commands"]
1751
1752 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex")
1753 apexRule2 := module2.Rule("apexRule")
1754 copyCmds2 := apexRule2.Args["copy_commands"]
1755
1756 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
1757 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_core_shared_commonapex")
1758 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
1759 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
1760 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
1761}
1762
1763func TestApexUsesFailsIfNotProvided(t *testing.T) {
1764 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
1765 apex {
1766 name: "myapex",
1767 key: "myapex.key",
1768 uses: ["commonapex"],
1769 }
1770
1771 apex {
1772 name: "commonapex",
1773 key: "myapex.key",
1774 }
1775
1776 apex_key {
1777 name: "myapex.key",
1778 public_key: "testkey.avbpubkey",
1779 private_key: "testkey.pem",
1780 }
1781 `)
1782 testApexError(t, `uses: "commonapex" is not a provider`, `
1783 apex {
1784 name: "myapex",
1785 key: "myapex.key",
1786 uses: ["commonapex"],
1787 }
1788
1789 cc_library {
1790 name: "commonapex",
1791 system_shared_libs: [],
1792 stl: "none",
1793 }
1794
1795 apex_key {
1796 name: "myapex.key",
1797 public_key: "testkey.avbpubkey",
1798 private_key: "testkey.pem",
1799 }
1800 `)
1801}
1802
1803func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
1804 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
1805 apex {
1806 name: "myapex",
1807 key: "myapex.key",
1808 use_vendor: true,
1809 uses: ["commonapex"],
1810 }
1811
1812 apex {
1813 name: "commonapex",
1814 key: "myapex.key",
1815 provide_cpp_shared_libs: true,
1816 }
1817
1818 apex_key {
1819 name: "myapex.key",
1820 public_key: "testkey.avbpubkey",
1821 private_key: "testkey.pem",
1822 }
1823 `)
1824}
1825
Jiyong Park4f7dd9b2019-08-12 10:37:49 +09001826func TestApexUsesFailsIfUseNoApex(t *testing.T) {
1827 testApexError(t, `tries to include no_apex module mylib2`, `
1828 apex {
1829 name: "commonapex",
1830 key: "myapex.key",
1831 native_shared_libs: ["mylib"],
1832 }
1833
1834 apex_key {
1835 name: "myapex.key",
1836 public_key: "testkey.avbpubkey",
1837 private_key: "testkey.pem",
1838 }
1839
1840 cc_library {
1841 name: "mylib",
1842 srcs: ["mylib.cpp"],
1843 shared_libs: ["mylib2"],
1844 system_shared_libs: [],
1845 stl: "none",
1846 }
1847
1848 cc_library {
1849 name: "mylib2",
1850 srcs: ["mylib.cpp"],
1851 system_shared_libs: [],
1852 stl: "none",
1853 no_apex: true,
1854 }
1855 `)
1856
Sundong Ahn2db7f462019-08-27 18:53:12 +09001857 testApexError(t, `tries to include no_apex module mylib2`, `
1858 apex {
1859 name: "commonapex",
1860 key: "myapex.key",
1861 native_shared_libs: ["mylib"],
1862 }
1863
1864 apex_key {
1865 name: "myapex.key",
1866 public_key: "testkey.avbpubkey",
1867 private_key: "testkey.pem",
1868 }
1869
1870 cc_library {
1871 name: "mylib",
1872 srcs: ["mylib.cpp"],
1873 static_libs: ["mylib2"],
1874 system_shared_libs: [],
1875 stl: "none",
1876 }
1877
1878 cc_library {
1879 name: "mylib2",
1880 srcs: ["mylib.cpp"],
1881 system_shared_libs: [],
1882 stl: "none",
1883 no_apex: true,
1884 }
1885 `)
1886
Jiyong Park4f7dd9b2019-08-12 10:37:49 +09001887 ctx, _ := testApex(t, `
1888 apex {
1889 name: "myapex",
1890 key: "myapex.key",
1891 native_shared_libs: ["mylib"],
1892 }
1893
1894 apex_key {
1895 name: "myapex.key",
1896 public_key: "testkey.avbpubkey",
1897 private_key: "testkey.pem",
1898 }
1899
1900 cc_library {
1901 name: "mylib",
1902 srcs: ["mylib.cpp"],
1903 shared_libs: ["mylib2"],
1904 system_shared_libs: [],
1905 stl: "none",
1906 }
1907
1908 cc_library {
1909 name: "mylib2",
1910 srcs: ["mylib.cpp"],
1911 shared_libs: ["mylib3"],
1912 system_shared_libs: [],
1913 stl: "none",
1914 stubs: {
1915 versions: ["1", "2", "3"],
1916 },
1917 }
1918
1919 cc_library {
1920 name: "mylib3",
1921 srcs: ["mylib.cpp"],
1922 system_shared_libs: [],
1923 stl: "none",
1924 no_apex: true,
1925 }
1926 `)
1927
1928 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1929 apexRule := module.Rule("apexRule")
1930 copyCmds := apexRule.Args["copy_commands"]
1931
1932 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1933 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1934 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1935
1936}
1937
Jooyung Hand48f3c32019-08-23 11:18:57 +09001938func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
1939 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
1940 apex {
1941 name: "myapex",
1942 key: "myapex.key",
1943 native_shared_libs: ["libfoo"],
1944 }
1945
1946 apex_key {
1947 name: "myapex.key",
1948 public_key: "testkey.avbpubkey",
1949 private_key: "testkey.pem",
1950 }
1951
1952 cc_library {
1953 name: "libfoo",
1954 stl: "none",
1955 system_shared_libs: [],
1956 enabled: false,
1957 }
1958 `)
1959 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
1960 apex {
1961 name: "myapex",
1962 key: "myapex.key",
1963 java_libs: ["myjar"],
1964 }
1965
1966 apex_key {
1967 name: "myapex.key",
1968 public_key: "testkey.avbpubkey",
1969 private_key: "testkey.pem",
1970 }
1971
1972 java_library {
1973 name: "myjar",
1974 srcs: ["foo/bar/MyClass.java"],
1975 sdk_version: "none",
1976 system_modules: "none",
1977 compile_dex: true,
1978 enabled: false,
1979 }
1980 `)
1981}
1982
Sundong Ahne1f05aa2019-08-27 13:55:42 +09001983func TestApexWithApps(t *testing.T) {
1984 ctx, _ := testApex(t, `
1985 apex {
1986 name: "myapex",
1987 key: "myapex.key",
1988 apps: [
1989 "AppFoo",
1990 ],
1991 }
1992
1993 apex_key {
1994 name: "myapex.key",
1995 public_key: "testkey.avbpubkey",
1996 private_key: "testkey.pem",
1997 }
1998
1999 android_app {
2000 name: "AppFoo",
2001 srcs: ["foo/bar/MyClass.java"],
2002 sdk_version: "none",
2003 system_modules: "none",
2004 }
2005 `)
2006
2007 module := ctx.ModuleForTests("myapex", "android_common_myapex")
2008 apexRule := module.Rule("apexRule")
2009 copyCmds := apexRule.Args["copy_commands"]
2010
2011 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
2012
2013}
2014
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002015func TestMain(m *testing.M) {
2016 run := func() int {
2017 setUp()
2018 defer tearDown()
2019
2020 return m.Run()
2021 }
2022
2023 os.Exit(run())
2024}