blob: 4f2a58348d6c89363b4c231af0d646dc0ae06f18 [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 Parkf7c3bbe2020-12-09 21:18:56 +090018 "fmt"
Jiyong Park25fc6a92018-11-18 18:02:45 +090019 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Paul Duffin37856732021-02-26 14:24:15 +000021 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070022 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010023 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090024 "sort"
Jiyong Parkd4a3a132021-03-17 20:21:35 +090025 "strconv"
Jiyong Park25fc6a92018-11-18 18:02:45 +090026 "strings"
27 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090028
29 "github.com/google/blueprint/proptools"
30
31 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080032 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090033 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000034 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090036 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090037 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090039)
40
Jooyung Hand3639552019-08-09 12:57:43 +090041// names returns name list from white space separated string
42func names(s string) (ns []string) {
43 for _, n := range strings.Split(s, " ") {
44 if len(n) > 0 {
45 ns = append(ns, n)
46 }
47 }
48 return
49}
50
Paul Duffin40b62572021-03-20 11:39:01 +000051func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090052 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010053 android.GroupFixturePreparers(
54 prepareForApexTest,
55 android.GroupFixturePreparers(preparers...),
56 ).
Paul Duffine05480a2021-03-08 15:07:14 +000057 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000058 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090059}
60
Paul Duffin40b62572021-03-20 11:39:01 +000061func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090062 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010063
64 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000065 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010066 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000067 }
Paul Duffin284165a2021-03-29 01:50:31 +010068
69 result := android.GroupFixturePreparers(
70 prepareForApexTest,
71 android.GroupFixturePreparers(preparers...),
72 optionalBpPreparer,
73 ).RunTest(t)
74
Paul Duffine05480a2021-03-08 15:07:14 +000075 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090076}
77
Paul Duffin810f33d2021-03-09 14:12:32 +000078func withFiles(files android.MockFS) android.FixturePreparer {
79 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090080}
81
Paul Duffin810f33d2021-03-09 14:12:32 +000082func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
83 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090084 for k, v := range targets {
85 config.Targets[k] = v
86 }
Paul Duffin810f33d2021-03-09 14:12:32 +000087 })
Jooyung Han344d5432019-08-23 11:17:39 +090088}
89
Jooyung Han35155c42020-02-06 17:33:20 +090090// withNativeBridgeTargets sets configuration with targets including:
91// - X86_64 (primary)
92// - X86 (secondary)
93// - Arm64 on X86_64 (native bridge)
94// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000095var withNativeBridgeEnabled = android.FixtureModifyConfig(
96 func(config android.Config) {
97 config.Targets[android.Android] = []android.Target{
98 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
99 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
100 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
101 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
102 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
103 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
104 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
105 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
106 }
107 },
108)
109
110func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
111 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
112 variables.ManifestPackageNameOverrides = specs
113 })
Jooyung Han35155c42020-02-06 17:33:20 +0900114}
115
Paul Duffin810f33d2021-03-09 14:12:32 +0000116var withBinder32bit = android.FixtureModifyProductVariables(
117 func(variables android.FixtureProductVariables) {
118 variables.Binder32bit = proptools.BoolPtr(true)
119 },
120)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900121
Paul Duffin810f33d2021-03-09 14:12:32 +0000122var withUnbundledBuild = android.FixtureModifyProductVariables(
123 func(variables android.FixtureProductVariables) {
124 variables.Unbundled_build = proptools.BoolPtr(true)
125 },
126)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900127
Paul Duffin284165a2021-03-29 01:50:31 +0100128// Legacy preparer used for running tests within the apex package.
129//
130// This includes everything that was needed to run any test in the apex package prior to the
131// introduction of the test fixtures. Tests that are being converted to use fixtures directly
132// rather than through the testApex...() methods should avoid using this and instead use the
133// various preparers directly, using android.GroupFixturePreparers(...) to group them when
134// necessary.
135//
136// deprecated
137var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000138 // General preparers in alphabetical order as test infrastructure will enforce correct
139 // registration order.
140 android.PrepareForTestWithAndroidBuildComponents,
141 bpf.PrepareForTestWithBpf,
142 cc.PrepareForTestWithCcBuildComponents,
143 java.PrepareForTestWithJavaDefaultModules,
144 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
145 rust.PrepareForTestWithRustDefaultModules,
146 sh.PrepareForTestWithShBuildComponents,
147
148 PrepareForTestWithApexBuildComponents,
149
150 // Additional apex test specific preparers.
151 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
152 filegroup {
153 name: "myapex-file_contexts",
154 srcs: [
155 "apex/myapex-file_contexts",
156 ],
157 }
158 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000159 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000160 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000161 "a.java": nil,
162 "PrebuiltAppFoo.apk": nil,
163 "PrebuiltAppFooPriv.apk": nil,
164 "apex_manifest.json": nil,
165 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000166 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
167 "system/sepolicy/apex/myapex2-file_contexts": nil,
168 "system/sepolicy/apex/otherapex-file_contexts": nil,
169 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
170 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
171 "mylib.cpp": nil,
172 "mytest.cpp": nil,
173 "mytest1.cpp": nil,
174 "mytest2.cpp": nil,
175 "mytest3.cpp": nil,
176 "myprebuilt": nil,
177 "my_include": nil,
178 "foo/bar/MyClass.java": nil,
179 "prebuilt.jar": nil,
180 "prebuilt.so": nil,
181 "vendor/foo/devkeys/test.x509.pem": nil,
182 "vendor/foo/devkeys/test.pk8": nil,
183 "testkey.x509.pem": nil,
184 "testkey.pk8": nil,
185 "testkey.override.x509.pem": nil,
186 "testkey.override.pk8": nil,
187 "vendor/foo/devkeys/testkey.avbpubkey": nil,
188 "vendor/foo/devkeys/testkey.pem": nil,
189 "NOTICE": nil,
190 "custom_notice": nil,
191 "custom_notice_for_static_lib": nil,
192 "testkey2.avbpubkey": nil,
193 "testkey2.pem": nil,
194 "myapex-arm64.apex": nil,
195 "myapex-arm.apex": nil,
196 "myapex.apks": nil,
197 "frameworks/base/api/current.txt": nil,
198 "framework/aidl/a.aidl": nil,
199 "build/make/core/proguard.flags": nil,
200 "build/make/core/proguard_basic_keeps.flags": nil,
201 "dummy.txt": nil,
202 "baz": nil,
203 "bar/baz": nil,
204 "testdata/baz": nil,
205 "AppSet.apks": nil,
206 "foo.rs": nil,
207 "libfoo.jar": nil,
208 "libbar.jar": nil,
209 },
210 ),
211
212 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
213 variables.DeviceVndkVersion = proptools.StringPtr("current")
214 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
215 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
216 variables.Platform_sdk_codename = proptools.StringPtr("Q")
217 variables.Platform_sdk_final = proptools.BoolPtr(false)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000218 // "Tiramisu" needs to be in the next line for compatibility with soong code,
219 // not because of these tests specifically (it's not used by the tests)
220 variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900221 variables.Platform_vndk_version = proptools.StringPtr("29")
Paul Duffin37aad602021-03-08 09:47:16 +0000222 }),
223)
224
Paul Duffin52bfaa42021-03-23 23:40:12 +0000225var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
226 "system/sepolicy/apex/myapex-file_contexts": nil,
227})
228
Jooyung Han643adc42020-02-27 13:50:06 +0900229// ensure that 'result' equals 'expected'
230func ensureEquals(t *testing.T, result string, expected string) {
231 t.Helper()
232 if result != expected {
233 t.Errorf("%q != %q", expected, result)
234 }
235}
236
Jiyong Park25fc6a92018-11-18 18:02:45 +0900237// ensure that 'result' contains 'expected'
238func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900239 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900240 if !strings.Contains(result, expected) {
241 t.Errorf("%q is not found in %q", expected, result)
242 }
243}
244
Liz Kammer5bd365f2020-05-27 15:15:11 -0700245// ensure that 'result' contains 'expected' exactly one time
246func ensureContainsOnce(t *testing.T, result string, expected string) {
247 t.Helper()
248 count := strings.Count(result, expected)
249 if count != 1 {
250 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
251 }
252}
253
Jiyong Park25fc6a92018-11-18 18:02:45 +0900254// ensures that 'result' does not contain 'notExpected'
255func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900256 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900257 if strings.Contains(result, notExpected) {
258 t.Errorf("%q is found in %q", notExpected, result)
259 }
260}
261
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700262func ensureMatches(t *testing.T, result string, expectedRex string) {
263 ok, err := regexp.MatchString(expectedRex, result)
264 if err != nil {
265 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
266 return
267 }
268 if !ok {
269 t.Errorf("%s does not match regular expession %s", result, expectedRex)
270 }
271}
272
Jiyong Park25fc6a92018-11-18 18:02:45 +0900273func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900274 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900275 if !android.InList(expected, result) {
276 t.Errorf("%q is not found in %v", expected, result)
277 }
278}
279
280func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900281 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900282 if android.InList(notExpected, result) {
283 t.Errorf("%q is found in %v", notExpected, result)
284 }
285}
286
Jooyung Hane1633032019-08-01 17:41:43 +0900287func ensureListEmpty(t *testing.T, result []string) {
288 t.Helper()
289 if len(result) > 0 {
290 t.Errorf("%q is expected to be empty", result)
291 }
292}
293
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000294func ensureListNotEmpty(t *testing.T, result []string) {
295 t.Helper()
296 if len(result) == 0 {
297 t.Errorf("%q is expected to be not empty", result)
298 }
299}
300
Jiyong Park25fc6a92018-11-18 18:02:45 +0900301// Minimal test
302func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800303 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900304 apex_defaults {
305 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900306 manifest: ":myapex.manifest",
307 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900308 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900309 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900310 native_shared_libs: [
311 "mylib",
312 "libfoo.ffi",
313 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900314 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800315 multilib: {
316 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900317 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800318 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900319 },
Jiyong Park77acec62020-06-01 21:39:15 +0900320 java_libs: [
321 "myjar",
322 "myjar_dex",
323 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000324 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900325 }
326
Jiyong Park30ca9372019-02-07 16:27:23 +0900327 apex {
328 name: "myapex",
329 defaults: ["myapex-defaults"],
330 }
331
Jiyong Park25fc6a92018-11-18 18:02:45 +0900332 apex_key {
333 name: "myapex.key",
334 public_key: "testkey.avbpubkey",
335 private_key: "testkey.pem",
336 }
337
Jiyong Park809bb722019-02-13 21:33:49 +0900338 filegroup {
339 name: "myapex.manifest",
340 srcs: ["apex_manifest.json"],
341 }
342
343 filegroup {
344 name: "myapex.androidmanifest",
345 srcs: ["AndroidManifest.xml"],
346 }
347
Jiyong Park25fc6a92018-11-18 18:02:45 +0900348 cc_library {
349 name: "mylib",
350 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900351 shared_libs: [
352 "mylib2",
353 "libbar.ffi",
354 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900355 system_shared_libs: [],
356 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000357 // TODO: remove //apex_available:platform
358 apex_available: [
359 "//apex_available:platform",
360 "myapex",
361 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900362 }
363
Alex Light3d673592019-01-18 14:37:31 -0800364 cc_binary {
365 name: "foo",
366 srcs: ["mylib.cpp"],
367 compile_multilib: "both",
368 multilib: {
369 lib32: {
370 suffix: "32",
371 },
372 lib64: {
373 suffix: "64",
374 },
375 },
376 symlinks: ["foo_link_"],
377 symlink_preferred_arch: true,
378 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800379 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700380 apex_available: [ "myapex", "com.android.gki.*" ],
381 }
382
Jiyong Park99644e92020-11-17 22:21:02 +0900383 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000384 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900385 srcs: ["foo.rs"],
386 rlibs: ["libfoo.rlib.rust"],
387 dylibs: ["libfoo.dylib.rust"],
388 apex_available: ["myapex"],
389 }
390
391 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000392 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900393 srcs: ["foo.rs"],
394 crate_name: "foo",
395 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900396 shared_libs: ["libfoo.shared_from_rust"],
397 }
398
399 cc_library_shared {
400 name: "libfoo.shared_from_rust",
401 srcs: ["mylib.cpp"],
402 system_shared_libs: [],
403 stl: "none",
404 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900405 }
406
407 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000408 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900409 srcs: ["foo.rs"],
410 crate_name: "foo",
411 apex_available: ["myapex"],
412 }
413
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900414 rust_ffi_shared {
415 name: "libfoo.ffi",
416 srcs: ["foo.rs"],
417 crate_name: "foo",
418 apex_available: ["myapex"],
419 }
420
421 rust_ffi_shared {
422 name: "libbar.ffi",
423 srcs: ["foo.rs"],
424 crate_name: "bar",
425 apex_available: ["myapex"],
426 }
427
Yifan Hongd22a84a2020-07-28 17:37:46 -0700428 apex {
429 name: "com.android.gki.fake",
430 binaries: ["foo"],
431 key: "myapex.key",
432 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000433 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800434 }
435
Paul Duffindddd5462020-04-07 15:25:44 +0100436 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900437 name: "mylib2",
438 srcs: ["mylib.cpp"],
439 system_shared_libs: [],
440 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900441 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900442 static_libs: ["libstatic"],
443 // TODO: remove //apex_available:platform
444 apex_available: [
445 "//apex_available:platform",
446 "myapex",
447 ],
448 }
449
Paul Duffindddd5462020-04-07 15:25:44 +0100450 cc_prebuilt_library_shared {
451 name: "mylib2",
452 srcs: ["prebuilt.so"],
453 // TODO: remove //apex_available:platform
454 apex_available: [
455 "//apex_available:platform",
456 "myapex",
457 ],
458 }
459
Jiyong Park9918e1a2020-03-17 19:16:40 +0900460 cc_library_static {
461 name: "libstatic",
462 srcs: ["mylib.cpp"],
463 system_shared_libs: [],
464 stl: "none",
465 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000466 // TODO: remove //apex_available:platform
467 apex_available: [
468 "//apex_available:platform",
469 "myapex",
470 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900471 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900472
473 java_library {
474 name: "myjar",
475 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900476 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900477 sdk_version: "none",
478 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900479 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900480 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000481 // TODO: remove //apex_available:platform
482 apex_available: [
483 "//apex_available:platform",
484 "myapex",
485 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900486 }
487
Jiyong Park77acec62020-06-01 21:39:15 +0900488 dex_import {
489 name: "myjar_dex",
490 jars: ["prebuilt.jar"],
491 apex_available: [
492 "//apex_available:platform",
493 "myapex",
494 ],
495 }
496
Jiyong Park7f7766d2019-07-25 22:02:35 +0900497 java_library {
498 name: "myotherjar",
499 srcs: ["foo/bar/MyClass.java"],
500 sdk_version: "none",
501 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900502 // TODO: remove //apex_available:platform
503 apex_available: [
504 "//apex_available:platform",
505 "myapex",
506 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900507 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900508
509 java_library {
510 name: "mysharedjar",
511 srcs: ["foo/bar/MyClass.java"],
512 sdk_version: "none",
513 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900514 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900515 `)
516
Paul Duffina71a67a2021-03-29 00:42:57 +0100517 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900518
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900519 // Make sure that Android.mk is created
520 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700521 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900522 var builder strings.Builder
523 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
524
525 androidMk := builder.String()
526 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
527 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
528
Jiyong Park42cca6c2019-04-01 11:15:50 +0900529 optFlags := apexRule.Args["opt_flags"]
530 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700531 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100532 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900533
Jiyong Park25fc6a92018-11-18 18:02:45 +0900534 copyCmds := apexRule.Args["copy_commands"]
535
536 // Ensure that main rule creates an output
537 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
538
539 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700540 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
541 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
542 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900543 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900544 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900545
546 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700547 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
548 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900549 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
550 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900551 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900552 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900553
554 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800555 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
556 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900557 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900558 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900559 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900560 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
561 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900562 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900563 // .. but not for java libs
564 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900565 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800566
Colin Cross7113d202019-11-20 16:39:12 -0800567 // Ensure that the platform variant ends with _shared or _common
568 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
569 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900570 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
571 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900572 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
573
574 // Ensure that dynamic dependency to java libs are not included
575 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800576
577 // Ensure that all symlinks are present.
578 found_foo_link_64 := false
579 found_foo := false
580 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900581 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800582 if strings.HasSuffix(cmd, "bin/foo") {
583 found_foo = true
584 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
585 found_foo_link_64 = true
586 }
587 }
588 }
589 good := found_foo && found_foo_link_64
590 if !good {
591 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
592 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900593
Sundong Ahnabb64432019-10-22 13:58:29 +0900594 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700595 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900596 if len(noticeInputs) != 3 {
597 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900598 }
599 ensureListContains(t, noticeInputs, "NOTICE")
600 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900601 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900602
Artur Satayeva8bd1132020-04-27 18:07:06 +0100603 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100604 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100605 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
606 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
607 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100608
609 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100610 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100611 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
612 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
613 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800614}
615
Jooyung Hanf21c7972019-12-16 22:32:06 +0900616func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800617 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900618 apex_defaults {
619 name: "myapex-defaults",
620 key: "myapex.key",
621 prebuilts: ["myetc"],
622 native_shared_libs: ["mylib"],
623 java_libs: ["myjar"],
624 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900625 rros: ["rro"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800626 bpfs: ["bpf", "netd_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000627 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900628 }
629
630 prebuilt_etc {
631 name: "myetc",
632 src: "myprebuilt",
633 }
634
635 apex {
636 name: "myapex",
637 defaults: ["myapex-defaults"],
638 }
639
640 apex_key {
641 name: "myapex.key",
642 public_key: "testkey.avbpubkey",
643 private_key: "testkey.pem",
644 }
645
646 cc_library {
647 name: "mylib",
648 system_shared_libs: [],
649 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000650 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900651 }
652
653 java_library {
654 name: "myjar",
655 srcs: ["foo/bar/MyClass.java"],
656 sdk_version: "none",
657 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000658 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900659 }
660
661 android_app {
662 name: "AppFoo",
663 srcs: ["foo/bar/MyClass.java"],
664 sdk_version: "none",
665 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000666 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900667 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900668
669 runtime_resource_overlay {
670 name: "rro",
671 theme: "blue",
672 }
673
markchien2f59ec92020-09-02 16:23:38 +0800674 bpf {
675 name: "bpf",
676 srcs: ["bpf.c", "bpf2.c"],
677 }
678
Ken Chenfad7f9d2021-11-10 22:02:57 +0800679 bpf {
680 name: "netd_test",
681 srcs: ["netd_test.c"],
682 sub_dir: "netd",
683 }
684
Jooyung Hanf21c7972019-12-16 22:32:06 +0900685 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000686 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900687 "etc/myetc",
688 "javalib/myjar.jar",
689 "lib64/mylib.so",
690 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900691 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800692 "etc/bpf/bpf.o",
693 "etc/bpf/bpf2.o",
Ken Chenfad7f9d2021-11-10 22:02:57 +0800694 "etc/bpf/netd/netd_test.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900695 })
696}
697
Jooyung Han01a3ee22019-11-02 02:52:25 +0900698func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800699 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900700 apex {
701 name: "myapex",
702 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000703 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900704 }
705
706 apex_key {
707 name: "myapex.key",
708 public_key: "testkey.avbpubkey",
709 private_key: "testkey.pem",
710 }
711 `)
712
713 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900714 args := module.Rule("apexRule").Args
715 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
716 t.Error("manifest should be apex_manifest.pb, but " + manifest)
717 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900718}
719
Liz Kammer4854a7d2021-05-27 14:28:27 -0400720func TestApexManifestMinSdkVersion(t *testing.T) {
721 ctx := testApex(t, `
722 apex_defaults {
723 name: "my_defaults",
724 key: "myapex.key",
725 product_specific: true,
726 file_contexts: ":my-file-contexts",
727 updatable: false,
728 }
729 apex {
730 name: "myapex_30",
731 min_sdk_version: "30",
732 defaults: ["my_defaults"],
733 }
734
735 apex {
736 name: "myapex_current",
737 min_sdk_version: "current",
738 defaults: ["my_defaults"],
739 }
740
741 apex {
742 name: "myapex_none",
743 defaults: ["my_defaults"],
744 }
745
746 apex_key {
747 name: "myapex.key",
748 public_key: "testkey.avbpubkey",
749 private_key: "testkey.pem",
750 }
751
752 filegroup {
753 name: "my-file-contexts",
754 srcs: ["product_specific_file_contexts"],
755 }
756 `, withFiles(map[string][]byte{
757 "product_specific_file_contexts": nil,
758 }), android.FixtureModifyProductVariables(
759 func(variables android.FixtureProductVariables) {
760 variables.Unbundled_build = proptools.BoolPtr(true)
761 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
762 }), android.FixtureMergeEnv(map[string]string{
763 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
764 }))
765
766 testCases := []struct {
767 module string
768 minSdkVersion string
769 }{
770 {
771 module: "myapex_30",
772 minSdkVersion: "30",
773 },
774 {
775 module: "myapex_current",
776 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
777 },
778 {
779 module: "myapex_none",
780 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
781 },
782 }
783 for _, tc := range testCases {
784 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module+"_image")
785 args := module.Rule("apexRule").Args
786 optFlags := args["opt_flags"]
787 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
788 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
789 }
790 }
791}
792
Alex Light5098a612018-11-29 17:12:15 -0800793func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800794 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800795 apex {
796 name: "myapex",
797 key: "myapex.key",
798 payload_type: "zip",
799 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000800 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800801 }
802
803 apex_key {
804 name: "myapex.key",
805 public_key: "testkey.avbpubkey",
806 private_key: "testkey.pem",
807 }
808
809 cc_library {
810 name: "mylib",
811 srcs: ["mylib.cpp"],
812 shared_libs: ["mylib2"],
813 system_shared_libs: [],
814 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000815 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800816 }
817
818 cc_library {
819 name: "mylib2",
820 srcs: ["mylib.cpp"],
821 system_shared_libs: [],
822 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000823 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800824 }
825 `)
826
Sundong Ahnabb64432019-10-22 13:58:29 +0900827 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800828 copyCmds := zipApexRule.Args["copy_commands"]
829
830 // Ensure that main rule creates an output
831 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
832
833 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700834 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800835
836 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700837 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800838
839 // Ensure that both direct and indirect deps are copied into apex
840 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
841 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900842}
843
844func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800845 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900846 apex {
847 name: "myapex",
848 key: "myapex.key",
849 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900850 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000851 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900852 }
853
854 apex_key {
855 name: "myapex.key",
856 public_key: "testkey.avbpubkey",
857 private_key: "testkey.pem",
858 }
859
860 cc_library {
861 name: "mylib",
862 srcs: ["mylib.cpp"],
863 shared_libs: ["mylib2", "mylib3"],
864 system_shared_libs: [],
865 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000866 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900867 }
868
869 cc_library {
870 name: "mylib2",
871 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900872 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900873 system_shared_libs: [],
874 stl: "none",
875 stubs: {
876 versions: ["1", "2", "3"],
877 },
878 }
879
880 cc_library {
881 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900882 srcs: ["mylib.cpp"],
883 shared_libs: ["mylib4"],
884 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900885 stl: "none",
886 stubs: {
887 versions: ["10", "11", "12"],
888 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000889 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900890 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900891
892 cc_library {
893 name: "mylib4",
894 srcs: ["mylib.cpp"],
895 system_shared_libs: [],
896 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000897 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900898 }
Jiyong Park105dc322021-06-11 17:22:09 +0900899
900 rust_binary {
901 name: "foo.rust",
902 srcs: ["foo.rs"],
903 shared_libs: ["libfoo.shared_from_rust"],
904 prefer_rlib: true,
905 apex_available: ["myapex"],
906 }
907
908 cc_library_shared {
909 name: "libfoo.shared_from_rust",
910 srcs: ["mylib.cpp"],
911 system_shared_libs: [],
912 stl: "none",
913 stubs: {
914 versions: ["10", "11", "12"],
915 },
916 }
917
Jiyong Park25fc6a92018-11-18 18:02:45 +0900918 `)
919
Sundong Ahnabb64432019-10-22 13:58:29 +0900920 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900921 copyCmds := apexRule.Args["copy_commands"]
922
923 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800924 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900925
926 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800927 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900928
929 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800930 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900931
Colin Crossaede88c2020-08-11 12:17:01 -0700932 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900933
934 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900935 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900936 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900937 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900938
939 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Crossaede88c2020-08-11 12:17:01 -0700940 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900941 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700942 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900943
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700944 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
945 // is replaced by sharing of "cFlags" in cc/builder.go.
946 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
947 // module variable representing "cflags". So it was not detected by ensureNotContains.
948 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
949 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
950 // including the original cflags's "-include mylib.h".
951 //
Jiyong Park64379952018-12-13 18:37:29 +0900952 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700953 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
954 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900955
956 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700957 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900958
Jooyung Hana57af4a2020-01-23 05:36:59 +0000959 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900960 "lib64/mylib.so",
961 "lib64/mylib3.so",
962 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +0900963 "bin/foo.rust",
964 "lib64/libc++.so", // by the implicit dependency from foo.rust
965 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +0900966 })
Jiyong Park105dc322021-06-11 17:22:09 +0900967
968 // Ensure that stub dependency from a rust module is not included
969 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
970 // The rust module is linked to the stub cc library
971 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
972 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
973 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +0900974
975 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
976 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900977}
978
Jiyong Park1bc84122021-06-22 20:23:05 +0900979func TestApexCanUsePrivateApis(t *testing.T) {
980 ctx := testApex(t, `
981 apex {
982 name: "myapex",
983 key: "myapex.key",
984 native_shared_libs: ["mylib"],
985 binaries: ["foo.rust"],
986 updatable: false,
987 platform_apis: true,
988 }
989
990 apex_key {
991 name: "myapex.key",
992 public_key: "testkey.avbpubkey",
993 private_key: "testkey.pem",
994 }
995
996 cc_library {
997 name: "mylib",
998 srcs: ["mylib.cpp"],
999 shared_libs: ["mylib2"],
1000 system_shared_libs: [],
1001 stl: "none",
1002 apex_available: [ "myapex" ],
1003 }
1004
1005 cc_library {
1006 name: "mylib2",
1007 srcs: ["mylib.cpp"],
1008 cflags: ["-include mylib.h"],
1009 system_shared_libs: [],
1010 stl: "none",
1011 stubs: {
1012 versions: ["1", "2", "3"],
1013 },
1014 }
1015
1016 rust_binary {
1017 name: "foo.rust",
1018 srcs: ["foo.rs"],
1019 shared_libs: ["libfoo.shared_from_rust"],
1020 prefer_rlib: true,
1021 apex_available: ["myapex"],
1022 }
1023
1024 cc_library_shared {
1025 name: "libfoo.shared_from_rust",
1026 srcs: ["mylib.cpp"],
1027 system_shared_libs: [],
1028 stl: "none",
1029 stubs: {
1030 versions: ["10", "11", "12"],
1031 },
1032 }
1033 `)
1034
1035 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1036 copyCmds := apexRule.Args["copy_commands"]
1037
1038 // Ensure that indirect stubs dep is not included
1039 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1040 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1041
1042 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1043 // of the platform_apis: true)
1044 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000_private").Rule("ld").Args["libFlags"]
1045 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1046 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1047 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000_private").Rule("rustc").Args["linkFlags"]
1048 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1049 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1050}
1051
Colin Cross7812fd32020-09-25 12:35:10 -07001052func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1053 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001054 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001055 apex {
1056 name: "myapex",
1057 key: "myapex.key",
1058 native_shared_libs: ["mylib", "mylib3"],
1059 min_sdk_version: "29",
1060 }
1061
1062 apex_key {
1063 name: "myapex.key",
1064 public_key: "testkey.avbpubkey",
1065 private_key: "testkey.pem",
1066 }
1067
1068 cc_library {
1069 name: "mylib",
1070 srcs: ["mylib.cpp"],
1071 shared_libs: ["mylib2", "mylib3"],
1072 system_shared_libs: [],
1073 stl: "none",
1074 apex_available: [ "myapex" ],
1075 min_sdk_version: "28",
1076 }
1077
1078 cc_library {
1079 name: "mylib2",
1080 srcs: ["mylib.cpp"],
1081 cflags: ["-include mylib.h"],
1082 system_shared_libs: [],
1083 stl: "none",
1084 stubs: {
1085 versions: ["28", "29", "30", "current"],
1086 },
1087 min_sdk_version: "28",
1088 }
1089
1090 cc_library {
1091 name: "mylib3",
1092 srcs: ["mylib.cpp"],
1093 shared_libs: ["mylib4"],
1094 system_shared_libs: [],
1095 stl: "none",
1096 stubs: {
1097 versions: ["28", "29", "30", "current"],
1098 },
1099 apex_available: [ "myapex" ],
1100 min_sdk_version: "28",
1101 }
1102
1103 cc_library {
1104 name: "mylib4",
1105 srcs: ["mylib.cpp"],
1106 system_shared_libs: [],
1107 stl: "none",
1108 apex_available: [ "myapex" ],
1109 min_sdk_version: "28",
1110 }
1111 `)
1112
1113 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1114 copyCmds := apexRule.Args["copy_commands"]
1115
1116 // Ensure that direct non-stubs dep is always included
1117 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1118
1119 // Ensure that indirect stubs dep is not included
1120 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1121
1122 // Ensure that direct stubs dep is included
1123 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1124
1125 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1126
Jiyong Park55549df2021-02-26 23:57:23 +09001127 // Ensure that mylib is linking with the latest version of stub for mylib2
1128 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001129 // ... and not linking to the non-stub (impl) variant of mylib2
1130 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1131
1132 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1133 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1134 // .. and not linking to the stubs variant of mylib3
1135 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1136
1137 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001138 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001139 ensureNotContains(t, mylib2Cflags, "-include ")
1140
1141 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -07001142 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -07001143
1144 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1145 "lib64/mylib.so",
1146 "lib64/mylib3.so",
1147 "lib64/mylib4.so",
1148 })
1149}
1150
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001151func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1152 t.Parallel()
1153 // myapex (Z)
1154 // mylib -----------------.
1155 // |
1156 // otherapex (29) |
1157 // libstub's versions: 29 Z current
1158 // |
1159 // <platform> |
1160 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001161 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001162 apex {
1163 name: "myapex",
1164 key: "myapex.key",
1165 native_shared_libs: ["mylib"],
1166 min_sdk_version: "Z", // non-final
1167 }
1168
1169 cc_library {
1170 name: "mylib",
1171 srcs: ["mylib.cpp"],
1172 shared_libs: ["libstub"],
1173 apex_available: ["myapex"],
1174 min_sdk_version: "Z",
1175 }
1176
1177 apex_key {
1178 name: "myapex.key",
1179 public_key: "testkey.avbpubkey",
1180 private_key: "testkey.pem",
1181 }
1182
1183 apex {
1184 name: "otherapex",
1185 key: "myapex.key",
1186 native_shared_libs: ["libstub"],
1187 min_sdk_version: "29",
1188 }
1189
1190 cc_library {
1191 name: "libstub",
1192 srcs: ["mylib.cpp"],
1193 stubs: {
1194 versions: ["29", "Z", "current"],
1195 },
1196 apex_available: ["otherapex"],
1197 min_sdk_version: "29",
1198 }
1199
1200 // platform module depending on libstub from otherapex should use the latest stub("current")
1201 cc_library {
1202 name: "libplatform",
1203 srcs: ["mylib.cpp"],
1204 shared_libs: ["libstub"],
1205 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001206 `,
1207 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1208 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1209 variables.Platform_sdk_final = proptools.BoolPtr(false)
1210 variables.Platform_version_active_codenames = []string{"Z"}
1211 }),
1212 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001213
Jiyong Park55549df2021-02-26 23:57:23 +09001214 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001215 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001216 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001217 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001218 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001219
1220 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1221 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1222 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1223 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1224 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1225}
1226
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001227func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001228 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001229 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001230 name: "myapex2",
1231 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001232 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001233 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001234 }
1235
1236 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001237 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001238 public_key: "testkey.avbpubkey",
1239 private_key: "testkey.pem",
1240 }
1241
1242 cc_library {
1243 name: "mylib",
1244 srcs: ["mylib.cpp"],
1245 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001246 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001247 system_shared_libs: [],
1248 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001249 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001250 }
1251
1252 cc_library {
1253 name: "libfoo",
1254 srcs: ["mylib.cpp"],
1255 shared_libs: ["libbar"],
1256 system_shared_libs: [],
1257 stl: "none",
1258 stubs: {
1259 versions: ["10", "20", "30"],
1260 },
1261 }
1262
1263 cc_library {
1264 name: "libbar",
1265 srcs: ["mylib.cpp"],
1266 system_shared_libs: [],
1267 stl: "none",
1268 }
1269
Jiyong Park678c8812020-02-07 17:25:49 +09001270 cc_library_static {
1271 name: "libbaz",
1272 srcs: ["mylib.cpp"],
1273 system_shared_libs: [],
1274 stl: "none",
1275 apex_available: [ "myapex2" ],
1276 }
1277
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001278 `)
1279
Jiyong Park83dc74b2020-01-14 18:38:44 +09001280 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001281 copyCmds := apexRule.Args["copy_commands"]
1282
1283 // Ensure that direct non-stubs dep is always included
1284 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1285
1286 // Ensure that indirect stubs dep is not included
1287 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1288
1289 // Ensure that dependency of stubs is not included
1290 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1291
Colin Crossaede88c2020-08-11 12:17:01 -07001292 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001293
1294 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001295 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001296 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001297 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001298
Jiyong Park3ff16992019-12-27 14:11:47 +09001299 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001300
1301 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1302 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001303
Artur Satayeva8bd1132020-04-27 18:07:06 +01001304 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001305 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001306
Artur Satayeva8bd1132020-04-27 18:07:06 +01001307 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001308 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001309}
1310
Jooyung Hand3639552019-08-09 12:57:43 +09001311func TestApexWithRuntimeLibsDependency(t *testing.T) {
1312 /*
1313 myapex
1314 |
1315 v (runtime_libs)
1316 mylib ------+------> libfoo [provides stub]
1317 |
1318 `------> libbar
1319 */
Colin Cross1c460562021-02-16 17:55:47 -08001320 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001321 apex {
1322 name: "myapex",
1323 key: "myapex.key",
1324 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001325 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001326 }
1327
1328 apex_key {
1329 name: "myapex.key",
1330 public_key: "testkey.avbpubkey",
1331 private_key: "testkey.pem",
1332 }
1333
1334 cc_library {
1335 name: "mylib",
1336 srcs: ["mylib.cpp"],
1337 runtime_libs: ["libfoo", "libbar"],
1338 system_shared_libs: [],
1339 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001340 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001341 }
1342
1343 cc_library {
1344 name: "libfoo",
1345 srcs: ["mylib.cpp"],
1346 system_shared_libs: [],
1347 stl: "none",
1348 stubs: {
1349 versions: ["10", "20", "30"],
1350 },
1351 }
1352
1353 cc_library {
1354 name: "libbar",
1355 srcs: ["mylib.cpp"],
1356 system_shared_libs: [],
1357 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001358 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001359 }
1360
1361 `)
1362
Sundong Ahnabb64432019-10-22 13:58:29 +09001363 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001364 copyCmds := apexRule.Args["copy_commands"]
1365
1366 // Ensure that direct non-stubs dep is always included
1367 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1368
1369 // Ensure that indirect stubs dep is not included
1370 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1371
1372 // Ensure that runtime_libs dep in included
1373 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1374
Sundong Ahnabb64432019-10-22 13:58:29 +09001375 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001376 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1377 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001378
1379}
1380
Paul Duffina02cae32021-03-09 01:44:06 +00001381var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1382 cc.PrepareForTestWithCcBuildComponents,
1383 PrepareForTestWithApexBuildComponents,
1384 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001385 apex {
1386 name: "com.android.runtime",
1387 key: "com.android.runtime.key",
1388 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001389 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001390 }
1391
1392 apex_key {
1393 name: "com.android.runtime.key",
1394 public_key: "testkey.avbpubkey",
1395 private_key: "testkey.pem",
1396 }
Paul Duffina02cae32021-03-09 01:44:06 +00001397 `),
1398 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1399)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001400
Paul Duffina02cae32021-03-09 01:44:06 +00001401func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001402 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001403 cc_library {
1404 name: "libc",
1405 no_libcrt: true,
1406 nocrt: true,
1407 stl: "none",
1408 system_shared_libs: [],
1409 stubs: { versions: ["1"] },
1410 apex_available: ["com.android.runtime"],
1411
1412 sanitize: {
1413 hwaddress: true,
1414 }
1415 }
1416
1417 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001418 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001419 no_libcrt: true,
1420 nocrt: true,
1421 stl: "none",
1422 system_shared_libs: [],
1423 srcs: [""],
1424 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001425 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001426
1427 sanitize: {
1428 never: true,
1429 },
Paul Duffina02cae32021-03-09 01:44:06 +00001430 } `)
1431 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001432
1433 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1434 "lib64/bionic/libc.so",
1435 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1436 })
1437
Colin Cross4c4c1be2022-02-10 11:41:18 -08001438 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001439
1440 installed := hwasan.Description("install libclang_rt.hwasan")
1441 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1442
1443 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1444 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1445 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1446}
1447
1448func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001449 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001450 prepareForTestOfRuntimeApexWithHwasan,
1451 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1452 variables.SanitizeDevice = []string{"hwaddress"}
1453 }),
1454 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001455 cc_library {
1456 name: "libc",
1457 no_libcrt: true,
1458 nocrt: true,
1459 stl: "none",
1460 system_shared_libs: [],
1461 stubs: { versions: ["1"] },
1462 apex_available: ["com.android.runtime"],
1463 }
1464
1465 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001466 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001467 no_libcrt: true,
1468 nocrt: true,
1469 stl: "none",
1470 system_shared_libs: [],
1471 srcs: [""],
1472 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001473 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001474
1475 sanitize: {
1476 never: true,
1477 },
1478 }
Paul Duffina02cae32021-03-09 01:44:06 +00001479 `)
1480 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001481
1482 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1483 "lib64/bionic/libc.so",
1484 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1485 })
1486
Colin Cross4c4c1be2022-02-10 11:41:18 -08001487 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001488
1489 installed := hwasan.Description("install libclang_rt.hwasan")
1490 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1491
1492 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1493 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1494 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1495}
1496
Jooyung Han61b66e92020-03-21 14:21:46 +00001497func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1498 testcases := []struct {
1499 name string
1500 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001501 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001502 shouldLink string
1503 shouldNotLink []string
1504 }{
1505 {
Jiyong Park55549df2021-02-26 23:57:23 +09001506 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001507 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001508 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001509 shouldLink: "current",
1510 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001511 },
1512 {
Jiyong Park55549df2021-02-26 23:57:23 +09001513 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001514 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001515 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001516 shouldLink: "current",
1517 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001518 },
1519 }
1520 for _, tc := range testcases {
1521 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001522 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001523 apex {
1524 name: "myapex",
1525 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001526 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001527 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001528 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001529 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001530
Jooyung Han61b66e92020-03-21 14:21:46 +00001531 apex_key {
1532 name: "myapex.key",
1533 public_key: "testkey.avbpubkey",
1534 private_key: "testkey.pem",
1535 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001536
Jooyung Han61b66e92020-03-21 14:21:46 +00001537 cc_library {
1538 name: "mylib",
1539 srcs: ["mylib.cpp"],
1540 vendor_available: true,
1541 shared_libs: ["libbar"],
1542 system_shared_libs: [],
1543 stl: "none",
1544 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001545 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001546 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001547
Jooyung Han61b66e92020-03-21 14:21:46 +00001548 cc_library {
1549 name: "libbar",
1550 srcs: ["mylib.cpp"],
1551 system_shared_libs: [],
1552 stl: "none",
1553 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001554 llndk: {
1555 symbol_file: "libbar.map.txt",
1556 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001557 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001558 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001559 withUnbundledBuild,
1560 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001561
Jooyung Han61b66e92020-03-21 14:21:46 +00001562 // Ensure that LLNDK dep is not included
1563 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1564 "lib64/mylib.so",
1565 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001566
Jooyung Han61b66e92020-03-21 14:21:46 +00001567 // Ensure that LLNDK dep is required
1568 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1569 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1570 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001571
Steven Moreland2c4000c2021-04-27 02:08:49 +00001572 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1573 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001574 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001575 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001576 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001577
Steven Moreland2c4000c2021-04-27 02:08:49 +00001578 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001579 ver := tc.shouldLink
1580 if tc.shouldLink == "current" {
1581 ver = strconv.Itoa(android.FutureApiLevelInt)
1582 }
1583 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001584 })
1585 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001586}
1587
Jiyong Park25fc6a92018-11-18 18:02:45 +09001588func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001589 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001590 apex {
1591 name: "myapex",
1592 key: "myapex.key",
1593 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001594 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001595 }
1596
1597 apex_key {
1598 name: "myapex.key",
1599 public_key: "testkey.avbpubkey",
1600 private_key: "testkey.pem",
1601 }
1602
1603 cc_library {
1604 name: "mylib",
1605 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001606 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001607 shared_libs: ["libdl#27"],
1608 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001609 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001610 }
1611
1612 cc_library_shared {
1613 name: "mylib_shared",
1614 srcs: ["mylib.cpp"],
1615 shared_libs: ["libdl#27"],
1616 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001617 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001618 }
1619
1620 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001621 name: "libBootstrap",
1622 srcs: ["mylib.cpp"],
1623 stl: "none",
1624 bootstrap: true,
1625 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001626 `)
1627
Sundong Ahnabb64432019-10-22 13:58:29 +09001628 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001629 copyCmds := apexRule.Args["copy_commands"]
1630
1631 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001632 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001633 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1634 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001635
1636 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001637 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001638
Colin Crossaede88c2020-08-11 12:17:01 -07001639 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1640 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1641 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001642
1643 // For dependency to libc
1644 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001645 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001646 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001647 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001648 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001649 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1650 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001651
1652 // For dependency to libm
1653 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001654 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001655 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001656 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001657 // ... and is not compiling with the stub
1658 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1659 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1660
1661 // For dependency to libdl
1662 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001663 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001664 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001665 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1666 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001667 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001668 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001669 // ... Cflags from stub is correctly exported to mylib
1670 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1671 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001672
1673 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001674 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1675 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1676 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1677 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001678}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001679
Jooyung Han749dc692020-04-15 11:03:39 +09001680func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001681 // there are three links between liba --> libz.
1682 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001683 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001684 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001685 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001686 apex {
1687 name: "myapex",
1688 key: "myapex.key",
1689 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001690 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001691 }
1692
1693 apex {
1694 name: "otherapex",
1695 key: "myapex.key",
1696 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001697 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001698 }
1699
1700 apex_key {
1701 name: "myapex.key",
1702 public_key: "testkey.avbpubkey",
1703 private_key: "testkey.pem",
1704 }
1705
1706 cc_library {
1707 name: "libx",
1708 shared_libs: ["liba"],
1709 system_shared_libs: [],
1710 stl: "none",
1711 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001712 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001713 }
1714
1715 cc_library {
1716 name: "liby",
1717 shared_libs: ["liba"],
1718 system_shared_libs: [],
1719 stl: "none",
1720 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001721 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001722 }
1723
1724 cc_library {
1725 name: "liba",
1726 shared_libs: ["libz"],
1727 system_shared_libs: [],
1728 stl: "none",
1729 apex_available: [
1730 "//apex_available:anyapex",
1731 "//apex_available:platform",
1732 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001733 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001734 }
1735
1736 cc_library {
1737 name: "libz",
1738 system_shared_libs: [],
1739 stl: "none",
1740 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001741 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001742 },
1743 }
Jooyung Han749dc692020-04-15 11:03:39 +09001744 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001745
1746 expectLink := func(from, from_variant, to, to_variant string) {
1747 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1748 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1749 }
1750 expectNoLink := func(from, from_variant, to, to_variant string) {
1751 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1752 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1753 }
1754 // platform liba is linked to non-stub version
1755 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001756 // liba in myapex is linked to current
1757 expectLink("liba", "shared_apex29", "libz", "shared_current")
1758 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001759 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001760 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001761 // liba in otherapex is linked to current
1762 expectLink("liba", "shared_apex30", "libz", "shared_current")
1763 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001764 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1765 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001766}
1767
Jooyung Hanaed150d2020-04-02 01:41:41 +09001768func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001769 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001770 apex {
1771 name: "myapex",
1772 key: "myapex.key",
1773 native_shared_libs: ["libx"],
1774 min_sdk_version: "R",
1775 }
1776
1777 apex_key {
1778 name: "myapex.key",
1779 public_key: "testkey.avbpubkey",
1780 private_key: "testkey.pem",
1781 }
1782
1783 cc_library {
1784 name: "libx",
1785 shared_libs: ["libz"],
1786 system_shared_libs: [],
1787 stl: "none",
1788 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001789 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001790 }
1791
1792 cc_library {
1793 name: "libz",
1794 system_shared_libs: [],
1795 stl: "none",
1796 stubs: {
1797 versions: ["29", "R"],
1798 },
1799 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001800 `,
1801 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1802 variables.Platform_version_active_codenames = []string{"R"}
1803 }),
1804 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001805
1806 expectLink := func(from, from_variant, to, to_variant string) {
1807 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1808 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1809 }
1810 expectNoLink := func(from, from_variant, to, to_variant string) {
1811 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1812 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1813 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001814 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1815 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001816 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1817 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001818}
1819
Jooyung Han4c4da062021-06-23 10:23:16 +09001820func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1821 testApex(t, `
1822 apex {
1823 name: "myapex",
1824 key: "myapex.key",
1825 java_libs: ["libx"],
1826 min_sdk_version: "S",
1827 }
1828
1829 apex_key {
1830 name: "myapex.key",
1831 public_key: "testkey.avbpubkey",
1832 private_key: "testkey.pem",
1833 }
1834
1835 java_library {
1836 name: "libx",
1837 srcs: ["a.java"],
1838 apex_available: [ "myapex" ],
1839 sdk_version: "current",
1840 min_sdk_version: "S", // should be okay
1841 }
1842 `,
1843 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1844 variables.Platform_version_active_codenames = []string{"S"}
1845 variables.Platform_sdk_codename = proptools.StringPtr("S")
1846 }),
1847 )
1848}
1849
Jooyung Han749dc692020-04-15 11:03:39 +09001850func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001851 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001852 apex {
1853 name: "myapex",
1854 key: "myapex.key",
1855 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001856 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001857 }
1858
1859 apex_key {
1860 name: "myapex.key",
1861 public_key: "testkey.avbpubkey",
1862 private_key: "testkey.pem",
1863 }
1864
1865 cc_library {
1866 name: "libx",
1867 shared_libs: ["libz"],
1868 system_shared_libs: [],
1869 stl: "none",
1870 apex_available: [ "myapex" ],
1871 }
1872
1873 cc_library {
1874 name: "libz",
1875 system_shared_libs: [],
1876 stl: "none",
1877 stubs: {
1878 versions: ["1", "2"],
1879 },
1880 }
1881 `)
1882
1883 expectLink := func(from, from_variant, to, to_variant string) {
1884 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1885 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1886 }
1887 expectNoLink := func(from, from_variant, to, to_variant string) {
1888 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1889 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1890 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001891 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001892 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001893 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001894 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001895}
1896
Jiyong Park5df7bd32021-08-25 16:18:46 +09001897func TestApexMinSdkVersion_crtobjectInVendorApex(t *testing.T) {
1898 ctx := testApex(t, `
1899 apex {
1900 name: "myapex",
1901 key: "myapex.key",
1902 native_shared_libs: ["mylib"],
1903 updatable: false,
1904 vendor: true,
1905 min_sdk_version: "29",
1906 }
1907
1908 apex_key {
1909 name: "myapex.key",
1910 public_key: "testkey.avbpubkey",
1911 private_key: "testkey.pem",
1912 }
1913
1914 cc_library {
1915 name: "mylib",
1916 vendor_available: true,
1917 system_shared_libs: [],
1918 stl: "none",
1919 apex_available: [ "myapex" ],
1920 min_sdk_version: "29",
1921 }
1922 `)
1923
1924 vendorVariant := "android_vendor.29_arm64_armv8-a"
1925
1926 // First check that the correct variant of crtbegin_so is used.
1927 ldRule := ctx.ModuleForTests("mylib", vendorVariant+"_shared_apex29").Rule("ld")
1928 crtBegin := names(ldRule.Args["crtBegin"])
1929 ensureListContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
1930
1931 // Ensure that the crtbegin_so used by the APEX is targeting 29
1932 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
1933 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
1934}
1935
Jooyung Han03b51852020-02-26 22:45:42 +09001936func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001937 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001938 apex {
1939 name: "myapex",
1940 key: "myapex.key",
1941 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001942 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001943 }
1944
1945 apex_key {
1946 name: "myapex.key",
1947 public_key: "testkey.avbpubkey",
1948 private_key: "testkey.pem",
1949 }
1950
1951 cc_library {
1952 name: "libx",
1953 system_shared_libs: [],
1954 stl: "none",
1955 apex_available: [ "myapex" ],
1956 stubs: {
1957 versions: ["1", "2"],
1958 },
1959 }
1960
1961 cc_library {
1962 name: "libz",
1963 shared_libs: ["libx"],
1964 system_shared_libs: [],
1965 stl: "none",
1966 }
1967 `)
1968
1969 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001970 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001971 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1972 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1973 }
1974 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001975 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001976 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1977 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1978 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001979 expectLink("libz", "shared", "libx", "shared_current")
1980 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09001981 expectNoLink("libz", "shared", "libz", "shared_1")
1982 expectNoLink("libz", "shared", "libz", "shared")
1983}
1984
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001985var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1986 func(variables android.FixtureProductVariables) {
1987 variables.SanitizeDevice = []string{"hwaddress"}
1988 },
1989)
1990
Jooyung Han75568392020-03-20 04:29:24 +09001991func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001992 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001993 apex {
1994 name: "myapex",
1995 key: "myapex.key",
1996 native_shared_libs: ["libx"],
1997 min_sdk_version: "29",
1998 }
1999
2000 apex_key {
2001 name: "myapex.key",
2002 public_key: "testkey.avbpubkey",
2003 private_key: "testkey.pem",
2004 }
2005
2006 cc_library {
2007 name: "libx",
2008 shared_libs: ["libbar"],
2009 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002010 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002011 }
2012
2013 cc_library {
2014 name: "libbar",
2015 stubs: {
2016 versions: ["29", "30"],
2017 },
2018 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002019 `,
2020 prepareForTestWithSantitizeHwaddress,
2021 )
Jooyung Han03b51852020-02-26 22:45:42 +09002022 expectLink := func(from, from_variant, to, to_variant string) {
2023 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2024 libFlags := ld.Args["libFlags"]
2025 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2026 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002027 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002028}
2029
Jooyung Han75568392020-03-20 04:29:24 +09002030func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002031 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002032 apex {
2033 name: "myapex",
2034 key: "myapex.key",
2035 native_shared_libs: ["libx"],
2036 min_sdk_version: "29",
2037 }
2038
2039 apex_key {
2040 name: "myapex.key",
2041 public_key: "testkey.avbpubkey",
2042 private_key: "testkey.pem",
2043 }
2044
2045 cc_library {
2046 name: "libx",
2047 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002048 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002049 }
Jooyung Han75568392020-03-20 04:29:24 +09002050 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002051
2052 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002053 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002054 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002055 // note that platform variant is not.
2056 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002057 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002058}
2059
Jooyung Han749dc692020-04-15 11:03:39 +09002060func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2061 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002062 apex {
2063 name: "myapex",
2064 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002065 native_shared_libs: ["mylib"],
2066 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002067 }
2068
2069 apex_key {
2070 name: "myapex.key",
2071 public_key: "testkey.avbpubkey",
2072 private_key: "testkey.pem",
2073 }
Jooyung Han749dc692020-04-15 11:03:39 +09002074
2075 cc_library {
2076 name: "mylib",
2077 srcs: ["mylib.cpp"],
2078 system_shared_libs: [],
2079 stl: "none",
2080 apex_available: [
2081 "myapex",
2082 ],
2083 min_sdk_version: "30",
2084 }
2085 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002086
2087 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2088 apex {
2089 name: "myapex",
2090 key: "myapex.key",
2091 native_shared_libs: ["libfoo.ffi"],
2092 min_sdk_version: "29",
2093 }
2094
2095 apex_key {
2096 name: "myapex.key",
2097 public_key: "testkey.avbpubkey",
2098 private_key: "testkey.pem",
2099 }
2100
2101 rust_ffi_shared {
2102 name: "libfoo.ffi",
2103 srcs: ["foo.rs"],
2104 crate_name: "foo",
2105 apex_available: [
2106 "myapex",
2107 ],
2108 min_sdk_version: "30",
2109 }
2110 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002111
2112 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2113 apex {
2114 name: "myapex",
2115 key: "myapex.key",
2116 java_libs: ["libfoo"],
2117 min_sdk_version: "29",
2118 }
2119
2120 apex_key {
2121 name: "myapex.key",
2122 public_key: "testkey.avbpubkey",
2123 private_key: "testkey.pem",
2124 }
2125
2126 java_import {
2127 name: "libfoo",
2128 jars: ["libfoo.jar"],
2129 apex_available: [
2130 "myapex",
2131 ],
2132 min_sdk_version: "30",
2133 }
2134 `)
Jooyung Han749dc692020-04-15 11:03:39 +09002135}
2136
2137func TestApexMinSdkVersion_Okay(t *testing.T) {
2138 testApex(t, `
2139 apex {
2140 name: "myapex",
2141 key: "myapex.key",
2142 native_shared_libs: ["libfoo"],
2143 java_libs: ["libbar"],
2144 min_sdk_version: "29",
2145 }
2146
2147 apex_key {
2148 name: "myapex.key",
2149 public_key: "testkey.avbpubkey",
2150 private_key: "testkey.pem",
2151 }
2152
2153 cc_library {
2154 name: "libfoo",
2155 srcs: ["mylib.cpp"],
2156 shared_libs: ["libfoo_dep"],
2157 apex_available: ["myapex"],
2158 min_sdk_version: "29",
2159 }
2160
2161 cc_library {
2162 name: "libfoo_dep",
2163 srcs: ["mylib.cpp"],
2164 apex_available: ["myapex"],
2165 min_sdk_version: "29",
2166 }
2167
2168 java_library {
2169 name: "libbar",
2170 sdk_version: "current",
2171 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002172 static_libs: [
2173 "libbar_dep",
2174 "libbar_import_dep",
2175 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002176 apex_available: ["myapex"],
2177 min_sdk_version: "29",
2178 }
2179
2180 java_library {
2181 name: "libbar_dep",
2182 sdk_version: "current",
2183 srcs: ["a.java"],
2184 apex_available: ["myapex"],
2185 min_sdk_version: "29",
2186 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002187
2188 java_import {
2189 name: "libbar_import_dep",
2190 jars: ["libbar.jar"],
2191 apex_available: ["myapex"],
2192 min_sdk_version: "29",
2193 }
Jooyung Han03b51852020-02-26 22:45:42 +09002194 `)
2195}
2196
Artur Satayev8cf899a2020-04-15 17:29:42 +01002197func TestJavaStableSdkVersion(t *testing.T) {
2198 testCases := []struct {
2199 name string
2200 expectedError string
2201 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002202 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002203 }{
2204 {
2205 name: "Non-updatable apex with non-stable dep",
2206 bp: `
2207 apex {
2208 name: "myapex",
2209 java_libs: ["myjar"],
2210 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002211 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002212 }
2213 apex_key {
2214 name: "myapex.key",
2215 public_key: "testkey.avbpubkey",
2216 private_key: "testkey.pem",
2217 }
2218 java_library {
2219 name: "myjar",
2220 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002221 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002222 apex_available: ["myapex"],
2223 }
2224 `,
2225 },
2226 {
2227 name: "Updatable apex with stable dep",
2228 bp: `
2229 apex {
2230 name: "myapex",
2231 java_libs: ["myjar"],
2232 key: "myapex.key",
2233 updatable: true,
2234 min_sdk_version: "29",
2235 }
2236 apex_key {
2237 name: "myapex.key",
2238 public_key: "testkey.avbpubkey",
2239 private_key: "testkey.pem",
2240 }
2241 java_library {
2242 name: "myjar",
2243 srcs: ["foo/bar/MyClass.java"],
2244 sdk_version: "current",
2245 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002246 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002247 }
2248 `,
2249 },
2250 {
2251 name: "Updatable apex with non-stable dep",
2252 expectedError: "cannot depend on \"myjar\"",
2253 bp: `
2254 apex {
2255 name: "myapex",
2256 java_libs: ["myjar"],
2257 key: "myapex.key",
2258 updatable: true,
2259 }
2260 apex_key {
2261 name: "myapex.key",
2262 public_key: "testkey.avbpubkey",
2263 private_key: "testkey.pem",
2264 }
2265 java_library {
2266 name: "myjar",
2267 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002268 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002269 apex_available: ["myapex"],
2270 }
2271 `,
2272 },
2273 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002274 name: "Updatable apex with non-stable legacy core platform dep",
2275 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2276 bp: `
2277 apex {
2278 name: "myapex",
2279 java_libs: ["myjar-uses-legacy"],
2280 key: "myapex.key",
2281 updatable: true,
2282 }
2283 apex_key {
2284 name: "myapex.key",
2285 public_key: "testkey.avbpubkey",
2286 private_key: "testkey.pem",
2287 }
2288 java_library {
2289 name: "myjar-uses-legacy",
2290 srcs: ["foo/bar/MyClass.java"],
2291 sdk_version: "core_platform",
2292 apex_available: ["myapex"],
2293 }
2294 `,
2295 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2296 },
2297 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002298 name: "Updatable apex with non-stable transitive dep",
2299 // This is not actually detecting that the transitive dependency is unstable, rather it is
2300 // detecting that the transitive dependency is building against a wider API surface than the
2301 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002302 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002303 bp: `
2304 apex {
2305 name: "myapex",
2306 java_libs: ["myjar"],
2307 key: "myapex.key",
2308 updatable: true,
2309 }
2310 apex_key {
2311 name: "myapex.key",
2312 public_key: "testkey.avbpubkey",
2313 private_key: "testkey.pem",
2314 }
2315 java_library {
2316 name: "myjar",
2317 srcs: ["foo/bar/MyClass.java"],
2318 sdk_version: "current",
2319 apex_available: ["myapex"],
2320 static_libs: ["transitive-jar"],
2321 }
2322 java_library {
2323 name: "transitive-jar",
2324 srcs: ["foo/bar/MyClass.java"],
2325 sdk_version: "core_platform",
2326 apex_available: ["myapex"],
2327 }
2328 `,
2329 },
2330 }
2331
2332 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002333 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2334 continue
2335 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002336 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002337 errorHandler := android.FixtureExpectsNoErrors
2338 if test.expectedError != "" {
2339 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002340 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002341 android.GroupFixturePreparers(
2342 java.PrepareForTestWithJavaDefaultModules,
2343 PrepareForTestWithApexBuildComponents,
2344 prepareForTestWithMyapex,
2345 android.OptionalFixturePreparer(test.preparer),
2346 ).
2347 ExtendWithErrorHandler(errorHandler).
2348 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002349 })
2350 }
2351}
2352
Jooyung Han749dc692020-04-15 11:03:39 +09002353func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2354 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2355 apex {
2356 name: "myapex",
2357 key: "myapex.key",
2358 native_shared_libs: ["mylib"],
2359 min_sdk_version: "29",
2360 }
2361
2362 apex_key {
2363 name: "myapex.key",
2364 public_key: "testkey.avbpubkey",
2365 private_key: "testkey.pem",
2366 }
2367
2368 cc_library {
2369 name: "mylib",
2370 srcs: ["mylib.cpp"],
2371 shared_libs: ["mylib2"],
2372 system_shared_libs: [],
2373 stl: "none",
2374 apex_available: [
2375 "myapex",
2376 ],
2377 min_sdk_version: "29",
2378 }
2379
2380 // indirect part of the apex
2381 cc_library {
2382 name: "mylib2",
2383 srcs: ["mylib.cpp"],
2384 system_shared_libs: [],
2385 stl: "none",
2386 apex_available: [
2387 "myapex",
2388 ],
2389 min_sdk_version: "30",
2390 }
2391 `)
2392}
2393
2394func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2395 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2396 apex {
2397 name: "myapex",
2398 key: "myapex.key",
2399 apps: ["AppFoo"],
2400 min_sdk_version: "29",
2401 }
2402
2403 apex_key {
2404 name: "myapex.key",
2405 public_key: "testkey.avbpubkey",
2406 private_key: "testkey.pem",
2407 }
2408
2409 android_app {
2410 name: "AppFoo",
2411 srcs: ["foo/bar/MyClass.java"],
2412 sdk_version: "current",
2413 min_sdk_version: "29",
2414 system_modules: "none",
2415 stl: "none",
2416 static_libs: ["bar"],
2417 apex_available: [ "myapex" ],
2418 }
2419
2420 java_library {
2421 name: "bar",
2422 sdk_version: "current",
2423 srcs: ["a.java"],
2424 apex_available: [ "myapex" ],
2425 }
2426 `)
2427}
2428
2429func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002430 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002431 apex {
2432 name: "myapex",
2433 key: "myapex.key",
2434 native_shared_libs: ["mylib"],
2435 min_sdk_version: "29",
2436 }
2437
2438 apex_key {
2439 name: "myapex.key",
2440 public_key: "testkey.avbpubkey",
2441 private_key: "testkey.pem",
2442 }
2443
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002444 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002445 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2446 cc_library {
2447 name: "mylib",
2448 srcs: ["mylib.cpp"],
2449 shared_libs: ["mylib2"],
2450 system_shared_libs: [],
2451 stl: "none",
2452 apex_available: ["myapex", "otherapex"],
2453 min_sdk_version: "29",
2454 }
2455
2456 cc_library {
2457 name: "mylib2",
2458 srcs: ["mylib.cpp"],
2459 system_shared_libs: [],
2460 stl: "none",
2461 apex_available: ["otherapex"],
2462 stubs: { versions: ["29", "30"] },
2463 min_sdk_version: "30",
2464 }
2465
2466 apex {
2467 name: "otherapex",
2468 key: "myapex.key",
2469 native_shared_libs: ["mylib", "mylib2"],
2470 min_sdk_version: "30",
2471 }
2472 `)
2473 expectLink := func(from, from_variant, to, to_variant string) {
2474 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2475 libFlags := ld.Args["libFlags"]
2476 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2477 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002478 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002479 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002480}
2481
Jooyung Haned124c32021-01-26 11:43:46 +09002482func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002483 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2484 func(variables android.FixtureProductVariables) {
2485 variables.Platform_sdk_codename = proptools.StringPtr("S")
2486 variables.Platform_version_active_codenames = []string{"S"}
2487 },
2488 )
Jooyung Haned124c32021-01-26 11:43:46 +09002489 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2490 apex {
2491 name: "myapex",
2492 key: "myapex.key",
2493 native_shared_libs: ["libfoo"],
2494 min_sdk_version: "S",
2495 }
2496 apex_key {
2497 name: "myapex.key",
2498 public_key: "testkey.avbpubkey",
2499 private_key: "testkey.pem",
2500 }
2501 cc_library {
2502 name: "libfoo",
2503 shared_libs: ["libbar"],
2504 apex_available: ["myapex"],
2505 min_sdk_version: "29",
2506 }
2507 cc_library {
2508 name: "libbar",
2509 apex_available: ["myapex"],
2510 }
2511 `, withSAsActiveCodeNames)
2512}
2513
2514func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002515 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2516 variables.Platform_sdk_codename = proptools.StringPtr("S")
2517 variables.Platform_version_active_codenames = []string{"S", "T"}
2518 })
Colin Cross1c460562021-02-16 17:55:47 -08002519 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002520 apex {
2521 name: "myapex",
2522 key: "myapex.key",
2523 native_shared_libs: ["libfoo"],
2524 min_sdk_version: "S",
2525 }
2526 apex_key {
2527 name: "myapex.key",
2528 public_key: "testkey.avbpubkey",
2529 private_key: "testkey.pem",
2530 }
2531 cc_library {
2532 name: "libfoo",
2533 shared_libs: ["libbar"],
2534 apex_available: ["myapex"],
2535 min_sdk_version: "S",
2536 }
2537 cc_library {
2538 name: "libbar",
2539 stubs: {
2540 symbol_file: "libbar.map.txt",
2541 versions: ["30", "S", "T"],
2542 },
2543 }
2544 `, withSAsActiveCodeNames)
2545
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002546 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002547 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2548 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002549 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002550}
2551
Jiyong Park7c2ee712018-12-07 00:42:25 +09002552func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002553 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002554 apex {
2555 name: "myapex",
2556 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002557 native_shared_libs: ["mylib"],
2558 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002559 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002560 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002561 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002562 }
2563
2564 apex_key {
2565 name: "myapex.key",
2566 public_key: "testkey.avbpubkey",
2567 private_key: "testkey.pem",
2568 }
2569
2570 prebuilt_etc {
2571 name: "myetc",
2572 src: "myprebuilt",
2573 sub_dir: "foo/bar",
2574 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002575
2576 cc_library {
2577 name: "mylib",
2578 srcs: ["mylib.cpp"],
2579 relative_install_path: "foo/bar",
2580 system_shared_libs: [],
2581 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002582 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002583 }
2584
2585 cc_binary {
2586 name: "mybin",
2587 srcs: ["mylib.cpp"],
2588 relative_install_path: "foo/bar",
2589 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002590 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002591 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002592 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002593 `)
2594
Sundong Ahnabb64432019-10-22 13:58:29 +09002595 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002596 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002597
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002598 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002599 ensureContains(t, cmd, "/etc ")
2600 ensureContains(t, cmd, "/etc/foo ")
2601 ensureContains(t, cmd, "/etc/foo/bar ")
2602 ensureContains(t, cmd, "/lib64 ")
2603 ensureContains(t, cmd, "/lib64/foo ")
2604 ensureContains(t, cmd, "/lib64/foo/bar ")
2605 ensureContains(t, cmd, "/lib ")
2606 ensureContains(t, cmd, "/lib/foo ")
2607 ensureContains(t, cmd, "/lib/foo/bar ")
2608 ensureContains(t, cmd, "/bin ")
2609 ensureContains(t, cmd, "/bin/foo ")
2610 ensureContains(t, cmd, "/bin/foo/bar ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002611}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002612
Jooyung Han35155c42020-02-06 17:33:20 +09002613func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002614 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002615 apex {
2616 name: "myapex",
2617 key: "myapex.key",
2618 multilib: {
2619 both: {
2620 native_shared_libs: ["mylib"],
2621 binaries: ["mybin"],
2622 },
2623 },
2624 compile_multilib: "both",
2625 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002626 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002627 }
2628
2629 apex_key {
2630 name: "myapex.key",
2631 public_key: "testkey.avbpubkey",
2632 private_key: "testkey.pem",
2633 }
2634
2635 cc_library {
2636 name: "mylib",
2637 relative_install_path: "foo/bar",
2638 system_shared_libs: [],
2639 stl: "none",
2640 apex_available: [ "myapex" ],
2641 native_bridge_supported: true,
2642 }
2643
2644 cc_binary {
2645 name: "mybin",
2646 relative_install_path: "foo/bar",
2647 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002648 stl: "none",
2649 apex_available: [ "myapex" ],
2650 native_bridge_supported: true,
2651 compile_multilib: "both", // default is "first" for binary
2652 multilib: {
2653 lib64: {
2654 suffix: "64",
2655 },
2656 },
2657 }
2658 `, withNativeBridgeEnabled)
2659 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2660 "bin/foo/bar/mybin",
2661 "bin/foo/bar/mybin64",
2662 "bin/arm/foo/bar/mybin",
2663 "bin/arm64/foo/bar/mybin64",
2664 "lib/foo/bar/mylib.so",
2665 "lib/arm/foo/bar/mylib.so",
2666 "lib64/foo/bar/mylib.so",
2667 "lib64/arm64/foo/bar/mylib.so",
2668 })
2669}
2670
Jooyung Han85d61762020-06-24 23:50:26 +09002671func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08002672 result := android.GroupFixturePreparers(
2673 prepareForApexTest,
2674 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
2675 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002676 apex {
2677 name: "myapex",
2678 key: "myapex.key",
2679 binaries: ["mybin"],
2680 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002681 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002682 }
2683 apex_key {
2684 name: "myapex.key",
2685 public_key: "testkey.avbpubkey",
2686 private_key: "testkey.pem",
2687 }
2688 cc_binary {
2689 name: "mybin",
2690 vendor: true,
2691 shared_libs: ["libfoo"],
2692 }
2693 cc_library {
2694 name: "libfoo",
2695 proprietary: true,
2696 }
2697 `)
2698
Colin Crossc68db4b2021-11-11 18:59:15 -08002699 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09002700 "bin/mybin",
2701 "lib64/libfoo.so",
2702 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2703 "lib64/libc++.so",
2704 })
2705
Colin Crossc68db4b2021-11-11 18:59:15 -08002706 apexBundle := result.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2707 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002708 name := apexBundle.BaseModuleName()
2709 prefix := "TARGET_"
2710 var builder strings.Builder
2711 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08002712 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01002713 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002714 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002715
Colin Crossc68db4b2021-11-11 18:59:15 -08002716 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002717 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2718 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002719}
2720
Jooyung Hanc5a96762022-02-04 11:54:50 +09002721func TestVendorApex_use_vndk_as_stable_TryingToIncludeVNDKLib(t *testing.T) {
2722 testApexError(t, `Trying to include a VNDK library`, `
2723 apex {
2724 name: "myapex",
2725 key: "myapex.key",
2726 native_shared_libs: ["libc++"], // libc++ is a VNDK lib
2727 vendor: true,
2728 use_vndk_as_stable: true,
2729 updatable: false,
2730 }
2731 apex_key {
2732 name: "myapex.key",
2733 public_key: "testkey.avbpubkey",
2734 private_key: "testkey.pem",
2735 }`)
2736}
2737
Jooyung Handf78e212020-07-22 15:54:47 +09002738func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Jooyung Han91f92032022-02-04 12:36:33 +09002739 // myapex myapex2
2740 // | |
2741 // mybin ------. mybin2
2742 // \ \ / |
2743 // (stable) .---\--------` |
2744 // \ / \ |
2745 // \ / \ /
2746 // libvndk libvendor
2747 // (vndk)
Colin Cross1c460562021-02-16 17:55:47 -08002748 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002749 apex {
2750 name: "myapex",
2751 key: "myapex.key",
2752 binaries: ["mybin"],
2753 vendor: true,
2754 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002755 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002756 }
2757 apex_key {
2758 name: "myapex.key",
2759 public_key: "testkey.avbpubkey",
2760 private_key: "testkey.pem",
2761 }
2762 cc_binary {
2763 name: "mybin",
2764 vendor: true,
2765 shared_libs: ["libvndk", "libvendor"],
2766 }
2767 cc_library {
2768 name: "libvndk",
2769 vndk: {
2770 enabled: true,
2771 },
2772 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002773 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002774 }
2775 cc_library {
2776 name: "libvendor",
2777 vendor: true,
Jooyung Han91f92032022-02-04 12:36:33 +09002778 stl: "none",
2779 }
2780 apex {
2781 name: "myapex2",
2782 key: "myapex.key",
2783 binaries: ["mybin2"],
2784 vendor: true,
2785 use_vndk_as_stable: false,
2786 updatable: false,
2787 }
2788 cc_binary {
2789 name: "mybin2",
2790 vendor: true,
2791 shared_libs: ["libvndk", "libvendor"],
Jooyung Handf78e212020-07-22 15:54:47 +09002792 }
2793 `)
2794
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002795 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002796
Jooyung Han91f92032022-02-04 12:36:33 +09002797 for _, tc := range []struct {
2798 name string
2799 apexName string
2800 moduleName string
2801 moduleVariant string
2802 libs []string
2803 contents []string
2804 requireVndkNamespace bool
2805 }{
2806 {
2807 name: "use_vndk_as_stable",
2808 apexName: "myapex",
2809 moduleName: "mybin",
2810 moduleVariant: vendorVariant + "_apex10000",
2811 libs: []string{
2812 // should link with vendor variants of VNDK libs(libvndk/libc++)
2813 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared/libvndk.so",
2814 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared/libc++.so",
2815 // unstable Vendor libs as APEX variant
2816 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2817 },
2818 contents: []string{
2819 "bin/mybin",
2820 "lib64/libvendor.so",
2821 // VNDK libs (libvndk/libc++) are not included
2822 },
2823 requireVndkNamespace: true,
2824 },
2825 {
2826 name: "!use_vndk_as_stable",
2827 apexName: "myapex2",
2828 moduleName: "mybin2",
2829 moduleVariant: vendorVariant + "_myapex2",
2830 libs: []string{
2831 // should link with "unique" APEX(myapex2) variant of VNDK libs(libvndk/libc++)
2832 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared_myapex2/libvndk.so",
2833 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared_myapex2/libc++.so",
2834 // unstable vendor libs have "merged" APEX variants
2835 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2836 },
2837 contents: []string{
2838 "bin/mybin2",
2839 "lib64/libvendor.so",
2840 // VNDK libs are included as well
2841 "lib64/libvndk.so",
2842 "lib64/libc++.so",
2843 },
2844 requireVndkNamespace: false,
2845 },
2846 } {
2847 t.Run(tc.name, func(t *testing.T) {
2848 // Check linked libs
2849 ldRule := ctx.ModuleForTests(tc.moduleName, tc.moduleVariant).Rule("ld")
2850 libs := names(ldRule.Args["libFlags"])
2851 for _, lib := range tc.libs {
2852 ensureListContains(t, libs, lib)
2853 }
2854 // Check apex contents
2855 ensureExactContents(t, ctx, tc.apexName, "android_common_"+tc.apexName+"_image", tc.contents)
Jooyung Handf78e212020-07-22 15:54:47 +09002856
Jooyung Han91f92032022-02-04 12:36:33 +09002857 // Check "requireNativeLibs"
2858 apexManifestRule := ctx.ModuleForTests(tc.apexName, "android_common_"+tc.apexName+"_image").Rule("apexManifestRule")
2859 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2860 if tc.requireVndkNamespace {
2861 ensureListContains(t, requireNativeLibs, ":vndk")
2862 } else {
2863 ensureListNotContains(t, requireNativeLibs, ":vndk")
2864 }
2865 })
2866 }
Jooyung Handf78e212020-07-22 15:54:47 +09002867}
2868
Justin Yun13decfb2021-03-08 19:25:55 +09002869func TestProductVariant(t *testing.T) {
2870 ctx := testApex(t, `
2871 apex {
2872 name: "myapex",
2873 key: "myapex.key",
2874 updatable: false,
2875 product_specific: true,
2876 binaries: ["foo"],
2877 }
2878
2879 apex_key {
2880 name: "myapex.key",
2881 public_key: "testkey.avbpubkey",
2882 private_key: "testkey.pem",
2883 }
2884
2885 cc_binary {
2886 name: "foo",
2887 product_available: true,
2888 apex_available: ["myapex"],
2889 srcs: ["foo.cpp"],
2890 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002891 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2892 variables.ProductVndkVersion = proptools.StringPtr("current")
2893 }),
2894 )
Justin Yun13decfb2021-03-08 19:25:55 +09002895
2896 cflags := strings.Fields(
Jooyung Han91f92032022-02-04 12:36:33 +09002897 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_myapex").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002898 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2899 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2900 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2901 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2902}
2903
Jooyung Han8e5685d2020-09-21 11:02:57 +09002904func TestApex_withPrebuiltFirmware(t *testing.T) {
2905 testCases := []struct {
2906 name string
2907 additionalProp string
2908 }{
2909 {"system apex with prebuilt_firmware", ""},
2910 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2911 }
2912 for _, tc := range testCases {
2913 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002914 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002915 apex {
2916 name: "myapex",
2917 key: "myapex.key",
2918 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002919 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002920 `+tc.additionalProp+`
2921 }
2922 apex_key {
2923 name: "myapex.key",
2924 public_key: "testkey.avbpubkey",
2925 private_key: "testkey.pem",
2926 }
2927 prebuilt_firmware {
2928 name: "myfirmware",
2929 src: "myfirmware.bin",
2930 filename_from_src: true,
2931 `+tc.additionalProp+`
2932 }
2933 `)
2934 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2935 "etc/firmware/myfirmware.bin",
2936 })
2937 })
2938 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002939}
2940
Jooyung Hanefb184e2020-06-25 17:14:25 +09002941func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002942 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002943 apex {
2944 name: "myapex",
2945 key: "myapex.key",
2946 vendor: true,
2947 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002948 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002949 }
2950
2951 apex_key {
2952 name: "myapex.key",
2953 public_key: "testkey.avbpubkey",
2954 private_key: "testkey.pem",
2955 }
2956
2957 cc_library {
2958 name: "mylib",
2959 vendor_available: true,
2960 }
2961 `)
2962
2963 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002964 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002965 name := apexBundle.BaseModuleName()
2966 prefix := "TARGET_"
2967 var builder strings.Builder
2968 data.Custom(&builder, name, prefix, "", data)
2969 androidMk := builder.String()
2970 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2971}
2972
Jooyung Han2ed99d02020-06-24 23:26:26 +09002973func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002974 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002975 apex {
2976 name: "myapex",
2977 key: "myapex.key",
2978 vintf_fragments: ["fragment.xml"],
2979 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002980 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002981 }
2982 apex_key {
2983 name: "myapex.key",
2984 public_key: "testkey.avbpubkey",
2985 private_key: "testkey.pem",
2986 }
2987 cc_binary {
2988 name: "mybin",
2989 }
2990 `)
2991
2992 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002993 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002994 name := apexBundle.BaseModuleName()
2995 prefix := "TARGET_"
2996 var builder strings.Builder
2997 data.Custom(&builder, name, prefix, "", data)
2998 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04002999 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003000 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003001}
3002
Jiyong Park16e91a02018-12-20 18:18:08 +09003003func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003004 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003005 apex {
3006 name: "myapex",
3007 key: "myapex.key",
3008 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003009 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003010 }
3011
3012 apex_key {
3013 name: "myapex.key",
3014 public_key: "testkey.avbpubkey",
3015 private_key: "testkey.pem",
3016 }
3017
3018 cc_library {
3019 name: "mylib",
3020 srcs: ["mylib.cpp"],
3021 system_shared_libs: [],
3022 stl: "none",
3023 stubs: {
3024 versions: ["1", "2", "3"],
3025 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003026 apex_available: [
3027 "//apex_available:platform",
3028 "myapex",
3029 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09003030 }
3031
3032 cc_binary {
3033 name: "not_in_apex",
3034 srcs: ["mylib.cpp"],
3035 static_libs: ["mylib"],
3036 static_executable: true,
3037 system_shared_libs: [],
3038 stl: "none",
3039 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003040 `)
3041
Colin Cross7113d202019-11-20 16:39:12 -08003042 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003043
3044 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003045 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003046}
Jiyong Park9335a262018-12-24 11:31:58 +09003047
3048func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003049 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003050 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003051 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003052 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003053 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003054 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003055 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003056 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003057 }
3058
3059 cc_library {
3060 name: "mylib",
3061 srcs: ["mylib.cpp"],
3062 system_shared_libs: [],
3063 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003064 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003065 }
3066
3067 apex_key {
3068 name: "myapex.key",
3069 public_key: "testkey.avbpubkey",
3070 private_key: "testkey.pem",
3071 }
3072
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003073 android_app_certificate {
3074 name: "myapex.certificate",
3075 certificate: "testkey",
3076 }
3077
3078 android_app_certificate {
3079 name: "myapex.certificate.override",
3080 certificate: "testkey.override",
3081 }
3082
Jiyong Park9335a262018-12-24 11:31:58 +09003083 `)
3084
3085 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003086 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003087
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003088 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3089 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003090 "vendor/foo/devkeys/testkey.avbpubkey")
3091 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003092 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3093 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003094 "vendor/foo/devkeys/testkey.pem")
3095 }
3096
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003097 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09003098 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003099 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003100 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003101 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003102 }
3103}
Jiyong Park58e364a2019-01-19 19:24:06 +09003104
Jooyung Hanf121a652019-12-17 14:30:11 +09003105func TestCertificate(t *testing.T) {
3106 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003107 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003108 apex {
3109 name: "myapex",
3110 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003111 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003112 }
3113 apex_key {
3114 name: "myapex.key",
3115 public_key: "testkey.avbpubkey",
3116 private_key: "testkey.pem",
3117 }`)
3118 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3119 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3120 if actual := rule.Args["certificates"]; actual != expected {
3121 t.Errorf("certificates should be %q, not %q", expected, actual)
3122 }
3123 })
3124 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003125 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003126 apex {
3127 name: "myapex_keytest",
3128 key: "myapex.key",
3129 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003130 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003131 }
3132 apex_key {
3133 name: "myapex.key",
3134 public_key: "testkey.avbpubkey",
3135 private_key: "testkey.pem",
3136 }
3137 android_app_certificate {
3138 name: "myapex.certificate.override",
3139 certificate: "testkey.override",
3140 }`)
3141 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3142 expected := "testkey.override.x509.pem testkey.override.pk8"
3143 if actual := rule.Args["certificates"]; actual != expected {
3144 t.Errorf("certificates should be %q, not %q", expected, actual)
3145 }
3146 })
3147 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003148 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003149 apex {
3150 name: "myapex",
3151 key: "myapex.key",
3152 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003153 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003154 }
3155 apex_key {
3156 name: "myapex.key",
3157 public_key: "testkey.avbpubkey",
3158 private_key: "testkey.pem",
3159 }
3160 android_app_certificate {
3161 name: "myapex.certificate",
3162 certificate: "testkey",
3163 }`)
3164 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3165 expected := "testkey.x509.pem testkey.pk8"
3166 if actual := rule.Args["certificates"]; actual != expected {
3167 t.Errorf("certificates should be %q, not %q", expected, actual)
3168 }
3169 })
3170 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003171 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003172 apex {
3173 name: "myapex_keytest",
3174 key: "myapex.key",
3175 file_contexts: ":myapex-file_contexts",
3176 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003177 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003178 }
3179 apex_key {
3180 name: "myapex.key",
3181 public_key: "testkey.avbpubkey",
3182 private_key: "testkey.pem",
3183 }
3184 android_app_certificate {
3185 name: "myapex.certificate.override",
3186 certificate: "testkey.override",
3187 }`)
3188 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3189 expected := "testkey.override.x509.pem testkey.override.pk8"
3190 if actual := rule.Args["certificates"]; actual != expected {
3191 t.Errorf("certificates should be %q, not %q", expected, actual)
3192 }
3193 })
3194 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003195 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003196 apex {
3197 name: "myapex",
3198 key: "myapex.key",
3199 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003200 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003201 }
3202 apex_key {
3203 name: "myapex.key",
3204 public_key: "testkey.avbpubkey",
3205 private_key: "testkey.pem",
3206 }`)
3207 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3208 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3209 if actual := rule.Args["certificates"]; actual != expected {
3210 t.Errorf("certificates should be %q, not %q", expected, actual)
3211 }
3212 })
3213 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003214 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003215 apex {
3216 name: "myapex_keytest",
3217 key: "myapex.key",
3218 file_contexts: ":myapex-file_contexts",
3219 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003220 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003221 }
3222 apex_key {
3223 name: "myapex.key",
3224 public_key: "testkey.avbpubkey",
3225 private_key: "testkey.pem",
3226 }
3227 android_app_certificate {
3228 name: "myapex.certificate.override",
3229 certificate: "testkey.override",
3230 }`)
3231 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3232 expected := "testkey.override.x509.pem testkey.override.pk8"
3233 if actual := rule.Args["certificates"]; actual != expected {
3234 t.Errorf("certificates should be %q, not %q", expected, actual)
3235 }
3236 })
3237}
3238
Jiyong Park58e364a2019-01-19 19:24:06 +09003239func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003240 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003241 apex {
3242 name: "myapex",
3243 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003244 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003245 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003246 }
3247
3248 apex {
3249 name: "otherapex",
3250 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003251 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003252 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003253 }
3254
3255 apex_key {
3256 name: "myapex.key",
3257 public_key: "testkey.avbpubkey",
3258 private_key: "testkey.pem",
3259 }
3260
3261 cc_library {
3262 name: "mylib",
3263 srcs: ["mylib.cpp"],
3264 system_shared_libs: [],
3265 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003266 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003267 "myapex",
3268 "otherapex",
3269 ],
Jooyung Han24282772020-03-21 23:20:55 +09003270 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003271 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003272 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003273 cc_library {
3274 name: "mylib2",
3275 srcs: ["mylib.cpp"],
3276 system_shared_libs: [],
3277 stl: "none",
3278 apex_available: [
3279 "myapex",
3280 "otherapex",
3281 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003282 static_libs: ["mylib3"],
3283 recovery_available: true,
3284 min_sdk_version: "29",
3285 }
3286 cc_library {
3287 name: "mylib3",
3288 srcs: ["mylib.cpp"],
3289 system_shared_libs: [],
3290 stl: "none",
3291 apex_available: [
3292 "myapex",
3293 "otherapex",
3294 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003295 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003296 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003297 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003298 `)
3299
Jooyung Hanc87a0592020-03-02 17:44:33 +09003300 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003301 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003302 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003303 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003304
Jooyung Hanccce2f22020-03-07 03:45:53 +09003305 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003306 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003307 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003308 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003309
Jooyung Hanccce2f22020-03-07 03:45:53 +09003310 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003311 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003312 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003313 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jiyong Park58e364a2019-01-19 19:24:06 +09003314
Colin Crossaede88c2020-08-11 12:17:01 -07003315 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3316 // each variant defines additional macros to distinguish which apex variant it is built for
3317
3318 // non-APEX variant does not have __ANDROID_APEX__ defined
3319 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3320 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3321
Dan Albertb19953d2020-11-17 15:29:36 -08003322 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003323 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3324 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003325 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003326
Jooyung Hanc87a0592020-03-02 17:44:33 +09003327 // non-APEX variant does not have __ANDROID_APEX__ defined
3328 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3329 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3330
Dan Albertb19953d2020-11-17 15:29:36 -08003331 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003332 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003333 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003334 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003335}
Jiyong Park7e636d02019-01-28 16:16:54 +09003336
3337func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003338 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003339 apex {
3340 name: "myapex",
3341 key: "myapex.key",
3342 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003343 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003344 }
3345
3346 apex_key {
3347 name: "myapex.key",
3348 public_key: "testkey.avbpubkey",
3349 private_key: "testkey.pem",
3350 }
3351
3352 cc_library_headers {
3353 name: "mylib_headers",
3354 export_include_dirs: ["my_include"],
3355 system_shared_libs: [],
3356 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003357 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003358 }
3359
3360 cc_library {
3361 name: "mylib",
3362 srcs: ["mylib.cpp"],
3363 system_shared_libs: [],
3364 stl: "none",
3365 header_libs: ["mylib_headers"],
3366 export_header_lib_headers: ["mylib_headers"],
3367 stubs: {
3368 versions: ["1", "2", "3"],
3369 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003370 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003371 }
3372
3373 cc_library {
3374 name: "otherlib",
3375 srcs: ["mylib.cpp"],
3376 system_shared_libs: [],
3377 stl: "none",
3378 shared_libs: ["mylib"],
3379 }
3380 `)
3381
Colin Cross7113d202019-11-20 16:39:12 -08003382 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003383
3384 // Ensure that the include path of the header lib is exported to 'otherlib'
3385 ensureContains(t, cFlags, "-Imy_include")
3386}
Alex Light9670d332019-01-29 18:07:33 -08003387
Jiyong Park7cd10e32020-01-14 09:22:18 +09003388type fileInApex struct {
3389 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003390 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003391 isLink bool
3392}
3393
Jooyung Hana57af4a2020-01-23 05:36:59 +00003394func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003395 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003396 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003397 copyCmds := apexRule.Args["copy_commands"]
3398 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003399 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003400 for _, cmd := range strings.Split(copyCmds, "&&") {
3401 cmd = strings.TrimSpace(cmd)
3402 if cmd == "" {
3403 continue
3404 }
3405 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003406 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003407 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003408 switch terms[0] {
3409 case "mkdir":
3410 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003411 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003412 t.Fatal("copyCmds contains invalid cp command", cmd)
3413 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003414 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003415 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003416 isLink = false
3417 case "ln":
3418 if len(terms) != 3 && len(terms) != 4 {
3419 // ln LINK TARGET or ln -s LINK TARGET
3420 t.Fatal("copyCmds contains invalid ln command", cmd)
3421 }
3422 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003423 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003424 isLink = true
3425 default:
3426 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3427 }
3428 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003429 index := strings.Index(dst, imageApexDir)
3430 if index == -1 {
3431 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3432 }
3433 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003434 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003435 }
3436 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003437 return ret
3438}
3439
Jooyung Hana57af4a2020-01-23 05:36:59 +00003440func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3441 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003442 var failed bool
3443 var surplus []string
3444 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003445 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003446 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003447 for _, expected := range files {
3448 if matched, _ := path.Match(expected, file.path); matched {
3449 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003450 mactchFound = true
3451 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003452 }
3453 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003454 if !mactchFound {
3455 surplus = append(surplus, file.path)
3456 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003457 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003458
Jooyung Han31c470b2019-10-18 16:26:59 +09003459 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003460 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003461 t.Log("surplus files", surplus)
3462 failed = true
3463 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003464
3465 if len(files) > len(filesMatched) {
3466 var missing []string
3467 for _, expected := range files {
3468 if !filesMatched[expected] {
3469 missing = append(missing, expected)
3470 }
3471 }
3472 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003473 t.Log("missing files", missing)
3474 failed = true
3475 }
3476 if failed {
3477 t.Fail()
3478 }
3479}
3480
Jooyung Han344d5432019-08-23 11:17:39 +09003481func TestVndkApexCurrent(t *testing.T) {
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003482 commonFiles := []string{
Jooyung Hane6436d72020-02-27 13:31:56 +09003483 "lib/libc++.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003484 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003485 "etc/llndk.libraries.29.txt",
3486 "etc/vndkcore.libraries.29.txt",
3487 "etc/vndksp.libraries.29.txt",
3488 "etc/vndkprivate.libraries.29.txt",
3489 "etc/vndkproduct.libraries.29.txt",
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003490 }
3491 testCases := []struct {
3492 vndkVersion string
3493 expectedFiles []string
3494 }{
3495 {
3496 vndkVersion: "current",
3497 expectedFiles: append(commonFiles,
3498 "lib/libvndk.so",
3499 "lib/libvndksp.so",
3500 "lib64/libvndk.so",
3501 "lib64/libvndksp.so"),
3502 },
3503 {
3504 vndkVersion: "",
3505 expectedFiles: append(commonFiles,
3506 // Legacy VNDK APEX contains only VNDK-SP files (of core variant)
3507 "lib/libvndksp.so",
3508 "lib64/libvndksp.so"),
3509 },
3510 }
3511 for _, tc := range testCases {
3512 t.Run("VNDK.current with DeviceVndkVersion="+tc.vndkVersion, func(t *testing.T) {
3513 ctx := testApex(t, `
3514 apex_vndk {
3515 name: "com.android.vndk.current",
3516 key: "com.android.vndk.current.key",
3517 updatable: false,
3518 }
3519
3520 apex_key {
3521 name: "com.android.vndk.current.key",
3522 public_key: "testkey.avbpubkey",
3523 private_key: "testkey.pem",
3524 }
3525
3526 cc_library {
3527 name: "libvndk",
3528 srcs: ["mylib.cpp"],
3529 vendor_available: true,
3530 product_available: true,
3531 vndk: {
3532 enabled: true,
3533 },
3534 system_shared_libs: [],
3535 stl: "none",
3536 apex_available: [ "com.android.vndk.current" ],
3537 }
3538
3539 cc_library {
3540 name: "libvndksp",
3541 srcs: ["mylib.cpp"],
3542 vendor_available: true,
3543 product_available: true,
3544 vndk: {
3545 enabled: true,
3546 support_system_process: true,
3547 },
3548 system_shared_libs: [],
3549 stl: "none",
3550 apex_available: [ "com.android.vndk.current" ],
3551 }
3552
3553 // VNDK-Ext should not cause any problems
3554
3555 cc_library {
3556 name: "libvndk.ext",
3557 srcs: ["mylib2.cpp"],
3558 vendor: true,
3559 vndk: {
3560 enabled: true,
3561 extends: "libvndk",
3562 },
3563 system_shared_libs: [],
3564 stl: "none",
3565 }
3566
3567 cc_library {
3568 name: "libvndksp.ext",
3569 srcs: ["mylib2.cpp"],
3570 vendor: true,
3571 vndk: {
3572 enabled: true,
3573 support_system_process: true,
3574 extends: "libvndksp",
3575 },
3576 system_shared_libs: [],
3577 stl: "none",
3578 }
3579 `+vndkLibrariesTxtFiles("current"), android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3580 variables.DeviceVndkVersion = proptools.StringPtr(tc.vndkVersion)
3581 }))
3582 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", tc.expectedFiles)
3583 })
3584 }
Jooyung Han344d5432019-08-23 11:17:39 +09003585}
3586
3587func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003588 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003589 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003590 name: "com.android.vndk.current",
3591 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003592 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003593 }
3594
3595 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003596 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003597 public_key: "testkey.avbpubkey",
3598 private_key: "testkey.pem",
3599 }
3600
3601 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003602 name: "libvndk",
3603 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003604 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003605 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003606 vndk: {
3607 enabled: true,
3608 },
3609 system_shared_libs: [],
3610 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003611 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003612 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003613
3614 cc_prebuilt_library_shared {
3615 name: "libvndk.arm",
3616 srcs: ["libvndk.arm.so"],
3617 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003618 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003619 vndk: {
3620 enabled: true,
3621 },
3622 enabled: false,
3623 arch: {
3624 arm: {
3625 enabled: true,
3626 },
3627 },
3628 system_shared_libs: [],
3629 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003630 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003631 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003632 `+vndkLibrariesTxtFiles("current"),
3633 withFiles(map[string][]byte{
3634 "libvndk.so": nil,
3635 "libvndk.arm.so": nil,
3636 }))
Colin Cross2807f002021-03-02 10:15:29 -08003637 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003638 "lib/libvndk.so",
3639 "lib/libvndk.arm.so",
3640 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003641 "lib/libc++.so",
3642 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003643 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003644 })
Jooyung Han344d5432019-08-23 11:17:39 +09003645}
3646
Jooyung Han39edb6c2019-11-06 16:53:07 +09003647func vndkLibrariesTxtFiles(vers ...string) (result string) {
3648 for _, v := range vers {
3649 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003650 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003651 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003652 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003653 name: "` + txt + `.libraries.txt",
3654 }
3655 `
3656 }
3657 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003658 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003659 result += `
3660 prebuilt_etc {
3661 name: "` + txt + `.libraries.` + v + `.txt",
3662 src: "dummy.txt",
3663 }
3664 `
3665 }
3666 }
3667 }
3668 return
3669}
3670
Jooyung Han344d5432019-08-23 11:17:39 +09003671func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003672 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003673 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003674 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003675 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003676 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003677 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003678 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003679 }
3680
3681 apex_key {
3682 name: "myapex.key",
3683 public_key: "testkey.avbpubkey",
3684 private_key: "testkey.pem",
3685 }
3686
Jooyung Han31c470b2019-10-18 16:26:59 +09003687 vndk_prebuilt_shared {
3688 name: "libvndk27",
3689 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003690 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003691 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003692 vndk: {
3693 enabled: true,
3694 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003695 target_arch: "arm64",
3696 arch: {
3697 arm: {
3698 srcs: ["libvndk27_arm.so"],
3699 },
3700 arm64: {
3701 srcs: ["libvndk27_arm64.so"],
3702 },
3703 },
Colin Cross2807f002021-03-02 10:15:29 -08003704 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003705 }
3706
3707 vndk_prebuilt_shared {
3708 name: "libvndk27",
3709 version: "27",
3710 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003711 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003712 vndk: {
3713 enabled: true,
3714 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003715 target_arch: "x86_64",
3716 arch: {
3717 x86: {
3718 srcs: ["libvndk27_x86.so"],
3719 },
3720 x86_64: {
3721 srcs: ["libvndk27_x86_64.so"],
3722 },
3723 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003724 }
3725 `+vndkLibrariesTxtFiles("27"),
3726 withFiles(map[string][]byte{
3727 "libvndk27_arm.so": nil,
3728 "libvndk27_arm64.so": nil,
3729 "libvndk27_x86.so": nil,
3730 "libvndk27_x86_64.so": nil,
3731 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003732
Colin Cross2807f002021-03-02 10:15:29 -08003733 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003734 "lib/libvndk27_arm.so",
3735 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003736 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003737 })
Jooyung Han344d5432019-08-23 11:17:39 +09003738}
3739
Jooyung Han90eee022019-10-01 20:02:42 +09003740func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003741 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003742 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003743 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003744 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003745 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003746 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003747 }
3748 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003749 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003750 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003751 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003752 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003753 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003754 }
3755 apex_key {
3756 name: "myapex.key",
3757 public_key: "testkey.avbpubkey",
3758 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003759 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003760
3761 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003762 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003763 actual := proptools.String(bundle.properties.Apex_name)
3764 if !reflect.DeepEqual(actual, expected) {
3765 t.Errorf("Got '%v', expected '%v'", actual, expected)
3766 }
3767 }
3768
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003769 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003770 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003771}
3772
Jooyung Han344d5432019-08-23 11:17:39 +09003773func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003774 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003775 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003776 name: "com.android.vndk.current",
3777 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003778 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003779 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003780 }
3781
3782 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003783 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003784 public_key: "testkey.avbpubkey",
3785 private_key: "testkey.pem",
3786 }
3787
3788 cc_library {
3789 name: "libvndk",
3790 srcs: ["mylib.cpp"],
3791 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003792 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003793 native_bridge_supported: true,
3794 host_supported: true,
3795 vndk: {
3796 enabled: true,
3797 },
3798 system_shared_libs: [],
3799 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003800 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003801 }
Colin Cross2807f002021-03-02 10:15:29 -08003802 `+vndkLibrariesTxtFiles("current"),
3803 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003804
Colin Cross2807f002021-03-02 10:15:29 -08003805 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003806 "lib/libvndk.so",
3807 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003808 "lib/libc++.so",
3809 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003810 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003811 })
Jooyung Han344d5432019-08-23 11:17:39 +09003812}
3813
3814func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003815 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003816 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003817 name: "com.android.vndk.current",
3818 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003819 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003820 native_bridge_supported: true,
3821 }
3822
3823 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003824 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003825 public_key: "testkey.avbpubkey",
3826 private_key: "testkey.pem",
3827 }
3828
3829 cc_library {
3830 name: "libvndk",
3831 srcs: ["mylib.cpp"],
3832 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003833 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003834 native_bridge_supported: true,
3835 host_supported: true,
3836 vndk: {
3837 enabled: true,
3838 },
3839 system_shared_libs: [],
3840 stl: "none",
3841 }
3842 `)
3843}
3844
Jooyung Han31c470b2019-10-18 16:26:59 +09003845func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003846 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003847 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003848 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003849 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003850 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003851 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003852 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003853 }
3854
3855 apex_key {
3856 name: "myapex.key",
3857 public_key: "testkey.avbpubkey",
3858 private_key: "testkey.pem",
3859 }
3860
3861 vndk_prebuilt_shared {
3862 name: "libvndk27",
3863 version: "27",
3864 target_arch: "arm",
3865 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003866 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003867 vndk: {
3868 enabled: true,
3869 },
3870 arch: {
3871 arm: {
3872 srcs: ["libvndk27.so"],
3873 }
3874 },
3875 }
3876
3877 vndk_prebuilt_shared {
3878 name: "libvndk27",
3879 version: "27",
3880 target_arch: "arm",
3881 binder32bit: true,
3882 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003883 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003884 vndk: {
3885 enabled: true,
3886 },
3887 arch: {
3888 arm: {
3889 srcs: ["libvndk27binder32.so"],
3890 }
3891 },
Colin Cross2807f002021-03-02 10:15:29 -08003892 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003893 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003894 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003895 withFiles(map[string][]byte{
3896 "libvndk27.so": nil,
3897 "libvndk27binder32.so": nil,
3898 }),
3899 withBinder32bit,
3900 withTargets(map[android.OsType][]android.Target{
3901 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003902 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3903 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003904 },
3905 }),
3906 )
3907
Colin Cross2807f002021-03-02 10:15:29 -08003908 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003909 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003910 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003911 })
3912}
3913
Jooyung Han45a96772020-06-15 14:59:42 +09003914func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003915 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003916 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003917 name: "com.android.vndk.current",
3918 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003919 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003920 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003921 }
3922
3923 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003924 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003925 public_key: "testkey.avbpubkey",
3926 private_key: "testkey.pem",
3927 }
3928
3929 cc_library {
3930 name: "libz",
3931 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003932 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003933 vndk: {
3934 enabled: true,
3935 },
3936 stubs: {
3937 symbol_file: "libz.map.txt",
3938 versions: ["30"],
3939 }
3940 }
3941 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3942 "libz.map.txt": nil,
3943 }))
3944
Colin Cross2807f002021-03-02 10:15:29 -08003945 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003946 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3947 ensureListEmpty(t, provideNativeLibs)
3948}
3949
Jooyung Hane1633032019-08-01 17:41:43 +09003950func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003951 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003952 apex {
3953 name: "myapex_nodep",
3954 key: "myapex.key",
3955 native_shared_libs: ["lib_nodep"],
3956 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003957 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003958 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003959 }
3960
3961 apex {
3962 name: "myapex_dep",
3963 key: "myapex.key",
3964 native_shared_libs: ["lib_dep"],
3965 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003966 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003967 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003968 }
3969
3970 apex {
3971 name: "myapex_provider",
3972 key: "myapex.key",
3973 native_shared_libs: ["libfoo"],
3974 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003975 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003976 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003977 }
3978
3979 apex {
3980 name: "myapex_selfcontained",
3981 key: "myapex.key",
3982 native_shared_libs: ["lib_dep", "libfoo"],
3983 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003984 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003985 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003986 }
3987
3988 apex_key {
3989 name: "myapex.key",
3990 public_key: "testkey.avbpubkey",
3991 private_key: "testkey.pem",
3992 }
3993
3994 cc_library {
3995 name: "lib_nodep",
3996 srcs: ["mylib.cpp"],
3997 system_shared_libs: [],
3998 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003999 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09004000 }
4001
4002 cc_library {
4003 name: "lib_dep",
4004 srcs: ["mylib.cpp"],
4005 shared_libs: ["libfoo"],
4006 system_shared_libs: [],
4007 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004008 apex_available: [
4009 "myapex_dep",
4010 "myapex_provider",
4011 "myapex_selfcontained",
4012 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004013 }
4014
4015 cc_library {
4016 name: "libfoo",
4017 srcs: ["mytest.cpp"],
4018 stubs: {
4019 versions: ["1"],
4020 },
4021 system_shared_libs: [],
4022 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004023 apex_available: [
4024 "myapex_provider",
4025 "myapex_selfcontained",
4026 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004027 }
4028 `)
4029
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004030 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004031 var provideNativeLibs, requireNativeLibs []string
4032
Sundong Ahnabb64432019-10-22 13:58:29 +09004033 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004034 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4035 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004036 ensureListEmpty(t, provideNativeLibs)
4037 ensureListEmpty(t, requireNativeLibs)
4038
Sundong Ahnabb64432019-10-22 13:58:29 +09004039 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004040 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4041 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004042 ensureListEmpty(t, provideNativeLibs)
4043 ensureListContains(t, requireNativeLibs, "libfoo.so")
4044
Sundong Ahnabb64432019-10-22 13:58:29 +09004045 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004046 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4047 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004048 ensureListContains(t, provideNativeLibs, "libfoo.so")
4049 ensureListEmpty(t, requireNativeLibs)
4050
Sundong Ahnabb64432019-10-22 13:58:29 +09004051 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004052 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4053 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004054 ensureListContains(t, provideNativeLibs, "libfoo.so")
4055 ensureListEmpty(t, requireNativeLibs)
4056}
4057
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004058func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004059 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004060 apex {
4061 name: "myapex",
4062 key: "myapex.key",
4063 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09004064 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004065 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004066 }
4067
4068 apex_key {
4069 name: "myapex.key",
4070 public_key: "testkey.avbpubkey",
4071 private_key: "testkey.pem",
4072 }
Jiyong Parkdb334862020-02-05 17:19:28 +09004073
4074 cc_library {
4075 name: "mylib",
4076 srcs: ["mylib.cpp"],
4077 system_shared_libs: [],
4078 stl: "none",
4079 apex_available: [
4080 "//apex_available:platform",
4081 "myapex",
4082 ],
4083 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004084 `)
4085
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004086 module := ctx.ModuleForTests("myapex", "android_common_com.android.myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004087 apexManifestRule := module.Rule("apexManifestRule")
4088 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
4089 apexRule := module.Rule("apexRule")
4090 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09004091
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004092 apexBundle := module.Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004093 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09004094 name := apexBundle.BaseModuleName()
4095 prefix := "TARGET_"
4096 var builder strings.Builder
4097 data.Custom(&builder, name, prefix, "", data)
4098 androidMk := builder.String()
4099 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
4100 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004101}
4102
Alex Light0851b882019-02-07 13:20:53 -08004103func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004104 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004105 apex {
4106 name: "myapex",
4107 key: "myapex.key",
4108 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004109 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004110 }
4111
4112 apex_key {
4113 name: "myapex.key",
4114 public_key: "testkey.avbpubkey",
4115 private_key: "testkey.pem",
4116 }
4117
4118 cc_library {
4119 name: "mylib_common",
4120 srcs: ["mylib.cpp"],
4121 system_shared_libs: [],
4122 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004123 apex_available: [
4124 "//apex_available:platform",
4125 "myapex",
4126 ],
Alex Light0851b882019-02-07 13:20:53 -08004127 }
4128 `)
4129
Sundong Ahnabb64432019-10-22 13:58:29 +09004130 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004131 apexRule := module.Rule("apexRule")
4132 copyCmds := apexRule.Args["copy_commands"]
4133
4134 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4135 t.Log("Apex was a test apex!")
4136 t.Fail()
4137 }
4138 // Ensure that main rule creates an output
4139 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4140
4141 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004142 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004143
4144 // Ensure that both direct and indirect deps are copied into apex
4145 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4146
Colin Cross7113d202019-11-20 16:39:12 -08004147 // Ensure that the platform variant ends with _shared
4148 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004149
Colin Cross56a83212020-09-15 18:30:11 -07004150 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004151 t.Log("Found mylib_common not in any apex!")
4152 t.Fail()
4153 }
4154}
4155
4156func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004157 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004158 apex_test {
4159 name: "myapex",
4160 key: "myapex.key",
4161 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004162 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004163 }
4164
4165 apex_key {
4166 name: "myapex.key",
4167 public_key: "testkey.avbpubkey",
4168 private_key: "testkey.pem",
4169 }
4170
4171 cc_library {
4172 name: "mylib_common_test",
4173 srcs: ["mylib.cpp"],
4174 system_shared_libs: [],
4175 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004176 // TODO: remove //apex_available:platform
4177 apex_available: [
4178 "//apex_available:platform",
4179 "myapex",
4180 ],
Alex Light0851b882019-02-07 13:20:53 -08004181 }
4182 `)
4183
Sundong Ahnabb64432019-10-22 13:58:29 +09004184 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004185 apexRule := module.Rule("apexRule")
4186 copyCmds := apexRule.Args["copy_commands"]
4187
4188 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4189 t.Log("Apex was not a test apex!")
4190 t.Fail()
4191 }
4192 // Ensure that main rule creates an output
4193 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4194
4195 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004196 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004197
4198 // Ensure that both direct and indirect deps are copied into apex
4199 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4200
Colin Cross7113d202019-11-20 16:39:12 -08004201 // Ensure that the platform variant ends with _shared
4202 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004203}
4204
Alex Light9670d332019-01-29 18:07:33 -08004205func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004206 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004207 apex {
4208 name: "myapex",
4209 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004210 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004211 multilib: {
4212 first: {
4213 native_shared_libs: ["mylib_common"],
4214 }
4215 },
4216 target: {
4217 android: {
4218 multilib: {
4219 first: {
4220 native_shared_libs: ["mylib"],
4221 }
4222 }
4223 },
4224 host: {
4225 multilib: {
4226 first: {
4227 native_shared_libs: ["mylib2"],
4228 }
4229 }
4230 }
4231 }
4232 }
4233
4234 apex_key {
4235 name: "myapex.key",
4236 public_key: "testkey.avbpubkey",
4237 private_key: "testkey.pem",
4238 }
4239
4240 cc_library {
4241 name: "mylib",
4242 srcs: ["mylib.cpp"],
4243 system_shared_libs: [],
4244 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004245 // TODO: remove //apex_available:platform
4246 apex_available: [
4247 "//apex_available:platform",
4248 "myapex",
4249 ],
Alex Light9670d332019-01-29 18:07:33 -08004250 }
4251
4252 cc_library {
4253 name: "mylib_common",
4254 srcs: ["mylib.cpp"],
4255 system_shared_libs: [],
4256 stl: "none",
4257 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004258 // TODO: remove //apex_available:platform
4259 apex_available: [
4260 "//apex_available:platform",
4261 "myapex",
4262 ],
Alex Light9670d332019-01-29 18:07:33 -08004263 }
4264
4265 cc_library {
4266 name: "mylib2",
4267 srcs: ["mylib.cpp"],
4268 system_shared_libs: [],
4269 stl: "none",
4270 compile_multilib: "first",
4271 }
4272 `)
4273
Sundong Ahnabb64432019-10-22 13:58:29 +09004274 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004275 copyCmds := apexRule.Args["copy_commands"]
4276
4277 // Ensure that main rule creates an output
4278 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4279
4280 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004281 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4282 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4283 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004284
4285 // Ensure that both direct and indirect deps are copied into apex
4286 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4287 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4288 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4289
Colin Cross7113d202019-11-20 16:39:12 -08004290 // Ensure that the platform variant ends with _shared
4291 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4292 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4293 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004294}
Jiyong Park04480cf2019-02-06 00:16:29 +09004295
Jiyong Park59140302020-12-14 18:44:04 +09004296func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004297 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004298 apex {
4299 name: "myapex",
4300 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004301 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09004302 arch: {
4303 arm64: {
4304 native_shared_libs: ["mylib.arm64"],
4305 },
4306 x86_64: {
4307 native_shared_libs: ["mylib.x64"],
4308 },
4309 }
4310 }
4311
4312 apex_key {
4313 name: "myapex.key",
4314 public_key: "testkey.avbpubkey",
4315 private_key: "testkey.pem",
4316 }
4317
4318 cc_library {
4319 name: "mylib.arm64",
4320 srcs: ["mylib.cpp"],
4321 system_shared_libs: [],
4322 stl: "none",
4323 // TODO: remove //apex_available:platform
4324 apex_available: [
4325 "//apex_available:platform",
4326 "myapex",
4327 ],
4328 }
4329
4330 cc_library {
4331 name: "mylib.x64",
4332 srcs: ["mylib.cpp"],
4333 system_shared_libs: [],
4334 stl: "none",
4335 // TODO: remove //apex_available:platform
4336 apex_available: [
4337 "//apex_available:platform",
4338 "myapex",
4339 ],
4340 }
4341 `)
4342
4343 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4344 copyCmds := apexRule.Args["copy_commands"]
4345
4346 // Ensure that apex variant is created for the direct dep
4347 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4348 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4349
4350 // Ensure that both direct and indirect deps are copied into apex
4351 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4352 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4353}
4354
Jiyong Park04480cf2019-02-06 00:16:29 +09004355func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004356 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004357 apex {
4358 name: "myapex",
4359 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004360 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004361 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004362 }
4363
4364 apex_key {
4365 name: "myapex.key",
4366 public_key: "testkey.avbpubkey",
4367 private_key: "testkey.pem",
4368 }
4369
4370 sh_binary {
4371 name: "myscript",
4372 src: "mylib.cpp",
4373 filename: "myscript.sh",
4374 sub_dir: "script",
4375 }
4376 `)
4377
Sundong Ahnabb64432019-10-22 13:58:29 +09004378 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004379 copyCmds := apexRule.Args["copy_commands"]
4380
4381 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4382}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004383
Jooyung Han91df2082019-11-20 01:49:42 +09004384func TestApexInVariousPartition(t *testing.T) {
4385 testcases := []struct {
4386 propName, parition, flattenedPartition string
4387 }{
4388 {"", "system", "system_ext"},
4389 {"product_specific: true", "product", "product"},
4390 {"soc_specific: true", "vendor", "vendor"},
4391 {"proprietary: true", "vendor", "vendor"},
4392 {"vendor: true", "vendor", "vendor"},
4393 {"system_ext_specific: true", "system_ext", "system_ext"},
4394 }
4395 for _, tc := range testcases {
4396 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004397 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004398 apex {
4399 name: "myapex",
4400 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004401 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004402 `+tc.propName+`
4403 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004404
Jooyung Han91df2082019-11-20 01:49:42 +09004405 apex_key {
4406 name: "myapex.key",
4407 public_key: "testkey.avbpubkey",
4408 private_key: "testkey.pem",
4409 }
4410 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004411
Jooyung Han91df2082019-11-20 01:49:42 +09004412 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004413 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4414 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004415 if actual != expected {
4416 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4417 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004418
Jooyung Han91df2082019-11-20 01:49:42 +09004419 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004420 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4421 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004422 if actual != expected {
4423 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4424 }
4425 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004426 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004427}
Jiyong Park67882562019-03-21 01:11:21 +09004428
Jooyung Han580eb4f2020-06-24 19:33:06 +09004429func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004430 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004431 apex {
4432 name: "myapex",
4433 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004434 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004435 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004436
Jooyung Han580eb4f2020-06-24 19:33:06 +09004437 apex_key {
4438 name: "myapex.key",
4439 public_key: "testkey.avbpubkey",
4440 private_key: "testkey.pem",
4441 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004442 `)
4443 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004444 rule := module.Output("file_contexts")
4445 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4446}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004447
Jooyung Han580eb4f2020-06-24 19:33:06 +09004448func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004449 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004450 apex {
4451 name: "myapex",
4452 key: "myapex.key",
4453 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004454 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004455 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004456
Jooyung Han580eb4f2020-06-24 19:33:06 +09004457 apex_key {
4458 name: "myapex.key",
4459 public_key: "testkey.avbpubkey",
4460 private_key: "testkey.pem",
4461 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004462 `, withFiles(map[string][]byte{
4463 "my_own_file_contexts": nil,
4464 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004465}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004466
Jooyung Han580eb4f2020-06-24 19:33:06 +09004467func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004468 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004469 apex {
4470 name: "myapex",
4471 key: "myapex.key",
4472 product_specific: true,
4473 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004474 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004475 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004476
Jooyung Han580eb4f2020-06-24 19:33:06 +09004477 apex_key {
4478 name: "myapex.key",
4479 public_key: "testkey.avbpubkey",
4480 private_key: "testkey.pem",
4481 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004482 `)
4483
Colin Cross1c460562021-02-16 17:55:47 -08004484 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004485 apex {
4486 name: "myapex",
4487 key: "myapex.key",
4488 product_specific: true,
4489 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004490 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004491 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004492
Jooyung Han580eb4f2020-06-24 19:33:06 +09004493 apex_key {
4494 name: "myapex.key",
4495 public_key: "testkey.avbpubkey",
4496 private_key: "testkey.pem",
4497 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004498 `, withFiles(map[string][]byte{
4499 "product_specific_file_contexts": nil,
4500 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004501 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4502 rule := module.Output("file_contexts")
4503 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4504}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004505
Jooyung Han580eb4f2020-06-24 19:33:06 +09004506func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004507 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004508 apex {
4509 name: "myapex",
4510 key: "myapex.key",
4511 product_specific: true,
4512 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004513 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004514 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004515
Jooyung Han580eb4f2020-06-24 19:33:06 +09004516 apex_key {
4517 name: "myapex.key",
4518 public_key: "testkey.avbpubkey",
4519 private_key: "testkey.pem",
4520 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004521
Jooyung Han580eb4f2020-06-24 19:33:06 +09004522 filegroup {
4523 name: "my-file-contexts",
4524 srcs: ["product_specific_file_contexts"],
4525 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004526 `, withFiles(map[string][]byte{
4527 "product_specific_file_contexts": nil,
4528 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004529 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4530 rule := module.Output("file_contexts")
4531 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004532}
4533
Jiyong Park67882562019-03-21 01:11:21 +09004534func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004535 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004536 apex_key {
4537 name: "myapex.key",
4538 public_key: ":my.avbpubkey",
4539 private_key: ":my.pem",
4540 product_specific: true,
4541 }
4542
4543 filegroup {
4544 name: "my.avbpubkey",
4545 srcs: ["testkey2.avbpubkey"],
4546 }
4547
4548 filegroup {
4549 name: "my.pem",
4550 srcs: ["testkey2.pem"],
4551 }
4552 `)
4553
4554 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4555 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004556 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004557 if actual_pubkey != expected_pubkey {
4558 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4559 }
4560 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004561 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004562 if actual_privkey != expected_privkey {
4563 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4564 }
4565}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004566
4567func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004568 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004569 prebuilt_apex {
4570 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004571 arch: {
4572 arm64: {
4573 src: "myapex-arm64.apex",
4574 },
4575 arm: {
4576 src: "myapex-arm.apex",
4577 },
4578 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004579 }
4580 `)
4581
Paul Duffin6717d882021-06-15 19:09:41 +01004582 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004583
Jiyong Parkc95714e2019-03-29 14:23:10 +09004584 expectedInput := "myapex-arm64.apex"
4585 if prebuilt.inputApex.String() != expectedInput {
4586 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4587 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004588}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004589
Paul Duffinc0609c62021-03-01 17:27:16 +00004590func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004591 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004592 prebuilt_apex {
4593 name: "myapex",
4594 }
4595 `)
4596}
4597
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004598func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004599 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004600 prebuilt_apex {
4601 name: "myapex",
4602 src: "myapex-arm.apex",
4603 filename: "notmyapex.apex",
4604 }
4605 `)
4606
Paul Duffin6717d882021-06-15 19:09:41 +01004607 p := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004608
4609 expected := "notmyapex.apex"
4610 if p.installFilename != expected {
4611 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4612 }
4613}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004614
Samiul Islam7c02e262021-09-08 17:48:28 +01004615func TestApexSetFilenameOverride(t *testing.T) {
4616 testApex(t, `
4617 apex_set {
4618 name: "com.company.android.myapex",
4619 apex_name: "com.android.myapex",
4620 set: "company-myapex.apks",
4621 filename: "com.company.android.myapex.apex"
4622 }
4623 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4624
4625 testApex(t, `
4626 apex_set {
4627 name: "com.company.android.myapex",
4628 apex_name: "com.android.myapex",
4629 set: "company-myapex.apks",
4630 filename: "com.company.android.myapex.capex"
4631 }
4632 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4633
4634 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4635 apex_set {
4636 name: "com.company.android.myapex",
4637 apex_name: "com.android.myapex",
4638 set: "company-myapex.apks",
4639 filename: "some-random-suffix"
4640 }
4641 `)
4642}
4643
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004644func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004645 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004646 prebuilt_apex {
4647 name: "myapex.prebuilt",
4648 src: "myapex-arm.apex",
4649 overrides: [
4650 "myapex",
4651 ],
4652 }
4653 `)
4654
Paul Duffin6717d882021-06-15 19:09:41 +01004655 p := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt").Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004656
4657 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004658 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004659 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004660 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004661 }
4662}
4663
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004664func TestPrebuiltApexName(t *testing.T) {
4665 testApex(t, `
4666 prebuilt_apex {
4667 name: "com.company.android.myapex",
4668 apex_name: "com.android.myapex",
4669 src: "company-myapex-arm.apex",
4670 }
4671 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4672
4673 testApex(t, `
4674 apex_set {
4675 name: "com.company.android.myapex",
4676 apex_name: "com.android.myapex",
4677 set: "company-myapex.apks",
4678 }
4679 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4680}
4681
4682func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4683 _ = android.GroupFixturePreparers(
4684 java.PrepareForTestWithJavaDefaultModules,
4685 PrepareForTestWithApexBuildComponents,
4686 android.FixtureWithRootAndroidBp(`
4687 platform_bootclasspath {
4688 name: "platform-bootclasspath",
4689 fragments: [
4690 {
4691 apex: "com.android.art",
4692 module: "art-bootclasspath-fragment",
4693 },
4694 ],
4695 }
4696
4697 prebuilt_apex {
4698 name: "com.company.android.art",
4699 apex_name: "com.android.art",
4700 src: "com.company.android.art-arm.apex",
4701 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4702 }
4703
4704 prebuilt_bootclasspath_fragment {
4705 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004706 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004707 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004708 hidden_api: {
4709 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4710 metadata: "my-bootclasspath-fragment/metadata.csv",
4711 index: "my-bootclasspath-fragment/index.csv",
4712 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4713 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4714 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004715 }
4716
4717 java_import {
4718 name: "core-oj",
4719 jars: ["prebuilt.jar"],
4720 }
4721 `),
4722 ).RunTest(t)
4723}
4724
Paul Duffin092153d2021-01-26 11:42:39 +00004725// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4726// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004727func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004728 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004729
Paul Duffin89886cb2021-02-05 16:44:03 +00004730 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004731 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004732 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004733 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004734 dexJarBuildPath := p.DexJarBuildPath().PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004735 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004736 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
4737 ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
4738 android.NormalizePathForTesting(dexJarBuildPath))
4739 }
4740
4741 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004742 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004743 // Make sure the import has been given the correct path to the dex jar.
4744 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4745 dexJarBuildPath := p.DexJarInstallPath()
4746 stem := android.RemoveOptionalPrebuiltPrefix(name)
4747 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
4748 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
4749 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00004750 }
4751
Paul Duffin39853512021-02-26 11:09:39 +00004752 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004753 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004754 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09004755 android.AssertArrayString(t, "Check if there is no source variant",
4756 []string{"android_common"},
4757 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00004758 }
4759
4760 t.Run("prebuilt only", func(t *testing.T) {
4761 bp := `
4762 prebuilt_apex {
4763 name: "myapex",
4764 arch: {
4765 arm64: {
4766 src: "myapex-arm64.apex",
4767 },
4768 arm: {
4769 src: "myapex-arm.apex",
4770 },
4771 },
Paul Duffin39853512021-02-26 11:09:39 +00004772 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004773 }
4774
4775 java_import {
4776 name: "libfoo",
4777 jars: ["libfoo.jar"],
4778 }
Paul Duffin39853512021-02-26 11:09:39 +00004779
4780 java_sdk_library_import {
4781 name: "libbar",
4782 public: {
4783 jars: ["libbar.jar"],
4784 },
4785 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004786 `
4787
4788 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4789 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4790
Martin Stjernholm44825602021-09-17 01:44:12 +01004791 deapexerName := deapexerModuleName("myapex")
4792 android.AssertStringEquals(t, "APEX module name from deapexer name", "myapex", apexModuleName(deapexerName))
4793
Paul Duffinf6932af2021-02-26 18:21:56 +00004794 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01004795 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00004796 rule := deapexer.Rule("deapexer")
4797 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4798 t.Errorf("expected: %q, found: %q", expected, actual)
4799 }
4800
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004801 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01004802 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004803 rule = prebuiltApex.Rule("android/soong/android.Cp")
4804 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4805 t.Errorf("expected: %q, found: %q", expected, actual)
4806 }
4807
Paul Duffin89886cb2021-02-05 16:44:03 +00004808 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004809 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004810
4811 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004812 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004813 })
4814
4815 t.Run("prebuilt with source preferred", func(t *testing.T) {
4816
4817 bp := `
4818 prebuilt_apex {
4819 name: "myapex",
4820 arch: {
4821 arm64: {
4822 src: "myapex-arm64.apex",
4823 },
4824 arm: {
4825 src: "myapex-arm.apex",
4826 },
4827 },
Paul Duffin39853512021-02-26 11:09:39 +00004828 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004829 }
4830
4831 java_import {
4832 name: "libfoo",
4833 jars: ["libfoo.jar"],
4834 }
4835
4836 java_library {
4837 name: "libfoo",
4838 }
Paul Duffin39853512021-02-26 11:09:39 +00004839
4840 java_sdk_library_import {
4841 name: "libbar",
4842 public: {
4843 jars: ["libbar.jar"],
4844 },
4845 }
4846
4847 java_sdk_library {
4848 name: "libbar",
4849 srcs: ["foo/bar/MyClass.java"],
4850 unsafe_ignore_missing_latest_api: true,
4851 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004852 `
4853
4854 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4855 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4856
Paul Duffin89886cb2021-02-05 16:44:03 +00004857 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004858 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004859 ensureNoSourceVariant(t, ctx, "libfoo")
4860
4861 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004862 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00004863 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004864 })
4865
4866 t.Run("prebuilt preferred with source", func(t *testing.T) {
4867 bp := `
4868 prebuilt_apex {
4869 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004870 arch: {
4871 arm64: {
4872 src: "myapex-arm64.apex",
4873 },
4874 arm: {
4875 src: "myapex-arm.apex",
4876 },
4877 },
Paul Duffin39853512021-02-26 11:09:39 +00004878 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004879 }
4880
4881 java_import {
4882 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004883 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004884 jars: ["libfoo.jar"],
4885 }
4886
4887 java_library {
4888 name: "libfoo",
4889 }
Paul Duffin39853512021-02-26 11:09:39 +00004890
4891 java_sdk_library_import {
4892 name: "libbar",
4893 prefer: true,
4894 public: {
4895 jars: ["libbar.jar"],
4896 },
4897 }
4898
4899 java_sdk_library {
4900 name: "libbar",
4901 srcs: ["foo/bar/MyClass.java"],
4902 unsafe_ignore_missing_latest_api: true,
4903 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004904 `
4905
4906 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4907 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4908
Paul Duffin89886cb2021-02-05 16:44:03 +00004909 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004910 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004911 ensureNoSourceVariant(t, ctx, "libfoo")
4912
4913 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004914 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00004915 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004916 })
4917}
4918
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004919func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01004920 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01004921 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01004922 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
4923 // is disabled.
4924 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
4925 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004926
Paul Duffin37856732021-02-26 14:24:15 +00004927 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4928 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01004929 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004930 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004931 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004932 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004933 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004934 foundLibfooJar = true
4935 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01004936 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004937 }
4938 }
4939 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02004940 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004941 }
4942 }
4943
Paul Duffin40a3f652021-07-19 13:11:24 +01004944 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004945 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01004946 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01004947 var rule android.TestingBuildParams
4948
4949 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
4950 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00004951 }
4952
Paul Duffin40a3f652021-07-19 13:11:24 +01004953 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
4954 t.Helper()
4955 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
4956 var rule android.TestingBuildParams
4957
4958 rule = platformBootclasspath.Output("hiddenapi-index.csv")
4959 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
4960 }
4961
Paul Duffin89f570a2021-06-16 01:42:33 +01004962 fragment := java.ApexVariantReference{
4963 Apex: proptools.StringPtr("myapex"),
4964 Module: proptools.StringPtr("my-bootclasspath-fragment"),
4965 }
4966
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004967 t.Run("prebuilt only", func(t *testing.T) {
4968 bp := `
4969 prebuilt_apex {
4970 name: "myapex",
4971 arch: {
4972 arm64: {
4973 src: "myapex-arm64.apex",
4974 },
4975 arm: {
4976 src: "myapex-arm.apex",
4977 },
4978 },
Paul Duffin89f570a2021-06-16 01:42:33 +01004979 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4980 }
4981
4982 prebuilt_bootclasspath_fragment {
4983 name: "my-bootclasspath-fragment",
4984 contents: ["libfoo", "libbar"],
4985 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01004986 hidden_api: {
4987 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4988 metadata: "my-bootclasspath-fragment/metadata.csv",
4989 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01004990 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
4991 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
4992 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01004993 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004994 }
4995
4996 java_import {
4997 name: "libfoo",
4998 jars: ["libfoo.jar"],
4999 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005000 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005001 }
Paul Duffin37856732021-02-26 14:24:15 +00005002
5003 java_sdk_library_import {
5004 name: "libbar",
5005 public: {
5006 jars: ["libbar.jar"],
5007 },
5008 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005009 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005010 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005011 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005012 `
5013
Paul Duffin89f570a2021-06-16 01:42:33 +01005014 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005015 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5016 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005017
Paul Duffin537ea3d2021-05-14 10:38:00 +01005018 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005019 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005020 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005021 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005022 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5023 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005024 })
5025
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005026 t.Run("apex_set only", func(t *testing.T) {
5027 bp := `
5028 apex_set {
5029 name: "myapex",
5030 set: "myapex.apks",
Paul Duffin89f570a2021-06-16 01:42:33 +01005031 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5032 }
5033
5034 prebuilt_bootclasspath_fragment {
5035 name: "my-bootclasspath-fragment",
5036 contents: ["libfoo", "libbar"],
5037 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005038 hidden_api: {
5039 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5040 metadata: "my-bootclasspath-fragment/metadata.csv",
5041 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005042 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5043 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5044 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005045 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005046 }
5047
5048 java_import {
5049 name: "libfoo",
5050 jars: ["libfoo.jar"],
5051 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005052 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005053 }
5054
5055 java_sdk_library_import {
5056 name: "libbar",
5057 public: {
5058 jars: ["libbar.jar"],
5059 },
5060 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005061 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005062 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005063 }
5064 `
5065
Paul Duffin89f570a2021-06-16 01:42:33 +01005066 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005067 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5068 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
5069
Paul Duffin537ea3d2021-05-14 10:38:00 +01005070 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005071 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005072 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005073 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005074 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5075 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005076 })
5077
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005078 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5079 bp := `
5080 prebuilt_apex {
5081 name: "myapex",
5082 arch: {
5083 arm64: {
5084 src: "myapex-arm64.apex",
5085 },
5086 arm: {
5087 src: "myapex-arm.apex",
5088 },
5089 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005090 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5091 }
5092
5093 prebuilt_bootclasspath_fragment {
5094 name: "my-bootclasspath-fragment",
5095 contents: ["libfoo", "libbar"],
5096 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005097 hidden_api: {
5098 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5099 metadata: "my-bootclasspath-fragment/metadata.csv",
5100 index: "my-bootclasspath-fragment/index.csv",
5101 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5102 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5103 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005104 }
5105
5106 java_import {
5107 name: "libfoo",
5108 jars: ["libfoo.jar"],
5109 apex_available: ["myapex"],
5110 }
5111
5112 java_library {
5113 name: "libfoo",
5114 srcs: ["foo/bar/MyClass.java"],
5115 apex_available: ["myapex"],
5116 }
Paul Duffin37856732021-02-26 14:24:15 +00005117
5118 java_sdk_library_import {
5119 name: "libbar",
5120 public: {
5121 jars: ["libbar.jar"],
5122 },
5123 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005124 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005125 }
5126
5127 java_sdk_library {
5128 name: "libbar",
5129 srcs: ["foo/bar/MyClass.java"],
5130 unsafe_ignore_missing_latest_api: true,
5131 apex_available: ["myapex"],
5132 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005133 `
5134
5135 // In this test the source (java_library) libfoo is active since the
5136 // prebuilt (java_import) defaults to prefer:false. However the
5137 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5138 // find the dex boot jar in it. We either need to disable the source libfoo
5139 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005140 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005141 // dexbootjar check is skipped if AllowMissingDependencies is true
5142 preparerAllowMissingDeps := android.GroupFixturePreparers(
5143 preparer,
5144 android.PrepareForTestWithAllowMissingDependencies,
5145 )
5146 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005147 })
5148
5149 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5150 bp := `
5151 prebuilt_apex {
5152 name: "myapex",
5153 arch: {
5154 arm64: {
5155 src: "myapex-arm64.apex",
5156 },
5157 arm: {
5158 src: "myapex-arm.apex",
5159 },
5160 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005161 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5162 }
5163
5164 prebuilt_bootclasspath_fragment {
5165 name: "my-bootclasspath-fragment",
5166 contents: ["libfoo", "libbar"],
5167 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005168 hidden_api: {
5169 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5170 metadata: "my-bootclasspath-fragment/metadata.csv",
5171 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005172 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5173 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5174 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005175 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005176 }
5177
5178 java_import {
5179 name: "libfoo",
5180 prefer: true,
5181 jars: ["libfoo.jar"],
5182 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005183 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005184 }
5185
5186 java_library {
5187 name: "libfoo",
5188 srcs: ["foo/bar/MyClass.java"],
5189 apex_available: ["myapex"],
5190 }
Paul Duffin37856732021-02-26 14:24:15 +00005191
5192 java_sdk_library_import {
5193 name: "libbar",
5194 prefer: true,
5195 public: {
5196 jars: ["libbar.jar"],
5197 },
5198 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005199 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005200 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005201 }
5202
5203 java_sdk_library {
5204 name: "libbar",
5205 srcs: ["foo/bar/MyClass.java"],
5206 unsafe_ignore_missing_latest_api: true,
5207 apex_available: ["myapex"],
5208 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005209 `
5210
Paul Duffin89f570a2021-06-16 01:42:33 +01005211 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005212 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5213 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005214
Paul Duffin537ea3d2021-05-14 10:38:00 +01005215 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005216 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005217 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005218 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005219 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5220 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005221 })
5222
5223 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5224 bp := `
5225 apex {
5226 name: "myapex",
5227 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00005228 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005229 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005230 }
5231
5232 apex_key {
5233 name: "myapex.key",
5234 public_key: "testkey.avbpubkey",
5235 private_key: "testkey.pem",
5236 }
5237
5238 prebuilt_apex {
5239 name: "myapex",
5240 arch: {
5241 arm64: {
5242 src: "myapex-arm64.apex",
5243 },
5244 arm: {
5245 src: "myapex-arm.apex",
5246 },
5247 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005248 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5249 }
5250
5251 prebuilt_bootclasspath_fragment {
5252 name: "my-bootclasspath-fragment",
5253 contents: ["libfoo", "libbar"],
5254 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005255 hidden_api: {
5256 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5257 metadata: "my-bootclasspath-fragment/metadata.csv",
5258 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005259 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5260 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5261 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005262 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005263 }
5264
5265 java_import {
5266 name: "libfoo",
5267 jars: ["libfoo.jar"],
5268 apex_available: ["myapex"],
5269 }
5270
5271 java_library {
5272 name: "libfoo",
5273 srcs: ["foo/bar/MyClass.java"],
5274 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005275 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005276 }
Paul Duffin37856732021-02-26 14:24:15 +00005277
5278 java_sdk_library_import {
5279 name: "libbar",
5280 public: {
5281 jars: ["libbar.jar"],
5282 },
5283 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005284 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005285 }
5286
5287 java_sdk_library {
5288 name: "libbar",
5289 srcs: ["foo/bar/MyClass.java"],
5290 unsafe_ignore_missing_latest_api: true,
5291 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005292 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005293 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005294 `
5295
Paul Duffin89f570a2021-06-16 01:42:33 +01005296 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005297 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
5298 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005299
Paul Duffin537ea3d2021-05-14 10:38:00 +01005300 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005301 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005302 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005303 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005304 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5305 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005306 })
5307
5308 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5309 bp := `
5310 apex {
5311 name: "myapex",
5312 enabled: false,
5313 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01005314 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005315 }
5316
5317 apex_key {
5318 name: "myapex.key",
5319 public_key: "testkey.avbpubkey",
5320 private_key: "testkey.pem",
5321 }
5322
5323 prebuilt_apex {
5324 name: "myapex",
5325 arch: {
5326 arm64: {
5327 src: "myapex-arm64.apex",
5328 },
5329 arm: {
5330 src: "myapex-arm.apex",
5331 },
5332 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005333 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5334 }
5335
5336 prebuilt_bootclasspath_fragment {
5337 name: "my-bootclasspath-fragment",
5338 contents: ["libfoo", "libbar"],
5339 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005340 hidden_api: {
5341 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5342 metadata: "my-bootclasspath-fragment/metadata.csv",
5343 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005344 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5345 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5346 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005347 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005348 }
5349
5350 java_import {
5351 name: "libfoo",
5352 prefer: true,
5353 jars: ["libfoo.jar"],
5354 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005355 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005356 }
5357
5358 java_library {
5359 name: "libfoo",
5360 srcs: ["foo/bar/MyClass.java"],
5361 apex_available: ["myapex"],
5362 }
Paul Duffin37856732021-02-26 14:24:15 +00005363
5364 java_sdk_library_import {
5365 name: "libbar",
5366 prefer: true,
5367 public: {
5368 jars: ["libbar.jar"],
5369 },
5370 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005371 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005372 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005373 }
5374
5375 java_sdk_library {
5376 name: "libbar",
5377 srcs: ["foo/bar/MyClass.java"],
5378 unsafe_ignore_missing_latest_api: true,
5379 apex_available: ["myapex"],
5380 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005381 `
5382
Paul Duffin89f570a2021-06-16 01:42:33 +01005383 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005384 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5385 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005386
Paul Duffin537ea3d2021-05-14 10:38:00 +01005387 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005388 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005389 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005390 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005391 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5392 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005393 })
5394}
5395
Roland Levillain630846d2019-06-26 12:48:34 +01005396func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005397 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005398 apex_test {
5399 name: "myapex",
5400 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005401 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005402 tests: [
5403 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005404 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005405 ],
5406 }
5407
5408 apex_key {
5409 name: "myapex.key",
5410 public_key: "testkey.avbpubkey",
5411 private_key: "testkey.pem",
5412 }
5413
Liz Kammer1c14a212020-05-12 15:26:55 -07005414 filegroup {
5415 name: "fg",
5416 srcs: [
5417 "baz",
5418 "bar/baz"
5419 ],
5420 }
5421
Roland Levillain630846d2019-06-26 12:48:34 +01005422 cc_test {
5423 name: "mytest",
5424 gtest: false,
5425 srcs: ["mytest.cpp"],
5426 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005427 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005428 system_shared_libs: [],
5429 static_executable: true,
5430 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005431 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005432 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005433
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005434 cc_library {
5435 name: "mylib",
5436 srcs: ["mylib.cpp"],
5437 system_shared_libs: [],
5438 stl: "none",
5439 }
5440
Liz Kammer5bd365f2020-05-27 15:15:11 -07005441 filegroup {
5442 name: "fg2",
5443 srcs: [
5444 "testdata/baz"
5445 ],
5446 }
5447
Roland Levillain9b5fde92019-06-28 15:41:19 +01005448 cc_test {
5449 name: "mytests",
5450 gtest: false,
5451 srcs: [
5452 "mytest1.cpp",
5453 "mytest2.cpp",
5454 "mytest3.cpp",
5455 ],
5456 test_per_src: true,
5457 relative_install_path: "test",
5458 system_shared_libs: [],
5459 static_executable: true,
5460 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005461 data: [
5462 ":fg",
5463 ":fg2",
5464 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005465 }
Roland Levillain630846d2019-06-26 12:48:34 +01005466 `)
5467
Sundong Ahnabb64432019-10-22 13:58:29 +09005468 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005469 copyCmds := apexRule.Args["copy_commands"]
5470
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005471 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005472 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005473 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005474
Liz Kammer1c14a212020-05-12 15:26:55 -07005475 //Ensure that test data are copied into apex.
5476 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5477 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5478
Roland Levillain9b5fde92019-06-28 15:41:19 +01005479 // Ensure that test deps built with `test_per_src` are copied into apex.
5480 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5481 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5482 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005483
5484 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07005485 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005486 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005487 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005488 prefix := "TARGET_"
5489 var builder strings.Builder
5490 data.Custom(&builder, name, prefix, "", data)
5491 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09005492 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5493 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5494 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5495 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09005496 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09005497 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005498 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005499
5500 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005501 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005502 data.Custom(&builder, name, prefix, "", data)
5503 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005504 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5505 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005506}
5507
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005508func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005509 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005510 apex {
5511 name: "myapex",
5512 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005513 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005514 }
5515 apex_key {
5516 name: "myapex.key",
5517 public_key: "testkey.avbpubkey",
5518 private_key: "testkey.pem",
5519 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005520 `,
5521 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5522 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5523 }),
5524 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005525 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09005526 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005527 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005528 var builder strings.Builder
5529 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5530 androidMk := builder.String()
5531 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
5532}
5533
Jooyung Hand48f3c32019-08-23 11:18:57 +09005534func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5535 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5536 apex {
5537 name: "myapex",
5538 key: "myapex.key",
5539 native_shared_libs: ["libfoo"],
5540 }
5541
5542 apex_key {
5543 name: "myapex.key",
5544 public_key: "testkey.avbpubkey",
5545 private_key: "testkey.pem",
5546 }
5547
5548 cc_library {
5549 name: "libfoo",
5550 stl: "none",
5551 system_shared_libs: [],
5552 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005553 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005554 }
5555 `)
5556 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5557 apex {
5558 name: "myapex",
5559 key: "myapex.key",
5560 java_libs: ["myjar"],
5561 }
5562
5563 apex_key {
5564 name: "myapex.key",
5565 public_key: "testkey.avbpubkey",
5566 private_key: "testkey.pem",
5567 }
5568
5569 java_library {
5570 name: "myjar",
5571 srcs: ["foo/bar/MyClass.java"],
5572 sdk_version: "none",
5573 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005574 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005575 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005576 }
5577 `)
5578}
5579
Bill Peckhama41a6962021-01-11 10:58:54 -08005580func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005581 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005582 apex {
5583 name: "myapex",
5584 key: "myapex.key",
5585 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005586 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005587 }
5588
5589 apex_key {
5590 name: "myapex.key",
5591 public_key: "testkey.avbpubkey",
5592 private_key: "testkey.pem",
5593 }
5594
5595 java_import {
5596 name: "myjavaimport",
5597 apex_available: ["myapex"],
5598 jars: ["my.jar"],
5599 compile_dex: true,
5600 }
5601 `)
5602
5603 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5604 apexRule := module.Rule("apexRule")
5605 copyCmds := apexRule.Args["copy_commands"]
5606 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5607}
5608
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005609func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005610 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005611 apex {
5612 name: "myapex",
5613 key: "myapex.key",
5614 apps: [
5615 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005616 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005617 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005618 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005619 }
5620
5621 apex_key {
5622 name: "myapex.key",
5623 public_key: "testkey.avbpubkey",
5624 private_key: "testkey.pem",
5625 }
5626
5627 android_app {
5628 name: "AppFoo",
5629 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005630 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005631 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005632 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005633 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005634 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005635 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005636
5637 android_app {
5638 name: "AppFooPriv",
5639 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005640 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005641 system_modules: "none",
5642 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005643 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005644 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005645 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005646
5647 cc_library_shared {
5648 name: "libjni",
5649 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005650 shared_libs: ["libfoo"],
5651 stl: "none",
5652 system_shared_libs: [],
5653 apex_available: [ "myapex" ],
5654 sdk_version: "current",
5655 }
5656
5657 cc_library_shared {
5658 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005659 stl: "none",
5660 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005661 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005662 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005663 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005664 `)
5665
Sundong Ahnabb64432019-10-22 13:58:29 +09005666 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005667 apexRule := module.Rule("apexRule")
5668 copyCmds := apexRule.Args["copy_commands"]
5669
5670 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005671 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005672
Colin Crossaede88c2020-08-11 12:17:01 -07005673 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005674 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005675 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005676 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005677 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005678 // JNI libraries including transitive deps are
5679 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005680 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005681 // ... embedded inside APK (jnilibs.zip)
5682 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5683 // ... and not directly inside the APEX
5684 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5685 }
Dario Frenicde2a032019-10-27 00:29:22 +01005686}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005687
Dario Frenicde2a032019-10-27 00:29:22 +01005688func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005689 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005690 apex {
5691 name: "myapex",
5692 key: "myapex.key",
5693 apps: [
5694 "AppFooPrebuilt",
5695 "AppFooPrivPrebuilt",
5696 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005697 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005698 }
5699
5700 apex_key {
5701 name: "myapex.key",
5702 public_key: "testkey.avbpubkey",
5703 private_key: "testkey.pem",
5704 }
5705
5706 android_app_import {
5707 name: "AppFooPrebuilt",
5708 apk: "PrebuiltAppFoo.apk",
5709 presigned: true,
5710 dex_preopt: {
5711 enabled: false,
5712 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005713 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005714 }
5715
5716 android_app_import {
5717 name: "AppFooPrivPrebuilt",
5718 apk: "PrebuiltAppFooPriv.apk",
5719 privileged: true,
5720 presigned: true,
5721 dex_preopt: {
5722 enabled: false,
5723 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005724 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005725 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005726 }
5727 `)
5728
Sundong Ahnabb64432019-10-22 13:58:29 +09005729 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005730 apexRule := module.Rule("apexRule")
5731 copyCmds := apexRule.Args["copy_commands"]
5732
5733 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005734 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5735}
5736
5737func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005738 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005739 apex {
5740 name: "myapex",
5741 key: "myapex.key",
5742 apps: [
5743 "AppFoo",
5744 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005745 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005746 }
5747
5748 apex_key {
5749 name: "myapex.key",
5750 public_key: "testkey.avbpubkey",
5751 private_key: "testkey.pem",
5752 }
5753
5754 android_app {
5755 name: "AppFoo",
5756 srcs: ["foo/bar/MyClass.java"],
5757 sdk_version: "none",
5758 system_modules: "none",
5759 apex_available: [ "myapex" ],
5760 }
5761
5762 android_app_import {
5763 name: "AppFoo",
5764 apk: "AppFooPrebuilt.apk",
5765 filename: "AppFooPrebuilt.apk",
5766 presigned: true,
5767 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005768 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005769 }
5770 `, withFiles(map[string][]byte{
5771 "AppFooPrebuilt.apk": nil,
5772 }))
5773
5774 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005775 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005776 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005777}
5778
Dario Freni6f3937c2019-12-20 22:58:03 +00005779func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005780 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005781 apex {
5782 name: "myapex",
5783 key: "myapex.key",
5784 apps: [
5785 "TesterHelpAppFoo",
5786 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005787 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005788 }
5789
5790 apex_key {
5791 name: "myapex.key",
5792 public_key: "testkey.avbpubkey",
5793 private_key: "testkey.pem",
5794 }
5795
5796 android_test_helper_app {
5797 name: "TesterHelpAppFoo",
5798 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005799 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005800 }
5801
5802 `)
5803
5804 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5805 apexRule := module.Rule("apexRule")
5806 copyCmds := apexRule.Args["copy_commands"]
5807
5808 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5809}
5810
Jooyung Han18020ea2019-11-13 10:50:48 +09005811func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5812 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005813 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005814 apex {
5815 name: "myapex",
5816 key: "myapex.key",
5817 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005818 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005819 }
5820
5821 apex_key {
5822 name: "myapex.key",
5823 public_key: "testkey.avbpubkey",
5824 private_key: "testkey.pem",
5825 }
5826
5827 apex {
5828 name: "otherapex",
5829 key: "myapex.key",
5830 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005831 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005832 }
5833
5834 cc_defaults {
5835 name: "libfoo-defaults",
5836 apex_available: ["otherapex"],
5837 }
5838
5839 cc_library {
5840 name: "libfoo",
5841 defaults: ["libfoo-defaults"],
5842 stl: "none",
5843 system_shared_libs: [],
5844 }`)
5845}
5846
Paul Duffine52e66f2020-03-30 17:54:29 +01005847func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005848 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005849 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005850 apex {
5851 name: "myapex",
5852 key: "myapex.key",
5853 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005854 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005855 }
5856
5857 apex_key {
5858 name: "myapex.key",
5859 public_key: "testkey.avbpubkey",
5860 private_key: "testkey.pem",
5861 }
5862
5863 apex {
5864 name: "otherapex",
5865 key: "otherapex.key",
5866 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005867 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005868 }
5869
5870 apex_key {
5871 name: "otherapex.key",
5872 public_key: "testkey.avbpubkey",
5873 private_key: "testkey.pem",
5874 }
5875
5876 cc_library {
5877 name: "libfoo",
5878 stl: "none",
5879 system_shared_libs: [],
5880 apex_available: ["otherapex"],
5881 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005882}
Jiyong Park127b40b2019-09-30 16:04:35 +09005883
Paul Duffine52e66f2020-03-30 17:54:29 +01005884func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005885 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005886 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005887.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005888.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005889.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005890.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005891.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005892.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005893 apex {
5894 name: "myapex",
5895 key: "myapex.key",
5896 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005897 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005898 }
5899
5900 apex_key {
5901 name: "myapex.key",
5902 public_key: "testkey.avbpubkey",
5903 private_key: "testkey.pem",
5904 }
5905
Jiyong Park127b40b2019-09-30 16:04:35 +09005906 cc_library {
5907 name: "libfoo",
5908 stl: "none",
5909 shared_libs: ["libbar"],
5910 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005911 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005912 }
5913
5914 cc_library {
5915 name: "libbar",
5916 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005917 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005918 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005919 apex_available: ["myapex"],
5920 }
5921
5922 cc_library {
5923 name: "libbaz",
5924 stl: "none",
5925 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005926 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005927}
Jiyong Park127b40b2019-09-30 16:04:35 +09005928
Paul Duffine52e66f2020-03-30 17:54:29 +01005929func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005930 testApexError(t, "\"otherapex\" is not a valid module name", `
5931 apex {
5932 name: "myapex",
5933 key: "myapex.key",
5934 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005935 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005936 }
5937
5938 apex_key {
5939 name: "myapex.key",
5940 public_key: "testkey.avbpubkey",
5941 private_key: "testkey.pem",
5942 }
5943
5944 cc_library {
5945 name: "libfoo",
5946 stl: "none",
5947 system_shared_libs: [],
5948 apex_available: ["otherapex"],
5949 }`)
5950
Paul Duffine52e66f2020-03-30 17:54:29 +01005951 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005952 apex {
5953 name: "myapex",
5954 key: "myapex.key",
5955 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005956 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005957 }
5958
5959 apex_key {
5960 name: "myapex.key",
5961 public_key: "testkey.avbpubkey",
5962 private_key: "testkey.pem",
5963 }
5964
5965 cc_library {
5966 name: "libfoo",
5967 stl: "none",
5968 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005969 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005970 apex_available: ["myapex"],
5971 }
5972
5973 cc_library {
5974 name: "libbar",
5975 stl: "none",
5976 system_shared_libs: [],
5977 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005978 }
5979
5980 cc_library {
5981 name: "libbaz",
5982 stl: "none",
5983 system_shared_libs: [],
5984 stubs: {
5985 versions: ["10", "20", "30"],
5986 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005987 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005988}
Jiyong Park127b40b2019-09-30 16:04:35 +09005989
Jiyong Park89e850a2020-04-07 16:37:39 +09005990func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005991 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005992 apex {
5993 name: "myapex",
5994 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005995 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005996 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005997 }
5998
5999 apex_key {
6000 name: "myapex.key",
6001 public_key: "testkey.avbpubkey",
6002 private_key: "testkey.pem",
6003 }
6004
6005 cc_library {
6006 name: "libfoo",
6007 stl: "none",
6008 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006009 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006010 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006011 }
6012
6013 cc_library {
6014 name: "libfoo2",
6015 stl: "none",
6016 system_shared_libs: [],
6017 shared_libs: ["libbaz"],
6018 apex_available: ["//apex_available:platform"],
6019 }
6020
6021 cc_library {
6022 name: "libbar",
6023 stl: "none",
6024 system_shared_libs: [],
6025 apex_available: ["myapex"],
6026 }
6027
6028 cc_library {
6029 name: "libbaz",
6030 stl: "none",
6031 system_shared_libs: [],
6032 apex_available: ["myapex"],
6033 stubs: {
6034 versions: ["1"],
6035 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006036 }`)
6037
Jiyong Park89e850a2020-04-07 16:37:39 +09006038 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6039 // because it depends on libbar which isn't available to platform
6040 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6041 if libfoo.NotAvailableForPlatform() != true {
6042 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6043 }
6044
6045 // libfoo2 however can be available to platform because it depends on libbaz which provides
6046 // stubs
6047 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6048 if libfoo2.NotAvailableForPlatform() == true {
6049 t.Errorf("%q should be available to platform", libfoo2.String())
6050 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006051}
Jiyong Parka90ca002019-10-07 15:47:24 +09006052
Paul Duffine52e66f2020-03-30 17:54:29 +01006053func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006054 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006055 apex {
6056 name: "myapex",
6057 key: "myapex.key",
6058 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006059 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006060 }
6061
6062 apex_key {
6063 name: "myapex.key",
6064 public_key: "testkey.avbpubkey",
6065 private_key: "testkey.pem",
6066 }
6067
6068 cc_library {
6069 name: "libfoo",
6070 stl: "none",
6071 system_shared_libs: [],
6072 apex_available: ["myapex"],
6073 static: {
6074 apex_available: ["//apex_available:platform"],
6075 },
6076 }`)
6077
Jiyong Park89e850a2020-04-07 16:37:39 +09006078 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6079 if libfooShared.NotAvailableForPlatform() != true {
6080 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6081 }
6082 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6083 if libfooStatic.NotAvailableForPlatform() != false {
6084 t.Errorf("%q should be available to platform", libfooStatic.String())
6085 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006086}
6087
Jiyong Park5d790c32019-11-15 18:40:32 +09006088func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006089 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006090 apex {
6091 name: "myapex",
6092 key: "myapex.key",
6093 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006094 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006095 prebuilts: ["myetc"],
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006096 bootclasspath_fragments: ["mybootclasspath_fragment"],
6097 systemserverclasspath_fragments: ["mysystemserverclasspath_fragment"],
6098 java_libs: ["myjava_library"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006099 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006100 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006101 }
6102
6103 override_apex {
6104 name: "override_myapex",
6105 base: "myapex",
6106 apps: ["override_app"],
markchien7c803b82021-08-26 22:10:06 +08006107 bpfs: ["override_bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006108 prebuilts: ["override_myetc"],
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006109 bootclasspath_fragments: ["override_bootclasspath_fragment"],
6110 systemserverclasspath_fragments: ["override_systemserverclasspath_fragment"],
6111 java_libs: ["override_java_library"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006112 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08006113 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006114 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006115 key: "mynewapex.key",
6116 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006117 }
6118
6119 apex_key {
6120 name: "myapex.key",
6121 public_key: "testkey.avbpubkey",
6122 private_key: "testkey.pem",
6123 }
6124
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006125 apex_key {
6126 name: "mynewapex.key",
6127 public_key: "testkey2.avbpubkey",
6128 private_key: "testkey2.pem",
6129 }
6130
6131 android_app_certificate {
6132 name: "myapex.certificate",
6133 certificate: "testkey",
6134 }
6135
Jiyong Park5d790c32019-11-15 18:40:32 +09006136 android_app {
6137 name: "app",
6138 srcs: ["foo/bar/MyClass.java"],
6139 package_name: "foo",
6140 sdk_version: "none",
6141 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006142 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006143 }
6144
6145 override_android_app {
6146 name: "override_app",
6147 base: "app",
6148 package_name: "bar",
6149 }
markchien7c803b82021-08-26 22:10:06 +08006150
6151 bpf {
6152 name: "bpf",
6153 srcs: ["bpf.c"],
6154 }
6155
6156 bpf {
6157 name: "override_bpf",
6158 srcs: ["override_bpf.c"],
6159 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006160
6161 prebuilt_etc {
6162 name: "myetc",
6163 src: "myprebuilt",
6164 }
6165
6166 prebuilt_etc {
6167 name: "override_myetc",
6168 src: "override_myprebuilt",
6169 }
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006170
6171 java_library {
6172 name: "bcplib",
6173 srcs: ["a.java"],
6174 compile_dex: true,
6175 apex_available: ["myapex"],
6176 permitted_packages: ["bcp.lib"],
6177 }
6178
6179 bootclasspath_fragment {
6180 name: "mybootclasspath_fragment",
6181 contents: ["bcplib"],
6182 apex_available: ["myapex"],
6183 }
6184
6185 java_library {
6186 name: "override_bcplib",
6187 srcs: ["a.java"],
6188 compile_dex: true,
6189 apex_available: ["myapex"],
6190 permitted_packages: ["override.bcp.lib"],
6191 }
6192
6193 bootclasspath_fragment {
6194 name: "override_bootclasspath_fragment",
6195 contents: ["override_bcplib"],
6196 apex_available: ["myapex"],
6197 }
6198
6199 java_library {
6200 name: "systemserverlib",
6201 srcs: ["a.java"],
6202 apex_available: ["myapex"],
6203 }
6204
6205 systemserverclasspath_fragment {
6206 name: "mysystemserverclasspath_fragment",
6207 standalone_contents: ["systemserverlib"],
6208 apex_available: ["myapex"],
6209 }
6210
6211 java_library {
6212 name: "override_systemserverlib",
6213 srcs: ["a.java"],
6214 apex_available: ["myapex"],
6215 }
6216
6217 systemserverclasspath_fragment {
6218 name: "override_systemserverclasspath_fragment",
6219 standalone_contents: ["override_systemserverlib"],
6220 apex_available: ["myapex"],
6221 }
6222
6223 java_library {
6224 name: "myjava_library",
6225 srcs: ["a.java"],
6226 compile_dex: true,
6227 apex_available: ["myapex"],
6228 }
6229
6230 java_library {
6231 name: "override_java_library",
6232 srcs: ["a.java"],
6233 compile_dex: true,
6234 apex_available: ["myapex"],
6235 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006236 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006237
Jiyong Park317645e2019-12-05 13:20:58 +09006238 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
6239 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
6240 if originalVariant.GetOverriddenBy() != "" {
6241 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6242 }
6243 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6244 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6245 }
6246
Jiyong Park5d790c32019-11-15 18:40:32 +09006247 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
6248 apexRule := module.Rule("apexRule")
6249 copyCmds := apexRule.Args["copy_commands"]
6250
6251 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006252 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006253
markchien7c803b82021-08-26 22:10:06 +08006254 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
6255 ensureContains(t, copyCmds, "image.apex/etc/bpf/override_bpf.o")
6256
Daniel Norman5a3ce132021-08-26 15:44:43 -07006257 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6258 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6259
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006260 apexBundle := module.Module().(*apexBundle)
6261 name := apexBundle.Name()
6262 if name != "override_myapex" {
6263 t.Errorf("name should be \"override_myapex\", but was %q", name)
6264 }
6265
Baligh Uddin004d7172020-02-19 21:29:28 -08006266 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6267 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6268 }
6269
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006270 android.AssertArrayString(t, "Bootclasspath_fragments does not match",
6271 []string{"override_bootclasspath_fragment"}, apexBundle.overridableProperties.Bootclasspath_fragments)
6272 android.AssertArrayString(t, "Systemserverclasspath_fragments does not match",
6273 []string{"override_systemserverclasspath_fragment"}, apexBundle.overridableProperties.Systemserverclasspath_fragments)
6274 android.AssertArrayString(t, "Java_libs does not match",
6275 []string{"override_java_library"}, apexBundle.overridableProperties.Java_libs)
6276
Jiyong Park20bacab2020-03-03 11:45:41 +09006277 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006278 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006279 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6280
6281 signApkRule := module.Rule("signapk")
6282 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006283
Colin Crossaa255532020-07-03 13:18:24 -07006284 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006285 var builder strings.Builder
6286 data.Custom(&builder, name, "TARGET_", "", data)
6287 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09006288 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
markchien7c803b82021-08-26 22:10:06 +08006289 ensureContains(t, androidMk, "LOCAL_MODULE := override_bpf.o.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006290 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006291 ensureContains(t, androidMk, "LOCAL_MODULE := override_bcplib.override_myapex")
6292 ensureContains(t, androidMk, "LOCAL_MODULE := override_systemserverlib.override_myapex")
6293 ensureContains(t, androidMk, "LOCAL_MODULE := override_java_library.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006294 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006295 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006296 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006297 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006298 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006299 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006300 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_bcplib.myapex")
6301 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_systemserverlib.myapex")
6302 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_java_library.pb.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006303 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006304}
6305
Jooyung Han214bf372019-11-12 13:03:50 +09006306func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006307 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09006308 apex {
6309 name: "myapex",
6310 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006311 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09006312 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09006313 }
6314
6315 apex_key {
6316 name: "myapex.key",
6317 public_key: "testkey.avbpubkey",
6318 private_key: "testkey.pem",
6319 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006320
6321 cc_library {
6322 name: "mylib",
6323 srcs: ["mylib.cpp"],
6324 stl: "libc++",
6325 system_shared_libs: [],
6326 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09006327 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006328 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006329 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09006330
6331 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6332 args := module.Rule("apexRule").Args
6333 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00006334 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006335
6336 // The copies of the libraries in the apex should have one more dependency than
6337 // the ones outside the apex, namely the unwinder. Ideally we should check
6338 // the dependency names directly here but for some reason the names are blank in
6339 // this test.
6340 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07006341 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006342 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
6343 if len(apexImplicits) != len(nonApexImplicits)+1 {
6344 t.Errorf("%q missing unwinder dep", lib)
6345 }
6346 }
Jooyung Han214bf372019-11-12 13:03:50 +09006347}
6348
Paul Duffine05480a2021-03-08 15:07:14 +00006349var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01006350 "api/current.txt": nil,
6351 "api/removed.txt": nil,
6352 "api/system-current.txt": nil,
6353 "api/system-removed.txt": nil,
6354 "api/test-current.txt": nil,
6355 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01006356
Anton Hanssondff2c782020-12-21 17:10:01 +00006357 "100/public/api/foo.txt": nil,
6358 "100/public/api/foo-removed.txt": nil,
6359 "100/system/api/foo.txt": nil,
6360 "100/system/api/foo-removed.txt": nil,
6361
Paul Duffineedc5d52020-06-12 17:46:39 +01006362 // For java_sdk_library_import
6363 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01006364}
6365
Jooyung Han58f26ab2019-12-18 15:34:32 +09006366func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006367 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09006368 apex {
6369 name: "myapex",
6370 key: "myapex.key",
6371 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006372 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09006373 }
6374
6375 apex_key {
6376 name: "myapex.key",
6377 public_key: "testkey.avbpubkey",
6378 private_key: "testkey.pem",
6379 }
6380
6381 java_sdk_library {
6382 name: "foo",
6383 srcs: ["a.java"],
6384 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006385 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09006386 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006387
6388 prebuilt_apis {
6389 name: "sdk",
6390 api_dirs: ["100"],
6391 }
Paul Duffin9b879592020-05-26 13:21:35 +01006392 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09006393
6394 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00006395 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09006396 "javalib/foo.jar",
6397 "etc/permissions/foo.xml",
6398 })
6399 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09006400 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00006401 ensureMatches(t, sdkLibrary.RuleParams.Command, `<library\\n\s+name=\\\"foo\\\"\\n\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09006402}
6403
Paul Duffin9b879592020-05-26 13:21:35 +01006404func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006405 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006406 apex {
6407 name: "myapex",
6408 key: "myapex.key",
6409 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006410 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006411 }
6412
6413 apex_key {
6414 name: "myapex.key",
6415 public_key: "testkey.avbpubkey",
6416 private_key: "testkey.pem",
6417 }
6418
6419 java_sdk_library {
6420 name: "foo",
6421 srcs: ["a.java"],
6422 api_packages: ["foo"],
6423 apex_available: ["myapex"],
6424 sdk_version: "none",
6425 system_modules: "none",
6426 }
6427
6428 java_library {
6429 name: "bar",
6430 srcs: ["a.java"],
6431 libs: ["foo"],
6432 apex_available: ["myapex"],
6433 sdk_version: "none",
6434 system_modules: "none",
6435 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006436
6437 prebuilt_apis {
6438 name: "sdk",
6439 api_dirs: ["100"],
6440 }
Paul Duffin9b879592020-05-26 13:21:35 +01006441 `, withFiles(filesForSdkLibrary))
6442
6443 // java_sdk_library installs both impl jar and permission XML
6444 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6445 "javalib/bar.jar",
6446 "javalib/foo.jar",
6447 "etc/permissions/foo.xml",
6448 })
6449
6450 // The bar library should depend on the implementation jar.
6451 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006452 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006453 t.Errorf("expected %q, found %#q", expected, actual)
6454 }
6455}
6456
6457func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006458 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006459 apex {
6460 name: "myapex",
6461 key: "myapex.key",
6462 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006463 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006464 }
6465
6466 apex_key {
6467 name: "myapex.key",
6468 public_key: "testkey.avbpubkey",
6469 private_key: "testkey.pem",
6470 }
6471
6472 java_sdk_library {
6473 name: "foo",
6474 srcs: ["a.java"],
6475 api_packages: ["foo"],
6476 apex_available: ["myapex"],
6477 sdk_version: "none",
6478 system_modules: "none",
6479 }
6480
6481 java_library {
6482 name: "bar",
6483 srcs: ["a.java"],
6484 libs: ["foo"],
6485 sdk_version: "none",
6486 system_modules: "none",
6487 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006488
6489 prebuilt_apis {
6490 name: "sdk",
6491 api_dirs: ["100"],
6492 }
Paul Duffin9b879592020-05-26 13:21:35 +01006493 `, withFiles(filesForSdkLibrary))
6494
6495 // java_sdk_library installs both impl jar and permission XML
6496 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6497 "javalib/foo.jar",
6498 "etc/permissions/foo.xml",
6499 })
6500
6501 // The bar library should depend on the stubs jar.
6502 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006503 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006504 t.Errorf("expected %q, found %#q", expected, actual)
6505 }
6506}
6507
Paul Duffineedc5d52020-06-12 17:46:39 +01006508func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006509 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00006510 prebuilt_apis {
6511 name: "sdk",
6512 api_dirs: ["100"],
6513 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01006514 withFiles(map[string][]byte{
6515 "apex/a.java": nil,
6516 "apex/apex_manifest.json": nil,
6517 "apex/Android.bp": []byte(`
6518 package {
6519 default_visibility: ["//visibility:private"],
6520 }
6521
6522 apex {
6523 name: "myapex",
6524 key: "myapex.key",
6525 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006526 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006527 }
6528
6529 apex_key {
6530 name: "myapex.key",
6531 public_key: "testkey.avbpubkey",
6532 private_key: "testkey.pem",
6533 }
6534
6535 java_library {
6536 name: "bar",
6537 srcs: ["a.java"],
6538 libs: ["foo"],
6539 apex_available: ["myapex"],
6540 sdk_version: "none",
6541 system_modules: "none",
6542 }
6543`),
6544 "source/a.java": nil,
6545 "source/api/current.txt": nil,
6546 "source/api/removed.txt": nil,
6547 "source/Android.bp": []byte(`
6548 package {
6549 default_visibility: ["//visibility:private"],
6550 }
6551
6552 java_sdk_library {
6553 name: "foo",
6554 visibility: ["//apex"],
6555 srcs: ["a.java"],
6556 api_packages: ["foo"],
6557 apex_available: ["myapex"],
6558 sdk_version: "none",
6559 system_modules: "none",
6560 public: {
6561 enabled: true,
6562 },
6563 }
6564`),
6565 "prebuilt/a.jar": nil,
6566 "prebuilt/Android.bp": []byte(`
6567 package {
6568 default_visibility: ["//visibility:private"],
6569 }
6570
6571 java_sdk_library_import {
6572 name: "foo",
6573 visibility: ["//apex", "//source"],
6574 apex_available: ["myapex"],
6575 prefer: true,
6576 public: {
6577 jars: ["a.jar"],
6578 },
6579 }
6580`),
Anton Hanssondff2c782020-12-21 17:10:01 +00006581 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01006582 )
6583
6584 // java_sdk_library installs both impl jar and permission XML
6585 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6586 "javalib/bar.jar",
6587 "javalib/foo.jar",
6588 "etc/permissions/foo.xml",
6589 })
6590
6591 // The bar library should depend on the implementation jar.
6592 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006593 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01006594 t.Errorf("expected %q, found %#q", expected, actual)
6595 }
6596}
6597
6598func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
6599 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
6600 apex {
6601 name: "myapex",
6602 key: "myapex.key",
6603 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006604 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006605 }
6606
6607 apex_key {
6608 name: "myapex.key",
6609 public_key: "testkey.avbpubkey",
6610 private_key: "testkey.pem",
6611 }
6612
6613 java_sdk_library_import {
6614 name: "foo",
6615 apex_available: ["myapex"],
6616 prefer: true,
6617 public: {
6618 jars: ["a.jar"],
6619 },
6620 }
6621
6622 `, withFiles(filesForSdkLibrary))
6623}
6624
atrost6e126252020-01-27 17:01:16 +00006625func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01006626 result := android.GroupFixturePreparers(
6627 prepareForApexTest,
6628 java.PrepareForTestWithPlatformCompatConfig,
6629 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00006630 apex {
6631 name: "myapex",
6632 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00006633 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00006634 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006635 updatable: false,
atrost6e126252020-01-27 17:01:16 +00006636 }
6637
6638 apex_key {
6639 name: "myapex.key",
6640 public_key: "testkey.avbpubkey",
6641 private_key: "testkey.pem",
6642 }
6643
6644 platform_compat_config {
6645 name: "myjar-platform-compat-config",
6646 src: ":myjar",
6647 }
6648
6649 java_library {
6650 name: "myjar",
6651 srcs: ["foo/bar/MyClass.java"],
6652 sdk_version: "none",
6653 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00006654 apex_available: [ "myapex" ],
6655 }
Paul Duffin1b29e002021-03-16 15:06:54 +00006656
6657 // Make sure that a preferred prebuilt does not affect the apex contents.
6658 prebuilt_platform_compat_config {
6659 name: "myjar-platform-compat-config",
6660 metadata: "compat-config/metadata.xml",
6661 prefer: true,
6662 }
atrost6e126252020-01-27 17:01:16 +00006663 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00006664 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00006665 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6666 "etc/compatconfig/myjar-platform-compat-config.xml",
6667 "javalib/myjar.jar",
6668 })
6669}
6670
Jiyong Park479321d2019-12-16 11:47:12 +09006671func TestRejectNonInstallableJavaLibrary(t *testing.T) {
6672 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
6673 apex {
6674 name: "myapex",
6675 key: "myapex.key",
6676 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006677 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09006678 }
6679
6680 apex_key {
6681 name: "myapex.key",
6682 public_key: "testkey.avbpubkey",
6683 private_key: "testkey.pem",
6684 }
6685
6686 java_library {
6687 name: "myjar",
6688 srcs: ["foo/bar/MyClass.java"],
6689 sdk_version: "none",
6690 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09006691 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006692 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09006693 }
6694 `)
6695}
6696
Jiyong Park7afd1072019-12-30 16:56:33 +09006697func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006698 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09006699 apex {
6700 name: "myapex",
6701 key: "myapex.key",
6702 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006703 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09006704 }
6705
6706 apex_key {
6707 name: "myapex.key",
6708 public_key: "testkey.avbpubkey",
6709 private_key: "testkey.pem",
6710 }
6711
6712 cc_library {
6713 name: "mylib",
6714 srcs: ["mylib.cpp"],
6715 system_shared_libs: [],
6716 stl: "none",
6717 required: ["a", "b"],
6718 host_required: ["c", "d"],
6719 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006720 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006721 }
6722 `)
6723
6724 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006725 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006726 name := apexBundle.BaseModuleName()
6727 prefix := "TARGET_"
6728 var builder strings.Builder
6729 data.Custom(&builder, name, prefix, "", data)
6730 androidMk := builder.String()
6731 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6732 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6733 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6734}
6735
Jiyong Park7cd10e32020-01-14 09:22:18 +09006736func TestSymlinksFromApexToSystem(t *testing.T) {
6737 bp := `
6738 apex {
6739 name: "myapex",
6740 key: "myapex.key",
6741 native_shared_libs: ["mylib"],
6742 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006743 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006744 }
6745
Jiyong Park9d677202020-02-19 16:29:35 +09006746 apex {
6747 name: "myapex.updatable",
6748 key: "myapex.key",
6749 native_shared_libs: ["mylib"],
6750 java_libs: ["myjar"],
6751 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006752 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006753 }
6754
Jiyong Park7cd10e32020-01-14 09:22:18 +09006755 apex_key {
6756 name: "myapex.key",
6757 public_key: "testkey.avbpubkey",
6758 private_key: "testkey.pem",
6759 }
6760
6761 cc_library {
6762 name: "mylib",
6763 srcs: ["mylib.cpp"],
6764 shared_libs: ["myotherlib"],
6765 system_shared_libs: [],
6766 stl: "none",
6767 apex_available: [
6768 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006769 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006770 "//apex_available:platform",
6771 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006772 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006773 }
6774
6775 cc_library {
6776 name: "myotherlib",
6777 srcs: ["mylib.cpp"],
6778 system_shared_libs: [],
6779 stl: "none",
6780 apex_available: [
6781 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006782 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006783 "//apex_available:platform",
6784 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006785 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006786 }
6787
6788 java_library {
6789 name: "myjar",
6790 srcs: ["foo/bar/MyClass.java"],
6791 sdk_version: "none",
6792 system_modules: "none",
6793 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006794 apex_available: [
6795 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006796 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006797 "//apex_available:platform",
6798 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006799 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006800 }
6801
6802 java_library {
6803 name: "myotherjar",
6804 srcs: ["foo/bar/MyClass.java"],
6805 sdk_version: "none",
6806 system_modules: "none",
6807 apex_available: [
6808 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006809 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006810 "//apex_available:platform",
6811 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006812 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006813 }
6814 `
6815
6816 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6817 for _, f := range files {
6818 if f.path == file {
6819 if f.isLink {
6820 t.Errorf("%q is not a real file", file)
6821 }
6822 return
6823 }
6824 }
6825 t.Errorf("%q is not found", file)
6826 }
6827
6828 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6829 for _, f := range files {
6830 if f.path == file {
6831 if !f.isLink {
6832 t.Errorf("%q is not a symlink", file)
6833 }
6834 return
6835 }
6836 }
6837 t.Errorf("%q is not found", file)
6838 }
6839
Jiyong Park9d677202020-02-19 16:29:35 +09006840 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6841 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006842 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006843 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006844 ensureRealfileExists(t, files, "javalib/myjar.jar")
6845 ensureRealfileExists(t, files, "lib64/mylib.so")
6846 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6847
Jiyong Park9d677202020-02-19 16:29:35 +09006848 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6849 ensureRealfileExists(t, files, "javalib/myjar.jar")
6850 ensureRealfileExists(t, files, "lib64/mylib.so")
6851 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6852
6853 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006854 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006855 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006856 ensureRealfileExists(t, files, "javalib/myjar.jar")
6857 ensureRealfileExists(t, files, "lib64/mylib.so")
6858 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006859
6860 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6861 ensureRealfileExists(t, files, "javalib/myjar.jar")
6862 ensureRealfileExists(t, files, "lib64/mylib.so")
6863 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006864}
6865
Yo Chiange8128052020-07-23 20:09:18 +08006866func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006867 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006868 apex {
6869 name: "myapex",
6870 key: "myapex.key",
6871 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006872 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006873 }
6874
6875 apex_key {
6876 name: "myapex.key",
6877 public_key: "testkey.avbpubkey",
6878 private_key: "testkey.pem",
6879 }
6880
6881 cc_library_shared {
6882 name: "mylib",
6883 srcs: ["mylib.cpp"],
6884 shared_libs: ["myotherlib"],
6885 system_shared_libs: [],
6886 stl: "none",
6887 apex_available: [
6888 "myapex",
6889 "//apex_available:platform",
6890 ],
6891 }
6892
6893 cc_prebuilt_library_shared {
6894 name: "myotherlib",
6895 srcs: ["prebuilt.so"],
6896 system_shared_libs: [],
6897 stl: "none",
6898 apex_available: [
6899 "myapex",
6900 "//apex_available:platform",
6901 ],
6902 }
6903 `)
6904
6905 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006906 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006907 var builder strings.Builder
6908 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6909 androidMk := builder.String()
6910 // `myotherlib` is added to `myapex` as symlink
6911 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6912 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6913 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6914 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006915 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += mylib.myapex:64 myotherlib:64 apex_manifest.pb.myapex apex_pubkey.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08006916}
6917
Jooyung Han643adc42020-02-27 13:50:06 +09006918func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006919 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006920 apex {
6921 name: "myapex",
6922 key: "myapex.key",
Jiyong Park34d5c332022-02-24 18:02:44 +09006923 jni_libs: ["mylib", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006924 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006925 }
6926
6927 apex_key {
6928 name: "myapex.key",
6929 public_key: "testkey.avbpubkey",
6930 private_key: "testkey.pem",
6931 }
6932
6933 cc_library {
6934 name: "mylib",
6935 srcs: ["mylib.cpp"],
6936 shared_libs: ["mylib2"],
6937 system_shared_libs: [],
6938 stl: "none",
6939 apex_available: [ "myapex" ],
6940 }
6941
6942 cc_library {
6943 name: "mylib2",
6944 srcs: ["mylib.cpp"],
6945 system_shared_libs: [],
6946 stl: "none",
6947 apex_available: [ "myapex" ],
6948 }
Jiyong Park34d5c332022-02-24 18:02:44 +09006949
6950 rust_ffi_shared {
6951 name: "libfoo.rust",
6952 crate_name: "foo",
6953 srcs: ["foo.rs"],
6954 shared_libs: ["libfoo.shared_from_rust"],
6955 prefer_rlib: true,
6956 apex_available: ["myapex"],
6957 }
6958
6959 cc_library_shared {
6960 name: "libfoo.shared_from_rust",
6961 srcs: ["mylib.cpp"],
6962 system_shared_libs: [],
6963 stl: "none",
6964 stubs: {
6965 versions: ["10", "11", "12"],
6966 },
6967 }
6968
Jooyung Han643adc42020-02-27 13:50:06 +09006969 `)
6970
6971 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6972 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiyong Park34d5c332022-02-24 18:02:44 +09006973 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so")
Jooyung Han643adc42020-02-27 13:50:06 +09006974 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6975 "lib64/mylib.so",
6976 "lib64/mylib2.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09006977 "lib64/libfoo.rust.so",
6978 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
6979 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09006980 })
Jiyong Park34d5c332022-02-24 18:02:44 +09006981
6982 // b/220397949
6983 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09006984}
6985
Jooyung Han49f67012020-04-17 13:43:10 +09006986func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006987 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006988 apex {
6989 name: "myapex",
6990 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006991 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006992 }
6993 apex_key {
6994 name: "myapex.key",
6995 public_key: "testkey.avbpubkey",
6996 private_key: "testkey.pem",
6997 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006998 `,
6999 android.FixtureModifyConfig(func(config android.Config) {
7000 delete(config.Targets, android.Android)
7001 config.AndroidCommonTarget = android.Target{}
7002 }),
7003 )
Jooyung Han49f67012020-04-17 13:43:10 +09007004
7005 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7006 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7007 }
7008}
7009
Jiyong Parkbd159612020-02-28 15:22:21 +09007010func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007011 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007012 apex {
7013 name: "myapex",
7014 key: "myapex.key",
7015 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007016 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007017 }
7018
7019 apex_key {
7020 name: "myapex.key",
7021 public_key: "testkey.avbpubkey",
7022 private_key: "testkey.pem",
7023 }
7024
7025 android_app {
7026 name: "AppFoo",
7027 srcs: ["foo/bar/MyClass.java"],
7028 sdk_version: "none",
7029 system_modules: "none",
7030 apex_available: [ "myapex" ],
7031 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007032 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007033
Colin Crosscf371cc2020-11-13 11:48:42 -08007034 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09007035 content := bundleConfigRule.Args["content"]
7036
7037 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007038 ensureContains(t, content, `"apex_config":{"apex_embedded_apk_config":[{"package_name":"com.android.foo","path":"app/AppFoo/AppFoo.apk"}]}`)
Jiyong Parkbd159612020-02-28 15:22:21 +09007039}
7040
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007041func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007042 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007043 apex {
7044 name: "myapex",
7045 key: "myapex.key",
7046 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007047 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007048 }
7049
7050 apex_key {
7051 name: "myapex.key",
7052 public_key: "testkey.avbpubkey",
7053 private_key: "testkey.pem",
7054 }
7055
7056 android_app_set {
7057 name: "AppSet",
7058 set: "AppSet.apks",
7059 }`)
7060 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08007061 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007062 content := bundleConfigRule.Args["content"]
7063 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7064 s := mod.Rule("apexRule").Args["copy_commands"]
7065 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
7066 if len(copyCmds) != 3 {
7067 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
7068 }
7069 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
7070 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
7071 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
7072}
7073
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007074func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007075 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007076 apex_set {
7077 name: "myapex",
7078 filename: "foo_v2.apex",
7079 sanitized: {
7080 none: { set: "myapex.apks", },
7081 hwaddress: { set: "myapex.hwasan.apks", },
7082 },
Paul Duffin24704672021-04-06 16:09:30 +01007083 }
7084 `
7085 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007086
Paul Duffin24704672021-04-06 16:09:30 +01007087 // Check that the extractor produces the correct output file from the correct input file.
7088 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007089
Paul Duffin24704672021-04-06 16:09:30 +01007090 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7091 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007092
Paul Duffin24704672021-04-06 16:09:30 +01007093 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7094
7095 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007096 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7097 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007098
7099 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007100}
7101
Paul Duffin89f570a2021-06-16 01:42:33 +01007102func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007103 t.Helper()
7104
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007105 bp := `
7106 java_library {
7107 name: "some-updatable-apex-lib",
7108 srcs: ["a.java"],
7109 sdk_version: "current",
7110 apex_available: [
7111 "some-updatable-apex",
7112 ],
satayevabcd5972021-08-06 17:49:46 +01007113 permitted_packages: ["some.updatable.apex.lib"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007114 }
7115
7116 java_library {
7117 name: "some-non-updatable-apex-lib",
7118 srcs: ["a.java"],
7119 apex_available: [
7120 "some-non-updatable-apex",
7121 ],
Paul Duffin89f570a2021-06-16 01:42:33 +01007122 compile_dex: true,
satayevabcd5972021-08-06 17:49:46 +01007123 permitted_packages: ["some.non.updatable.apex.lib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007124 }
7125
7126 bootclasspath_fragment {
7127 name: "some-non-updatable-fragment",
7128 contents: ["some-non-updatable-apex-lib"],
7129 apex_available: [
7130 "some-non-updatable-apex",
7131 ],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007132 }
7133
7134 java_library {
7135 name: "some-platform-lib",
7136 srcs: ["a.java"],
7137 sdk_version: "current",
7138 installable: true,
7139 }
7140
7141 java_library {
7142 name: "some-art-lib",
7143 srcs: ["a.java"],
7144 sdk_version: "current",
7145 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00007146 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007147 ],
7148 hostdex: true,
Paul Duffine5218812021-06-07 13:28:19 +01007149 compile_dex: true,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007150 }
7151
7152 apex {
7153 name: "some-updatable-apex",
7154 key: "some-updatable-apex.key",
7155 java_libs: ["some-updatable-apex-lib"],
7156 updatable: true,
7157 min_sdk_version: "current",
7158 }
7159
7160 apex {
7161 name: "some-non-updatable-apex",
7162 key: "some-non-updatable-apex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007163 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007164 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007165 }
7166
7167 apex_key {
7168 name: "some-updatable-apex.key",
7169 }
7170
7171 apex_key {
7172 name: "some-non-updatable-apex.key",
7173 }
7174
7175 apex {
Paul Duffind376f792021-01-26 11:59:35 +00007176 name: "com.android.art.debug",
7177 key: "com.android.art.debug.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007178 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007179 updatable: true,
7180 min_sdk_version: "current",
7181 }
7182
Paul Duffinf23bc472021-04-27 12:42:20 +01007183 bootclasspath_fragment {
7184 name: "art-bootclasspath-fragment",
7185 image_name: "art",
7186 contents: ["some-art-lib"],
7187 apex_available: [
7188 "com.android.art.debug",
7189 ],
7190 }
7191
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007192 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00007193 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007194 }
7195
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007196 filegroup {
7197 name: "some-updatable-apex-file_contexts",
7198 srcs: [
7199 "system/sepolicy/apex/some-updatable-apex-file_contexts",
7200 ],
7201 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007202
7203 filegroup {
7204 name: "some-non-updatable-apex-file_contexts",
7205 srcs: [
7206 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
7207 ],
7208 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007209 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00007210
Paul Duffin89f570a2021-06-16 01:42:33 +01007211 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00007212}
7213
Paul Duffin89f570a2021-06-16 01:42:33 +01007214func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007215 t.Helper()
7216
Paul Duffin55607122021-03-30 23:32:51 +01007217 fs := android.MockFS{
7218 "a.java": nil,
7219 "a.jar": nil,
7220 "apex_manifest.json": nil,
7221 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007222 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007223 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7224 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7225 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007226 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007227 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007228
Paul Duffin55607122021-03-30 23:32:51 +01007229 errorHandler := android.FixtureExpectsNoErrors
7230 if errmsg != "" {
7231 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007232 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007233
Paul Duffin55607122021-03-30 23:32:51 +01007234 result := android.GroupFixturePreparers(
7235 cc.PrepareForTestWithCcDefaultModules,
7236 java.PrepareForTestWithHiddenApiBuildComponents,
7237 java.PrepareForTestWithJavaDefaultModules,
7238 java.PrepareForTestWithJavaSdkLibraryFiles,
7239 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007240 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007241 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007242 android.FixtureModifyMockFS(func(fs android.MockFS) {
7243 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7244 insert := ""
7245 for _, fragment := range fragments {
7246 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7247 }
7248 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7249 platform_bootclasspath {
7250 name: "platform-bootclasspath",
7251 fragments: [
7252 %s
7253 ],
7254 }
7255 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01007256 }
Paul Duffin89f570a2021-06-16 01:42:33 +01007257 }),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00007258 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01007259 ).
7260 ExtendWithErrorHandler(errorHandler).
7261 RunTestWithBp(t, bp)
7262
7263 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007264}
7265
Martin Stjernholm43c44b02021-06-30 16:35:07 +01007266func TestDuplicateDeapexeresFromPrebuiltApexes(t *testing.T) {
7267 preparers := android.GroupFixturePreparers(
7268 java.PrepareForTestWithJavaDefaultModules,
7269 PrepareForTestWithApexBuildComponents,
7270 ).
7271 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
7272 "Multiple installable prebuilt APEXes provide ambiguous deapexers: com.android.myapex and com.mycompany.android.myapex"))
7273
7274 bpBase := `
7275 apex_set {
7276 name: "com.android.myapex",
7277 installable: true,
7278 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7279 set: "myapex.apks",
7280 }
7281
7282 apex_set {
7283 name: "com.mycompany.android.myapex",
7284 apex_name: "com.android.myapex",
7285 installable: true,
7286 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7287 set: "company-myapex.apks",
7288 }
7289
7290 prebuilt_bootclasspath_fragment {
7291 name: "my-bootclasspath-fragment",
7292 apex_available: ["com.android.myapex"],
7293 %s
7294 }
7295 `
7296
7297 t.Run("java_import", func(t *testing.T) {
7298 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7299 java_import {
7300 name: "libfoo",
7301 jars: ["libfoo.jar"],
7302 apex_available: ["com.android.myapex"],
7303 }
7304 `)
7305 })
7306
7307 t.Run("java_sdk_library_import", func(t *testing.T) {
7308 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7309 java_sdk_library_import {
7310 name: "libfoo",
7311 public: {
7312 jars: ["libbar.jar"],
7313 },
7314 apex_available: ["com.android.myapex"],
7315 }
7316 `)
7317 })
7318
7319 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7320 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7321 image_name: "art",
7322 contents: ["libfoo"],
7323 `)+`
7324 java_sdk_library_import {
7325 name: "libfoo",
7326 public: {
7327 jars: ["libbar.jar"],
7328 },
7329 apex_available: ["com.android.myapex"],
7330 }
7331 `)
7332 })
7333}
7334
Jooyung Han548640b2020-04-27 12:10:30 +09007335func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
7336 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7337 apex {
7338 name: "myapex",
7339 key: "myapex.key",
7340 updatable: true,
7341 }
7342
7343 apex_key {
7344 name: "myapex.key",
7345 public_key: "testkey.avbpubkey",
7346 private_key: "testkey.pem",
7347 }
7348 `)
7349}
7350
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007351func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
7352 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7353 apex {
7354 name: "myapex",
7355 key: "myapex.key",
7356 }
7357
7358 apex_key {
7359 name: "myapex.key",
7360 public_key: "testkey.avbpubkey",
7361 private_key: "testkey.pem",
7362 }
7363 `)
7364}
7365
Daniel Norman69109112021-12-02 12:52:42 -08007366func TestUpdatable_cannot_be_vendor_apex(t *testing.T) {
7367 testApexError(t, `"myapex" .*: updatable: vendor APEXes are not updatable`, `
7368 apex {
7369 name: "myapex",
7370 key: "myapex.key",
7371 updatable: true,
7372 soc_specific: true,
7373 }
7374
7375 apex_key {
7376 name: "myapex.key",
7377 public_key: "testkey.avbpubkey",
7378 private_key: "testkey.pem",
7379 }
7380 `)
7381}
7382
satayevb98371c2021-06-15 16:49:50 +01007383func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
7384 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
7385 apex {
7386 name: "myapex",
7387 key: "myapex.key",
7388 systemserverclasspath_fragments: [
7389 "mysystemserverclasspathfragment",
7390 ],
7391 min_sdk_version: "29",
7392 updatable: true,
7393 }
7394
7395 apex_key {
7396 name: "myapex.key",
7397 public_key: "testkey.avbpubkey",
7398 private_key: "testkey.pem",
7399 }
7400
7401 java_library {
7402 name: "foo",
7403 srcs: ["b.java"],
7404 min_sdk_version: "29",
7405 installable: true,
7406 apex_available: [
7407 "myapex",
7408 ],
7409 }
7410
7411 systemserverclasspath_fragment {
7412 name: "mysystemserverclasspathfragment",
7413 generate_classpaths_proto: false,
7414 contents: [
7415 "foo",
7416 ],
7417 apex_available: [
7418 "myapex",
7419 ],
7420 }
satayevabcd5972021-08-06 17:49:46 +01007421 `,
7422 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
7423 )
satayevb98371c2021-06-15 16:49:50 +01007424}
7425
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007426func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007427 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
7428 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
7429 // modules to be included in the BootJars.
7430 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
7431 return android.GroupFixturePreparers(
7432 dexpreopt.FixtureSetBootJars(bootJars...),
7433 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7434 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7435 }),
7436 )
7437 }
7438
7439 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
7440 // same value. This can result in an invalid configuration as it allows non art apex jars to be
7441 // specified in the ArtApexJars configuration.
7442 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
7443 return android.GroupFixturePreparers(
7444 dexpreopt.FixtureSetArtBootJars(bootJars...),
7445 dexpreopt.FixtureSetBootJars(bootJars...),
7446 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7447 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7448 }),
7449 )
7450 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007451
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007452 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007453 preparer := android.GroupFixturePreparers(
7454 java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib"),
7455 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7456 )
7457 fragments := []java.ApexVariantReference{
7458 {
7459 Apex: proptools.StringPtr("com.android.art.debug"),
7460 Module: proptools.StringPtr("art-bootclasspath-fragment"),
7461 },
7462 {
7463 Apex: proptools.StringPtr("some-non-updatable-apex"),
7464 Module: proptools.StringPtr("some-non-updatable-fragment"),
7465 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007466 }
satayevabcd5972021-08-06 17:49:46 +01007467 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007468 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007469
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007470 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007471 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
7472 // Update the dexpreopt BootJars directly.
satayevabcd5972021-08-06 17:49:46 +01007473 preparer := android.GroupFixturePreparers(
7474 prepareSetBootJars("com.android.art.debug:some-art-lib"),
7475 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7476 )
Paul Duffin60264a02021-04-12 20:02:36 +01007477 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007478 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007479
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007480 t.Run("updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01007481 err := `ArtApexJars expects this to be in apex "some-updatable-apex" but this is only in apexes.*"com.android.art.debug"`
Paul Duffin60264a02021-04-12 20:02:36 +01007482 // Update the dexpreopt ArtApexJars directly.
7483 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
7484 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007485 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007486
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007487 t.Run("non-updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01007488 err := `ArtApexJars expects this to be in apex "some-non-updatable-apex" but this is only in apexes.*"com.android.art.debug"`
Paul Duffin60264a02021-04-12 20:02:36 +01007489 // Update the dexpreopt ArtApexJars directly.
7490 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
7491 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007492 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007493
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007494 t.Run("updatable jar from some other apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007495 err := `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the framework boot image`
satayevabcd5972021-08-06 17:49:46 +01007496 preparer := android.GroupFixturePreparers(
7497 java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib"),
7498 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7499 )
Paul Duffin60264a02021-04-12 20:02:36 +01007500 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007501 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007502
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007503 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007504 preparer := java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01007505 fragment := java.ApexVariantReference{
7506 Apex: proptools.StringPtr("some-non-updatable-apex"),
7507 Module: proptools.StringPtr("some-non-updatable-fragment"),
7508 }
7509 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007510 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007511
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007512 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007513 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007514 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7515 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007516 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007517
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007518 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007519 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007520 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7521 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007522 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007523
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007524 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01007525 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01007526 // Update the dexpreopt ArtApexJars directly.
7527 preparer := prepareSetArtJars("platform:some-platform-lib")
7528 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007529 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007530
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007531 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007532 preparer := android.GroupFixturePreparers(
7533 java.FixtureConfigureBootJars("platform:some-platform-lib"),
7534 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7535 )
7536 fragments := []java.ApexVariantReference{
7537 {
7538 Apex: proptools.StringPtr("some-non-updatable-apex"),
7539 Module: proptools.StringPtr("some-non-updatable-fragment"),
7540 },
7541 }
7542 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007543 })
Paul Duffin064b70c2020-11-02 17:32:38 +00007544}
7545
7546func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007547 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00007548 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01007549 fragment := java.ApexVariantReference{
7550 Apex: proptools.StringPtr("myapex"),
7551 Module: proptools.StringPtr("my-bootclasspath-fragment"),
7552 }
7553
Paul Duffin064b70c2020-11-02 17:32:38 +00007554 testDexpreoptWithApexes(t, `
7555 prebuilt_apex {
7556 name: "myapex" ,
7557 arch: {
7558 arm64: {
7559 src: "myapex-arm64.apex",
7560 },
7561 arm: {
7562 src: "myapex-arm.apex",
7563 },
7564 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007565 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7566 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007567
Paul Duffin89f570a2021-06-16 01:42:33 +01007568 prebuilt_bootclasspath_fragment {
7569 name: "my-bootclasspath-fragment",
7570 contents: ["libfoo"],
7571 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01007572 hidden_api: {
7573 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
7574 metadata: "my-bootclasspath-fragment/metadata.csv",
7575 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01007576 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
7577 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
7578 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01007579 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007580 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007581
Paul Duffin89f570a2021-06-16 01:42:33 +01007582 java_import {
7583 name: "libfoo",
7584 jars: ["libfoo.jar"],
7585 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01007586 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007587 }
7588 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00007589 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007590}
7591
satayevd604b212021-07-21 14:23:52 +01007592func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01007593 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01007594 bp += `
7595 apex_key {
7596 name: "myapex.key",
7597 public_key: "testkey.avbpubkey",
7598 private_key: "testkey.pem",
7599 }`
Paul Duffin45338f02021-03-30 23:07:52 +01007600 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01007601 "lib1/src/A.java": nil,
7602 "lib2/src/B.java": nil,
7603 "system/sepolicy/apex/myapex-file_contexts": nil,
7604 }
7605
Paul Duffin45338f02021-03-30 23:07:52 +01007606 errorHandler := android.FixtureExpectsNoErrors
7607 if errmsg != "" {
7608 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07007609 }
Colin Crossae8600b2020-10-29 17:09:13 -07007610
Paul Duffin45338f02021-03-30 23:07:52 +01007611 android.GroupFixturePreparers(
7612 android.PrepareForTestWithAndroidBuildComponents,
7613 java.PrepareForTestWithJavaBuildComponents,
7614 PrepareForTestWithApexBuildComponents,
7615 android.PrepareForTestWithNeverallowRules(rules),
7616 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01007617 apexBootJars := make([]string, 0, len(bootJars))
7618 for _, apexBootJar := range bootJars {
7619 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01007620 }
satayevd604b212021-07-21 14:23:52 +01007621 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01007622 }),
7623 fs.AddToFixture(),
7624 ).
7625 ExtendWithErrorHandler(errorHandler).
7626 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01007627}
7628
7629func TestApexPermittedPackagesRules(t *testing.T) {
7630 testcases := []struct {
7631 name string
7632 expectedError string
7633 bp string
7634 bootJars []string
7635 modulesPackages map[string][]string
7636 }{
7637
7638 {
7639 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
7640 expectedError: "",
7641 bp: `
7642 java_library {
7643 name: "bcp_lib1",
7644 srcs: ["lib1/src/*.java"],
7645 permitted_packages: ["foo.bar"],
7646 apex_available: ["myapex"],
7647 sdk_version: "none",
7648 system_modules: "none",
Remi NGUYEN VAN1fdd6ca2021-12-02 19:39:35 +09007649 min_sdk_version: "30",
Andrei Onea115e7e72020-06-05 21:14:03 +01007650 }
7651 java_library {
7652 name: "nonbcp_lib2",
7653 srcs: ["lib2/src/*.java"],
7654 apex_available: ["myapex"],
7655 permitted_packages: ["a.b"],
7656 sdk_version: "none",
7657 system_modules: "none",
Remi NGUYEN VAN1fdd6ca2021-12-02 19:39:35 +09007658 min_sdk_version: "30",
Andrei Onea115e7e72020-06-05 21:14:03 +01007659 }
7660 apex {
7661 name: "myapex",
Remi NGUYEN VAN1fdd6ca2021-12-02 19:39:35 +09007662 min_sdk_version: "30",
Andrei Onea115e7e72020-06-05 21:14:03 +01007663 key: "myapex.key",
7664 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007665 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01007666 }`,
7667 bootJars: []string{"bcp_lib1"},
7668 modulesPackages: map[string][]string{
7669 "myapex": []string{
7670 "foo.bar",
7671 },
7672 },
7673 },
7674 {
Remi NGUYEN VAN1fdd6ca2021-12-02 19:39:35 +09007675 name: "Bootclasspath apex jar not satisfying allowed module packages on Q.",
Andrei Onead967aee2022-01-19 15:36:40 +00007676 expectedError: `(?s)module "bcp_lib2" .* which is restricted because jars that are part of the myapex module may only use these package prefixes: foo.bar with min_sdk < T. Please consider the following alternatives:\n 1. If the offending code is from a statically linked library, consider removing that dependency and using an alternative already in the bootclasspath, or perhaps a shared library. 2. Move the offending code into an allowed package.\n 3. Jarjar the offending code. Please be mindful of the potential system health implications of bundling that code, particularly if the offending jar is part of the bootclasspath.`,
Andrei Onea115e7e72020-06-05 21:14:03 +01007677 bp: `
7678 java_library {
7679 name: "bcp_lib1",
7680 srcs: ["lib1/src/*.java"],
7681 apex_available: ["myapex"],
7682 permitted_packages: ["foo.bar"],
7683 sdk_version: "none",
7684 system_modules: "none",
Remi NGUYEN VAN1fdd6ca2021-12-02 19:39:35 +09007685 min_sdk_version: "29",
Andrei Onea115e7e72020-06-05 21:14:03 +01007686 }
7687 java_library {
7688 name: "bcp_lib2",
7689 srcs: ["lib2/src/*.java"],
7690 apex_available: ["myapex"],
7691 permitted_packages: ["foo.bar", "bar.baz"],
7692 sdk_version: "none",
7693 system_modules: "none",
Remi NGUYEN VAN1fdd6ca2021-12-02 19:39:35 +09007694 min_sdk_version: "29",
Andrei Onea115e7e72020-06-05 21:14:03 +01007695 }
7696 apex {
7697 name: "myapex",
Remi NGUYEN VAN1fdd6ca2021-12-02 19:39:35 +09007698 min_sdk_version: "29",
7699 key: "myapex.key",
7700 java_libs: ["bcp_lib1", "bcp_lib2"],
7701 updatable: false,
7702 }
7703 `,
7704 bootJars: []string{"bcp_lib1", "bcp_lib2"},
7705 modulesPackages: map[string][]string{
7706 "myapex": []string{
7707 "foo.bar",
7708 },
7709 },
7710 },
7711 {
7712 name: "Bootclasspath apex jar not satisfying allowed module packages on R.",
Andrei Onead967aee2022-01-19 15:36:40 +00007713 expectedError: `(?s)module "bcp_lib2" .* which is restricted because jars that are part of the myapex module may only use these package prefixes: foo.bar with min_sdk < T. Please consider the following alternatives:\n 1. If the offending code is from a statically linked library, consider removing that dependency and using an alternative already in the bootclasspath, or perhaps a shared library. 2. Move the offending code into an allowed package.\n 3. Jarjar the offending code. Please be mindful of the potential system health implications of bundling that code, particularly if the offending jar is part of the bootclasspath.`,
Remi NGUYEN VAN1fdd6ca2021-12-02 19:39:35 +09007714 bp: `
7715 java_library {
7716 name: "bcp_lib1",
7717 srcs: ["lib1/src/*.java"],
7718 apex_available: ["myapex"],
7719 permitted_packages: ["foo.bar"],
7720 sdk_version: "none",
7721 system_modules: "none",
7722 min_sdk_version: "30",
7723 }
7724 java_library {
7725 name: "bcp_lib2",
7726 srcs: ["lib2/src/*.java"],
7727 apex_available: ["myapex"],
7728 permitted_packages: ["foo.bar", "bar.baz"],
7729 sdk_version: "none",
7730 system_modules: "none",
7731 min_sdk_version: "30",
7732 }
7733 apex {
7734 name: "myapex",
7735 min_sdk_version: "30",
7736 key: "myapex.key",
7737 java_libs: ["bcp_lib1", "bcp_lib2"],
7738 updatable: false,
7739 }
7740 `,
7741 bootJars: []string{"bcp_lib1", "bcp_lib2"},
7742 modulesPackages: map[string][]string{
7743 "myapex": []string{
7744 "foo.bar",
7745 },
7746 },
7747 },
7748 {
7749 name: "Bootclasspath apex jar >= T not satisfying Q/R/S allowed module packages.",
7750 expectedError: "",
7751 bp: `
7752 java_library {
7753 name: "bcp_lib1",
7754 srcs: ["lib1/src/*.java"],
7755 apex_available: ["myapex"],
7756 permitted_packages: ["foo.bar"],
7757 sdk_version: "none",
7758 system_modules: "none",
7759 min_sdk_version: "current",
7760 }
7761 java_library {
7762 name: "bcp_lib2",
7763 srcs: ["lib2/src/*.java"],
7764 apex_available: ["myapex"],
7765 permitted_packages: ["foo.bar", "bar.baz"],
7766 sdk_version: "none",
7767 system_modules: "none",
7768 min_sdk_version: "current",
7769 }
7770 apex {
7771 name: "myapex",
7772 min_sdk_version: "current",
Andrei Onea115e7e72020-06-05 21:14:03 +01007773 key: "myapex.key",
7774 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007775 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01007776 }
7777 `,
7778 bootJars: []string{"bcp_lib1", "bcp_lib2"},
7779 modulesPackages: map[string][]string{
7780 "myapex": []string{
7781 "foo.bar",
7782 },
7783 },
7784 },
7785 }
7786 for _, tc := range testcases {
7787 t.Run(tc.name, func(t *testing.T) {
7788 rules := createApexPermittedPackagesRules(tc.modulesPackages)
7789 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
7790 })
7791 }
7792}
7793
Jiyong Park62304bb2020-04-13 16:19:48 +09007794func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007795 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09007796 apex {
7797 name: "myapex",
7798 key: "myapex.key",
7799 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007800 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09007801 }
7802
7803 apex_key {
7804 name: "myapex.key",
7805 public_key: "testkey.avbpubkey",
7806 private_key: "testkey.pem",
7807 }
7808
7809 cc_library {
7810 name: "mylib",
7811 srcs: ["mylib.cpp"],
7812 system_shared_libs: [],
7813 stl: "none",
7814 stubs: {
7815 versions: ["1"],
7816 },
7817 apex_available: ["myapex"],
7818 }
7819
7820 cc_library {
7821 name: "myprivlib",
7822 srcs: ["mylib.cpp"],
7823 system_shared_libs: [],
7824 stl: "none",
7825 apex_available: ["myapex"],
7826 }
7827
7828
7829 cc_test {
7830 name: "mytest",
7831 gtest: false,
7832 srcs: ["mylib.cpp"],
7833 system_shared_libs: [],
7834 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09007835 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09007836 test_for: ["myapex"]
7837 }
Jiyong Park46a512f2020-12-04 18:02:13 +09007838
7839 cc_library {
7840 name: "mytestlib",
7841 srcs: ["mylib.cpp"],
7842 system_shared_libs: [],
7843 shared_libs: ["mylib", "myprivlib"],
7844 stl: "none",
7845 test_for: ["myapex"],
7846 }
7847
7848 cc_benchmark {
7849 name: "mybench",
7850 srcs: ["mylib.cpp"],
7851 system_shared_libs: [],
7852 shared_libs: ["mylib", "myprivlib"],
7853 stl: "none",
7854 test_for: ["myapex"],
7855 }
Jiyong Park62304bb2020-04-13 16:19:48 +09007856 `)
7857
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007858 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01007859 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007860 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
7861 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
7862 }
7863
7864 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09007865 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007866 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7867 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7868 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7869}
Jiyong Park46a512f2020-12-04 18:02:13 +09007870
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007871func TestIndirectTestFor(t *testing.T) {
7872 ctx := testApex(t, `
7873 apex {
7874 name: "myapex",
7875 key: "myapex.key",
7876 native_shared_libs: ["mylib", "myprivlib"],
7877 updatable: false,
7878 }
Jiyong Park46a512f2020-12-04 18:02:13 +09007879
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007880 apex_key {
7881 name: "myapex.key",
7882 public_key: "testkey.avbpubkey",
7883 private_key: "testkey.pem",
7884 }
7885
7886 cc_library {
7887 name: "mylib",
7888 srcs: ["mylib.cpp"],
7889 system_shared_libs: [],
7890 stl: "none",
7891 stubs: {
7892 versions: ["1"],
7893 },
7894 apex_available: ["myapex"],
7895 }
7896
7897 cc_library {
7898 name: "myprivlib",
7899 srcs: ["mylib.cpp"],
7900 system_shared_libs: [],
7901 stl: "none",
7902 shared_libs: ["mylib"],
7903 apex_available: ["myapex"],
7904 }
7905
7906 cc_library {
7907 name: "mytestlib",
7908 srcs: ["mylib.cpp"],
7909 system_shared_libs: [],
7910 shared_libs: ["myprivlib"],
7911 stl: "none",
7912 test_for: ["myapex"],
7913 }
7914 `)
7915
7916 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01007917 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007918 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
7919 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
7920 }
7921
7922 // The platform variant of mytestlib links to the platform variant of the
7923 // internal myprivlib.
7924 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
7925
7926 // The platform variant of myprivlib links to the platform variant of mylib
7927 // and bypasses its stubs.
7928 ensureLinkedLibIs("myprivlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
Jiyong Park62304bb2020-04-13 16:19:48 +09007929}
7930
Martin Stjernholmec009002021-03-27 15:18:31 +00007931func TestTestForForLibInOtherApex(t *testing.T) {
7932 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
7933 _ = testApex(t, `
7934 apex {
7935 name: "com.android.art",
7936 key: "myapex.key",
7937 native_shared_libs: ["mylib"],
7938 updatable: false,
7939 }
7940
7941 apex {
7942 name: "com.android.art.debug",
7943 key: "myapex.key",
7944 native_shared_libs: ["mylib", "mytestlib"],
7945 updatable: false,
7946 }
7947
7948 apex_key {
7949 name: "myapex.key",
7950 public_key: "testkey.avbpubkey",
7951 private_key: "testkey.pem",
7952 }
7953
7954 cc_library {
7955 name: "mylib",
7956 srcs: ["mylib.cpp"],
7957 system_shared_libs: [],
7958 stl: "none",
7959 stubs: {
7960 versions: ["1"],
7961 },
7962 apex_available: ["com.android.art", "com.android.art.debug"],
7963 }
7964
7965 cc_library {
7966 name: "mytestlib",
7967 srcs: ["mylib.cpp"],
7968 system_shared_libs: [],
7969 shared_libs: ["mylib"],
7970 stl: "none",
7971 apex_available: ["com.android.art.debug"],
7972 test_for: ["com.android.art"],
7973 }
7974 `,
7975 android.MockFS{
7976 "system/sepolicy/apex/com.android.art-file_contexts": nil,
7977 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
7978 }.AddToFixture())
7979}
7980
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007981// TODO(jungjw): Move this to proptools
7982func intPtr(i int) *int {
7983 return &i
7984}
7985
7986func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007987 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007988 apex_set {
7989 name: "myapex",
7990 set: "myapex.apks",
7991 filename: "foo_v2.apex",
7992 overrides: ["foo"],
7993 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007994 `,
7995 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7996 variables.Platform_sdk_version = intPtr(30)
7997 }),
7998 android.FixtureModifyConfig(func(config android.Config) {
7999 config.Targets[android.Android] = []android.Target{
8000 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8001 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8002 }
8003 }),
8004 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008005
Paul Duffin24704672021-04-06 16:09:30 +01008006 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008007
8008 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008009 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008010 actual := extractedApex.Args["abis"]
8011 expected := "ARMEABI_V7A,ARM64_V8A"
8012 if actual != expected {
8013 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8014 }
8015 actual = extractedApex.Args["sdk-version"]
8016 expected = "30"
8017 if actual != expected {
8018 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8019 }
8020
Paul Duffin6717d882021-06-15 19:09:41 +01008021 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008022 a := m.Module().(*ApexSet)
8023 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008024 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008025 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8026 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8027 }
8028}
8029
Jiyong Park7d95a512020-05-10 15:16:24 +09008030func TestNoStaticLinkingToStubsLib(t *testing.T) {
8031 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8032 apex {
8033 name: "myapex",
8034 key: "myapex.key",
8035 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008036 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008037 }
8038
8039 apex_key {
8040 name: "myapex.key",
8041 public_key: "testkey.avbpubkey",
8042 private_key: "testkey.pem",
8043 }
8044
8045 cc_library {
8046 name: "mylib",
8047 srcs: ["mylib.cpp"],
8048 static_libs: ["otherlib"],
8049 system_shared_libs: [],
8050 stl: "none",
8051 apex_available: [ "myapex" ],
8052 }
8053
8054 cc_library {
8055 name: "otherlib",
8056 srcs: ["mylib.cpp"],
8057 system_shared_libs: [],
8058 stl: "none",
8059 stubs: {
8060 versions: ["1", "2", "3"],
8061 },
8062 apex_available: [ "myapex" ],
8063 }
8064 `)
8065}
8066
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008067func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008068 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008069 apex {
8070 name: "myapex",
8071 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008072 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008073 custom_sign_tool: "sign_myapex",
8074 }
8075
8076 apex_key {
8077 name: "myapex.key",
8078 public_key: "testkey.avbpubkey",
8079 private_key: "testkey.pem",
8080 }
8081 `)
8082
8083 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8084 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8085 ensureContains(t, content, `name="myapex.apex" public_key="vendor/foo/devkeys/testkey.avbpubkey" private_key="vendor/foo/devkeys/testkey.pem" container_certificate="vendor/foo/devkeys/test.x509.pem" container_private_key="vendor/foo/devkeys/test.pk8" partition="system_ext" sign_tool="sign_myapex"`)
8086}
8087
8088func TestApexKeysTxtOverrides(t *testing.T) {
8089 ctx := testApex(t, `
8090 apex {
8091 name: "myapex",
8092 key: "myapex.key",
8093 updatable: false,
8094 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008095 }
8096
8097 apex_key {
8098 name: "myapex.key",
8099 public_key: "testkey.avbpubkey",
8100 private_key: "testkey.pem",
8101 }
8102
8103 prebuilt_apex {
8104 name: "myapex",
8105 prefer: true,
8106 arch: {
8107 arm64: {
8108 src: "myapex-arm64.apex",
8109 },
8110 arm: {
8111 src: "myapex-arm.apex",
8112 },
8113 },
8114 }
8115
8116 apex_set {
8117 name: "myapex_set",
8118 set: "myapex.apks",
8119 filename: "myapex_set.apex",
8120 overrides: ["myapex"],
8121 }
8122 `)
8123
8124 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8125 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8126 ensureContains(t, content, `name="myapex_set.apex" public_key="PRESIGNED" private_key="PRESIGNED" container_certificate="PRESIGNED" container_private_key="PRESIGNED" partition="system"`)
Jiyong Park03a7f3e2020-06-18 19:34:42 +09008127 ensureContains(t, content, `name="myapex.apex" public_key="PRESIGNED" private_key="PRESIGNED" container_certificate="PRESIGNED" container_private_key="PRESIGNED" partition="system"`)
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008128}
8129
Jooyung Han938b5932020-06-20 12:47:47 +09008130func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008131 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008132 apex {
8133 name: "myapex",
8134 key: "myapex.key",
8135 apps: ["app"],
8136 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008137 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008138 }
8139
8140 apex_key {
8141 name: "myapex.key",
8142 public_key: "testkey.avbpubkey",
8143 private_key: "testkey.pem",
8144 }
8145
8146 android_app {
8147 name: "app",
8148 srcs: ["foo/bar/MyClass.java"],
8149 package_name: "foo",
8150 sdk_version: "none",
8151 system_modules: "none",
8152 apex_available: [ "myapex" ],
8153 }
8154 `, withFiles(map[string][]byte{
8155 "sub/Android.bp": []byte(`
8156 override_apex {
8157 name: "override_myapex",
8158 base: "myapex",
8159 apps: ["override_app"],
8160 allowed_files: ":allowed",
8161 }
8162 // Overridable "path" property should be referenced indirectly
8163 filegroup {
8164 name: "allowed",
8165 srcs: ["allowed.txt"],
8166 }
8167 override_android_app {
8168 name: "override_app",
8169 base: "app",
8170 package_name: "bar",
8171 }
8172 `),
8173 }))
8174
8175 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
8176 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8177 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8178 }
8179
8180 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
8181 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8182 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8183 }
8184}
8185
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008186func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008187 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008188 apex {
8189 name: "myapex",
8190 key: "myapex.key",
8191 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008192 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008193 }
8194
8195 apex_key {
8196 name: "myapex.key",
8197 public_key: "testkey.avbpubkey",
8198 private_key: "testkey.pem",
8199 }
8200
8201 cc_library {
8202 name: "mylib",
8203 srcs: ["mylib.cpp"],
8204 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008205 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008206 },
8207 apex_available: ["myapex"],
8208 }
8209
8210 cc_prebuilt_library_shared {
8211 name: "mylib",
8212 prefer: false,
8213 srcs: ["prebuilt.so"],
8214 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008215 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008216 },
8217 apex_available: ["myapex"],
8218 }
8219 `)
8220}
8221
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008222func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008223 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008224 apex {
8225 name: "myapex",
8226 key: "myapex.key",
8227 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008228 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008229 }
8230 apex_key {
8231 name: "myapex.key",
8232 public_key: "testkey.avbpubkey",
8233 private_key: "testkey.pem",
8234 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008235 `,
8236 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8237 variables.CompressedApex = proptools.BoolPtr(true)
8238 }),
8239 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008240
8241 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
8242 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
8243
8244 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
8245 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
8246
8247 // Make sure output of bundle is .capex
8248 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8249 ensureContains(t, ab.outputFile.String(), "myapex.capex")
8250
8251 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07008252 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008253 var builder strings.Builder
8254 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8255 androidMk := builder.String()
8256 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
8257}
8258
Martin Stjernholm2856c662020-12-02 15:03:42 +00008259func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008260 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00008261 apex {
8262 name: "myapex",
8263 key: "myapex.key",
8264 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008265 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00008266 }
8267
8268 apex_key {
8269 name: "myapex.key",
8270 public_key: "testkey.avbpubkey",
8271 private_key: "testkey.pem",
8272 }
8273
8274 cc_library {
8275 name: "mylib",
8276 srcs: ["mylib.cpp"],
8277 apex_available: ["myapex"],
8278 shared_libs: ["otherlib"],
8279 system_shared_libs: [],
8280 }
8281
8282 cc_library {
8283 name: "otherlib",
8284 srcs: ["mylib.cpp"],
8285 stubs: {
8286 versions: ["current"],
8287 },
8288 }
8289
8290 cc_prebuilt_library_shared {
8291 name: "otherlib",
8292 prefer: true,
8293 srcs: ["prebuilt.so"],
8294 stubs: {
8295 versions: ["current"],
8296 },
8297 }
8298 `)
8299
8300 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07008301 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00008302 var builder strings.Builder
8303 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8304 androidMk := builder.String()
8305
8306 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
8307 // a thing there.
8308 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
8309}
8310
Jiyong Parke3867542020-12-03 17:28:25 +09008311func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008312 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09008313 apex {
8314 name: "myapex",
8315 key: "myapex.key",
8316 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008317 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09008318 }
8319
8320 apex_key {
8321 name: "myapex.key",
8322 public_key: "testkey.avbpubkey",
8323 private_key: "testkey.pem",
8324 }
8325
8326 cc_library {
8327 name: "mylib",
8328 srcs: ["mylib.cpp"],
8329 system_shared_libs: [],
8330 stl: "none",
8331 apex_available: ["myapex"],
8332 shared_libs: ["mylib2"],
8333 target: {
8334 apex: {
8335 exclude_shared_libs: ["mylib2"],
8336 },
8337 },
8338 }
8339
8340 cc_library {
8341 name: "mylib2",
8342 srcs: ["mylib.cpp"],
8343 system_shared_libs: [],
8344 stl: "none",
8345 }
8346 `)
8347
8348 // Check if mylib is linked to mylib2 for the non-apex target
8349 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
8350 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
8351
8352 // Make sure that the link doesn't occur for the apex target
8353 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
8354 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
8355
8356 // It shouldn't appear in the copy cmd as well.
8357 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
8358 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
8359}
8360
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008361func TestPrebuiltStubLibDep(t *testing.T) {
8362 bpBase := `
8363 apex {
8364 name: "myapex",
8365 key: "myapex.key",
8366 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008367 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008368 }
8369 apex_key {
8370 name: "myapex.key",
8371 public_key: "testkey.avbpubkey",
8372 private_key: "testkey.pem",
8373 }
8374 cc_library {
8375 name: "mylib",
8376 srcs: ["mylib.cpp"],
8377 apex_available: ["myapex"],
8378 shared_libs: ["stublib"],
8379 system_shared_libs: [],
8380 }
8381 apex {
8382 name: "otherapex",
8383 enabled: %s,
8384 key: "myapex.key",
8385 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008386 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008387 }
8388 `
8389
8390 stublibSourceBp := `
8391 cc_library {
8392 name: "stublib",
8393 srcs: ["mylib.cpp"],
8394 apex_available: ["otherapex"],
8395 system_shared_libs: [],
8396 stl: "none",
8397 stubs: {
8398 versions: ["1"],
8399 },
8400 }
8401 `
8402
8403 stublibPrebuiltBp := `
8404 cc_prebuilt_library_shared {
8405 name: "stublib",
8406 srcs: ["prebuilt.so"],
8407 apex_available: ["otherapex"],
8408 stubs: {
8409 versions: ["1"],
8410 },
8411 %s
8412 }
8413 `
8414
8415 tests := []struct {
8416 name string
8417 stublibBp string
8418 usePrebuilt bool
8419 modNames []string // Modules to collect AndroidMkEntries for
8420 otherApexEnabled []string
8421 }{
8422 {
8423 name: "only_source",
8424 stublibBp: stublibSourceBp,
8425 usePrebuilt: false,
8426 modNames: []string{"stublib"},
8427 otherApexEnabled: []string{"true", "false"},
8428 },
8429 {
8430 name: "source_preferred",
8431 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
8432 usePrebuilt: false,
8433 modNames: []string{"stublib", "prebuilt_stublib"},
8434 otherApexEnabled: []string{"true", "false"},
8435 },
8436 {
8437 name: "prebuilt_preferred",
8438 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
8439 usePrebuilt: true,
8440 modNames: []string{"stublib", "prebuilt_stublib"},
8441 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8442 },
8443 {
8444 name: "only_prebuilt",
8445 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
8446 usePrebuilt: true,
8447 modNames: []string{"stublib"},
8448 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8449 },
8450 }
8451
8452 for _, test := range tests {
8453 t.Run(test.name, func(t *testing.T) {
8454 for _, otherApexEnabled := range test.otherApexEnabled {
8455 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008456 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008457
8458 type modAndMkEntries struct {
8459 mod *cc.Module
8460 mkEntries android.AndroidMkEntries
8461 }
8462 entries := []*modAndMkEntries{}
8463
8464 // Gather shared lib modules that are installable
8465 for _, modName := range test.modNames {
8466 for _, variant := range ctx.ModuleVariantsForTests(modName) {
8467 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
8468 continue
8469 }
8470 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08008471 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008472 continue
8473 }
Colin Crossaa255532020-07-03 13:18:24 -07008474 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008475 if ent.Disabled {
8476 continue
8477 }
8478 entries = append(entries, &modAndMkEntries{
8479 mod: mod,
8480 mkEntries: ent,
8481 })
8482 }
8483 }
8484 }
8485
8486 var entry *modAndMkEntries = nil
8487 for _, ent := range entries {
8488 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
8489 if entry != nil {
8490 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
8491 } else {
8492 entry = ent
8493 }
8494 }
8495 }
8496
8497 if entry == nil {
8498 t.Errorf("AndroidMk entry for \"stublib\" missing")
8499 } else {
8500 isPrebuilt := entry.mod.Prebuilt() != nil
8501 if isPrebuilt != test.usePrebuilt {
8502 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
8503 }
8504 if !entry.mod.IsStubs() {
8505 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
8506 }
8507 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
8508 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
8509 }
Jiyong Park892a98f2020-12-14 09:20:00 +09008510 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09008511 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09008512 if !android.InList(expected, cflags) {
8513 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
8514 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008515 }
8516 })
8517 }
8518 })
8519 }
8520}
8521
Martin Stjernholmdf298b32021-05-21 20:57:29 +01008522func TestHostApexInHostOnlyBuild(t *testing.T) {
8523 testApex(t, `
8524 apex {
8525 name: "myapex",
8526 host_supported: true,
8527 key: "myapex.key",
8528 updatable: false,
8529 payload_type: "zip",
8530 }
8531 apex_key {
8532 name: "myapex.key",
8533 public_key: "testkey.avbpubkey",
8534 private_key: "testkey.pem",
8535 }
8536 `,
8537 android.FixtureModifyConfig(func(config android.Config) {
8538 // We may not have device targets in all builds, e.g. in
8539 // prebuilts/build-tools/build-prebuilts.sh
8540 config.Targets[android.Android] = []android.Target{}
8541 }))
8542}
8543
Colin Crossc33e5212021-05-25 18:16:02 -07008544func TestApexJavaCoverage(t *testing.T) {
8545 bp := `
8546 apex {
8547 name: "myapex",
8548 key: "myapex.key",
8549 java_libs: ["mylib"],
8550 bootclasspath_fragments: ["mybootclasspathfragment"],
8551 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
8552 updatable: false,
8553 }
8554
8555 apex_key {
8556 name: "myapex.key",
8557 public_key: "testkey.avbpubkey",
8558 private_key: "testkey.pem",
8559 }
8560
8561 java_library {
8562 name: "mylib",
8563 srcs: ["mylib.java"],
8564 apex_available: ["myapex"],
8565 compile_dex: true,
8566 }
8567
8568 bootclasspath_fragment {
8569 name: "mybootclasspathfragment",
8570 contents: ["mybootclasspathlib"],
8571 apex_available: ["myapex"],
8572 }
8573
8574 java_library {
8575 name: "mybootclasspathlib",
8576 srcs: ["mybootclasspathlib.java"],
8577 apex_available: ["myapex"],
8578 compile_dex: true,
8579 }
8580
8581 systemserverclasspath_fragment {
8582 name: "mysystemserverclasspathfragment",
8583 contents: ["mysystemserverclasspathlib"],
8584 apex_available: ["myapex"],
8585 }
8586
8587 java_library {
8588 name: "mysystemserverclasspathlib",
8589 srcs: ["mysystemserverclasspathlib.java"],
8590 apex_available: ["myapex"],
8591 compile_dex: true,
8592 }
8593 `
8594
8595 result := android.GroupFixturePreparers(
8596 PrepareForTestWithApexBuildComponents,
8597 prepareForTestWithMyapex,
8598 java.PrepareForTestWithJavaDefaultModules,
8599 android.PrepareForTestWithAndroidBuildComponents,
8600 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01008601 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
8602 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Colin Crossc33e5212021-05-25 18:16:02 -07008603 android.FixtureMergeEnv(map[string]string{
8604 "EMMA_INSTRUMENT": "true",
8605 }),
8606 ).RunTest(t)
8607
8608 // Make sure jacoco ran on both mylib and mybootclasspathlib
8609 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8610 t.Errorf("Failed to find jacoco rule for mylib")
8611 }
8612 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8613 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
8614 }
8615 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8616 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
8617 }
8618}
8619
Jiyong Park192600a2021-08-03 07:52:17 +00008620func TestProhibitStaticExecutable(t *testing.T) {
8621 testApexError(t, `executable mybin is static`, `
8622 apex {
8623 name: "myapex",
8624 key: "myapex.key",
8625 binaries: ["mybin"],
8626 min_sdk_version: "29",
8627 }
8628
8629 apex_key {
8630 name: "myapex.key",
8631 public_key: "testkey.avbpubkey",
8632 private_key: "testkey.pem",
8633 }
8634
8635 cc_binary {
8636 name: "mybin",
8637 srcs: ["mylib.cpp"],
8638 relative_install_path: "foo/bar",
8639 static_executable: true,
8640 system_shared_libs: [],
8641 stl: "none",
8642 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09008643 min_sdk_version: "29",
8644 }
8645 `)
8646
8647 testApexError(t, `executable mybin.rust is static`, `
8648 apex {
8649 name: "myapex",
8650 key: "myapex.key",
8651 binaries: ["mybin.rust"],
8652 min_sdk_version: "29",
8653 }
8654
8655 apex_key {
8656 name: "myapex.key",
8657 public_key: "testkey.avbpubkey",
8658 private_key: "testkey.pem",
8659 }
8660
8661 rust_binary {
8662 name: "mybin.rust",
8663 srcs: ["foo.rs"],
8664 static_executable: true,
8665 apex_available: ["myapex"],
8666 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00008667 }
8668 `)
8669}
8670
Jiakai Zhang470b7e22021-09-30 09:34:26 +00008671func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
8672 ctx := testApex(t, `
8673 apex {
8674 name: "myapex",
8675 key: "myapex.key",
8676 updatable: false,
8677 java_libs: ["foo"],
8678 }
8679
8680 apex_key {
8681 name: "myapex.key",
8682 public_key: "testkey.avbpubkey",
8683 private_key: "testkey.pem",
8684 }
8685
8686 java_library {
8687 name: "foo",
8688 srcs: ["foo.java"],
8689 apex_available: ["myapex"],
8690 installable: true,
8691 }
8692 `,
8693 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8694 )
8695
8696 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8697 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
8698 var builder strings.Builder
8699 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
8700 androidMk := builder.String()
8701 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex")
8702}
8703
8704func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
8705 ctx := testApex(t, `
8706 prebuilt_apex {
8707 name: "myapex",
8708 arch: {
8709 arm64: {
8710 src: "myapex-arm64.apex",
8711 },
8712 arm: {
8713 src: "myapex-arm.apex",
8714 },
8715 },
8716 exported_java_libs: ["foo"],
8717 }
8718
8719 java_import {
8720 name: "foo",
8721 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00008722 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00008723 }
8724 `,
8725 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8726 )
8727
8728 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
8729 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
8730 mainModuleEntries := entriesList[0]
8731 android.AssertArrayString(t,
8732 "LOCAL_REQUIRED_MODULES",
8733 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
8734 []string{
8735 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
8736 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
8737 })
8738}
8739
Jiyong Parkcacc4f32021-10-28 14:26:03 +09008740func TestAndroidMk_RequiredModules(t *testing.T) {
8741 ctx := testApex(t, `
8742 apex {
8743 name: "myapex",
8744 key: "myapex.key",
8745 updatable: false,
8746 java_libs: ["foo"],
8747 required: ["otherapex"],
8748 }
8749
8750 apex {
8751 name: "otherapex",
8752 key: "myapex.key",
8753 updatable: false,
8754 java_libs: ["foo"],
8755 required: ["otherapex"],
8756 }
8757
8758 apex_key {
8759 name: "myapex.key",
8760 public_key: "testkey.avbpubkey",
8761 private_key: "testkey.pem",
8762 }
8763
8764 java_library {
8765 name: "foo",
8766 srcs: ["foo.java"],
8767 apex_available: ["myapex", "otherapex"],
8768 installable: true,
8769 }
8770 `)
8771
8772 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8773 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
8774 var builder strings.Builder
8775 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
8776 androidMk := builder.String()
8777 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherapex")
8778}
8779
Jiakai Zhangd70dff72022-02-24 15:06:05 +00008780func TestAndroidMk_RequiredDeps(t *testing.T) {
8781 ctx := testApex(t, `
8782 apex {
8783 name: "myapex",
8784 key: "myapex.key",
8785 updatable: false,
8786 }
8787
8788 apex_key {
8789 name: "myapex.key",
8790 public_key: "testkey.avbpubkey",
8791 private_key: "testkey.pem",
8792 }
8793 `)
8794
8795 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8796 bundle.requiredDeps = append(bundle.requiredDeps, "foo")
8797 data := android.AndroidMkDataForTest(t, ctx, bundle)
8798 var builder strings.Builder
8799 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
8800 androidMk := builder.String()
8801 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += foo")
8802
8803 flattenedBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
8804 flattenedBundle.requiredDeps = append(flattenedBundle.requiredDeps, "foo")
8805 flattenedData := android.AndroidMkDataForTest(t, ctx, flattenedBundle)
8806 var flattenedBuilder strings.Builder
8807 flattenedData.Custom(&flattenedBuilder, flattenedBundle.BaseModuleName(), "TARGET_", "", flattenedData)
8808 flattenedAndroidMk := flattenedBuilder.String()
8809 ensureContains(t, flattenedAndroidMk, "LOCAL_REQUIRED_MODULES += foo")
8810}
8811
Jooyung Hana6d36672022-02-24 13:58:07 +09008812func TestApexOutputFileProducer(t *testing.T) {
8813 for _, tc := range []struct {
8814 name string
8815 ref string
8816 expected_data []string
8817 }{
8818 {
8819 name: "test_using_output",
8820 ref: ":myapex",
8821 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.capex:myapex.capex"},
8822 },
8823 {
8824 name: "test_using_apex",
8825 ref: ":myapex{.apex}",
8826 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.apex:myapex.apex"},
8827 },
8828 } {
8829 t.Run(tc.name, func(t *testing.T) {
8830 ctx := testApex(t, `
8831 apex {
8832 name: "myapex",
8833 key: "myapex.key",
8834 compressible: true,
8835 updatable: false,
8836 }
8837
8838 apex_key {
8839 name: "myapex.key",
8840 public_key: "testkey.avbpubkey",
8841 private_key: "testkey.pem",
8842 }
8843
8844 java_test {
8845 name: "`+tc.name+`",
8846 srcs: ["a.java"],
8847 data: ["`+tc.ref+`"],
8848 }
8849 `,
8850 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8851 variables.CompressedApex = proptools.BoolPtr(true)
8852 }))
8853 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
8854 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
8855 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
8856 })
8857 }
8858}
8859
satayev758968a2021-12-06 11:42:40 +00008860func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
8861 preparer := android.GroupFixturePreparers(
8862 PrepareForTestWithApexBuildComponents,
8863 prepareForTestWithMyapex,
8864 java.PrepareForTestWithJavaSdkLibraryFiles,
8865 java.PrepareForTestWithJavaDefaultModules,
8866 android.PrepareForTestWithAndroidBuildComponents,
8867 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
8868 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
8869 )
8870
8871 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
8872 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
8873 preparer.RunTestWithBp(t, `
8874 apex {
8875 name: "myapex",
8876 key: "myapex.key",
8877 bootclasspath_fragments: ["mybootclasspathfragment"],
8878 min_sdk_version: "30",
8879 updatable: false,
8880 }
8881
8882 apex_key {
8883 name: "myapex.key",
8884 public_key: "testkey.avbpubkey",
8885 private_key: "testkey.pem",
8886 }
8887
8888 bootclasspath_fragment {
8889 name: "mybootclasspathfragment",
8890 contents: ["mybootclasspathlib"],
8891 apex_available: ["myapex"],
8892 }
8893
8894 java_sdk_library {
8895 name: "mybootclasspathlib",
8896 srcs: ["mybootclasspathlib.java"],
8897 apex_available: ["myapex"],
8898 compile_dex: true,
8899 unsafe_ignore_missing_latest_api: true,
8900 min_sdk_version: "31",
8901 static_libs: ["util"],
8902 }
8903
8904 java_library {
8905 name: "util",
8906 srcs: ["a.java"],
8907 apex_available: ["myapex"],
8908 min_sdk_version: "31",
8909 static_libs: ["another_util"],
8910 }
8911
8912 java_library {
8913 name: "another_util",
8914 srcs: ["a.java"],
8915 min_sdk_version: "31",
8916 apex_available: ["myapex"],
8917 }
8918 `)
8919 })
8920
8921 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
8922 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
8923 preparer.RunTestWithBp(t, `
8924 apex {
8925 name: "myapex",
8926 key: "myapex.key",
8927 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
8928 min_sdk_version: "30",
8929 updatable: false,
8930 }
8931
8932 apex_key {
8933 name: "myapex.key",
8934 public_key: "testkey.avbpubkey",
8935 private_key: "testkey.pem",
8936 }
8937
8938 systemserverclasspath_fragment {
8939 name: "mysystemserverclasspathfragment",
8940 contents: ["mysystemserverclasspathlib"],
8941 apex_available: ["myapex"],
8942 }
8943
8944 java_sdk_library {
8945 name: "mysystemserverclasspathlib",
8946 srcs: ["mysystemserverclasspathlib.java"],
8947 apex_available: ["myapex"],
8948 compile_dex: true,
8949 min_sdk_version: "32",
8950 unsafe_ignore_missing_latest_api: true,
8951 static_libs: ["util"],
8952 }
8953
8954 java_library {
8955 name: "util",
8956 srcs: ["a.java"],
8957 apex_available: ["myapex"],
8958 min_sdk_version: "31",
8959 static_libs: ["another_util"],
8960 }
8961
8962 java_library {
8963 name: "another_util",
8964 srcs: ["a.java"],
8965 min_sdk_version: "31",
8966 apex_available: ["myapex"],
8967 }
8968 `)
8969 })
8970
8971 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
8972 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
8973 RunTestWithBp(t, `
8974 apex {
8975 name: "myapex",
8976 key: "myapex.key",
8977 bootclasspath_fragments: ["mybootclasspathfragment"],
8978 min_sdk_version: "30",
8979 updatable: false,
8980 }
8981
8982 apex_key {
8983 name: "myapex.key",
8984 public_key: "testkey.avbpubkey",
8985 private_key: "testkey.pem",
8986 }
8987
8988 bootclasspath_fragment {
8989 name: "mybootclasspathfragment",
8990 contents: ["mybootclasspathlib"],
8991 apex_available: ["myapex"],
8992 }
8993
8994 java_sdk_library {
8995 name: "mybootclasspathlib",
8996 srcs: ["mybootclasspathlib.java"],
8997 apex_available: ["myapex"],
8998 compile_dex: true,
8999 unsafe_ignore_missing_latest_api: true,
9000 }
9001 `)
9002 })
9003
9004 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9005 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9006 RunTestWithBp(t, `
9007 apex {
9008 name: "myapex",
9009 key: "myapex.key",
9010 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9011 min_sdk_version: "30",
9012 updatable: false,
9013 }
9014
9015 apex_key {
9016 name: "myapex.key",
9017 public_key: "testkey.avbpubkey",
9018 private_key: "testkey.pem",
9019 }
9020
9021 systemserverclasspath_fragment {
9022 name: "mysystemserverclasspathfragment",
9023 contents: ["mysystemserverclasspathlib"],
9024 apex_available: ["myapex"],
9025 }
9026
9027 java_sdk_library {
9028 name: "mysystemserverclasspathlib",
9029 srcs: ["mysystemserverclasspathlib.java"],
9030 apex_available: ["myapex"],
9031 compile_dex: true,
9032 unsafe_ignore_missing_latest_api: true,
9033 }
9034 `)
9035 })
9036}
9037
Jiakai Zhang6decef92022-01-12 17:56:19 +00009038// Verifies that the APEX depends on all the Make modules in the list.
9039func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9040 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9041 for _, dep := range deps {
9042 android.AssertStringListContains(t, "", a.requiredDeps, dep)
9043 }
9044}
9045
9046// Verifies that the APEX does not depend on any of the Make modules in the list.
9047func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9048 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9049 for _, dep := range deps {
9050 android.AssertStringListDoesNotContain(t, "", a.requiredDeps, dep)
9051 }
9052}
9053
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07009054func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01009055 os.Exit(m.Run())
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07009056}