diff --git a/cc/coverage.go b/cc/coverage.go index c0f697398..cbd8a6f3e 100644 --- a/cc/coverage.go +++ b/cc/coverage.go @@ -48,6 +48,7 @@ func (cov *coverage) props() []interface{} { func getGcovProfileLibraryName(ctx ModuleContextIntf) string { // This function should only ever be called for a cc.Module, so the // following statement should always succeed. + // LINT.IfChange if ctx.useSdk() { return "libprofile-extras_ndk" } else { @@ -63,6 +64,7 @@ func getClangProfileLibraryName(ctx ModuleContextIntf) string { } else { return "libprofile-clang-extras" } + // LINT.ThenChange(library.go) } func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps { diff --git a/cc/library.go b/cc/library.go index aec6433d8..0cdc9fad6 100644 --- a/cc/library.go +++ b/cc/library.go @@ -32,6 +32,20 @@ import ( "github.com/google/blueprint/proptools" ) +var ( + alwaysLinkLibraries = map[string]bool{ + // Coverage libraries are _always_ added as a whole_static_dep. By converting as these as + // alwayslink = True, we can add these as to deps (e.g. as a regular static dep) in Bazel + // without any extra complications in cc_shared_library roots to prevent linking the same + // library repeatedly. + "libprofile-extras_ndk": true, + "libprofile-extras": true, + "libprofile-clang-extras_ndk": true, + "libprofile-clang-extras_cfi_support": true, + "libprofile-clang-extras": true, + } +) + // LibraryProperties is a collection of properties shared by cc library rules/cc. type LibraryProperties struct { // local file name to pass to the linker as -unexported_symbols_list @@ -435,6 +449,10 @@ func libraryBp2Build(ctx android.TopDownMutatorContext, m *Module) { Bzl_load_location: "//build/bazel/rules/cc:cc_library_shared.bzl", } + if _, ok := alwaysLinkLibraries[m.Name()]; ok { + staticTargetAttrs.Alwayslink = proptools.BoolPtr(true) + } + var tagsForStaticVariant bazel.StringListAttribute if compilerAttrs.stubsSymbolFile == nil && len(compilerAttrs.stubsVersions.Value) == 0 { tagsForStaticVariant = android.ApexAvailableTagsWithoutTestApexes(ctx, m) @@ -2947,6 +2965,10 @@ func sharedOrStaticLibraryBp2Build(ctx android.TopDownMutatorContext, module *Mo var attrs interface{} if isStatic { commonAttrs.Deps.Add(baseAttributes.protoDependency) + var alwayslink *bool + if _, ok := alwaysLinkLibraries[module.Name()]; ok && isStatic { + alwayslink = proptools.BoolPtr(true) + } attrs = &bazelCcLibraryStaticAttributes{ staticOrSharedAttributes: commonAttrs, Rtti: compilerAttrs.rtti, @@ -2960,8 +2982,10 @@ func sharedOrStaticLibraryBp2Build(ctx android.TopDownMutatorContext, module *Mo Conlyflags: compilerAttrs.conlyFlags, Asflags: asFlags, - Features: *features, + Alwayslink: alwayslink, + Features: *features, } + } else { commonAttrs.Dynamic_deps.Add(baseAttributes.protoDependency) @@ -3043,7 +3067,8 @@ type bazelCcLibraryStaticAttributes struct { Conlyflags bazel.StringListAttribute Asflags bazel.StringListAttribute - Features bazel.StringListAttribute + Alwayslink *bool + Features bazel.StringListAttribute } // TODO(b/199902614): Can this be factored to share with the other Attributes?