blob: d2884145f441f7e11371ad01d82a76468fa01ad2 [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 "io/ioutil"
20 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090021 "path"
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 Park25fc6a92018-11-18 18:02:45 +090025 "strings"
26 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090027
28 "github.com/google/blueprint/proptools"
29
30 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080031 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090032 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000033 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070034 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090035 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090036 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070037 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090038)
39
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070040var buildDir string
41
Jooyung Hand3639552019-08-09 12:57:43 +090042// names returns name list from white space separated string
43func names(s string) (ns []string) {
44 for _, n := range strings.Split(s, " ") {
45 if len(n) > 0 {
46 ns = append(ns, n)
47 }
48 }
49 return
50}
51
Jooyung Han344d5432019-08-23 11:17:39 +090052func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
53 t.Helper()
54 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090055 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
56 if len(errs) > 0 {
57 android.FailIfNoMatchingErrors(t, pattern, errs)
58 return
59 }
60 _, errs = ctx.PrepareBuildActions(config)
61 if len(errs) > 0 {
62 android.FailIfNoMatchingErrors(t, pattern, errs)
63 return
64 }
65
66 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
67}
68
Jooyung Han344d5432019-08-23 11:17:39 +090069func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
70 t.Helper()
71 ctx, config := testApexContext(t, bp, handlers...)
Paul Duffineedc5d52020-06-12 17:46:39 +010072 _, errs := ctx.ParseBlueprintsFiles(".")
Jooyung Han5c998b92019-06-27 11:30:33 +090073 android.FailIfErrored(t, errs)
74 _, errs = ctx.PrepareBuildActions(config)
75 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070076 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090077}
78
Jooyung Han344d5432019-08-23 11:17:39 +090079type testCustomizer func(fs map[string][]byte, config android.Config)
80
81func withFiles(files map[string][]byte) testCustomizer {
82 return func(fs map[string][]byte, config android.Config) {
83 for k, v := range files {
84 fs[k] = v
85 }
86 }
87}
88
89func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
90 return func(fs map[string][]byte, config android.Config) {
91 for k, v := range targets {
92 config.Targets[k] = v
93 }
94 }
95}
96
Jooyung Han35155c42020-02-06 17:33:20 +090097// withNativeBridgeTargets sets configuration with targets including:
98// - X86_64 (primary)
99// - X86 (secondary)
100// - Arm64 on X86_64 (native bridge)
101// - Arm on X86 (native bridge)
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700102func withNativeBridgeEnabled(_ map[string][]byte, config android.Config) {
Jooyung Han35155c42020-02-06 17:33:20 +0900103 config.Targets[android.Android] = []android.Target{
104 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
105 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
106 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
107 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
108 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
109 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
110 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
111 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
112 }
113}
114
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900115func withManifestPackageNameOverrides(specs []string) testCustomizer {
116 return func(fs map[string][]byte, config android.Config) {
117 config.TestProductVariables.ManifestPackageNameOverrides = specs
118 }
119}
120
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700121func withBinder32bit(_ map[string][]byte, config android.Config) {
Jooyung Han31c470b2019-10-18 16:26:59 +0900122 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
123}
124
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700125func withUnbundledBuild(_ map[string][]byte, config android.Config) {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900126 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
127}
128
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700129func testApexContext(_ *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900130 bp = bp + `
Jooyung Han54aca7b2019-11-20 02:26:02 +0900131 filegroup {
132 name: "myapex-file_contexts",
133 srcs: [
134 "system/sepolicy/apex/myapex-file_contexts",
135 ],
136 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900137 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800138
Colin Crossf9aabd72020-02-15 11:29:50 -0800139 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
140
Jiyong Park99644e92020-11-17 22:21:02 +0900141 bp = bp + rust.GatherRequiredDepsForTest()
142
Dario Frenicde2a032019-10-27 00:29:22 +0100143 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900144
Jooyung Han344d5432019-08-23 11:17:39 +0900145 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900146 "a.java": nil,
147 "PrebuiltAppFoo.apk": nil,
148 "PrebuiltAppFooPriv.apk": nil,
149 "build/make/target/product/security": nil,
150 "apex_manifest.json": nil,
151 "AndroidManifest.xml": nil,
152 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park9d677202020-02-19 16:29:35 +0900153 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900154 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900155 "system/sepolicy/apex/otherapex-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900156 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800157 "mylib.cpp": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800158 "mytest.cpp": nil,
159 "mytest1.cpp": nil,
160 "mytest2.cpp": nil,
161 "mytest3.cpp": nil,
162 "myprebuilt": nil,
163 "my_include": nil,
164 "foo/bar/MyClass.java": nil,
165 "prebuilt.jar": nil,
Paul Duffindddd5462020-04-07 15:25:44 +0100166 "prebuilt.so": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800167 "vendor/foo/devkeys/test.x509.pem": nil,
168 "vendor/foo/devkeys/test.pk8": nil,
169 "testkey.x509.pem": nil,
170 "testkey.pk8": nil,
171 "testkey.override.x509.pem": nil,
172 "testkey.override.pk8": nil,
173 "vendor/foo/devkeys/testkey.avbpubkey": nil,
174 "vendor/foo/devkeys/testkey.pem": nil,
175 "NOTICE": nil,
176 "custom_notice": nil,
Jiyong Park9918e1a2020-03-17 19:16:40 +0900177 "custom_notice_for_static_lib": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800178 "testkey2.avbpubkey": nil,
179 "testkey2.pem": nil,
180 "myapex-arm64.apex": nil,
181 "myapex-arm.apex": nil,
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700182 "myapex.apks": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800183 "frameworks/base/api/current.txt": nil,
184 "framework/aidl/a.aidl": nil,
185 "build/make/core/proguard.flags": nil,
186 "build/make/core/proguard_basic_keeps.flags": nil,
187 "dummy.txt": nil,
Liz Kammer1c14a212020-05-12 15:26:55 -0700188 "baz": nil,
189 "bar/baz": nil,
Liz Kammer5bd365f2020-05-27 15:15:11 -0700190 "testdata/baz": nil,
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700191 "AppSet.apks": nil,
Jiyong Park99644e92020-11-17 22:21:02 +0900192 "foo.rs": nil,
Paul Duffin064b70c2020-11-02 17:32:38 +0000193 "libfoo.jar": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900194 }
195
Colin Crossf9aabd72020-02-15 11:29:50 -0800196 cc.GatherRequiredFilesForTest(fs)
197
Jooyung Han344d5432019-08-23 11:17:39 +0900198 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800199 // The fs now needs to be populated before creating the config, call handlers twice
200 // for now, once to get any fs changes, and later after the config was created to
201 // set product variables or targets.
202 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
203 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900204 }
205
Colin Cross98be1bb2019-12-13 20:41:13 -0800206 config := android.TestArchConfig(buildDir, nil, bp, fs)
207 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
208 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
209 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
210 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
211 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
Dan Albert4f378d72020-07-23 17:32:15 -0700212 config.TestProductVariables.Platform_version_active_codenames = []string{"Q"}
Colin Cross98be1bb2019-12-13 20:41:13 -0800213 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
214
215 for _, handler := range handlers {
216 // The fs now needs to be populated before creating the config, call handlers twice
217 // for now, earlier to get any fs changes, and now after the config was created to
218 // set product variables or targets.
219 tempFS := map[string][]byte{}
220 handler(tempFS, config)
221 }
222
Colin Crossae8600b2020-10-29 17:09:13 -0700223 ctx := android.NewTestArchContext(config)
Paul Duffineedc5d52020-06-12 17:46:39 +0100224
225 // from android package
226 android.RegisterPackageBuildComponents(ctx)
227 ctx.PreArchMutators(android.RegisterVisibilityRuleChecker)
228
Colin Cross98be1bb2019-12-13 20:41:13 -0800229 ctx.RegisterModuleType("apex", BundleFactory)
230 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
231 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
232 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
233 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
234 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
235 ctx.RegisterModuleType("override_apex", overrideApexFactory)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700236 ctx.RegisterModuleType("apex_set", apexSetFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800237
Jooyung Hana57af4a2020-01-23 05:36:59 +0000238 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +0100239 ctx.PreArchMutators(android.RegisterComponentsMutator)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000240 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
241
Paul Duffin021f4e52020-07-30 16:04:17 +0100242 android.RegisterPrebuiltMutators(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +0100243
Paul Duffin021f4e52020-07-30 16:04:17 +0100244 // Register these after the prebuilt mutators have been registered to match what
245 // happens at runtime.
Paul Duffineedc5d52020-06-12 17:46:39 +0100246 ctx.PreArchMutators(android.RegisterVisibilityRuleGatherer)
247 ctx.PostDepsMutators(android.RegisterVisibilityRuleEnforcer)
248
Paul Duffin021f4e52020-07-30 16:04:17 +0100249 cc.RegisterRequiredBuildComponentsForTest(ctx)
Jiyong Park99644e92020-11-17 22:21:02 +0900250 rust.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffin021f4e52020-07-30 16:04:17 +0100251
Colin Cross98be1bb2019-12-13 20:41:13 -0800252 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800253 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
Colin Crosse4e44bc2020-12-28 13:50:21 -0800254 cc.RegisterVndkLibraryTxtTypes(ctx)
Jooyung Han0703fd82020-08-26 22:11:53 +0900255 prebuilt_etc.RegisterPrebuiltEtcBuildComponents(ctx)
atrost6e126252020-01-27 17:01:16 +0000256 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700257 ctx.RegisterModuleType("sh_binary", sh.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800258 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000259 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000260 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000261 java.RegisterAppBuildComponents(ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800262 java.RegisterAppImportBuildComponents(ctx)
263 java.RegisterAppSetBuildComponents(ctx)
264 java.RegisterRuntimeResourceOverlayBuildComponents(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +0100265 java.RegisterSdkLibraryBuildComponents(ctx)
Anton Hanssondff2c782020-12-21 17:10:01 +0000266 java.RegisterPrebuiltApisBuildComponents(ctx)
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900267 ctx.RegisterSingletonType("apex_keys_text", apexKeysTextFactory)
markchien2f59ec92020-09-02 16:23:38 +0800268 ctx.RegisterModuleType("bpf", bpf.BpfFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800269
Colin Cross98be1bb2019-12-13 20:41:13 -0800270 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800271 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800272
Colin Crossae8600b2020-10-29 17:09:13 -0700273 ctx.Register()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900274
Jooyung Han5c998b92019-06-27 11:30:33 +0900275 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900276}
277
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700278func setUp() {
279 var err error
280 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900281 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700282 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900283 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900284}
285
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700286func tearDown() {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700287 _ = os.RemoveAll(buildDir)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900288}
289
Jooyung Han643adc42020-02-27 13:50:06 +0900290// ensure that 'result' equals 'expected'
291func ensureEquals(t *testing.T, result string, expected string) {
292 t.Helper()
293 if result != expected {
294 t.Errorf("%q != %q", expected, result)
295 }
296}
297
Jiyong Park25fc6a92018-11-18 18:02:45 +0900298// ensure that 'result' contains 'expected'
299func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900300 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900301 if !strings.Contains(result, expected) {
302 t.Errorf("%q is not found in %q", expected, result)
303 }
304}
305
Liz Kammer5bd365f2020-05-27 15:15:11 -0700306// ensure that 'result' contains 'expected' exactly one time
307func ensureContainsOnce(t *testing.T, result string, expected string) {
308 t.Helper()
309 count := strings.Count(result, expected)
310 if count != 1 {
311 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
312 }
313}
314
Jiyong Park25fc6a92018-11-18 18:02:45 +0900315// ensures that 'result' does not contain 'notExpected'
316func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900317 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900318 if strings.Contains(result, notExpected) {
319 t.Errorf("%q is found in %q", notExpected, result)
320 }
321}
322
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700323func ensureMatches(t *testing.T, result string, expectedRex string) {
324 ok, err := regexp.MatchString(expectedRex, result)
325 if err != nil {
326 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
327 return
328 }
329 if !ok {
330 t.Errorf("%s does not match regular expession %s", result, expectedRex)
331 }
332}
333
Jiyong Park25fc6a92018-11-18 18:02:45 +0900334func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900335 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900336 if !android.InList(expected, result) {
337 t.Errorf("%q is not found in %v", expected, result)
338 }
339}
340
341func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900342 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900343 if android.InList(notExpected, result) {
344 t.Errorf("%q is found in %v", notExpected, result)
345 }
346}
347
Jooyung Hane1633032019-08-01 17:41:43 +0900348func ensureListEmpty(t *testing.T, result []string) {
349 t.Helper()
350 if len(result) > 0 {
351 t.Errorf("%q is expected to be empty", result)
352 }
353}
354
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000355func ensureListNotEmpty(t *testing.T, result []string) {
356 t.Helper()
357 if len(result) == 0 {
358 t.Errorf("%q is expected to be not empty", result)
359 }
360}
361
Jiyong Park25fc6a92018-11-18 18:02:45 +0900362// Minimal test
363func TestBasicApex(t *testing.T) {
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900364 ctx, config := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900365 apex_defaults {
366 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900367 manifest: ":myapex.manifest",
368 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900369 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900370 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900371 native_shared_libs: [
372 "mylib",
373 "libfoo.ffi",
374 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900375 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800376 multilib: {
377 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900378 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800379 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900380 },
Jiyong Park77acec62020-06-01 21:39:15 +0900381 java_libs: [
382 "myjar",
383 "myjar_dex",
384 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900385 }
386
Jiyong Park30ca9372019-02-07 16:27:23 +0900387 apex {
388 name: "myapex",
389 defaults: ["myapex-defaults"],
390 }
391
Jiyong Park25fc6a92018-11-18 18:02:45 +0900392 apex_key {
393 name: "myapex.key",
394 public_key: "testkey.avbpubkey",
395 private_key: "testkey.pem",
396 }
397
Jiyong Park809bb722019-02-13 21:33:49 +0900398 filegroup {
399 name: "myapex.manifest",
400 srcs: ["apex_manifest.json"],
401 }
402
403 filegroup {
404 name: "myapex.androidmanifest",
405 srcs: ["AndroidManifest.xml"],
406 }
407
Jiyong Park25fc6a92018-11-18 18:02:45 +0900408 cc_library {
409 name: "mylib",
410 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900411 shared_libs: [
412 "mylib2",
413 "libbar.ffi",
414 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900415 system_shared_libs: [],
416 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000417 // TODO: remove //apex_available:platform
418 apex_available: [
419 "//apex_available:platform",
420 "myapex",
421 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900422 }
423
Alex Light3d673592019-01-18 14:37:31 -0800424 cc_binary {
425 name: "foo",
426 srcs: ["mylib.cpp"],
427 compile_multilib: "both",
428 multilib: {
429 lib32: {
430 suffix: "32",
431 },
432 lib64: {
433 suffix: "64",
434 },
435 },
436 symlinks: ["foo_link_"],
437 symlink_preferred_arch: true,
438 system_shared_libs: [],
439 static_executable: true,
440 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700441 apex_available: [ "myapex", "com.android.gki.*" ],
442 }
443
Jiyong Park99644e92020-11-17 22:21:02 +0900444 rust_binary {
445 name: "foo.rust",
446 srcs: ["foo.rs"],
447 rlibs: ["libfoo.rlib.rust"],
448 dylibs: ["libfoo.dylib.rust"],
449 apex_available: ["myapex"],
450 }
451
452 rust_library_rlib {
453 name: "libfoo.rlib.rust",
454 srcs: ["foo.rs"],
455 crate_name: "foo",
456 apex_available: ["myapex"],
457 }
458
459 rust_library_dylib {
460 name: "libfoo.dylib.rust",
461 srcs: ["foo.rs"],
462 crate_name: "foo",
463 apex_available: ["myapex"],
464 }
465
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900466 rust_ffi_shared {
467 name: "libfoo.ffi",
468 srcs: ["foo.rs"],
469 crate_name: "foo",
470 apex_available: ["myapex"],
471 }
472
473 rust_ffi_shared {
474 name: "libbar.ffi",
475 srcs: ["foo.rs"],
476 crate_name: "bar",
477 apex_available: ["myapex"],
478 }
479
Yifan Hongd22a84a2020-07-28 17:37:46 -0700480 apex {
481 name: "com.android.gki.fake",
482 binaries: ["foo"],
483 key: "myapex.key",
484 file_contexts: ":myapex-file_contexts",
Alex Light3d673592019-01-18 14:37:31 -0800485 }
486
Paul Duffindddd5462020-04-07 15:25:44 +0100487 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900488 name: "mylib2",
489 srcs: ["mylib.cpp"],
490 system_shared_libs: [],
491 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900492 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900493 static_libs: ["libstatic"],
494 // TODO: remove //apex_available:platform
495 apex_available: [
496 "//apex_available:platform",
497 "myapex",
498 ],
499 }
500
Paul Duffindddd5462020-04-07 15:25:44 +0100501 cc_prebuilt_library_shared {
502 name: "mylib2",
503 srcs: ["prebuilt.so"],
504 // TODO: remove //apex_available:platform
505 apex_available: [
506 "//apex_available:platform",
507 "myapex",
508 ],
509 }
510
Jiyong Park9918e1a2020-03-17 19:16:40 +0900511 cc_library_static {
512 name: "libstatic",
513 srcs: ["mylib.cpp"],
514 system_shared_libs: [],
515 stl: "none",
516 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000517 // TODO: remove //apex_available:platform
518 apex_available: [
519 "//apex_available:platform",
520 "myapex",
521 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900522 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900523
524 java_library {
525 name: "myjar",
526 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900527 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900528 sdk_version: "none",
529 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900530 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900531 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000532 // TODO: remove //apex_available:platform
533 apex_available: [
534 "//apex_available:platform",
535 "myapex",
536 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900537 }
538
Jiyong Park77acec62020-06-01 21:39:15 +0900539 dex_import {
540 name: "myjar_dex",
541 jars: ["prebuilt.jar"],
542 apex_available: [
543 "//apex_available:platform",
544 "myapex",
545 ],
546 }
547
Jiyong Park7f7766d2019-07-25 22:02:35 +0900548 java_library {
549 name: "myotherjar",
550 srcs: ["foo/bar/MyClass.java"],
551 sdk_version: "none",
552 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900553 // TODO: remove //apex_available:platform
554 apex_available: [
555 "//apex_available:platform",
556 "myapex",
557 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900558 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900559
560 java_library {
561 name: "mysharedjar",
562 srcs: ["foo/bar/MyClass.java"],
563 sdk_version: "none",
564 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900565 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900566 `)
567
Sundong Ahnabb64432019-10-22 13:58:29 +0900568 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900569
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900570 // Make sure that Android.mk is created
571 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
572 data := android.AndroidMkDataForTest(t, config, "", ab)
573 var builder strings.Builder
574 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
575
576 androidMk := builder.String()
577 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
578 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
579
Jiyong Park42cca6c2019-04-01 11:15:50 +0900580 optFlags := apexRule.Args["opt_flags"]
581 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700582 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900583 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900584
Jiyong Park25fc6a92018-11-18 18:02:45 +0900585 copyCmds := apexRule.Args["copy_commands"]
586
587 // Ensure that main rule creates an output
588 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
589
590 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700591 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
592 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
593 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900594 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900595 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900596
597 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700598 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
599 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900600 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
601 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900602 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900603
604 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800605 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
606 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900607 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900608 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900609 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900610 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
611 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900612 // .. but not for java libs
613 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900614 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800615
Colin Cross7113d202019-11-20 16:39:12 -0800616 // Ensure that the platform variant ends with _shared or _common
617 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
618 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900619 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
620 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900621 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
622
623 // Ensure that dynamic dependency to java libs are not included
624 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800625
626 // Ensure that all symlinks are present.
627 found_foo_link_64 := false
628 found_foo := false
629 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900630 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800631 if strings.HasSuffix(cmd, "bin/foo") {
632 found_foo = true
633 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
634 found_foo_link_64 = true
635 }
636 }
637 }
638 good := found_foo && found_foo_link_64
639 if !good {
640 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
641 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900642
Sundong Ahnabb64432019-10-22 13:58:29 +0900643 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700644 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900645 if len(noticeInputs) != 3 {
646 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900647 }
648 ensureListContains(t, noticeInputs, "NOTICE")
649 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900650 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900651
Artur Satayeva8bd1132020-04-27 18:07:06 +0100652 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100653 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
654 ensureListContains(t, fullDepsInfo, " mylib(minSdkVersion:(no version)) <- myapex")
655 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
656 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
657 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100658
659 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100660 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
661 ensureListContains(t, flatDepsInfo, "mylib(minSdkVersion:(no version))")
662 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
663 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
664 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800665}
666
Jooyung Hanf21c7972019-12-16 22:32:06 +0900667func TestDefaults(t *testing.T) {
668 ctx, _ := testApex(t, `
669 apex_defaults {
670 name: "myapex-defaults",
671 key: "myapex.key",
672 prebuilts: ["myetc"],
673 native_shared_libs: ["mylib"],
674 java_libs: ["myjar"],
675 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900676 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800677 bpfs: ["bpf"],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900678 }
679
680 prebuilt_etc {
681 name: "myetc",
682 src: "myprebuilt",
683 }
684
685 apex {
686 name: "myapex",
687 defaults: ["myapex-defaults"],
688 }
689
690 apex_key {
691 name: "myapex.key",
692 public_key: "testkey.avbpubkey",
693 private_key: "testkey.pem",
694 }
695
696 cc_library {
697 name: "mylib",
698 system_shared_libs: [],
699 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000700 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900701 }
702
703 java_library {
704 name: "myjar",
705 srcs: ["foo/bar/MyClass.java"],
706 sdk_version: "none",
707 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000708 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900709 }
710
711 android_app {
712 name: "AppFoo",
713 srcs: ["foo/bar/MyClass.java"],
714 sdk_version: "none",
715 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000716 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900717 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900718
719 runtime_resource_overlay {
720 name: "rro",
721 theme: "blue",
722 }
723
markchien2f59ec92020-09-02 16:23:38 +0800724 bpf {
725 name: "bpf",
726 srcs: ["bpf.c", "bpf2.c"],
727 }
728
Jooyung Hanf21c7972019-12-16 22:32:06 +0900729 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000730 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900731 "etc/myetc",
732 "javalib/myjar.jar",
733 "lib64/mylib.so",
734 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900735 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800736 "etc/bpf/bpf.o",
737 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900738 })
739}
740
Jooyung Han01a3ee22019-11-02 02:52:25 +0900741func TestApexManifest(t *testing.T) {
742 ctx, _ := testApex(t, `
743 apex {
744 name: "myapex",
745 key: "myapex.key",
746 }
747
748 apex_key {
749 name: "myapex.key",
750 public_key: "testkey.avbpubkey",
751 private_key: "testkey.pem",
752 }
753 `)
754
755 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900756 args := module.Rule("apexRule").Args
757 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
758 t.Error("manifest should be apex_manifest.pb, but " + manifest)
759 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900760}
761
Alex Light5098a612018-11-29 17:12:15 -0800762func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700763 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800764 apex {
765 name: "myapex",
766 key: "myapex.key",
767 payload_type: "zip",
768 native_shared_libs: ["mylib"],
769 }
770
771 apex_key {
772 name: "myapex.key",
773 public_key: "testkey.avbpubkey",
774 private_key: "testkey.pem",
775 }
776
777 cc_library {
778 name: "mylib",
779 srcs: ["mylib.cpp"],
780 shared_libs: ["mylib2"],
781 system_shared_libs: [],
782 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000783 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800784 }
785
786 cc_library {
787 name: "mylib2",
788 srcs: ["mylib.cpp"],
789 system_shared_libs: [],
790 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000791 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800792 }
793 `)
794
Sundong Ahnabb64432019-10-22 13:58:29 +0900795 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800796 copyCmds := zipApexRule.Args["copy_commands"]
797
798 // Ensure that main rule creates an output
799 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
800
801 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700802 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800803
804 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700805 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800806
807 // Ensure that both direct and indirect deps are copied into apex
808 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
809 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900810}
811
812func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700813 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900814 apex {
815 name: "myapex",
816 key: "myapex.key",
817 native_shared_libs: ["mylib", "mylib3"],
818 }
819
820 apex_key {
821 name: "myapex.key",
822 public_key: "testkey.avbpubkey",
823 private_key: "testkey.pem",
824 }
825
826 cc_library {
827 name: "mylib",
828 srcs: ["mylib.cpp"],
829 shared_libs: ["mylib2", "mylib3"],
830 system_shared_libs: [],
831 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000832 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900833 }
834
835 cc_library {
836 name: "mylib2",
837 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900838 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900839 system_shared_libs: [],
840 stl: "none",
841 stubs: {
842 versions: ["1", "2", "3"],
843 },
844 }
845
846 cc_library {
847 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900848 srcs: ["mylib.cpp"],
849 shared_libs: ["mylib4"],
850 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900851 stl: "none",
852 stubs: {
853 versions: ["10", "11", "12"],
854 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000855 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900856 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900857
858 cc_library {
859 name: "mylib4",
860 srcs: ["mylib.cpp"],
861 system_shared_libs: [],
862 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000863 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900864 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900865 `)
866
Sundong Ahnabb64432019-10-22 13:58:29 +0900867 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900868 copyCmds := apexRule.Args["copy_commands"]
869
870 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800871 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900872
873 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800874 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900875
876 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800877 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900878
Colin Crossaede88c2020-08-11 12:17:01 -0700879 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900880
881 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900882 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900883 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900884 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900885
886 // 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 -0700887 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900888 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700889 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900890
891 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900892 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900893 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900894
895 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700896 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900897
Jooyung Hana57af4a2020-01-23 05:36:59 +0000898 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900899 "lib64/mylib.so",
900 "lib64/mylib3.so",
901 "lib64/mylib4.so",
902 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900903}
904
Colin Cross7812fd32020-09-25 12:35:10 -0700905func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
906 t.Parallel()
907 ctx, _ := testApex(t, `
908 apex {
909 name: "myapex",
910 key: "myapex.key",
911 native_shared_libs: ["mylib", "mylib3"],
912 min_sdk_version: "29",
913 }
914
915 apex_key {
916 name: "myapex.key",
917 public_key: "testkey.avbpubkey",
918 private_key: "testkey.pem",
919 }
920
921 cc_library {
922 name: "mylib",
923 srcs: ["mylib.cpp"],
924 shared_libs: ["mylib2", "mylib3"],
925 system_shared_libs: [],
926 stl: "none",
927 apex_available: [ "myapex" ],
928 min_sdk_version: "28",
929 }
930
931 cc_library {
932 name: "mylib2",
933 srcs: ["mylib.cpp"],
934 cflags: ["-include mylib.h"],
935 system_shared_libs: [],
936 stl: "none",
937 stubs: {
938 versions: ["28", "29", "30", "current"],
939 },
940 min_sdk_version: "28",
941 }
942
943 cc_library {
944 name: "mylib3",
945 srcs: ["mylib.cpp"],
946 shared_libs: ["mylib4"],
947 system_shared_libs: [],
948 stl: "none",
949 stubs: {
950 versions: ["28", "29", "30", "current"],
951 },
952 apex_available: [ "myapex" ],
953 min_sdk_version: "28",
954 }
955
956 cc_library {
957 name: "mylib4",
958 srcs: ["mylib.cpp"],
959 system_shared_libs: [],
960 stl: "none",
961 apex_available: [ "myapex" ],
962 min_sdk_version: "28",
963 }
964 `)
965
966 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
967 copyCmds := apexRule.Args["copy_commands"]
968
969 // Ensure that direct non-stubs dep is always included
970 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
971
972 // Ensure that indirect stubs dep is not included
973 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
974
975 // Ensure that direct stubs dep is included
976 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
977
978 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
979
980 // Ensure that mylib is linking with the version 29 stubs for mylib2
981 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_29/mylib2.so")
982 // ... and not linking to the non-stub (impl) variant of mylib2
983 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
984
985 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
986 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
987 // .. and not linking to the stubs variant of mylib3
988 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
989
990 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -0700991 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -0700992 ensureNotContains(t, mylib2Cflags, "-include ")
993
994 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700995 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -0700996
997 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
998 "lib64/mylib.so",
999 "lib64/mylib3.so",
1000 "lib64/mylib4.so",
1001 })
1002}
1003
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001004func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001005 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001006 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001007 name: "myapex2",
1008 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001009 native_shared_libs: ["mylib"],
1010 }
1011
1012 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001013 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001014 public_key: "testkey.avbpubkey",
1015 private_key: "testkey.pem",
1016 }
1017
1018 cc_library {
1019 name: "mylib",
1020 srcs: ["mylib.cpp"],
1021 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001022 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001023 system_shared_libs: [],
1024 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001025 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001026 }
1027
1028 cc_library {
1029 name: "libfoo",
1030 srcs: ["mylib.cpp"],
1031 shared_libs: ["libbar"],
1032 system_shared_libs: [],
1033 stl: "none",
1034 stubs: {
1035 versions: ["10", "20", "30"],
1036 },
1037 }
1038
1039 cc_library {
1040 name: "libbar",
1041 srcs: ["mylib.cpp"],
1042 system_shared_libs: [],
1043 stl: "none",
1044 }
1045
Jiyong Park678c8812020-02-07 17:25:49 +09001046 cc_library_static {
1047 name: "libbaz",
1048 srcs: ["mylib.cpp"],
1049 system_shared_libs: [],
1050 stl: "none",
1051 apex_available: [ "myapex2" ],
1052 }
1053
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001054 `)
1055
Jiyong Park83dc74b2020-01-14 18:38:44 +09001056 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001057 copyCmds := apexRule.Args["copy_commands"]
1058
1059 // Ensure that direct non-stubs dep is always included
1060 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1061
1062 // Ensure that indirect stubs dep is not included
1063 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1064
1065 // Ensure that dependency of stubs is not included
1066 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1067
Colin Crossaede88c2020-08-11 12:17:01 -07001068 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001069
1070 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001071 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001072 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001073 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001074
Jiyong Park3ff16992019-12-27 14:11:47 +09001075 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001076
1077 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1078 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001079
Artur Satayeva8bd1132020-04-27 18:07:06 +01001080 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001081 ensureListContains(t, fullDepsInfo, " mylib(minSdkVersion:(no version)) <- myapex2")
1082 ensureListContains(t, fullDepsInfo, " libbaz(minSdkVersion:(no version)) <- mylib")
1083 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001084
Artur Satayeva8bd1132020-04-27 18:07:06 +01001085 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001086 ensureListContains(t, flatDepsInfo, "mylib(minSdkVersion:(no version))")
1087 ensureListContains(t, flatDepsInfo, "libbaz(minSdkVersion:(no version))")
1088 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001089}
1090
Jooyung Hand3639552019-08-09 12:57:43 +09001091func TestApexWithRuntimeLibsDependency(t *testing.T) {
1092 /*
1093 myapex
1094 |
1095 v (runtime_libs)
1096 mylib ------+------> libfoo [provides stub]
1097 |
1098 `------> libbar
1099 */
1100 ctx, _ := testApex(t, `
1101 apex {
1102 name: "myapex",
1103 key: "myapex.key",
1104 native_shared_libs: ["mylib"],
1105 }
1106
1107 apex_key {
1108 name: "myapex.key",
1109 public_key: "testkey.avbpubkey",
1110 private_key: "testkey.pem",
1111 }
1112
1113 cc_library {
1114 name: "mylib",
1115 srcs: ["mylib.cpp"],
1116 runtime_libs: ["libfoo", "libbar"],
1117 system_shared_libs: [],
1118 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001119 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001120 }
1121
1122 cc_library {
1123 name: "libfoo",
1124 srcs: ["mylib.cpp"],
1125 system_shared_libs: [],
1126 stl: "none",
1127 stubs: {
1128 versions: ["10", "20", "30"],
1129 },
1130 }
1131
1132 cc_library {
1133 name: "libbar",
1134 srcs: ["mylib.cpp"],
1135 system_shared_libs: [],
1136 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001137 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001138 }
1139
1140 `)
1141
Sundong Ahnabb64432019-10-22 13:58:29 +09001142 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001143 copyCmds := apexRule.Args["copy_commands"]
1144
1145 // Ensure that direct non-stubs dep is always included
1146 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1147
1148 // Ensure that indirect stubs dep is not included
1149 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1150
1151 // Ensure that runtime_libs dep in included
1152 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1153
Sundong Ahnabb64432019-10-22 13:58:29 +09001154 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001155 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1156 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001157
1158}
1159
Jooyung Han8ce8db92020-05-15 19:05:05 +09001160func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
1161 ctx, _ := testApex(t, "", func(fs map[string][]byte, config android.Config) {
1162 bp := `
1163 apex {
1164 name: "com.android.runtime",
1165 key: "com.android.runtime.key",
1166 native_shared_libs: ["libc"],
1167 }
1168
1169 apex_key {
1170 name: "com.android.runtime.key",
1171 public_key: "testkey.avbpubkey",
1172 private_key: "testkey.pem",
1173 }
1174
1175 cc_library {
1176 name: "libc",
1177 no_libcrt: true,
1178 nocrt: true,
1179 stl: "none",
1180 system_shared_libs: [],
1181 stubs: { versions: ["1"] },
1182 apex_available: ["com.android.runtime"],
1183
1184 sanitize: {
1185 hwaddress: true,
1186 }
1187 }
1188
1189 cc_prebuilt_library_shared {
1190 name: "libclang_rt.hwasan-aarch64-android",
1191 no_libcrt: true,
1192 nocrt: true,
1193 stl: "none",
1194 system_shared_libs: [],
1195 srcs: [""],
1196 stubs: { versions: ["1"] },
1197
1198 sanitize: {
1199 never: true,
1200 },
1201 }
1202 `
1203 // override bp to use hard-coded names: com.android.runtime and libc
1204 fs["Android.bp"] = []byte(bp)
1205 fs["system/sepolicy/apex/com.android.runtime-file_contexts"] = nil
1206 })
1207
1208 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1209 "lib64/bionic/libc.so",
1210 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1211 })
1212
1213 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1214
1215 installed := hwasan.Description("install libclang_rt.hwasan")
1216 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1217
1218 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1219 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1220 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1221}
1222
1223func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
1224 ctx, _ := testApex(t, "", func(fs map[string][]byte, config android.Config) {
1225 bp := `
1226 apex {
1227 name: "com.android.runtime",
1228 key: "com.android.runtime.key",
1229 native_shared_libs: ["libc"],
1230 }
1231
1232 apex_key {
1233 name: "com.android.runtime.key",
1234 public_key: "testkey.avbpubkey",
1235 private_key: "testkey.pem",
1236 }
1237
1238 cc_library {
1239 name: "libc",
1240 no_libcrt: true,
1241 nocrt: true,
1242 stl: "none",
1243 system_shared_libs: [],
1244 stubs: { versions: ["1"] },
1245 apex_available: ["com.android.runtime"],
1246 }
1247
1248 cc_prebuilt_library_shared {
1249 name: "libclang_rt.hwasan-aarch64-android",
1250 no_libcrt: true,
1251 nocrt: true,
1252 stl: "none",
1253 system_shared_libs: [],
1254 srcs: [""],
1255 stubs: { versions: ["1"] },
1256
1257 sanitize: {
1258 never: true,
1259 },
1260 }
1261 `
1262 // override bp to use hard-coded names: com.android.runtime and libc
1263 fs["Android.bp"] = []byte(bp)
1264 fs["system/sepolicy/apex/com.android.runtime-file_contexts"] = nil
1265
1266 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1267 })
1268
1269 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1270 "lib64/bionic/libc.so",
1271 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1272 })
1273
1274 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1275
1276 installed := hwasan.Description("install libclang_rt.hwasan")
1277 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1278
1279 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1280 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1281 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1282}
1283
Jooyung Han61b66e92020-03-21 14:21:46 +00001284func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1285 testcases := []struct {
1286 name string
1287 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001288 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001289 shouldLink string
1290 shouldNotLink []string
1291 }{
1292 {
Jooyung Han75568392020-03-20 04:29:24 +09001293 name: "should link to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001294 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001295 apexVariant: "apex10000",
Jooyung Han61b66e92020-03-21 14:21:46 +00001296 shouldLink: "30",
1297 shouldNotLink: []string{"29"},
1298 },
1299 {
1300 name: "should link to llndk#29",
Jooyung Han749dc692020-04-15 11:03:39 +09001301 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001302 apexVariant: "apex29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001303 shouldLink: "29",
1304 shouldNotLink: []string{"30"},
1305 },
1306 }
1307 for _, tc := range testcases {
1308 t.Run(tc.name, func(t *testing.T) {
1309 ctx, _ := testApex(t, `
1310 apex {
1311 name: "myapex",
1312 key: "myapex.key",
1313 use_vendor: true,
1314 native_shared_libs: ["mylib"],
Jooyung Han749dc692020-04-15 11:03:39 +09001315 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001316 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001317
Jooyung Han61b66e92020-03-21 14:21:46 +00001318 apex_key {
1319 name: "myapex.key",
1320 public_key: "testkey.avbpubkey",
1321 private_key: "testkey.pem",
1322 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001323
Jooyung Han61b66e92020-03-21 14:21:46 +00001324 cc_library {
1325 name: "mylib",
1326 srcs: ["mylib.cpp"],
1327 vendor_available: true,
1328 shared_libs: ["libbar"],
1329 system_shared_libs: [],
1330 stl: "none",
1331 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001332 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001333 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001334
Jooyung Han61b66e92020-03-21 14:21:46 +00001335 cc_library {
1336 name: "libbar",
1337 srcs: ["mylib.cpp"],
1338 system_shared_libs: [],
1339 stl: "none",
1340 stubs: { versions: ["29","30"] },
Colin Cross0477b422020-10-13 18:43:54 -07001341 llndk_stubs: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001342 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001343
Jooyung Han61b66e92020-03-21 14:21:46 +00001344 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001345 name: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001346 symbol_file: "",
1347 }
1348 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07001349 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Han61b66e92020-03-21 14:21:46 +00001350 }, withUnbundledBuild)
Jooyung Han9c80bae2019-08-20 17:30:57 +09001351
Jooyung Han61b66e92020-03-21 14:21:46 +00001352 // Ensure that LLNDK dep is not included
1353 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1354 "lib64/mylib.so",
1355 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001356
Jooyung Han61b66e92020-03-21 14:21:46 +00001357 // Ensure that LLNDK dep is required
1358 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1359 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1360 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001361
Colin Crossaede88c2020-08-11 12:17:01 -07001362 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
Colin Cross127bb8b2020-12-16 16:46:01 -08001363 ensureContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001364 for _, ver := range tc.shouldNotLink {
Colin Cross127bb8b2020-12-16 16:46:01 -08001365 ensureNotContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001366 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001367
Colin Crossaede88c2020-08-11 12:17:01 -07001368 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jooyung Han61b66e92020-03-21 14:21:46 +00001369 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
1370 })
1371 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001372}
1373
Jiyong Park25fc6a92018-11-18 18:02:45 +09001374func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001375 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001376 apex {
1377 name: "myapex",
1378 key: "myapex.key",
1379 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
1380 }
1381
1382 apex_key {
1383 name: "myapex.key",
1384 public_key: "testkey.avbpubkey",
1385 private_key: "testkey.pem",
1386 }
1387
1388 cc_library {
1389 name: "mylib",
1390 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001391 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001392 shared_libs: ["libdl#27"],
1393 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001394 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001395 }
1396
1397 cc_library_shared {
1398 name: "mylib_shared",
1399 srcs: ["mylib.cpp"],
1400 shared_libs: ["libdl#27"],
1401 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001402 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001403 }
1404
1405 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001406 name: "libBootstrap",
1407 srcs: ["mylib.cpp"],
1408 stl: "none",
1409 bootstrap: true,
1410 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001411 `)
1412
Sundong Ahnabb64432019-10-22 13:58:29 +09001413 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001414 copyCmds := apexRule.Args["copy_commands"]
1415
1416 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001417 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001418 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1419 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001420
1421 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001422 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001423
Colin Crossaede88c2020-08-11 12:17:01 -07001424 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1425 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1426 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001427
1428 // For dependency to libc
1429 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001430 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001431 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001432 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001433 // ... Cflags from stub is correctly exported to mylib
1434 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1435 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1436
1437 // For dependency to libm
1438 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001439 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001440 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001441 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001442 // ... and is not compiling with the stub
1443 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1444 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1445
1446 // For dependency to libdl
1447 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001448 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001449 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001450 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1451 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001452 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001453 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001454 // ... Cflags from stub is correctly exported to mylib
1455 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1456 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001457
1458 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001459 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1460 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1461 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1462 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001463}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001464
Jooyung Han749dc692020-04-15 11:03:39 +09001465func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001466 // there are three links between liba --> libz
Jooyung Han749dc692020-04-15 11:03:39 +09001467 // 1) myapex -> libx -> liba -> libz : this should be #29 link, but fallback to #28
1468 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001469 // 3) (platform) -> liba -> libz : this should be non-stub link
1470 ctx, _ := testApex(t, `
1471 apex {
1472 name: "myapex",
1473 key: "myapex.key",
1474 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001475 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001476 }
1477
1478 apex {
1479 name: "otherapex",
1480 key: "myapex.key",
1481 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001482 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001483 }
1484
1485 apex_key {
1486 name: "myapex.key",
1487 public_key: "testkey.avbpubkey",
1488 private_key: "testkey.pem",
1489 }
1490
1491 cc_library {
1492 name: "libx",
1493 shared_libs: ["liba"],
1494 system_shared_libs: [],
1495 stl: "none",
1496 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001497 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001498 }
1499
1500 cc_library {
1501 name: "liby",
1502 shared_libs: ["liba"],
1503 system_shared_libs: [],
1504 stl: "none",
1505 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001506 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001507 }
1508
1509 cc_library {
1510 name: "liba",
1511 shared_libs: ["libz"],
1512 system_shared_libs: [],
1513 stl: "none",
1514 apex_available: [
1515 "//apex_available:anyapex",
1516 "//apex_available:platform",
1517 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001518 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001519 }
1520
1521 cc_library {
1522 name: "libz",
1523 system_shared_libs: [],
1524 stl: "none",
1525 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001526 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001527 },
1528 }
Jooyung Han749dc692020-04-15 11:03:39 +09001529 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001530
1531 expectLink := func(from, from_variant, to, to_variant string) {
1532 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1533 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1534 }
1535 expectNoLink := func(from, from_variant, to, to_variant string) {
1536 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1537 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1538 }
1539 // platform liba is linked to non-stub version
1540 expectLink("liba", "shared", "libz", "shared")
Jooyung Han749dc692020-04-15 11:03:39 +09001541 // liba in myapex is linked to #28
Colin Crossaede88c2020-08-11 12:17:01 -07001542 expectLink("liba", "shared_apex29", "libz", "shared_28")
1543 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
1544 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jooyung Han749dc692020-04-15 11:03:39 +09001545 // liba in otherapex is linked to #30
Colin Crossaede88c2020-08-11 12:17:01 -07001546 expectLink("liba", "shared_apex30", "libz", "shared_30")
1547 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1548 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001549}
1550
Jooyung Hanaed150d2020-04-02 01:41:41 +09001551func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
1552 ctx, _ := testApex(t, `
1553 apex {
1554 name: "myapex",
1555 key: "myapex.key",
1556 native_shared_libs: ["libx"],
1557 min_sdk_version: "R",
1558 }
1559
1560 apex_key {
1561 name: "myapex.key",
1562 public_key: "testkey.avbpubkey",
1563 private_key: "testkey.pem",
1564 }
1565
1566 cc_library {
1567 name: "libx",
1568 shared_libs: ["libz"],
1569 system_shared_libs: [],
1570 stl: "none",
1571 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001572 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001573 }
1574
1575 cc_library {
1576 name: "libz",
1577 system_shared_libs: [],
1578 stl: "none",
1579 stubs: {
1580 versions: ["29", "R"],
1581 },
1582 }
1583 `, func(fs map[string][]byte, config android.Config) {
1584 config.TestProductVariables.Platform_version_active_codenames = []string{"R"}
1585 })
1586
1587 expectLink := func(from, from_variant, to, to_variant string) {
1588 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1589 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1590 }
1591 expectNoLink := func(from, from_variant, to, to_variant string) {
1592 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1593 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1594 }
Dan Albertc8060532020-07-22 22:32:17 -07001595 expectLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001596 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1597 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001598}
1599
Jooyung Han749dc692020-04-15 11:03:39 +09001600func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001601 ctx, _ := testApex(t, `
1602 apex {
1603 name: "myapex",
1604 key: "myapex.key",
1605 native_shared_libs: ["libx"],
1606 }
1607
1608 apex_key {
1609 name: "myapex.key",
1610 public_key: "testkey.avbpubkey",
1611 private_key: "testkey.pem",
1612 }
1613
1614 cc_library {
1615 name: "libx",
1616 shared_libs: ["libz"],
1617 system_shared_libs: [],
1618 stl: "none",
1619 apex_available: [ "myapex" ],
1620 }
1621
1622 cc_library {
1623 name: "libz",
1624 system_shared_libs: [],
1625 stl: "none",
1626 stubs: {
1627 versions: ["1", "2"],
1628 },
1629 }
1630 `)
1631
1632 expectLink := func(from, from_variant, to, to_variant string) {
1633 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1634 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1635 }
1636 expectNoLink := func(from, from_variant, to, to_variant string) {
1637 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1638 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1639 }
Colin Crossaede88c2020-08-11 12:17:01 -07001640 expectLink("libx", "shared_apex10000", "libz", "shared_2")
1641 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
1642 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001643}
1644
1645func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
1646 ctx, _ := testApex(t, `
1647 apex {
1648 name: "myapex",
1649 key: "myapex.key",
1650 native_shared_libs: ["libx"],
1651 }
1652
1653 apex_key {
1654 name: "myapex.key",
1655 public_key: "testkey.avbpubkey",
1656 private_key: "testkey.pem",
1657 }
1658
1659 cc_library {
1660 name: "libx",
1661 system_shared_libs: [],
1662 stl: "none",
1663 apex_available: [ "myapex" ],
1664 stubs: {
1665 versions: ["1", "2"],
1666 },
1667 }
1668
1669 cc_library {
1670 name: "libz",
1671 shared_libs: ["libx"],
1672 system_shared_libs: [],
1673 stl: "none",
1674 }
1675 `)
1676
1677 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001678 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001679 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1680 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1681 }
1682 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001683 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001684 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1685 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1686 }
1687 expectLink("libz", "shared", "libx", "shared_2")
1688 expectNoLink("libz", "shared", "libz", "shared_1")
1689 expectNoLink("libz", "shared", "libz", "shared")
1690}
1691
Jooyung Han75568392020-03-20 04:29:24 +09001692func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001693 ctx, _ := testApex(t, `
1694 apex {
1695 name: "myapex",
1696 key: "myapex.key",
1697 native_shared_libs: ["libx"],
1698 min_sdk_version: "29",
1699 }
1700
1701 apex_key {
1702 name: "myapex.key",
1703 public_key: "testkey.avbpubkey",
1704 private_key: "testkey.pem",
1705 }
1706
1707 cc_library {
1708 name: "libx",
1709 shared_libs: ["libbar"],
1710 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001711 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001712 }
1713
1714 cc_library {
1715 name: "libbar",
1716 stubs: {
1717 versions: ["29", "30"],
1718 },
1719 }
Jooyung Han75568392020-03-20 04:29:24 +09001720 `, func(fs map[string][]byte, config android.Config) {
1721 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1722 })
Jooyung Han03b51852020-02-26 22:45:42 +09001723 expectLink := func(from, from_variant, to, to_variant string) {
1724 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1725 libFlags := ld.Args["libFlags"]
1726 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1727 }
Colin Crossaede88c2020-08-11 12:17:01 -07001728 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_30")
Jooyung Han03b51852020-02-26 22:45:42 +09001729}
1730
Jooyung Han75568392020-03-20 04:29:24 +09001731func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001732 ctx, _ := testApex(t, `
1733 apex {
1734 name: "myapex",
1735 key: "myapex.key",
1736 native_shared_libs: ["libx"],
1737 min_sdk_version: "29",
1738 }
1739
1740 apex_key {
1741 name: "myapex.key",
1742 public_key: "testkey.avbpubkey",
1743 private_key: "testkey.pem",
1744 }
1745
1746 cc_library {
1747 name: "libx",
1748 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001749 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001750 }
Jooyung Han75568392020-03-20 04:29:24 +09001751 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001752
1753 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001754 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001755 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001756 // note that platform variant is not.
1757 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001758 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001759}
1760
Jooyung Han749dc692020-04-15 11:03:39 +09001761func TestApexMinSdkVersion_ErrorIfIncompatibleStubs(t *testing.T) {
Jooyung Han75568392020-03-20 04:29:24 +09001762 testApexError(t, `"libz" .*: not found a version\(<=29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001763 apex {
1764 name: "myapex",
1765 key: "myapex.key",
1766 native_shared_libs: ["libx"],
1767 min_sdk_version: "29",
1768 }
1769
1770 apex_key {
1771 name: "myapex.key",
1772 public_key: "testkey.avbpubkey",
1773 private_key: "testkey.pem",
1774 }
1775
1776 cc_library {
1777 name: "libx",
1778 shared_libs: ["libz"],
1779 system_shared_libs: [],
1780 stl: "none",
1781 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001782 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001783 }
1784
1785 cc_library {
1786 name: "libz",
1787 system_shared_libs: [],
1788 stl: "none",
1789 stubs: {
1790 versions: ["30"],
1791 },
1792 }
Jooyung Han75568392020-03-20 04:29:24 +09001793 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001794}
Jooyung Han03b51852020-02-26 22:45:42 +09001795
Jooyung Han749dc692020-04-15 11:03:39 +09001796func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1797 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001798 apex {
1799 name: "myapex",
1800 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001801 native_shared_libs: ["mylib"],
1802 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001803 }
1804
1805 apex_key {
1806 name: "myapex.key",
1807 public_key: "testkey.avbpubkey",
1808 private_key: "testkey.pem",
1809 }
Jooyung Han749dc692020-04-15 11:03:39 +09001810
1811 cc_library {
1812 name: "mylib",
1813 srcs: ["mylib.cpp"],
1814 system_shared_libs: [],
1815 stl: "none",
1816 apex_available: [
1817 "myapex",
1818 ],
1819 min_sdk_version: "30",
1820 }
1821 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001822
1823 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
1824 apex {
1825 name: "myapex",
1826 key: "myapex.key",
1827 native_shared_libs: ["libfoo.ffi"],
1828 min_sdk_version: "29",
1829 }
1830
1831 apex_key {
1832 name: "myapex.key",
1833 public_key: "testkey.avbpubkey",
1834 private_key: "testkey.pem",
1835 }
1836
1837 rust_ffi_shared {
1838 name: "libfoo.ffi",
1839 srcs: ["foo.rs"],
1840 crate_name: "foo",
1841 apex_available: [
1842 "myapex",
1843 ],
1844 min_sdk_version: "30",
1845 }
1846 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001847}
1848
1849func TestApexMinSdkVersion_Okay(t *testing.T) {
1850 testApex(t, `
1851 apex {
1852 name: "myapex",
1853 key: "myapex.key",
1854 native_shared_libs: ["libfoo"],
1855 java_libs: ["libbar"],
1856 min_sdk_version: "29",
1857 }
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: "libfoo",
1867 srcs: ["mylib.cpp"],
1868 shared_libs: ["libfoo_dep"],
1869 apex_available: ["myapex"],
1870 min_sdk_version: "29",
1871 }
1872
1873 cc_library {
1874 name: "libfoo_dep",
1875 srcs: ["mylib.cpp"],
1876 apex_available: ["myapex"],
1877 min_sdk_version: "29",
1878 }
1879
1880 java_library {
1881 name: "libbar",
1882 sdk_version: "current",
1883 srcs: ["a.java"],
1884 static_libs: ["libbar_dep"],
1885 apex_available: ["myapex"],
1886 min_sdk_version: "29",
1887 }
1888
1889 java_library {
1890 name: "libbar_dep",
1891 sdk_version: "current",
1892 srcs: ["a.java"],
1893 apex_available: ["myapex"],
1894 min_sdk_version: "29",
1895 }
Jooyung Han03b51852020-02-26 22:45:42 +09001896 `)
1897}
1898
Artur Satayev8cf899a2020-04-15 17:29:42 +01001899func TestJavaStableSdkVersion(t *testing.T) {
1900 testCases := []struct {
1901 name string
1902 expectedError string
1903 bp string
1904 }{
1905 {
1906 name: "Non-updatable apex with non-stable dep",
1907 bp: `
1908 apex {
1909 name: "myapex",
1910 java_libs: ["myjar"],
1911 key: "myapex.key",
1912 }
1913 apex_key {
1914 name: "myapex.key",
1915 public_key: "testkey.avbpubkey",
1916 private_key: "testkey.pem",
1917 }
1918 java_library {
1919 name: "myjar",
1920 srcs: ["foo/bar/MyClass.java"],
1921 sdk_version: "core_platform",
1922 apex_available: ["myapex"],
1923 }
1924 `,
1925 },
1926 {
1927 name: "Updatable apex with stable dep",
1928 bp: `
1929 apex {
1930 name: "myapex",
1931 java_libs: ["myjar"],
1932 key: "myapex.key",
1933 updatable: true,
1934 min_sdk_version: "29",
1935 }
1936 apex_key {
1937 name: "myapex.key",
1938 public_key: "testkey.avbpubkey",
1939 private_key: "testkey.pem",
1940 }
1941 java_library {
1942 name: "myjar",
1943 srcs: ["foo/bar/MyClass.java"],
1944 sdk_version: "current",
1945 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001946 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001947 }
1948 `,
1949 },
1950 {
1951 name: "Updatable apex with non-stable dep",
1952 expectedError: "cannot depend on \"myjar\"",
1953 bp: `
1954 apex {
1955 name: "myapex",
1956 java_libs: ["myjar"],
1957 key: "myapex.key",
1958 updatable: true,
1959 }
1960 apex_key {
1961 name: "myapex.key",
1962 public_key: "testkey.avbpubkey",
1963 private_key: "testkey.pem",
1964 }
1965 java_library {
1966 name: "myjar",
1967 srcs: ["foo/bar/MyClass.java"],
1968 sdk_version: "core_platform",
1969 apex_available: ["myapex"],
1970 }
1971 `,
1972 },
1973 {
1974 name: "Updatable apex with non-stable transitive dep",
1975 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against non-public Android API.",
1976 bp: `
1977 apex {
1978 name: "myapex",
1979 java_libs: ["myjar"],
1980 key: "myapex.key",
1981 updatable: true,
1982 }
1983 apex_key {
1984 name: "myapex.key",
1985 public_key: "testkey.avbpubkey",
1986 private_key: "testkey.pem",
1987 }
1988 java_library {
1989 name: "myjar",
1990 srcs: ["foo/bar/MyClass.java"],
1991 sdk_version: "current",
1992 apex_available: ["myapex"],
1993 static_libs: ["transitive-jar"],
1994 }
1995 java_library {
1996 name: "transitive-jar",
1997 srcs: ["foo/bar/MyClass.java"],
1998 sdk_version: "core_platform",
1999 apex_available: ["myapex"],
2000 }
2001 `,
2002 },
2003 }
2004
2005 for _, test := range testCases {
2006 t.Run(test.name, func(t *testing.T) {
2007 if test.expectedError == "" {
2008 testApex(t, test.bp)
2009 } else {
2010 testApexError(t, test.expectedError, test.bp)
2011 }
2012 })
2013 }
2014}
2015
Jooyung Han749dc692020-04-15 11:03:39 +09002016func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2017 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2018 apex {
2019 name: "myapex",
2020 key: "myapex.key",
2021 native_shared_libs: ["mylib"],
2022 min_sdk_version: "29",
2023 }
2024
2025 apex_key {
2026 name: "myapex.key",
2027 public_key: "testkey.avbpubkey",
2028 private_key: "testkey.pem",
2029 }
2030
2031 cc_library {
2032 name: "mylib",
2033 srcs: ["mylib.cpp"],
2034 shared_libs: ["mylib2"],
2035 system_shared_libs: [],
2036 stl: "none",
2037 apex_available: [
2038 "myapex",
2039 ],
2040 min_sdk_version: "29",
2041 }
2042
2043 // indirect part of the apex
2044 cc_library {
2045 name: "mylib2",
2046 srcs: ["mylib.cpp"],
2047 system_shared_libs: [],
2048 stl: "none",
2049 apex_available: [
2050 "myapex",
2051 ],
2052 min_sdk_version: "30",
2053 }
2054 `)
2055}
2056
2057func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2058 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2059 apex {
2060 name: "myapex",
2061 key: "myapex.key",
2062 apps: ["AppFoo"],
2063 min_sdk_version: "29",
2064 }
2065
2066 apex_key {
2067 name: "myapex.key",
2068 public_key: "testkey.avbpubkey",
2069 private_key: "testkey.pem",
2070 }
2071
2072 android_app {
2073 name: "AppFoo",
2074 srcs: ["foo/bar/MyClass.java"],
2075 sdk_version: "current",
2076 min_sdk_version: "29",
2077 system_modules: "none",
2078 stl: "none",
2079 static_libs: ["bar"],
2080 apex_available: [ "myapex" ],
2081 }
2082
2083 java_library {
2084 name: "bar",
2085 sdk_version: "current",
2086 srcs: ["a.java"],
2087 apex_available: [ "myapex" ],
2088 }
2089 `)
2090}
2091
2092func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
2093 ctx, _ := testApex(t, `
2094 apex {
2095 name: "myapex",
2096 key: "myapex.key",
2097 native_shared_libs: ["mylib"],
2098 min_sdk_version: "29",
2099 }
2100
2101 apex_key {
2102 name: "myapex.key",
2103 public_key: "testkey.avbpubkey",
2104 private_key: "testkey.pem",
2105 }
2106
2107 // mylib in myapex will link to mylib2#29
2108 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2109 cc_library {
2110 name: "mylib",
2111 srcs: ["mylib.cpp"],
2112 shared_libs: ["mylib2"],
2113 system_shared_libs: [],
2114 stl: "none",
2115 apex_available: ["myapex", "otherapex"],
2116 min_sdk_version: "29",
2117 }
2118
2119 cc_library {
2120 name: "mylib2",
2121 srcs: ["mylib.cpp"],
2122 system_shared_libs: [],
2123 stl: "none",
2124 apex_available: ["otherapex"],
2125 stubs: { versions: ["29", "30"] },
2126 min_sdk_version: "30",
2127 }
2128
2129 apex {
2130 name: "otherapex",
2131 key: "myapex.key",
2132 native_shared_libs: ["mylib", "mylib2"],
2133 min_sdk_version: "30",
2134 }
2135 `)
2136 expectLink := func(from, from_variant, to, to_variant string) {
2137 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2138 libFlags := ld.Args["libFlags"]
2139 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2140 }
Colin Crossaede88c2020-08-11 12:17:01 -07002141 expectLink("mylib", "shared_apex29", "mylib2", "shared_29")
2142 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002143}
2144
Jiyong Park7c2ee712018-12-07 00:42:25 +09002145func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002146 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002147 apex {
2148 name: "myapex",
2149 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002150 native_shared_libs: ["mylib"],
2151 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002152 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002153 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09002154 }
2155
2156 apex_key {
2157 name: "myapex.key",
2158 public_key: "testkey.avbpubkey",
2159 private_key: "testkey.pem",
2160 }
2161
2162 prebuilt_etc {
2163 name: "myetc",
2164 src: "myprebuilt",
2165 sub_dir: "foo/bar",
2166 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002167
2168 cc_library {
2169 name: "mylib",
2170 srcs: ["mylib.cpp"],
2171 relative_install_path: "foo/bar",
2172 system_shared_libs: [],
2173 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002174 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002175 }
2176
2177 cc_binary {
2178 name: "mybin",
2179 srcs: ["mylib.cpp"],
2180 relative_install_path: "foo/bar",
2181 system_shared_libs: [],
2182 static_executable: true,
2183 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002184 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002185 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002186 `)
2187
Sundong Ahnabb64432019-10-22 13:58:29 +09002188 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002189 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2190
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002191 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002192 ensureListContains(t, dirs, "etc")
2193 ensureListContains(t, dirs, "etc/foo")
2194 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002195 ensureListContains(t, dirs, "lib64")
2196 ensureListContains(t, dirs, "lib64/foo")
2197 ensureListContains(t, dirs, "lib64/foo/bar")
2198 ensureListContains(t, dirs, "lib")
2199 ensureListContains(t, dirs, "lib/foo")
2200 ensureListContains(t, dirs, "lib/foo/bar")
2201
Jiyong Parkbd13e442019-03-15 18:10:35 +09002202 ensureListContains(t, dirs, "bin")
2203 ensureListContains(t, dirs, "bin/foo")
2204 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002205}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002206
Jooyung Han35155c42020-02-06 17:33:20 +09002207func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
2208 ctx, _ := testApex(t, `
2209 apex {
2210 name: "myapex",
2211 key: "myapex.key",
2212 multilib: {
2213 both: {
2214 native_shared_libs: ["mylib"],
2215 binaries: ["mybin"],
2216 },
2217 },
2218 compile_multilib: "both",
2219 native_bridge_supported: true,
2220 }
2221
2222 apex_key {
2223 name: "myapex.key",
2224 public_key: "testkey.avbpubkey",
2225 private_key: "testkey.pem",
2226 }
2227
2228 cc_library {
2229 name: "mylib",
2230 relative_install_path: "foo/bar",
2231 system_shared_libs: [],
2232 stl: "none",
2233 apex_available: [ "myapex" ],
2234 native_bridge_supported: true,
2235 }
2236
2237 cc_binary {
2238 name: "mybin",
2239 relative_install_path: "foo/bar",
2240 system_shared_libs: [],
2241 static_executable: true,
2242 stl: "none",
2243 apex_available: [ "myapex" ],
2244 native_bridge_supported: true,
2245 compile_multilib: "both", // default is "first" for binary
2246 multilib: {
2247 lib64: {
2248 suffix: "64",
2249 },
2250 },
2251 }
2252 `, withNativeBridgeEnabled)
2253 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2254 "bin/foo/bar/mybin",
2255 "bin/foo/bar/mybin64",
2256 "bin/arm/foo/bar/mybin",
2257 "bin/arm64/foo/bar/mybin64",
2258 "lib/foo/bar/mylib.so",
2259 "lib/arm/foo/bar/mylib.so",
2260 "lib64/foo/bar/mylib.so",
2261 "lib64/arm64/foo/bar/mylib.so",
2262 })
2263}
2264
Jiyong Parkda6eb592018-12-19 17:12:36 +09002265func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002266 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002267 apex {
2268 name: "myapex",
2269 key: "myapex.key",
2270 native_shared_libs: ["mylib"],
2271 use_vendor: true,
2272 }
2273
2274 apex_key {
2275 name: "myapex.key",
2276 public_key: "testkey.avbpubkey",
2277 private_key: "testkey.pem",
2278 }
2279
2280 cc_library {
2281 name: "mylib",
2282 srcs: ["mylib.cpp"],
2283 shared_libs: ["mylib2"],
2284 system_shared_libs: [],
2285 vendor_available: true,
2286 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002287 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002288 }
2289
2290 cc_library {
2291 name: "mylib2",
2292 srcs: ["mylib.cpp"],
2293 system_shared_libs: [],
2294 vendor_available: true,
2295 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002296 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002297 }
Jooyung Handc782442019-11-01 03:14:38 +09002298 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002299 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09002300 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09002301
2302 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002303 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002304 for _, implicit := range i.Implicits {
2305 inputsList = append(inputsList, implicit.String())
2306 }
2307 }
2308 inputsString := strings.Join(inputsList, " ")
2309
2310 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossaede88c2020-08-11 12:17:01 -07002311 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib.so")
2312 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002313
2314 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002315 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2316 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002317}
Jiyong Park16e91a02018-12-20 18:18:08 +09002318
Jooyung Han85d61762020-06-24 23:50:26 +09002319func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002320 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2321 apex {
2322 name: "myapex",
2323 key: "myapex.key",
2324 use_vendor: true,
2325 }
2326 apex_key {
2327 name: "myapex.key",
2328 public_key: "testkey.avbpubkey",
2329 private_key: "testkey.pem",
2330 }
2331 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002332 setUseVendorAllowListForTest(config, []string{""})
Jooyung Handc782442019-11-01 03:14:38 +09002333 })
Colin Cross440e0d02020-06-11 11:32:11 -07002334 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002335 testApex(t, `
2336 apex {
2337 name: "myapex",
2338 key: "myapex.key",
2339 use_vendor: true,
2340 }
2341 apex_key {
2342 name: "myapex.key",
2343 public_key: "testkey.avbpubkey",
2344 private_key: "testkey.pem",
2345 }
2346 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002347 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09002348 })
2349}
2350
Jooyung Han5c998b92019-06-27 11:30:33 +09002351func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2352 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2353 apex {
2354 name: "myapex",
2355 key: "myapex.key",
2356 native_shared_libs: ["mylib"],
2357 use_vendor: true,
2358 }
2359
2360 apex_key {
2361 name: "myapex.key",
2362 public_key: "testkey.avbpubkey",
2363 private_key: "testkey.pem",
2364 }
2365
2366 cc_library {
2367 name: "mylib",
2368 srcs: ["mylib.cpp"],
2369 system_shared_libs: [],
2370 stl: "none",
2371 }
2372 `)
2373}
2374
Jooyung Han85d61762020-06-24 23:50:26 +09002375func TestVendorApex(t *testing.T) {
2376 ctx, config := testApex(t, `
2377 apex {
2378 name: "myapex",
2379 key: "myapex.key",
2380 binaries: ["mybin"],
2381 vendor: true,
2382 }
2383 apex_key {
2384 name: "myapex.key",
2385 public_key: "testkey.avbpubkey",
2386 private_key: "testkey.pem",
2387 }
2388 cc_binary {
2389 name: "mybin",
2390 vendor: true,
2391 shared_libs: ["libfoo"],
2392 }
2393 cc_library {
2394 name: "libfoo",
2395 proprietary: true,
2396 }
2397 `)
2398
2399 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2400 "bin/mybin",
2401 "lib64/libfoo.so",
2402 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2403 "lib64/libc++.so",
2404 })
2405
2406 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2407 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2408 name := apexBundle.BaseModuleName()
2409 prefix := "TARGET_"
2410 var builder strings.Builder
2411 data.Custom(&builder, name, prefix, "", data)
2412 androidMk := builder.String()
2413 ensureContains(t, androidMk, `LOCAL_MODULE_PATH := /tmp/target/product/test_device/vendor/apex`)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002414
2415 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2416 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2417 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002418}
2419
Jooyung Handf78e212020-07-22 15:54:47 +09002420func TestVendorApex_use_vndk_as_stable(t *testing.T) {
2421 ctx, _ := testApex(t, `
2422 apex {
2423 name: "myapex",
2424 key: "myapex.key",
2425 binaries: ["mybin"],
2426 vendor: true,
2427 use_vndk_as_stable: true,
2428 }
2429 apex_key {
2430 name: "myapex.key",
2431 public_key: "testkey.avbpubkey",
2432 private_key: "testkey.pem",
2433 }
2434 cc_binary {
2435 name: "mybin",
2436 vendor: true,
2437 shared_libs: ["libvndk", "libvendor"],
2438 }
2439 cc_library {
2440 name: "libvndk",
2441 vndk: {
2442 enabled: true,
2443 },
2444 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002445 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002446 }
2447 cc_library {
2448 name: "libvendor",
2449 vendor: true,
2450 }
2451 `)
2452
2453 vendorVariant := "android_vendor.VER_arm64_armv8-a"
2454
Colin Crossaede88c2020-08-11 12:17:01 -07002455 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002456 libs := names(ldRule.Args["libFlags"])
2457 // VNDK libs(libvndk/libc++) as they are
2458 ensureListContains(t, libs, buildDir+"/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
2459 ensureListContains(t, libs, buildDir+"/.intermediates/libc++/"+vendorVariant+"_shared/libc++.so")
2460 // non-stable Vendor libs as APEX variants
Colin Crossaede88c2020-08-11 12:17:01 -07002461 ensureListContains(t, libs, buildDir+"/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002462
2463 // VNDK libs are not included when use_vndk_as_stable: true
2464 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2465 "bin/mybin",
2466 "lib64/libvendor.so",
2467 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002468
2469 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2470 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2471 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002472}
2473
Jooyung Han8e5685d2020-09-21 11:02:57 +09002474func TestApex_withPrebuiltFirmware(t *testing.T) {
2475 testCases := []struct {
2476 name string
2477 additionalProp string
2478 }{
2479 {"system apex with prebuilt_firmware", ""},
2480 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2481 }
2482 for _, tc := range testCases {
2483 t.Run(tc.name, func(t *testing.T) {
2484 ctx, _ := testApex(t, `
2485 apex {
2486 name: "myapex",
2487 key: "myapex.key",
2488 prebuilts: ["myfirmware"],
2489 `+tc.additionalProp+`
2490 }
2491 apex_key {
2492 name: "myapex.key",
2493 public_key: "testkey.avbpubkey",
2494 private_key: "testkey.pem",
2495 }
2496 prebuilt_firmware {
2497 name: "myfirmware",
2498 src: "myfirmware.bin",
2499 filename_from_src: true,
2500 `+tc.additionalProp+`
2501 }
2502 `)
2503 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2504 "etc/firmware/myfirmware.bin",
2505 })
2506 })
2507 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002508}
2509
Jooyung Hanefb184e2020-06-25 17:14:25 +09002510func TestAndroidMk_UseVendorRequired(t *testing.T) {
2511 ctx, config := testApex(t, `
2512 apex {
2513 name: "myapex",
2514 key: "myapex.key",
2515 use_vendor: true,
2516 native_shared_libs: ["mylib"],
2517 }
2518
2519 apex_key {
2520 name: "myapex.key",
2521 public_key: "testkey.avbpubkey",
2522 private_key: "testkey.pem",
2523 }
2524
2525 cc_library {
2526 name: "mylib",
2527 vendor_available: true,
2528 apex_available: ["myapex"],
2529 }
2530 `, func(fs map[string][]byte, config android.Config) {
2531 setUseVendorAllowListForTest(config, []string{"myapex"})
2532 })
2533
2534 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2535 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2536 name := apexBundle.BaseModuleName()
2537 prefix := "TARGET_"
2538 var builder strings.Builder
2539 data.Custom(&builder, name, prefix, "", data)
2540 androidMk := builder.String()
2541 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2542}
2543
2544func TestAndroidMk_VendorApexRequired(t *testing.T) {
2545 ctx, config := testApex(t, `
2546 apex {
2547 name: "myapex",
2548 key: "myapex.key",
2549 vendor: true,
2550 native_shared_libs: ["mylib"],
2551 }
2552
2553 apex_key {
2554 name: "myapex.key",
2555 public_key: "testkey.avbpubkey",
2556 private_key: "testkey.pem",
2557 }
2558
2559 cc_library {
2560 name: "mylib",
2561 vendor_available: true,
2562 }
2563 `)
2564
2565 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2566 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2567 name := apexBundle.BaseModuleName()
2568 prefix := "TARGET_"
2569 var builder strings.Builder
2570 data.Custom(&builder, name, prefix, "", data)
2571 androidMk := builder.String()
2572 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2573}
2574
Jooyung Han2ed99d02020-06-24 23:26:26 +09002575func TestAndroidMkWritesCommonProperties(t *testing.T) {
2576 ctx, config := testApex(t, `
2577 apex {
2578 name: "myapex",
2579 key: "myapex.key",
2580 vintf_fragments: ["fragment.xml"],
2581 init_rc: ["init.rc"],
2582 }
2583 apex_key {
2584 name: "myapex.key",
2585 public_key: "testkey.avbpubkey",
2586 private_key: "testkey.pem",
2587 }
2588 cc_binary {
2589 name: "mybin",
2590 }
2591 `)
2592
2593 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2594 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2595 name := apexBundle.BaseModuleName()
2596 prefix := "TARGET_"
2597 var builder strings.Builder
2598 data.Custom(&builder, name, prefix, "", data)
2599 androidMk := builder.String()
2600 ensureContains(t, androidMk, "LOCAL_VINTF_FRAGMENTS := fragment.xml\n")
2601 ensureContains(t, androidMk, "LOCAL_INIT_RC := init.rc\n")
2602}
2603
Jiyong Park16e91a02018-12-20 18:18:08 +09002604func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002605 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002606 apex {
2607 name: "myapex",
2608 key: "myapex.key",
2609 native_shared_libs: ["mylib"],
2610 }
2611
2612 apex_key {
2613 name: "myapex.key",
2614 public_key: "testkey.avbpubkey",
2615 private_key: "testkey.pem",
2616 }
2617
2618 cc_library {
2619 name: "mylib",
2620 srcs: ["mylib.cpp"],
2621 system_shared_libs: [],
2622 stl: "none",
2623 stubs: {
2624 versions: ["1", "2", "3"],
2625 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002626 apex_available: [
2627 "//apex_available:platform",
2628 "myapex",
2629 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002630 }
2631
2632 cc_binary {
2633 name: "not_in_apex",
2634 srcs: ["mylib.cpp"],
2635 static_libs: ["mylib"],
2636 static_executable: true,
2637 system_shared_libs: [],
2638 stl: "none",
2639 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002640 `)
2641
Colin Cross7113d202019-11-20 16:39:12 -08002642 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002643
2644 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002645 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002646}
Jiyong Park9335a262018-12-24 11:31:58 +09002647
2648func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002649 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002650 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002651 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002652 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002653 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002654 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002655 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09002656 }
2657
2658 cc_library {
2659 name: "mylib",
2660 srcs: ["mylib.cpp"],
2661 system_shared_libs: [],
2662 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002663 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002664 }
2665
2666 apex_key {
2667 name: "myapex.key",
2668 public_key: "testkey.avbpubkey",
2669 private_key: "testkey.pem",
2670 }
2671
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002672 android_app_certificate {
2673 name: "myapex.certificate",
2674 certificate: "testkey",
2675 }
2676
2677 android_app_certificate {
2678 name: "myapex.certificate.override",
2679 certificate: "testkey.override",
2680 }
2681
Jiyong Park9335a262018-12-24 11:31:58 +09002682 `)
2683
2684 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002685 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002686
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002687 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2688 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002689 "vendor/foo/devkeys/testkey.avbpubkey")
2690 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002691 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2692 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002693 "vendor/foo/devkeys/testkey.pem")
2694 }
2695
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002696 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002697 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002698 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002699 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002700 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002701 }
2702}
Jiyong Park58e364a2019-01-19 19:24:06 +09002703
Jooyung Hanf121a652019-12-17 14:30:11 +09002704func TestCertificate(t *testing.T) {
2705 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
2706 ctx, _ := testApex(t, `
2707 apex {
2708 name: "myapex",
2709 key: "myapex.key",
2710 }
2711 apex_key {
2712 name: "myapex.key",
2713 public_key: "testkey.avbpubkey",
2714 private_key: "testkey.pem",
2715 }`)
2716 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2717 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2718 if actual := rule.Args["certificates"]; actual != expected {
2719 t.Errorf("certificates should be %q, not %q", expected, actual)
2720 }
2721 })
2722 t.Run("override when unspecified", func(t *testing.T) {
2723 ctx, _ := testApex(t, `
2724 apex {
2725 name: "myapex_keytest",
2726 key: "myapex.key",
2727 file_contexts: ":myapex-file_contexts",
2728 }
2729 apex_key {
2730 name: "myapex.key",
2731 public_key: "testkey.avbpubkey",
2732 private_key: "testkey.pem",
2733 }
2734 android_app_certificate {
2735 name: "myapex.certificate.override",
2736 certificate: "testkey.override",
2737 }`)
2738 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2739 expected := "testkey.override.x509.pem testkey.override.pk8"
2740 if actual := rule.Args["certificates"]; actual != expected {
2741 t.Errorf("certificates should be %q, not %q", expected, actual)
2742 }
2743 })
2744 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
2745 ctx, _ := testApex(t, `
2746 apex {
2747 name: "myapex",
2748 key: "myapex.key",
2749 certificate: ":myapex.certificate",
2750 }
2751 apex_key {
2752 name: "myapex.key",
2753 public_key: "testkey.avbpubkey",
2754 private_key: "testkey.pem",
2755 }
2756 android_app_certificate {
2757 name: "myapex.certificate",
2758 certificate: "testkey",
2759 }`)
2760 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2761 expected := "testkey.x509.pem testkey.pk8"
2762 if actual := rule.Args["certificates"]; actual != expected {
2763 t.Errorf("certificates should be %q, not %q", expected, actual)
2764 }
2765 })
2766 t.Run("override when specifiec as <:module>", func(t *testing.T) {
2767 ctx, _ := testApex(t, `
2768 apex {
2769 name: "myapex_keytest",
2770 key: "myapex.key",
2771 file_contexts: ":myapex-file_contexts",
2772 certificate: ":myapex.certificate",
2773 }
2774 apex_key {
2775 name: "myapex.key",
2776 public_key: "testkey.avbpubkey",
2777 private_key: "testkey.pem",
2778 }
2779 android_app_certificate {
2780 name: "myapex.certificate.override",
2781 certificate: "testkey.override",
2782 }`)
2783 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2784 expected := "testkey.override.x509.pem testkey.override.pk8"
2785 if actual := rule.Args["certificates"]; actual != expected {
2786 t.Errorf("certificates should be %q, not %q", expected, actual)
2787 }
2788 })
2789 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
2790 ctx, _ := testApex(t, `
2791 apex {
2792 name: "myapex",
2793 key: "myapex.key",
2794 certificate: "testkey",
2795 }
2796 apex_key {
2797 name: "myapex.key",
2798 public_key: "testkey.avbpubkey",
2799 private_key: "testkey.pem",
2800 }`)
2801 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2802 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2803 if actual := rule.Args["certificates"]; actual != expected {
2804 t.Errorf("certificates should be %q, not %q", expected, actual)
2805 }
2806 })
2807 t.Run("override when specified as <name>", func(t *testing.T) {
2808 ctx, _ := testApex(t, `
2809 apex {
2810 name: "myapex_keytest",
2811 key: "myapex.key",
2812 file_contexts: ":myapex-file_contexts",
2813 certificate: "testkey",
2814 }
2815 apex_key {
2816 name: "myapex.key",
2817 public_key: "testkey.avbpubkey",
2818 private_key: "testkey.pem",
2819 }
2820 android_app_certificate {
2821 name: "myapex.certificate.override",
2822 certificate: "testkey.override",
2823 }`)
2824 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2825 expected := "testkey.override.x509.pem testkey.override.pk8"
2826 if actual := rule.Args["certificates"]; actual != expected {
2827 t.Errorf("certificates should be %q, not %q", expected, actual)
2828 }
2829 })
2830}
2831
Jiyong Park58e364a2019-01-19 19:24:06 +09002832func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002833 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002834 apex {
2835 name: "myapex",
2836 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002837 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09002838 }
2839
2840 apex {
2841 name: "otherapex",
2842 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002843 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002844 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002845 }
2846
2847 apex_key {
2848 name: "myapex.key",
2849 public_key: "testkey.avbpubkey",
2850 private_key: "testkey.pem",
2851 }
2852
2853 cc_library {
2854 name: "mylib",
2855 srcs: ["mylib.cpp"],
2856 system_shared_libs: [],
2857 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002858 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002859 "myapex",
2860 "otherapex",
2861 ],
Jooyung Han24282772020-03-21 23:20:55 +09002862 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002863 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002864 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09002865 cc_library {
2866 name: "mylib2",
2867 srcs: ["mylib.cpp"],
2868 system_shared_libs: [],
2869 stl: "none",
2870 apex_available: [
2871 "myapex",
2872 "otherapex",
2873 ],
Colin Crossaede88c2020-08-11 12:17:01 -07002874 static_libs: ["mylib3"],
2875 recovery_available: true,
2876 min_sdk_version: "29",
2877 }
2878 cc_library {
2879 name: "mylib3",
2880 srcs: ["mylib.cpp"],
2881 system_shared_libs: [],
2882 stl: "none",
2883 apex_available: [
2884 "myapex",
2885 "otherapex",
2886 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09002887 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07002888 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002889 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002890 }
Jiyong Park58e364a2019-01-19 19:24:06 +09002891 `)
2892
Jooyung Hanc87a0592020-03-02 17:44:33 +09002893 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08002894 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002895 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002896 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002897
Jooyung Hanccce2f22020-03-07 03:45:53 +09002898 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07002899 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09002900 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002901 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09002902 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002903
Jooyung Hanccce2f22020-03-07 03:45:53 +09002904 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07002905 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09002906 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002907 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09002908 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002909
Colin Crossaede88c2020-08-11 12:17:01 -07002910 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
2911 // each variant defines additional macros to distinguish which apex variant it is built for
2912
2913 // non-APEX variant does not have __ANDROID_APEX__ defined
2914 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2915 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2916
2917 // APEX variant has __ANDROID_APEX__ defined
2918 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
2919 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2920 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2921 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
2922
2923 // APEX variant has __ANDROID_APEX__ defined
2924 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
2925 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2926 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2927 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
2928
Dan Albertb19953d2020-11-17 15:29:36 -08002929 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07002930 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2931 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002932 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07002933
2934 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
2935 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09002936
2937 // non-APEX variant does not have __ANDROID_APEX__ defined
2938 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2939 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2940
2941 // APEX variant has __ANDROID_APEX__ defined
2942 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002943 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07002944 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002945 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002946
Jooyung Hanc87a0592020-03-02 17:44:33 +09002947 // APEX variant has __ANDROID_APEX__ defined
2948 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002949 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002950 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07002951 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09002952
Dan Albertb19953d2020-11-17 15:29:36 -08002953 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07002954 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09002955 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002956 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002957}
Jiyong Park7e636d02019-01-28 16:16:54 +09002958
2959func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002960 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09002961 apex {
2962 name: "myapex",
2963 key: "myapex.key",
2964 native_shared_libs: ["mylib"],
2965 }
2966
2967 apex_key {
2968 name: "myapex.key",
2969 public_key: "testkey.avbpubkey",
2970 private_key: "testkey.pem",
2971 }
2972
2973 cc_library_headers {
2974 name: "mylib_headers",
2975 export_include_dirs: ["my_include"],
2976 system_shared_libs: [],
2977 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09002978 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09002979 }
2980
2981 cc_library {
2982 name: "mylib",
2983 srcs: ["mylib.cpp"],
2984 system_shared_libs: [],
2985 stl: "none",
2986 header_libs: ["mylib_headers"],
2987 export_header_lib_headers: ["mylib_headers"],
2988 stubs: {
2989 versions: ["1", "2", "3"],
2990 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002991 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09002992 }
2993
2994 cc_library {
2995 name: "otherlib",
2996 srcs: ["mylib.cpp"],
2997 system_shared_libs: [],
2998 stl: "none",
2999 shared_libs: ["mylib"],
3000 }
3001 `)
3002
Colin Cross7113d202019-11-20 16:39:12 -08003003 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003004
3005 // Ensure that the include path of the header lib is exported to 'otherlib'
3006 ensureContains(t, cFlags, "-Imy_include")
3007}
Alex Light9670d332019-01-29 18:07:33 -08003008
Jiyong Park7cd10e32020-01-14 09:22:18 +09003009type fileInApex struct {
3010 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003011 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003012 isLink bool
3013}
3014
Jooyung Hana57af4a2020-01-23 05:36:59 +00003015func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003016 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003017 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003018 copyCmds := apexRule.Args["copy_commands"]
3019 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003020 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003021 for _, cmd := range strings.Split(copyCmds, "&&") {
3022 cmd = strings.TrimSpace(cmd)
3023 if cmd == "" {
3024 continue
3025 }
3026 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003027 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003028 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003029 switch terms[0] {
3030 case "mkdir":
3031 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003032 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003033 t.Fatal("copyCmds contains invalid cp command", cmd)
3034 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003035 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003036 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003037 isLink = false
3038 case "ln":
3039 if len(terms) != 3 && len(terms) != 4 {
3040 // ln LINK TARGET or ln -s LINK TARGET
3041 t.Fatal("copyCmds contains invalid ln command", cmd)
3042 }
3043 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003044 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003045 isLink = true
3046 default:
3047 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3048 }
3049 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003050 index := strings.Index(dst, imageApexDir)
3051 if index == -1 {
3052 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3053 }
3054 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003055 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003056 }
3057 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003058 return ret
3059}
3060
Jooyung Hana57af4a2020-01-23 05:36:59 +00003061func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3062 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003063 var failed bool
3064 var surplus []string
3065 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003066 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003067 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003068 for _, expected := range files {
3069 if matched, _ := path.Match(expected, file.path); matched {
3070 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003071 mactchFound = true
3072 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003073 }
3074 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003075 if !mactchFound {
3076 surplus = append(surplus, file.path)
3077 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003078 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003079
Jooyung Han31c470b2019-10-18 16:26:59 +09003080 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003081 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003082 t.Log("surplus files", surplus)
3083 failed = true
3084 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003085
3086 if len(files) > len(filesMatched) {
3087 var missing []string
3088 for _, expected := range files {
3089 if !filesMatched[expected] {
3090 missing = append(missing, expected)
3091 }
3092 }
3093 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003094 t.Log("missing files", missing)
3095 failed = true
3096 }
3097 if failed {
3098 t.Fail()
3099 }
3100}
3101
Jooyung Han344d5432019-08-23 11:17:39 +09003102func TestVndkApexCurrent(t *testing.T) {
3103 ctx, _ := testApex(t, `
3104 apex_vndk {
3105 name: "myapex",
3106 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003107 }
3108
3109 apex_key {
3110 name: "myapex.key",
3111 public_key: "testkey.avbpubkey",
3112 private_key: "testkey.pem",
3113 }
3114
3115 cc_library {
3116 name: "libvndk",
3117 srcs: ["mylib.cpp"],
3118 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003119 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003120 vndk: {
3121 enabled: true,
3122 },
3123 system_shared_libs: [],
3124 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003125 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003126 }
3127
3128 cc_library {
3129 name: "libvndksp",
3130 srcs: ["mylib.cpp"],
3131 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003132 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003133 vndk: {
3134 enabled: true,
3135 support_system_process: true,
3136 },
3137 system_shared_libs: [],
3138 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003139 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003140 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003141 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003142
Jooyung Hana57af4a2020-01-23 05:36:59 +00003143 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003144 "lib/libvndk.so",
3145 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003146 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003147 "lib64/libvndk.so",
3148 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003149 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003150 "etc/llndk.libraries.VER.txt",
3151 "etc/vndkcore.libraries.VER.txt",
3152 "etc/vndksp.libraries.VER.txt",
3153 "etc/vndkprivate.libraries.VER.txt",
Justin Yun8a2600c2020-12-07 12:44:03 +09003154 "etc/vndkproduct.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003155 })
Jooyung Han344d5432019-08-23 11:17:39 +09003156}
3157
3158func TestVndkApexWithPrebuilt(t *testing.T) {
3159 ctx, _ := testApex(t, `
3160 apex_vndk {
3161 name: "myapex",
3162 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003163 }
3164
3165 apex_key {
3166 name: "myapex.key",
3167 public_key: "testkey.avbpubkey",
3168 private_key: "testkey.pem",
3169 }
3170
3171 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003172 name: "libvndk",
3173 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003174 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003175 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003176 vndk: {
3177 enabled: true,
3178 },
3179 system_shared_libs: [],
3180 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003181 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003182 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003183
3184 cc_prebuilt_library_shared {
3185 name: "libvndk.arm",
3186 srcs: ["libvndk.arm.so"],
3187 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003188 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003189 vndk: {
3190 enabled: true,
3191 },
3192 enabled: false,
3193 arch: {
3194 arm: {
3195 enabled: true,
3196 },
3197 },
3198 system_shared_libs: [],
3199 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003200 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003201 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003202 `+vndkLibrariesTxtFiles("current"),
3203 withFiles(map[string][]byte{
3204 "libvndk.so": nil,
3205 "libvndk.arm.so": nil,
3206 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003207
Jooyung Hana57af4a2020-01-23 05:36:59 +00003208 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003209 "lib/libvndk.so",
3210 "lib/libvndk.arm.so",
3211 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003212 "lib/libc++.so",
3213 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003214 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003215 })
Jooyung Han344d5432019-08-23 11:17:39 +09003216}
3217
Jooyung Han39edb6c2019-11-06 16:53:07 +09003218func vndkLibrariesTxtFiles(vers ...string) (result string) {
3219 for _, v := range vers {
3220 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003221 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003222 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003223 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003224 name: "` + txt + `.libraries.txt",
3225 }
3226 `
3227 }
3228 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003229 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003230 result += `
3231 prebuilt_etc {
3232 name: "` + txt + `.libraries.` + v + `.txt",
3233 src: "dummy.txt",
3234 }
3235 `
3236 }
3237 }
3238 }
3239 return
3240}
3241
Jooyung Han344d5432019-08-23 11:17:39 +09003242func TestVndkApexVersion(t *testing.T) {
3243 ctx, _ := testApex(t, `
3244 apex_vndk {
3245 name: "myapex_v27",
3246 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003247 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003248 vndk_version: "27",
3249 }
3250
3251 apex_key {
3252 name: "myapex.key",
3253 public_key: "testkey.avbpubkey",
3254 private_key: "testkey.pem",
3255 }
3256
Jooyung Han31c470b2019-10-18 16:26:59 +09003257 vndk_prebuilt_shared {
3258 name: "libvndk27",
3259 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003260 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003261 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003262 vndk: {
3263 enabled: true,
3264 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003265 target_arch: "arm64",
3266 arch: {
3267 arm: {
3268 srcs: ["libvndk27_arm.so"],
3269 },
3270 arm64: {
3271 srcs: ["libvndk27_arm64.so"],
3272 },
3273 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003274 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003275 }
3276
3277 vndk_prebuilt_shared {
3278 name: "libvndk27",
3279 version: "27",
3280 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003281 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003282 vndk: {
3283 enabled: true,
3284 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003285 target_arch: "x86_64",
3286 arch: {
3287 x86: {
3288 srcs: ["libvndk27_x86.so"],
3289 },
3290 x86_64: {
3291 srcs: ["libvndk27_x86_64.so"],
3292 },
3293 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003294 }
3295 `+vndkLibrariesTxtFiles("27"),
3296 withFiles(map[string][]byte{
3297 "libvndk27_arm.so": nil,
3298 "libvndk27_arm64.so": nil,
3299 "libvndk27_x86.so": nil,
3300 "libvndk27_x86_64.so": nil,
3301 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003302
Jooyung Hana57af4a2020-01-23 05:36:59 +00003303 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003304 "lib/libvndk27_arm.so",
3305 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003306 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003307 })
Jooyung Han344d5432019-08-23 11:17:39 +09003308}
3309
3310func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
3311 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
3312 apex_vndk {
3313 name: "myapex_v27",
3314 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003315 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003316 vndk_version: "27",
3317 }
3318 apex_vndk {
3319 name: "myapex_v27_other",
3320 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003321 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003322 vndk_version: "27",
3323 }
3324
3325 apex_key {
3326 name: "myapex.key",
3327 public_key: "testkey.avbpubkey",
3328 private_key: "testkey.pem",
3329 }
3330
3331 cc_library {
3332 name: "libvndk",
3333 srcs: ["mylib.cpp"],
3334 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003335 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003336 vndk: {
3337 enabled: true,
3338 },
3339 system_shared_libs: [],
3340 stl: "none",
3341 }
3342
3343 vndk_prebuilt_shared {
3344 name: "libvndk",
3345 version: "27",
3346 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003347 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003348 vndk: {
3349 enabled: true,
3350 },
3351 srcs: ["libvndk.so"],
3352 }
3353 `, withFiles(map[string][]byte{
3354 "libvndk.so": nil,
3355 }))
3356}
3357
Jooyung Han90eee022019-10-01 20:02:42 +09003358func TestVndkApexNameRule(t *testing.T) {
3359 ctx, _ := testApex(t, `
3360 apex_vndk {
3361 name: "myapex",
3362 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003363 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003364 }
3365 apex_vndk {
3366 name: "myapex_v28",
3367 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003368 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003369 vndk_version: "28",
3370 }
3371 apex_key {
3372 name: "myapex.key",
3373 public_key: "testkey.avbpubkey",
3374 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003375 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003376
3377 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003378 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003379 actual := proptools.String(bundle.properties.Apex_name)
3380 if !reflect.DeepEqual(actual, expected) {
3381 t.Errorf("Got '%v', expected '%v'", actual, expected)
3382 }
3383 }
3384
3385 assertApexName("com.android.vndk.vVER", "myapex")
3386 assertApexName("com.android.vndk.v28", "myapex_v28")
3387}
3388
Jooyung Han344d5432019-08-23 11:17:39 +09003389func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
3390 ctx, _ := testApex(t, `
3391 apex_vndk {
3392 name: "myapex",
3393 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003394 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003395 }
3396
3397 apex_key {
3398 name: "myapex.key",
3399 public_key: "testkey.avbpubkey",
3400 private_key: "testkey.pem",
3401 }
3402
3403 cc_library {
3404 name: "libvndk",
3405 srcs: ["mylib.cpp"],
3406 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003407 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003408 native_bridge_supported: true,
3409 host_supported: true,
3410 vndk: {
3411 enabled: true,
3412 },
3413 system_shared_libs: [],
3414 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003415 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003416 }
Jooyung Han35155c42020-02-06 17:33:20 +09003417 `+vndkLibrariesTxtFiles("current"), withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003418
Jooyung Hana57af4a2020-01-23 05:36:59 +00003419 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003420 "lib/libvndk.so",
3421 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003422 "lib/libc++.so",
3423 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003424 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003425 })
Jooyung Han344d5432019-08-23 11:17:39 +09003426}
3427
3428func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
3429 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
3430 apex_vndk {
3431 name: "myapex",
3432 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003433 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003434 native_bridge_supported: true,
3435 }
3436
3437 apex_key {
3438 name: "myapex.key",
3439 public_key: "testkey.avbpubkey",
3440 private_key: "testkey.pem",
3441 }
3442
3443 cc_library {
3444 name: "libvndk",
3445 srcs: ["mylib.cpp"],
3446 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003447 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003448 native_bridge_supported: true,
3449 host_supported: true,
3450 vndk: {
3451 enabled: true,
3452 },
3453 system_shared_libs: [],
3454 stl: "none",
3455 }
3456 `)
3457}
3458
Jooyung Han31c470b2019-10-18 16:26:59 +09003459func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003460 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003461 apex_vndk {
3462 name: "myapex_v27",
3463 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003464 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003465 vndk_version: "27",
3466 }
3467
3468 apex_key {
3469 name: "myapex.key",
3470 public_key: "testkey.avbpubkey",
3471 private_key: "testkey.pem",
3472 }
3473
3474 vndk_prebuilt_shared {
3475 name: "libvndk27",
3476 version: "27",
3477 target_arch: "arm",
3478 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003479 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003480 vndk: {
3481 enabled: true,
3482 },
3483 arch: {
3484 arm: {
3485 srcs: ["libvndk27.so"],
3486 }
3487 },
3488 }
3489
3490 vndk_prebuilt_shared {
3491 name: "libvndk27",
3492 version: "27",
3493 target_arch: "arm",
3494 binder32bit: true,
3495 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003496 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003497 vndk: {
3498 enabled: true,
3499 },
3500 arch: {
3501 arm: {
3502 srcs: ["libvndk27binder32.so"],
3503 }
3504 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003505 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003506 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003507 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003508 withFiles(map[string][]byte{
3509 "libvndk27.so": nil,
3510 "libvndk27binder32.so": nil,
3511 }),
3512 withBinder32bit,
3513 withTargets(map[android.OsType][]android.Target{
3514 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003515 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3516 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003517 },
3518 }),
3519 )
3520
Jooyung Hana57af4a2020-01-23 05:36:59 +00003521 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003522 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003523 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003524 })
3525}
3526
Jooyung Han45a96772020-06-15 14:59:42 +09003527func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
3528 ctx, _ := testApex(t, `
3529 apex_vndk {
3530 name: "myapex",
3531 key: "myapex.key",
3532 file_contexts: ":myapex-file_contexts",
3533 }
3534
3535 apex_key {
3536 name: "myapex.key",
3537 public_key: "testkey.avbpubkey",
3538 private_key: "testkey.pem",
3539 }
3540
3541 cc_library {
3542 name: "libz",
3543 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003544 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003545 vndk: {
3546 enabled: true,
3547 },
3548 stubs: {
3549 symbol_file: "libz.map.txt",
3550 versions: ["30"],
3551 }
3552 }
3553 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3554 "libz.map.txt": nil,
3555 }))
3556
3557 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_image").Rule("apexManifestRule")
3558 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3559 ensureListEmpty(t, provideNativeLibs)
3560}
3561
Jooyung Hane1633032019-08-01 17:41:43 +09003562func TestDependenciesInApexManifest(t *testing.T) {
3563 ctx, _ := testApex(t, `
3564 apex {
3565 name: "myapex_nodep",
3566 key: "myapex.key",
3567 native_shared_libs: ["lib_nodep"],
3568 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003569 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003570 }
3571
3572 apex {
3573 name: "myapex_dep",
3574 key: "myapex.key",
3575 native_shared_libs: ["lib_dep"],
3576 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003577 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003578 }
3579
3580 apex {
3581 name: "myapex_provider",
3582 key: "myapex.key",
3583 native_shared_libs: ["libfoo"],
3584 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003585 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003586 }
3587
3588 apex {
3589 name: "myapex_selfcontained",
3590 key: "myapex.key",
3591 native_shared_libs: ["lib_dep", "libfoo"],
3592 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003593 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003594 }
3595
3596 apex_key {
3597 name: "myapex.key",
3598 public_key: "testkey.avbpubkey",
3599 private_key: "testkey.pem",
3600 }
3601
3602 cc_library {
3603 name: "lib_nodep",
3604 srcs: ["mylib.cpp"],
3605 system_shared_libs: [],
3606 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003607 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003608 }
3609
3610 cc_library {
3611 name: "lib_dep",
3612 srcs: ["mylib.cpp"],
3613 shared_libs: ["libfoo"],
3614 system_shared_libs: [],
3615 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003616 apex_available: [
3617 "myapex_dep",
3618 "myapex_provider",
3619 "myapex_selfcontained",
3620 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003621 }
3622
3623 cc_library {
3624 name: "libfoo",
3625 srcs: ["mytest.cpp"],
3626 stubs: {
3627 versions: ["1"],
3628 },
3629 system_shared_libs: [],
3630 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003631 apex_available: [
3632 "myapex_provider",
3633 "myapex_selfcontained",
3634 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003635 }
3636 `)
3637
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003638 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003639 var provideNativeLibs, requireNativeLibs []string
3640
Sundong Ahnabb64432019-10-22 13:58:29 +09003641 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003642 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3643 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003644 ensureListEmpty(t, provideNativeLibs)
3645 ensureListEmpty(t, requireNativeLibs)
3646
Sundong Ahnabb64432019-10-22 13:58:29 +09003647 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003648 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3649 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003650 ensureListEmpty(t, provideNativeLibs)
3651 ensureListContains(t, requireNativeLibs, "libfoo.so")
3652
Sundong Ahnabb64432019-10-22 13:58:29 +09003653 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003654 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3655 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003656 ensureListContains(t, provideNativeLibs, "libfoo.so")
3657 ensureListEmpty(t, requireNativeLibs)
3658
Sundong Ahnabb64432019-10-22 13:58:29 +09003659 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003660 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3661 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003662 ensureListContains(t, provideNativeLibs, "libfoo.so")
3663 ensureListEmpty(t, requireNativeLibs)
3664}
3665
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003666func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09003667 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003668 apex {
3669 name: "myapex",
3670 key: "myapex.key",
3671 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003672 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003673 }
3674
3675 apex_key {
3676 name: "myapex.key",
3677 public_key: "testkey.avbpubkey",
3678 private_key: "testkey.pem",
3679 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003680
3681 cc_library {
3682 name: "mylib",
3683 srcs: ["mylib.cpp"],
3684 system_shared_libs: [],
3685 stl: "none",
3686 apex_available: [
3687 "//apex_available:platform",
3688 "myapex",
3689 ],
3690 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003691 `)
3692
Sundong Ahnabb64432019-10-22 13:58:29 +09003693 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003694 apexManifestRule := module.Rule("apexManifestRule")
3695 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3696 apexRule := module.Rule("apexRule")
3697 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003698
3699 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3700 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3701 name := apexBundle.BaseModuleName()
3702 prefix := "TARGET_"
3703 var builder strings.Builder
3704 data.Custom(&builder, name, prefix, "", data)
3705 androidMk := builder.String()
3706 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3707 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003708}
3709
Alex Light0851b882019-02-07 13:20:53 -08003710func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003711 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003712 apex {
3713 name: "myapex",
3714 key: "myapex.key",
3715 native_shared_libs: ["mylib_common"],
3716 }
3717
3718 apex_key {
3719 name: "myapex.key",
3720 public_key: "testkey.avbpubkey",
3721 private_key: "testkey.pem",
3722 }
3723
3724 cc_library {
3725 name: "mylib_common",
3726 srcs: ["mylib.cpp"],
3727 system_shared_libs: [],
3728 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003729 apex_available: [
3730 "//apex_available:platform",
3731 "myapex",
3732 ],
Alex Light0851b882019-02-07 13:20:53 -08003733 }
3734 `)
3735
Sundong Ahnabb64432019-10-22 13:58:29 +09003736 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003737 apexRule := module.Rule("apexRule")
3738 copyCmds := apexRule.Args["copy_commands"]
3739
3740 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3741 t.Log("Apex was a test apex!")
3742 t.Fail()
3743 }
3744 // Ensure that main rule creates an output
3745 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3746
3747 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003748 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003749
3750 // Ensure that both direct and indirect deps are copied into apex
3751 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3752
Colin Cross7113d202019-11-20 16:39:12 -08003753 // Ensure that the platform variant ends with _shared
3754 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003755
Colin Cross56a83212020-09-15 18:30:11 -07003756 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003757 t.Log("Found mylib_common not in any apex!")
3758 t.Fail()
3759 }
3760}
3761
3762func TestTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003763 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003764 apex_test {
3765 name: "myapex",
3766 key: "myapex.key",
3767 native_shared_libs: ["mylib_common_test"],
3768 }
3769
3770 apex_key {
3771 name: "myapex.key",
3772 public_key: "testkey.avbpubkey",
3773 private_key: "testkey.pem",
3774 }
3775
3776 cc_library {
3777 name: "mylib_common_test",
3778 srcs: ["mylib.cpp"],
3779 system_shared_libs: [],
3780 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003781 // TODO: remove //apex_available:platform
3782 apex_available: [
3783 "//apex_available:platform",
3784 "myapex",
3785 ],
Alex Light0851b882019-02-07 13:20:53 -08003786 }
3787 `)
3788
Sundong Ahnabb64432019-10-22 13:58:29 +09003789 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003790 apexRule := module.Rule("apexRule")
3791 copyCmds := apexRule.Args["copy_commands"]
3792
3793 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3794 t.Log("Apex was not a test apex!")
3795 t.Fail()
3796 }
3797 // Ensure that main rule creates an output
3798 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3799
3800 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003801 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003802
3803 // Ensure that both direct and indirect deps are copied into apex
3804 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3805
Colin Cross7113d202019-11-20 16:39:12 -08003806 // Ensure that the platform variant ends with _shared
3807 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003808}
3809
Alex Light9670d332019-01-29 18:07:33 -08003810func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003811 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003812 apex {
3813 name: "myapex",
3814 key: "myapex.key",
3815 multilib: {
3816 first: {
3817 native_shared_libs: ["mylib_common"],
3818 }
3819 },
3820 target: {
3821 android: {
3822 multilib: {
3823 first: {
3824 native_shared_libs: ["mylib"],
3825 }
3826 }
3827 },
3828 host: {
3829 multilib: {
3830 first: {
3831 native_shared_libs: ["mylib2"],
3832 }
3833 }
3834 }
3835 }
3836 }
3837
3838 apex_key {
3839 name: "myapex.key",
3840 public_key: "testkey.avbpubkey",
3841 private_key: "testkey.pem",
3842 }
3843
3844 cc_library {
3845 name: "mylib",
3846 srcs: ["mylib.cpp"],
3847 system_shared_libs: [],
3848 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003849 // TODO: remove //apex_available:platform
3850 apex_available: [
3851 "//apex_available:platform",
3852 "myapex",
3853 ],
Alex Light9670d332019-01-29 18:07:33 -08003854 }
3855
3856 cc_library {
3857 name: "mylib_common",
3858 srcs: ["mylib.cpp"],
3859 system_shared_libs: [],
3860 stl: "none",
3861 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003862 // TODO: remove //apex_available:platform
3863 apex_available: [
3864 "//apex_available:platform",
3865 "myapex",
3866 ],
Alex Light9670d332019-01-29 18:07:33 -08003867 }
3868
3869 cc_library {
3870 name: "mylib2",
3871 srcs: ["mylib.cpp"],
3872 system_shared_libs: [],
3873 stl: "none",
3874 compile_multilib: "first",
3875 }
3876 `)
3877
Sundong Ahnabb64432019-10-22 13:58:29 +09003878 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003879 copyCmds := apexRule.Args["copy_commands"]
3880
3881 // Ensure that main rule creates an output
3882 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3883
3884 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003885 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
3886 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
3887 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08003888
3889 // Ensure that both direct and indirect deps are copied into apex
3890 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3891 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3892 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3893
Colin Cross7113d202019-11-20 16:39:12 -08003894 // Ensure that the platform variant ends with _shared
3895 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
3896 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
3897 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08003898}
Jiyong Park04480cf2019-02-06 00:16:29 +09003899
Jiyong Park59140302020-12-14 18:44:04 +09003900func TestApexWithArch(t *testing.T) {
3901 ctx, _ := testApex(t, `
3902 apex {
3903 name: "myapex",
3904 key: "myapex.key",
3905 arch: {
3906 arm64: {
3907 native_shared_libs: ["mylib.arm64"],
3908 },
3909 x86_64: {
3910 native_shared_libs: ["mylib.x64"],
3911 },
3912 }
3913 }
3914
3915 apex_key {
3916 name: "myapex.key",
3917 public_key: "testkey.avbpubkey",
3918 private_key: "testkey.pem",
3919 }
3920
3921 cc_library {
3922 name: "mylib.arm64",
3923 srcs: ["mylib.cpp"],
3924 system_shared_libs: [],
3925 stl: "none",
3926 // TODO: remove //apex_available:platform
3927 apex_available: [
3928 "//apex_available:platform",
3929 "myapex",
3930 ],
3931 }
3932
3933 cc_library {
3934 name: "mylib.x64",
3935 srcs: ["mylib.cpp"],
3936 system_shared_libs: [],
3937 stl: "none",
3938 // TODO: remove //apex_available:platform
3939 apex_available: [
3940 "//apex_available:platform",
3941 "myapex",
3942 ],
3943 }
3944 `)
3945
3946 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
3947 copyCmds := apexRule.Args["copy_commands"]
3948
3949 // Ensure that apex variant is created for the direct dep
3950 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
3951 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
3952
3953 // Ensure that both direct and indirect deps are copied into apex
3954 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
3955 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
3956}
3957
Jiyong Park04480cf2019-02-06 00:16:29 +09003958func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003959 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09003960 apex {
3961 name: "myapex",
3962 key: "myapex.key",
3963 binaries: ["myscript"],
3964 }
3965
3966 apex_key {
3967 name: "myapex.key",
3968 public_key: "testkey.avbpubkey",
3969 private_key: "testkey.pem",
3970 }
3971
3972 sh_binary {
3973 name: "myscript",
3974 src: "mylib.cpp",
3975 filename: "myscript.sh",
3976 sub_dir: "script",
3977 }
3978 `)
3979
Sundong Ahnabb64432019-10-22 13:58:29 +09003980 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09003981 copyCmds := apexRule.Args["copy_commands"]
3982
3983 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
3984}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003985
Jooyung Han91df2082019-11-20 01:49:42 +09003986func TestApexInVariousPartition(t *testing.T) {
3987 testcases := []struct {
3988 propName, parition, flattenedPartition string
3989 }{
3990 {"", "system", "system_ext"},
3991 {"product_specific: true", "product", "product"},
3992 {"soc_specific: true", "vendor", "vendor"},
3993 {"proprietary: true", "vendor", "vendor"},
3994 {"vendor: true", "vendor", "vendor"},
3995 {"system_ext_specific: true", "system_ext", "system_ext"},
3996 }
3997 for _, tc := range testcases {
3998 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
3999 ctx, _ := testApex(t, `
4000 apex {
4001 name: "myapex",
4002 key: "myapex.key",
4003 `+tc.propName+`
4004 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004005
Jooyung Han91df2082019-11-20 01:49:42 +09004006 apex_key {
4007 name: "myapex.key",
4008 public_key: "testkey.avbpubkey",
4009 private_key: "testkey.pem",
4010 }
4011 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004012
Jooyung Han91df2082019-11-20 01:49:42 +09004013 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
4014 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
4015 actual := apex.installDir.String()
4016 if actual != expected {
4017 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4018 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004019
Jooyung Han91df2082019-11-20 01:49:42 +09004020 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
4021 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
4022 actual = flattened.installDir.String()
4023 if actual != expected {
4024 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4025 }
4026 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004027 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004028}
Jiyong Park67882562019-03-21 01:11:21 +09004029
Jooyung Han580eb4f2020-06-24 19:33:06 +09004030func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004031 ctx, _ := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004032 apex {
4033 name: "myapex",
4034 key: "myapex.key",
4035 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004036
Jooyung Han580eb4f2020-06-24 19:33:06 +09004037 apex_key {
4038 name: "myapex.key",
4039 public_key: "testkey.avbpubkey",
4040 private_key: "testkey.pem",
4041 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004042 `)
4043 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004044 rule := module.Output("file_contexts")
4045 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4046}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004047
Jooyung Han580eb4f2020-06-24 19:33:06 +09004048func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004049 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004050 apex {
4051 name: "myapex",
4052 key: "myapex.key",
4053 file_contexts: "my_own_file_contexts",
4054 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004055
Jooyung Han580eb4f2020-06-24 19:33:06 +09004056 apex_key {
4057 name: "myapex.key",
4058 public_key: "testkey.avbpubkey",
4059 private_key: "testkey.pem",
4060 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004061 `, withFiles(map[string][]byte{
4062 "my_own_file_contexts": nil,
4063 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004064}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004065
Jooyung Han580eb4f2020-06-24 19:33:06 +09004066func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004067 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004068 apex {
4069 name: "myapex",
4070 key: "myapex.key",
4071 product_specific: true,
4072 file_contexts: "product_specific_file_contexts",
4073 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004074
Jooyung Han580eb4f2020-06-24 19:33:06 +09004075 apex_key {
4076 name: "myapex.key",
4077 public_key: "testkey.avbpubkey",
4078 private_key: "testkey.pem",
4079 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004080 `)
4081
Jooyung Han580eb4f2020-06-24 19:33:06 +09004082 ctx, _ := testApex(t, `
4083 apex {
4084 name: "myapex",
4085 key: "myapex.key",
4086 product_specific: true,
4087 file_contexts: "product_specific_file_contexts",
4088 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004089
Jooyung Han580eb4f2020-06-24 19:33:06 +09004090 apex_key {
4091 name: "myapex.key",
4092 public_key: "testkey.avbpubkey",
4093 private_key: "testkey.pem",
4094 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004095 `, withFiles(map[string][]byte{
4096 "product_specific_file_contexts": nil,
4097 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004098 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4099 rule := module.Output("file_contexts")
4100 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4101}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004102
Jooyung Han580eb4f2020-06-24 19:33:06 +09004103func TestFileContexts_SetViaFileGroup(t *testing.T) {
4104 ctx, _ := testApex(t, `
4105 apex {
4106 name: "myapex",
4107 key: "myapex.key",
4108 product_specific: true,
4109 file_contexts: ":my-file-contexts",
4110 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004111
Jooyung Han580eb4f2020-06-24 19:33:06 +09004112 apex_key {
4113 name: "myapex.key",
4114 public_key: "testkey.avbpubkey",
4115 private_key: "testkey.pem",
4116 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004117
Jooyung Han580eb4f2020-06-24 19:33:06 +09004118 filegroup {
4119 name: "my-file-contexts",
4120 srcs: ["product_specific_file_contexts"],
4121 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004122 `, withFiles(map[string][]byte{
4123 "product_specific_file_contexts": nil,
4124 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004125 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4126 rule := module.Output("file_contexts")
4127 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004128}
4129
Jiyong Park67882562019-03-21 01:11:21 +09004130func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004131 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004132 apex_key {
4133 name: "myapex.key",
4134 public_key: ":my.avbpubkey",
4135 private_key: ":my.pem",
4136 product_specific: true,
4137 }
4138
4139 filegroup {
4140 name: "my.avbpubkey",
4141 srcs: ["testkey2.avbpubkey"],
4142 }
4143
4144 filegroup {
4145 name: "my.pem",
4146 srcs: ["testkey2.pem"],
4147 }
4148 `)
4149
4150 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4151 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004152 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004153 if actual_pubkey != expected_pubkey {
4154 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4155 }
4156 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004157 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004158 if actual_privkey != expected_privkey {
4159 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4160 }
4161}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004162
4163func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004164 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004165 prebuilt_apex {
4166 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004167 arch: {
4168 arm64: {
4169 src: "myapex-arm64.apex",
4170 },
4171 arm: {
4172 src: "myapex-arm.apex",
4173 },
4174 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004175 }
4176 `)
4177
4178 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4179
Jiyong Parkc95714e2019-03-29 14:23:10 +09004180 expectedInput := "myapex-arm64.apex"
4181 if prebuilt.inputApex.String() != expectedInput {
4182 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4183 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004184}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004185
4186func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004187 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004188 prebuilt_apex {
4189 name: "myapex",
4190 src: "myapex-arm.apex",
4191 filename: "notmyapex.apex",
4192 }
4193 `)
4194
4195 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4196
4197 expected := "notmyapex.apex"
4198 if p.installFilename != expected {
4199 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4200 }
4201}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004202
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004203func TestPrebuiltOverrides(t *testing.T) {
4204 ctx, config := testApex(t, `
4205 prebuilt_apex {
4206 name: "myapex.prebuilt",
4207 src: "myapex-arm.apex",
4208 overrides: [
4209 "myapex",
4210 ],
4211 }
4212 `)
4213
4214 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
4215
4216 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09004217 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004218 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004219 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004220 }
4221}
4222
Paul Duffin064b70c2020-11-02 17:32:38 +00004223func TestPrebuiltExportDexImplementationJars(t *testing.T) {
4224 transform := func(config *dexpreopt.GlobalConfig) {
4225 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
4226 }
4227
4228 checkDexJarBuildPath := func(ctx *android.TestContext, name string) {
4229 // Make sure the import has been given the correct path to the dex jar.
4230 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.Dependency)
4231 dexJarBuildPath := p.DexJarBuildPath()
4232 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
4233 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4234 }
4235 }
4236
4237 ensureNoSourceVariant := func(ctx *android.TestContext) {
4238 // Make sure that an apex variant is not created for the source module.
4239 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests("libfoo"); !reflect.DeepEqual(expected, actual) {
4240 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4241 }
4242 }
4243
4244 t.Run("prebuilt only", func(t *testing.T) {
4245 bp := `
4246 prebuilt_apex {
4247 name: "myapex",
4248 arch: {
4249 arm64: {
4250 src: "myapex-arm64.apex",
4251 },
4252 arm: {
4253 src: "myapex-arm.apex",
4254 },
4255 },
4256 exported_java_libs: ["libfoo"],
4257 }
4258
4259 java_import {
4260 name: "libfoo",
4261 jars: ["libfoo.jar"],
4262 }
4263 `
4264
4265 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4266 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4267
4268 checkDexJarBuildPath(ctx, "libfoo")
4269 })
4270
4271 t.Run("prebuilt with source preferred", func(t *testing.T) {
4272
4273 bp := `
4274 prebuilt_apex {
4275 name: "myapex",
4276 arch: {
4277 arm64: {
4278 src: "myapex-arm64.apex",
4279 },
4280 arm: {
4281 src: "myapex-arm.apex",
4282 },
4283 },
4284 exported_java_libs: ["libfoo"],
4285 }
4286
4287 java_import {
4288 name: "libfoo",
4289 jars: ["libfoo.jar"],
4290 }
4291
4292 java_library {
4293 name: "libfoo",
4294 }
4295 `
4296
4297 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4298 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4299
4300 checkDexJarBuildPath(ctx, "prebuilt_libfoo")
4301 ensureNoSourceVariant(ctx)
4302 })
4303
4304 t.Run("prebuilt preferred with source", func(t *testing.T) {
4305 bp := `
4306 prebuilt_apex {
4307 name: "myapex",
4308 prefer: true,
4309 arch: {
4310 arm64: {
4311 src: "myapex-arm64.apex",
4312 },
4313 arm: {
4314 src: "myapex-arm.apex",
4315 },
4316 },
4317 exported_java_libs: ["libfoo"],
4318 }
4319
4320 java_import {
4321 name: "libfoo",
4322 jars: ["libfoo.jar"],
4323 }
4324
4325 java_library {
4326 name: "libfoo",
4327 }
4328 `
4329
4330 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4331 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4332
4333 checkDexJarBuildPath(ctx, "prebuilt_libfoo")
4334 ensureNoSourceVariant(ctx)
4335 })
4336}
4337
Roland Levillain630846d2019-06-26 12:48:34 +01004338func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01004339 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004340 apex_test {
4341 name: "myapex",
4342 key: "myapex.key",
4343 tests: [
4344 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004345 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004346 ],
4347 }
4348
4349 apex_key {
4350 name: "myapex.key",
4351 public_key: "testkey.avbpubkey",
4352 private_key: "testkey.pem",
4353 }
4354
Liz Kammer1c14a212020-05-12 15:26:55 -07004355 filegroup {
4356 name: "fg",
4357 srcs: [
4358 "baz",
4359 "bar/baz"
4360 ],
4361 }
4362
Roland Levillain630846d2019-06-26 12:48:34 +01004363 cc_test {
4364 name: "mytest",
4365 gtest: false,
4366 srcs: ["mytest.cpp"],
4367 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004368 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004369 system_shared_libs: [],
4370 static_executable: true,
4371 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004372 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004373 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004374
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004375 cc_library {
4376 name: "mylib",
4377 srcs: ["mylib.cpp"],
4378 system_shared_libs: [],
4379 stl: "none",
4380 }
4381
Liz Kammer5bd365f2020-05-27 15:15:11 -07004382 filegroup {
4383 name: "fg2",
4384 srcs: [
4385 "testdata/baz"
4386 ],
4387 }
4388
Roland Levillain9b5fde92019-06-28 15:41:19 +01004389 cc_test {
4390 name: "mytests",
4391 gtest: false,
4392 srcs: [
4393 "mytest1.cpp",
4394 "mytest2.cpp",
4395 "mytest3.cpp",
4396 ],
4397 test_per_src: true,
4398 relative_install_path: "test",
4399 system_shared_libs: [],
4400 static_executable: true,
4401 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004402 data: [
4403 ":fg",
4404 ":fg2",
4405 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004406 }
Roland Levillain630846d2019-06-26 12:48:34 +01004407 `)
4408
Sundong Ahnabb64432019-10-22 13:58:29 +09004409 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004410 copyCmds := apexRule.Args["copy_commands"]
4411
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004412 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004413 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004414 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004415
Liz Kammer1c14a212020-05-12 15:26:55 -07004416 //Ensure that test data are copied into apex.
4417 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4418 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4419
Roland Levillain9b5fde92019-06-28 15:41:19 +01004420 // Ensure that test deps built with `test_per_src` are copied into apex.
4421 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4422 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4423 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004424
4425 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004426 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
4427 data := android.AndroidMkDataForTest(t, config, "", bundle)
4428 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004429 prefix := "TARGET_"
4430 var builder strings.Builder
4431 data.Custom(&builder, name, prefix, "", data)
4432 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004433 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4434 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4435 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4436 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004437 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004438 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004439 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004440
4441 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
4442 data = android.AndroidMkDataForTest(t, config, "", flatBundle)
4443 data.Custom(&builder, name, prefix, "", data)
4444 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004445 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4446 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004447}
4448
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004449func TestInstallExtraFlattenedApexes(t *testing.T) {
4450 ctx, config := testApex(t, `
4451 apex {
4452 name: "myapex",
4453 key: "myapex.key",
4454 }
4455 apex_key {
4456 name: "myapex.key",
4457 public_key: "testkey.avbpubkey",
4458 private_key: "testkey.pem",
4459 }
4460 `, func(fs map[string][]byte, config android.Config) {
4461 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4462 })
4463 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004464 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004465 mk := android.AndroidMkDataForTest(t, config, "", ab)
4466 var builder strings.Builder
4467 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4468 androidMk := builder.String()
4469 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4470}
4471
Jooyung Hand48f3c32019-08-23 11:18:57 +09004472func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4473 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4474 apex {
4475 name: "myapex",
4476 key: "myapex.key",
4477 native_shared_libs: ["libfoo"],
4478 }
4479
4480 apex_key {
4481 name: "myapex.key",
4482 public_key: "testkey.avbpubkey",
4483 private_key: "testkey.pem",
4484 }
4485
4486 cc_library {
4487 name: "libfoo",
4488 stl: "none",
4489 system_shared_libs: [],
4490 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004491 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004492 }
4493 `)
4494 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
4495 apex {
4496 name: "myapex",
4497 key: "myapex.key",
4498 java_libs: ["myjar"],
4499 }
4500
4501 apex_key {
4502 name: "myapex.key",
4503 public_key: "testkey.avbpubkey",
4504 private_key: "testkey.pem",
4505 }
4506
4507 java_library {
4508 name: "myjar",
4509 srcs: ["foo/bar/MyClass.java"],
4510 sdk_version: "none",
4511 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09004512 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004513 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004514 }
4515 `)
4516}
4517
Bill Peckhama41a6962021-01-11 10:58:54 -08004518func TestApexWithJavaImport(t *testing.T) {
4519 ctx, _ := testApex(t, `
4520 apex {
4521 name: "myapex",
4522 key: "myapex.key",
4523 java_libs: ["myjavaimport"],
4524 }
4525
4526 apex_key {
4527 name: "myapex.key",
4528 public_key: "testkey.avbpubkey",
4529 private_key: "testkey.pem",
4530 }
4531
4532 java_import {
4533 name: "myjavaimport",
4534 apex_available: ["myapex"],
4535 jars: ["my.jar"],
4536 compile_dex: true,
4537 }
4538 `)
4539
4540 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4541 apexRule := module.Rule("apexRule")
4542 copyCmds := apexRule.Args["copy_commands"]
4543 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
4544}
4545
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004546func TestApexWithApps(t *testing.T) {
4547 ctx, _ := testApex(t, `
4548 apex {
4549 name: "myapex",
4550 key: "myapex.key",
4551 apps: [
4552 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09004553 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004554 ],
4555 }
4556
4557 apex_key {
4558 name: "myapex.key",
4559 public_key: "testkey.avbpubkey",
4560 private_key: "testkey.pem",
4561 }
4562
4563 android_app {
4564 name: "AppFoo",
4565 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08004566 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004567 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09004568 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08004569 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004570 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004571 }
Jiyong Parkf7487312019-10-17 12:54:30 +09004572
4573 android_app {
4574 name: "AppFooPriv",
4575 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08004576 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09004577 system_modules: "none",
4578 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08004579 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004580 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09004581 }
Jiyong Park8be103b2019-11-08 15:53:48 +09004582
4583 cc_library_shared {
4584 name: "libjni",
4585 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004586 shared_libs: ["libfoo"],
4587 stl: "none",
4588 system_shared_libs: [],
4589 apex_available: [ "myapex" ],
4590 sdk_version: "current",
4591 }
4592
4593 cc_library_shared {
4594 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09004595 stl: "none",
4596 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09004597 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08004598 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09004599 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004600 `)
4601
Sundong Ahnabb64432019-10-22 13:58:29 +09004602 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004603 apexRule := module.Rule("apexRule")
4604 copyCmds := apexRule.Args["copy_commands"]
4605
4606 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09004607 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09004608
Colin Crossaede88c2020-08-11 12:17:01 -07004609 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004610 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09004611 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004612 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09004613 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004614 // JNI libraries including transitive deps are
4615 for _, jni := range []string{"libjni", "libfoo"} {
Colin Crossaede88c2020-08-11 12:17:01 -07004616 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004617 // ... embedded inside APK (jnilibs.zip)
4618 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
4619 // ... and not directly inside the APEX
4620 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
4621 }
Dario Frenicde2a032019-10-27 00:29:22 +01004622}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004623
Dario Frenicde2a032019-10-27 00:29:22 +01004624func TestApexWithAppImports(t *testing.T) {
4625 ctx, _ := testApex(t, `
4626 apex {
4627 name: "myapex",
4628 key: "myapex.key",
4629 apps: [
4630 "AppFooPrebuilt",
4631 "AppFooPrivPrebuilt",
4632 ],
4633 }
4634
4635 apex_key {
4636 name: "myapex.key",
4637 public_key: "testkey.avbpubkey",
4638 private_key: "testkey.pem",
4639 }
4640
4641 android_app_import {
4642 name: "AppFooPrebuilt",
4643 apk: "PrebuiltAppFoo.apk",
4644 presigned: true,
4645 dex_preopt: {
4646 enabled: false,
4647 },
Jiyong Park592a6a42020-04-21 22:34:28 +09004648 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01004649 }
4650
4651 android_app_import {
4652 name: "AppFooPrivPrebuilt",
4653 apk: "PrebuiltAppFooPriv.apk",
4654 privileged: true,
4655 presigned: true,
4656 dex_preopt: {
4657 enabled: false,
4658 },
Jooyung Han39ee1192020-03-23 20:21:11 +09004659 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09004660 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01004661 }
4662 `)
4663
Sundong Ahnabb64432019-10-22 13:58:29 +09004664 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01004665 apexRule := module.Rule("apexRule")
4666 copyCmds := apexRule.Args["copy_commands"]
4667
4668 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09004669 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
4670}
4671
4672func TestApexWithAppImportsPrefer(t *testing.T) {
4673 ctx, _ := testApex(t, `
4674 apex {
4675 name: "myapex",
4676 key: "myapex.key",
4677 apps: [
4678 "AppFoo",
4679 ],
4680 }
4681
4682 apex_key {
4683 name: "myapex.key",
4684 public_key: "testkey.avbpubkey",
4685 private_key: "testkey.pem",
4686 }
4687
4688 android_app {
4689 name: "AppFoo",
4690 srcs: ["foo/bar/MyClass.java"],
4691 sdk_version: "none",
4692 system_modules: "none",
4693 apex_available: [ "myapex" ],
4694 }
4695
4696 android_app_import {
4697 name: "AppFoo",
4698 apk: "AppFooPrebuilt.apk",
4699 filename: "AppFooPrebuilt.apk",
4700 presigned: true,
4701 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09004702 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09004703 }
4704 `, withFiles(map[string][]byte{
4705 "AppFooPrebuilt.apk": nil,
4706 }))
4707
4708 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4709 "app/AppFoo/AppFooPrebuilt.apk",
4710 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004711}
4712
Dario Freni6f3937c2019-12-20 22:58:03 +00004713func TestApexWithTestHelperApp(t *testing.T) {
4714 ctx, _ := testApex(t, `
4715 apex {
4716 name: "myapex",
4717 key: "myapex.key",
4718 apps: [
4719 "TesterHelpAppFoo",
4720 ],
4721 }
4722
4723 apex_key {
4724 name: "myapex.key",
4725 public_key: "testkey.avbpubkey",
4726 private_key: "testkey.pem",
4727 }
4728
4729 android_test_helper_app {
4730 name: "TesterHelpAppFoo",
4731 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004732 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00004733 }
4734
4735 `)
4736
4737 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4738 apexRule := module.Rule("apexRule")
4739 copyCmds := apexRule.Args["copy_commands"]
4740
4741 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
4742}
4743
Jooyung Han18020ea2019-11-13 10:50:48 +09004744func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
4745 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00004746 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09004747 apex {
4748 name: "myapex",
4749 key: "myapex.key",
4750 native_shared_libs: ["libfoo"],
4751 }
4752
4753 apex_key {
4754 name: "myapex.key",
4755 public_key: "testkey.avbpubkey",
4756 private_key: "testkey.pem",
4757 }
4758
4759 apex {
4760 name: "otherapex",
4761 key: "myapex.key",
4762 native_shared_libs: ["libfoo"],
4763 }
4764
4765 cc_defaults {
4766 name: "libfoo-defaults",
4767 apex_available: ["otherapex"],
4768 }
4769
4770 cc_library {
4771 name: "libfoo",
4772 defaults: ["libfoo-defaults"],
4773 stl: "none",
4774 system_shared_libs: [],
4775 }`)
4776}
4777
Paul Duffine52e66f2020-03-30 17:54:29 +01004778func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09004779 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00004780 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09004781 apex {
4782 name: "myapex",
4783 key: "myapex.key",
4784 native_shared_libs: ["libfoo"],
4785 }
4786
4787 apex_key {
4788 name: "myapex.key",
4789 public_key: "testkey.avbpubkey",
4790 private_key: "testkey.pem",
4791 }
4792
4793 apex {
4794 name: "otherapex",
4795 key: "otherapex.key",
4796 native_shared_libs: ["libfoo"],
4797 }
4798
4799 apex_key {
4800 name: "otherapex.key",
4801 public_key: "testkey.avbpubkey",
4802 private_key: "testkey.pem",
4803 }
4804
4805 cc_library {
4806 name: "libfoo",
4807 stl: "none",
4808 system_shared_libs: [],
4809 apex_available: ["otherapex"],
4810 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01004811}
Jiyong Park127b40b2019-09-30 16:04:35 +09004812
Paul Duffine52e66f2020-03-30 17:54:29 +01004813func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09004814 // libbbaz is an indirect dep
Steven Moreland6e36cd62020-10-22 01:08:35 +00004815 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'. Dependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07004816.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01004817.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07004818.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01004819.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07004820.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01004821.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09004822 apex {
4823 name: "myapex",
4824 key: "myapex.key",
4825 native_shared_libs: ["libfoo"],
4826 }
4827
4828 apex_key {
4829 name: "myapex.key",
4830 public_key: "testkey.avbpubkey",
4831 private_key: "testkey.pem",
4832 }
4833
Jiyong Park127b40b2019-09-30 16:04:35 +09004834 cc_library {
4835 name: "libfoo",
4836 stl: "none",
4837 shared_libs: ["libbar"],
4838 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09004839 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004840 }
4841
4842 cc_library {
4843 name: "libbar",
4844 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09004845 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004846 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09004847 apex_available: ["myapex"],
4848 }
4849
4850 cc_library {
4851 name: "libbaz",
4852 stl: "none",
4853 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09004854 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01004855}
Jiyong Park127b40b2019-09-30 16:04:35 +09004856
Paul Duffine52e66f2020-03-30 17:54:29 +01004857func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09004858 testApexError(t, "\"otherapex\" is not a valid module name", `
4859 apex {
4860 name: "myapex",
4861 key: "myapex.key",
4862 native_shared_libs: ["libfoo"],
4863 }
4864
4865 apex_key {
4866 name: "myapex.key",
4867 public_key: "testkey.avbpubkey",
4868 private_key: "testkey.pem",
4869 }
4870
4871 cc_library {
4872 name: "libfoo",
4873 stl: "none",
4874 system_shared_libs: [],
4875 apex_available: ["otherapex"],
4876 }`)
4877
Paul Duffine52e66f2020-03-30 17:54:29 +01004878 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09004879 apex {
4880 name: "myapex",
4881 key: "myapex.key",
4882 native_shared_libs: ["libfoo", "libbar"],
4883 }
4884
4885 apex_key {
4886 name: "myapex.key",
4887 public_key: "testkey.avbpubkey",
4888 private_key: "testkey.pem",
4889 }
4890
4891 cc_library {
4892 name: "libfoo",
4893 stl: "none",
4894 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09004895 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004896 apex_available: ["myapex"],
4897 }
4898
4899 cc_library {
4900 name: "libbar",
4901 stl: "none",
4902 system_shared_libs: [],
4903 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09004904 }
4905
4906 cc_library {
4907 name: "libbaz",
4908 stl: "none",
4909 system_shared_libs: [],
4910 stubs: {
4911 versions: ["10", "20", "30"],
4912 },
Jiyong Park127b40b2019-09-30 16:04:35 +09004913 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01004914}
Jiyong Park127b40b2019-09-30 16:04:35 +09004915
Jiyong Park89e850a2020-04-07 16:37:39 +09004916func TestApexAvailable_CheckForPlatform(t *testing.T) {
Paul Duffine52e66f2020-03-30 17:54:29 +01004917 ctx, _ := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09004918 apex {
4919 name: "myapex",
4920 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09004921 native_shared_libs: ["libbar", "libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004922 }
4923
4924 apex_key {
4925 name: "myapex.key",
4926 public_key: "testkey.avbpubkey",
4927 private_key: "testkey.pem",
4928 }
4929
4930 cc_library {
4931 name: "libfoo",
4932 stl: "none",
4933 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09004934 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004935 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09004936 }
4937
4938 cc_library {
4939 name: "libfoo2",
4940 stl: "none",
4941 system_shared_libs: [],
4942 shared_libs: ["libbaz"],
4943 apex_available: ["//apex_available:platform"],
4944 }
4945
4946 cc_library {
4947 name: "libbar",
4948 stl: "none",
4949 system_shared_libs: [],
4950 apex_available: ["myapex"],
4951 }
4952
4953 cc_library {
4954 name: "libbaz",
4955 stl: "none",
4956 system_shared_libs: [],
4957 apex_available: ["myapex"],
4958 stubs: {
4959 versions: ["1"],
4960 },
Jiyong Park127b40b2019-09-30 16:04:35 +09004961 }`)
4962
Jiyong Park89e850a2020-04-07 16:37:39 +09004963 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
4964 // because it depends on libbar which isn't available to platform
4965 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
4966 if libfoo.NotAvailableForPlatform() != true {
4967 t.Errorf("%q shouldn't be available to platform", libfoo.String())
4968 }
4969
4970 // libfoo2 however can be available to platform because it depends on libbaz which provides
4971 // stubs
4972 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
4973 if libfoo2.NotAvailableForPlatform() == true {
4974 t.Errorf("%q should be available to platform", libfoo2.String())
4975 }
Paul Duffine52e66f2020-03-30 17:54:29 +01004976}
Jiyong Parka90ca002019-10-07 15:47:24 +09004977
Paul Duffine52e66f2020-03-30 17:54:29 +01004978func TestApexAvailable_CreatedForApex(t *testing.T) {
Jiyong Park89e850a2020-04-07 16:37:39 +09004979 ctx, _ := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09004980 apex {
4981 name: "myapex",
4982 key: "myapex.key",
4983 native_shared_libs: ["libfoo"],
4984 }
4985
4986 apex_key {
4987 name: "myapex.key",
4988 public_key: "testkey.avbpubkey",
4989 private_key: "testkey.pem",
4990 }
4991
4992 cc_library {
4993 name: "libfoo",
4994 stl: "none",
4995 system_shared_libs: [],
4996 apex_available: ["myapex"],
4997 static: {
4998 apex_available: ["//apex_available:platform"],
4999 },
5000 }`)
5001
Jiyong Park89e850a2020-04-07 16:37:39 +09005002 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5003 if libfooShared.NotAvailableForPlatform() != true {
5004 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5005 }
5006 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5007 if libfooStatic.NotAvailableForPlatform() != false {
5008 t.Errorf("%q should be available to platform", libfooStatic.String())
5009 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005010}
5011
Jiyong Park5d790c32019-11-15 18:40:32 +09005012func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005013 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005014 apex {
5015 name: "myapex",
5016 key: "myapex.key",
5017 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005018 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09005019 }
5020
5021 override_apex {
5022 name: "override_myapex",
5023 base: "myapex",
5024 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005025 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005026 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005027 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09005028 }
5029
5030 apex_key {
5031 name: "myapex.key",
5032 public_key: "testkey.avbpubkey",
5033 private_key: "testkey.pem",
5034 }
5035
5036 android_app {
5037 name: "app",
5038 srcs: ["foo/bar/MyClass.java"],
5039 package_name: "foo",
5040 sdk_version: "none",
5041 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005042 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005043 }
5044
5045 override_android_app {
5046 name: "override_app",
5047 base: "app",
5048 package_name: "bar",
5049 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005050 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005051
Jiyong Park317645e2019-12-05 13:20:58 +09005052 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5053 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5054 if originalVariant.GetOverriddenBy() != "" {
5055 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5056 }
5057 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5058 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5059 }
5060
Jiyong Park5d790c32019-11-15 18:40:32 +09005061 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5062 apexRule := module.Rule("apexRule")
5063 copyCmds := apexRule.Args["copy_commands"]
5064
5065 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005066 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005067
5068 apexBundle := module.Module().(*apexBundle)
5069 name := apexBundle.Name()
5070 if name != "override_myapex" {
5071 t.Errorf("name should be \"override_myapex\", but was %q", name)
5072 }
5073
Baligh Uddin004d7172020-02-19 21:29:28 -08005074 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5075 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5076 }
5077
Jiyong Park20bacab2020-03-03 11:45:41 +09005078 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005079 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09005080
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005081 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
5082 var builder strings.Builder
5083 data.Custom(&builder, name, "TARGET_", "", data)
5084 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005085 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005086 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5087 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005088 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005089 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005090 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005091 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5092 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005093}
5094
Jooyung Han214bf372019-11-12 13:03:50 +09005095func TestLegacyAndroid10Support(t *testing.T) {
5096 ctx, _ := testApex(t, `
5097 apex {
5098 name: "myapex",
5099 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005100 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005101 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005102 }
5103
5104 apex_key {
5105 name: "myapex.key",
5106 public_key: "testkey.avbpubkey",
5107 private_key: "testkey.pem",
5108 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005109
5110 cc_library {
5111 name: "mylib",
5112 srcs: ["mylib.cpp"],
5113 stl: "libc++",
5114 system_shared_libs: [],
5115 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005116 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005117 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005118 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005119
5120 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5121 args := module.Rule("apexRule").Args
5122 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005123 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005124
5125 // The copies of the libraries in the apex should have one more dependency than
5126 // the ones outside the apex, namely the unwinder. Ideally we should check
5127 // the dependency names directly here but for some reason the names are blank in
5128 // this test.
5129 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005130 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005131 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5132 if len(apexImplicits) != len(nonApexImplicits)+1 {
5133 t.Errorf("%q missing unwinder dep", lib)
5134 }
5135 }
Jooyung Han214bf372019-11-12 13:03:50 +09005136}
5137
Paul Duffin9b879592020-05-26 13:21:35 +01005138var filesForSdkLibrary = map[string][]byte{
5139 "api/current.txt": nil,
5140 "api/removed.txt": nil,
5141 "api/system-current.txt": nil,
5142 "api/system-removed.txt": nil,
5143 "api/test-current.txt": nil,
5144 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005145
Anton Hanssondff2c782020-12-21 17:10:01 +00005146 "100/public/api/foo.txt": nil,
5147 "100/public/api/foo-removed.txt": nil,
5148 "100/system/api/foo.txt": nil,
5149 "100/system/api/foo-removed.txt": nil,
5150
Paul Duffineedc5d52020-06-12 17:46:39 +01005151 // For java_sdk_library_import
5152 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005153}
5154
Jooyung Han58f26ab2019-12-18 15:34:32 +09005155func TestJavaSDKLibrary(t *testing.T) {
5156 ctx, _ := testApex(t, `
5157 apex {
5158 name: "myapex",
5159 key: "myapex.key",
5160 java_libs: ["foo"],
5161 }
5162
5163 apex_key {
5164 name: "myapex.key",
5165 public_key: "testkey.avbpubkey",
5166 private_key: "testkey.pem",
5167 }
5168
5169 java_sdk_library {
5170 name: "foo",
5171 srcs: ["a.java"],
5172 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005173 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005174 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005175
5176 prebuilt_apis {
5177 name: "sdk",
5178 api_dirs: ["100"],
5179 }
Paul Duffin9b879592020-05-26 13:21:35 +01005180 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005181
5182 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005183 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005184 "javalib/foo.jar",
5185 "etc/permissions/foo.xml",
5186 })
5187 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005188 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5189 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005190}
5191
Paul Duffin9b879592020-05-26 13:21:35 +01005192func TestJavaSDKLibrary_WithinApex(t *testing.T) {
5193 ctx, _ := testApex(t, `
5194 apex {
5195 name: "myapex",
5196 key: "myapex.key",
5197 java_libs: ["foo", "bar"],
5198 }
5199
5200 apex_key {
5201 name: "myapex.key",
5202 public_key: "testkey.avbpubkey",
5203 private_key: "testkey.pem",
5204 }
5205
5206 java_sdk_library {
5207 name: "foo",
5208 srcs: ["a.java"],
5209 api_packages: ["foo"],
5210 apex_available: ["myapex"],
5211 sdk_version: "none",
5212 system_modules: "none",
5213 }
5214
5215 java_library {
5216 name: "bar",
5217 srcs: ["a.java"],
5218 libs: ["foo"],
5219 apex_available: ["myapex"],
5220 sdk_version: "none",
5221 system_modules: "none",
5222 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005223
5224 prebuilt_apis {
5225 name: "sdk",
5226 api_dirs: ["100"],
5227 }
Paul Duffin9b879592020-05-26 13:21:35 +01005228 `, withFiles(filesForSdkLibrary))
5229
5230 // java_sdk_library installs both impl jar and permission XML
5231 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5232 "javalib/bar.jar",
5233 "javalib/foo.jar",
5234 "etc/permissions/foo.xml",
5235 })
5236
5237 // The bar library should depend on the implementation jar.
5238 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5239 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5240 t.Errorf("expected %q, found %#q", expected, actual)
5241 }
5242}
5243
5244func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
5245 ctx, _ := testApex(t, `
5246 apex {
5247 name: "myapex",
5248 key: "myapex.key",
5249 java_libs: ["foo"],
5250 }
5251
5252 apex_key {
5253 name: "myapex.key",
5254 public_key: "testkey.avbpubkey",
5255 private_key: "testkey.pem",
5256 }
5257
5258 java_sdk_library {
5259 name: "foo",
5260 srcs: ["a.java"],
5261 api_packages: ["foo"],
5262 apex_available: ["myapex"],
5263 sdk_version: "none",
5264 system_modules: "none",
5265 }
5266
5267 java_library {
5268 name: "bar",
5269 srcs: ["a.java"],
5270 libs: ["foo"],
5271 sdk_version: "none",
5272 system_modules: "none",
5273 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005274
5275 prebuilt_apis {
5276 name: "sdk",
5277 api_dirs: ["100"],
5278 }
Paul Duffin9b879592020-05-26 13:21:35 +01005279 `, withFiles(filesForSdkLibrary))
5280
5281 // java_sdk_library installs both impl jar and permission XML
5282 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5283 "javalib/foo.jar",
5284 "etc/permissions/foo.xml",
5285 })
5286
5287 // The bar library should depend on the stubs jar.
5288 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
5289 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5290 t.Errorf("expected %q, found %#q", expected, actual)
5291 }
5292}
5293
Paul Duffineedc5d52020-06-12 17:46:39 +01005294func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Anton Hanssondff2c782020-12-21 17:10:01 +00005295 ctx, _ := testApex(t, `
5296 prebuilt_apis {
5297 name: "sdk",
5298 api_dirs: ["100"],
5299 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005300 withFiles(map[string][]byte{
5301 "apex/a.java": nil,
5302 "apex/apex_manifest.json": nil,
5303 "apex/Android.bp": []byte(`
5304 package {
5305 default_visibility: ["//visibility:private"],
5306 }
5307
5308 apex {
5309 name: "myapex",
5310 key: "myapex.key",
5311 java_libs: ["foo", "bar"],
5312 }
5313
5314 apex_key {
5315 name: "myapex.key",
5316 public_key: "testkey.avbpubkey",
5317 private_key: "testkey.pem",
5318 }
5319
5320 java_library {
5321 name: "bar",
5322 srcs: ["a.java"],
5323 libs: ["foo"],
5324 apex_available: ["myapex"],
5325 sdk_version: "none",
5326 system_modules: "none",
5327 }
5328`),
5329 "source/a.java": nil,
5330 "source/api/current.txt": nil,
5331 "source/api/removed.txt": nil,
5332 "source/Android.bp": []byte(`
5333 package {
5334 default_visibility: ["//visibility:private"],
5335 }
5336
5337 java_sdk_library {
5338 name: "foo",
5339 visibility: ["//apex"],
5340 srcs: ["a.java"],
5341 api_packages: ["foo"],
5342 apex_available: ["myapex"],
5343 sdk_version: "none",
5344 system_modules: "none",
5345 public: {
5346 enabled: true,
5347 },
5348 }
5349`),
5350 "prebuilt/a.jar": nil,
5351 "prebuilt/Android.bp": []byte(`
5352 package {
5353 default_visibility: ["//visibility:private"],
5354 }
5355
5356 java_sdk_library_import {
5357 name: "foo",
5358 visibility: ["//apex", "//source"],
5359 apex_available: ["myapex"],
5360 prefer: true,
5361 public: {
5362 jars: ["a.jar"],
5363 },
5364 }
5365`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005366 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005367 )
5368
5369 // java_sdk_library installs both impl jar and permission XML
5370 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5371 "javalib/bar.jar",
5372 "javalib/foo.jar",
5373 "etc/permissions/foo.xml",
5374 })
5375
5376 // The bar library should depend on the implementation jar.
5377 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5378 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5379 t.Errorf("expected %q, found %#q", expected, actual)
5380 }
5381}
5382
5383func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5384 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5385 apex {
5386 name: "myapex",
5387 key: "myapex.key",
5388 java_libs: ["foo"],
5389 }
5390
5391 apex_key {
5392 name: "myapex.key",
5393 public_key: "testkey.avbpubkey",
5394 private_key: "testkey.pem",
5395 }
5396
5397 java_sdk_library_import {
5398 name: "foo",
5399 apex_available: ["myapex"],
5400 prefer: true,
5401 public: {
5402 jars: ["a.jar"],
5403 },
5404 }
5405
5406 `, withFiles(filesForSdkLibrary))
5407}
5408
atrost6e126252020-01-27 17:01:16 +00005409func TestCompatConfig(t *testing.T) {
5410 ctx, _ := testApex(t, `
5411 apex {
5412 name: "myapex",
5413 key: "myapex.key",
5414 prebuilts: ["myjar-platform-compat-config"],
5415 java_libs: ["myjar"],
5416 }
5417
5418 apex_key {
5419 name: "myapex.key",
5420 public_key: "testkey.avbpubkey",
5421 private_key: "testkey.pem",
5422 }
5423
5424 platform_compat_config {
5425 name: "myjar-platform-compat-config",
5426 src: ":myjar",
5427 }
5428
5429 java_library {
5430 name: "myjar",
5431 srcs: ["foo/bar/MyClass.java"],
5432 sdk_version: "none",
5433 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005434 apex_available: [ "myapex" ],
5435 }
5436 `)
5437 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5438 "etc/compatconfig/myjar-platform-compat-config.xml",
5439 "javalib/myjar.jar",
5440 })
5441}
5442
Jiyong Park479321d2019-12-16 11:47:12 +09005443func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5444 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5445 apex {
5446 name: "myapex",
5447 key: "myapex.key",
5448 java_libs: ["myjar"],
5449 }
5450
5451 apex_key {
5452 name: "myapex.key",
5453 public_key: "testkey.avbpubkey",
5454 private_key: "testkey.pem",
5455 }
5456
5457 java_library {
5458 name: "myjar",
5459 srcs: ["foo/bar/MyClass.java"],
5460 sdk_version: "none",
5461 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09005462 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005463 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09005464 }
5465 `)
5466}
5467
Jiyong Park7afd1072019-12-30 16:56:33 +09005468func TestCarryRequiredModuleNames(t *testing.T) {
5469 ctx, config := testApex(t, `
5470 apex {
5471 name: "myapex",
5472 key: "myapex.key",
5473 native_shared_libs: ["mylib"],
5474 }
5475
5476 apex_key {
5477 name: "myapex.key",
5478 public_key: "testkey.avbpubkey",
5479 private_key: "testkey.pem",
5480 }
5481
5482 cc_library {
5483 name: "mylib",
5484 srcs: ["mylib.cpp"],
5485 system_shared_libs: [],
5486 stl: "none",
5487 required: ["a", "b"],
5488 host_required: ["c", "d"],
5489 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005490 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09005491 }
5492 `)
5493
5494 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
5495 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
5496 name := apexBundle.BaseModuleName()
5497 prefix := "TARGET_"
5498 var builder strings.Builder
5499 data.Custom(&builder, name, prefix, "", data)
5500 androidMk := builder.String()
5501 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
5502 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
5503 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
5504}
5505
Jiyong Park7cd10e32020-01-14 09:22:18 +09005506func TestSymlinksFromApexToSystem(t *testing.T) {
5507 bp := `
5508 apex {
5509 name: "myapex",
5510 key: "myapex.key",
5511 native_shared_libs: ["mylib"],
5512 java_libs: ["myjar"],
5513 }
5514
Jiyong Park9d677202020-02-19 16:29:35 +09005515 apex {
5516 name: "myapex.updatable",
5517 key: "myapex.key",
5518 native_shared_libs: ["mylib"],
5519 java_libs: ["myjar"],
5520 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09005521 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09005522 }
5523
Jiyong Park7cd10e32020-01-14 09:22:18 +09005524 apex_key {
5525 name: "myapex.key",
5526 public_key: "testkey.avbpubkey",
5527 private_key: "testkey.pem",
5528 }
5529
5530 cc_library {
5531 name: "mylib",
5532 srcs: ["mylib.cpp"],
5533 shared_libs: ["myotherlib"],
5534 system_shared_libs: [],
5535 stl: "none",
5536 apex_available: [
5537 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005538 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005539 "//apex_available:platform",
5540 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005541 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005542 }
5543
5544 cc_library {
5545 name: "myotherlib",
5546 srcs: ["mylib.cpp"],
5547 system_shared_libs: [],
5548 stl: "none",
5549 apex_available: [
5550 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005551 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005552 "//apex_available:platform",
5553 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005554 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005555 }
5556
5557 java_library {
5558 name: "myjar",
5559 srcs: ["foo/bar/MyClass.java"],
5560 sdk_version: "none",
5561 system_modules: "none",
5562 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09005563 apex_available: [
5564 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005565 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005566 "//apex_available:platform",
5567 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005568 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005569 }
5570
5571 java_library {
5572 name: "myotherjar",
5573 srcs: ["foo/bar/MyClass.java"],
5574 sdk_version: "none",
5575 system_modules: "none",
5576 apex_available: [
5577 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005578 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005579 "//apex_available:platform",
5580 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005581 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005582 }
5583 `
5584
5585 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
5586 for _, f := range files {
5587 if f.path == file {
5588 if f.isLink {
5589 t.Errorf("%q is not a real file", file)
5590 }
5591 return
5592 }
5593 }
5594 t.Errorf("%q is not found", file)
5595 }
5596
5597 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
5598 for _, f := range files {
5599 if f.path == file {
5600 if !f.isLink {
5601 t.Errorf("%q is not a symlink", file)
5602 }
5603 return
5604 }
5605 }
5606 t.Errorf("%q is not found", file)
5607 }
5608
Jiyong Park9d677202020-02-19 16:29:35 +09005609 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
5610 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09005611 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00005612 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09005613 ensureRealfileExists(t, files, "javalib/myjar.jar")
5614 ensureRealfileExists(t, files, "lib64/mylib.so")
5615 ensureRealfileExists(t, files, "lib64/myotherlib.so")
5616
Jiyong Park9d677202020-02-19 16:29:35 +09005617 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
5618 ensureRealfileExists(t, files, "javalib/myjar.jar")
5619 ensureRealfileExists(t, files, "lib64/mylib.so")
5620 ensureRealfileExists(t, files, "lib64/myotherlib.so")
5621
5622 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09005623 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00005624 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09005625 ensureRealfileExists(t, files, "javalib/myjar.jar")
5626 ensureRealfileExists(t, files, "lib64/mylib.so")
5627 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09005628
5629 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
5630 ensureRealfileExists(t, files, "javalib/myjar.jar")
5631 ensureRealfileExists(t, files, "lib64/mylib.so")
5632 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09005633}
5634
Yo Chiange8128052020-07-23 20:09:18 +08005635func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
5636 ctx, config := testApex(t, `
5637 apex {
5638 name: "myapex",
5639 key: "myapex.key",
5640 native_shared_libs: ["mylib"],
5641 }
5642
5643 apex_key {
5644 name: "myapex.key",
5645 public_key: "testkey.avbpubkey",
5646 private_key: "testkey.pem",
5647 }
5648
5649 cc_library_shared {
5650 name: "mylib",
5651 srcs: ["mylib.cpp"],
5652 shared_libs: ["myotherlib"],
5653 system_shared_libs: [],
5654 stl: "none",
5655 apex_available: [
5656 "myapex",
5657 "//apex_available:platform",
5658 ],
5659 }
5660
5661 cc_prebuilt_library_shared {
5662 name: "myotherlib",
5663 srcs: ["prebuilt.so"],
5664 system_shared_libs: [],
5665 stl: "none",
5666 apex_available: [
5667 "myapex",
5668 "//apex_available:platform",
5669 ],
5670 }
5671 `)
5672
5673 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
5674 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
5675 var builder strings.Builder
5676 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
5677 androidMk := builder.String()
5678 // `myotherlib` is added to `myapex` as symlink
5679 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
5680 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
5681 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
5682 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09005683 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 +08005684}
5685
Jooyung Han643adc42020-02-27 13:50:06 +09005686func TestApexWithJniLibs(t *testing.T) {
5687 ctx, _ := testApex(t, `
5688 apex {
5689 name: "myapex",
5690 key: "myapex.key",
5691 jni_libs: ["mylib"],
5692 }
5693
5694 apex_key {
5695 name: "myapex.key",
5696 public_key: "testkey.avbpubkey",
5697 private_key: "testkey.pem",
5698 }
5699
5700 cc_library {
5701 name: "mylib",
5702 srcs: ["mylib.cpp"],
5703 shared_libs: ["mylib2"],
5704 system_shared_libs: [],
5705 stl: "none",
5706 apex_available: [ "myapex" ],
5707 }
5708
5709 cc_library {
5710 name: "mylib2",
5711 srcs: ["mylib.cpp"],
5712 system_shared_libs: [],
5713 stl: "none",
5714 apex_available: [ "myapex" ],
5715 }
5716 `)
5717
5718 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
5719 // Notice mylib2.so (transitive dep) is not added as a jni_lib
5720 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
5721 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5722 "lib64/mylib.so",
5723 "lib64/mylib2.so",
5724 })
5725}
5726
Jooyung Han49f67012020-04-17 13:43:10 +09005727func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
5728 ctx, _ := testApex(t, `
5729 apex {
5730 name: "myapex",
5731 key: "myapex.key",
5732 }
5733 apex_key {
5734 name: "myapex.key",
5735 public_key: "testkey.avbpubkey",
5736 private_key: "testkey.pem",
5737 }
5738 `, func(fs map[string][]byte, config android.Config) {
5739 delete(config.Targets, android.Android)
5740 config.AndroidCommonTarget = android.Target{}
5741 })
5742
5743 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
5744 t.Errorf("Expected variants: %v, but got: %v", expected, got)
5745 }
5746}
5747
Jiyong Parkbd159612020-02-28 15:22:21 +09005748func TestAppBundle(t *testing.T) {
5749 ctx, _ := testApex(t, `
5750 apex {
5751 name: "myapex",
5752 key: "myapex.key",
5753 apps: ["AppFoo"],
5754 }
5755
5756 apex_key {
5757 name: "myapex.key",
5758 public_key: "testkey.avbpubkey",
5759 private_key: "testkey.pem",
5760 }
5761
5762 android_app {
5763 name: "AppFoo",
5764 srcs: ["foo/bar/MyClass.java"],
5765 sdk_version: "none",
5766 system_modules: "none",
5767 apex_available: [ "myapex" ],
5768 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09005769 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09005770
Colin Crosscf371cc2020-11-13 11:48:42 -08005771 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09005772 content := bundleConfigRule.Args["content"]
5773
5774 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09005775 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 +09005776}
5777
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005778func TestAppSetBundle(t *testing.T) {
5779 ctx, _ := testApex(t, `
5780 apex {
5781 name: "myapex",
5782 key: "myapex.key",
5783 apps: ["AppSet"],
5784 }
5785
5786 apex_key {
5787 name: "myapex.key",
5788 public_key: "testkey.avbpubkey",
5789 private_key: "testkey.pem",
5790 }
5791
5792 android_app_set {
5793 name: "AppSet",
5794 set: "AppSet.apks",
5795 }`)
5796 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08005797 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005798 content := bundleConfigRule.Args["content"]
5799 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
5800 s := mod.Rule("apexRule").Args["copy_commands"]
5801 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
5802 if len(copyCmds) != 3 {
5803 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
5804 }
5805 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
5806 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
5807 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
5808}
5809
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07005810func TestAppSetBundlePrebuilt(t *testing.T) {
5811 ctx, _ := testApex(t, "", func(fs map[string][]byte, config android.Config) {
5812 bp := `
5813 apex_set {
5814 name: "myapex",
5815 filename: "foo_v2.apex",
5816 sanitized: {
5817 none: { set: "myapex.apks", },
5818 hwaddress: { set: "myapex.hwasan.apks", },
5819 },
5820 }`
5821 fs["Android.bp"] = []byte(bp)
5822
5823 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
5824 })
5825
5826 m := ctx.ModuleForTests("myapex", "android_common")
5827 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
5828
5829 actual := extractedApex.Inputs
5830 if len(actual) != 1 {
5831 t.Errorf("expected a single input")
5832 }
5833
5834 expected := "myapex.hwasan.apks"
5835 if actual[0].String() != expected {
5836 t.Errorf("expected %s, got %s", expected, actual[0].String())
5837 }
5838}
5839
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005840func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005841 t.Helper()
5842
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005843 bp := `
5844 java_library {
5845 name: "some-updatable-apex-lib",
5846 srcs: ["a.java"],
5847 sdk_version: "current",
5848 apex_available: [
5849 "some-updatable-apex",
5850 ],
5851 }
5852
5853 java_library {
5854 name: "some-non-updatable-apex-lib",
5855 srcs: ["a.java"],
5856 apex_available: [
5857 "some-non-updatable-apex",
5858 ],
5859 }
5860
5861 java_library {
5862 name: "some-platform-lib",
5863 srcs: ["a.java"],
5864 sdk_version: "current",
5865 installable: true,
5866 }
5867
5868 java_library {
5869 name: "some-art-lib",
5870 srcs: ["a.java"],
5871 sdk_version: "current",
5872 apex_available: [
5873 "com.android.art.something",
5874 ],
5875 hostdex: true,
5876 }
5877
5878 apex {
5879 name: "some-updatable-apex",
5880 key: "some-updatable-apex.key",
5881 java_libs: ["some-updatable-apex-lib"],
5882 updatable: true,
5883 min_sdk_version: "current",
5884 }
5885
5886 apex {
5887 name: "some-non-updatable-apex",
5888 key: "some-non-updatable-apex.key",
5889 java_libs: ["some-non-updatable-apex-lib"],
5890 }
5891
5892 apex_key {
5893 name: "some-updatable-apex.key",
5894 }
5895
5896 apex_key {
5897 name: "some-non-updatable-apex.key",
5898 }
5899
5900 apex {
5901 name: "com.android.art.something",
5902 key: "com.android.art.something.key",
5903 java_libs: ["some-art-lib"],
5904 updatable: true,
5905 min_sdk_version: "current",
5906 }
5907
5908 apex_key {
5909 name: "com.android.art.something.key",
5910 }
5911
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005912 filegroup {
5913 name: "some-updatable-apex-file_contexts",
5914 srcs: [
5915 "system/sepolicy/apex/some-updatable-apex-file_contexts",
5916 ],
5917 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01005918
5919 filegroup {
5920 name: "some-non-updatable-apex-file_contexts",
5921 srcs: [
5922 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
5923 ],
5924 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005925 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00005926
5927 testDexpreoptWithApexes(t, bp, errmsg, transformDexpreoptConfig)
5928}
5929
Paul Duffin064b70c2020-11-02 17:32:38 +00005930func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00005931 t.Helper()
5932
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005933 bp += cc.GatherRequiredDepsForTest(android.Android)
5934 bp += java.GatherRequiredDepsForTest()
5935 bp += dexpreopt.BpToolModulesForTest()
5936
5937 fs := map[string][]byte{
5938 "a.java": nil,
5939 "a.jar": nil,
5940 "build/make/target/product/security": nil,
5941 "apex_manifest.json": nil,
5942 "AndroidManifest.xml": nil,
5943 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01005944 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005945 "system/sepolicy/apex/com.android.art.something-file_contexts": nil,
5946 "framework/aidl/a.aidl": nil,
5947 }
5948 cc.GatherRequiredFilesForTest(fs)
5949
Colin Crossae8600b2020-10-29 17:09:13 -07005950 config := android.TestArchConfig(buildDir, nil, bp, fs)
5951
5952 ctx := android.NewTestArchContext(config)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005953 ctx.RegisterModuleType("apex", BundleFactory)
5954 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
Paul Duffin064b70c2020-11-02 17:32:38 +00005955 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005956 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinc988c8e2020-04-29 18:27:14 +01005957 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin021f4e52020-07-30 16:04:17 +01005958 android.RegisterPrebuiltMutators(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005959 cc.RegisterRequiredBuildComponentsForTest(ctx)
5960 java.RegisterJavaBuildComponents(ctx)
5961 java.RegisterSystemModulesBuildComponents(ctx)
5962 java.RegisterAppBuildComponents(ctx)
5963 java.RegisterDexpreoptBootJarsComponents(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005964 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
5965 ctx.PreDepsMutators(RegisterPreDepsMutators)
5966 ctx.PostDepsMutators(RegisterPostDepsMutators)
5967
Colin Crossae8600b2020-10-29 17:09:13 -07005968 ctx.Register()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005969
5970 _ = dexpreopt.GlobalSoongConfigForTests(config)
5971 dexpreopt.RegisterToolModulesForTest(ctx)
5972 pathCtx := android.PathContextForTesting(config)
5973 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
5974 transformDexpreoptConfig(dexpreoptConfig)
5975 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
5976
5977 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
5978 android.FailIfErrored(t, errs)
5979
5980 _, errs = ctx.PrepareBuildActions(config)
5981 if errmsg == "" {
5982 android.FailIfErrored(t, errs)
5983 } else if len(errs) > 0 {
5984 android.FailIfNoMatchingErrors(t, errmsg, errs)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005985 } else {
5986 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
5987 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005988
5989 return ctx
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005990}
5991
Jooyung Han548640b2020-04-27 12:10:30 +09005992func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
5993 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
5994 apex {
5995 name: "myapex",
5996 key: "myapex.key",
5997 updatable: true,
5998 }
5999
6000 apex_key {
6001 name: "myapex.key",
6002 public_key: "testkey.avbpubkey",
6003 private_key: "testkey.pem",
6004 }
6005 `)
6006}
6007
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006008func TestNoUpdatableJarsInBootImage(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006009 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006010 var transform func(*dexpreopt.GlobalConfig)
6011
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006012 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
6013 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006014 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"com.android.art.something:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006015 }
6016 testNoUpdatableJarsInBootImage(t, "", transform)
6017 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006018
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006019 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006020 err = `module "some-art-lib" from updatable apexes \["com.android.art.something"\] is not allowed in the framework boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006021 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006022 config.BootJars = android.CreateTestConfiguredJarList([]string{"com.android.art.something:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006023 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006024 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006025 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006026
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006027 t.Run("updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006028 err = `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006029 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006030 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006031 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006032 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006033 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006034
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006035 t.Run("non-updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006036 err = `module "some-non-updatable-apex-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006037 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006038 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006039 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006040 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006041 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006042
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006043 t.Run("updatable jar from some other apex in the framework boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006044 err = `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the framework boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006045 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006046 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006047 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006048 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006049 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006050
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006051 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
6052 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006053 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006054 }
6055 testNoUpdatableJarsInBootImage(t, "", transform)
6056 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006057
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006058 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006059 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006060 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006061 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006062 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006063 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006064 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006065
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006066 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006067 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006068 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006069 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006070 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006071 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006072 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006073
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006074 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006075 err = `module "some-platform-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006076 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006077 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006078 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006079 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006080 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006081
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006082 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
6083 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006084 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006085 }
6086 testNoUpdatableJarsInBootImage(t, "", transform)
6087 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006088
6089}
6090
6091func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
6092 transform := func(config *dexpreopt.GlobalConfig) {
6093 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
6094 }
6095 t.Run("prebuilt no source", func(t *testing.T) {
6096 testDexpreoptWithApexes(t, `
6097 prebuilt_apex {
6098 name: "myapex" ,
6099 arch: {
6100 arm64: {
6101 src: "myapex-arm64.apex",
6102 },
6103 arm: {
6104 src: "myapex-arm.apex",
6105 },
6106 },
6107 exported_java_libs: ["libfoo"],
6108 }
6109
6110 java_import {
6111 name: "libfoo",
6112 jars: ["libfoo.jar"],
6113 }
6114`, "", transform)
6115 })
6116
6117 t.Run("prebuilt no source", func(t *testing.T) {
6118 testDexpreoptWithApexes(t, `
6119 prebuilt_apex {
6120 name: "myapex" ,
6121 arch: {
6122 arm64: {
6123 src: "myapex-arm64.apex",
6124 },
6125 arm: {
6126 src: "myapex-arm.apex",
6127 },
6128 },
6129 exported_java_libs: ["libfoo"],
6130 }
6131
6132 java_import {
6133 name: "libfoo",
6134 jars: ["libfoo.jar"],
6135 }
6136`, "", transform)
6137 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006138}
6139
Andrei Onea115e7e72020-06-05 21:14:03 +01006140func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6141 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006142 bp += `
6143 apex_key {
6144 name: "myapex.key",
6145 public_key: "testkey.avbpubkey",
6146 private_key: "testkey.pem",
6147 }`
6148 fs := map[string][]byte{
6149 "lib1/src/A.java": nil,
6150 "lib2/src/B.java": nil,
6151 "system/sepolicy/apex/myapex-file_contexts": nil,
6152 }
6153
Colin Crossae8600b2020-10-29 17:09:13 -07006154 config := android.TestArchConfig(buildDir, nil, bp, fs)
6155 android.SetTestNeverallowRules(config, rules)
6156 updatableBootJars := make([]string, 0, len(apexBootJars))
6157 for _, apexBootJar := range apexBootJars {
6158 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6159 }
6160 config.TestProductVariables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6161
6162 ctx := android.NewTestArchContext(config)
Andrei Onea115e7e72020-06-05 21:14:03 +01006163 ctx.RegisterModuleType("apex", BundleFactory)
6164 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
6165 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
6166 cc.RegisterRequiredBuildComponentsForTest(ctx)
6167 java.RegisterJavaBuildComponents(ctx)
6168 java.RegisterSystemModulesBuildComponents(ctx)
6169 java.RegisterDexpreoptBootJarsComponents(ctx)
6170 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6171 ctx.PreDepsMutators(RegisterPreDepsMutators)
6172 ctx.PostDepsMutators(RegisterPostDepsMutators)
6173 ctx.PostDepsMutators(android.RegisterNeverallowMutator)
6174
Colin Crossae8600b2020-10-29 17:09:13 -07006175 ctx.Register()
Andrei Onea115e7e72020-06-05 21:14:03 +01006176
6177 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6178 android.FailIfErrored(t, errs)
6179
6180 _, errs = ctx.PrepareBuildActions(config)
6181 if errmsg == "" {
6182 android.FailIfErrored(t, errs)
6183 } else if len(errs) > 0 {
6184 android.FailIfNoMatchingErrors(t, errmsg, errs)
6185 return
6186 } else {
6187 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6188 }
6189}
6190
6191func TestApexPermittedPackagesRules(t *testing.T) {
6192 testcases := []struct {
6193 name string
6194 expectedError string
6195 bp string
6196 bootJars []string
6197 modulesPackages map[string][]string
6198 }{
6199
6200 {
6201 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6202 expectedError: "",
6203 bp: `
6204 java_library {
6205 name: "bcp_lib1",
6206 srcs: ["lib1/src/*.java"],
6207 permitted_packages: ["foo.bar"],
6208 apex_available: ["myapex"],
6209 sdk_version: "none",
6210 system_modules: "none",
6211 }
6212 java_library {
6213 name: "nonbcp_lib2",
6214 srcs: ["lib2/src/*.java"],
6215 apex_available: ["myapex"],
6216 permitted_packages: ["a.b"],
6217 sdk_version: "none",
6218 system_modules: "none",
6219 }
6220 apex {
6221 name: "myapex",
6222 key: "myapex.key",
6223 java_libs: ["bcp_lib1", "nonbcp_lib2"],
6224 }`,
6225 bootJars: []string{"bcp_lib1"},
6226 modulesPackages: map[string][]string{
6227 "myapex": []string{
6228 "foo.bar",
6229 },
6230 },
6231 },
6232 {
6233 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6234 expectedError: `module "bcp_lib2" .* which is restricted because jars that are part of the myapex module may only allow these packages: foo.bar. Please jarjar or move code around.`,
6235 bp: `
6236 java_library {
6237 name: "bcp_lib1",
6238 srcs: ["lib1/src/*.java"],
6239 apex_available: ["myapex"],
6240 permitted_packages: ["foo.bar"],
6241 sdk_version: "none",
6242 system_modules: "none",
6243 }
6244 java_library {
6245 name: "bcp_lib2",
6246 srcs: ["lib2/src/*.java"],
6247 apex_available: ["myapex"],
6248 permitted_packages: ["foo.bar", "bar.baz"],
6249 sdk_version: "none",
6250 system_modules: "none",
6251 }
6252 apex {
6253 name: "myapex",
6254 key: "myapex.key",
6255 java_libs: ["bcp_lib1", "bcp_lib2"],
6256 }
6257 `,
6258 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6259 modulesPackages: map[string][]string{
6260 "myapex": []string{
6261 "foo.bar",
6262 },
6263 },
6264 },
6265 }
6266 for _, tc := range testcases {
6267 t.Run(tc.name, func(t *testing.T) {
6268 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6269 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6270 })
6271 }
6272}
6273
Jiyong Park62304bb2020-04-13 16:19:48 +09006274func TestTestFor(t *testing.T) {
6275 ctx, _ := testApex(t, `
6276 apex {
6277 name: "myapex",
6278 key: "myapex.key",
6279 native_shared_libs: ["mylib", "myprivlib"],
6280 }
6281
6282 apex_key {
6283 name: "myapex.key",
6284 public_key: "testkey.avbpubkey",
6285 private_key: "testkey.pem",
6286 }
6287
6288 cc_library {
6289 name: "mylib",
6290 srcs: ["mylib.cpp"],
6291 system_shared_libs: [],
6292 stl: "none",
6293 stubs: {
6294 versions: ["1"],
6295 },
6296 apex_available: ["myapex"],
6297 }
6298
6299 cc_library {
6300 name: "myprivlib",
6301 srcs: ["mylib.cpp"],
6302 system_shared_libs: [],
6303 stl: "none",
6304 apex_available: ["myapex"],
6305 }
6306
6307
6308 cc_test {
6309 name: "mytest",
6310 gtest: false,
6311 srcs: ["mylib.cpp"],
6312 system_shared_libs: [],
6313 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006314 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006315 test_for: ["myapex"]
6316 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006317
6318 cc_library {
6319 name: "mytestlib",
6320 srcs: ["mylib.cpp"],
6321 system_shared_libs: [],
6322 shared_libs: ["mylib", "myprivlib"],
6323 stl: "none",
6324 test_for: ["myapex"],
6325 }
6326
6327 cc_benchmark {
6328 name: "mybench",
6329 srcs: ["mylib.cpp"],
6330 system_shared_libs: [],
6331 shared_libs: ["mylib", "myprivlib"],
6332 stl: "none",
6333 test_for: ["myapex"],
6334 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006335 `)
6336
6337 // the test 'mytest' is a test for the apex, therefore is linked to the
6338 // actual implementation of mylib instead of its stub.
6339 ldFlags := ctx.ModuleForTests("mytest", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6340 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6341 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park46a512f2020-12-04 18:02:13 +09006342
6343 // The same should be true for cc_library
6344 ldFlags = ctx.ModuleForTests("mytestlib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
6345 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6346 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
6347
6348 // ... and for cc_benchmark
6349 ldFlags = ctx.ModuleForTests("mybench", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6350 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6351 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park62304bb2020-04-13 16:19:48 +09006352}
6353
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006354// TODO(jungjw): Move this to proptools
6355func intPtr(i int) *int {
6356 return &i
6357}
6358
6359func TestApexSet(t *testing.T) {
6360 ctx, config := testApex(t, `
6361 apex_set {
6362 name: "myapex",
6363 set: "myapex.apks",
6364 filename: "foo_v2.apex",
6365 overrides: ["foo"],
6366 }
6367 `, func(fs map[string][]byte, config android.Config) {
6368 config.TestProductVariables.Platform_sdk_version = intPtr(30)
Jaewoong Jung829b7132020-06-10 12:23:32 -07006369 config.Targets[android.Android] = []android.Target{
6370 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
6371 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
6372 }
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006373 })
6374
6375 m := ctx.ModuleForTests("myapex", "android_common")
6376
6377 // Check extract_apks tool parameters.
6378 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6379 actual := extractedApex.Args["abis"]
6380 expected := "ARMEABI_V7A,ARM64_V8A"
6381 if actual != expected {
6382 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6383 }
6384 actual = extractedApex.Args["sdk-version"]
6385 expected = "30"
6386 if actual != expected {
6387 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6388 }
6389
6390 a := m.Module().(*ApexSet)
6391 expectedOverrides := []string{"foo"}
6392 actualOverrides := android.AndroidMkEntriesForTest(t, config, "", a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
6393 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
6394 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
6395 }
6396}
6397
Jiyong Park7d95a512020-05-10 15:16:24 +09006398func TestNoStaticLinkingToStubsLib(t *testing.T) {
6399 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
6400 apex {
6401 name: "myapex",
6402 key: "myapex.key",
6403 native_shared_libs: ["mylib"],
6404 }
6405
6406 apex_key {
6407 name: "myapex.key",
6408 public_key: "testkey.avbpubkey",
6409 private_key: "testkey.pem",
6410 }
6411
6412 cc_library {
6413 name: "mylib",
6414 srcs: ["mylib.cpp"],
6415 static_libs: ["otherlib"],
6416 system_shared_libs: [],
6417 stl: "none",
6418 apex_available: [ "myapex" ],
6419 }
6420
6421 cc_library {
6422 name: "otherlib",
6423 srcs: ["mylib.cpp"],
6424 system_shared_libs: [],
6425 stl: "none",
6426 stubs: {
6427 versions: ["1", "2", "3"],
6428 },
6429 apex_available: [ "myapex" ],
6430 }
6431 `)
6432}
6433
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006434func TestApexKeysTxt(t *testing.T) {
6435 ctx, _ := testApex(t, `
6436 apex {
6437 name: "myapex",
6438 key: "myapex.key",
6439 }
6440
6441 apex_key {
6442 name: "myapex.key",
6443 public_key: "testkey.avbpubkey",
6444 private_key: "testkey.pem",
6445 }
6446
6447 prebuilt_apex {
6448 name: "myapex",
6449 prefer: true,
6450 arch: {
6451 arm64: {
6452 src: "myapex-arm64.apex",
6453 },
6454 arm: {
6455 src: "myapex-arm.apex",
6456 },
6457 },
6458 }
6459
6460 apex_set {
6461 name: "myapex_set",
6462 set: "myapex.apks",
6463 filename: "myapex_set.apex",
6464 overrides: ["myapex"],
6465 }
6466 `)
6467
6468 apexKeysText := ctx.SingletonForTests("apex_keys_text")
6469 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
6470 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 +09006471 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 +09006472}
6473
Jooyung Han938b5932020-06-20 12:47:47 +09006474func TestAllowedFiles(t *testing.T) {
6475 ctx, _ := testApex(t, `
6476 apex {
6477 name: "myapex",
6478 key: "myapex.key",
6479 apps: ["app"],
6480 allowed_files: "allowed.txt",
6481 }
6482
6483 apex_key {
6484 name: "myapex.key",
6485 public_key: "testkey.avbpubkey",
6486 private_key: "testkey.pem",
6487 }
6488
6489 android_app {
6490 name: "app",
6491 srcs: ["foo/bar/MyClass.java"],
6492 package_name: "foo",
6493 sdk_version: "none",
6494 system_modules: "none",
6495 apex_available: [ "myapex" ],
6496 }
6497 `, withFiles(map[string][]byte{
6498 "sub/Android.bp": []byte(`
6499 override_apex {
6500 name: "override_myapex",
6501 base: "myapex",
6502 apps: ["override_app"],
6503 allowed_files: ":allowed",
6504 }
6505 // Overridable "path" property should be referenced indirectly
6506 filegroup {
6507 name: "allowed",
6508 srcs: ["allowed.txt"],
6509 }
6510 override_android_app {
6511 name: "override_app",
6512 base: "app",
6513 package_name: "bar",
6514 }
6515 `),
6516 }))
6517
6518 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
6519 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
6520 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
6521 }
6522
6523 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
6524 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
6525 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
6526 }
6527}
6528
Martin Stjernholm58c33f02020-07-06 22:56:01 +01006529func TestNonPreferredPrebuiltDependency(t *testing.T) {
6530 _, _ = testApex(t, `
6531 apex {
6532 name: "myapex",
6533 key: "myapex.key",
6534 native_shared_libs: ["mylib"],
6535 }
6536
6537 apex_key {
6538 name: "myapex.key",
6539 public_key: "testkey.avbpubkey",
6540 private_key: "testkey.pem",
6541 }
6542
6543 cc_library {
6544 name: "mylib",
6545 srcs: ["mylib.cpp"],
6546 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07006547 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01006548 },
6549 apex_available: ["myapex"],
6550 }
6551
6552 cc_prebuilt_library_shared {
6553 name: "mylib",
6554 prefer: false,
6555 srcs: ["prebuilt.so"],
6556 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07006557 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01006558 },
6559 apex_available: ["myapex"],
6560 }
6561 `)
6562}
6563
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00006564func TestCompressedApex(t *testing.T) {
6565 ctx, config := testApex(t, `
6566 apex {
6567 name: "myapex",
6568 key: "myapex.key",
6569 compressible: true,
6570 }
6571 apex_key {
6572 name: "myapex.key",
6573 public_key: "testkey.avbpubkey",
6574 private_key: "testkey.pem",
6575 }
6576 `, func(fs map[string][]byte, config android.Config) {
6577 config.TestProductVariables.CompressedApex = proptools.BoolPtr(true)
6578 })
6579
6580 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
6581 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
6582
6583 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
6584 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
6585
6586 // Make sure output of bundle is .capex
6587 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
6588 ensureContains(t, ab.outputFile.String(), "myapex.capex")
6589
6590 // Verify android.mk rules
6591 data := android.AndroidMkDataForTest(t, config, "", ab)
6592 var builder strings.Builder
6593 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
6594 androidMk := builder.String()
6595 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
6596}
6597
Martin Stjernholm2856c662020-12-02 15:03:42 +00006598func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
6599 ctx, config := testApex(t, `
6600 apex {
6601 name: "myapex",
6602 key: "myapex.key",
6603 native_shared_libs: ["mylib"],
6604 }
6605
6606 apex_key {
6607 name: "myapex.key",
6608 public_key: "testkey.avbpubkey",
6609 private_key: "testkey.pem",
6610 }
6611
6612 cc_library {
6613 name: "mylib",
6614 srcs: ["mylib.cpp"],
6615 apex_available: ["myapex"],
6616 shared_libs: ["otherlib"],
6617 system_shared_libs: [],
6618 }
6619
6620 cc_library {
6621 name: "otherlib",
6622 srcs: ["mylib.cpp"],
6623 stubs: {
6624 versions: ["current"],
6625 },
6626 }
6627
6628 cc_prebuilt_library_shared {
6629 name: "otherlib",
6630 prefer: true,
6631 srcs: ["prebuilt.so"],
6632 stubs: {
6633 versions: ["current"],
6634 },
6635 }
6636 `)
6637
6638 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
6639 data := android.AndroidMkDataForTest(t, config, "", ab)
6640 var builder strings.Builder
6641 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
6642 androidMk := builder.String()
6643
6644 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
6645 // a thing there.
6646 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
6647}
6648
Jiyong Parke3867542020-12-03 17:28:25 +09006649func TestExcludeDependency(t *testing.T) {
6650 ctx, _ := testApex(t, `
6651 apex {
6652 name: "myapex",
6653 key: "myapex.key",
6654 native_shared_libs: ["mylib"],
6655 }
6656
6657 apex_key {
6658 name: "myapex.key",
6659 public_key: "testkey.avbpubkey",
6660 private_key: "testkey.pem",
6661 }
6662
6663 cc_library {
6664 name: "mylib",
6665 srcs: ["mylib.cpp"],
6666 system_shared_libs: [],
6667 stl: "none",
6668 apex_available: ["myapex"],
6669 shared_libs: ["mylib2"],
6670 target: {
6671 apex: {
6672 exclude_shared_libs: ["mylib2"],
6673 },
6674 },
6675 }
6676
6677 cc_library {
6678 name: "mylib2",
6679 srcs: ["mylib.cpp"],
6680 system_shared_libs: [],
6681 stl: "none",
6682 }
6683 `)
6684
6685 // Check if mylib is linked to mylib2 for the non-apex target
6686 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
6687 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
6688
6689 // Make sure that the link doesn't occur for the apex target
6690 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
6691 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
6692
6693 // It shouldn't appear in the copy cmd as well.
6694 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
6695 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
6696}
6697
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09006698func TestPrebuiltStubLibDep(t *testing.T) {
6699 bpBase := `
6700 apex {
6701 name: "myapex",
6702 key: "myapex.key",
6703 native_shared_libs: ["mylib"],
6704 }
6705 apex_key {
6706 name: "myapex.key",
6707 public_key: "testkey.avbpubkey",
6708 private_key: "testkey.pem",
6709 }
6710 cc_library {
6711 name: "mylib",
6712 srcs: ["mylib.cpp"],
6713 apex_available: ["myapex"],
6714 shared_libs: ["stublib"],
6715 system_shared_libs: [],
6716 }
6717 apex {
6718 name: "otherapex",
6719 enabled: %s,
6720 key: "myapex.key",
6721 native_shared_libs: ["stublib"],
6722 }
6723 `
6724
6725 stublibSourceBp := `
6726 cc_library {
6727 name: "stublib",
6728 srcs: ["mylib.cpp"],
6729 apex_available: ["otherapex"],
6730 system_shared_libs: [],
6731 stl: "none",
6732 stubs: {
6733 versions: ["1"],
6734 },
6735 }
6736 `
6737
6738 stublibPrebuiltBp := `
6739 cc_prebuilt_library_shared {
6740 name: "stublib",
6741 srcs: ["prebuilt.so"],
6742 apex_available: ["otherapex"],
6743 stubs: {
6744 versions: ["1"],
6745 },
6746 %s
6747 }
6748 `
6749
6750 tests := []struct {
6751 name string
6752 stublibBp string
6753 usePrebuilt bool
6754 modNames []string // Modules to collect AndroidMkEntries for
6755 otherApexEnabled []string
6756 }{
6757 {
6758 name: "only_source",
6759 stublibBp: stublibSourceBp,
6760 usePrebuilt: false,
6761 modNames: []string{"stublib"},
6762 otherApexEnabled: []string{"true", "false"},
6763 },
6764 {
6765 name: "source_preferred",
6766 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
6767 usePrebuilt: false,
6768 modNames: []string{"stublib", "prebuilt_stublib"},
6769 otherApexEnabled: []string{"true", "false"},
6770 },
6771 {
6772 name: "prebuilt_preferred",
6773 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
6774 usePrebuilt: true,
6775 modNames: []string{"stublib", "prebuilt_stublib"},
6776 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
6777 },
6778 {
6779 name: "only_prebuilt",
6780 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
6781 usePrebuilt: true,
6782 modNames: []string{"stublib"},
6783 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
6784 },
6785 }
6786
6787 for _, test := range tests {
6788 t.Run(test.name, func(t *testing.T) {
6789 for _, otherApexEnabled := range test.otherApexEnabled {
6790 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
6791 ctx, config := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
6792
6793 type modAndMkEntries struct {
6794 mod *cc.Module
6795 mkEntries android.AndroidMkEntries
6796 }
6797 entries := []*modAndMkEntries{}
6798
6799 // Gather shared lib modules that are installable
6800 for _, modName := range test.modNames {
6801 for _, variant := range ctx.ModuleVariantsForTests(modName) {
6802 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
6803 continue
6804 }
6805 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08006806 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09006807 continue
6808 }
6809 for _, ent := range android.AndroidMkEntriesForTest(t, config, "", mod) {
6810 if ent.Disabled {
6811 continue
6812 }
6813 entries = append(entries, &modAndMkEntries{
6814 mod: mod,
6815 mkEntries: ent,
6816 })
6817 }
6818 }
6819 }
6820
6821 var entry *modAndMkEntries = nil
6822 for _, ent := range entries {
6823 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
6824 if entry != nil {
6825 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
6826 } else {
6827 entry = ent
6828 }
6829 }
6830 }
6831
6832 if entry == nil {
6833 t.Errorf("AndroidMk entry for \"stublib\" missing")
6834 } else {
6835 isPrebuilt := entry.mod.Prebuilt() != nil
6836 if isPrebuilt != test.usePrebuilt {
6837 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
6838 }
6839 if !entry.mod.IsStubs() {
6840 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
6841 }
6842 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
6843 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
6844 }
Jiyong Park892a98f2020-12-14 09:20:00 +09006845 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
6846 expected := "-D__STUBLIB_API__=1"
6847 if !android.InList(expected, cflags) {
6848 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
6849 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09006850 }
6851 })
6852 }
6853 })
6854 }
6855}
6856
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07006857func TestMain(m *testing.M) {
6858 run := func() int {
6859 setUp()
6860 defer tearDown()
6861
6862 return m.Run()
6863 }
6864
6865 os.Exit(run())
6866}