From 5b0d4f3a30aa901f68987f7b2353ad2fc9c7d077 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Tue, 4 Apr 2023 23:22:11 +0000 Subject: [PATCH] Include NDK CRT object variants in the sysroot. The actual NDK distribution will overwrite these (they're branded with the platform's version information, which isn't what the NDK wants to brand apps with), but including them here makes it easier to iterate on the platform sysroot in situations that don't require a strictly correct NDK sysroot. Bug: None Test: None Change-Id: I22d4de9caa8753578a2327b1ece0deb005708b08 --- cc/ndk_sysroot.go | 7 +++++++ cc/object.go | 30 +++++++++++++++++++++++------- cc/object_test.go | 2 +- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/cc/ndk_sysroot.go b/cc/ndk_sysroot.go index 622558edf..dffc6c614 100644 --- a/cc/ndk_sysroot.go +++ b/cc/ndk_sysroot.go @@ -142,6 +142,13 @@ func (n *ndkSingleton) GenerateBuildActions(ctx android.SingletonContext) { staticLibInstallPaths, library.ndkSysrootPath) } } + + if object, ok := m.linker.(*objectLinker); ok { + if object.ndkSysrootPath != nil { + staticLibInstallPaths = append( + staticLibInstallPaths, object.ndkSysrootPath) + } + } } }) diff --git a/cc/object.go b/cc/object.go index ef4446746..d65cdea74 100644 --- a/cc/object.go +++ b/cc/object.go @@ -44,6 +44,10 @@ var ccObjectSdkMemberType = &librarySdkMemberType{ type objectLinker struct { *baseLinker Properties ObjectLinkerProperties + + // Location of the object in the sysroot. Empty if the object is not + // included in the NDK. + ndkSysrootPath android.Path } type objectBazelHandler struct { @@ -99,6 +103,10 @@ type ObjectLinkerProperties struct { // Indicates that this module is a CRT object. CRT objects will be split // into a variant per-API level between min_sdk_version and current. Crt *bool + + // Indicates that this module should not be included in the NDK sysroot. + // Only applies to CRT objects. Defaults to false. + Exclude_from_ndk_sysroot *bool } func newObject(hod android.HostOrDeviceSupported) *Module { @@ -268,17 +276,28 @@ func (object *objectLinker) link(ctx ModuleContext, objs = objs.Append(deps.Objs) - var outputFile android.Path + var output android.WritablePath builderFlags := flagsToBuilderFlags(flags) outputName := ctx.ModuleName() if !strings.HasSuffix(outputName, objectExtension) { outputName += objectExtension } - if len(objs.objFiles) == 1 && String(object.Properties.Linker_script) == "" { - output := android.PathForModuleOut(ctx, outputName) - outputFile = output + // isForPlatform is terribly named and actually means isNotApex. + if Bool(object.Properties.Crt) && + !Bool(object.Properties.Exclude_from_ndk_sysroot) && ctx.useSdk() && + ctx.isSdkVariant() && ctx.isForPlatform() { + output = getVersionedLibraryInstallPath(ctx, + nativeApiLevelOrPanic(ctx, ctx.sdkVersion())).Join(ctx, outputName) + object.ndkSysrootPath = output + } else { + output = android.PathForModuleOut(ctx, outputName) + } + + outputFile := output + + if len(objs.objFiles) == 1 && String(object.Properties.Linker_script) == "" { if String(object.Properties.Prefix_symbols) != "" { transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), objs.objFiles[0], builderFlags, output) @@ -290,9 +309,6 @@ func (object *objectLinker) link(ctx ModuleContext, }) } } else { - output := android.PathForModuleOut(ctx, outputName) - outputFile = output - if String(object.Properties.Prefix_symbols) != "" { input := android.PathForModuleOut(ctx, "unprefixed", outputName) transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), input, diff --git a/cc/object_test.go b/cc/object_test.go index 5359a357a..b1e2a0fd2 100644 --- a/cc/object_test.go +++ b/cc/object_test.go @@ -65,7 +65,7 @@ func TestUseCrtObjectOfCorrectVersion(t *testing.T) { variant := "android_arm64_armv8-a_sdk" crt := ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"] android.AssertStringDoesContain(t, "crt dep of sdk variant", crt, - variant+"_29/crtbegin_dynamic.o") + "29/crtbegin_dynamic.o") // platform variant uses the crt object built for platform variant = "android_arm64_armv8-a"