blob: 7ae49f6a06e08ddd10bdb474aed86020cbe4975d [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 Park25fc6a92018-11-18 18:02:45 +090027)
28
29func testApex(t *testing.T, bp string) *android.TestContext {
30 config, buildDir := setup(t)
31 defer teardown(buildDir)
32
33 ctx := android.NewTestArchContext()
Alex Lightee250722018-12-06 14:00:02 -080034 ctx.RegisterModuleType("apex", android.ModuleFactoryAdaptor(ApexBundleFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090035 ctx.RegisterModuleType("apex_key", android.ModuleFactoryAdaptor(apexKeyFactory))
36
37 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
38 ctx.TopDown("apex_deps", apexDepsMutator)
39 ctx.BottomUp("apex", apexMutator)
40 })
41
42 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
43 ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(cc.LibrarySharedFactory))
Jiyong Park7e636d02019-01-28 16:16:54 +090044 ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(cc.LibraryHeaderFactory))
Jiyong Park16e91a02018-12-20 18:18:08 +090045 ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(cc.BinaryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090046 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
Jiyong Parkda6eb592018-12-19 17:12:36 +090047 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090048 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
Jiyong Park7c2ee712018-12-07 00:42:25 +090049 ctx.RegisterModuleType("prebuilt_etc", android.ModuleFactoryAdaptor(android.PrebuiltEtcFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090050 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkda6eb592018-12-19 17:12:36 +090051 ctx.BottomUp("image", cc.ImageMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090052 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Jiyong Parkda6eb592018-12-19 17:12:36 +090053 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090054 ctx.BottomUp("version", cc.VersionMutator).Parallel()
55 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
56 })
57
58 ctx.Register()
59
60 bp = bp + `
61 toolchain_library {
62 name: "libcompiler_rt-extras",
63 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090064 vendor_available: true,
65 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090066 }
67
68 toolchain_library {
69 name: "libatomic",
70 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090071 vendor_available: true,
72 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090073 }
74
75 toolchain_library {
76 name: "libgcc",
77 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090078 vendor_available: true,
79 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090080 }
81
82 toolchain_library {
83 name: "libclang_rt.builtins-aarch64-android",
84 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090085 vendor_available: true,
86 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090087 }
88
89 toolchain_library {
90 name: "libclang_rt.builtins-arm-android",
91 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090092 vendor_available: true,
93 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090094 }
95
96 cc_object {
97 name: "crtbegin_so",
98 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +090099 vendor_available: true,
100 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900101 }
102
103 cc_object {
104 name: "crtend_so",
105 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900106 vendor_available: true,
107 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900108 }
109
Alex Light3d673592019-01-18 14:37:31 -0800110 cc_object {
111 name: "crtbegin_static",
112 stl: "none",
113 }
114
115 cc_object {
116 name: "crtend_android",
117 stl: "none",
118 }
119
Jiyong Parkda6eb592018-12-19 17:12:36 +0900120 llndk_library {
121 name: "libc",
122 symbol_file: "",
123 }
124
125 llndk_library {
126 name: "libm",
127 symbol_file: "",
128 }
129
130 llndk_library {
131 name: "libdl",
132 symbol_file: "",
133 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900134 `
135
136 ctx.MockFileSystem(map[string][]byte{
Jiyong Park58e364a2019-01-19 19:24:06 +0900137 "Android.bp": []byte(bp),
138 "build/target/product/security": nil,
139 "apex_manifest.json": nil,
140 "system/sepolicy/apex/myapex-file_contexts": nil,
141 "system/sepolicy/apex/otherapex-file_contexts": nil,
142 "mylib.cpp": nil,
143 "myprebuilt": nil,
Jiyong Park7e636d02019-01-28 16:16:54 +0900144 "my_include": nil,
Jiyong Park58e364a2019-01-19 19:24:06 +0900145 "vendor/foo/devkeys/test.x509.pem": nil,
146 "vendor/foo/devkeys/test.pk8": nil,
147 "vendor/foo/devkeys/testkey.avbpubkey": nil,
148 "vendor/foo/devkeys/testkey.pem": nil,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900149 })
150 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
151 android.FailIfErrored(t, errs)
152 _, errs = ctx.PrepareBuildActions(config)
153 android.FailIfErrored(t, errs)
154
155 return ctx
156}
157
158func setup(t *testing.T) (config android.Config, buildDir string) {
159 buildDir, err := ioutil.TempDir("", "soong_apex_test")
160 if err != nil {
161 t.Fatal(err)
162 }
163
164 config = android.TestArchConfig(buildDir, nil)
Jiyong Parkda6eb592018-12-19 17:12:36 +0900165 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
Jiyong Park9335a262018-12-24 11:31:58 +0900166 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900167 return
168}
169
170func teardown(buildDir string) {
171 os.RemoveAll(buildDir)
172}
173
174// ensure that 'result' contains 'expected'
175func ensureContains(t *testing.T, result string, expected string) {
176 if !strings.Contains(result, expected) {
177 t.Errorf("%q is not found in %q", expected, result)
178 }
179}
180
181// ensures that 'result' does not contain 'notExpected'
182func ensureNotContains(t *testing.T, result string, notExpected string) {
183 if strings.Contains(result, notExpected) {
184 t.Errorf("%q is found in %q", notExpected, result)
185 }
186}
187
188func ensureListContains(t *testing.T, result []string, expected string) {
189 if !android.InList(expected, result) {
190 t.Errorf("%q is not found in %v", expected, result)
191 }
192}
193
194func ensureListNotContains(t *testing.T, result []string, notExpected string) {
195 if android.InList(notExpected, result) {
196 t.Errorf("%q is found in %v", notExpected, result)
197 }
198}
199
200// Minimal test
201func TestBasicApex(t *testing.T) {
202 ctx := testApex(t, `
203 apex {
204 name: "myapex",
205 key: "myapex.key",
206 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800207 multilib: {
208 both: {
209 binaries: ["foo",],
210 }
211 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900212 }
213
214 apex_key {
215 name: "myapex.key",
216 public_key: "testkey.avbpubkey",
217 private_key: "testkey.pem",
218 }
219
220 cc_library {
221 name: "mylib",
222 srcs: ["mylib.cpp"],
223 shared_libs: ["mylib2"],
224 system_shared_libs: [],
225 stl: "none",
226 }
227
Alex Light3d673592019-01-18 14:37:31 -0800228 cc_binary {
229 name: "foo",
230 srcs: ["mylib.cpp"],
231 compile_multilib: "both",
232 multilib: {
233 lib32: {
234 suffix: "32",
235 },
236 lib64: {
237 suffix: "64",
238 },
239 },
240 symlinks: ["foo_link_"],
241 symlink_preferred_arch: true,
242 system_shared_libs: [],
243 static_executable: true,
244 stl: "none",
245 }
246
Jiyong Park25fc6a92018-11-18 18:02:45 +0900247 cc_library {
248 name: "mylib2",
249 srcs: ["mylib.cpp"],
250 system_shared_libs: [],
251 stl: "none",
252 }
253 `)
254
255 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
256 copyCmds := apexRule.Args["copy_commands"]
257
258 // Ensure that main rule creates an output
259 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
260
261 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900262 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900263
264 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900265 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900266
267 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800268 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
269 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Logan Chien3aeedc92018-12-26 15:32:21 +0800270
271 // Ensure that the platform variant ends with _core_shared
272 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
273 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Alex Light3d673592019-01-18 14:37:31 -0800274
275 // Ensure that all symlinks are present.
276 found_foo_link_64 := false
277 found_foo := false
278 for _, cmd := range strings.Split(copyCmds, " && ") {
279 if strings.HasPrefix(cmd, "ln -s foo64") {
280 if strings.HasSuffix(cmd, "bin/foo") {
281 found_foo = true
282 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
283 found_foo_link_64 = true
284 }
285 }
286 }
287 good := found_foo && found_foo_link_64
288 if !good {
289 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
290 }
Alex Light5098a612018-11-29 17:12:15 -0800291}
292
293func TestBasicZipApex(t *testing.T) {
294 ctx := testApex(t, `
295 apex {
296 name: "myapex",
297 key: "myapex.key",
298 payload_type: "zip",
299 native_shared_libs: ["mylib"],
300 }
301
302 apex_key {
303 name: "myapex.key",
304 public_key: "testkey.avbpubkey",
305 private_key: "testkey.pem",
306 }
307
308 cc_library {
309 name: "mylib",
310 srcs: ["mylib.cpp"],
311 shared_libs: ["mylib2"],
312 system_shared_libs: [],
313 stl: "none",
314 }
315
316 cc_library {
317 name: "mylib2",
318 srcs: ["mylib.cpp"],
319 system_shared_libs: [],
320 stl: "none",
321 }
322 `)
323
324 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("zipApexRule")
325 copyCmds := zipApexRule.Args["copy_commands"]
326
327 // Ensure that main rule creates an output
328 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
329
330 // Ensure that APEX variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900331 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800332
333 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900334 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800335
336 // Ensure that both direct and indirect deps are copied into apex
337 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
338 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900339}
340
341func TestApexWithStubs(t *testing.T) {
342 ctx := testApex(t, `
343 apex {
344 name: "myapex",
345 key: "myapex.key",
346 native_shared_libs: ["mylib", "mylib3"],
347 }
348
349 apex_key {
350 name: "myapex.key",
351 public_key: "testkey.avbpubkey",
352 private_key: "testkey.pem",
353 }
354
355 cc_library {
356 name: "mylib",
357 srcs: ["mylib.cpp"],
358 shared_libs: ["mylib2", "mylib3"],
359 system_shared_libs: [],
360 stl: "none",
361 }
362
363 cc_library {
364 name: "mylib2",
365 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900366 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900367 system_shared_libs: [],
368 stl: "none",
369 stubs: {
370 versions: ["1", "2", "3"],
371 },
372 }
373
374 cc_library {
375 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900376 srcs: ["mylib.cpp"],
377 shared_libs: ["mylib4"],
378 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900379 stl: "none",
380 stubs: {
381 versions: ["10", "11", "12"],
382 },
383 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900384
385 cc_library {
386 name: "mylib4",
387 srcs: ["mylib.cpp"],
388 system_shared_libs: [],
389 stl: "none",
390 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900391 `)
392
393 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
394 copyCmds := apexRule.Args["copy_commands"]
395
396 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800397 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900398
399 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800400 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900401
402 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800403 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900404
Jiyong Parkda6eb592018-12-19 17:12:36 +0900405 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900406
407 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900408 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900409 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900410 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900411
412 // 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 +0900413 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900414 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900415 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900416
417 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900418 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900419 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900420
421 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900422 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 +0900423}
424
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900425func TestApexWithExplicitStubsDependency(t *testing.T) {
426 ctx := testApex(t, `
427 apex {
428 name: "myapex",
429 key: "myapex.key",
430 native_shared_libs: ["mylib"],
431 }
432
433 apex_key {
434 name: "myapex.key",
435 public_key: "testkey.avbpubkey",
436 private_key: "testkey.pem",
437 }
438
439 cc_library {
440 name: "mylib",
441 srcs: ["mylib.cpp"],
442 shared_libs: ["libfoo#10"],
443 system_shared_libs: [],
444 stl: "none",
445 }
446
447 cc_library {
448 name: "libfoo",
449 srcs: ["mylib.cpp"],
450 shared_libs: ["libbar"],
451 system_shared_libs: [],
452 stl: "none",
453 stubs: {
454 versions: ["10", "20", "30"],
455 },
456 }
457
458 cc_library {
459 name: "libbar",
460 srcs: ["mylib.cpp"],
461 system_shared_libs: [],
462 stl: "none",
463 }
464
465 `)
466
467 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
468 copyCmds := apexRule.Args["copy_commands"]
469
470 // Ensure that direct non-stubs dep is always included
471 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
472
473 // Ensure that indirect stubs dep is not included
474 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
475
476 // Ensure that dependency of stubs is not included
477 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
478
Jiyong Parkda6eb592018-12-19 17:12:36 +0900479 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900480
481 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900482 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900483 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900484 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900485
Jiyong Parkda6eb592018-12-19 17:12:36 +0900486 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900487
488 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
489 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
490}
491
Jiyong Park25fc6a92018-11-18 18:02:45 +0900492func TestApexWithSystemLibsStubs(t *testing.T) {
493 ctx := testApex(t, `
494 apex {
495 name: "myapex",
496 key: "myapex.key",
497 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
498 }
499
500 apex_key {
501 name: "myapex.key",
502 public_key: "testkey.avbpubkey",
503 private_key: "testkey.pem",
504 }
505
506 cc_library {
507 name: "mylib",
508 srcs: ["mylib.cpp"],
509 shared_libs: ["libdl#27"],
510 stl: "none",
511 }
512
513 cc_library_shared {
514 name: "mylib_shared",
515 srcs: ["mylib.cpp"],
516 shared_libs: ["libdl#27"],
517 stl: "none",
518 }
519
520 cc_library {
521 name: "libc",
522 no_libgcc: true,
523 nocrt: true,
524 system_shared_libs: [],
525 stl: "none",
526 stubs: {
527 versions: ["27", "28", "29"],
528 },
529 }
530
531 cc_library {
532 name: "libm",
533 no_libgcc: true,
534 nocrt: true,
535 system_shared_libs: [],
536 stl: "none",
537 stubs: {
538 versions: ["27", "28", "29"],
539 },
540 }
541
542 cc_library {
543 name: "libdl",
544 no_libgcc: true,
545 nocrt: true,
546 system_shared_libs: [],
547 stl: "none",
548 stubs: {
549 versions: ["27", "28", "29"],
550 },
551 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900552
553 cc_library {
554 name: "libBootstrap",
555 srcs: ["mylib.cpp"],
556 stl: "none",
557 bootstrap: true,
558 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900559 `)
560
561 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
562 copyCmds := apexRule.Args["copy_commands"]
563
564 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800565 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900566 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
567 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900568
569 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900570 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900571
Jiyong Parkda6eb592018-12-19 17:12:36 +0900572 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
573 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
574 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900575
576 // For dependency to libc
577 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900578 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900579 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900580 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900581 // ... Cflags from stub is correctly exported to mylib
582 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
583 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
584
585 // For dependency to libm
586 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900587 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900588 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900589 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900590 // ... and is not compiling with the stub
591 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
592 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
593
594 // For dependency to libdl
595 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900596 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900597 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900598 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
599 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900600 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900601 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900602 // ... Cflags from stub is correctly exported to mylib
603 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
604 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900605
606 // Ensure that libBootstrap is depending on the platform variant of bionic libs
607 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
608 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
609 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
610 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900611}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900612
613func TestFilesInSubDir(t *testing.T) {
614 ctx := testApex(t, `
615 apex {
616 name: "myapex",
617 key: "myapex.key",
618 prebuilts: ["myetc"],
619 }
620
621 apex_key {
622 name: "myapex.key",
623 public_key: "testkey.avbpubkey",
624 private_key: "testkey.pem",
625 }
626
627 prebuilt_etc {
628 name: "myetc",
629 src: "myprebuilt",
630 sub_dir: "foo/bar",
631 }
632 `)
633
634 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
635 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
636
637 // Ensure that etc, etc/foo, and etc/foo/bar are all listed
638 ensureListContains(t, dirs, "etc")
639 ensureListContains(t, dirs, "etc/foo")
640 ensureListContains(t, dirs, "etc/foo/bar")
641}
Jiyong Parkda6eb592018-12-19 17:12:36 +0900642
643func TestUseVendor(t *testing.T) {
644 ctx := testApex(t, `
645 apex {
646 name: "myapex",
647 key: "myapex.key",
648 native_shared_libs: ["mylib"],
649 use_vendor: true,
650 }
651
652 apex_key {
653 name: "myapex.key",
654 public_key: "testkey.avbpubkey",
655 private_key: "testkey.pem",
656 }
657
658 cc_library {
659 name: "mylib",
660 srcs: ["mylib.cpp"],
661 shared_libs: ["mylib2"],
662 system_shared_libs: [],
663 vendor_available: true,
664 stl: "none",
665 }
666
667 cc_library {
668 name: "mylib2",
669 srcs: ["mylib.cpp"],
670 system_shared_libs: [],
671 vendor_available: true,
672 stl: "none",
673 }
674 `)
675
676 inputsList := []string{}
677 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
678 for _, implicit := range i.Implicits {
679 inputsList = append(inputsList, implicit.String())
680 }
681 }
682 inputsString := strings.Join(inputsList, " ")
683
684 // ensure that the apex includes vendor variants of the direct and indirect deps
685 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib.so")
686 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib2.so")
687
688 // ensure that the apex does not include core variants
689 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
690 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
691}
Jiyong Park16e91a02018-12-20 18:18:08 +0900692
693func TestStaticLinking(t *testing.T) {
694 ctx := testApex(t, `
695 apex {
696 name: "myapex",
697 key: "myapex.key",
698 native_shared_libs: ["mylib"],
699 }
700
701 apex_key {
702 name: "myapex.key",
703 public_key: "testkey.avbpubkey",
704 private_key: "testkey.pem",
705 }
706
707 cc_library {
708 name: "mylib",
709 srcs: ["mylib.cpp"],
710 system_shared_libs: [],
711 stl: "none",
712 stubs: {
713 versions: ["1", "2", "3"],
714 },
715 }
716
717 cc_binary {
718 name: "not_in_apex",
719 srcs: ["mylib.cpp"],
720 static_libs: ["mylib"],
721 static_executable: true,
722 system_shared_libs: [],
723 stl: "none",
724 }
Jiyong Park16e91a02018-12-20 18:18:08 +0900725 `)
726
727 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
728
729 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +0800730 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +0900731}
Jiyong Park9335a262018-12-24 11:31:58 +0900732
733func TestKeys(t *testing.T) {
734 ctx := testApex(t, `
735 apex {
736 name: "myapex",
737 key: "myapex.key",
738 native_shared_libs: ["mylib"],
739 }
740
741 cc_library {
742 name: "mylib",
743 srcs: ["mylib.cpp"],
744 system_shared_libs: [],
745 stl: "none",
746 }
747
748 apex_key {
749 name: "myapex.key",
750 public_key: "testkey.avbpubkey",
751 private_key: "testkey.pem",
752 }
753
754 `)
755
756 // check the APEX keys
757 keys := ctx.ModuleForTests("myapex.key", "").Module().(*apexKey)
758
759 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
760 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
761 "vendor/foo/devkeys/testkey.avbpubkey")
762 }
763 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
764 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
765 "vendor/foo/devkeys/testkey.pem")
766 }
767
768 // check the APK certs
769 certs := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk").Args["certificates"]
770 if certs != "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8" {
771 t.Errorf("cert and private key %q are not %q", certs,
772 "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8")
773 }
774}
Jiyong Park58e364a2019-01-19 19:24:06 +0900775
776func TestMacro(t *testing.T) {
777 ctx := testApex(t, `
778 apex {
779 name: "myapex",
780 key: "myapex.key",
781 native_shared_libs: ["mylib"],
782 }
783
784 apex {
785 name: "otherapex",
786 key: "myapex.key",
787 native_shared_libs: ["mylib"],
788 }
789
790 apex_key {
791 name: "myapex.key",
792 public_key: "testkey.avbpubkey",
793 private_key: "testkey.pem",
794 }
795
796 cc_library {
797 name: "mylib",
798 srcs: ["mylib.cpp"],
799 system_shared_libs: [],
800 stl: "none",
801 }
802 `)
803
804 // non-APEX variant does not have __ANDROID__APEX__ defined
805 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
806 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
807 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
808
809 // APEX variant has __ANDROID_APEX__=<apexname> defined
810 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
811 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
812 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
813
814 // APEX variant has __ANDROID_APEX__=<apexname> defined
815 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
816 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
817 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
818}
Jiyong Park7e636d02019-01-28 16:16:54 +0900819
820func TestHeaderLibsDependency(t *testing.T) {
821 ctx := testApex(t, `
822 apex {
823 name: "myapex",
824 key: "myapex.key",
825 native_shared_libs: ["mylib"],
826 }
827
828 apex_key {
829 name: "myapex.key",
830 public_key: "testkey.avbpubkey",
831 private_key: "testkey.pem",
832 }
833
834 cc_library_headers {
835 name: "mylib_headers",
836 export_include_dirs: ["my_include"],
837 system_shared_libs: [],
838 stl: "none",
839 }
840
841 cc_library {
842 name: "mylib",
843 srcs: ["mylib.cpp"],
844 system_shared_libs: [],
845 stl: "none",
846 header_libs: ["mylib_headers"],
847 export_header_lib_headers: ["mylib_headers"],
848 stubs: {
849 versions: ["1", "2", "3"],
850 },
851 }
852
853 cc_library {
854 name: "otherlib",
855 srcs: ["mylib.cpp"],
856 system_shared_libs: [],
857 stl: "none",
858 shared_libs: ["mylib"],
859 }
860 `)
861
862 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
863
864 // Ensure that the include path of the header lib is exported to 'otherlib'
865 ensureContains(t, cFlags, "-Imy_include")
866}