Merge "Append _alwayslink to prebuilt whole static deps"

This commit is contained in:
Liz Kammer
2021-06-11 19:47:12 +00:00
committed by Gerrit Code Review
3 changed files with 95 additions and 18 deletions

View File

@@ -83,6 +83,36 @@ type BazelConversionPathContext interface {
// or ":<module>") and returns a Bazel-compatible label which corresponds to dependencies on the // or ":<module>") and returns a Bazel-compatible label which corresponds to dependencies on the
// module within the given ctx. // module within the given ctx.
func BazelLabelForModuleDeps(ctx BazelConversionPathContext, modules []string) bazel.LabelList { func BazelLabelForModuleDeps(ctx BazelConversionPathContext, modules []string) bazel.LabelList {
return bazelLabelForModuleDeps(ctx, modules, false)
}
// BazelLabelForModuleWholeDeps expects a list of references to other modules, ("<module>"
// or ":<module>") and returns a Bazel-compatible label which corresponds to dependencies on the
// module within the given ctx, where prebuilt dependencies will be appended with _alwayslink so
// they can be handled as whole static libraries.
func BazelLabelForModuleWholeDeps(ctx BazelConversionPathContext, modules []string) bazel.LabelList {
return bazelLabelForModuleDeps(ctx, modules, true)
}
// BazelLabelForModuleDepsExcludes expects two lists: modules (containing modules to include in the
// list), and excludes (modules to exclude from the list). Both of these should contain references
// to other modules, ("<module>" or ":<module>"). It returns a Bazel-compatible label list which
// corresponds to dependencies on the module within the given ctx, and the excluded dependencies.
func BazelLabelForModuleDepsExcludes(ctx BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
return bazelLabelForModuleDepsExcludes(ctx, modules, excludes, false)
}
// BazelLabelForModuleWholeDepsExcludes expects two lists: modules (containing modules to include in
// the list), and excludes (modules to exclude from the list). Both of these should contain
// references to other modules, ("<module>" or ":<module>"). It returns a Bazel-compatible label
// list which corresponds to dependencies on the module within the given ctx, and the excluded
// dependencies. Prebuilt dependencies will be appended with _alwayslink so they can be handled as
// whole static libraries.
func BazelLabelForModuleWholeDepsExcludes(ctx BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
return bazelLabelForModuleDepsExcludes(ctx, modules, excludes, true)
}
func bazelLabelForModuleDeps(ctx BazelConversionPathContext, modules []string, isWholeLibs bool) bazel.LabelList {
var labels bazel.LabelList var labels bazel.LabelList
for _, module := range modules { for _, module := range modules {
bpText := module bpText := module
@@ -90,7 +120,7 @@ func BazelLabelForModuleDeps(ctx BazelConversionPathContext, modules []string) b
module = ":" + module module = ":" + module
} }
if m, t := SrcIsModuleWithTag(module); m != "" { if m, t := SrcIsModuleWithTag(module); m != "" {
l := getOtherModuleLabel(ctx, m, t) l := getOtherModuleLabel(ctx, m, t, isWholeLibs)
l.OriginalModuleName = bpText l.OriginalModuleName = bpText
labels.Includes = append(labels.Includes, l) labels.Includes = append(labels.Includes, l)
} else { } else {
@@ -100,16 +130,12 @@ func BazelLabelForModuleDeps(ctx BazelConversionPathContext, modules []string) b
return labels return labels
} }
// BazelLabelForModuleDeps expects two lists: modules (containing modules to include in the list), func bazelLabelForModuleDepsExcludes(ctx BazelConversionPathContext, modules, excludes []string, isWholeLibs bool) bazel.LabelList {
// and excludes (modules to exclude from the list). Both of these should contain references to other moduleLabels := bazelLabelForModuleDeps(ctx, RemoveListFromList(modules, excludes), isWholeLibs)
// modules, ("<module>" or ":<module>"). It returns a Bazel-compatible label list which corresponds
// to dependencies on the module within the given ctx, and the excluded dependencies.
func BazelLabelForModuleDepsExcludes(ctx BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
moduleLabels := BazelLabelForModuleDeps(ctx, RemoveListFromList(modules, excludes))
if len(excludes) == 0 { if len(excludes) == 0 {
return moduleLabels return moduleLabels
} }
excludeLabels := BazelLabelForModuleDeps(ctx, excludes) excludeLabels := bazelLabelForModuleDeps(ctx, excludes, isWholeLibs)
return bazel.LabelList{ return bazel.LabelList{
Includes: moduleLabels.Includes, Includes: moduleLabels.Includes,
Excludes: excludeLabels.Includes, Excludes: excludeLabels.Includes,
@@ -273,7 +299,7 @@ func expandSrcsForBazel(ctx BazelConversionPathContext, paths, expandedExcludes
for _, p := range paths { for _, p := range paths {
if m, tag := SrcIsModuleWithTag(p); m != "" { if m, tag := SrcIsModuleWithTag(p); m != "" {
l := getOtherModuleLabel(ctx, m, tag) l := getOtherModuleLabel(ctx, m, tag, false)
if !InList(l.Label, expandedExcludes) { if !InList(l.Label, expandedExcludes) {
l.OriginalModuleName = fmt.Sprintf(":%s", m) l.OriginalModuleName = fmt.Sprintf(":%s", m)
labels.Includes = append(labels.Includes, l) labels.Includes = append(labels.Includes, l)
@@ -304,7 +330,7 @@ func expandSrcsForBazel(ctx BazelConversionPathContext, paths, expandedExcludes
// getOtherModuleLabel returns a bazel.Label for the given dependency/tag combination for the // getOtherModuleLabel returns a bazel.Label for the given dependency/tag combination for the
// module. The label will be relative to the current directory if appropriate. The dependency must // module. The label will be relative to the current directory if appropriate. The dependency must
// already be resolved by either deps mutator or path deps mutator. // already be resolved by either deps mutator or path deps mutator.
func getOtherModuleLabel(ctx BazelConversionPathContext, dep, tag string) bazel.Label { func getOtherModuleLabel(ctx BazelConversionPathContext, dep, tag string, isWholeLibs bool) bazel.Label {
m, _ := ctx.GetDirectDep(dep) m, _ := ctx.GetDirectDep(dep)
if m == nil { if m == nil {
panic(fmt.Errorf(`Cannot get direct dep %q of %q. panic(fmt.Errorf(`Cannot get direct dep %q of %q.
@@ -313,6 +339,11 @@ func getOtherModuleLabel(ctx BazelConversionPathContext, dep, tag string) bazel.
} }
otherLabel := bazelModuleLabel(ctx, m, tag) otherLabel := bazelModuleLabel(ctx, m, tag)
label := bazelModuleLabel(ctx, ctx.Module(), "") label := bazelModuleLabel(ctx, ctx.Module(), "")
if isWholeLibs {
if m, ok := m.(Module); ok && IsModulePrebuilt(m) {
otherLabel += "_alwayslink"
}
}
if samePackage(label, otherLabel) { if samePackage(label, otherLabel) {
otherLabel = bazelShortLabel(otherLabel) otherLabel = bazelShortLabel(otherLabel)
} }

View File

@@ -49,6 +49,7 @@ func registerCcLibraryModuleTypes(ctx android.RegistrationContext) {
cc.RegisterCCBuildComponents(ctx) cc.RegisterCCBuildComponents(ctx)
ctx.RegisterModuleType("filegroup", android.FileGroupFactory) ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory) ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory)
ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory) ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory) ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
} }
@@ -401,6 +402,48 @@ cc_library { name: "shared_dep_for_both" }
}) })
} }
func TestCcLibraryWholeStaticLibsAlwaysLink(t *testing.T) {
runCcLibraryTestCase(t, bp2buildTestCase{
moduleTypeUnderTest: "cc_library",
moduleTypeUnderTestFactory: cc.LibraryFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
dir: "foo/bar",
filesystem: map[string]string{
"foo/bar/Android.bp": `
cc_library {
name: "a",
whole_static_libs: ["whole_static_lib_for_both"],
static: {
whole_static_libs: ["whole_static_lib_for_static"],
},
shared: {
whole_static_libs: ["whole_static_lib_for_shared"],
},
bazel_module: { bp2build_available: true },
}
cc_prebuilt_library_static { name: "whole_static_lib_for_shared" }
cc_prebuilt_library_static { name: "whole_static_lib_for_static" }
cc_prebuilt_library_static { name: "whole_static_lib_for_both" }
`,
},
blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library(
name = "a",
copts = [
"-Ifoo/bar",
"-I$(BINDIR)/foo/bar",
],
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"],
)`},
})
}
func TestCcLibrarySharedStaticPropsInArch(t *testing.T) { func TestCcLibrarySharedStaticPropsInArch(t *testing.T) {
runCcLibraryTestCase(t, bp2buildTestCase{ runCcLibraryTestCase(t, bp2buildTestCase{
description: "cc_library shared/static props in arch", description: "cc_library shared/static props in arch",

View File

@@ -255,7 +255,7 @@ func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, module
srcs: bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, props.Srcs)), srcs: bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, props.Srcs)),
staticDeps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, props.Static_libs)), staticDeps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, props.Static_libs)),
dynamicDeps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, props.Shared_libs)), dynamicDeps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, props.Shared_libs)),
wholeArchiveDeps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)), wholeArchiveDeps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleWholeDeps(ctx, props.Whole_static_libs)),
} }
setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) { setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
@@ -263,7 +263,7 @@ func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, module
attrs.srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs)) attrs.srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs))
attrs.staticDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Static_libs)) attrs.staticDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Static_libs))
attrs.dynamicDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Shared_libs)) attrs.dynamicDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Shared_libs))
attrs.wholeArchiveDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)) attrs.wholeArchiveDeps.SetSelectValue(axis, config, android.BazelLabelForModuleWholeDeps(ctx, props.Whole_static_libs))
} }
if isStatic { if isStatic {
@@ -554,7 +554,7 @@ func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module)
staticLibs := android.FirstUniqueStrings(baseLinkerProps.Static_libs) staticLibs := android.FirstUniqueStrings(baseLinkerProps.Static_libs)
staticDeps.Value = android.BazelLabelForModuleDepsExcludes(ctx, staticLibs, baseLinkerProps.Exclude_static_libs) staticDeps.Value = android.BazelLabelForModuleDepsExcludes(ctx, staticLibs, baseLinkerProps.Exclude_static_libs)
wholeArchiveLibs := android.FirstUniqueStrings(baseLinkerProps.Whole_static_libs) wholeArchiveLibs := android.FirstUniqueStrings(baseLinkerProps.Whole_static_libs)
wholeArchiveDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDepsExcludes(ctx, wholeArchiveLibs, baseLinkerProps.Exclude_static_libs)) wholeArchiveDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleWholeDepsExcludes(ctx, wholeArchiveLibs, baseLinkerProps.Exclude_static_libs))
sharedLibs := android.FirstUniqueStrings(baseLinkerProps.Shared_libs) sharedLibs := android.FirstUniqueStrings(baseLinkerProps.Shared_libs)
dynamicDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDepsExcludes(ctx, sharedLibs, baseLinkerProps.Exclude_shared_libs)) dynamicDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDepsExcludes(ctx, sharedLibs, baseLinkerProps.Exclude_shared_libs))
@@ -581,7 +581,7 @@ func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module)
staticLibs := android.FirstUniqueStrings(baseLinkerProps.Static_libs) staticLibs := android.FirstUniqueStrings(baseLinkerProps.Static_libs)
staticDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDepsExcludes(ctx, staticLibs, baseLinkerProps.Exclude_static_libs)) staticDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDepsExcludes(ctx, staticLibs, baseLinkerProps.Exclude_static_libs))
wholeArchiveLibs := android.FirstUniqueStrings(baseLinkerProps.Whole_static_libs) wholeArchiveLibs := android.FirstUniqueStrings(baseLinkerProps.Whole_static_libs)
wholeArchiveDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDepsExcludes(ctx, wholeArchiveLibs, baseLinkerProps.Exclude_static_libs)) wholeArchiveDeps.SetSelectValue(axis, config, android.BazelLabelForModuleWholeDepsExcludes(ctx, wholeArchiveLibs, baseLinkerProps.Exclude_static_libs))
sharedLibs := android.FirstUniqueStrings(baseLinkerProps.Shared_libs) sharedLibs := android.FirstUniqueStrings(baseLinkerProps.Shared_libs)
dynamicDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDepsExcludes(ctx, sharedLibs, baseLinkerProps.Exclude_shared_libs)) dynamicDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDepsExcludes(ctx, sharedLibs, baseLinkerProps.Exclude_shared_libs))
@@ -604,13 +604,15 @@ func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module)
excludesField string excludesField string
// reference to the bazel attribute that should be set for the given product variable config // reference to the bazel attribute that should be set for the given product variable config
attribute *bazel.LabelListAttribute attribute *bazel.LabelListAttribute
depResolutionFunc func(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList
} }
productVarToDepFields := map[string]productVarDep{ productVarToDepFields := map[string]productVarDep{
// product variables do not support exclude_shared_libs // product variables do not support exclude_shared_libs
"Shared_libs": productVarDep{attribute: &dynamicDeps}, "Shared_libs": productVarDep{attribute: &dynamicDeps, depResolutionFunc: android.BazelLabelForModuleDepsExcludes},
"Static_libs": productVarDep{"Exclude_static_libs", &staticDeps}, "Static_libs": productVarDep{"Exclude_static_libs", &staticDeps, android.BazelLabelForModuleDepsExcludes},
"Whole_static_libs": productVarDep{"Exclude_static_libs", &wholeArchiveDeps}, "Whole_static_libs": productVarDep{"Exclude_static_libs", &wholeArchiveDeps, android.BazelLabelForModuleWholeDepsExcludes},
} }
productVariableProps := android.ProductVariableProperties(ctx) productVariableProps := android.ProductVariableProperties(ctx)
@@ -644,7 +646,8 @@ func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module)
if excludes, ok = excludesProp.Property.([]string); excludesExists && !ok { if excludes, ok = excludesProp.Property.([]string); excludesExists && !ok {
ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField) ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField)
} }
dep.attribute.SetSelectValue(bazel.ProductVariableConfigurationAxis(config), config, android.BazelLabelForModuleDepsExcludes(ctx, android.FirstUniqueStrings(includes), excludes))
dep.attribute.SetSelectValue(bazel.ProductVariableConfigurationAxis(config), config, dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes))
} }
} }