diff --git a/android/apex.go b/android/apex.go index 5dcf73bc6..958b6aad4 100644 --- a/android/apex.go +++ b/android/apex.go @@ -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 @@ -926,3 +934,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 +} diff --git a/android/mutator.go b/android/mutator.go index 4ec960472..c0040b7b9 100644 --- a/android/mutator.go +++ b/android/mutator.go @@ -758,6 +758,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, diff --git a/apex/apex.go b/apex/apex.go index baf4737d8..69547c3b7 100644 --- a/apex/apex.go +++ b/apex/apex.go @@ -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 +} diff --git a/cc/library.go b/cc/library.go index ee09389ca..13b333a12 100644 --- a/cc/library.go +++ b/cc/library.go @@ -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) }