blob: 699ef6d1dddeca2b1c8a483666d694371e71abb5 [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 Park16e91a02018-12-20 18:18:08 +090044 ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(cc.BinaryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090045 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
Jiyong Parkda6eb592018-12-19 17:12:36 +090046 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090047 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
Jiyong Park7c2ee712018-12-07 00:42:25 +090048 ctx.RegisterModuleType("prebuilt_etc", android.ModuleFactoryAdaptor(android.PrebuiltEtcFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090049 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkda6eb592018-12-19 17:12:36 +090050 ctx.BottomUp("image", cc.ImageMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090051 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Jiyong Parkda6eb592018-12-19 17:12:36 +090052 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090053 ctx.BottomUp("version", cc.VersionMutator).Parallel()
54 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
55 })
56
57 ctx.Register()
58
59 bp = bp + `
60 toolchain_library {
61 name: "libcompiler_rt-extras",
62 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090063 vendor_available: true,
64 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090065 }
66
67 toolchain_library {
68 name: "libatomic",
69 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090070 vendor_available: true,
71 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090072 }
73
74 toolchain_library {
75 name: "libgcc",
76 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090077 vendor_available: true,
78 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090079 }
80
81 toolchain_library {
82 name: "libclang_rt.builtins-aarch64-android",
83 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090084 vendor_available: true,
85 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090086 }
87
88 toolchain_library {
89 name: "libclang_rt.builtins-arm-android",
90 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090091 vendor_available: true,
92 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090093 }
94
95 cc_object {
96 name: "crtbegin_so",
97 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +090098 vendor_available: true,
99 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900100 }
101
102 cc_object {
103 name: "crtend_so",
104 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900105 vendor_available: true,
106 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900107 }
108
Jiyong Parkda6eb592018-12-19 17:12:36 +0900109 llndk_library {
110 name: "libc",
111 symbol_file: "",
112 }
113
114 llndk_library {
115 name: "libm",
116 symbol_file: "",
117 }
118
119 llndk_library {
120 name: "libdl",
121 symbol_file: "",
122 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900123 `
124
125 ctx.MockFileSystem(map[string][]byte{
Jiyong Park58e364a2019-01-19 19:24:06 +0900126 "Android.bp": []byte(bp),
127 "build/target/product/security": nil,
128 "apex_manifest.json": nil,
129 "system/sepolicy/apex/myapex-file_contexts": nil,
130 "system/sepolicy/apex/otherapex-file_contexts": nil,
131 "mylib.cpp": nil,
132 "myprebuilt": nil,
133 "vendor/foo/devkeys/test.x509.pem": nil,
134 "vendor/foo/devkeys/test.pk8": nil,
135 "vendor/foo/devkeys/testkey.avbpubkey": nil,
136 "vendor/foo/devkeys/testkey.pem": nil,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900137 })
138 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
139 android.FailIfErrored(t, errs)
140 _, errs = ctx.PrepareBuildActions(config)
141 android.FailIfErrored(t, errs)
142
143 return ctx
144}
145
146func setup(t *testing.T) (config android.Config, buildDir string) {
147 buildDir, err := ioutil.TempDir("", "soong_apex_test")
148 if err != nil {
149 t.Fatal(err)
150 }
151
152 config = android.TestArchConfig(buildDir, nil)
Jiyong Parkda6eb592018-12-19 17:12:36 +0900153 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
Jiyong Park9335a262018-12-24 11:31:58 +0900154 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900155 return
156}
157
158func teardown(buildDir string) {
159 os.RemoveAll(buildDir)
160}
161
162// ensure that 'result' contains 'expected'
163func ensureContains(t *testing.T, result string, expected string) {
164 if !strings.Contains(result, expected) {
165 t.Errorf("%q is not found in %q", expected, result)
166 }
167}
168
169// ensures that 'result' does not contain 'notExpected'
170func ensureNotContains(t *testing.T, result string, notExpected string) {
171 if strings.Contains(result, notExpected) {
172 t.Errorf("%q is found in %q", notExpected, result)
173 }
174}
175
176func ensureListContains(t *testing.T, result []string, expected string) {
177 if !android.InList(expected, result) {
178 t.Errorf("%q is not found in %v", expected, result)
179 }
180}
181
182func ensureListNotContains(t *testing.T, result []string, notExpected string) {
183 if android.InList(notExpected, result) {
184 t.Errorf("%q is found in %v", notExpected, result)
185 }
186}
187
188// Minimal test
189func TestBasicApex(t *testing.T) {
190 ctx := testApex(t, `
191 apex {
192 name: "myapex",
193 key: "myapex.key",
194 native_shared_libs: ["mylib"],
195 }
196
197 apex_key {
198 name: "myapex.key",
199 public_key: "testkey.avbpubkey",
200 private_key: "testkey.pem",
201 }
202
203 cc_library {
204 name: "mylib",
205 srcs: ["mylib.cpp"],
206 shared_libs: ["mylib2"],
207 system_shared_libs: [],
208 stl: "none",
209 }
210
211 cc_library {
212 name: "mylib2",
213 srcs: ["mylib.cpp"],
214 system_shared_libs: [],
215 stl: "none",
216 }
217 `)
218
219 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
220 copyCmds := apexRule.Args["copy_commands"]
221
222 // Ensure that main rule creates an output
223 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
224
225 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900226 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900227
228 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900229 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900230
231 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800232 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
233 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Logan Chien3aeedc92018-12-26 15:32:21 +0800234
235 // Ensure that the platform variant ends with _core_shared
236 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
237 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Alex Light5098a612018-11-29 17:12:15 -0800238}
239
240func TestBasicZipApex(t *testing.T) {
241 ctx := testApex(t, `
242 apex {
243 name: "myapex",
244 key: "myapex.key",
245 payload_type: "zip",
246 native_shared_libs: ["mylib"],
247 }
248
249 apex_key {
250 name: "myapex.key",
251 public_key: "testkey.avbpubkey",
252 private_key: "testkey.pem",
253 }
254
255 cc_library {
256 name: "mylib",
257 srcs: ["mylib.cpp"],
258 shared_libs: ["mylib2"],
259 system_shared_libs: [],
260 stl: "none",
261 }
262
263 cc_library {
264 name: "mylib2",
265 srcs: ["mylib.cpp"],
266 system_shared_libs: [],
267 stl: "none",
268 }
269 `)
270
271 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("zipApexRule")
272 copyCmds := zipApexRule.Args["copy_commands"]
273
274 // Ensure that main rule creates an output
275 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
276
277 // Ensure that APEX variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900278 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800279
280 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900281 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800282
283 // Ensure that both direct and indirect deps are copied into apex
284 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
285 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900286}
287
288func TestApexWithStubs(t *testing.T) {
289 ctx := testApex(t, `
290 apex {
291 name: "myapex",
292 key: "myapex.key",
293 native_shared_libs: ["mylib", "mylib3"],
294 }
295
296 apex_key {
297 name: "myapex.key",
298 public_key: "testkey.avbpubkey",
299 private_key: "testkey.pem",
300 }
301
302 cc_library {
303 name: "mylib",
304 srcs: ["mylib.cpp"],
305 shared_libs: ["mylib2", "mylib3"],
306 system_shared_libs: [],
307 stl: "none",
308 }
309
310 cc_library {
311 name: "mylib2",
312 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900313 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900314 system_shared_libs: [],
315 stl: "none",
316 stubs: {
317 versions: ["1", "2", "3"],
318 },
319 }
320
321 cc_library {
322 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900323 srcs: ["mylib.cpp"],
324 shared_libs: ["mylib4"],
325 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900326 stl: "none",
327 stubs: {
328 versions: ["10", "11", "12"],
329 },
330 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900331
332 cc_library {
333 name: "mylib4",
334 srcs: ["mylib.cpp"],
335 system_shared_libs: [],
336 stl: "none",
337 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900338 `)
339
340 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
341 copyCmds := apexRule.Args["copy_commands"]
342
343 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800344 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900345
346 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800347 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900348
349 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800350 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900351
Jiyong Parkda6eb592018-12-19 17:12:36 +0900352 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353
354 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900355 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900356 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900357 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900358
359 // 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 +0900360 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900361 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900362 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900363
364 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900365 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900366 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900367
368 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900369 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 +0900370}
371
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900372func TestApexWithExplicitStubsDependency(t *testing.T) {
373 ctx := testApex(t, `
374 apex {
375 name: "myapex",
376 key: "myapex.key",
377 native_shared_libs: ["mylib"],
378 }
379
380 apex_key {
381 name: "myapex.key",
382 public_key: "testkey.avbpubkey",
383 private_key: "testkey.pem",
384 }
385
386 cc_library {
387 name: "mylib",
388 srcs: ["mylib.cpp"],
389 shared_libs: ["libfoo#10"],
390 system_shared_libs: [],
391 stl: "none",
392 }
393
394 cc_library {
395 name: "libfoo",
396 srcs: ["mylib.cpp"],
397 shared_libs: ["libbar"],
398 system_shared_libs: [],
399 stl: "none",
400 stubs: {
401 versions: ["10", "20", "30"],
402 },
403 }
404
405 cc_library {
406 name: "libbar",
407 srcs: ["mylib.cpp"],
408 system_shared_libs: [],
409 stl: "none",
410 }
411
412 `)
413
414 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
415 copyCmds := apexRule.Args["copy_commands"]
416
417 // Ensure that direct non-stubs dep is always included
418 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
419
420 // Ensure that indirect stubs dep is not included
421 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
422
423 // Ensure that dependency of stubs is not included
424 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
425
Jiyong Parkda6eb592018-12-19 17:12:36 +0900426 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900427
428 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900429 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900430 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900431 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900432
Jiyong Parkda6eb592018-12-19 17:12:36 +0900433 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900434
435 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
436 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
437}
438
Jiyong Park25fc6a92018-11-18 18:02:45 +0900439func TestApexWithSystemLibsStubs(t *testing.T) {
440 ctx := testApex(t, `
441 apex {
442 name: "myapex",
443 key: "myapex.key",
444 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
445 }
446
447 apex_key {
448 name: "myapex.key",
449 public_key: "testkey.avbpubkey",
450 private_key: "testkey.pem",
451 }
452
453 cc_library {
454 name: "mylib",
455 srcs: ["mylib.cpp"],
456 shared_libs: ["libdl#27"],
457 stl: "none",
458 }
459
460 cc_library_shared {
461 name: "mylib_shared",
462 srcs: ["mylib.cpp"],
463 shared_libs: ["libdl#27"],
464 stl: "none",
465 }
466
467 cc_library {
468 name: "libc",
469 no_libgcc: true,
470 nocrt: true,
471 system_shared_libs: [],
472 stl: "none",
473 stubs: {
474 versions: ["27", "28", "29"],
475 },
476 }
477
478 cc_library {
479 name: "libm",
480 no_libgcc: true,
481 nocrt: true,
482 system_shared_libs: [],
483 stl: "none",
484 stubs: {
485 versions: ["27", "28", "29"],
486 },
487 }
488
489 cc_library {
490 name: "libdl",
491 no_libgcc: true,
492 nocrt: true,
493 system_shared_libs: [],
494 stl: "none",
495 stubs: {
496 versions: ["27", "28", "29"],
497 },
498 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900499
500 cc_library {
501 name: "libBootstrap",
502 srcs: ["mylib.cpp"],
503 stl: "none",
504 bootstrap: true,
505 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900506 `)
507
508 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
509 copyCmds := apexRule.Args["copy_commands"]
510
511 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800512 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900513 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
514 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900515
516 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900517 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900518
Jiyong Parkda6eb592018-12-19 17:12:36 +0900519 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
520 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
521 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900522
523 // For dependency to libc
524 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900525 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900526 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900527 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900528 // ... Cflags from stub is correctly exported to mylib
529 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
530 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
531
532 // For dependency to libm
533 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900534 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900535 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900536 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900537 // ... and is not compiling with the stub
538 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
539 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
540
541 // For dependency to libdl
542 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900543 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900544 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900545 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
546 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900547 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900548 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900549 // ... Cflags from stub is correctly exported to mylib
550 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
551 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900552
553 // Ensure that libBootstrap is depending on the platform variant of bionic libs
554 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
555 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
556 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
557 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900558}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900559
560func TestFilesInSubDir(t *testing.T) {
561 ctx := testApex(t, `
562 apex {
563 name: "myapex",
564 key: "myapex.key",
565 prebuilts: ["myetc"],
566 }
567
568 apex_key {
569 name: "myapex.key",
570 public_key: "testkey.avbpubkey",
571 private_key: "testkey.pem",
572 }
573
574 prebuilt_etc {
575 name: "myetc",
576 src: "myprebuilt",
577 sub_dir: "foo/bar",
578 }
579 `)
580
581 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
582 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
583
584 // Ensure that etc, etc/foo, and etc/foo/bar are all listed
585 ensureListContains(t, dirs, "etc")
586 ensureListContains(t, dirs, "etc/foo")
587 ensureListContains(t, dirs, "etc/foo/bar")
588}
Jiyong Parkda6eb592018-12-19 17:12:36 +0900589
590func TestUseVendor(t *testing.T) {
591 ctx := testApex(t, `
592 apex {
593 name: "myapex",
594 key: "myapex.key",
595 native_shared_libs: ["mylib"],
596 use_vendor: true,
597 }
598
599 apex_key {
600 name: "myapex.key",
601 public_key: "testkey.avbpubkey",
602 private_key: "testkey.pem",
603 }
604
605 cc_library {
606 name: "mylib",
607 srcs: ["mylib.cpp"],
608 shared_libs: ["mylib2"],
609 system_shared_libs: [],
610 vendor_available: true,
611 stl: "none",
612 }
613
614 cc_library {
615 name: "mylib2",
616 srcs: ["mylib.cpp"],
617 system_shared_libs: [],
618 vendor_available: true,
619 stl: "none",
620 }
621 `)
622
623 inputsList := []string{}
624 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
625 for _, implicit := range i.Implicits {
626 inputsList = append(inputsList, implicit.String())
627 }
628 }
629 inputsString := strings.Join(inputsList, " ")
630
631 // ensure that the apex includes vendor variants of the direct and indirect deps
632 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib.so")
633 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib2.so")
634
635 // ensure that the apex does not include core variants
636 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
637 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
638}
Jiyong Park16e91a02018-12-20 18:18:08 +0900639
640func TestStaticLinking(t *testing.T) {
641 ctx := testApex(t, `
642 apex {
643 name: "myapex",
644 key: "myapex.key",
645 native_shared_libs: ["mylib"],
646 }
647
648 apex_key {
649 name: "myapex.key",
650 public_key: "testkey.avbpubkey",
651 private_key: "testkey.pem",
652 }
653
654 cc_library {
655 name: "mylib",
656 srcs: ["mylib.cpp"],
657 system_shared_libs: [],
658 stl: "none",
659 stubs: {
660 versions: ["1", "2", "3"],
661 },
662 }
663
664 cc_binary {
665 name: "not_in_apex",
666 srcs: ["mylib.cpp"],
667 static_libs: ["mylib"],
668 static_executable: true,
669 system_shared_libs: [],
670 stl: "none",
671 }
672
673 cc_object {
674 name: "crtbegin_static",
675 stl: "none",
676 }
677
678 cc_object {
679 name: "crtend_android",
680 stl: "none",
681 }
682
683 `)
684
685 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
686
687 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +0800688 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +0900689}
Jiyong Park9335a262018-12-24 11:31:58 +0900690
691func TestKeys(t *testing.T) {
692 ctx := testApex(t, `
693 apex {
694 name: "myapex",
695 key: "myapex.key",
696 native_shared_libs: ["mylib"],
697 }
698
699 cc_library {
700 name: "mylib",
701 srcs: ["mylib.cpp"],
702 system_shared_libs: [],
703 stl: "none",
704 }
705
706 apex_key {
707 name: "myapex.key",
708 public_key: "testkey.avbpubkey",
709 private_key: "testkey.pem",
710 }
711
712 `)
713
714 // check the APEX keys
715 keys := ctx.ModuleForTests("myapex.key", "").Module().(*apexKey)
716
717 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
718 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
719 "vendor/foo/devkeys/testkey.avbpubkey")
720 }
721 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
722 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
723 "vendor/foo/devkeys/testkey.pem")
724 }
725
726 // check the APK certs
727 certs := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk").Args["certificates"]
728 if certs != "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8" {
729 t.Errorf("cert and private key %q are not %q", certs,
730 "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8")
731 }
732}
Jiyong Park58e364a2019-01-19 19:24:06 +0900733
734func TestMacro(t *testing.T) {
735 ctx := testApex(t, `
736 apex {
737 name: "myapex",
738 key: "myapex.key",
739 native_shared_libs: ["mylib"],
740 }
741
742 apex {
743 name: "otherapex",
744 key: "myapex.key",
745 native_shared_libs: ["mylib"],
746 }
747
748 apex_key {
749 name: "myapex.key",
750 public_key: "testkey.avbpubkey",
751 private_key: "testkey.pem",
752 }
753
754 cc_library {
755 name: "mylib",
756 srcs: ["mylib.cpp"],
757 system_shared_libs: [],
758 stl: "none",
759 }
760 `)
761
762 // non-APEX variant does not have __ANDROID__APEX__ defined
763 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
764 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
765 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
766
767 // APEX variant has __ANDROID_APEX__=<apexname> defined
768 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
769 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
770 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
771
772 // APEX variant has __ANDROID_APEX__=<apexname> defined
773 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
774 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
775 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
776}