Handle cc_library shared/static only properties

Also handle whole_static_libs via a different attribute
(whole_archive_deps), separating these dependencies from regular static
deps.

Test: Build //bionic/libdl with bazel in conjunction with bzl
changes

Change-Id: I45019b6127a0d2731592ec35537ca15e77001cc2
This commit is contained in:
Chris Parsons 2021-05-06 16:23:19 -04:00
parent d8561166eb
commit 0864831019
4 changed files with 190 additions and 73 deletions

View file

@ -257,24 +257,74 @@ cc_library {
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
dir: "foo/bar", dir: "foo/bar",
filesystem: map[string]string{ filesystem: map[string]string{
"foo/bar/a.cpp": "", "foo/bar/both.cpp": "",
"foo/bar/sharedonly.cpp": "",
"foo/bar/staticonly.cpp": "",
"foo/bar/Android.bp": ` "foo/bar/Android.bp": `
cc_library { cc_library {
name: "a", name: "a",
shared: { whole_static_libs: ["b"] }, srcs: ["both.cpp"],
static: { srcs: ["a.cpp"] }, cflags: ["bothflag"],
shared_libs: ["shared_dep_for_both"],
static_libs: ["static_dep_for_both"],
whole_static_libs: ["whole_static_lib_for_both"],
static: {
srcs: ["staticonly.cpp"],
cflags: ["staticflag"],
shared_libs: ["shared_dep_for_static"],
static_libs: ["static_dep_for_static"],
whole_static_libs: ["whole_static_lib_for_static"],
},
shared: {
srcs: ["sharedonly.cpp"],
cflags: ["sharedflag"],
shared_libs: ["shared_dep_for_shared"],
static_libs: ["static_dep_for_shared"],
whole_static_libs: ["whole_static_lib_for_shared"],
},
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }
cc_library_static { name: "b" } cc_library_static { name: "static_dep_for_shared" }
cc_library_static { name: "static_dep_for_static" }
cc_library_static { name: "static_dep_for_both" }
cc_library_static { name: "whole_static_lib_for_shared" }
cc_library_static { name: "whole_static_lib_for_static" }
cc_library_static { name: "whole_static_lib_for_both" }
cc_library { name: "shared_dep_for_shared" }
cc_library { name: "shared_dep_for_static" }
cc_library { name: "shared_dep_for_both" }
`, `,
}, },
bp: soongCcLibraryPreamble, bp: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{`cc_library(
name = "a", name = "a",
copts = ["-Ifoo/bar"], copts = [
srcs = ["a.cpp"], "bothflag",
static_deps_for_shared = [":b"], "-Ifoo/bar",
],
deps = [":static_dep_for_both"],
dynamic_deps = [":shared_dep_for_both"],
dynamic_deps_for_shared = [":shared_dep_for_shared"],
dynamic_deps_for_static = [":shared_dep_for_static"],
shared_copts = ["sharedflag"],
shared_srcs = ["sharedonly.cpp"],
srcs = ["both.cpp"],
static_copts = ["staticflag"],
static_deps_for_shared = [":static_dep_for_shared"],
static_deps_for_static = [":static_dep_for_static"],
static_srcs = ["staticonly.cpp"],
whole_archive_deps = [":whole_static_lib_for_both"],
whole_archive_deps_for_shared = [":whole_static_lib_for_shared"],
whole_archive_deps_for_static = [":whole_static_lib_for_static"],
)`}, )`},
}, },
{ {

View file

@ -189,8 +189,6 @@ cc_library_static {
":header_lib_2", ":header_lib_2",
":static_lib_1", ":static_lib_1",
":static_lib_2", ":static_lib_2",
":whole_static_lib_1",
":whole_static_lib_2",
], ],
includes = [ includes = [
"export_include_dir_1", "export_include_dir_1",
@ -201,6 +199,10 @@ cc_library_static {
"foo_static1.cc", "foo_static1.cc",
"foo_static2.cc", "foo_static2.cc",
], ],
whole_archive_deps = [
":whole_static_lib_1",
":whole_static_lib_2",
],
)`, `cc_library_static( )`, `cc_library_static(
name = "static_lib_1", name = "static_lib_1",
copts = ["-I."], copts = ["-I."],
@ -423,13 +425,14 @@ cc_library_static {
name = "foo_static", name = "foo_static",
copts = ["-I."], copts = ["-I."],
deps = select({ deps = select({
"//build/bazel/platforms/arch:arm64": [ "//build/bazel/platforms/arch:arm64": [":static_dep"],
":static_dep",
":static_dep2",
],
"//conditions:default": [], "//conditions:default": [],
}), }),
linkstatic = True, linkstatic = True,
whole_archive_deps = select({
"//build/bazel/platforms/arch:arm64": [":static_dep2"],
"//conditions:default": [],
}),
)`, `cc_library_static( )`, `cc_library_static(
name = "static_dep", name = "static_dep",
copts = ["-I."], copts = ["-I."],
@ -458,13 +461,14 @@ cc_library_static {
name = "foo_static", name = "foo_static",
copts = ["-I."], copts = ["-I."],
deps = select({ deps = select({
"//build/bazel/platforms/os:android": [ "//build/bazel/platforms/os:android": [":static_dep"],
":static_dep",
":static_dep2",
],
"//conditions:default": [], "//conditions:default": [],
}), }),
linkstatic = True, linkstatic = True,
whole_archive_deps = select({
"//build/bazel/platforms/os:android": [":static_dep2"],
"//conditions:default": [],
}),
)`, `cc_library_static( )`, `cc_library_static(
name = "static_dep", name = "static_dep",
copts = ["-I."], copts = ["-I."],
@ -497,10 +501,7 @@ cc_library_static {
expectedBazelTargets: []string{`cc_library_static( expectedBazelTargets: []string{`cc_library_static(
name = "foo_static", name = "foo_static",
copts = ["-I."], copts = ["-I."],
deps = [ deps = [":static_dep"] + select({
":static_dep",
":static_dep2",
] + select({
"//build/bazel/platforms/arch:arm64": [":static_dep4"], "//build/bazel/platforms/arch:arm64": [":static_dep4"],
"//conditions:default": [], "//conditions:default": [],
}) + select({ }) + select({
@ -508,6 +509,7 @@ cc_library_static {
"//conditions:default": [], "//conditions:default": [],
}), }),
linkstatic = True, linkstatic = True,
whole_archive_deps = [":static_dep2"],
)`, `cc_library_static( )`, `cc_library_static(
name = "static_dep", name = "static_dep",
copts = ["-I."], copts = ["-I."],
@ -732,8 +734,7 @@ cc_library_static {
cc_library_static { name: "static_dep" } cc_library_static { name: "static_dep" }
cc_library_static { cc_library_static {
name: "foo_static", name: "foo_static",
static_libs: ["static_dep"], static_libs: ["static_dep", "static_dep"],
whole_static_libs: ["static_dep"],
}`, }`,
expectedBazelTargets: []string{`cc_library_static( expectedBazelTargets: []string{`cc_library_static(
name = "foo_static", name = "foo_static",

View file

@ -85,7 +85,11 @@ func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) {
} }
type sharedAttributes struct { type sharedAttributes struct {
staticDeps bazel.LabelListAttribute copts bazel.StringListAttribute
srcs bazel.LabelListAttribute
staticDeps bazel.LabelListAttribute
dynamicDeps bazel.LabelListAttribute
wholeArchiveDeps bazel.LabelListAttribute
} }
// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library. // bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
@ -95,17 +99,35 @@ func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module)
return sharedAttributes{} return sharedAttributes{}
} }
var staticDeps bazel.LabelListAttribute copts := bazel.StringListAttribute{Value: lib.SharedProperties.Shared.Cflags}
staticDeps.Value = android.BazelLabelForModuleDeps(ctx, lib.SharedProperties.Shared.Whole_static_libs) srcs := bazel.LabelListAttribute{
Value: android.BazelLabelForModuleSrc(ctx, lib.SharedProperties.Shared.Srcs)}
staticDeps := bazel.LabelListAttribute{
Value: android.BazelLabelForModuleDeps(ctx, lib.SharedProperties.Shared.Static_libs)}
dynamicDeps := bazel.LabelListAttribute{
Value: android.BazelLabelForModuleDeps(ctx, lib.SharedProperties.Shared.Shared_libs)}
wholeArchiveDeps := bazel.LabelListAttribute{
Value: android.BazelLabelForModuleDeps(ctx, lib.SharedProperties.Shared.Whole_static_libs)}
return sharedAttributes{ return sharedAttributes{
staticDeps: staticDeps, copts: copts,
srcs: srcs,
staticDeps: staticDeps,
dynamicDeps: dynamicDeps,
wholeArchiveDeps: wholeArchiveDeps,
} }
} }
type staticAttributes struct { type staticAttributes struct {
srcs bazel.LabelListAttribute copts bazel.StringListAttribute
srcs bazel.LabelListAttribute
staticDeps bazel.LabelListAttribute
dynamicDeps bazel.LabelListAttribute
wholeArchiveDeps bazel.LabelListAttribute
} }
// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library. // bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
@ -115,11 +137,26 @@ func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module)
return staticAttributes{} return staticAttributes{}
} }
var srcs bazel.LabelListAttribute copts := bazel.StringListAttribute{Value: lib.StaticProperties.Static.Cflags}
srcs.Value = android.BazelLabelForModuleSrc(ctx, lib.StaticProperties.Static.Srcs)
srcs := bazel.LabelListAttribute{
Value: android.BazelLabelForModuleSrc(ctx, lib.StaticProperties.Static.Srcs)}
staticDeps := bazel.LabelListAttribute{
Value: android.BazelLabelForModuleDeps(ctx, lib.StaticProperties.Static.Static_libs)}
dynamicDeps := bazel.LabelListAttribute{
Value: android.BazelLabelForModuleDeps(ctx, lib.StaticProperties.Static.Shared_libs)}
wholeArchiveDeps := bazel.LabelListAttribute{
Value: android.BazelLabelForModuleDeps(ctx, lib.StaticProperties.Static.Whole_static_libs)}
return staticAttributes{ return staticAttributes{
srcs: srcs, copts: copts,
srcs: srcs,
staticDeps: staticDeps,
dynamicDeps: dynamicDeps,
wholeArchiveDeps: wholeArchiveDeps,
} }
} }
@ -246,10 +283,11 @@ func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Modul
// Convenience struct to hold all attributes parsed from linker properties. // Convenience struct to hold all attributes parsed from linker properties.
type linkerAttributes struct { type linkerAttributes struct {
deps bazel.LabelListAttribute deps bazel.LabelListAttribute
dynamicDeps bazel.LabelListAttribute dynamicDeps bazel.LabelListAttribute
linkopts bazel.StringListAttribute wholeArchiveDeps bazel.LabelListAttribute
versionScript bazel.LabelAttribute linkopts bazel.StringListAttribute
versionScript bazel.LabelAttribute
} }
// FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here // FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here
@ -266,6 +304,7 @@ func getBp2BuildLinkerFlags(linkerProperties *BaseLinkerProperties) []string {
func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes { func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes {
var deps bazel.LabelListAttribute var deps bazel.LabelListAttribute
var dynamicDeps bazel.LabelListAttribute var dynamicDeps bazel.LabelListAttribute
var wholeArchiveDeps bazel.LabelListAttribute
var linkopts bazel.StringListAttribute var linkopts bazel.StringListAttribute
var versionScript bazel.LabelAttribute var versionScript bazel.LabelAttribute
@ -274,11 +313,11 @@ func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module)
libs := baseLinkerProps.Header_libs libs := baseLinkerProps.Header_libs
libs = append(libs, baseLinkerProps.Export_header_lib_headers...) libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
libs = append(libs, baseLinkerProps.Static_libs...) libs = append(libs, baseLinkerProps.Static_libs...)
libs = append(libs, baseLinkerProps.Whole_static_libs...) wholeArchiveLibs := baseLinkerProps.Whole_static_libs
libs = android.SortedUniqueStrings(libs) libs = android.SortedUniqueStrings(libs)
deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs)) deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs))
linkopts.Value = getBp2BuildLinkerFlags(baseLinkerProps) linkopts.Value = getBp2BuildLinkerFlags(baseLinkerProps)
wholeArchiveDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
if baseLinkerProps.Version_script != nil { if baseLinkerProps.Version_script != nil {
versionScript.Value = android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script) versionScript.Value = android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)
@ -296,11 +335,11 @@ func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module)
libs := baseLinkerProps.Header_libs libs := baseLinkerProps.Header_libs
libs = append(libs, baseLinkerProps.Export_header_lib_headers...) libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
libs = append(libs, baseLinkerProps.Static_libs...) libs = append(libs, baseLinkerProps.Static_libs...)
libs = append(libs, baseLinkerProps.Whole_static_libs...) wholeArchiveLibs := baseLinkerProps.Whole_static_libs
libs = android.SortedUniqueStrings(libs) libs = android.SortedUniqueStrings(libs)
deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs)) deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs))
linkopts.SetValueForArch(arch.Name, getBp2BuildLinkerFlags(baseLinkerProps)) linkopts.SetValueForArch(arch.Name, getBp2BuildLinkerFlags(baseLinkerProps))
wholeArchiveDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
if baseLinkerProps.Version_script != nil { if baseLinkerProps.Version_script != nil {
versionScript.SetValueForArch(arch.Name, versionScript.SetValueForArch(arch.Name,
@ -317,8 +356,9 @@ func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module)
libs := baseLinkerProps.Header_libs libs := baseLinkerProps.Header_libs
libs = append(libs, baseLinkerProps.Export_header_lib_headers...) libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
libs = append(libs, baseLinkerProps.Static_libs...) libs = append(libs, baseLinkerProps.Static_libs...)
libs = append(libs, baseLinkerProps.Whole_static_libs...) wholeArchiveLibs := baseLinkerProps.Whole_static_libs
libs = android.SortedUniqueStrings(libs) libs = android.SortedUniqueStrings(libs)
wholeArchiveDeps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
deps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, libs)) deps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, libs))
linkopts.SetValueForOS(os.Name, getBp2BuildLinkerFlags(baseLinkerProps)) linkopts.SetValueForOS(os.Name, getBp2BuildLinkerFlags(baseLinkerProps))
@ -329,10 +369,11 @@ func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module)
} }
return linkerAttributes{ return linkerAttributes{
deps: deps, deps: deps,
dynamicDeps: dynamicDeps, dynamicDeps: dynamicDeps,
linkopts: linkopts, wholeArchiveDeps: wholeArchiveDeps,
versionScript: versionScript, linkopts: linkopts,
versionScript: versionScript,
} }
} }

View file

@ -220,16 +220,29 @@ func RegisterLibraryBuildComponents(ctx android.RegistrationContext) {
// For bp2build conversion. // For bp2build conversion.
type bazelCcLibraryAttributes struct { type bazelCcLibraryAttributes struct {
Srcs bazel.LabelListAttribute // Attributes pertaining to both static and shared variants.
Hdrs bazel.LabelListAttribute Srcs bazel.LabelListAttribute
Copts bazel.StringListAttribute Hdrs bazel.LabelListAttribute
Linkopts bazel.StringListAttribute Deps bazel.LabelListAttribute
Deps bazel.LabelListAttribute Dynamic_deps bazel.LabelListAttribute
Dynamic_deps bazel.LabelListAttribute Whole_archive_deps bazel.LabelListAttribute
User_link_flags bazel.StringListAttribute Copts bazel.StringListAttribute
Includes bazel.StringListAttribute Includes bazel.StringListAttribute
Static_deps_for_shared bazel.LabelListAttribute Linkopts bazel.StringListAttribute
Version_script bazel.LabelAttribute // Attributes pertaining to shared variant.
Shared_copts bazel.StringListAttribute
Shared_srcs bazel.LabelListAttribute
Static_deps_for_shared bazel.LabelListAttribute
Dynamic_deps_for_shared bazel.LabelListAttribute
Whole_archive_deps_for_shared bazel.LabelListAttribute
User_link_flags bazel.StringListAttribute
Version_script bazel.LabelAttribute
// Attributes pertaining to static variant.
Static_copts bazel.StringListAttribute
Static_srcs bazel.LabelListAttribute
Static_deps_for_static bazel.LabelListAttribute
Dynamic_deps_for_static bazel.LabelListAttribute
Whole_archive_deps_for_static bazel.LabelListAttribute
} }
type bazelCcLibrary struct { type bazelCcLibrary struct {
@ -276,17 +289,26 @@ func CcLibraryBp2Build(ctx android.TopDownMutatorContext) {
var srcs bazel.LabelListAttribute var srcs bazel.LabelListAttribute
srcs.Append(compilerAttrs.srcs) srcs.Append(compilerAttrs.srcs)
srcs.Append(staticAttrs.srcs)
attrs := &bazelCcLibraryAttributes{ attrs := &bazelCcLibraryAttributes{
Srcs: srcs, Srcs: srcs,
Copts: compilerAttrs.copts, Deps: linkerAttrs.deps,
Linkopts: linkerAttrs.linkopts, Dynamic_deps: linkerAttrs.dynamicDeps,
Deps: linkerAttrs.deps, Whole_archive_deps: linkerAttrs.wholeArchiveDeps,
Dynamic_deps: linkerAttrs.dynamicDeps, Copts: compilerAttrs.copts,
Version_script: linkerAttrs.versionScript, Includes: exportedIncludes,
Static_deps_for_shared: sharedAttrs.staticDeps, Linkopts: linkerAttrs.linkopts,
Includes: exportedIncludes, Shared_copts: sharedAttrs.copts,
Shared_srcs: sharedAttrs.srcs,
Static_deps_for_shared: sharedAttrs.staticDeps,
Whole_archive_deps_for_shared: sharedAttrs.wholeArchiveDeps,
Dynamic_deps_for_shared: sharedAttrs.dynamicDeps,
Version_script: linkerAttrs.versionScript,
Static_copts: staticAttrs.copts,
Static_srcs: staticAttrs.srcs,
Static_deps_for_static: staticAttrs.staticDeps,
Whole_archive_deps_for_static: staticAttrs.wholeArchiveDeps,
Dynamic_deps_for_static: staticAttrs.dynamicDeps,
} }
props := bazel.BazelTargetModuleProperties{ props := bazel.BazelTargetModuleProperties{
@ -2194,13 +2216,14 @@ func maybeInjectBoringSSLHash(ctx android.ModuleContext, outputFile android.Modu
} }
type bazelCcLibraryStaticAttributes struct { type bazelCcLibraryStaticAttributes struct {
Copts bazel.StringListAttribute Copts bazel.StringListAttribute
Srcs bazel.LabelListAttribute Srcs bazel.LabelListAttribute
Deps bazel.LabelListAttribute Deps bazel.LabelListAttribute
Linkopts bazel.StringListAttribute Whole_archive_deps bazel.LabelListAttribute
Linkstatic bool Linkopts bazel.StringListAttribute
Includes bazel.StringListAttribute Linkstatic bool
Hdrs bazel.LabelListAttribute Includes bazel.StringListAttribute
Hdrs bazel.LabelListAttribute
} }
type bazelCcLibraryStatic struct { type bazelCcLibraryStatic struct {
@ -2221,9 +2244,11 @@ func ccLibraryStaticBp2BuildInternal(ctx android.TopDownMutatorContext, module *
exportedIncludes := bp2BuildParseExportedIncludes(ctx, module) exportedIncludes := bp2BuildParseExportedIncludes(ctx, module)
attrs := &bazelCcLibraryStaticAttributes{ attrs := &bazelCcLibraryStaticAttributes{
Copts: compilerAttrs.copts, Copts: compilerAttrs.copts,
Srcs: compilerAttrs.srcs, Srcs: compilerAttrs.srcs,
Deps: linkerAttrs.deps, Deps: linkerAttrs.deps,
Whole_archive_deps: linkerAttrs.wholeArchiveDeps,
Linkopts: linkerAttrs.linkopts, Linkopts: linkerAttrs.linkopts,
Linkstatic: true, Linkstatic: true,
Includes: exportedIncludes, Includes: exportedIncludes,