Quellcode-Bibliothek apex_test.go
Sprache: unbekannt
|
|
Spracherkennung für: .go vermutete Sprache: Unknown {[0] [0] [0]} [Methode: Schwerpunktbildung, einfache Gewichte, sechs Dimensionen]
// Copyright 2018 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2. 0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2. 0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package apex
import (
"fmt"
"path"
"path/filepath"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"testing"
"android/soong/aconfig/codegen"
"android/soong/provenance"
"github.com/google/blueprint/proptools"
"android/soong/android"
"android/soong/bpf"
"android/soong/cc"
"android/soong/dexpreopt"
prebuilt_etc "android/soong/etc"
"android/soong/filesystem"
"android/soong/java"
"android/soong/kernel"
"android/soong/rust"
"android/soong/sh"
)
// names returns name list from white space separated string
func names(s string) (ns []string) {
for _, n := range strings.Split(s, " ") {
if len(n) > 0 {
ns = append(ns, n)
}
}
return
}
func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePrepar er) {
t.Helper()
android.GroupFixturePreparers(
prepareForApexTest,
android.GroupFixturePreparers(preparers...),
).
ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
RunTestWithBp(t, bp)
}
func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
t.Helper()
optionalBpPreparer := android.NullFixturePreparer
if bp != "" {
optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
}
result := android.GroupFixturePreparers(
prepareForApexTest,
android.GroupFixturePreparers(preparers...),
optionalBpPreparer,
).RunTest(t)
return result.TestContext
}
func withFiles(files android.MockFS) android.FixturePreparer {
return files.AddToFixture()
}
func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
variables.ManifestPackageNameOverrides = specs
})
}
func withApexGlobalMinSdkVersionOverride(minSdkOverride *string) android.FixturePreparer {
return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
variables.ApexGlobalMinSdkVersionOverride = minSdkOverride
})
}
var withBinder32bit = android.FixtureModifyProductVariables(
func(variables android.FixtureProductVariables) {
variables.Binder32bit = proptools.BoolPtr(true)
},
)
var withUnbundledBuild = android.FixtureModifyProductVariables(
func(variables android.FixtureProductVariables) {
variables.Unbundled_build = proptools.BoolPtr(true)
},
)
// Legacy preparer used for running tests within the apex package.
//
// This includes everything that was needed to run any test in the apex package prior to the
// introduction of the test fixtures. Tests that are being converted to use fixtures directly
// rather than through the testApex...() methods should avoid using this and instead use the
// various preparers directly, using android.GroupFixturePreparers(...) to group them when
// necessary.
//
// deprecated
var prepareForApexTest = android.GroupFixturePreparers(
// General preparers in alphabetical order as test infrastructure will enforce correct
// registration order.
android.PrepareForTestWithAndroidBuildComponents,
android.PrepareForTestWithHostTools("kernel_modules_builder", "zipsync", "soong_zip", "merge_zips", "depmod"),
bpf.PrepareForTestWithBpf,
cc.PrepareForTestWithCcBuildComponents,
java.PrepareForTestWithDexpreopt,
prebuilt_etc.PrepareForTestWithPrebuiltEtc,
rust.PrepareForTestWithRustDefaultModules,
sh.PrepareForTestWithShBuildComponents,
codegen.PrepareForTestWithAconfigBuildComponents,
kernel.PrepareForTestWithPrebuiltKernelModules,
PrepareForTestWithApexBuildComponents,
// Additional apex test specific preparers.
android.FixtureAddTextFile("system/sepolicy/Android.bp", `
filegroup {
name: "myapex-file_contexts",
srcs: [
"apex/myapex-file_contexts",
],
}
`),
prepareForTestWithMyapex,
android.FixtureMergeMockFs(android.MockFS{
"a.java": nil,
"PrebuiltAppFoo.apk": nil,
"PrebuiltAppFooPriv.apk": nil,
"apex_manifest.json": nil,
"AndroidManifest.xml": nil,
"system/sepolicy/apex/myapex.updatable-file_contexts": nil,
"system/sepolicy/apex/myapex2-file_contexts": nil,
"system/sepolicy/apex/otherapex-file_contexts": nil,
"system/sepolicy/apex/com.android.vndk-file_contexts": nil,
"system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
"mylib.cpp": nil,
"mytest.cpp": nil,
"mytest1.cpp": nil,
"mytest2.cpp": nil,
"mytest3.cpp": nil,
"myprebuilt": nil,
"my_include": nil,
"foo/bar/MyClass.java": nil,
"prebuilt.jar": nil,
"prebuilt.so": nil,
"vendor/foo/devkeys/test.x509.pem": nil,
"vendor/foo/devkeys/test.pk8": nil,
"testkey.x509.pem": nil,
"testkey.pk8": nil,
"testkey.override.x509.pem": nil,
"testkey.override.pk8": nil,
"vendor/foo/devkeys/testkey.avbpubkey": nil,
"vendor/foo/devkeys/testkey.pem": nil,
"NOTICE": nil,
"custom_notice": nil,
"custom_notice_for_static_lib": nil,
"testkey2.avbpubkey": nil,
"testkey2.pem": nil,
"myapex-arm64.apex": nil,
"myapex-arm.apex": nil,
"myapex.apks": nil,
"frameworks/base/api/current.txt": nil,
"framework/aidl/a.aidl": nil,
"dummy.txt": nil,
"baz": nil,
"bar/baz": nil,
"testdata/baz": nil,
"AppSet.apks": nil,
"foo.rs": nil,
"libfoo.jar": nil,
"libbar.jar": nil,
},
),
android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
variables.Platform_sdk_codename = proptools.StringPtr("Q")
variables.Platform_sdk_final = proptools.BoolPtr(false)
// "Tiramisu" needs to be in the next line for compatibility with soong code,
// not because of these tests specifically (it's not used by the tests)
variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
variables.BuildId = proptools.StringPtr("TEST.BUILD_ID")
}),
)
var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
"system/sepolicy/apex/myapex-file_contexts": nil,
})
var prepareForTestWithOtherapex = android.FixtureMergeMockFs(android.MockFS{
"system/sepolicy/apex/otherapex-file_contexts": nil,
})
// ensure that 'result' equals 'expected'
func ensureEquals(t *testing.T, result string, expected string) {
t.Helper()
if result != expected {
t.Errorf("%q != %q", expected, result)
}
}
// ensure that 'result' contains 'expected'
func ensureContains(t *testing.T, result string, expected string) {
t.Helper()
if !strings.Contains(result, expected) {
t.Errorf("%q is not found in %q", expected, result)
}
}
// ensure that 'result' contains 'expected' exactly one time
func ensureContainsOnce(t *testing.T, result string, expected string) {
t.Helper()
count := strings.Count(result, expected)
if count != 1 {
t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
}
}
// ensures that 'result' does not contain 'notExpected'
func ensureNotContains(t *testing.T, result string, notExpected string) {
t.Helper()
if strings.Contains(result, notExpected) {
t.Errorf("%q is found in %q", notExpected, result)
}
}
func ensureMatches(t *testing.T, result string, expectedRex string) {
t.Helper()
ok, err := regexp.MatchString(expectedRex, result)
if err != nil {
t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
return
}
if !ok {
t.Errorf("%s does not match regular expession %s", result, expectedRex)
}
}
func ensureListContainsMatch(t *testing.T, result []string, expectedRex string) {
t.Helper()
p := regexp.MustCompile(expectedRex)
if android.IndexListPred(func(s string) bool { return p.MatchString(s) }, result) == -1 {
t.Errorf("%q is not found in %v", expectedRex, result)
}
}
func ensureListContains(t *testing.T, result []string, expected string) {
t.Helper()
if !android.InList(expected, result) {
t.Errorf("%q is not found in %v", expected, result)
}
}
func ensureListNotContains(t *testing.T, result []string, notExpected string) {
t.Helper()
if android.InList(notExpected, result) {
t.Errorf("%q is found in %v", notExpected, result)
}
}
func ensureListEmpty(t *testing.T, result []string) {
t.Helper()
if len(result) > 0 {
t.Errorf("%q is expected to be empty", result)
}
}
func ensureListNotEmpty(t *testing.T, result []string) {
t.Helper()
if len(result) == 0 {
t.Errorf("%q is expected to be not empty", result)
}
}
// Minimal test
func TestBasicApex(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex_defaults {
name: "myapex-defaults",
manifest: ":myapex.manifest",
androidManifest: ":myapex.androidmanifest",
key: "myapex.key",
binaries: ["foo.rust"],
native_shared_libs: [
"mylib",
"libfoo.ffi",
],
rust_dyn_libs: ["libfoo.dylib.rust"],
multilib: {
both: {
binaries: ["foo"],
}
},
java_libs: [
"myjar",
"myjar_dex",
],
updatable: false,
}
apex {
name: "myapex",
defaults: ["myapex-defaults"],
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
filegroup {
name: "myapex.manifest",
srcs: ["apex_manifest.json"],
}
filegroup {
name: "myapex.androidmanifest",
srcs: ["AndroidManifest.xml"],
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
shared_libs: [
"mylib2",
"libbar.ffi",
],
system_shared_libs: [],
stl: "none",
// TODO: remove //apex_available:platform
apex_available: [
"//apex_available:platform",
"myapex",
],
}
cc_binary {
name: "foo",
srcs: ["mylib.cpp"],
compile_multilib: "both",
multilib: {
lib32: {
suffix: "32",
},
lib64: {
suffix: "64",
},
},
symlinks: ["foo_link_"],
symlink_preferred_arch: true,
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
}
rust_binary {
name: "foo.rust",
srcs: ["foo.rs"],
rlibs: ["libfoo.rlib.rust"],
rustlibs: ["libfoo.transitive.dylib.rust"],
apex_available: ["myapex"],
}
rust_library_rlib {
name: "libfoo.rlib.rust",
srcs: ["foo.rs"],
crate_name: "foo",
apex_available: ["myapex"],
shared_libs: ["libfoo.shared_from_rust"],
}
cc_library_shared {
name: "libfoo.shared_from_rust",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: ["myapex"],
}
rust_library_dylib {
name: "libfoo.dylib.rust",
srcs: ["foo.rs"],
crate_name: "foo",
apex_available: ["myapex"],
}
rust_library_dylib {
name: "libfoo.transitive.dylib.rust",
srcs: ["foo.rs"],
crate_name: "foo",
apex_available: ["myapex"],
}
rust_ffi_shared {
name: "libfoo.ffi",
srcs: ["foo.rs"],
crate_name: "foo",
apex_available: ["myapex"],
}
rust_ffi_shared {
name: "libbar.ffi",
srcs: ["foo.rs"],
crate_name: "bar",
apex_available: ["myapex"],
}
cc_library_shared {
name: "mylib2",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
static_libs: ["libstatic"],
// TODO: remove //apex_available:platform
apex_available: [
"//apex_available:platform",
"myapex",
],
}
cc_prebuilt_library_shared {
name: "mylib2",
srcs: ["prebuilt.so"],
// TODO: remove //apex_available:platform
apex_available: [
"//apex_available:platform",
"myapex",
],
}
cc_library_static {
name: "libstatic",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
// TODO: remove //apex_available:platform
apex_available: [
"//apex_available:platform",
"myapex",
],
}
java_library {
name: "myjar",
srcs: ["foo/bar/MyClass.java"],
stem: "myjar_stem",
sdk_version: "none",
system_modules: "none",
static_libs: ["myotherjar"],
libs: ["mysharedjar"],
// TODO: remove //apex_available:platform
apex_available: [
"//apex_available:platform",
"myapex",
],
compile_dex: true,
}
dex_import {
name: "myjar_dex",
jars: ["prebuilt.jar"],
apex_available: [
"//apex_available:platform",
"myapex",
],
}
java_library {
name: "myotherjar",
srcs: ["foo/bar/MyClass.java"],
sdk_version: "none",
system_modules: "none",
// TODO: remove //apex_available:platform
apex_available: [
"//apex_available:platform",
"myapex",
],
}
java_library {
name: "mysharedjar",
srcs: ["foo/bar/MyClass.java"],
sdk_version: "none",
system_modules: "none",
}
`)
apexRule := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Rule("apexRule")
// Make sure that Android.mk is created
ab := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Module().(*apexBundle)
data := android.AndroidMkDataForTest(t, ctx, ab)
var builder strings.Builder
data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
androidMk := builder.String()
ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
optFlags := apexRule.Args["opt_flags"]
ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
// Ensure that the NOTICE output is being packaged as an asset.
ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex/NOTICE")
copyCmds := apexRule.Args["copy_commands"]
// Ensure that main rule creates an output
ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
// Ensure that apex variant is created for the direct dep
ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
// Ensure that apex variant is created for the indirect dep
ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.transitive.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
// Ensure that both direct and indirect deps are copied into apex
ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
ensureContains(t, copyCmds, "image.apex/lib64/libfoo.transitive.dylib.rust.dylib.so")
ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
// .. but not for java libs
ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
// Ensure that the platform variant ends with _shared or _common
ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
// Ensure that dynamic dependency to java libs are not included
ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
// Ensure that all symlinks are present.
found_foo_link_64 := false
found_foo := false
for _, cmd := range strings.Split(copyCmds, " && ") {
if strings.Contains(cmd, "ln -sfn foo64") {
if strings.HasSuffix(cmd, "bin/foo") {
found_foo = true
} else if strings.HasSuffix(cmd, "bin/foo_link_64") {
found_foo_link_64 = true
}
}
}
good := found_foo && found_foo_link_64
if !good {
t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
}
fullDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
ctx.ModuleForTests(t, "myapex", "android_common_myapex").Output("depsinfo/fulllist.txt")), "\n")
ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
flatDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
ctx.ModuleForTests(t, "myapex", "android_common_myapex").Output("depsinfo/flatlist.txt")), "\n")
ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
}
func TestDefaults(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex_defaults {
name: "myapex-defaults",
key: "myapex.key",
prebuilts: ["myetc"],
native_shared_libs: ["mylib"],
java_libs: ["myjar"],
apps: ["AppFoo"],
rros: ["rro"],
bpfs: ["bpf", "netdTest"],
updatable: false,
}
prebuilt_etc {
name: "myetc",
src: "myprebuilt",
}
apex {
name: "myapex",
defaults: ["myapex-defaults"],
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib",
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
}
java_library {
name: "myjar",
srcs: ["foo/bar/MyClass.java"],
sdk_version: "none",
system_modules: "none",
apex_available: [ "myapex" ],
compile_dex: true,
}
android_app {
name: "AppFoo",
srcs: ["foo/bar/MyClass.java"],
sdk_version: "none",
system_modules: "none",
apex_available: [ "myapex" ],
}
runtime_resource_overlay {
name: "rro",
theme: "blue",
}
bpf {
name: "bpf",
srcs: ["bpf.c", "bpf2.c"],
}
bpf {
name: "netdTest",
srcs: ["netdTest.c"],
sub_dir: "netd",
}
`)
ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
"etc/myetc",
"javalib/myjar.jar",
"lib64/mylib.so",
"app/AppFoo@TEST.BUILD_ID/AppFoo.apk",
"overlay/blue/rro.apk",
"etc/bpf/bpf.o",
"etc/bpf/bpf2.o",
"etc/bpf/netd/netdTest.o",
})
}
func TestApexManifest(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
`)
module := ctx.ModuleForTests(t, "myapex", "android_common_myapex")
args := module.Rule("apexRule").Args
if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
t.Error("manifest should be apex_manifest.pb, but " + manifest)
}
}
func TestApexManifestMinSdkVersion(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex_defaults {
name: "my_defaults",
key: "myapex.key",
product_specific: true,
file_contexts: ":my-file-contexts",
updatable: false,
}
apex {
name: "myapex_30",
min_sdk_version: "30",
defaults: ["my_defaults"],
}
apex {
name: "myapex_current",
min_sdk_version: "current",
defaults: ["my_defaults"],
}
apex {
name: "myapex_none",
defaults: ["my_defaults"],
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
filegroup {
name: "my-file-contexts",
srcs: ["product_specific_file_contexts"],
}
`, withFiles(map[string][]byte{
"product_specific_file_contexts": nil,
}), android.FixtureModifyProductVariables(
func(variables android.FixtureProductVariables) {
variables.Unbundled_build = proptools.BoolPtr(true)
variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
}), android.FixtureMergeEnv(map[string]string{
"UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
}))
testCases := []struct {
module string
minSdkVersion string
}{
{
module: "myapex_30",
minSdkVersion: "30",
},
{
module: "myapex_current",
minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
},
{
module: "myapex_none",
minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
},
}
for _, tc := range testCases {
module := ctx.ModuleForTests(t, tc.module, "android_common_"+tc.module)
args := module.Rule("apexRule").Args
optFlags := args["opt_flags"]
if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
}
}
}
func TestApexWithDessertSha(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex_defaults {
name: "my_defaults",
key: "myapex.key",
product_specific: true,
file_contexts: ":my-file-contexts",
updatable: false,
}
apex {
name: "myapex_30",
min_sdk_version: "30",
defaults: ["my_defaults"],
}
apex {
name: "myapex_current",
min_sdk_version: "current",
defaults: ["my_defaults"],
}
apex {
name: "myapex_none",
defaults: ["my_defaults"],
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
filegroup {
name: "my-file-contexts",
srcs: ["product_specific_file_contexts"],
}
`, withFiles(map[string][]byte{
"product_specific_file_contexts": nil,
}), android.FixtureModifyProductVariables(
func(variables android.FixtureProductVariables) {
variables.Unbundled_build = proptools.BoolPtr(true)
variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
}), android.FixtureMergeEnv(map[string]string{
"UNBUNDLED_BUILD_TARGET_SDK_WITH_DESSERT_SHA": "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
}))
testCases := []struct {
module string
minSdkVersion string
}{
{
module: "myapex_30",
minSdkVersion: "30",
},
{
module: "myapex_current",
minSdkVersion: "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
},
{
module: "myapex_none",
minSdkVersion: "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
},
}
for _, tc := range testCases {
module := ctx.ModuleForTests(t, tc.module, "android_common_"+tc.module)
args := module.Rule("apexRule").Args
optFlags := args["opt_flags"]
if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
}
}
}
func TestFileContexts(t *testing.T) {
t.Parallel()
for _, vendor := range []bool{true, false} {
prop := ""
if vendor {
prop = "vendor: true,\n"
}
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
updatable: false,
`+prop+`
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
`)
rule := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Output("file_contexts")
if vendor {
android.AssertStringDoesContain(t, "should force-label as vendor_apex_metadata_file",
rule.RuleParams.Command,
"apex_manifest\\\\.pb u:object_r:vendor_apex_metadata_file:s0")
} else {
android.AssertStringDoesContain(t, "should force-label as system_file",
rule.RuleParams.Command,
"apex_manifest\\\\.pb u:object_r:system_file:s0")
}
}
}
func TestApexWithStubs(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: [
"mylib",
"mylib3",
"libmylib3_rs",
],
binaries: ["foo.rust"],
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
shared_libs: [
"mylib2",
"mylib3#impl",
"libmylib2_rs",
"libmylib3_rs#impl",
"my_prebuilt_platform_lib",
"my_prebuilt_platform_stub_only_lib",
],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
}
cc_library {
name: "mylib2",
srcs: ["mylib.cpp"],
cflags: ["-include mylib.h"],
system_shared_libs: [],
stl: "none",
stubs: {
symbol_file: "mylib2.map.txt",
versions: ["1", "2", "3"],
},
}
rust_ffi {
name: "libmylib2_rs",
crate_name: "mylib2",
srcs: ["mylib.rs"],
stubs: {
symbol_file: "mylib2.map.txt",
versions: ["1", "2", "3"],
},
}
cc_library {
name: "mylib3",
srcs: ["mylib.cpp"],
shared_libs: ["mylib4"],
system_shared_libs: [],
stl: "none",
stubs: {
symbol_file: "mylib3.map.txt",
versions: ["10", "11", "12"],
},
apex_available: [ "myapex" ],
}
rust_ffi {
name: "libmylib3_rs",
crate_name: "mylib3",
srcs: ["mylib.rs"],
shared_libs: ["mylib4.from_rust"],
stubs: {
symbol_file: "mylib3.map.txt",
versions: ["10", "11", "12"],
},
apex_available: [ "myapex" ],
}
cc_library {
name: "mylib4",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
}
cc_library {
name: "mylib4.from_rust",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
}
cc_prebuilt_library_shared {
name: "my_prebuilt_platform_lib",
stubs: {
symbol_file: "my_prebuilt_platform_lib.map.txt",
versions: ["1", "2", "3"],
},
srcs: ["foo.so"],
}
// Similar to my_prebuilt_platform_lib, but this library only provides stubs, i.e. srcs is empty
cc_prebuilt_library_shared {
name: "my_prebuilt_platform_stub_only_lib",
stubs: {
symbol_file: "my_prebuilt_platform_stub_only_lib.map.txt",
versions: ["1", "2", "3"],
}
}
rust_binary {
name: "foo.rust",
srcs: ["foo.rs"],
shared_libs: [
"libfoo.shared_from_rust",
"libfoo_rs.shared_from_rust",
],
prefer_rlib: true,
apex_available: ["myapex"],
}
cc_library_shared {
name: "libfoo.shared_from_rust",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
stubs: {
versions: ["10", "11", "12"],
},
}
rust_ffi {
name: "libfoo_rs.shared_from_rust",
crate_name: "foo_rs",
srcs: ["mylib.rs"],
stubs: {
versions: ["10", "11", "12"],
},
}
`)
apexRule := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Rule("apexRule")
copyCmds := apexRule.Args["copy_commands"]
// Ensure that direct non-stubs dep is always included
ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
// Ensure that indirect stubs dep is not included
ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
ensureNotContains(t, copyCmds, "image.apex/lib64/libmylib2_rs.so")
// Ensure that direct stubs dep is included
ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
ensureContains(t, copyCmds, "image.apex/lib64/libmylib3_rs.so")
mylibLdFlags := ctx.ModuleForTests(t, "mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
// Ensure that mylib is linking with the latest version of stubs for mylib2
ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
ensureContains(t, mylibLdFlags, "libmylib2_rs/android_arm64_armv8-a_shared_current/unstripped/libmylib2_rs.so")
// ... and not linking to the non-stub (impl) variant of mylib2
ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
ensureNotContains(t, mylibLdFlags, "libmylib2_rs/android_arm64_armv8-a_shared/unstripped/libmylib2_rs.so")
// Ensure that mylib is linking with the non-stub (impl) of mylib3 (because the dependency is added with mylib3#impl)
ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
ensureContains(t, mylibLdFlags, "libmylib3_rs/android_arm64_armv8-a_shared_apex10000/unstripped/libmylib3_rs.so")
// .. and not linking to the stubs variant of mylib3
ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
ensureNotContains(t, mylibLdFlags, "libmylib3_rs/android_arm64_armv8-a_shared_12/unstripped/mylib3.so")
// Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
// is replaced by sharing of "cFlags" in cc/builder.go.
// The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
// module variable representing "cflags". So it was not detected by ensureNotContains.
// Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
// content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
// including the original cflags's "-include mylib.h".
//
// Ensure that stubs libs are built without -include flags
// mylib2Cflags := ctx.ModuleForTests(t, "mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
// ensureNotContains(t, mylib2Cflags, "-include ")
// Ensure that genstub for platform-provided lib is invoked with --systemapi
ensureContains(t, ctx.ModuleForTests(t, "mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
ensureContains(t, ctx.ModuleForTests(t, "libmylib2_rs", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
// Ensure that genstub for apex-provided lib is invoked with --apex
ensureContains(t, ctx.ModuleForTests(t, "mylib3", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
ensureContains(t, ctx.ModuleForTests(t, "libmylib3_rs", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
"lib64/mylib.so",
"lib64/mylib3.so",
"lib64/libmylib3_rs.so",
"lib64/mylib4.so",
"lib64/mylib4.from_rust.so",
"bin/foo.rust",
"lib64/libstd.dylib.so", // implicit rust ffi dep
})
// Ensure that stub dependency from a rust module is not included
ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo_rs.shared_from_rust.so")
// The rust module is linked to the stub cc library
rustDeps := ctx.ModuleForTests(t, "foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
ensureContains(t, rustDeps, "libfoo_rs.shared_from_rust/android_arm64_armv8-a_shared_current/unstripped/libfoo_rs.shared_from_rust.so")
ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
ensureNotContains(t, rustDeps, "libfoo_rs.shared_from_rust/android_arm64_armv8-a_shared/unstripped/libfoo_rs.shared_from_rust.so")
apexManifestRule := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Rule("apexManifestRule")
ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo_rs.shared_from_rust.so")
// Ensure that mylib is linking with the latest version of stubs for my_prebuilt_platform_lib
ensureContains(t, mylibLdFlags, "my_prebuilt_platform_lib/android_arm64_armv8-a_shared_current/my_prebuilt_platform_lib.so")
// ... and not linking to the non-stub (impl) variant of my_prebuilt_platform_lib
ensureNotContains(t, mylibLdFlags, "my_prebuilt_platform_lib/android_arm64_armv8-a_shared/my_prebuilt_platform_lib.so")
// Ensure that genstub for platform-provided lib is invoked with --systemapi
ensureContains(t, ctx.ModuleForTests(t, "my_prebuilt_platform_lib", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
// Ensure that mylib is linking with the latest version of stubs for my_prebuilt_platform_lib
ensureContains(t, mylibLdFlags, "my_prebuilt_platform_stub_only_lib/android_arm64_armv8-a_shared_current/my_prebuilt_platform_stub_only_lib.so")
// ... and not linking to the non-stub (impl) variant of my_prebuilt_platform_lib
ensureNotContains(t, mylibLdFlags, "my_prebuilt_platform_stub_only_lib/android_arm64_armv8-a_shared/my_prebuilt_platform_stub_only_lib.so")
// Ensure that genstub for platform-provided lib is invoked with --systemapi
ensureContains(t, ctx.ModuleForTests(t, "my_prebuilt_platform_stub_only_lib", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
}
func TestApexShouldNotEmbedStubVariant(t *testing.T) {
t.Parallel()
testApexError(t, `module "myapex" .*: native_shared_libs: "libbar" is a stub`, `
apex {
name: "myapex",
key: "myapex.key",
vendor: true,
updatable: false,
native_shared_libs: ["libbar"], // should not add an LLNDK stub in a vendor apex
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "libbar",
srcs: ["mylib.cpp"],
llndk: {
symbol_file: "libbar.map.txt",
}
}
`)
}
func TestApexCanUsePrivateApis(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["mylib"],
binaries: ["foo.rust"],
updatable: false,
platform_apis: true,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
shared_libs: [
"mylib2",
"libmylib2_rust"
],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
}
cc_library {
name: "mylib2",
srcs: ["mylib.cpp"],
cflags: ["-include mylib.h"],
system_shared_libs: [],
stl: "none",
stubs: {
versions: ["1", "2", "3"],
},
}
rust_ffi {
name: "libmylib2_rust",
crate_name: "mylib2_rust",
srcs: ["mylib.rs"],
stubs: {
versions: ["1", "2", "3"],
},
}
rust_binary {
name: "foo.rust",
srcs: ["foo.rs"],
shared_libs: [
"libfoo.shared_from_rust",
"libmylib_rust.shared_from_rust"
],
prefer_rlib: true,
apex_available: ["myapex"],
}
cc_library_shared {
name: "libfoo.shared_from_rust",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
stubs: {
versions: ["10", "11", "12"],
},
}
rust_ffi {
name: "libmylib_rust.shared_from_rust",
crate_name: "mylib_rust",
srcs: ["mylib.rs"],
stubs: {
versions: ["1", "2", "3"],
},
}
`)
apexRule := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Rule("apexRule")
copyCmds := apexRule.Args["copy_commands"]
// Ensure that indirect stubs dep is not included
ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
ensureNotContains(t, copyCmds, "image.apex/lib64/libmylib_rust.so")
ensureNotContains(t, copyCmds, "image.apex/lib64/libmylib_rust.shared_from_rust.so")
ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
// Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
// of the platform_apis: true)
mylibLdFlags := ctx.ModuleForTests(t, "mylib", "android_arm64_armv8-a_shared_apex10000_p").Rule("ld").Args["libFlags"]
ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
ensureNotContains(t, mylibLdFlags, "libmylib2_rust/android_arm64_armv8-a_shared_current/unstripped/libmylib2_rust.so")
ensureContains(t, mylibLdFlags, "libmylib2_rust/android_arm64_armv8-a_shared/unstripped/libmylib2_rust.so")
rustDeps := ctx.ModuleForTests(t, "foo.rust", "android_arm64_armv8-a_apex10000_p").Rule("rustc").Args["linkFlags"]
ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
ensureNotContains(t, rustDeps, "libmylib_rust.shared_from_rust/android_arm64_armv8-a_shared_current/unstripped/libmylib_rust.shared_from_rust.so")
ensureContains(t, rustDeps, "libmylib_rust.shared_from_rust/android_arm64_armv8-a_shared/unstripped/libmylib_rust.shared_from_rust.so")
}
func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: [
"mylib",
"mylib3",
"libmylib3_rust",
],
min_sdk_version: "29",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
shared_libs: [
"mylib2",
"mylib3#impl",
"libmylib2_rust",
"libmylib3_rust#impl",
],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
min_sdk_version: "28",
}
cc_library {
name: "mylib2",
srcs: ["mylib.cpp"],
cflags: ["-include mylib.h"],
system_shared_libs: [],
stl: "none",
stubs: {
symbol_file: "mylib2.map.txt",
versions: ["28", "29", "30", "current"],
},
min_sdk_version: "28",
}
rust_ffi {
name: "libmylib2_rust",
crate_name: "mylib2_rust",
srcs: ["mylib.rs"],
stubs: {
symbol_file: "mylib2.map.txt",
versions: ["28", "29", "30", "current"],
},
min_sdk_version: "28",
}
cc_library {
name: "mylib3",
srcs: ["mylib.cpp"],
shared_libs: ["mylib4"],
system_shared_libs: [],
stl: "none",
stubs: {
symbol_file: "mylib3.map.txt",
versions: ["28", "29", "30", "current"],
},
apex_available: [ "myapex" ],
min_sdk_version: "28",
}
rust_ffi {
name: "libmylib3_rust",
crate_name: "mylib3_rust",
srcs: ["mylib.rs"],
shared_libs: ["libmylib4.from_rust"],
stubs: {
symbol_file: "mylib3.map.txt",
versions: ["28", "29", "30", "current"],
},
apex_available: [ "myapex" ],
min_sdk_version: "28",
}
cc_library {
name: "mylib4",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
min_sdk_version: "28",
}
rust_ffi {
name: "libmylib4.from_rust",
crate_name: "mylib4",
srcs: ["mylib.rs"],
apex_available: [ "myapex" ],
min_sdk_version: "28",
}
`)
apexRule := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Rule("apexRule")
copyCmds := apexRule.Args["copy_commands"]
// Ensure that direct non-stubs dep is always included
ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
ensureContains(t, copyCmds, "image.apex/lib64/libmylib3_rust.so")
// Ensure that indirect stubs dep is not included
ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
ensureNotContains(t, copyCmds, "image.apex/lib64/libmylib2_rust.so")
// Ensure that direct stubs dep is included
ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
mylibLdFlags := ctx.ModuleForTests(t, "mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
// Ensure that mylib is linking with the latest version of stub for mylib2
ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
ensureContains(t, mylibLdFlags, "libmylib2_rust/android_arm64_armv8-a_shared_current/unstripped/libmylib2_rust.so")
// ... and not linking to the non-stub (impl) variant of mylib2
ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
ensureNotContains(t, mylibLdFlags, "libmylib2_rust/android_arm64_armv8-a_shared/unstripped/libmylib2_rust.so")
// Ensure that mylib is linking with the non-stub (impl) of mylib3 (because the dependency is added with mylib3#impl)
ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
ensureContains(t, mylibLdFlags, "libmylib3_rust/android_arm64_armv8-a_shared_apex29/unstripped/libmylib3_rust.so")
// .. and not linking to the stubs variant of mylib3
ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
ensureNotContains(t, mylibLdFlags, "libmylib3_rust/android_arm64_armv8-a_shared_29/unstripped/libmylib3_rust.so")
// Ensure that stubs libs are built without -include flags
mylib2Cflags := ctx.ModuleForTests(t, "mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
ensureNotContains(t, mylib2Cflags, "-include ")
// Ensure that genstub is invoked with --systemapi
ensureContains(t, ctx.ModuleForTests(t, "mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"], "--systemapi")
ensureContains(t, ctx.ModuleForTests(t, "libmylib2_rust", "android_arm64_armv8-a_shared_29").Rule("cc.genStubSrc").Args["flags"], "--systemapi")
ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
"lib64/mylib.so",
"lib64/mylib3.so",
"lib64/libmylib3_rust.so",
"lib64/mylib4.so",
"lib64/libmylib4.from_rust.so",
"lib64/libstd.dylib.so", // by the implicit dependency from foo.rust
})
}
func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
t.Parallel()
// myapex (Z)
// mylib -----------------.
// |
// otherapex (29) |
// libstub's versions: 29 Z current
// |
// <platform> |
// libplatform ----------------'
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["mylib"],
min_sdk_version: "Z", // non-final
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
shared_libs: [
"libstub",
"libstub_rust",
],
apex_available: ["myapex"],
min_sdk_version: "Z",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
apex {
name: "otherapex",
key: "myapex.key",
native_shared_libs: [
"libstub",
"libstub_rust",
],
min_sdk_version: "29",
}
cc_library {
name: "libstub",
srcs: ["mylib.cpp"],
stubs: {
versions: ["29", "Z", "current"],
},
apex_available: ["otherapex"],
min_sdk_version: "29",
}
rust_ffi {
name: "libstub_rust",
crate_name: "stub_rust",
srcs: ["mylib.rs"],
stubs: {
versions: ["29", "Z", "current"],
},
apex_available: ["otherapex"],
min_sdk_version: "29",
}
// platform module depending on libstub from otherapex should use the latest stub("current")
cc_library {
name: "libplatform",
srcs: ["mylib.cpp"],
shared_libs: [
"libstub",
"libstub_rust",
],
split_all_variants: true,
}
`,
android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
variables.Platform_sdk_codename = proptools.StringPtr("Z")
variables.Platform_sdk_final = proptools.BoolPtr(false)
variables.Platform_version_active_codenames = []string{"Z"}
}),
)
// Ensure that mylib from myapex is built against the latest stub (current)
mylibCflags := ctx.ModuleForTests(t, "mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
// rust stubs do not emit -D__LIBFOO_API__ flags as this is deprecated behavior for cc stubs
mylibLdflags := ctx.ModuleForTests(t, "mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
ensureContains(t, mylibLdflags, "libstub_rust/android_arm64_armv8-a_shared_current/unstripped/libstub_rust.so ")
// Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
libplatformCflags := ctx.ModuleForTests(t, "libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
// rust stubs do not emit -D__LIBFOO_API__ flags as this is deprecated behavior for cc stubs
libplatformLdflags := ctx.ModuleForTests(t, "libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
ensureContains(t, libplatformLdflags, "libstub_rust/android_arm64_armv8-a_shared_current/unstripped/libstub_rust.so ")
}
func TestApexWithExplicitStubsDependency(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex2",
key: "myapex2.key",
native_shared_libs: ["mylib"],
updatable: false,
}
apex_key {
name: "myapex2.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
shared_libs: [
"libfoo#10",
"libfoo_rust#10"
],
static_libs: ["libbaz"],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex2" ],
}
cc_library {
name: "libfoo",
srcs: ["mylib.cpp"],
shared_libs: ["libbar"],
system_shared_libs: [],
stl: "none",
stubs: {
versions: ["10", "20", "30"],
},
}
rust_ffi {
name: "libfoo_rust",
crate_name: "foo_rust",
srcs: ["mylib.cpp"],
shared_libs: ["libbar.from_rust"],
stubs: {
versions: ["10", "20", "30"],
},
}
cc_library {
name: "libbar",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
}
cc_library {
name: "libbar.from_rust",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
}
cc_library_static {
name: "libbaz",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex2" ],
}
`)
apexRule := ctx.ModuleForTests(t, "myapex2", "android_common_myapex2").Rule("apexRule")
copyCmds := apexRule.Args["copy_commands"]
// Ensure that direct non-stubs dep is always included
ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
// Ensure that indirect stubs dep is not included
ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo_rust.so")
// Ensure that dependency of stubs is not included
ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.from_rust.so")
mylibLdFlags := ctx.ModuleForTests(t, "mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
// Ensure that mylib is linking with version 10 of libfoo
ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
ensureContains(t, mylibLdFlags, "libfoo_rust/android_arm64_armv8-a_shared_10/unstripped/libfoo_rust.so")
// ... and not linking to the non-stub (impl) variant of libfoo
ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
ensureNotContains(t, mylibLdFlags, "libfoo_rust/android_arm64_armv8-a_shared/unstripped/libfoo_rust.so")
libFooStubsLdFlags := ctx.ModuleForTests(t, "libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
libFooRustStubsLdFlags := ctx.ModuleForTests(t, "libfoo_rust", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
// Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
ensureNotContains(t, libFooRustStubsLdFlags, "libbar.from_rust.so")
fullDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
ctx.ModuleForTests(t, "myapex2", "android_common_myapex2").Output("depsinfo/fulllist.txt")), "\n")
ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
flatDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
ctx.ModuleForTests(t, "myapex2", "android_common_myapex2").Output("depsinfo/flatlist.txt")), "\n")
ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
}
func TestApexWithRuntimeLibsDependency(t *testing.T) {
t.Parallel()
/*
myapex
|
v (runtime_libs)
mylib ------+------> libfoo [provides stub]
|
`------> libbar
*/
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["mylib"],
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
static_libs: ["libstatic"],
shared_libs: ["libshared"],
runtime_libs: [
"libfoo",
"libbar",
"libfoo_rs",
],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
}
cc_library {
name: "libfoo",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
stubs: {
versions: ["10", "20", "30"],
},
}
rust_ffi {
name: "libfoo_rs",
crate_name: "foo_rs",
srcs: ["mylib.rs"],
stubs: {
versions: ["10", "20", "30"],
},
}
cc_library {
name: "libbar",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
}
cc_library {
name: "libstatic",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
runtime_libs: ["libstatic_to_runtime"],
}
cc_library {
name: "libshared",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
runtime_libs: ["libshared_to_runtime"],
}
cc_library {
name: "libstatic_to_runtime",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
}
cc_library {
name: "libshared_to_runtime",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
}
`)
apexRule := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Rule("apexRule")
copyCmds := apexRule.Args["copy_commands"]
// Ensure that direct non-stubs dep is always included
ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
// Ensure that indirect stubs dep is not included
ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo_rs.so")
// Ensure that runtime_libs dep in included
ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
ensureContains(t, copyCmds, "image.apex/lib64/libshared.so")
ensureContains(t, copyCmds, "image.apex/lib64/libshared_to_runtime.so")
ensureNotContains(t, copyCmds, "image.apex/lib64/libstatic_to_runtime.so")
apexManifestRule := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Rule("apexManifestRule")
ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo_rs.so")
}
var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
cc.PrepareForTestWithCcBuildComponents,
PrepareForTestWithApexBuildComponents,
android.FixtureAddTextFile("bionic/apex/Android.bp", `
apex {
name: "com.android.runtime",
key: "com.android.runtime.key",
native_shared_libs: ["libc"],
updatable: false,
}
apex_key {
name: "com.android.runtime.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
`),
android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
)
func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
t.Parallel()
result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
cc_library {
name: "libc",
no_libcrt: true,
nocrt: true,
no_crt_pad_segment: true,
stl: "none",
system_shared_libs: [],
stubs: { versions: ["1"] },
apex_available: ["com.android.runtime"],
sanitize: {
hwaddress: true,
}
}
cc_prebuilt_library_shared {
name: "libclang_rt.hwasan",
no_libcrt: true,
nocrt: true,
no_crt_pad_segment: true,
stl: "none",
system_shared_libs: [],
srcs: [""],
stubs: { versions: ["1"] },
stem: "libclang_rt.hwasan-aarch64-android",
sanitize: {
never: true,
},
apex_available: [
"//apex_available:anyapex",
"//apex_available:platform",
],
} `)
ctx := result.TestContext
ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime", []string{
"lib64/bionic/libc.so",
"lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
})
hwasan := ctx.ModuleForTests(t, "libclang_rt.hwasan", "android_arm64_armv8-a_shared")
installed := hwasan.Description("install libclang_rt.hwasan")
ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
symlink := hwasan.Description("install symlink libclang_rt.hwasan")
ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
}
func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
t.Parallel()
result := android.GroupFixturePreparers(
prepareForTestOfRuntimeApexWithHwasan,
android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
variables.SanitizeDevice = []string{"hwaddress"}
}),
).RunTestWithBp(t, `
cc_library {
name: "libc",
no_libcrt: true,
nocrt: true,
no_crt_pad_segment: true,
stl: "none",
system_shared_libs: [],
stubs: { versions: ["1"] },
apex_available: ["com.android.runtime"],
}
cc_prebuilt_library_shared {
name: "libclang_rt.hwasan",
no_libcrt: true,
nocrt: true,
no_crt_pad_segment: true,
stl: "none",
system_shared_libs: [],
srcs: [""],
stubs: { versions: ["1"] },
stem: "libclang_rt.hwasan-aarch64-android",
sanitize: {
never: true,
},
apex_available: [
"//apex_available:anyapex",
"//apex_available:platform",
],
}
`)
ctx := result.TestContext
ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime", []string{
"lib64/bionic/libc.so",
"lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
})
hwasan := ctx.ModuleForTests(t, "libclang_rt.hwasan", "android_arm64_armv8-a_shared")
installed := hwasan.Description("install libclang_rt.hwasan")
ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
symlink := hwasan.Description("install symlink libclang_rt.hwasan")
ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
}
func TestApexDependsOnLLNDKTransitively(t *testing.T) {
t.Parallel()
testcases := []struct {
name string
minSdkVersion string
apexVariant string
shouldLink string
shouldNotLink []string
}{
{
name: "unspecified version links to the latest",
minSdkVersion: "",
apexVariant: "apex10000",
shouldLink: "current",
shouldNotLink: []string{"29", "30"},
},
{
name: "always use the latest",
minSdkVersion: "min_sdk_version: \"29\",",
apexVariant: "apex29",
shouldLink: "current",
shouldNotLink: []string{"29", "30"},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["mylib"],
updatable: false,
`+tc.minSdkVersion+`
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
vendor_available: true,
shared_libs: ["libbar"],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
min_sdk_version: "29",
}
cc_library {
name: "libbar",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
stubs: { versions: ["29","30"] },
llndk: {
symbol_file: "libbar.map.txt",
}
}
`,
withUnbundledBuild,
)
// Ensure that LLNDK dep is not included
ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
"lib64/mylib.so",
})
// Ensure that LLNDK dep is required
apexManifestRule := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Rule("apexManifestRule")
ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
mylibLdFlags := ctx.ModuleForTests(t, "mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
for _, ver := range tc.shouldNotLink {
ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
}
mylibCFlags := ctx.ModuleForTests(t, "mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
ver := tc.shouldLink
if tc.shouldLink == "current" {
ver = strconv.Itoa(android.FutureApiLevelInt)
}
ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
})
}
}
func TestApexWithSystemLibsStubs(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm", "libmylib_rs"],
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
system_shared_libs: ["libc"],
shared_libs: ["libdl#27", "libm#impl"],
stl: "none",
apex_available: [ "myapex" ],
}
rust_ffi {
name: "libmylib_rs",
crate_name: "mylib_rs",
shared_libs: ["libvers#27", "libm#impl"],
srcs: ["mylib.rs"],
apex_available: [ "myapex" ],
}
cc_library_shared {
name: "mylib_shared",
srcs: ["mylib.cpp"],
shared_libs: ["libdl#27"],
stl: "none",
apex_available: [ "myapex" ],
}
cc_library {
name: "libBootstrap",
srcs: ["mylib.cpp"],
stl: "none",
bootstrap: true,
split_all_variants: true,
}
rust_ffi {
name: "libbootstrap_rs",
srcs: ["mylib.cpp"],
crate_name: "bootstrap_rs",
bootstrap: true,
}
cc_library {
name: "libvers",
srcs: ["mylib.cpp"],
stl: "none",
stubs: { versions: ["27","30"] },
}
`)
apexRule := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Rule("apexRule")
copyCmds := apexRule.Args["copy_commands"]
// Ensure that mylib, libmylib_rs, libm, libdl, libstd.dylib.so (from Rust) are included.
ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
ensureContains(t, copyCmds, "image.apex/lib64/libmylib_rs.so")
ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
ensureContains(t, copyCmds, "image.apex/lib64/libstd.dylib.so")
// Ensure that libc and liblog (from Rust) is not included (since it has stubs and not listed in native_shared_libs)
ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
ensureNotContains(t, copyCmds, "image.apex/lib64/liblog.so")
mylibLdFlags := ctx.ModuleForTests(t, "mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
mylibRsFlags := ctx.ModuleForTests(t, "libmylib_rs", "android_arm64_armv8-a_shared_apex10000").Rule("rustc").Args["linkFlags"]
mylibCFlags := ctx.ModuleForTests(t, "mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
mylibSharedCFlags := ctx.ModuleForTests(t, "mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
// For dependency to libc
// Ensure that mylib is linking with the latest version of stubs
ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
// ... and not linking to the non-stub (impl) variant
ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
// ... Cflags from stub is correctly exported to mylib
ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
// For dependency to libm
// Ensure that mylib is linking with the non-stub (impl) variant
ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
// ... and not linking to the stub variant
ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
// ... and is not compiling with the stub
ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
// For dependency to libdl
// Ensure that mylib is linking with the specified version of stubs
ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
// ... and not linking to the other versions of stubs
ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
// ... and not linking to the non-stub (impl) variant
ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
// ... Cflags from stub is correctly exported to mylib
ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
// Rust checks
// For dependency to libc, liblog
// Ensure that libmylib_rs is linking with the latest versions of stubs
ensureContains(t, mylibRsFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
ensureContains(t, mylibRsFlags, "liblog/android_arm64_armv8-a_shared_current/liblog.so")
// ... and not linking to the non-stub (impl) variants
ensureNotContains(t, mylibRsFlags, "libc/android_arm64_armv8-a_shared/libc.so")
ensureNotContains(t, mylibRsFlags, "liblog/android_arm64_armv8-a_shared/liblog.so")
// For libm dependency (explicit)
// Ensure that mylib is linking with the non-stub (impl) variant
ensureContains(t, mylibRsFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
// ... and not linking to the stub variant
ensureNotContains(t, mylibRsFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
// For dependency to libvers
// (We do not use libdl#27 as Rust links the system libs implicitly and does
// not currently have a system_shared_libs equivalent to prevent this)
// Ensure that mylib is linking with the specified version of stubs
ensureContains(t, mylibRsFlags, "libvers/android_arm64_armv8-a_shared_27/libvers.so")
// ... and not linking to the other versions of stubs
ensureNotContains(t, mylibRsFlags, "libvers/android_arm64_armv8-a_shared_30/libvers.so")
// ... and not linking to the non-stub (impl) variant
ensureNotContains(t, mylibRsFlags, "libvers/android_arm64_armv8-a_shared_apex10000/libvers.so")
// Ensure that libBootstrap is depending on the platform variant of bionic libs
libFlags := ctx.ModuleForTests(t, "libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
// Ensure that libbootstrap_rs is depending on the platform variant of bionic libs
libRsFlags := ctx.ModuleForTests(t, "libbootstrap_rs", "android_arm64_armv8-a_shared").Rule("rustc").Args["linkFlags"]
ensureContains(t, libRsFlags, "libc/android_arm64_armv8-a_shared/libc.so")
ensureContains(t, libRsFlags, "libm/android_arm64_armv8-a_shared/libm.so")
ensureContains(t, libRsFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
}
func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
t.Parallel()
// there are three links between liba --> libz.
// 1) myapex -> libx -> liba -> libz : this should be #30 link
// 2) otherapex -> liby -> liba -> libz : this should be #30 link
// 3) (platform) -> liba -> libz : this should be non-stub link
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["libx"],
min_sdk_version: "29",
}
apex {
name: "otherapex",
key: "myapex.key",
native_shared_libs: ["liby"],
min_sdk_version: "30",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "libx",
shared_libs: ["liba"],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
min_sdk_version: "29",
}
cc_library {
name: "liby",
shared_libs: ["liba"],
system_shared_libs: [],
stl: "none",
apex_available: [ "otherapex" ],
min_sdk_version: "29",
}
cc_library {
name: "liba",
shared_libs: ["libz", "libz_rs"],
system_shared_libs: [],
stl: "none",
apex_available: [
"//apex_available:anyapex",
"//apex_available:platform",
],
min_sdk_version: "29",
}
cc_library {
name: "libz",
system_shared_libs: [],
stl: "none",
stubs: {
versions: ["28", "30"],
},
}
rust_ffi {
name: "libz_rs",
crate_name: "z_rs",
srcs: ["foo.rs"],
stubs: {
versions: ["28", "30"],
},
}
`)
expectLink := func(from, from_variant, to, to_variant string) {
ldArgs := ctx.ModuleForTests(t, from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
}
expectNoLink := func(from, from_variant, to, to_variant string) {
ldArgs := ctx.ModuleForTests(t, from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
}
// platform liba is linked to non-stub version
expectLink("liba", "shared", "libz", "shared")
expectLink("liba", "shared", "unstripped/libz_rs", "shared")
// liba in myapex is linked to current
expectLink("liba", "shared_apex29", "libz", "shared_current")
expectNoLink("liba", "shared_apex29", "libz", "shared_30")
expectNoLink("liba", "shared_apex29", "libz", "shared_28")
expectNoLink("liba", "shared_apex29", "libz", "shared")
expectLink("liba", "shared_apex29", "unstripped/libz_rs", "shared_current")
expectNoLink("liba", "shared_apex29", "unstripped/libz_rs", "shared_30")
expectNoLink("liba", "shared_apex29", "unstripped/libz_rs", "shared_28")
expectNoLink("liba", "shared_apex29", "unstripped/libz_rs", "shared")
// liba in otherapex is linked to current
expectLink("liba", "shared_apex30", "libz", "shared_current")
expectNoLink("liba", "shared_apex30", "libz", "shared_30")
expectNoLink("liba", "shared_apex30", "libz", "shared_28")
expectNoLink("liba", "shared_apex30", "libz", "shared")
expectLink("liba", "shared_apex30", "unstripped/libz_rs", "shared_current")
expectNoLink("liba", "shared_apex30", "unstripped/libz_rs", "shared_30")
expectNoLink("liba", "shared_apex30", "unstripped/libz_rs", "shared_28")
expectNoLink("liba", "shared_apex30", "unstripped/libz_rs", "shared")
}
func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["libx"],
min_sdk_version: "R",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "libx",
shared_libs: ["libz"],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
min_sdk_version: "R",
}
cc_library {
name: "libz",
system_shared_libs: [],
stl: "none",
stubs: {
versions: ["29", "R"],
},
}
`,
android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
variables.Platform_version_active_codenames = []string{"R"}
}),
)
expectLink := func(from, from_variant, to, to_variant string) {
ldArgs := ctx.ModuleForTests(t, from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
}
expectNoLink := func(from, from_variant, to, to_variant string) {
ldArgs := ctx.ModuleForTests(t, from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
}
expectLink("libx", "shared_apex10000", "libz", "shared_current")
expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
expectNoLink("libx", "shared_apex10000", "libz", "shared")
}
func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
t.Parallel()
testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
java_libs: ["libx"],
min_sdk_version: "S",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
java_library {
name: "libx",
srcs: ["a.java"],
apex_available: [ "myapex" ],
sdk_version: "current",
min_sdk_version: "S", // should be okay
compile_dex: true,
}
`,
android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
variables.Platform_version_active_codenames = []string{"S"}
variables.Platform_sdk_codename = proptools.StringPtr("S")
}),
)
}
func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["libx"],
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "libx",
shared_libs: ["libz"],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
}
cc_library {
name: "libz",
system_shared_libs: [],
stl: "none",
stubs: {
versions: ["1", "2"],
},
}
`)
expectLink := func(from, from_variant, to, to_variant string) {
ldArgs := ctx.ModuleForTests(t, from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
}
expectNoLink := func(from, from_variant, to, to_variant string) {
ldArgs := ctx.ModuleForTests(t, from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
}
expectLink("libx", "shared_apex10000", "libz", "shared_current")
expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
expectNoLink("libx", "shared_apex10000", "libz", "shared")
}
func TestApexMinSdkVersion_InVendorApex(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["mylib"],
updatable: true,
vendor: true,
min_sdk_version: "29",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
vendor_available: true,
min_sdk_version: "29",
shared_libs: ["libbar"],
}
cc_library {
name: "libbar",
stubs: { versions: ["29", "30"] },
llndk: { symbol_file: "libbar.map.txt" },
}
`)
vendorVariant := "android_vendor_arm64_armv8-a"
mylib := ctx.ModuleForTests(t, "mylib", vendorVariant+"_shared_apex29")
// Ensure that mylib links with "current" LLNDK
libFlags := names(mylib.Rule("ld").Args["libFlags"])
ensureListContains(t, libFlags, "out/soong/.intermediates/libbar/"+vendorVariant+"_shared/libbar.so")
// Ensure that mylib is targeting 29
ccRule := ctx.ModuleForTests(t, "mylib", vendorVariant+"_static_apex29").Output("obj/mylib.o")
ensureContains(t, ccRule.Args["cFlags"], "-target aarch64-linux-android29")
// Ensure that the correct variant of crtbegin_so is used.
crtBegin := mylib.Rule("ld").Args["crtBegin"]
ensureContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
// Ensure that the crtbegin_so used by the APEX is targeting 29
cflags := ctx.ModuleForTests(t, "crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
}
func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["libx", "libx_rs"],
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "libx",
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
stubs: {
versions: ["1", "2"],
},
}
rust_ffi {
name: "libx_rs",
crate_name: "x_rs",
srcs: ["x.rs"],
apex_available: [ "myapex" ],
stubs: {
versions: ["1", "2"],
},
}
cc_library {
name: "libz",
shared_libs: ["libx", "libx_rs",],
system_shared_libs: [],
stl: "none",
split_all_variants: true,
}
`)
expectLink := func(from, from_variant, to, to_variant string) {
t.Helper()
ldArgs := ctx.ModuleForTests(t, from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
}
expectNoLink := func(from, from_variant, to, to_variant string) {
t.Helper()
ldArgs := ctx.ModuleForTests(t, from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
}
expectLink("libz", "shared", "libx", "shared_current")
expectNoLink("libz", "shared", "libx", "shared_2")
expectLink("libz", "shared", "unstripped/libx_rs", "shared_current")
expectNoLink("libz", "shared", "unstripped/libx_rs", "shared_2")
expectNoLink("libz", "shared", "libz", "shared_1")
expectNoLink("libz", "shared", "libz", "shared")
}
var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
func(variables android.FixtureProductVariables) {
variables.SanitizeDevice = []string{"hwaddress"}
},
)
func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["libx"],
min_sdk_version: "29",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "libx",
shared_libs: ["libbar", "libbar_rs"],
apex_available: [ "myapex" ],
min_sdk_version: "29",
}
rust_ffi {
name: "libbar_rs",
crate_name: "bar_rs",
srcs: ["bar.rs"],
stubs: { versions: ["29", "30"] },
}
cc_library {
name: "libbar",
stubs: {
versions: ["29", "30"],
},
}
`,
prepareForTestWithSantitizeHwaddress,
)
expectLink := func(from, from_variant, to, to_variant string) {
ld := ctx.ModuleForTests(t, from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
libFlags := ld.Args["libFlags"]
ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
}
expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
expectLink("libx", "shared_hwasan_apex29", "unstripped/libbar_rs", "shared_current")
}
func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["libx"],
min_sdk_version: "29",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "libx",
apex_available: [ "myapex" ],
min_sdk_version: "29",
}
`)
// ensure apex variant of c++ is linked with static unwinder
cm := ctx.ModuleForTests(t, "libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
// note that platform variant is not.
cm = ctx.ModuleForTests(t, "libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
}
func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
t.Parallel()
testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["mylib"],
min_sdk_version: "29",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: [
"myapex",
],
min_sdk_version: "30",
}
`)
testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["libfoo.ffi"],
min_sdk_version: "29",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
rust_ffi_shared {
name: "libfoo.ffi",
srcs: ["foo.rs"],
crate_name: "foo",
apex_available: [
"myapex",
],
min_sdk_version: "30",
}
`)
testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
apex {
name: "myapex",
key: "myapex.key",
java_libs: ["libfoo"],
min_sdk_version: "29",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
java_import {
name: "libfoo",
jars: ["libfoo.jar"],
apex_available: [
"myapex",
],
min_sdk_version: "30",
compile_dex: true,
}
`)
// Skip check for modules compiling against core API surface
testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
java_libs: ["libfoo"],
min_sdk_version: "29",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
java_library {
name: "libfoo",
srcs: ["Foo.java"],
apex_available: [
"myapex",
],
// Compile against core API surface
sdk_version: "core_current",
min_sdk_version: "30",
compile_dex: true,
}
`)
}
func TestApexMinSdkVersion_Okay(t *testing.T) {
t.Parallel()
testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["libfoo"],
java_libs: ["libbar"],
min_sdk_version: "29",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "libfoo",
srcs: ["mylib.cpp"],
shared_libs: ["libfoo_dep"],
apex_available: ["myapex"],
min_sdk_version: "29",
}
cc_library {
name: "libfoo_dep",
srcs: ["mylib.cpp"],
apex_available: ["myapex"],
min_sdk_version: "29",
}
java_library {
name: "libbar",
sdk_version: "current",
srcs: ["a.java"],
static_libs: [
"libbar_dep",
"libbar_import_dep",
],
apex_available: ["myapex"],
min_sdk_version: "29",
compile_dex: true,
}
java_library {
name: "libbar_dep",
sdk_version: "current",
srcs: ["a.java"],
apex_available: ["myapex"],
min_sdk_version: "29",
}
java_import {
name: "libbar_import_dep",
jars: ["libbar.jar"],
apex_available: ["myapex"],
min_sdk_version: "29",
}
`)
}
func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
t.Parallel()
// Tests that an apex dependency with min_sdk_version higher than the
// min_sdk_version of the apex is allowed as long as the dependency's
// min_sdk_version is less than or equal to the api level that the
// architecture was introduced in. In this case, arm64 didn't exist
// until api level 21, so the arm64 code will never need to run on
// an api level 20 device, even if other architectures of the apex
// will.
testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["libfoo"],
min_sdk_version: "20",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "libfoo",
srcs: ["mylib.cpp"],
apex_available: ["myapex"],
min_sdk_version: "21",
stl: "none",
}
`)
}
func TestJavaStableSdkVersion(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
expectedError string
bp string
preparer android.FixturePreparer
}{
{
name: "Non-updatable apex with non-stable dep",
bp: `
apex {
name: "myapex",
java_libs: ["myjar"],
key: "myapex.key",
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
java_library {
name: "myjar",
srcs: ["foo/bar/MyClass.java"],
sdk_version: "test_current",
apex_available: ["myapex"],
compile_dex: true,
}
`,
},
{
name: "Updatable apex with stable dep",
bp: `
apex {
name: "myapex",
java_libs: ["myjar"],
key: "myapex.key",
updatable: true,
min_sdk_version: "29",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
java_library {
name: "myjar",
srcs: ["foo/bar/MyClass.java"],
sdk_version: "current",
apex_available: ["myapex"],
min_sdk_version: "29",
compile_dex: true,
}
`,
},
{
name: "Updatable apex with non-stable dep",
expectedError: "cannot depend on \"myjar\"",
bp: `
apex {
name: "myapex",
java_libs: ["myjar"],
key: "myapex.key",
updatable: true,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
java_library {
name: "myjar",
srcs: ["foo/bar/MyClass.java"],
sdk_version: "test_current",
apex_available: ["myapex"],
compile_dex: true,
}
`,
},
{
name: "Updatable apex with non-stable legacy core platform dep",
expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
bp: `
apex {
name: "myapex",
java_libs: ["myjar-uses-legacy"],
key: "myapex.key",
updatable: true,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
java_library {
name: "myjar-uses-legacy",
srcs: ["foo/bar/MyClass.java"],
sdk_version: "core_platform",
apex_available: ["myapex"],
compile_dex: true,
}
`,
preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
},
{
name: "Updatable apex with non-stable transitive dep",
// This is not actually detecting that the transitive dependency is unstable, rather it is
// detecting that the transitive dependency is building against a wider API surface than the
// module that depends on it is using.
expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
bp: `
apex {
name: "myapex",
java_libs: ["myjar"],
key: "myapex.key",
updatable: true,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
java_library {
name: "myjar",
srcs: ["foo/bar/MyClass.java"],
sdk_version: "current",
apex_available: ["myapex"],
static_libs: ["transitive-jar"],
compile_dex: true,
}
java_library {
name: "transitive-jar",
srcs: ["foo/bar/MyClass.java"],
sdk_version: "core_platform",
apex_available: ["myapex"],
}
`,
},
}
for _, test := range testCases {
if test.name != "Updatable apex with non-stable legacy core platform dep" {
continue
}
t.Run(test.name, func(t *testing.T) {
t.Parallel()
errorHandler := android.FixtureExpectsNoErrors
if test.expectedError != "" {
errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
}
android.GroupFixturePreparers(
java.PrepareForTestWithJavaDefaultModules,
PrepareForTestWithApexBuildComponents,
prepareForTestWithMyapex,
android.OptionalFixturePreparer(test.preparer),
).
ExtendWithErrorHandler(errorHandler).
RunTestWithBp(t, test.bp)
})
}
}
func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["mylib"],
min_sdk_version: "29",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
shared_libs: ["mylib2"],
system_shared_libs: [],
stl: "none",
apex_available: [
"myapex",
],
min_sdk_version: "29",
}
// indirect part of the apex
cc_library {
name: "mylib2",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: [
"myapex",
],
min_sdk_version: "30",
}
`)
}
func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
t.Parallel()
testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
apex {
name: "myapex",
key: "myapex.key",
apps: ["AppFoo"],
min_sdk_version: "29",
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
android_app {
name: "AppFoo",
srcs: ["foo/bar/MyClass.java"],
sdk_version: "current",
min_sdk_version: "29",
system_modules: "none",
stl: "none",
static_libs: ["bar"],
apex_available: [ "myapex" ],
}
java_library {
name: "bar",
sdk_version: "current",
srcs: ["a.java"],
apex_available: [ "myapex" ],
}
`)
}
func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["mylib"],
min_sdk_version: "29",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
// mylib will link to mylib2#current
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
shared_libs: ["mylib2"],
system_shared_libs: [],
stl: "none",
apex_available: ["myapex", "otherapex"],
min_sdk_version: "29",
}
cc_library {
name: "mylib2",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: ["otherapex"],
stubs: { versions: ["29", "30"] },
min_sdk_version: "30",
}
apex {
name: "otherapex",
key: "myapex.key",
native_shared_libs: ["mylib", "mylib2"],
min_sdk_version: "30",
}
`)
expectLink := func(from, from_variant, to, to_variant string) {
ld := ctx.ModuleForTests(t, from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
libFlags := ld.Args["libFlags"]
ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
}
expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
expectLink("mylib", "shared_apex30", "mylib2", "shared_current")
}
func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
t.Parallel()
withSAsActiveCodeNames := android.FixtureModifyProductVariables(
func(variables android.FixtureProductVariables) {
variables.Platform_sdk_codename = proptools.StringPtr("S")
variables.Platform_version_active_codenames = []string{"S"}
},
)
testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["libfoo"],
min_sdk_version: "S",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "libfoo",
shared_libs: ["libbar"],
apex_available: ["myapex"],
min_sdk_version: "29",
}
cc_library {
name: "libbar",
apex_available: ["myapex"],
}
`, withSAsActiveCodeNames)
}
func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
t.Parallel()
withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
variables.Platform_sdk_codename = proptools.StringPtr("S")
variables.Platform_version_active_codenames = []string{"S", "T"}
})
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["libfoo"],
min_sdk_version: "S",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "libfoo",
shared_libs: ["libbar"],
apex_available: ["myapex"],
min_sdk_version: "S",
}
cc_library {
name: "libbar",
stubs: {
symbol_file: "libbar.map.txt",
versions: ["30", "S", "T"],
},
}
`, withSAsActiveCodeNames)
// ensure libfoo is linked with current version of libbar stub
libfoo := ctx.ModuleForTests(t, "libfoo", "android_arm64_armv8-a_shared_apex10000")
libFlags := libfoo.Rule("ld").Args["libFlags"]
ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
}
func TestFilesInSubDir(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["mylib"],
binaries: ["mybin", "mybin.rust"],
prebuilts: ["myetc"],
compile_multilib: "both",
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
prebuilt_etc {
name: "myetc",
src: "myprebuilt",
sub_dir: "foo/bar",
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
relative_install_path: "foo/bar",
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
}
cc_binary {
name: "mybin",
srcs: ["mylib.cpp"],
relative_install_path: "foo/bar",
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
}
rust_binary {
name: "mybin.rust",
srcs: ["foo.rs"],
relative_install_path: "rust_subdir",
apex_available: [ "myapex" ],
}
`)
generateFsRule := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Rule("generateFsConfig")
cmd := generateFsRule.RuleParams.Command
// Ensure that the subdirectories are all listed
ensureContains(t, cmd, "/etc ")
ensureContains(t, cmd, "/etc/foo ")
ensureContains(t, cmd, "/etc/foo/bar ")
ensureContains(t, cmd, "/lib64 ")
ensureContains(t, cmd, "/lib64/foo ")
ensureContains(t, cmd, "/lib64/foo/bar ")
ensureContains(t, cmd, "/lib ")
ensureContains(t, cmd, "/lib/foo ")
ensureContains(t, cmd, "/lib/foo/bar ")
ensureContains(t, cmd, "/bin ")
ensureContains(t, cmd, "/bin/foo ")
ensureContains(t, cmd, "/bin/foo/bar ")
ensureContains(t, cmd, "/bin/rust_subdir ")
}
func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
multilib: {
both: {
native_shared_libs: ["mylib"],
binaries: ["mybin"],
},
},
compile_multilib: "both",
native_bridge_supported: true,
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib",
relative_install_path: "foo/bar",
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
native_bridge_supported: true,
}
cc_binary {
name: "mybin",
relative_install_path: "foo/bar",
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
native_bridge_supported: true,
compile_multilib: "both", // default is "first" for binary
multilib: {
lib64: {
suffix: "64",
},
},
}
`, android.PrepareForNativeBridgeEnabled)
ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
"bin/foo/bar/mybin",
"bin/foo/bar/mybin64",
"bin/arm/foo/bar/mybin",
"bin/arm64/foo/bar/mybin64",
"lib/foo/bar/mylib.so",
"lib/arm/foo/bar/mylib.so",
"lib64/foo/bar/mylib.so",
"lib64/arm64/foo/bar/mylib.so",
})
}
func TestVendorApex(t *testing.T) {
t.Parallel()
result := android.GroupFixturePreparers(
prepareForApexTest,
android.FixtureModifyConfig(android.SetKatiEnabledForTests),
).RunTestWithBp(t, `
apex {
name: "myapex",
key: "myapex.key",
binaries: ["mybin"],
vendor: true,
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_binary {
name: "mybin",
vendor: true,
shared_libs: ["libfoo"],
}
cc_library {
name: "libfoo",
proprietary: true,
}
`)
ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex", []string{
"bin/mybin",
"lib64/libfoo.so",
// TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
"lib64/libc++.so",
})
apexBundle := result.ModuleForTests(t, "myapex", "android_common_myapex").Module().(*apexBundle)
data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
name := apexBundle.BaseModuleName()
prefix := "TARGET_"
var builder strings.Builder
data.Custom(&builder, name, prefix, "", data)
androidMk := android.StringRelativeToTop(result.Config, builder.String())
installPath := "out/target/product/test_device/vendor/apex"
ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
apexManifestRule := result.ModuleForTests(t, "myapex", "android_common_myapex").Rule("apexManifestRule")
requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
ensureListNotContains(t, requireNativeLibs, ":vndk")
}
func TestProductVariant(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
updatable: false,
product_specific: true,
binaries: ["foo"],
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_binary {
name: "foo",
product_available: true,
apex_available: ["myapex"],
srcs: ["foo.cpp"],
}
`)
cflags := strings.Fields(
ctx.ModuleForTests(t, "foo", "android_product_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
ensureListContains(t, cflags, "-D__ANDROID_APEX__")
ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
}
func TestApex_withPrebuiltFirmware(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
additionalProp string
}{
{"system apex with prebuilt_firmware", ""},
{"vendor apex with prebuilt_firmware", "vendor: true,"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
prebuilts: ["myfirmware"],
updatable: false,
`+tc.additionalProp+`
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
prebuilt_firmware {
name: "myfirmware",
src: "myfirmware.bin",
filename_from_src: true,
`+tc.additionalProp+`
}
`)
ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
"etc/firmware/myfirmware.bin",
})
})
}
}
func TestApex_withPrebuiltKernelModules(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
kernel_modules: ["mykernelmodules"],
updatable: false,
vendor: true,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
prebuilt_kernel_modules {
name: "mykernelmodules",
srcs: ["*.ko"],
}
`,
withFiles(android.MockFS{
"mod1.ko": nil,
"mod2.ko": nil,
}))
ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
"lib/modules/modules.load",
"installs.zip unzips to lib/modules",
})
}
func TestAndroidMk_VendorApexRequired(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
vendor: true,
native_shared_libs: ["mylib"],
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib",
vendor_available: true,
}
`)
apexBundle := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Module().(*apexBundle)
data := android.AndroidMkDataForTest(t, ctx, apexBundle)
name := apexBundle.BaseModuleName()
prefix := "TARGET_"
var builder strings.Builder
data.Custom(&builder, name, prefix, "", data)
androidMk := builder.String()
ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++.vendor.myapex:64 mylib.vendor.myapex:64 libc.vendor libm.vendor libdl.vendor\n")
}
func TestAndroidMkWritesCommonProperties(t *testing.T) {
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
vintf_fragments: ["fragment.xml"],
init_rc: ["init.rc"],
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_binary {
name: "mybin",
}
`)
apexBundle := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Module().(*apexBundle)
data := android.AndroidMkDataForTest(t, ctx, apexBundle)
name := apexBundle.BaseModuleName()
prefix := "TARGET_"
var builder strings.Builder
data.Custom(&builder, name, prefix, "", data)
androidMk := builder.String()
ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
}
func TestStaticLinking(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["mylib"],
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
stubs: {
versions: ["1", "2", "3"],
},
apex_available: ["myapex"],
}
rust_ffi {
name: "libmylib_rs",
crate_name: "mylib_rs",
srcs: ["mylib.rs"],
stubs: {
versions: ["1", "2", "3"],
},
apex_available: ["myapex"],
}
cc_binary {
name: "not_in_apex",
srcs: ["mylib.cpp"],
static_libs: ["mylib", "libmylib_rs"],
static_executable: true,
system_shared_libs: [],
stl: "none",
}
`)
ldFlags := ctx.ModuleForTests(t, "not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
// Ensure that not_in_apex is linking with the static variant of mylib
ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
ensureContains(t, ldFlags, "generated_rust_staticlib/librustlibs.a")
}
func TestKeys(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex_keytest",
key: "myapex.key",
certificate: ":myapex.certificate",
native_shared_libs: ["mylib"],
file_contexts: ":myapex-file_contexts",
updatable: false,
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex_keytest" ],
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
android_app_certificate {
name: "myapex.certificate",
certificate: "testkey",
}
android_app_certificate {
name: "myapex.certificate.override",
certificate: "testkey.override",
}
`)
// check the APEX keys
keys := ctx.ModuleForTests(t, "myapex.key", "android_common").Module().(*apexKey)
if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
"vendor/foo/devkeys/testkey.avbpubkey")
}
if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
"vendor/foo/devkeys/testkey.pem")
}
// check the APK certs. It should be overridden to myapex.certificate.override
certs := ctx.ModuleForTests(t, "myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
if certs != "testkey.override.x509.pem testkey.override.pk8" {
t.Errorf("cert and private key %q are not %q", certs,
"testkey.override.509.pem testkey.override.pk8")
}
}
func TestCertificate(t *testing.T) {
t.Parallel()
t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}`)
rule := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Rule("signapk")
expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
if actual := rule.Args["certificates"]; actual != expected {
t.Errorf("certificates should be %q, not %q", expected, actual)
}
})
t.Run("override when unspecified", func(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex_keytest",
key: "myapex.key",
file_contexts: ":myapex-file_contexts",
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
android_app_certificate {
name: "myapex.certificate.override",
certificate: "testkey.override",
}`)
rule := ctx.ModuleForTests(t, "myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
expected := "testkey.override.x509.pem testkey.override.pk8"
if actual := rule.Args["certificates"]; actual != expected {
t.Errorf("certificates should be %q, not %q", expected, actual)
}
})
t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
certificate: ":myapex.certificate",
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
android_app_certificate {
name: "myapex.certificate",
certificate: "testkey",
}`)
rule := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Rule("signapk")
expected := "testkey.x509.pem testkey.pk8"
if actual := rule.Args["certificates"]; actual != expected {
t.Errorf("certificates should be %q, not %q", expected, actual)
}
})
t.Run("override when specifiec as <:module>", func(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex_keytest",
key: "myapex.key",
file_contexts: ":myapex-file_contexts",
certificate: ":myapex.certificate",
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
android_app_certificate {
name: "myapex.certificate.override",
certificate: "testkey.override",
}`)
rule := ctx.ModuleForTests(t, "myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
expected := "testkey.override.x509.pem testkey.override.pk8"
if actual := rule.Args["certificates"]; actual != expected {
t.Errorf("certificates should be %q, not %q", expected, actual)
}
})
t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
certificate: "testkey",
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}`,
android.MockFS{
"vendor/foo/devkeys/testkey.x509.pem": nil,
}.AddToFixture(),
)
rule := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Rule("signapk")
expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
if actual := rule.Args["certificates"]; actual != expected {
t.Errorf("certificates should be %q, not %q", expected, actual)
}
})
t.Run("override when specified as <name>", func(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex_keytest",
key: "myapex.key",
file_contexts: ":myapex-file_contexts",
certificate: "testkey",
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
android_app_certificate {
name: "myapex.certificate.override",
certificate: "testkey.override",
}`)
rule := ctx.ModuleForTests(t, "myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
expected := "testkey.override.x509.pem testkey.override.pk8"
if actual := rule.Args["certificates"]; actual != expected {
t.Errorf("certificates should be %q, not %q", expected, actual)
}
})
}
func TestMacro(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["mylib", "mylib2"],
updatable: false,
}
apex {
name: "otherapex",
key: "myapex.key",
native_shared_libs: ["mylib", "mylib2"],
min_sdk_version: "29",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: [
"myapex",
"otherapex",
],
recovery_available: true,
min_sdk_version: "29",
}
cc_library {
name: "mylib2",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: [
"myapex",
"otherapex",
],
static_libs: ["mylib3"],
recovery_available: true,
min_sdk_version: "29",
split_all_variants: true,
}
cc_library {
name: "mylib3",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: [
"myapex",
"otherapex",
],
recovery_available: true,
min_sdk_version: "29",
split_all_variants: true,
}
`)
// non-APEX variant does not have __ANDROID_APEX__ defined
mylibCFlags := ctx.ModuleForTests(t, "mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
// APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
mylibCFlags = ctx.ModuleForTests(t, "mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
// APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
mylibCFlags = ctx.ModuleForTests(t, "mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
// When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
// each variant defines additional macros to distinguish which apex variant it is built for
// non-APEX variant does not have __ANDROID_APEX__ defined
mylibCFlags = ctx.ModuleForTests(t, "mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
// recovery variant does not set __ANDROID_APEX__
mylibCFlags = ctx.ModuleForTests(t, "mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
// non-APEX variant does not have __ANDROID_APEX__ defined
mylibCFlags = ctx.ModuleForTests(t, "mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
// recovery variant does not set __ANDROID_APEX__
mylibCFlags = ctx.ModuleForTests(t, "mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
}
func TestHeaderLibsDependency(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["mylib"],
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library_headers {
name: "mylib_headers",
export_include_dirs: ["my_include"],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex" ],
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
header_libs: ["mylib_headers"],
export_header_lib_headers: ["mylib_headers"],
stubs: {
versions: ["1", "2", "3"],
},
apex_available: [ "myapex" ],
}
cc_library {
name: "otherlib",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
shared_libs: ["mylib"],
}
`)
cFlags := ctx.ModuleForTests(t, "otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
// Ensure that the include path of the header lib is exported to 'otherlib'
ensureContains(t, cFlags, "-Imy_include")
}
type fileInApex struct {
path string // path in apex
src string // src path
isLink bool
isZip bool
isRm bool
}
func (f fileInApex) String() string {
if f.isZip {
return f.src + " unzips to " + f.path
}
if f.isRm {
return "remove " + f.path
}
return f.src + ":" + f.path
}
func (f fileInApex) match(expectation string) bool {
if f.isZip {
if before, after, found := strings.Cut(expectation, " unzips to "); found {
matchSrc, _ := path.Match(before, f.src)
matchDst, _ := path.Match(after, f.path)
return matchSrc && matchDst
}
return false
}
if f.isRm {
if strings.HasPrefix(expectation, "remove ") {
match, _ := path.Match(strings.TrimPrefix(expectation, "remove "), f.path)
return match
}
return false
}
parts := strings.Split(expectation, ":")
if len(parts) == 1 {
match, _ := path.Match(parts[0], f.path)
return match
}
if len(parts) == 2 {
matchSrc, _ := path.Match(parts[0], f.src)
matchDst, _ := path.Match(parts[1], f.path)
return matchSrc && matchDst
}
panic("invalid expected file specification: " + expectation)
}
func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
t.Helper()
module := ctx.ModuleForTests(t, moduleName, variant)
apexRule := module.MaybeRule("apexRule")
apexDir := "/image.apex/"
copyCmds := apexRule.Args["copy_commands"]
var ret []fileInApex
for _, cmd := range strings.Split(copyCmds, "&&") {
cmd = strings.TrimSpace(cmd)
if cmd == "" {
continue
}
terms := strings.Split(cmd, " ")
var dst, src string
var isLink, isZip, isRm bool
switch {
case strings.HasSuffix(terms[0], "mkdir"):
case strings.HasSuffix(terms[0], "cp"):
if len(terms) != 3 && len(terms) != 4 {
t.Fatal("copyCmds contains invalid cp command", cmd)
}
dst = terms[len(terms)-1]
src = terms[len(terms)-2]
isLink = false
case strings.HasSuffix(terms[0], "ln"):
if len(terms) != 3 && len(terms) != 4 {
// ln LINK TARGET or ln -s LINK TARGET
t.Fatal("copyCmds contains invalid ln command", cmd)
}
dst = terms[len(terms)-1]
src = terms[len(terms)-2]
isLink = true
case strings.HasSuffix(terms[0], "zipsync"):
if len(terms) != 4 {
t.Fatal("copyCmds contains invalid zipsync command", cmd)
}
dst = terms[len(terms)-2]
src = path.Base(terms[len(terms)-1])
isZip = true
case strings.HasSuffix(terms[0], "rm"):
if len(terms) != 3 {
t.Fatal("copyCmds contains invalid rm command", cmd)
}
if terms[1] != "-rf" {
t.Fatal("copyCmds contains invalid rm command", cmd)
}
dst = terms[len(terms)-1]
isRm = true
default:
t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
}
if dst != "" {
index := strings.Index(dst, apexDir)
if index == -1 {
t.Fatal("copyCmds should copy a file to "+apexDir, cmd)
}
dstFile := dst[index+len(apexDir):]
ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink, isZip: isZip, isRm: isRm})
}
}
return ret
}
func assertFileListEquals(t *testing.T, expectedFiles []string, actualFiles []fileInApex) {
t.Helper()
var failed bool
var surplus []string
filesMatched := make(map[string]bool)
for _, file := range actualFiles {
matchFound := false
for _, expected := range expectedFiles {
if file.match(expected) {
matchFound = true
filesMatched[expected] = true
break
}
}
if !matchFound {
surplus = append(surplus, file.String())
}
}
if len(surplus) > 0 {
sort.Strings(surplus)
t.Log("surplus files", surplus)
failed = true
}
if len(expectedFiles) > len(filesMatched) {
var missing []string
for _, expected := range expectedFiles {
if !filesMatched[expected] {
missing = append(missing, expected)
}
}
sort.Strings(missing)
t.Log("missing files", missing)
failed = true
}
if failed {
t.Fail()
}
}
func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
assertFileListEquals(t, files, getFiles(t, ctx, moduleName, variant))
}
func ensureExactDeapexedContents(t *testing.T, ctx *android.TestContext, moduleName string, variant string, files []string) {
deapexer := ctx.ModuleForTests(t, moduleName, variant).Description("deapex")
outputs := make([]string, 0, len(deapexer.ImplicitOutputs)+1)
if deapexer.Output != nil {
outputs = append(outputs, deapexer.Output.String())
}
for _, output := range deapexer.ImplicitOutputs {
outputs = append(outputs, output.String())
}
actualFiles := make([]fileInApex, 0, len(outputs))
for _, output := range outputs {
dir := "/deapexer/"
pos := strings.LastIndex(output, dir)
if pos == -1 {
t.Fatal("Unknown deapexer output ", output)
}
path := output[pos+len(dir):]
actualFiles = append(actualFiles, fileInApex{path: path, src: "", isLink: false})
}
assertFileListEquals(t, files, actualFiles)
}
func vndkLibrariesTxtFiles(vers ...string) (result string) {
for _, v := range vers {
for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
result += `
prebuilt_etc {
name: "` + txt + `.libraries.` + v + `.txt",
src: "dummy.txt",
}
`
}
}
return
}
func TestVndkApexVersion(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex_vndk {
name: "com.android.vndk.v27",
key: "myapex.key",
file_contexts: ":myapex-file_contexts",
vndk_version: "27",
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
vndk_prebuilt_shared {
name: "libvndk27",
version: "27",
vendor_available: true,
product_available: true,
vndk: {
enabled: true,
},
target_arch: "arm64",
arch: {
arm: {
srcs: ["libvndk27_arm.so"],
},
arm64: {
srcs: ["libvndk27_arm64.so"],
},
},
apex_available: [ "com.android.vndk.v27" ],
}
vndk_prebuilt_shared {
name: "libvndk27",
version: "27",
vendor_available: true,
product_available: true,
vndk: {
enabled: true,
},
target_arch: "x86_64",
arch: {
x86: {
srcs: ["libvndk27_x86.so"],
},
x86_64: {
srcs: ["libvndk27_x86_64.so"],
},
},
}
`+vndkLibrariesTxtFiles("27"),
withFiles(map[string][]byte{
"libvndk27_arm.so": nil,
"libvndk27_arm64.so": nil,
"libvndk27_x86.so": nil,
"libvndk27_x86_64.so": nil,
}))
ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
"lib/libvndk27_arm.so",
"lib64/libvndk27_arm64.so",
"etc/*",
})
}
func TestVndkApexNameRule(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex_vndk {
name: "com.android.vndk.v29",
key: "myapex.key",
file_contexts: ":myapex-file_contexts",
vndk_version: "29",
updatable: false,
}
apex_vndk {
name: "com.android.vndk.v28",
key: "myapex.key",
file_contexts: ":myapex-file_contexts",
vndk_version: "28",
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}`+vndkLibrariesTxtFiles("28", "29"))
assertApexName := func(expected, moduleName string) {
module := ctx.ModuleForTests(t, moduleName, "android_common")
apexManifestRule := module.Rule("apexManifestRule")
ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
}
assertApexName("com.android.vndk.v29", "com.android.vndk.v29")
assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
}
func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
t.Parallel()
testApexError(t, `module "com.android.vndk.v30" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
apex_vndk {
name: "com.android.vndk.v30",
key: "com.android.vndk.v30.key",
file_contexts: ":myapex-file_contexts",
native_bridge_supported: true,
}
apex_key {
name: "com.android.vndk.v30.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
vndk_prebuilt_shared {
name: "libvndk",
version: "30",
target_arch: "arm",
srcs: ["mylib.cpp"],
vendor_available: true,
product_available: true,
native_bridge_supported: true,
vndk: {
enabled: true,
},
}
`)
}
func TestVndkApexWithBinder32(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex_vndk {
name: "com.android.vndk.v27",
key: "myapex.key",
file_contexts: ":myapex-file_contexts",
vndk_version: "27",
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
vndk_prebuilt_shared {
name: "libvndk27",
version: "27",
target_arch: "arm",
vendor_available: true,
product_available: true,
vndk: {
enabled: true,
},
arch: {
arm: {
srcs: ["libvndk27.so"],
}
},
}
vndk_prebuilt_shared {
name: "libvndk27",
version: "27",
target_arch: "arm",
binder32bit: true,
vendor_available: true,
product_available: true,
vndk: {
enabled: true,
},
arch: {
arm: {
srcs: ["libvndk27binder32.so"],
}
},
apex_available: [ "com.android.vndk.v27" ],
}
`+vndkLibrariesTxtFiles("27"),
withFiles(map[string][]byte{
"libvndk27.so": nil,
"libvndk27binder32.so": nil,
}),
withBinder32bit,
android.FixtureModifyConfig(func(config android.Config) {
target := android.Target{
Os: android.Android,
Arch: android.Arch{
ArchType: android.Arm,
ArchVariant: "armv7-a-neon",
Abi: []string{"armeabi-v7a"},
},
NativeBridge: android.NativeBridgeDisabled,
NativeBridgeHostArchName: "",
NativeBridgeRelativePath: "",
}
config.Targets[android.Android] = []android.Target{target}
config.AndroidFirstDeviceTarget = target
}),
)
ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
"lib/libvndk27binder32.so",
"etc/*",
})
}
func TestDependenciesInApexManifest(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex_nodep",
key: "myapex.key",
native_shared_libs: ["lib_nodep"],
compile_multilib: "both",
file_contexts: ":myapex-file_contexts",
updatable: false,
}
apex {
name: "myapex_dep",
key: "myapex.key",
native_shared_libs: ["lib_dep"],
compile_multilib: "both",
file_contexts: ":myapex-file_contexts",
updatable: false,
}
apex {
name: "myapex_provider",
key: "myapex.key",
native_shared_libs: ["libfoo"],
compile_multilib: "both",
file_contexts: ":myapex-file_contexts",
updatable: false,
}
apex {
name: "myapex_selfcontained",
key: "myapex.key",
native_shared_libs: ["lib_dep_on_bar", "libbar"],
compile_multilib: "both",
file_contexts: ":myapex-file_contexts",
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "lib_nodep",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: [ "myapex_nodep" ],
}
cc_library {
name: "lib_dep",
srcs: ["mylib.cpp"],
shared_libs: ["libfoo"],
system_shared_libs: [],
stl: "none",
apex_available: [
"myapex_dep",
"myapex_provider",
"myapex_selfcontained",
],
}
cc_library {
name: "lib_dep_on_bar",
srcs: ["mylib.cpp"],
shared_libs: ["libbar"],
system_shared_libs: [],
stl: "none",
apex_available: [
"myapex_selfcontained",
],
}
cc_library {
name: "libfoo",
srcs: ["mytest.cpp"],
stubs: {
versions: ["1"],
},
system_shared_libs: [],
stl: "none",
apex_available: [
"myapex_provider",
],
}
cc_library {
name: "libbar",
srcs: ["mytest.cpp"],
stubs: {
versions: ["1"],
},
system_shared_libs: [],
stl: "none",
apex_available: [
"myapex_selfcontained",
],
}
`)
var apexManifestRule android.TestingBuildParams
var provideNativeLibs, requireNativeLibs []string
apexManifestRule = ctx.ModuleForTests(t, "myapex_nodep", "android_common_myapex_nodep").Rule("apexManifestRule")
provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
ensureListEmpty(t, provideNativeLibs)
ensureListEmpty(t, requireNativeLibs)
apexManifestRule = ctx.ModuleForTests(t, "myapex_dep", "android_common_myapex_dep").Rule("apexManifestRule")
provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
ensureListEmpty(t, provideNativeLibs)
ensureListContains(t, requireNativeLibs, "libfoo.so")
apexManifestRule = ctx.ModuleForTests(t, "myapex_provider", "android_common_myapex_provider").Rule("apexManifestRule")
provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
ensureListContains(t, provideNativeLibs, "libfoo.so")
ensureListEmpty(t, requireNativeLibs)
apexManifestRule = ctx.ModuleForTests(t, "myapex_selfcontained", "android_common_myapex_selfcontained").Rule("apexManifestRule")
provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
ensureListContains(t, provideNativeLibs, "libbar.so")
ensureListEmpty(t, requireNativeLibs)
}
func TestOverrideApexManifestDefaultVersion(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["mylib"],
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: [
"//apex_available:platform",
"myapex",
],
}
`, android.FixtureMergeEnv(map[string]string{
"OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
}))
module := ctx.ModuleForTests(t, "myapex", "android_common_myapex")
apexManifestRule := module.Rule("apexManifestRule")
ensureContains(t, apexManifestRule.Args["default_version"], "1234")
}
func TestCompileMultilibProp(t *testing.T) {
t.Parallel()
testCases := []struct {
compileMultiLibProp string
containedLibs []string
notContainedLibs []string
}{
{
containedLibs: []string{
"image.apex/lib64/mylib.so",
"image.apex/lib/mylib.so",
},
compileMultiLibProp: `compile_multilib: "both",`,
},
{
containedLibs: []string{"image.apex/lib64/mylib.so"},
notContainedLibs: []string{"image.apex/lib/mylib.so"},
compileMultiLibProp: `compile_multilib: "first",`,
},
{
containedLibs: []string{"image.apex/lib64/mylib.so"},
notContainedLibs: []string{"image.apex/lib/mylib.so"},
// compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
},
{
containedLibs: []string{"image.apex/lib64/mylib.so"},
notContainedLibs: []string{"image.apex/lib/mylib.so"},
compileMultiLibProp: `compile_multilib: "64",`,
},
{
containedLibs: []string{"image.apex/lib/mylib.so"},
notContainedLibs: []string{"image.apex/lib64/mylib.so"},
compileMultiLibProp: `compile_multilib: "32",`,
},
}
for _, testCase := range testCases {
ctx := testApex(t, fmt.Sprintf(`
apex {
name: "myapex",
key: "myapex.key",
%s
native_shared_libs: ["mylib"],
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
apex_available: [
"//apex_available:platform",
"myapex",
],
}
`, testCase.compileMultiLibProp),
)
module := ctx.ModuleForTests(t, "myapex", "android_common_myapex")
apexRule := module.Rule("apexRule")
copyCmds := apexRule.Args["copy_commands"]
for _, containedLib := range testCase.containedLibs {
ensureContains(t, copyCmds, containedLib)
}
for _, notContainedLib := range testCase.notContainedLibs {
ensureNotContains(t, copyCmds, notContainedLib)
}
}
}
func TestNonTestApex(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["mylib_common"],
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib_common",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
apex_available: [
"//apex_available:platform",
"myapex",
],
}
`)
module := ctx.ModuleForTests(t, "myapex", "android_common_myapex")
apexRule := module.Rule("apexRule")
copyCmds := apexRule.Args["copy_commands"]
if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
t.Log("Apex was a test apex!")
t.Fail()
}
// Ensure that main rule creates an output
ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
// Ensure that apex variant is created for the direct dep
ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
// Ensure that both direct and indirect deps are copied into apex
ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
// Ensure that the platform variant ends with _shared
ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
if !ctx.ModuleForTests(t, "mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
t.Log("Found mylib_common not in any apex!")
t.Fail()
}
}
func TestTestApex(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex_test {
name: "myapex",
key: "myapex.key",
native_shared_libs: ["mylib_common_test"],
updatable: false,
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib_common_test",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
// TODO: remove //apex_available:platform
apex_available: [
"//apex_available:platform",
"myapex",
],
}
`)
module := ctx.ModuleForTests(t, "myapex", "android_common_myapex")
apexRule := module.Rule("apexRule")
copyCmds := apexRule.Args["copy_commands"]
if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
t.Log("Apex was not a test apex!")
t.Fail()
}
// Ensure that main rule creates an output
ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
// Ensure that apex variant is created for the direct dep
ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
// Ensure that both direct and indirect deps are copied into apex
ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
// Ensure that the platform variant ends with _shared
ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
}
func TestLibzVendorIsntStable(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
updatable: false,
binaries: ["mybin"],
}
apex {
name: "myvendorapex",
key: "myapex.key",
file_contexts: "myvendorapex_file_contexts",
vendor: true,
updatable: false,
binaries: ["mybin"],
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_binary {
name: "mybin",
vendor_available: true,
system_shared_libs: [],
stl: "none",
shared_libs: ["libz"],
apex_available: ["//apex_available:anyapex"],
}
cc_library {
name: "libz",
vendor_available: true,
system_shared_libs: [],
stl: "none",
stubs: {
versions: ["28", "30"],
},
target: {
vendor: {
no_stubs: true,
},
},
}
`, withFiles(map[string][]byte{
"myvendorapex_file_contexts": nil,
}))
// libz provides stubs for core variant.
{
ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
"bin/mybin",
})
apexManifestRule := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Rule("apexManifestRule")
android.AssertStringEquals(t, "should require libz", apexManifestRule.Args["requireNativeLibs"], "libz.so")
}
// libz doesn't provide stubs for vendor variant.
{
ensureExactContents(t, ctx, "myvendorapex", "android_common_myvendorapex", []string{
"bin/mybin",
"lib64/libz.so",
})
apexManifestRule := ctx.ModuleForTests(t, "myvendorapex", "android_common_myvendorapex").Rule("apexManifestRule")
android.AssertStringEquals(t, "should not require libz", apexManifestRule.Args["requireNativeLibs"], "")
}
}
func TestApexWithTarget(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
updatable: false,
multilib: {
first: {
native_shared_libs: ["mylib_common"],
}
},
target: {
android: {
multilib: {
first: {
native_shared_libs: ["mylib"],
}
}
},
host: {
multilib: {
first: {
native_shared_libs: ["mylib2"],
}
}
}
}
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
// TODO: remove //apex_available:platform
apex_available: [
"//apex_available:platform",
"myapex",
],
}
cc_library {
name: "mylib_common",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
compile_multilib: "first",
// TODO: remove //apex_available:platform
apex_available: [
"//apex_available:platform",
"myapex",
],
split_all_variants: true,
}
cc_library {
name: "mylib2",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
compile_multilib: "first",
split_all_variants: true,
}
`)
apexRule := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Rule("apexRule")
copyCmds := apexRule.Args["copy_commands"]
// Ensure that main rule creates an output
ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
// Ensure that apex variant is created for the direct dep
ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
// Ensure that both direct and indirect deps are copied into apex
ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
// Ensure that the platform variant ends with _shared
ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
}
func TestApexWithArch(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
updatable: false,
native_shared_libs: ["mylib.generic"],
arch: {
arm64: {
native_shared_libs: ["mylib.arm64"],
exclude_native_shared_libs: ["mylib.generic"],
},
x86_64: {
native_shared_libs: ["mylib.x64"],
exclude_native_shared_libs: ["mylib.generic"],
},
}
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
cc_library {
name: "mylib.generic",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
// TODO: remove //apex_available:platform
apex_available: [
"//apex_available:platform",
"myapex",
],
}
cc_library {
name: "mylib.arm64",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
// TODO: remove //apex_available:platform
apex_available: [
"//apex_available:platform",
"myapex",
],
}
cc_library {
name: "mylib.x64",
srcs: ["mylib.cpp"],
system_shared_libs: [],
stl: "none",
// TODO: remove //apex_available:platform
apex_available: [
"//apex_available:platform",
"myapex",
],
}
`)
apexRule := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Rule("apexRule")
copyCmds := apexRule.Args["copy_commands"]
// Ensure that apex variant is created for the direct dep
ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
// Ensure that both direct and indirect deps are copied into apex
ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
}
func TestApexWithShBinary(t *testing.T) {
t.Parallel()
ctx := testApex(t, `
apex {
name: "myapex",
key: "myapex.key",
sh_binaries: ["myscript"],
updatable: false,
compile_multilib: "both",
}
apex_key {
name: "myapex.key",
public_key: "testkey.avbpubkey",
private_key: "testkey.pem",
}
sh_binary {
name: "myscript",
src: "mylib.cpp",
filename: "myscript.sh",
sub_dir: "script",
}
`)
apexRule := ctx.ModuleForTests(t, "myapex", "android_common_myapex").Rule("apexRule") | |