blob: 1315c25df6cd4df443fdf369851c2052444f21be [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",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900618 native_shared_libs: ["mylib"],
619 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900620 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900621 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900622 }
623
624 apex_key {
625 name: "myapex.key",
626 public_key: "testkey.avbpubkey",
627 private_key: "testkey.pem",
628 }
629
630 prebuilt_etc {
631 name: "myetc",
632 src: "myprebuilt",
633 sub_dir: "foo/bar",
634 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900635
636 cc_library {
637 name: "mylib",
638 srcs: ["mylib.cpp"],
639 relative_install_path: "foo/bar",
640 system_shared_libs: [],
641 stl: "none",
642 }
643
644 cc_binary {
645 name: "mybin",
646 srcs: ["mylib.cpp"],
647 relative_install_path: "foo/bar",
648 system_shared_libs: [],
649 static_executable: true,
650 stl: "none",
651 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900652 `)
653
654 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
655 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
656
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900657 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +0900658 ensureListContains(t, dirs, "etc")
659 ensureListContains(t, dirs, "etc/foo")
660 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900661 ensureListContains(t, dirs, "lib64")
662 ensureListContains(t, dirs, "lib64/foo")
663 ensureListContains(t, dirs, "lib64/foo/bar")
664 ensureListContains(t, dirs, "lib")
665 ensureListContains(t, dirs, "lib/foo")
666 ensureListContains(t, dirs, "lib/foo/bar")
667
668 // TODO(b/123721777) respect relative path for binaries
669 // ensureListContains(t, dirs, "bin")
670 // ensureListContains(t, dirs, "bin/foo")
671 // ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +0900672}
Jiyong Parkda6eb592018-12-19 17:12:36 +0900673
674func TestUseVendor(t *testing.T) {
675 ctx := testApex(t, `
676 apex {
677 name: "myapex",
678 key: "myapex.key",
679 native_shared_libs: ["mylib"],
680 use_vendor: true,
681 }
682
683 apex_key {
684 name: "myapex.key",
685 public_key: "testkey.avbpubkey",
686 private_key: "testkey.pem",
687 }
688
689 cc_library {
690 name: "mylib",
691 srcs: ["mylib.cpp"],
692 shared_libs: ["mylib2"],
693 system_shared_libs: [],
694 vendor_available: true,
695 stl: "none",
696 }
697
698 cc_library {
699 name: "mylib2",
700 srcs: ["mylib.cpp"],
701 system_shared_libs: [],
702 vendor_available: true,
703 stl: "none",
704 }
705 `)
706
707 inputsList := []string{}
708 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
709 for _, implicit := range i.Implicits {
710 inputsList = append(inputsList, implicit.String())
711 }
712 }
713 inputsString := strings.Join(inputsList, " ")
714
715 // ensure that the apex includes vendor variants of the direct and indirect deps
716 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib.so")
717 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib2.so")
718
719 // ensure that the apex does not include core variants
720 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
721 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
722}
Jiyong Park16e91a02018-12-20 18:18:08 +0900723
724func TestStaticLinking(t *testing.T) {
725 ctx := testApex(t, `
726 apex {
727 name: "myapex",
728 key: "myapex.key",
729 native_shared_libs: ["mylib"],
730 }
731
732 apex_key {
733 name: "myapex.key",
734 public_key: "testkey.avbpubkey",
735 private_key: "testkey.pem",
736 }
737
738 cc_library {
739 name: "mylib",
740 srcs: ["mylib.cpp"],
741 system_shared_libs: [],
742 stl: "none",
743 stubs: {
744 versions: ["1", "2", "3"],
745 },
746 }
747
748 cc_binary {
749 name: "not_in_apex",
750 srcs: ["mylib.cpp"],
751 static_libs: ["mylib"],
752 static_executable: true,
753 system_shared_libs: [],
754 stl: "none",
755 }
Jiyong Park16e91a02018-12-20 18:18:08 +0900756 `)
757
758 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
759
760 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +0800761 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +0900762}
Jiyong Park9335a262018-12-24 11:31:58 +0900763
764func TestKeys(t *testing.T) {
765 ctx := testApex(t, `
766 apex {
767 name: "myapex",
768 key: "myapex.key",
769 native_shared_libs: ["mylib"],
770 }
771
772 cc_library {
773 name: "mylib",
774 srcs: ["mylib.cpp"],
775 system_shared_libs: [],
776 stl: "none",
777 }
778
779 apex_key {
780 name: "myapex.key",
781 public_key: "testkey.avbpubkey",
782 private_key: "testkey.pem",
783 }
784
785 `)
786
787 // check the APEX keys
788 keys := ctx.ModuleForTests("myapex.key", "").Module().(*apexKey)
789
790 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
791 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
792 "vendor/foo/devkeys/testkey.avbpubkey")
793 }
794 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
795 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
796 "vendor/foo/devkeys/testkey.pem")
797 }
798
799 // check the APK certs
800 certs := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk").Args["certificates"]
801 if certs != "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8" {
802 t.Errorf("cert and private key %q are not %q", certs,
803 "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8")
804 }
805}
Jiyong Park58e364a2019-01-19 19:24:06 +0900806
807func TestMacro(t *testing.T) {
808 ctx := testApex(t, `
809 apex {
810 name: "myapex",
811 key: "myapex.key",
812 native_shared_libs: ["mylib"],
813 }
814
815 apex {
816 name: "otherapex",
817 key: "myapex.key",
818 native_shared_libs: ["mylib"],
819 }
820
821 apex_key {
822 name: "myapex.key",
823 public_key: "testkey.avbpubkey",
824 private_key: "testkey.pem",
825 }
826
827 cc_library {
828 name: "mylib",
829 srcs: ["mylib.cpp"],
830 system_shared_libs: [],
831 stl: "none",
832 }
833 `)
834
835 // non-APEX variant does not have __ANDROID__APEX__ defined
836 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
837 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
838 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
839
840 // APEX variant has __ANDROID_APEX__=<apexname> defined
841 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
842 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
843 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
844
845 // APEX variant has __ANDROID_APEX__=<apexname> defined
846 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
847 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
848 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
849}
Jiyong Park7e636d02019-01-28 16:16:54 +0900850
851func TestHeaderLibsDependency(t *testing.T) {
852 ctx := testApex(t, `
853 apex {
854 name: "myapex",
855 key: "myapex.key",
856 native_shared_libs: ["mylib"],
857 }
858
859 apex_key {
860 name: "myapex.key",
861 public_key: "testkey.avbpubkey",
862 private_key: "testkey.pem",
863 }
864
865 cc_library_headers {
866 name: "mylib_headers",
867 export_include_dirs: ["my_include"],
868 system_shared_libs: [],
869 stl: "none",
870 }
871
872 cc_library {
873 name: "mylib",
874 srcs: ["mylib.cpp"],
875 system_shared_libs: [],
876 stl: "none",
877 header_libs: ["mylib_headers"],
878 export_header_lib_headers: ["mylib_headers"],
879 stubs: {
880 versions: ["1", "2", "3"],
881 },
882 }
883
884 cc_library {
885 name: "otherlib",
886 srcs: ["mylib.cpp"],
887 system_shared_libs: [],
888 stl: "none",
889 shared_libs: ["mylib"],
890 }
891 `)
892
893 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
894
895 // Ensure that the include path of the header lib is exported to 'otherlib'
896 ensureContains(t, cFlags, "-Imy_include")
897}
Alex Light9670d332019-01-29 18:07:33 -0800898
899func TestApexWithTarget(t *testing.T) {
900 ctx := testApex(t, `
901 apex {
902 name: "myapex",
903 key: "myapex.key",
904 multilib: {
905 first: {
906 native_shared_libs: ["mylib_common"],
907 }
908 },
909 target: {
910 android: {
911 multilib: {
912 first: {
913 native_shared_libs: ["mylib"],
914 }
915 }
916 },
917 host: {
918 multilib: {
919 first: {
920 native_shared_libs: ["mylib2"],
921 }
922 }
923 }
924 }
925 }
926
927 apex_key {
928 name: "myapex.key",
929 public_key: "testkey.avbpubkey",
930 private_key: "testkey.pem",
931 }
932
933 cc_library {
934 name: "mylib",
935 srcs: ["mylib.cpp"],
936 system_shared_libs: [],
937 stl: "none",
938 }
939
940 cc_library {
941 name: "mylib_common",
942 srcs: ["mylib.cpp"],
943 system_shared_libs: [],
944 stl: "none",
945 compile_multilib: "first",
946 }
947
948 cc_library {
949 name: "mylib2",
950 srcs: ["mylib.cpp"],
951 system_shared_libs: [],
952 stl: "none",
953 compile_multilib: "first",
954 }
955 `)
956
957 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
958 copyCmds := apexRule.Args["copy_commands"]
959
960 // Ensure that main rule creates an output
961 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
962
963 // Ensure that apex variant is created for the direct dep
964 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
965 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
966 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
967
968 // Ensure that both direct and indirect deps are copied into apex
969 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
970 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
971 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
972
973 // Ensure that the platform variant ends with _core_shared
974 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
975 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
976 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
977}