Merge changes from topic "bp2build-ignore-test-apex-tags"

* changes:
  Ignore test apexes from bp2build generated tags
  Ignore test apexes from bp2build generated tags
This commit is contained in:
Spandan Das 2023-05-01 16:05:38 +00:00 committed by Gerrit Code Review
commit 934d5047ef
14 changed files with 71 additions and 17 deletions

View file

@ -462,6 +462,14 @@ const (
AvailableToGkiApex = "com.android.gki.*"
)
var (
AvailableToRecognziedWildcards = []string{
AvailableToPlatform,
AvailableToAnyApex,
AvailableToGkiApex,
}
)
// CheckAvailableForApex provides the default algorithm for checking the apex availability. When the
// availability is empty, it defaults to ["//apex_available:platform"] which means "available to the
// platform but not available to any APEX". When the list is not empty, `what` is matched against
@ -925,3 +933,9 @@ func CheckMinSdkVersion(ctx ModuleContext, minSdkVersion ApiLevel, walk WalkPayl
return true
})
}
// Implemented by apexBundle.
type ApexTestInterface interface {
// Return true if the apex bundle is an apex_test
IsTestApex() bool
}

View file

@ -781,6 +781,35 @@ func ApexAvailableTags(mod Module) bazel.StringListAttribute {
return attr
}
func ApexAvailableTagsWithoutTestApexes(ctx BaseModuleContext, mod Module) bazel.StringListAttribute {
attr := bazel.StringListAttribute{}
if am, ok := mod.(ApexModule); ok {
apexAvailableWithoutTestApexes := removeTestApexes(ctx, am.apexModuleBase().ApexAvailable())
// If a user does not specify apex_available in Android.bp, then soong provides a default.
// To avoid verbosity of BUILD files, remove this default from user-facing BUILD files.
if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 {
apexAvailableWithoutTestApexes = []string{}
}
attr.Value = ConvertApexAvailableToTags(apexAvailableWithoutTestApexes)
}
return attr
}
func removeTestApexes(ctx BaseModuleContext, apex_available []string) []string {
testApexes := []string{}
for _, aa := range apex_available {
// ignore the wildcards
if InList(aa, AvailableToRecognziedWildcards) {
continue
}
mod, _ := ctx.ModuleFromName(aa)
if apex, ok := mod.(ApexTestInterface); ok && apex.IsTestApex() {
testApexes = append(testApexes, aa)
}
}
return RemoveListFromList(CopyOf(apex_available), testApexes)
}
func ConvertApexAvailableToTags(apexAvailable []string) []string {
if len(apexAvailable) == 0 {
// We need nil specifically to make bp2build not add the tags property at all,
@ -794,6 +823,13 @@ func ConvertApexAvailableToTags(apexAvailable []string) []string {
return result
}
// ConvertApexAvailableToTagsWithoutTestApexes converts a list of apex names to a list of bazel tags
// This function drops any test apexes from the input.
func ConvertApexAvailableToTagsWithoutTestApexes(ctx BaseModuleContext, apexAvailable []string) []string {
noTestApexes := removeTestApexes(ctx, apexAvailable)
return ConvertApexAvailableToTags(noTestApexes)
}
func (t *topDownMutatorContext) createBazelTargetModule(
bazelProps bazel.BazelTargetModuleProperties,
commonAttrs CommonAttributes,

View file

@ -234,7 +234,7 @@ func Bp2buildProtoProperties(ctx Bp2buildMutatorContext, m *ModuleBase, srcs baz
}
}
tags := ApexAvailableTags(ctx.Module())
tags := ApexAvailableTagsWithoutTestApexes(ctx.(TopDownMutatorContext), ctx.Module())
ctx.CreateBazelTargetModule(
bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},

View file

@ -3811,3 +3811,7 @@ func makeSharedLibsAttributes(config string, libsLabelList bazel.LabelList,
func invalidCompileMultilib(ctx android.TopDownMutatorContext, value string) {
ctx.PropertyErrorf("compile_multilib", "Invalid value: %s", value)
}
func (a *apexBundle) IsTestApex() bool {
return a.testApex
}

View file

@ -661,7 +661,7 @@ func binaryBp2build(ctx android.TopDownMutatorContext, m *Module) {
// shared with cc_test
binaryAttrs := binaryBp2buildAttrs(ctx, m)
tags := android.ApexAvailableTags(m)
tags := android.ApexAvailableTagsWithoutTestApexes(ctx, m)
ctx.CreateBazelTargetModule(bazel.BazelTargetModuleProperties{
Rule_class: "cc_binary",
Bzl_load_location: "//build/bazel/rules/cc:cc_binary.bzl",

View file

@ -271,7 +271,7 @@ func bp2buildParseStaticOrSharedProps(ctx android.BazelConversionPathContext, mo
attrs.Srcs_c = partitionedSrcs[cSrcPartition]
attrs.Srcs_as = partitionedSrcs[asSrcPartition]
attrs.Apex_available = android.ConvertApexAvailableToTags(apexAvailable)
attrs.Apex_available = android.ConvertApexAvailableToTagsWithoutTestApexes(ctx.(android.TopDownMutatorContext), apexAvailable)
attrs.Features.Append(convertHiddenVisibilityToFeatureStaticOrShared(ctx, module, isStatic))
@ -924,7 +924,7 @@ func bp2buildCcAidlLibrary(
return false
})
apexAvailableTags := android.ApexAvailableTags(ctx.Module())
apexAvailableTags := android.ApexAvailableTagsWithoutTestApexes(ctx.(android.TopDownMutatorContext), ctx.Module())
sdkAttrs := bp2BuildParseSdkAttributes(m)
if !aidlSrcs.IsEmpty() {

View file

@ -433,11 +433,11 @@ func libraryBp2Build(ctx android.TopDownMutatorContext, m *Module) {
var tagsForStaticVariant bazel.StringListAttribute
if compilerAttrs.stubsSymbolFile == nil && len(compilerAttrs.stubsVersions.Value) == 0 {
tagsForStaticVariant = android.ApexAvailableTags(m)
tagsForStaticVariant = android.ApexAvailableTagsWithoutTestApexes(ctx, m)
}
tagsForStaticVariant.Append(bazel.StringListAttribute{Value: staticAttrs.Apex_available})
tagsForSharedVariant := android.ApexAvailableTags(m)
tagsForSharedVariant := android.ApexAvailableTagsWithoutTestApexes(ctx, m)
tagsForSharedVariant.Append(bazel.StringListAttribute{Value: sharedAttrs.Apex_available})
ctx.CreateBazelTargetModuleWithRestrictions(staticProps,
@ -3002,7 +3002,7 @@ func sharedOrStaticLibraryBp2Build(ctx android.TopDownMutatorContext, module *Mo
Bzl_load_location: fmt.Sprintf("//build/bazel/rules/cc:%s.bzl", modType),
}
tags := android.ApexAvailableTags(module)
tags := android.ApexAvailableTagsWithoutTestApexes(ctx, module)
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name(), Tags: tags}, attrs)
}

View file

@ -151,7 +151,7 @@ func libraryHeadersBp2Build(ctx android.TopDownMutatorContext, module *Module) {
Bzl_load_location: "//build/bazel/rules/cc:cc_library_headers.bzl",
}
tags := android.ApexAvailableTags(module)
tags := android.ApexAvailableTagsWithoutTestApexes(ctx, module)
ctx.CreateBazelTargetModule(props, android.CommonAttributes{
Name: module.Name(),

View file

@ -226,7 +226,7 @@ func objectBp2Build(ctx android.TopDownMutatorContext, m *Module) {
Bzl_load_location: "//build/bazel/rules/cc:cc_object.bzl",
}
tags := android.ApexAvailableTags(m)
tags := android.ApexAvailableTagsWithoutTestApexes(ctx, m)
ctx.CreateBazelTargetModule(props, android.CommonAttributes{
Name: m.Name(),

View file

@ -389,7 +389,7 @@ func prebuiltLibraryStaticBp2Build(ctx android.TopDownMutatorContext, module *Mo
name += "_bp2build_cc_library_static"
}
tags := android.ApexAvailableTags(module)
tags := android.ApexAvailableTagsWithoutTestApexes(ctx, module)
ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name, Tags: tags}, attrs, prebuiltAttrs.Enabled)
_true := true
@ -420,7 +420,7 @@ func prebuiltLibrarySharedBp2Build(ctx android.TopDownMutatorContext, module *Mo
}
name := android.RemoveOptionalPrebuiltPrefix(module.Name())
tags := android.ApexAvailableTags(module)
tags := android.ApexAvailableTagsWithoutTestApexes(ctx, module)
ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name, Tags: tags}, attrs, prebuiltAttrs.Enabled)
}
@ -650,7 +650,7 @@ func prebuiltObjectBp2Build(ctx android.TopDownMutatorContext, module *Module) {
}
name := android.RemoveOptionalPrebuiltPrefix(module.Name())
tags := android.ApexAvailableTags(module)
tags := android.ApexAvailableTagsWithoutTestApexes(ctx, module)
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name, Tags: tags}, attrs)
}
@ -813,7 +813,7 @@ func prebuiltBinaryBp2Build(ctx android.TopDownMutatorContext, module *Module) {
}
name := android.RemoveOptionalPrebuiltPrefix(module.Name())
tags := android.ApexAvailableTags(module)
tags := android.ApexAvailableTagsWithoutTestApexes(ctx, module)
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name, Tags: tags}, attrs)
}

View file

@ -207,7 +207,7 @@ func bp2buildProto(ctx android.Bp2buildMutatorContext, m *Module, protoSrcs baze
protoAttrs.Min_sdk_version = m.Properties.Min_sdk_version
name := m.Name() + suffix
tags := android.ApexAvailableTags(m)
tags := android.ApexAvailableTagsWithoutTestApexes(ctx.(android.TopDownMutatorContext), m)
ctx.CreateBazelTargetModule(
bazel.BazelTargetModuleProperties{
Rule_class: rule_class,

View file

@ -38,7 +38,7 @@ type SyspropLibraryLabels struct {
}
func Bp2buildSysprop(ctx android.Bp2buildMutatorContext, labels SyspropLibraryLabels, srcs bazel.LabelListAttribute, minSdkVersion *string) {
apexAvailableTags := android.ApexAvailableTags(ctx.Module())
apexAvailableTags := android.ApexAvailableTagsWithoutTestApexes(ctx.(android.TopDownMutatorContext), ctx.Module())
ctx.CreateBazelTargetModule(
bazel.BazelTargetModuleProperties{
Rule_class: "sysprop_library",

View file

@ -940,7 +940,7 @@ func (m *Module) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
}
}
tags := android.ApexAvailableTags(m)
tags := android.ApexAvailableTagsWithoutTestApexes(ctx, m)
if ctx.ModuleType() == "gensrcs" {
// The Output_extension prop is not in an immediately accessible field

View file

@ -2839,7 +2839,7 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext)
return android.IsConvertedToAidlLibrary(ctx, src.OriginalModuleName)
})
apexAvailableTags := android.ApexAvailableTags(ctx.Module())
apexAvailableTags := android.ApexAvailableTagsWithoutTestApexes(ctx, ctx.Module())
if !aidlSrcs.IsEmpty() {
aidlLibName := m.Name() + "_aidl_library"