diff --git a/apex/apex_test.go b/apex/apex_test.go index 77117c550..dd5bf7082 100644 --- a/apex/apex_test.go +++ b/apex/apex_test.go @@ -41,6 +41,7 @@ func testApex(t *testing.T, bp string) *android.TestContext { ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory)) ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(cc.LibrarySharedFactory)) + ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(cc.BinaryFactory)) ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory)) ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory)) ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory)) @@ -614,3 +615,54 @@ func TestUseVendor(t *testing.T) { ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so") ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so") } + +func TestStaticLinking(t *testing.T) { + ctx := testApex(t, ` + apex { + name: "myapex", + key: "myapex.key", + native_shared_libs: ["mylib"], + } + + 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"], + }, + } + + cc_binary { + name: "not_in_apex", + srcs: ["mylib.cpp"], + static_libs: ["mylib"], + static_executable: true, + system_shared_libs: [], + stl: "none", + } + + cc_object { + name: "crtbegin_static", + stl: "none", + } + + cc_object { + name: "crtend_android", + stl: "none", + } + + `) + + ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").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_core_static/mylib.a") +} diff --git a/cc/binary.go b/cc/binary.go index bc9abfe4b..c9e6cab50 100644 --- a/cc/binary.go +++ b/cc/binary.go @@ -51,12 +51,12 @@ type BinaryLinkerProperties struct { } func init() { - android.RegisterModuleType("cc_binary", binaryFactory) + android.RegisterModuleType("cc_binary", BinaryFactory) android.RegisterModuleType("cc_binary_host", binaryHostFactory) } // Module factory for binaries -func binaryFactory() android.Module { +func BinaryFactory() android.Module { module, _ := NewBinary(android.HostAndDeviceSupported) return module.Init() } diff --git a/cc/cc.go b/cc/cc.go index 139e85feb..c3e554cbd 100644 --- a/cc/cc.go +++ b/cc/cc.go @@ -1419,7 +1419,12 @@ func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps { } if t, ok := depTag.(dependencyTag); ok && t.library { - if dependentLibrary, ok := ccDep.linker.(*libraryDecorator); ok { + depIsStatic := false + switch depTag { + case staticDepTag, staticExportDepTag, lateStaticDepTag, wholeStaticDepTag: + depIsStatic = true + } + if dependentLibrary, ok := ccDep.linker.(*libraryDecorator); ok && !depIsStatic { depIsStubs := dependentLibrary.buildStubs() depHasStubs := ccDep.HasStubsVariants() depInSameApex := android.DirectlyInApex(c.ApexName(), depName) diff --git a/cc/cc_test.go b/cc/cc_test.go index 29974c99a..96233a10e 100644 --- a/cc/cc_test.go +++ b/cc/cc_test.go @@ -53,7 +53,7 @@ func TestMain(m *testing.M) { func createTestContext(t *testing.T, config android.Config, bp string) *android.TestContext { ctx := android.NewTestArchContext() - ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(binaryFactory)) + ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(BinaryFactory)) ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(LibraryFactory)) ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(LibrarySharedFactory)) ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(LibraryHeaderFactory))