bp2build: group shared/static attrs into a struct.

This makes bp2build generate these attrs into a Starlark dictionary,
passed into the cc_library macro directly. This makes the BUILD target
representation more similar to the Android.bp one, and also makes
it more legible.

Test: TH
Change-Id: I42b427cc4b22c6376d3d24e40b9af1692bb0c692
This commit is contained in:
Jingwen Chen 2021-06-11 12:51:48 +00:00
parent 3420b834d8
commit c4dc9b4f08
3 changed files with 130 additions and 143 deletions

View file

@ -385,19 +385,23 @@ cc_library { name: "shared_dep_for_both" }
"-I$(BINDIR)/foo/bar",
],
dynamic_deps = [":shared_dep_for_both"],
dynamic_deps_for_shared = [":shared_dep_for_shared"],
dynamic_deps_for_static = [":shared_dep_for_static"],
implementation_deps = [":static_dep_for_both"],
shared_copts = ["sharedflag"],
shared_srcs = ["sharedonly.cpp"],
shared = {
"copts": ["sharedflag"],
"dynamic_deps": [":shared_dep_for_shared"],
"srcs": ["sharedonly.cpp"],
"static_deps": [":static_dep_for_shared"],
"whole_archive_deps": [":whole_static_lib_for_shared"],
},
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"],
static = {
"copts": ["staticflag"],
"dynamic_deps": [":shared_dep_for_static"],
"srcs": ["staticonly.cpp"],
"static_deps": [":static_dep_for_static"],
"whole_archive_deps": [":whole_static_lib_for_static"],
},
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"],
)`},
})
}
@ -437,9 +441,13 @@ cc_prebuilt_library_static { name: "whole_static_lib_for_both" }
"-Ifoo/bar",
"-I$(BINDIR)/foo/bar",
],
shared = {
"whole_archive_deps": [":whole_static_lib_for_shared_alwayslink"],
},
static = {
"whole_archive_deps": [":whole_static_lib_for_static_alwayslink"],
},
whole_archive_deps = [":whole_static_lib_for_both_alwayslink"],
whole_archive_deps_for_shared = [":whole_static_lib_for_shared_alwayslink"],
whole_archive_deps_for_static = [":whole_static_lib_for_static_alwayslink"],
)`},
})
}
@ -529,52 +537,56 @@ cc_library_static { name: "android_dep_for_shared" }
"-Ifoo/bar",
"-I$(BINDIR)/foo/bar",
],
dynamic_deps_for_shared = select({
"//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
"//conditions:default": [],
}),
implementation_deps = [":static_dep_for_both"],
shared_copts = ["sharedflag"] + select({
"//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
"//conditions:default": [],
}) + select({
"//build/bazel/platforms/os:android": ["-DANDROID_SHARED"],
"//conditions:default": [],
}) + select({
"//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
"//conditions:default": [],
}),
shared_srcs = ["sharedonly.cpp"] + select({
"//build/bazel/platforms/arch:arm": ["arm_shared.cpp"],
"//conditions:default": [],
}) + select({
"//build/bazel/platforms/os:android": ["android_shared.cpp"],
"//conditions:default": [],
}),
shared = {
"copts": ["sharedflag"] + select({
"//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
"//conditions:default": [],
}) + select({
"//build/bazel/platforms/os:android": ["-DANDROID_SHARED"],
"//conditions:default": [],
}) + select({
"//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
"//conditions:default": [],
}),
"dynamic_deps": select({
"//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
"//conditions:default": [],
}),
"srcs": ["sharedonly.cpp"] + select({
"//build/bazel/platforms/arch:arm": ["arm_shared.cpp"],
"//conditions:default": [],
}) + select({
"//build/bazel/platforms/os:android": ["android_shared.cpp"],
"//conditions:default": [],
}),
"static_deps": [":static_dep_for_shared"] + select({
"//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
"//conditions:default": [],
}) + select({
"//build/bazel/platforms/os:android": [":android_dep_for_shared"],
"//conditions:default": [],
}),
"whole_archive_deps": select({
"//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
"//conditions:default": [],
}),
},
srcs = ["both.cpp"],
static_copts = ["staticflag"] + select({
"//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
"//conditions:default": [],
}),
static_deps_for_shared = [":static_dep_for_shared"] + select({
"//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
"//conditions:default": [],
}) + select({
"//build/bazel/platforms/os:android": [":android_dep_for_shared"],
"//conditions:default": [],
}),
static_deps_for_static = [":static_dep_for_static"] + select({
"//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
"//conditions:default": [],
}),
static_srcs = ["staticonly.cpp"] + select({
"//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
"//conditions:default": [],
}),
whole_archive_deps_for_shared = select({
"//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
"//conditions:default": [],
}),
static = {
"copts": ["staticflag"] + select({
"//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
"//conditions:default": [],
}),
"srcs": ["staticonly.cpp"] + select({
"//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
"//conditions:default": [],
}),
"static_deps": [":static_dep_for_static"] + select({
"//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
"//conditions:default": [],
}),
},
)`},
})
}
@ -666,20 +678,22 @@ filegroup {
"-Ifoo/bar",
"-I$(BINDIR)/foo/bar",
],
shared_srcs = [
":shared_filegroup_cpp_srcs",
"shared_source.cc",
"shared_source.cpp",
],
shared_srcs_as = [
"shared_source.s",
"shared_source.S",
":shared_filegroup_as_srcs",
],
shared_srcs_c = [
"shared_source.c",
":shared_filegroup_c_srcs",
],
shared = {
"srcs": [
":shared_filegroup_cpp_srcs",
"shared_source.cc",
"shared_source.cpp",
],
"srcs_as": [
"shared_source.s",
"shared_source.S",
":shared_filegroup_as_srcs",
],
"srcs_c": [
"shared_source.c",
":shared_filegroup_c_srcs",
],
},
srcs = [
":both_filegroup_cpp_srcs",
"both_source.cc",
@ -694,20 +708,22 @@ filegroup {
"both_source.c",
":both_filegroup_c_srcs",
],
static_srcs = [
":static_filegroup_cpp_srcs",
"static_source.cc",
"static_source.cpp",
],
static_srcs_as = [
"static_source.s",
"static_source.S",
":static_filegroup_as_srcs",
],
static_srcs_c = [
"static_source.c",
":static_filegroup_c_srcs",
],
static = {
"srcs": [
":static_filegroup_cpp_srcs",
"static_source.cc",
"static_source.cpp",
],
"srcs_as": [
"static_source.s",
"static_source.S",
":static_filegroup_as_srcs",
],
"srcs_c": [
"static_source.c",
":static_filegroup_c_srcs",
],
},
)`},
})
}

View file

@ -142,15 +142,14 @@ func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) {
// staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties --
// properties which apply to either the shared or static version of a cc_library module.
type staticOrSharedAttributes struct {
srcs bazel.LabelListAttribute
srcs_c bazel.LabelListAttribute
srcs_as bazel.LabelListAttribute
Srcs bazel.LabelListAttribute
Srcs_c bazel.LabelListAttribute
Srcs_as bazel.LabelListAttribute
Copts bazel.StringListAttribute
copts bazel.StringListAttribute
staticDeps bazel.LabelListAttribute
dynamicDeps bazel.LabelListAttribute
wholeArchiveDeps bazel.LabelListAttribute
Static_deps bazel.LabelListAttribute
Dynamic_deps bazel.LabelListAttribute
Whole_archive_deps bazel.LabelListAttribute
}
func groupSrcsByExtension(ctx android.TopDownMutatorContext, srcs bazel.LabelListAttribute) (cppSrcs, cSrcs, asSrcs bazel.LabelListAttribute) {
@ -251,19 +250,19 @@ func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, module
}
attrs := staticOrSharedAttributes{
copts: bazel.StringListAttribute{Value: props.Cflags},
srcs: bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, props.Srcs)),
staticDeps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, props.Static_libs)),
dynamicDeps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, props.Shared_libs)),
wholeArchiveDeps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleWholeDeps(ctx, props.Whole_static_libs)),
Copts: bazel.StringListAttribute{Value: props.Cflags},
Srcs: bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, props.Srcs)),
Static_deps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, props.Static_libs)),
Dynamic_deps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, props.Shared_libs)),
Whole_archive_deps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleWholeDeps(ctx, props.Whole_static_libs)),
}
setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
attrs.copts.SetSelectValue(axis, config, props.Cflags)
attrs.srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs))
attrs.staticDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Static_libs))
attrs.dynamicDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Shared_libs))
attrs.wholeArchiveDeps.SetSelectValue(axis, config, android.BazelLabelForModuleWholeDeps(ctx, props.Whole_static_libs))
attrs.Copts.SetSelectValue(axis, config, props.Cflags)
attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs))
attrs.Static_deps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Static_libs))
attrs.Dynamic_deps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Shared_libs))
attrs.Whole_archive_deps.SetSelectValue(axis, config, android.BazelLabelForModuleWholeDeps(ctx, props.Whole_static_libs))
}
if isStatic {
@ -284,10 +283,10 @@ func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, module
}
}
cppSrcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, attrs.srcs)
attrs.srcs = cppSrcs
attrs.srcs_c = cSrcs
attrs.srcs_as = asSrcs
cppSrcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, attrs.Srcs)
attrs.Srcs = cppSrcs
attrs.Srcs_c = cSrcs
attrs.Srcs_as = asSrcs
return attrs
}

View file

@ -237,29 +237,12 @@ type bazelCcLibraryAttributes struct {
Linkopts bazel.StringListAttribute
Use_libcrt bazel.BoolAttribute
// Attributes pertaining to shared variant.
Shared_srcs bazel.LabelListAttribute
Shared_srcs_c bazel.LabelListAttribute
Shared_srcs_as bazel.LabelListAttribute
Shared_copts bazel.StringListAttribute
// This is shared only.
Version_script bazel.LabelAttribute
Exported_deps_for_shared 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_srcs bazel.LabelListAttribute
Static_srcs_c bazel.LabelListAttribute
Static_srcs_as bazel.LabelListAttribute
Static_copts bazel.StringListAttribute
Exported_deps_for_static bazel.LabelListAttribute
Static_deps_for_static bazel.LabelListAttribute
Dynamic_deps_for_static bazel.LabelListAttribute
Whole_archive_deps_for_static bazel.LabelListAttribute
// Common properties shared between both shared and static variants.
Shared staticOrSharedAttributes
Static staticOrSharedAttributes
Strip stripAttributes
}
@ -334,6 +317,8 @@ func CcLibraryBp2Build(ctx android.TopDownMutatorContext) {
Linkopts: linkerAttrs.linkopts,
Use_libcrt: linkerAttrs.useLibcrt,
Version_script: linkerAttrs.versionScript,
Strip: stripAttributes{
Keep_symbols: linkerAttrs.stripKeepSymbols,
Keep_symbols_and_debug_frame: linkerAttrs.stripKeepSymbolsAndDebugFrame,
@ -342,22 +327,9 @@ func CcLibraryBp2Build(ctx android.TopDownMutatorContext) {
None: linkerAttrs.stripNone,
},
Shared_srcs: sharedAttrs.srcs,
Shared_srcs_c: sharedAttrs.srcs_c,
Shared_srcs_as: sharedAttrs.srcs_as,
Shared_copts: sharedAttrs.copts,
Static_deps_for_shared: sharedAttrs.staticDeps,
Whole_archive_deps_for_shared: sharedAttrs.wholeArchiveDeps,
Dynamic_deps_for_shared: sharedAttrs.dynamicDeps,
Version_script: linkerAttrs.versionScript,
Shared: sharedAttrs,
Static_srcs: staticAttrs.srcs,
Static_srcs_c: staticAttrs.srcs_c,
Static_srcs_as: staticAttrs.srcs_as,
Static_copts: staticAttrs.copts,
Static_deps_for_static: staticAttrs.staticDeps,
Whole_archive_deps_for_static: staticAttrs.wholeArchiveDeps,
Dynamic_deps_for_static: staticAttrs.dynamicDeps,
Static: staticAttrs,
}
props := bazel.BazelTargetModuleProperties{