blob: f73be20a6f9390bbc561d3799e579ac8f84f43b6 [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"
20 "strings"
21 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090022
23 "github.com/google/blueprint/proptools"
24
25 "android/soong/android"
26 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090027 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090028)
29
30func testApex(t *testing.T, bp string) *android.TestContext {
31 config, buildDir := setup(t)
32 defer teardown(buildDir)
33
34 ctx := android.NewTestArchContext()
Alex Light0851b882019-02-07 13:20:53 -080035 ctx.RegisterModuleType("apex", android.ModuleFactoryAdaptor(apexBundleFactory))
36 ctx.RegisterModuleType("apex_test", android.ModuleFactoryAdaptor(testApexBundleFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090037 ctx.RegisterModuleType("apex_key", android.ModuleFactoryAdaptor(apexKeyFactory))
Jiyong Park30ca9372019-02-07 16:27:23 +090038 ctx.RegisterModuleType("apex_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Jaewoong Jung939ebd52019-03-26 15:07:36 -070039 ctx.RegisterModuleType("prebuilt_apex", android.ModuleFactoryAdaptor(PrebuiltFactory))
Jiyong Park30ca9372019-02-07 16:27:23 +090040 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Park25fc6a92018-11-18 18:02:45 +090041
42 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
43 ctx.TopDown("apex_deps", apexDepsMutator)
44 ctx.BottomUp("apex", apexMutator)
Jaewoong Jung939ebd52019-03-26 15:07:36 -070045 ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel()
46 ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090047 })
48
49 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
50 ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(cc.LibrarySharedFactory))
Jiyong Park7e636d02019-01-28 16:16:54 +090051 ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(cc.LibraryHeaderFactory))
Jiyong Park16e91a02018-12-20 18:18:08 +090052 ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(cc.BinaryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090053 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
Jiyong Parkda6eb592018-12-19 17:12:36 +090054 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090055 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
Jiyong Park7c2ee712018-12-07 00:42:25 +090056 ctx.RegisterModuleType("prebuilt_etc", android.ModuleFactoryAdaptor(android.PrebuiltEtcFactory))
Jiyong Park04480cf2019-02-06 00:16:29 +090057 ctx.RegisterModuleType("sh_binary", android.ModuleFactoryAdaptor(android.ShBinaryFactory))
Jiyong Parkb2742fd2019-02-11 11:38:15 +090058 ctx.RegisterModuleType("android_app_certificate", android.ModuleFactoryAdaptor(java.AndroidAppCertificateFactory))
Jiyong Park809bb722019-02-13 21:33:49 +090059 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Jaewoong Jung939ebd52019-03-26 15:07:36 -070060 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
61 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
62 })
Jiyong Park25fc6a92018-11-18 18:02:45 +090063 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkda6eb592018-12-19 17:12:36 +090064 ctx.BottomUp("image", cc.ImageMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090065 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Jiyong Parkda6eb592018-12-19 17:12:36 +090066 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090067 ctx.BottomUp("version", cc.VersionMutator).Parallel()
68 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
69 })
70
71 ctx.Register()
72
73 bp = bp + `
74 toolchain_library {
75 name: "libcompiler_rt-extras",
76 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090077 vendor_available: true,
78 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090079 }
80
81 toolchain_library {
82 name: "libatomic",
83 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090084 vendor_available: true,
85 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090086 }
87
88 toolchain_library {
89 name: "libgcc",
90 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090091 vendor_available: true,
92 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090093 }
94
95 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -070096 name: "libgcc_stripped",
97 src: "",
98 vendor_available: true,
99 recovery_available: true,
100 }
101
102 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900103 name: "libclang_rt.builtins-aarch64-android",
104 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900105 vendor_available: true,
106 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900107 }
108
109 toolchain_library {
110 name: "libclang_rt.builtins-arm-android",
111 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900112 vendor_available: true,
113 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900114 }
115
116 cc_object {
117 name: "crtbegin_so",
118 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900119 vendor_available: true,
120 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900121 }
122
123 cc_object {
124 name: "crtend_so",
125 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900126 vendor_available: true,
127 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900128 }
129
Alex Light3d673592019-01-18 14:37:31 -0800130 cc_object {
131 name: "crtbegin_static",
132 stl: "none",
133 }
134
135 cc_object {
136 name: "crtend_android",
137 stl: "none",
138 }
139
Jiyong Parkda6eb592018-12-19 17:12:36 +0900140 llndk_library {
141 name: "libc",
142 symbol_file: "",
143 }
144
145 llndk_library {
146 name: "libm",
147 symbol_file: "",
148 }
149
150 llndk_library {
151 name: "libdl",
152 symbol_file: "",
153 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900154 `
155
156 ctx.MockFileSystem(map[string][]byte{
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900157 "Android.bp": []byte(bp),
Dan Willemsen412160e2019-04-09 21:36:26 -0700158 "build/make/target/product/security": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900159 "apex_manifest.json": nil,
Jiyong Park809bb722019-02-13 21:33:49 +0900160 "AndroidManifest.xml": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900161 "system/sepolicy/apex/myapex-file_contexts": nil,
162 "system/sepolicy/apex/myapex_keytest-file_contexts": nil,
163 "system/sepolicy/apex/otherapex-file_contexts": nil,
164 "mylib.cpp": nil,
165 "myprebuilt": nil,
166 "my_include": nil,
167 "vendor/foo/devkeys/test.x509.pem": nil,
168 "vendor/foo/devkeys/test.pk8": nil,
169 "testkey.x509.pem": nil,
170 "testkey.pk8": nil,
171 "testkey.override.x509.pem": nil,
172 "testkey.override.pk8": nil,
173 "vendor/foo/devkeys/testkey.avbpubkey": nil,
174 "vendor/foo/devkeys/testkey.pem": nil,
Jiyong Park52818fc2019-03-18 12:01:38 +0900175 "NOTICE": nil,
176 "custom_notice": nil,
Jiyong Park67882562019-03-21 01:11:21 +0900177 "testkey2.avbpubkey": nil,
178 "testkey2.pem": nil,
Jiyong Parkc95714e2019-03-29 14:23:10 +0900179 "myapex-arm64.apex": nil,
180 "myapex-arm.apex": nil,
Jiyong Park71b519d2019-04-18 17:25:49 +0900181 "frameworks/base/api/current.txt": nil,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900182 })
183 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
184 android.FailIfErrored(t, errs)
185 _, errs = ctx.PrepareBuildActions(config)
186 android.FailIfErrored(t, errs)
187
188 return ctx
189}
190
191func setup(t *testing.T) (config android.Config, buildDir string) {
192 buildDir, err := ioutil.TempDir("", "soong_apex_test")
193 if err != nil {
194 t.Fatal(err)
195 }
196
197 config = android.TestArchConfig(buildDir, nil)
Jiyong Parkda6eb592018-12-19 17:12:36 +0900198 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
Jiyong Park9335a262018-12-24 11:31:58 +0900199 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900200 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
Jiyong Park71b519d2019-04-18 17:25:49 +0900201 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
202 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900203 return
204}
205
206func teardown(buildDir string) {
207 os.RemoveAll(buildDir)
208}
209
210// ensure that 'result' contains 'expected'
211func ensureContains(t *testing.T, result string, expected string) {
212 if !strings.Contains(result, expected) {
213 t.Errorf("%q is not found in %q", expected, result)
214 }
215}
216
217// ensures that 'result' does not contain 'notExpected'
218func ensureNotContains(t *testing.T, result string, notExpected string) {
219 if strings.Contains(result, notExpected) {
220 t.Errorf("%q is found in %q", notExpected, result)
221 }
222}
223
224func ensureListContains(t *testing.T, result []string, expected string) {
225 if !android.InList(expected, result) {
226 t.Errorf("%q is not found in %v", expected, result)
227 }
228}
229
230func ensureListNotContains(t *testing.T, result []string, notExpected string) {
231 if android.InList(notExpected, result) {
232 t.Errorf("%q is found in %v", notExpected, result)
233 }
234}
235
236// Minimal test
237func TestBasicApex(t *testing.T) {
238 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900239 apex_defaults {
240 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900241 manifest: ":myapex.manifest",
242 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900243 key: "myapex.key",
244 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800245 multilib: {
246 both: {
247 binaries: ["foo",],
248 }
249 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900250 }
251
Jiyong Park30ca9372019-02-07 16:27:23 +0900252 apex {
253 name: "myapex",
254 defaults: ["myapex-defaults"],
255 }
256
Jiyong Park25fc6a92018-11-18 18:02:45 +0900257 apex_key {
258 name: "myapex.key",
259 public_key: "testkey.avbpubkey",
260 private_key: "testkey.pem",
261 }
262
Jiyong Park809bb722019-02-13 21:33:49 +0900263 filegroup {
264 name: "myapex.manifest",
265 srcs: ["apex_manifest.json"],
266 }
267
268 filegroup {
269 name: "myapex.androidmanifest",
270 srcs: ["AndroidManifest.xml"],
271 }
272
Jiyong Park25fc6a92018-11-18 18:02:45 +0900273 cc_library {
274 name: "mylib",
275 srcs: ["mylib.cpp"],
276 shared_libs: ["mylib2"],
277 system_shared_libs: [],
278 stl: "none",
279 }
280
Alex Light3d673592019-01-18 14:37:31 -0800281 cc_binary {
282 name: "foo",
283 srcs: ["mylib.cpp"],
284 compile_multilib: "both",
285 multilib: {
286 lib32: {
287 suffix: "32",
288 },
289 lib64: {
290 suffix: "64",
291 },
292 },
293 symlinks: ["foo_link_"],
294 symlink_preferred_arch: true,
295 system_shared_libs: [],
296 static_executable: true,
297 stl: "none",
298 }
299
Jiyong Park25fc6a92018-11-18 18:02:45 +0900300 cc_library {
301 name: "mylib2",
302 srcs: ["mylib.cpp"],
303 system_shared_libs: [],
304 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900305 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900306 }
307 `)
308
309 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900310
311 optFlags := apexRule.Args["opt_flags"]
312 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
313
Jiyong Park25fc6a92018-11-18 18:02:45 +0900314 copyCmds := apexRule.Args["copy_commands"]
315
316 // Ensure that main rule creates an output
317 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
318
319 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900320 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900321
322 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900323 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900324
325 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800326 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
327 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Logan Chien3aeedc92018-12-26 15:32:21 +0800328
329 // Ensure that the platform variant ends with _core_shared
330 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
331 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Alex Light3d673592019-01-18 14:37:31 -0800332
333 // Ensure that all symlinks are present.
334 found_foo_link_64 := false
335 found_foo := false
336 for _, cmd := range strings.Split(copyCmds, " && ") {
337 if strings.HasPrefix(cmd, "ln -s foo64") {
338 if strings.HasSuffix(cmd, "bin/foo") {
339 found_foo = true
340 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
341 found_foo_link_64 = true
342 }
343 }
344 }
345 good := found_foo && found_foo_link_64
346 if !good {
347 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
348 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900349
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700350 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("mergeNoticesRule")
351 noticeInputs := mergeNoticesRule.Inputs.Strings()
352 if len(noticeInputs) != 4 {
353 t.Errorf("number of input notice files: expected = 4, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900354 }
355 ensureListContains(t, noticeInputs, "NOTICE")
356 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800357}
358
359func TestBasicZipApex(t *testing.T) {
360 ctx := testApex(t, `
361 apex {
362 name: "myapex",
363 key: "myapex.key",
364 payload_type: "zip",
365 native_shared_libs: ["mylib"],
366 }
367
368 apex_key {
369 name: "myapex.key",
370 public_key: "testkey.avbpubkey",
371 private_key: "testkey.pem",
372 }
373
374 cc_library {
375 name: "mylib",
376 srcs: ["mylib.cpp"],
377 shared_libs: ["mylib2"],
378 system_shared_libs: [],
379 stl: "none",
380 }
381
382 cc_library {
383 name: "mylib2",
384 srcs: ["mylib.cpp"],
385 system_shared_libs: [],
386 stl: "none",
387 }
388 `)
389
390 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("zipApexRule")
391 copyCmds := zipApexRule.Args["copy_commands"]
392
393 // Ensure that main rule creates an output
394 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
395
396 // Ensure that APEX variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900397 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800398
399 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900400 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800401
402 // Ensure that both direct and indirect deps are copied into apex
403 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
404 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900405}
406
407func TestApexWithStubs(t *testing.T) {
408 ctx := testApex(t, `
409 apex {
410 name: "myapex",
411 key: "myapex.key",
412 native_shared_libs: ["mylib", "mylib3"],
413 }
414
415 apex_key {
416 name: "myapex.key",
417 public_key: "testkey.avbpubkey",
418 private_key: "testkey.pem",
419 }
420
421 cc_library {
422 name: "mylib",
423 srcs: ["mylib.cpp"],
424 shared_libs: ["mylib2", "mylib3"],
425 system_shared_libs: [],
426 stl: "none",
427 }
428
429 cc_library {
430 name: "mylib2",
431 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900432 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900433 system_shared_libs: [],
434 stl: "none",
435 stubs: {
436 versions: ["1", "2", "3"],
437 },
438 }
439
440 cc_library {
441 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900442 srcs: ["mylib.cpp"],
443 shared_libs: ["mylib4"],
444 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900445 stl: "none",
446 stubs: {
447 versions: ["10", "11", "12"],
448 },
449 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900450
451 cc_library {
452 name: "mylib4",
453 srcs: ["mylib.cpp"],
454 system_shared_libs: [],
455 stl: "none",
456 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900457 `)
458
459 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
460 copyCmds := apexRule.Args["copy_commands"]
461
462 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800463 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900464
465 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800466 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900467
468 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800469 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900470
Jiyong Parkda6eb592018-12-19 17:12:36 +0900471 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900472
473 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900474 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900475 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900476 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900477
478 // 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 +0900479 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900480 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900481 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900482
483 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900484 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900485 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900486
487 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900488 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 +0900489}
490
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900491func TestApexWithExplicitStubsDependency(t *testing.T) {
492 ctx := testApex(t, `
493 apex {
494 name: "myapex",
495 key: "myapex.key",
496 native_shared_libs: ["mylib"],
497 }
498
499 apex_key {
500 name: "myapex.key",
501 public_key: "testkey.avbpubkey",
502 private_key: "testkey.pem",
503 }
504
505 cc_library {
506 name: "mylib",
507 srcs: ["mylib.cpp"],
508 shared_libs: ["libfoo#10"],
509 system_shared_libs: [],
510 stl: "none",
511 }
512
513 cc_library {
514 name: "libfoo",
515 srcs: ["mylib.cpp"],
516 shared_libs: ["libbar"],
517 system_shared_libs: [],
518 stl: "none",
519 stubs: {
520 versions: ["10", "20", "30"],
521 },
522 }
523
524 cc_library {
525 name: "libbar",
526 srcs: ["mylib.cpp"],
527 system_shared_libs: [],
528 stl: "none",
529 }
530
531 `)
532
533 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
534 copyCmds := apexRule.Args["copy_commands"]
535
536 // Ensure that direct non-stubs dep is always included
537 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
538
539 // Ensure that indirect stubs dep is not included
540 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
541
542 // Ensure that dependency of stubs is not included
543 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
544
Jiyong Parkda6eb592018-12-19 17:12:36 +0900545 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900546
547 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900548 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900549 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900550 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900551
Jiyong Parkda6eb592018-12-19 17:12:36 +0900552 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900553
554 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
555 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
556}
557
Jiyong Park25fc6a92018-11-18 18:02:45 +0900558func TestApexWithSystemLibsStubs(t *testing.T) {
559 ctx := testApex(t, `
560 apex {
561 name: "myapex",
562 key: "myapex.key",
563 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
564 }
565
566 apex_key {
567 name: "myapex.key",
568 public_key: "testkey.avbpubkey",
569 private_key: "testkey.pem",
570 }
571
572 cc_library {
573 name: "mylib",
574 srcs: ["mylib.cpp"],
575 shared_libs: ["libdl#27"],
576 stl: "none",
577 }
578
579 cc_library_shared {
580 name: "mylib_shared",
581 srcs: ["mylib.cpp"],
582 shared_libs: ["libdl#27"],
583 stl: "none",
584 }
585
586 cc_library {
587 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700588 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900589 nocrt: true,
590 system_shared_libs: [],
591 stl: "none",
592 stubs: {
593 versions: ["27", "28", "29"],
594 },
595 }
596
597 cc_library {
598 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700599 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900600 nocrt: true,
601 system_shared_libs: [],
602 stl: "none",
603 stubs: {
604 versions: ["27", "28", "29"],
605 },
606 }
607
608 cc_library {
609 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700610 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900611 nocrt: true,
612 system_shared_libs: [],
613 stl: "none",
614 stubs: {
615 versions: ["27", "28", "29"],
616 },
617 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900618
619 cc_library {
620 name: "libBootstrap",
621 srcs: ["mylib.cpp"],
622 stl: "none",
623 bootstrap: true,
624 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900625 `)
626
627 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
628 copyCmds := apexRule.Args["copy_commands"]
629
630 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800631 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900632 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
633 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900634
635 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900636 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900637
Jiyong Parkda6eb592018-12-19 17:12:36 +0900638 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
639 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
640 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900641
642 // For dependency to libc
643 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900644 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900645 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900646 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900647 // ... Cflags from stub is correctly exported to mylib
648 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
649 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
650
651 // For dependency to libm
652 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900653 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900654 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900655 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900656 // ... and is not compiling with the stub
657 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
658 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
659
660 // For dependency to libdl
661 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900662 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900663 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900664 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
665 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900666 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900667 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900668 // ... Cflags from stub is correctly exported to mylib
669 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
670 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900671
672 // Ensure that libBootstrap is depending on the platform variant of bionic libs
673 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
674 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
675 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
676 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900677}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900678
679func TestFilesInSubDir(t *testing.T) {
680 ctx := testApex(t, `
681 apex {
682 name: "myapex",
683 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900684 native_shared_libs: ["mylib"],
685 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900686 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900687 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900688 }
689
690 apex_key {
691 name: "myapex.key",
692 public_key: "testkey.avbpubkey",
693 private_key: "testkey.pem",
694 }
695
696 prebuilt_etc {
697 name: "myetc",
698 src: "myprebuilt",
699 sub_dir: "foo/bar",
700 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900701
702 cc_library {
703 name: "mylib",
704 srcs: ["mylib.cpp"],
705 relative_install_path: "foo/bar",
706 system_shared_libs: [],
707 stl: "none",
708 }
709
710 cc_binary {
711 name: "mybin",
712 srcs: ["mylib.cpp"],
713 relative_install_path: "foo/bar",
714 system_shared_libs: [],
715 static_executable: true,
716 stl: "none",
717 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900718 `)
719
720 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
721 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
722
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900723 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +0900724 ensureListContains(t, dirs, "etc")
725 ensureListContains(t, dirs, "etc/foo")
726 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900727 ensureListContains(t, dirs, "lib64")
728 ensureListContains(t, dirs, "lib64/foo")
729 ensureListContains(t, dirs, "lib64/foo/bar")
730 ensureListContains(t, dirs, "lib")
731 ensureListContains(t, dirs, "lib/foo")
732 ensureListContains(t, dirs, "lib/foo/bar")
733
Jiyong Parkbd13e442019-03-15 18:10:35 +0900734 ensureListContains(t, dirs, "bin")
735 ensureListContains(t, dirs, "bin/foo")
736 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +0900737}
Jiyong Parkda6eb592018-12-19 17:12:36 +0900738
739func TestUseVendor(t *testing.T) {
740 ctx := testApex(t, `
741 apex {
742 name: "myapex",
743 key: "myapex.key",
744 native_shared_libs: ["mylib"],
745 use_vendor: true,
746 }
747
748 apex_key {
749 name: "myapex.key",
750 public_key: "testkey.avbpubkey",
751 private_key: "testkey.pem",
752 }
753
754 cc_library {
755 name: "mylib",
756 srcs: ["mylib.cpp"],
757 shared_libs: ["mylib2"],
758 system_shared_libs: [],
759 vendor_available: true,
760 stl: "none",
761 }
762
763 cc_library {
764 name: "mylib2",
765 srcs: ["mylib.cpp"],
766 system_shared_libs: [],
767 vendor_available: true,
768 stl: "none",
769 }
770 `)
771
772 inputsList := []string{}
773 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
774 for _, implicit := range i.Implicits {
775 inputsList = append(inputsList, implicit.String())
776 }
777 }
778 inputsString := strings.Join(inputsList, " ")
779
780 // ensure that the apex includes vendor variants of the direct and indirect deps
781 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib.so")
782 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib2.so")
783
784 // ensure that the apex does not include core variants
785 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
786 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
787}
Jiyong Park16e91a02018-12-20 18:18:08 +0900788
789func TestStaticLinking(t *testing.T) {
790 ctx := testApex(t, `
791 apex {
792 name: "myapex",
793 key: "myapex.key",
794 native_shared_libs: ["mylib"],
795 }
796
797 apex_key {
798 name: "myapex.key",
799 public_key: "testkey.avbpubkey",
800 private_key: "testkey.pem",
801 }
802
803 cc_library {
804 name: "mylib",
805 srcs: ["mylib.cpp"],
806 system_shared_libs: [],
807 stl: "none",
808 stubs: {
809 versions: ["1", "2", "3"],
810 },
811 }
812
813 cc_binary {
814 name: "not_in_apex",
815 srcs: ["mylib.cpp"],
816 static_libs: ["mylib"],
817 static_executable: true,
818 system_shared_libs: [],
819 stl: "none",
820 }
Jiyong Park16e91a02018-12-20 18:18:08 +0900821 `)
822
823 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
824
825 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +0800826 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +0900827}
Jiyong Park9335a262018-12-24 11:31:58 +0900828
829func TestKeys(t *testing.T) {
830 ctx := testApex(t, `
831 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900832 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +0900833 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900834 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +0900835 native_shared_libs: ["mylib"],
836 }
837
838 cc_library {
839 name: "mylib",
840 srcs: ["mylib.cpp"],
841 system_shared_libs: [],
842 stl: "none",
843 }
844
845 apex_key {
846 name: "myapex.key",
847 public_key: "testkey.avbpubkey",
848 private_key: "testkey.pem",
849 }
850
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900851 android_app_certificate {
852 name: "myapex.certificate",
853 certificate: "testkey",
854 }
855
856 android_app_certificate {
857 name: "myapex.certificate.override",
858 certificate: "testkey.override",
859 }
860
Jiyong Park9335a262018-12-24 11:31:58 +0900861 `)
862
863 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +0900864 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +0900865
866 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
867 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
868 "vendor/foo/devkeys/testkey.avbpubkey")
869 }
870 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
871 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
872 "vendor/foo/devkeys/testkey.pem")
873 }
874
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900875 // check the APK certs. It should be overridden to myapex.certificate.override
876 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
877 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +0900878 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900879 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +0900880 }
881}
Jiyong Park58e364a2019-01-19 19:24:06 +0900882
883func TestMacro(t *testing.T) {
884 ctx := testApex(t, `
885 apex {
886 name: "myapex",
887 key: "myapex.key",
888 native_shared_libs: ["mylib"],
889 }
890
891 apex {
892 name: "otherapex",
893 key: "myapex.key",
894 native_shared_libs: ["mylib"],
895 }
896
897 apex_key {
898 name: "myapex.key",
899 public_key: "testkey.avbpubkey",
900 private_key: "testkey.pem",
901 }
902
903 cc_library {
904 name: "mylib",
905 srcs: ["mylib.cpp"],
906 system_shared_libs: [],
907 stl: "none",
908 }
909 `)
910
911 // non-APEX variant does not have __ANDROID__APEX__ defined
912 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
913 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
914 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
915
916 // APEX variant has __ANDROID_APEX__=<apexname> defined
917 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
918 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
919 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
920
921 // APEX variant has __ANDROID_APEX__=<apexname> defined
922 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
923 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
924 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
925}
Jiyong Park7e636d02019-01-28 16:16:54 +0900926
927func TestHeaderLibsDependency(t *testing.T) {
928 ctx := testApex(t, `
929 apex {
930 name: "myapex",
931 key: "myapex.key",
932 native_shared_libs: ["mylib"],
933 }
934
935 apex_key {
936 name: "myapex.key",
937 public_key: "testkey.avbpubkey",
938 private_key: "testkey.pem",
939 }
940
941 cc_library_headers {
942 name: "mylib_headers",
943 export_include_dirs: ["my_include"],
944 system_shared_libs: [],
945 stl: "none",
946 }
947
948 cc_library {
949 name: "mylib",
950 srcs: ["mylib.cpp"],
951 system_shared_libs: [],
952 stl: "none",
953 header_libs: ["mylib_headers"],
954 export_header_lib_headers: ["mylib_headers"],
955 stubs: {
956 versions: ["1", "2", "3"],
957 },
958 }
959
960 cc_library {
961 name: "otherlib",
962 srcs: ["mylib.cpp"],
963 system_shared_libs: [],
964 stl: "none",
965 shared_libs: ["mylib"],
966 }
967 `)
968
969 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
970
971 // Ensure that the include path of the header lib is exported to 'otherlib'
972 ensureContains(t, cFlags, "-Imy_include")
973}
Alex Light9670d332019-01-29 18:07:33 -0800974
Alex Light0851b882019-02-07 13:20:53 -0800975func TestNonTestApex(t *testing.T) {
976 ctx := testApex(t, `
977 apex {
978 name: "myapex",
979 key: "myapex.key",
980 native_shared_libs: ["mylib_common"],
981 }
982
983 apex_key {
984 name: "myapex.key",
985 public_key: "testkey.avbpubkey",
986 private_key: "testkey.pem",
987 }
988
989 cc_library {
990 name: "mylib_common",
991 srcs: ["mylib.cpp"],
992 system_shared_libs: [],
993 stl: "none",
994 }
995 `)
996
997 module := ctx.ModuleForTests("myapex", "android_common_myapex")
998 apexRule := module.Rule("apexRule")
999 copyCmds := apexRule.Args["copy_commands"]
1000
1001 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
1002 t.Log("Apex was a test apex!")
1003 t.Fail()
1004 }
1005 // Ensure that main rule creates an output
1006 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1007
1008 // Ensure that apex variant is created for the direct dep
1009 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1010
1011 // Ensure that both direct and indirect deps are copied into apex
1012 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1013
1014 // Ensure that the platform variant ends with _core_shared
1015 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1016
1017 if !android.InAnyApex("mylib_common") {
1018 t.Log("Found mylib_common not in any apex!")
1019 t.Fail()
1020 }
1021}
1022
1023func TestTestApex(t *testing.T) {
1024 if android.InAnyApex("mylib_common_test") {
1025 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!")
1026 }
1027 ctx := testApex(t, `
1028 apex_test {
1029 name: "myapex",
1030 key: "myapex.key",
1031 native_shared_libs: ["mylib_common_test"],
1032 }
1033
1034 apex_key {
1035 name: "myapex.key",
1036 public_key: "testkey.avbpubkey",
1037 private_key: "testkey.pem",
1038 }
1039
1040 cc_library {
1041 name: "mylib_common_test",
1042 srcs: ["mylib.cpp"],
1043 system_shared_libs: [],
1044 stl: "none",
1045 }
1046 `)
1047
1048 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1049 apexRule := module.Rule("apexRule")
1050 copyCmds := apexRule.Args["copy_commands"]
1051
1052 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
1053 t.Log("Apex was not a test apex!")
1054 t.Fail()
1055 }
1056 // Ensure that main rule creates an output
1057 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1058
1059 // Ensure that apex variant is created for the direct dep
1060 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared_myapex")
1061
1062 // Ensure that both direct and indirect deps are copied into apex
1063 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
1064
1065 // Ensure that the platform variant ends with _core_shared
1066 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared")
1067
1068 if android.InAnyApex("mylib_common_test") {
1069 t.Log("Found mylib_common_test in some apex!")
1070 t.Fail()
1071 }
1072}
1073
Alex Light9670d332019-01-29 18:07:33 -08001074func TestApexWithTarget(t *testing.T) {
1075 ctx := testApex(t, `
1076 apex {
1077 name: "myapex",
1078 key: "myapex.key",
1079 multilib: {
1080 first: {
1081 native_shared_libs: ["mylib_common"],
1082 }
1083 },
1084 target: {
1085 android: {
1086 multilib: {
1087 first: {
1088 native_shared_libs: ["mylib"],
1089 }
1090 }
1091 },
1092 host: {
1093 multilib: {
1094 first: {
1095 native_shared_libs: ["mylib2"],
1096 }
1097 }
1098 }
1099 }
1100 }
1101
1102 apex_key {
1103 name: "myapex.key",
1104 public_key: "testkey.avbpubkey",
1105 private_key: "testkey.pem",
1106 }
1107
1108 cc_library {
1109 name: "mylib",
1110 srcs: ["mylib.cpp"],
1111 system_shared_libs: [],
1112 stl: "none",
1113 }
1114
1115 cc_library {
1116 name: "mylib_common",
1117 srcs: ["mylib.cpp"],
1118 system_shared_libs: [],
1119 stl: "none",
1120 compile_multilib: "first",
1121 }
1122
1123 cc_library {
1124 name: "mylib2",
1125 srcs: ["mylib.cpp"],
1126 system_shared_libs: [],
1127 stl: "none",
1128 compile_multilib: "first",
1129 }
1130 `)
1131
1132 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1133 copyCmds := apexRule.Args["copy_commands"]
1134
1135 // Ensure that main rule creates an output
1136 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1137
1138 // Ensure that apex variant is created for the direct dep
1139 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
1140 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1141 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
1142
1143 // Ensure that both direct and indirect deps are copied into apex
1144 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1145 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1146 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1147
1148 // Ensure that the platform variant ends with _core_shared
1149 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
1150 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1151 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
1152}
Jiyong Park04480cf2019-02-06 00:16:29 +09001153
1154func TestApexWithShBinary(t *testing.T) {
1155 ctx := testApex(t, `
1156 apex {
1157 name: "myapex",
1158 key: "myapex.key",
1159 binaries: ["myscript"],
1160 }
1161
1162 apex_key {
1163 name: "myapex.key",
1164 public_key: "testkey.avbpubkey",
1165 private_key: "testkey.pem",
1166 }
1167
1168 sh_binary {
1169 name: "myscript",
1170 src: "mylib.cpp",
1171 filename: "myscript.sh",
1172 sub_dir: "script",
1173 }
1174 `)
1175
1176 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1177 copyCmds := apexRule.Args["copy_commands"]
1178
1179 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
1180}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001181
1182func TestApexInProductPartition(t *testing.T) {
1183 ctx := testApex(t, `
1184 apex {
1185 name: "myapex",
1186 key: "myapex.key",
1187 native_shared_libs: ["mylib"],
1188 product_specific: true,
1189 }
1190
1191 apex_key {
1192 name: "myapex.key",
1193 public_key: "testkey.avbpubkey",
1194 private_key: "testkey.pem",
1195 product_specific: true,
1196 }
1197
1198 cc_library {
1199 name: "mylib",
1200 srcs: ["mylib.cpp"],
1201 system_shared_libs: [],
1202 stl: "none",
1203 }
1204 `)
1205
1206 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
1207 expected := "target/product/test_device/product/apex"
1208 actual := apex.installDir.RelPathString()
1209 if actual != expected {
1210 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
1211 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001212}
Jiyong Park67882562019-03-21 01:11:21 +09001213
1214func TestApexKeyFromOtherModule(t *testing.T) {
1215 ctx := testApex(t, `
1216 apex_key {
1217 name: "myapex.key",
1218 public_key: ":my.avbpubkey",
1219 private_key: ":my.pem",
1220 product_specific: true,
1221 }
1222
1223 filegroup {
1224 name: "my.avbpubkey",
1225 srcs: ["testkey2.avbpubkey"],
1226 }
1227
1228 filegroup {
1229 name: "my.pem",
1230 srcs: ["testkey2.pem"],
1231 }
1232 `)
1233
1234 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
1235 expected_pubkey := "testkey2.avbpubkey"
1236 actual_pubkey := apex_key.public_key_file.String()
1237 if actual_pubkey != expected_pubkey {
1238 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
1239 }
1240 expected_privkey := "testkey2.pem"
1241 actual_privkey := apex_key.private_key_file.String()
1242 if actual_privkey != expected_privkey {
1243 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
1244 }
1245}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001246
1247func TestPrebuilt(t *testing.T) {
1248 ctx := testApex(t, `
1249 prebuilt_apex {
1250 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09001251 arch: {
1252 arm64: {
1253 src: "myapex-arm64.apex",
1254 },
1255 arm: {
1256 src: "myapex-arm.apex",
1257 },
1258 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001259 }
1260 `)
1261
1262 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1263
Jiyong Parkc95714e2019-03-29 14:23:10 +09001264 expectedInput := "myapex-arm64.apex"
1265 if prebuilt.inputApex.String() != expectedInput {
1266 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
1267 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001268}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001269
1270func TestPrebuiltFilenameOverride(t *testing.T) {
1271 ctx := testApex(t, `
1272 prebuilt_apex {
1273 name: "myapex",
1274 src: "myapex-arm.apex",
1275 filename: "notmyapex.apex",
1276 }
1277 `)
1278
1279 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1280
1281 expected := "notmyapex.apex"
1282 if p.installFilename != expected {
1283 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
1284 }
1285}