blob: 6163d0f4eca74d50c909bf5d6d70b248e418723f [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))
Jiyong Park30ca9372019-02-07 16:27:23 +090036 ctx.RegisterModuleType("apex_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
37 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Park25fc6a92018-11-18 18:02:45 +090038
39 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
40 ctx.TopDown("apex_deps", apexDepsMutator)
41 ctx.BottomUp("apex", apexMutator)
42 })
43
44 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
45 ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(cc.LibrarySharedFactory))
Jiyong Park7e636d02019-01-28 16:16:54 +090046 ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(cc.LibraryHeaderFactory))
Jiyong Park16e91a02018-12-20 18:18:08 +090047 ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(cc.BinaryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090048 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
Jiyong Parkda6eb592018-12-19 17:12:36 +090049 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090050 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
Jiyong Park7c2ee712018-12-07 00:42:25 +090051 ctx.RegisterModuleType("prebuilt_etc", android.ModuleFactoryAdaptor(android.PrebuiltEtcFactory))
Jiyong Park04480cf2019-02-06 00:16:29 +090052 ctx.RegisterModuleType("sh_binary", android.ModuleFactoryAdaptor(android.ShBinaryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090053 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkda6eb592018-12-19 17:12:36 +090054 ctx.BottomUp("image", cc.ImageMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090055 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Jiyong Parkda6eb592018-12-19 17:12:36 +090056 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090057 ctx.BottomUp("version", cc.VersionMutator).Parallel()
58 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
59 })
60
61 ctx.Register()
62
63 bp = bp + `
64 toolchain_library {
65 name: "libcompiler_rt-extras",
66 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090067 vendor_available: true,
68 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090069 }
70
71 toolchain_library {
72 name: "libatomic",
73 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090074 vendor_available: true,
75 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090076 }
77
78 toolchain_library {
79 name: "libgcc",
80 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090081 vendor_available: true,
82 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090083 }
84
85 toolchain_library {
86 name: "libclang_rt.builtins-aarch64-android",
87 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090088 vendor_available: true,
89 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090090 }
91
92 toolchain_library {
93 name: "libclang_rt.builtins-arm-android",
94 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090095 vendor_available: true,
96 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090097 }
98
99 cc_object {
100 name: "crtbegin_so",
101 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900102 vendor_available: true,
103 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900104 }
105
106 cc_object {
107 name: "crtend_so",
108 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900109 vendor_available: true,
110 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900111 }
112
Alex Light3d673592019-01-18 14:37:31 -0800113 cc_object {
114 name: "crtbegin_static",
115 stl: "none",
116 }
117
118 cc_object {
119 name: "crtend_android",
120 stl: "none",
121 }
122
Jiyong Parkda6eb592018-12-19 17:12:36 +0900123 llndk_library {
124 name: "libc",
125 symbol_file: "",
126 }
127
128 llndk_library {
129 name: "libm",
130 symbol_file: "",
131 }
132
133 llndk_library {
134 name: "libdl",
135 symbol_file: "",
136 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900137 `
138
139 ctx.MockFileSystem(map[string][]byte{
Jiyong Park58e364a2019-01-19 19:24:06 +0900140 "Android.bp": []byte(bp),
141 "build/target/product/security": nil,
142 "apex_manifest.json": nil,
143 "system/sepolicy/apex/myapex-file_contexts": nil,
144 "system/sepolicy/apex/otherapex-file_contexts": nil,
145 "mylib.cpp": nil,
146 "myprebuilt": nil,
Jiyong Park7e636d02019-01-28 16:16:54 +0900147 "my_include": nil,
Jiyong Park58e364a2019-01-19 19:24:06 +0900148 "vendor/foo/devkeys/test.x509.pem": nil,
149 "vendor/foo/devkeys/test.pk8": nil,
150 "vendor/foo/devkeys/testkey.avbpubkey": nil,
151 "vendor/foo/devkeys/testkey.pem": nil,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900152 })
153 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
154 android.FailIfErrored(t, errs)
155 _, errs = ctx.PrepareBuildActions(config)
156 android.FailIfErrored(t, errs)
157
158 return ctx
159}
160
161func setup(t *testing.T) (config android.Config, buildDir string) {
162 buildDir, err := ioutil.TempDir("", "soong_apex_test")
163 if err != nil {
164 t.Fatal(err)
165 }
166
167 config = android.TestArchConfig(buildDir, nil)
Jiyong Parkda6eb592018-12-19 17:12:36 +0900168 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
Jiyong Park9335a262018-12-24 11:31:58 +0900169 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900170 return
171}
172
173func teardown(buildDir string) {
174 os.RemoveAll(buildDir)
175}
176
177// ensure that 'result' contains 'expected'
178func ensureContains(t *testing.T, result string, expected string) {
179 if !strings.Contains(result, expected) {
180 t.Errorf("%q is not found in %q", expected, result)
181 }
182}
183
184// ensures that 'result' does not contain 'notExpected'
185func ensureNotContains(t *testing.T, result string, notExpected string) {
186 if strings.Contains(result, notExpected) {
187 t.Errorf("%q is found in %q", notExpected, result)
188 }
189}
190
191func ensureListContains(t *testing.T, result []string, expected string) {
192 if !android.InList(expected, result) {
193 t.Errorf("%q is not found in %v", expected, result)
194 }
195}
196
197func ensureListNotContains(t *testing.T, result []string, notExpected string) {
198 if android.InList(notExpected, result) {
199 t.Errorf("%q is found in %v", notExpected, result)
200 }
201}
202
203// Minimal test
204func TestBasicApex(t *testing.T) {
205 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900206 apex_defaults {
207 name: "myapex-defaults",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900208 key: "myapex.key",
209 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800210 multilib: {
211 both: {
212 binaries: ["foo",],
213 }
214 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900215 }
216
Jiyong Park30ca9372019-02-07 16:27:23 +0900217 apex {
218 name: "myapex",
219 defaults: ["myapex-defaults"],
220 }
221
Jiyong Park25fc6a92018-11-18 18:02:45 +0900222 apex_key {
223 name: "myapex.key",
224 public_key: "testkey.avbpubkey",
225 private_key: "testkey.pem",
226 }
227
228 cc_library {
229 name: "mylib",
230 srcs: ["mylib.cpp"],
231 shared_libs: ["mylib2"],
232 system_shared_libs: [],
233 stl: "none",
234 }
235
Alex Light3d673592019-01-18 14:37:31 -0800236 cc_binary {
237 name: "foo",
238 srcs: ["mylib.cpp"],
239 compile_multilib: "both",
240 multilib: {
241 lib32: {
242 suffix: "32",
243 },
244 lib64: {
245 suffix: "64",
246 },
247 },
248 symlinks: ["foo_link_"],
249 symlink_preferred_arch: true,
250 system_shared_libs: [],
251 static_executable: true,
252 stl: "none",
253 }
254
Jiyong Park25fc6a92018-11-18 18:02:45 +0900255 cc_library {
256 name: "mylib2",
257 srcs: ["mylib.cpp"],
258 system_shared_libs: [],
259 stl: "none",
260 }
261 `)
262
263 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
264 copyCmds := apexRule.Args["copy_commands"]
265
266 // Ensure that main rule creates an output
267 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
268
269 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900270 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900271
272 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900273 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900274
275 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800276 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
277 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Logan Chien3aeedc92018-12-26 15:32:21 +0800278
279 // Ensure that the platform variant ends with _core_shared
280 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
281 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Alex Light3d673592019-01-18 14:37:31 -0800282
283 // Ensure that all symlinks are present.
284 found_foo_link_64 := false
285 found_foo := false
286 for _, cmd := range strings.Split(copyCmds, " && ") {
287 if strings.HasPrefix(cmd, "ln -s foo64") {
288 if strings.HasSuffix(cmd, "bin/foo") {
289 found_foo = true
290 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
291 found_foo_link_64 = true
292 }
293 }
294 }
295 good := found_foo && found_foo_link_64
296 if !good {
297 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
298 }
Alex Light5098a612018-11-29 17:12:15 -0800299}
300
301func TestBasicZipApex(t *testing.T) {
302 ctx := testApex(t, `
303 apex {
304 name: "myapex",
305 key: "myapex.key",
306 payload_type: "zip",
307 native_shared_libs: ["mylib"],
308 }
309
310 apex_key {
311 name: "myapex.key",
312 public_key: "testkey.avbpubkey",
313 private_key: "testkey.pem",
314 }
315
316 cc_library {
317 name: "mylib",
318 srcs: ["mylib.cpp"],
319 shared_libs: ["mylib2"],
320 system_shared_libs: [],
321 stl: "none",
322 }
323
324 cc_library {
325 name: "mylib2",
326 srcs: ["mylib.cpp"],
327 system_shared_libs: [],
328 stl: "none",
329 }
330 `)
331
332 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("zipApexRule")
333 copyCmds := zipApexRule.Args["copy_commands"]
334
335 // Ensure that main rule creates an output
336 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
337
338 // Ensure that APEX variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900339 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800340
341 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900342 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800343
344 // Ensure that both direct and indirect deps are copied into apex
345 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
346 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900347}
348
349func TestApexWithStubs(t *testing.T) {
350 ctx := testApex(t, `
351 apex {
352 name: "myapex",
353 key: "myapex.key",
354 native_shared_libs: ["mylib", "mylib3"],
355 }
356
357 apex_key {
358 name: "myapex.key",
359 public_key: "testkey.avbpubkey",
360 private_key: "testkey.pem",
361 }
362
363 cc_library {
364 name: "mylib",
365 srcs: ["mylib.cpp"],
366 shared_libs: ["mylib2", "mylib3"],
367 system_shared_libs: [],
368 stl: "none",
369 }
370
371 cc_library {
372 name: "mylib2",
373 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900374 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900375 system_shared_libs: [],
376 stl: "none",
377 stubs: {
378 versions: ["1", "2", "3"],
379 },
380 }
381
382 cc_library {
383 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900384 srcs: ["mylib.cpp"],
385 shared_libs: ["mylib4"],
386 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900387 stl: "none",
388 stubs: {
389 versions: ["10", "11", "12"],
390 },
391 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900392
393 cc_library {
394 name: "mylib4",
395 srcs: ["mylib.cpp"],
396 system_shared_libs: [],
397 stl: "none",
398 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900399 `)
400
401 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
402 copyCmds := apexRule.Args["copy_commands"]
403
404 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800405 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900406
407 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800408 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900409
410 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800411 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900412
Jiyong Parkda6eb592018-12-19 17:12:36 +0900413 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900414
415 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900416 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900417 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900418 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900419
420 // 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 +0900421 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900422 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900423 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900424
425 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900426 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900427 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900428
429 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900430 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 +0900431}
432
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900433func TestApexWithExplicitStubsDependency(t *testing.T) {
434 ctx := testApex(t, `
435 apex {
436 name: "myapex",
437 key: "myapex.key",
438 native_shared_libs: ["mylib"],
439 }
440
441 apex_key {
442 name: "myapex.key",
443 public_key: "testkey.avbpubkey",
444 private_key: "testkey.pem",
445 }
446
447 cc_library {
448 name: "mylib",
449 srcs: ["mylib.cpp"],
450 shared_libs: ["libfoo#10"],
451 system_shared_libs: [],
452 stl: "none",
453 }
454
455 cc_library {
456 name: "libfoo",
457 srcs: ["mylib.cpp"],
458 shared_libs: ["libbar"],
459 system_shared_libs: [],
460 stl: "none",
461 stubs: {
462 versions: ["10", "20", "30"],
463 },
464 }
465
466 cc_library {
467 name: "libbar",
468 srcs: ["mylib.cpp"],
469 system_shared_libs: [],
470 stl: "none",
471 }
472
473 `)
474
475 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
476 copyCmds := apexRule.Args["copy_commands"]
477
478 // Ensure that direct non-stubs dep is always included
479 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
480
481 // Ensure that indirect stubs dep is not included
482 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
483
484 // Ensure that dependency of stubs is not included
485 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
486
Jiyong Parkda6eb592018-12-19 17:12:36 +0900487 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900488
489 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900490 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900491 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900492 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900493
Jiyong Parkda6eb592018-12-19 17:12:36 +0900494 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900495
496 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
497 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
498}
499
Jiyong Park25fc6a92018-11-18 18:02:45 +0900500func TestApexWithSystemLibsStubs(t *testing.T) {
501 ctx := testApex(t, `
502 apex {
503 name: "myapex",
504 key: "myapex.key",
505 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
506 }
507
508 apex_key {
509 name: "myapex.key",
510 public_key: "testkey.avbpubkey",
511 private_key: "testkey.pem",
512 }
513
514 cc_library {
515 name: "mylib",
516 srcs: ["mylib.cpp"],
517 shared_libs: ["libdl#27"],
518 stl: "none",
519 }
520
521 cc_library_shared {
522 name: "mylib_shared",
523 srcs: ["mylib.cpp"],
524 shared_libs: ["libdl#27"],
525 stl: "none",
526 }
527
528 cc_library {
529 name: "libc",
530 no_libgcc: true,
531 nocrt: true,
532 system_shared_libs: [],
533 stl: "none",
534 stubs: {
535 versions: ["27", "28", "29"],
536 },
537 }
538
539 cc_library {
540 name: "libm",
541 no_libgcc: true,
542 nocrt: true,
543 system_shared_libs: [],
544 stl: "none",
545 stubs: {
546 versions: ["27", "28", "29"],
547 },
548 }
549
550 cc_library {
551 name: "libdl",
552 no_libgcc: true,
553 nocrt: true,
554 system_shared_libs: [],
555 stl: "none",
556 stubs: {
557 versions: ["27", "28", "29"],
558 },
559 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900560
561 cc_library {
562 name: "libBootstrap",
563 srcs: ["mylib.cpp"],
564 stl: "none",
565 bootstrap: true,
566 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900567 `)
568
569 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
570 copyCmds := apexRule.Args["copy_commands"]
571
572 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800573 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900574 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
575 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900576
577 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900578 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900579
Jiyong Parkda6eb592018-12-19 17:12:36 +0900580 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
581 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
582 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900583
584 // For dependency to libc
585 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900586 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900587 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900588 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900589 // ... Cflags from stub is correctly exported to mylib
590 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
591 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
592
593 // For dependency to libm
594 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900595 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900596 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900597 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900598 // ... and is not compiling with the stub
599 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
600 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
601
602 // For dependency to libdl
603 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900604 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900605 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900606 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
607 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900608 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900609 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900610 // ... Cflags from stub is correctly exported to mylib
611 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
612 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900613
614 // Ensure that libBootstrap is depending on the platform variant of bionic libs
615 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
616 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
617 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
618 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900619}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900620
621func TestFilesInSubDir(t *testing.T) {
622 ctx := testApex(t, `
623 apex {
624 name: "myapex",
625 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900626 native_shared_libs: ["mylib"],
627 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900628 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900629 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900630 }
631
632 apex_key {
633 name: "myapex.key",
634 public_key: "testkey.avbpubkey",
635 private_key: "testkey.pem",
636 }
637
638 prebuilt_etc {
639 name: "myetc",
640 src: "myprebuilt",
641 sub_dir: "foo/bar",
642 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900643
644 cc_library {
645 name: "mylib",
646 srcs: ["mylib.cpp"],
647 relative_install_path: "foo/bar",
648 system_shared_libs: [],
649 stl: "none",
650 }
651
652 cc_binary {
653 name: "mybin",
654 srcs: ["mylib.cpp"],
655 relative_install_path: "foo/bar",
656 system_shared_libs: [],
657 static_executable: true,
658 stl: "none",
659 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900660 `)
661
662 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
663 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
664
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900665 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +0900666 ensureListContains(t, dirs, "etc")
667 ensureListContains(t, dirs, "etc/foo")
668 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900669 ensureListContains(t, dirs, "lib64")
670 ensureListContains(t, dirs, "lib64/foo")
671 ensureListContains(t, dirs, "lib64/foo/bar")
672 ensureListContains(t, dirs, "lib")
673 ensureListContains(t, dirs, "lib/foo")
674 ensureListContains(t, dirs, "lib/foo/bar")
675
676 // TODO(b/123721777) respect relative path for binaries
677 // ensureListContains(t, dirs, "bin")
678 // ensureListContains(t, dirs, "bin/foo")
679 // ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +0900680}
Jiyong Parkda6eb592018-12-19 17:12:36 +0900681
682func TestUseVendor(t *testing.T) {
683 ctx := testApex(t, `
684 apex {
685 name: "myapex",
686 key: "myapex.key",
687 native_shared_libs: ["mylib"],
688 use_vendor: true,
689 }
690
691 apex_key {
692 name: "myapex.key",
693 public_key: "testkey.avbpubkey",
694 private_key: "testkey.pem",
695 }
696
697 cc_library {
698 name: "mylib",
699 srcs: ["mylib.cpp"],
700 shared_libs: ["mylib2"],
701 system_shared_libs: [],
702 vendor_available: true,
703 stl: "none",
704 }
705
706 cc_library {
707 name: "mylib2",
708 srcs: ["mylib.cpp"],
709 system_shared_libs: [],
710 vendor_available: true,
711 stl: "none",
712 }
713 `)
714
715 inputsList := []string{}
716 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
717 for _, implicit := range i.Implicits {
718 inputsList = append(inputsList, implicit.String())
719 }
720 }
721 inputsString := strings.Join(inputsList, " ")
722
723 // ensure that the apex includes vendor variants of the direct and indirect deps
724 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib.so")
725 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib2.so")
726
727 // ensure that the apex does not include core variants
728 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
729 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
730}
Jiyong Park16e91a02018-12-20 18:18:08 +0900731
732func TestStaticLinking(t *testing.T) {
733 ctx := testApex(t, `
734 apex {
735 name: "myapex",
736 key: "myapex.key",
737 native_shared_libs: ["mylib"],
738 }
739
740 apex_key {
741 name: "myapex.key",
742 public_key: "testkey.avbpubkey",
743 private_key: "testkey.pem",
744 }
745
746 cc_library {
747 name: "mylib",
748 srcs: ["mylib.cpp"],
749 system_shared_libs: [],
750 stl: "none",
751 stubs: {
752 versions: ["1", "2", "3"],
753 },
754 }
755
756 cc_binary {
757 name: "not_in_apex",
758 srcs: ["mylib.cpp"],
759 static_libs: ["mylib"],
760 static_executable: true,
761 system_shared_libs: [],
762 stl: "none",
763 }
Jiyong Park16e91a02018-12-20 18:18:08 +0900764 `)
765
766 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
767
768 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +0800769 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +0900770}
Jiyong Park9335a262018-12-24 11:31:58 +0900771
772func TestKeys(t *testing.T) {
773 ctx := testApex(t, `
774 apex {
775 name: "myapex",
776 key: "myapex.key",
777 native_shared_libs: ["mylib"],
778 }
779
780 cc_library {
781 name: "mylib",
782 srcs: ["mylib.cpp"],
783 system_shared_libs: [],
784 stl: "none",
785 }
786
787 apex_key {
788 name: "myapex.key",
789 public_key: "testkey.avbpubkey",
790 private_key: "testkey.pem",
791 }
792
793 `)
794
795 // check the APEX keys
796 keys := ctx.ModuleForTests("myapex.key", "").Module().(*apexKey)
797
798 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
799 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
800 "vendor/foo/devkeys/testkey.avbpubkey")
801 }
802 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
803 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
804 "vendor/foo/devkeys/testkey.pem")
805 }
806
807 // check the APK certs
808 certs := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk").Args["certificates"]
809 if certs != "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8" {
810 t.Errorf("cert and private key %q are not %q", certs,
811 "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8")
812 }
813}
Jiyong Park58e364a2019-01-19 19:24:06 +0900814
815func TestMacro(t *testing.T) {
816 ctx := testApex(t, `
817 apex {
818 name: "myapex",
819 key: "myapex.key",
820 native_shared_libs: ["mylib"],
821 }
822
823 apex {
824 name: "otherapex",
825 key: "myapex.key",
826 native_shared_libs: ["mylib"],
827 }
828
829 apex_key {
830 name: "myapex.key",
831 public_key: "testkey.avbpubkey",
832 private_key: "testkey.pem",
833 }
834
835 cc_library {
836 name: "mylib",
837 srcs: ["mylib.cpp"],
838 system_shared_libs: [],
839 stl: "none",
840 }
841 `)
842
843 // non-APEX variant does not have __ANDROID__APEX__ defined
844 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
845 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
846 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
847
848 // APEX variant has __ANDROID_APEX__=<apexname> defined
849 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
850 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
851 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
852
853 // APEX variant has __ANDROID_APEX__=<apexname> defined
854 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
855 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
856 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
857}
Jiyong Park7e636d02019-01-28 16:16:54 +0900858
859func TestHeaderLibsDependency(t *testing.T) {
860 ctx := testApex(t, `
861 apex {
862 name: "myapex",
863 key: "myapex.key",
864 native_shared_libs: ["mylib"],
865 }
866
867 apex_key {
868 name: "myapex.key",
869 public_key: "testkey.avbpubkey",
870 private_key: "testkey.pem",
871 }
872
873 cc_library_headers {
874 name: "mylib_headers",
875 export_include_dirs: ["my_include"],
876 system_shared_libs: [],
877 stl: "none",
878 }
879
880 cc_library {
881 name: "mylib",
882 srcs: ["mylib.cpp"],
883 system_shared_libs: [],
884 stl: "none",
885 header_libs: ["mylib_headers"],
886 export_header_lib_headers: ["mylib_headers"],
887 stubs: {
888 versions: ["1", "2", "3"],
889 },
890 }
891
892 cc_library {
893 name: "otherlib",
894 srcs: ["mylib.cpp"],
895 system_shared_libs: [],
896 stl: "none",
897 shared_libs: ["mylib"],
898 }
899 `)
900
901 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
902
903 // Ensure that the include path of the header lib is exported to 'otherlib'
904 ensureContains(t, cFlags, "-Imy_include")
905}
Alex Light9670d332019-01-29 18:07:33 -0800906
907func TestApexWithTarget(t *testing.T) {
908 ctx := testApex(t, `
909 apex {
910 name: "myapex",
911 key: "myapex.key",
912 multilib: {
913 first: {
914 native_shared_libs: ["mylib_common"],
915 }
916 },
917 target: {
918 android: {
919 multilib: {
920 first: {
921 native_shared_libs: ["mylib"],
922 }
923 }
924 },
925 host: {
926 multilib: {
927 first: {
928 native_shared_libs: ["mylib2"],
929 }
930 }
931 }
932 }
933 }
934
935 apex_key {
936 name: "myapex.key",
937 public_key: "testkey.avbpubkey",
938 private_key: "testkey.pem",
939 }
940
941 cc_library {
942 name: "mylib",
943 srcs: ["mylib.cpp"],
944 system_shared_libs: [],
945 stl: "none",
946 }
947
948 cc_library {
949 name: "mylib_common",
950 srcs: ["mylib.cpp"],
951 system_shared_libs: [],
952 stl: "none",
953 compile_multilib: "first",
954 }
955
956 cc_library {
957 name: "mylib2",
958 srcs: ["mylib.cpp"],
959 system_shared_libs: [],
960 stl: "none",
961 compile_multilib: "first",
962 }
963 `)
964
965 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
966 copyCmds := apexRule.Args["copy_commands"]
967
968 // Ensure that main rule creates an output
969 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
970
971 // Ensure that apex variant is created for the direct dep
972 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
973 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
974 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
975
976 // Ensure that both direct and indirect deps are copied into apex
977 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
978 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
979 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
980
981 // Ensure that the platform variant ends with _core_shared
982 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
983 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
984 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
985}
Jiyong Park04480cf2019-02-06 00:16:29 +0900986
987func TestApexWithShBinary(t *testing.T) {
988 ctx := testApex(t, `
989 apex {
990 name: "myapex",
991 key: "myapex.key",
992 binaries: ["myscript"],
993 }
994
995 apex_key {
996 name: "myapex.key",
997 public_key: "testkey.avbpubkey",
998 private_key: "testkey.pem",
999 }
1000
1001 sh_binary {
1002 name: "myscript",
1003 src: "mylib.cpp",
1004 filename: "myscript.sh",
1005 sub_dir: "script",
1006 }
1007 `)
1008
1009 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1010 copyCmds := apexRule.Args["copy_commands"]
1011
1012 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
1013}