Merge "Remove ConvertWithBp2build implementations" into main

This commit is contained in:
Colin Cross
2023-12-11 23:12:53 +00:00
committed by Gerrit Code Review
168 changed files with 64 additions and 39712 deletions

View File

@@ -17,7 +17,6 @@ package android
import (
"reflect"
"android/soong/ui/metrics/bp2build_metrics_proto"
"github.com/google/blueprint"
"github.com/google/blueprint/proptools"
)
@@ -118,11 +117,6 @@ type DefaultsVisibilityProperties struct {
type DefaultsModuleBase struct {
DefaultableModuleBase
// Included to support setting bazel_module.label for multiple Soong modules to the same Bazel
// target. This is primarily useful for modules that were architecture specific and instead are
// handled in Bazel as a select().
BazelModuleBase
}
// The common pattern for defaults modules is to register separate instances of
@@ -165,7 +159,6 @@ func (d *DefaultsModuleBase) isDefaults() bool {
type DefaultsModule interface {
Module
Defaults
Bazelable
}
func (d *DefaultsModuleBase) properties() []interface{} {
@@ -178,13 +171,6 @@ func (d *DefaultsModuleBase) productVariableProperties() interface{} {
func (d *DefaultsModuleBase) GenerateAndroidBuildActions(ctx ModuleContext) {}
// ConvertWithBp2build to fulfill Bazelable interface; however, at this time defaults module are
// *NOT* converted with bp2build
func (defaultable *DefaultsModuleBase) ConvertWithBp2build(ctx Bp2buildMutatorContext) {
// Defaults types are never convertible.
ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
}
func InitDefaultsModule(module DefaultsModule) {
commonProperties := &commonProperties{}
@@ -194,8 +180,6 @@ func InitDefaultsModule(module DefaultsModule) {
&ApexProperties{},
&distProperties{})
// Bazel module must be initialized _before_ Defaults to be included in cc_defaults module.
InitBazelModule(module)
initAndroidModuleBase(module)
initProductVariableModule(module)
initArchModule(module)
@@ -225,60 +209,10 @@ func InitDefaultsModule(module DefaultsModule) {
var _ Defaults = (*DefaultsModuleBase)(nil)
// applyNamespacedVariableDefaults only runs in bp2build mode for
// defaultable/defaults modules. Its purpose is to merge namespaced product
// variable props from defaults deps, even if those defaults are custom module
// types created from soong_config_module_type, e.g. one that's wrapping a
// cc_defaults or java_defaults.
func applyNamespacedVariableDefaults(defaultDep Defaults, ctx TopDownMutatorContext) {
var dep, b Bazelable
dep, ok := defaultDep.(Bazelable)
if !ok {
if depMod, ok := defaultDep.(Module); ok {
// Track that this dependency hasn't been converted to bp2build yet.
ctx.AddUnconvertedBp2buildDep(depMod.Name())
return
} else {
panic("Expected default dep to be a Module.")
}
}
b, ok = ctx.Module().(Bazelable)
if !ok {
return
}
// namespacedVariableProps is a map from namespaces (e.g. acme, android,
// vendor_foo) to a slice of soong_config_variable struct pointers,
// containing properties for that particular module.
src := dep.namespacedVariableProps()
dst := b.namespacedVariableProps()
if dst == nil {
dst = make(namespacedVariableProperties)
}
// Propagate all soong_config_variable structs from the dep. We'll merge the
// actual property values later in variable.go.
for namespace := range src {
if dst[namespace] == nil {
dst[namespace] = []interface{}{}
}
for _, i := range src[namespace] {
dst[namespace] = append(dst[namespace], i)
}
}
b.setNamespacedVariableProps(dst)
}
func (defaultable *DefaultableModuleBase) applyDefaults(ctx TopDownMutatorContext,
defaultsList []Defaults) {
for _, defaults := range defaultsList {
if ctx.Config().BuildMode == Bp2build {
applyNamespacedVariableDefaults(defaults, ctx)
}
for _, prop := range defaultable.defaultableProperties {
if prop == defaultable.defaultableVariableProperties {
defaultable.applyDefaultVariableProperties(ctx, defaults, prop)

View File

@@ -15,15 +15,9 @@
package android
import (
"path/filepath"
"regexp"
"strings"
"android/soong/bazel"
"android/soong/bazel/cquery"
"android/soong/ui/metrics/bp2build_metrics_proto"
"github.com/google/blueprint"
"github.com/google/blueprint/proptools"
)
func init() {
@@ -39,181 +33,6 @@ func RegisterFilegroupBuildComponents(ctx RegistrationContext) {
ctx.RegisterModuleType("filegroup_defaults", FileGroupDefaultsFactory)
}
var convertedProtoLibrarySuffix = "_bp2build_converted"
// IsFilegroup checks that a module is a filegroup type
func IsFilegroup(ctx bazel.OtherModuleContext, m blueprint.Module) bool {
return ctx.OtherModuleType(m) == "filegroup"
}
var (
// ignoring case, checks for proto or protos as an independent word in the name, whether at the
// beginning, end, or middle. e.g. "proto.foo", "bar-protos", "baz_proto_srcs" would all match
filegroupLikelyProtoPattern = regexp.MustCompile("(?i)(^|[^a-z])proto(s)?([^a-z]|$)")
filegroupLikelyAidlPattern = regexp.MustCompile("(?i)(^|[^a-z])aidl(s)?([^a-z]|$)")
ProtoSrcLabelPartition = bazel.LabelPartition{
Extensions: []string{".proto"},
LabelMapper: isFilegroupWithPattern(filegroupLikelyProtoPattern),
}
AidlSrcLabelPartition = bazel.LabelPartition{
Extensions: []string{".aidl"},
LabelMapper: isFilegroupWithPattern(filegroupLikelyAidlPattern),
}
)
func isFilegroupWithPattern(pattern *regexp.Regexp) bazel.LabelMapper {
return func(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) {
m, exists := ctx.ModuleFromName(label.OriginalModuleName)
labelStr := label.Label
if !exists || !IsFilegroup(ctx, m) {
return labelStr, false
}
likelyMatched := pattern.MatchString(label.OriginalModuleName)
return labelStr, likelyMatched
}
}
// https://docs.bazel.build/versions/master/be/general.html#filegroup
type bazelFilegroupAttributes struct {
Srcs bazel.LabelListAttribute
Applicable_licenses bazel.LabelListAttribute
}
type bazelAidlLibraryAttributes struct {
Srcs bazel.LabelListAttribute
Strip_import_prefix *string
}
// ConvertWithBp2build performs bp2build conversion of filegroup
func (fg *fileGroup) ConvertWithBp2build(ctx Bp2buildMutatorContext) {
srcs := bazel.MakeLabelListAttribute(
BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs))
// For Bazel compatibility, don't generate the filegroup if there is only 1
// source file, and that the source file is named the same as the module
// itself. In Bazel, eponymous filegroups like this would be an error.
//
// Instead, dependents on this single-file filegroup can just depend
// on the file target, instead of rule target, directly.
//
// You may ask: what if a filegroup has multiple files, and one of them
// shares the name? The answer: we haven't seen that in the wild, and
// should lock Soong itself down to prevent the behavior. For now,
// we raise an error if bp2build sees this problem.
for _, f := range srcs.Value.Includes {
if f.Label == fg.Name() {
if len(srcs.Value.Includes) > 1 {
ctx.ModuleErrorf("filegroup '%s' cannot contain a file with the same name", fg.Name())
ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_SRC_NAME_COLLISION, "")
} else {
panic("This situation should have been handled by FileGroupFactory's call to InitBazelModuleAsHandcrafted")
}
return
}
}
// Convert module that has only AIDL files to aidl_library
// If the module has a mixed bag of AIDL and non-AIDL files, split the filegroup manually
// and then convert
if fg.ShouldConvertToAidlLibrary(ctx) {
tags := []string{"apex_available=//apex_available:anyapex"}
attrs := &bazelAidlLibraryAttributes{
Srcs: srcs,
Strip_import_prefix: fg.properties.Path,
}
props := bazel.BazelTargetModuleProperties{
Rule_class: "aidl_library",
Bzl_load_location: "//build/bazel/rules/aidl:aidl_library.bzl",
}
ctx.CreateBazelTargetModule(
props,
CommonAttributes{
Name: fg.Name(),
Tags: bazel.MakeStringListAttribute(tags),
},
attrs)
} else {
if fg.ShouldConvertToProtoLibrary(ctx) {
pkgToSrcs := partitionSrcsByPackage(ctx.ModuleDir(), bazel.MakeLabelList(srcs.Value.Includes))
if len(pkgToSrcs) > 1 {
ctx.ModuleErrorf("TODO: Add bp2build support for multiple package .protosrcs in filegroup")
return
}
pkg := SortedKeys(pkgToSrcs)[0]
attrs := &ProtoAttrs{
Srcs: bazel.MakeLabelListAttribute(pkgToSrcs[pkg]),
Strip_import_prefix: fg.properties.Path,
}
tags := []string{
"apex_available=//apex_available:anyapex",
// TODO(b/246997908): we can remove this tag if we could figure out a solution for this bug.
"manual",
}
if pkg != ctx.ModuleDir() {
// Since we are creating the proto_library in a subpackage, create an import_prefix relative to the current package
if rel, err := filepath.Rel(ctx.ModuleDir(), pkg); err != nil {
ctx.ModuleErrorf("Could not get relative path for %v %v", pkg, err)
} else if rel != "." {
attrs.Import_prefix = &rel
// Strip the package prefix
attrs.Strip_import_prefix = proptools.StringPtr("")
}
}
ctx.CreateBazelTargetModule(
bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
CommonAttributes{
Name: fg.Name() + "_proto",
Dir: proptools.StringPtr(pkg),
Tags: bazel.MakeStringListAttribute(tags),
},
attrs)
// Create an alias in the current dir. The actual target might exist in a different package, but rdeps
// can reliabily use this alias
ctx.CreateBazelTargetModule(
bazel.BazelTargetModuleProperties{Rule_class: "alias"},
CommonAttributes{
Name: fg.Name() + convertedProtoLibrarySuffix,
// TODO(b/246997908): we can remove this tag if we could figure out a solution for this bug.
Tags: bazel.MakeStringListAttribute(tags),
},
&bazelAliasAttributes{
Actual: bazel.MakeLabelAttribute("//" + pkg + ":" + fg.Name() + "_proto"),
},
)
}
// TODO(b/242847534): Still convert to a filegroup because other unconverted
// modules may depend on the filegroup
attrs := &bazelFilegroupAttributes{
Srcs: srcs,
}
props := bazel.BazelTargetModuleProperties{
Rule_class: "filegroup",
Bzl_load_location: "//build/bazel/rules:filegroup.bzl",
}
ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs)
}
}
type FileGroupPath interface {
GetPath(ctx Bp2buildMutatorContext) string
}
func (fg *fileGroup) GetPath(ctx Bp2buildMutatorContext) string {
if fg.properties.Path != nil {
return *fg.properties.Path
}
return ""
}
type fileGroupProperties struct {
// srcs lists files that will be included in this filegroup
Srcs []string `android:"path"`
@@ -233,18 +52,12 @@ type fileGroupProperties struct {
type fileGroup struct {
ModuleBase
BazelModuleBase
DefaultableModuleBase
FileGroupAsLibrary
FileGroupPath
properties fileGroupProperties
srcs Paths
}
var _ MixedBuildBuildable = (*fileGroup)(nil)
var _ SourceFileProducer = (*fileGroup)(nil)
var _ FileGroupAsLibrary = (*fileGroup)(nil)
var _ FileGroupPath = (*fileGroup)(nil)
// filegroup contains a list of files that are referenced by other modules
// properties (such as "srcs") using the syntax ":<name>". filegroup are
@@ -253,17 +66,6 @@ func FileGroupFactory() Module {
module := &fileGroup{}
module.AddProperties(&module.properties)
InitAndroidModule(module)
InitBazelModule(module)
AddBazelHandcraftedHook(module, func(ctx LoadHookContext) string {
// If there is a single src with the same name as the filegroup module name,
// then don't generate this filegroup. It will be OK for other targets
// to depend on this source file by name directly.
fg := ctx.Module().(*fileGroup)
if len(fg.properties.Srcs) == 1 && fg.Name() == fg.properties.Srcs[0] {
return fg.Name()
}
return ""
})
InitDefaultableModule(module)
return module
}
@@ -303,101 +105,6 @@ func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) {
}
}
func (fg *fileGroup) QueueBazelCall(ctx BaseModuleContext) {
bazelCtx := ctx.Config().BazelContext
bazelCtx.QueueBazelRequest(
fg.GetBazelLabel(ctx, fg),
cquery.GetOutputFiles,
configKey{arch: Common.String(), osType: CommonOS})
}
func (fg *fileGroup) IsMixedBuildSupported(ctx BaseModuleContext) bool {
// TODO(b/247782695), TODO(b/242847534) Fix mixed builds for filegroups
return false
}
func (fg *fileGroup) ProcessBazelQueryResponse(ctx ModuleContext) {
bazelCtx := ctx.Config().BazelContext
// This is a short-term solution because we rely on info from Android.bp to handle
// a converted module. This will block when we want to remove Android.bp for all
// converted modules at some point.
// TODO(b/242847534): Implement a long-term solution in which we don't need to rely
// on info form Android.bp for modules that are already converted to Bazel
relativeRoot := ctx.ModuleDir()
if fg.properties.Path != nil {
relativeRoot = filepath.Join(relativeRoot, *fg.properties.Path)
}
filePaths, err := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), configKey{arch: Common.String(), osType: CommonOS})
if err != nil {
ctx.ModuleErrorf(err.Error())
return
}
bazelOuts := make(Paths, 0, len(filePaths))
for _, p := range filePaths {
bazelOuts = append(bazelOuts, PathForBazelOutRelative(ctx, relativeRoot, p))
}
fg.srcs = bazelOuts
}
func (fg *fileGroup) ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool {
return fg.shouldConvertToLibrary(ctx, ".aidl")
}
func (fg *fileGroup) ShouldConvertToProtoLibrary(ctx BazelConversionPathContext) bool {
return fg.shouldConvertToLibrary(ctx, ".proto")
}
func (fg *fileGroup) shouldConvertToLibrary(ctx BazelConversionPathContext, suffix string) bool {
if len(fg.properties.Srcs) == 0 || !fg.ShouldConvertWithBp2build(ctx) {
return false
}
for _, src := range fg.properties.Srcs {
if !strings.HasSuffix(src, suffix) {
return false
}
}
return true
}
func (fg *fileGroup) GetAidlLibraryLabel(ctx BazelConversionPathContext) string {
return fg.getFileGroupAsLibraryLabel(ctx)
}
func (fg *fileGroup) GetProtoLibraryLabel(ctx BazelConversionPathContext) string {
return fg.getFileGroupAsLibraryLabel(ctx) + convertedProtoLibrarySuffix
}
func (fg *fileGroup) getFileGroupAsLibraryLabel(ctx BazelConversionPathContext) string {
if ctx.OtherModuleDir(fg.module) == ctx.ModuleDir() {
return ":" + fg.Name()
} else {
return fg.GetBazelLabel(ctx, fg)
}
}
// Given a name in srcs prop, check to see if the name references a filegroup
// and the filegroup is converted to aidl_library
func IsConvertedToAidlLibrary(ctx BazelConversionPathContext, name string) bool {
if fg, ok := ToFileGroupAsLibrary(ctx, name); ok {
return fg.ShouldConvertToAidlLibrary(ctx)
}
return false
}
func ToFileGroupAsLibrary(ctx BazelConversionPathContext, name string) (FileGroupAsLibrary, bool) {
if module, ok := ctx.ModuleFromName(name); ok {
if IsFilegroup(ctx, module) {
if fg, ok := module.(FileGroupAsLibrary); ok {
return fg, true
}
}
}
return nil, false
}
// Defaults
type FileGroupDefaults struct {
ModuleBase

View File

@@ -40,17 +40,8 @@ func TestFileGroupWithPathProp(t *testing.T) {
}
for _, testCase := range testCases {
outBaseDir := "outputbase"
result := GroupFixturePreparers(
PrepareForTestWithFilegroup,
FixtureModifyConfig(func(config Config) {
config.BazelContext = MockBazelContext{
OutputBaseDir: outBaseDir,
LabelToOutputFiles: map[string][]string{
"//:baz": []string{"a/b/c/d/test.aidl"},
},
}
}),
).RunTestWithBp(t, testCase.bp)
fg := result.Module("baz", "").(*fileGroup)

View File

@@ -15,12 +15,7 @@
package android
import (
"fmt"
"os"
"github.com/google/blueprint"
"android/soong/bazel"
)
type licenseKindDependencyTag struct {
@@ -53,54 +48,13 @@ type licenseProperties struct {
Visibility []string
}
var _ Bazelable = &licenseModule{}
type licenseModule struct {
ModuleBase
DefaultableModuleBase
BazelModuleBase
properties licenseProperties
}
type bazelLicenseAttributes struct {
License_kinds []string
Copyright_notice *string
License_text bazel.LabelAttribute
Package_name *string
Visibility []string
}
func (m *licenseModule) ConvertWithBp2build(ctx Bp2buildMutatorContext) {
attrs := &bazelLicenseAttributes{
License_kinds: m.properties.License_kinds,
Copyright_notice: m.properties.Copyright_notice,
Package_name: m.properties.Package_name,
Visibility: m.properties.Visibility,
}
// TODO(asmundak): Soong supports multiple license texts while Bazel's license
// rule does not. Have android_license create a genrule to concatenate multiple
// license texts.
if len(m.properties.License_text) > 1 && ctx.Config().IsEnvTrue("BP2BUILD_VERBOSE") {
fmt.Fprintf(os.Stderr, "warning: using only the first license_text item from //%s:%s\n",
ctx.ModuleDir(), m.Name())
}
if len(m.properties.License_text) >= 1 {
attrs.License_text.SetValue(BazelLabelForModuleSrcSingle(ctx, m.properties.License_text[0]))
}
ctx.CreateBazelTargetModule(
bazel.BazelTargetModuleProperties{
Rule_class: "android_license",
Bzl_load_location: "//build/bazel/rules/license:license.bzl",
},
CommonAttributes{
Name: m.Name(),
},
attrs)
}
func (m *licenseModule) DepsMutator(ctx BottomUpMutatorContext) {
for i, license := range m.properties.License_kinds {
for j := i + 1; j < len(m.properties.License_kinds); j++ {
@@ -131,14 +85,13 @@ func LicenseFactory() Module {
module := &licenseModule{}
base := module.base()
module.AddProperties(&base.nameProperties, &module.properties, &base.commonProperties.BazelConversionStatus)
module.AddProperties(&base.nameProperties, &module.properties)
// The visibility property needs to be checked and parsed by the visibility module.
setPrimaryVisibilityProperty(module, "visibility", &module.properties.Visibility)
initAndroidModuleBase(module)
InitDefaultableModule(module)
InitBazelModule(module)
return module
}

View File

@@ -14,8 +14,6 @@
package android
import "android/soong/bazel"
func init() {
RegisterLicenseKindBuildComponents(InitRegistrationContext)
}
@@ -34,39 +32,13 @@ type licenseKindProperties struct {
Visibility []string
}
var _ Bazelable = &licenseKindModule{}
type licenseKindModule struct {
ModuleBase
DefaultableModuleBase
BazelModuleBase
properties licenseKindProperties
}
type bazelLicenseKindAttributes struct {
Conditions []string
Url string
Visibility []string
}
func (m *licenseKindModule) ConvertWithBp2build(ctx Bp2buildMutatorContext) {
attrs := &bazelLicenseKindAttributes{
Conditions: m.properties.Conditions,
Url: m.properties.Url,
Visibility: m.properties.Visibility,
}
ctx.CreateBazelTargetModule(
bazel.BazelTargetModuleProperties{
Rule_class: "license_kind",
Bzl_load_location: "@rules_license//rules:license_kind.bzl",
},
CommonAttributes{
Name: m.Name(),
},
attrs)
}
func (m *licenseKindModule) DepsMutator(ctx BottomUpMutatorContext) {
// Nothing to do.
}
@@ -79,14 +51,13 @@ func LicenseKindFactory() Module {
module := &licenseKindModule{}
base := module.base()
module.AddProperties(&base.nameProperties, &module.properties, &base.commonProperties.BazelConversionStatus)
module.AddProperties(&base.nameProperties, &module.properties)
// The visibility property needs to be checked and parsed by the visibility module.
setPrimaryVisibilityProperty(module, "visibility", &module.properties.Visibility)
initAndroidModuleBase(module)
InitDefaultableModule(module)
InitBazelModule(module)
return module
}

View File

@@ -15,9 +15,6 @@
package android
import (
"path/filepath"
"android/soong/bazel"
"github.com/google/blueprint"
"github.com/google/blueprint/proptools"
)
@@ -40,52 +37,12 @@ type packageProperties struct {
Default_applicable_licenses []string
}
type bazelPackageAttributes struct {
Default_visibility []string
Default_package_metadata bazel.LabelListAttribute
}
type packageModule struct {
ModuleBase
BazelModuleBase
properties packageProperties
}
var _ Bazelable = &packageModule{}
func (p *packageModule) ConvertWithBp2build(ctx Bp2buildMutatorContext) {
defaultPackageMetadata := bazel.MakeLabelListAttribute(BazelLabelForModuleDeps(ctx, p.properties.Default_applicable_licenses))
// If METADATA file exists in the package, add it to package(default_package_metadata=) using a
// filegroup(name="default_metadata_file") which can be accessed later on each module in Bazel
// using attribute "applicable_licenses".
// Attribute applicable_licenses of filegroup "default_metadata_file" has to be set to [],
// otherwise Bazel reports cyclic reference error.
if existed, _, _ := ctx.Config().fs.Exists(filepath.Join(ctx.ModuleDir(), "METADATA")); existed {
ctx.CreateBazelTargetModule(
bazel.BazelTargetModuleProperties{
Rule_class: "filegroup",
},
CommonAttributes{Name: "default_metadata_file"},
&bazelFilegroupAttributes{
Srcs: bazel.MakeLabelListAttribute(BazelLabelForModuleSrc(ctx, []string{"METADATA"})),
Applicable_licenses: bazel.LabelListAttribute{Value: bazel.LabelList{Includes: []bazel.Label{}}, EmitEmptyList: true},
})
defaultPackageMetadata.Value.Add(&bazel.Label{Label: ":default_metadata_file"})
}
ctx.CreateBazelTargetModule(
bazel.BazelTargetModuleProperties{
Rule_class: "package",
},
CommonAttributes{},
&bazelPackageAttributes{
Default_package_metadata: defaultPackageMetadata,
// FIXME(asmundak): once b/221436821 is resolved
Default_visibility: []string{"//visibility:public"},
})
}
func (p *packageModule) GenerateAndroidBuildActions(ModuleContext) {
// Nothing to do.
}
@@ -102,7 +59,7 @@ func (p *packageModule) qualifiedModuleId(ctx BaseModuleContext) qualifiedModule
func PackageFactory() Module {
module := &packageModule{}
module.AddProperties(&module.properties, &module.commonProperties.BazelConversionStatus)
module.AddProperties(&module.properties)
// The name is the relative path from build root to the directory containing this
// module. Set that name at the earliest possible moment that information is available
@@ -119,7 +76,5 @@ func PackageFactory() Module {
// its checking and parsing phases so make it the primary licenses property.
setPrimaryLicensesProperty(module, "default_applicable_licenses", &module.properties.Default_applicable_licenses)
InitBazelModule(module)
return module
}

View File

@@ -15,11 +15,7 @@
package android
import (
"path/filepath"
"strings"
"sync"
"android/soong/bazel"
"github.com/google/blueprint"
"github.com/google/blueprint/proptools"
@@ -154,278 +150,3 @@ func ProtoRule(rule *RuleBuilder, protoFile Path, flags ProtoFlags, deps Paths,
rule.Command().
BuiltTool("dep_fixer").Flag(depFile.String())
}
// Bp2buildProtoInfo contains information necessary to pass on to language specific conversion.
type Bp2buildProtoInfo struct {
Type *string
Proto_libs bazel.LabelList
Transitive_proto_libs bazel.LabelList
}
type ProtoAttrs struct {
Srcs bazel.LabelListAttribute
Import_prefix *string
Strip_import_prefix *string
Deps bazel.LabelListAttribute
}
// For each package in the include_dirs property a proto_library target should
// be added to the BUILD file in that package and a mapping should be added here
var includeDirsToProtoDeps = map[string]string{
"external/protobuf/src": "//external/protobuf:libprotobuf-proto",
}
// Partitions srcs by the pkg it is in
// srcs has been created using `TransformSubpackagePaths`
// This function uses existence of Android.bp/BUILD files to create a label that is compatible with the package structure of bp2build workspace
func partitionSrcsByPackage(currentDir string, srcs bazel.LabelList) map[string]bazel.LabelList {
getPackageFromLabel := func(label string) string {
// Remove any preceding //
label = strings.TrimPrefix(label, "//")
split := strings.Split(label, ":")
if len(split) == 1 {
// e.g. foo.proto
return currentDir
} else if split[0] == "" {
// e.g. :foo.proto
return currentDir
} else {
return split[0]
}
}
pkgToSrcs := map[string]bazel.LabelList{}
for _, src := range srcs.Includes {
pkg := getPackageFromLabel(src.Label)
list := pkgToSrcs[pkg]
list.Add(&src)
pkgToSrcs[pkg] = list
}
return pkgToSrcs
}
// Bp2buildProtoProperties converts proto properties, creating a proto_library and returning the
// information necessary for language-specific handling.
func Bp2buildProtoProperties(ctx Bp2buildMutatorContext, m *ModuleBase, srcs bazel.LabelListAttribute) (Bp2buildProtoInfo, bool) {
var info Bp2buildProtoInfo
if srcs.IsEmpty() {
return info, false
}
var protoLibraries bazel.LabelList
var transitiveProtoLibraries bazel.LabelList
var directProtoSrcs bazel.LabelList
// For filegroups that should be converted to proto_library just collect the
// labels of converted proto_library targets.
for _, protoSrc := range srcs.Value.Includes {
src := protoSrc.OriginalModuleName
if fg, ok := ToFileGroupAsLibrary(ctx, src); ok &&
fg.ShouldConvertToProtoLibrary(ctx) {
protoLibraries.Add(&bazel.Label{
Label: fg.GetProtoLibraryLabel(ctx),
})
} else {
directProtoSrcs.Add(&protoSrc)
}
}
name := m.Name() + "_proto"
depsFromFilegroup := protoLibraries
var canonicalPathFromRoot bool
if len(directProtoSrcs.Includes) > 0 {
pkgToSrcs := partitionSrcsByPackage(ctx.ModuleDir(), directProtoSrcs)
protoIncludeDirs := []string{}
for _, pkg := range SortedStringKeys(pkgToSrcs) {
srcs := pkgToSrcs[pkg]
attrs := ProtoAttrs{
Srcs: bazel.MakeLabelListAttribute(srcs),
}
attrs.Deps.Append(bazel.MakeLabelListAttribute(depsFromFilegroup))
for axis, configToProps := range m.GetArchVariantProperties(ctx, &ProtoProperties{}) {
for _, rawProps := range configToProps {
var props *ProtoProperties
var ok bool
if props, ok = rawProps.(*ProtoProperties); !ok {
ctx.ModuleErrorf("Could not cast ProtoProperties to expected type")
}
if axis == bazel.NoConfigAxis {
info.Type = props.Proto.Type
canonicalPathFromRoot = proptools.BoolDefault(props.Proto.Canonical_path_from_root, canonicalPathFromRootDefault)
if !canonicalPathFromRoot {
// an empty string indicates to strips the package path
path := ""
attrs.Strip_import_prefix = &path
}
for _, dir := range props.Proto.Include_dirs {
if dep, ok := includeDirsToProtoDeps[dir]; ok {
attrs.Deps.Add(bazel.MakeLabelAttribute(dep))
} else {
protoIncludeDirs = append(protoIncludeDirs, dir)
}
}
// proto.local_include_dirs are similar to proto.include_dirs, except that it is relative to the module directory
for _, dir := range props.Proto.Local_include_dirs {
relativeToTop := pathForModuleSrc(ctx, dir).String()
protoIncludeDirs = append(protoIncludeDirs, relativeToTop)
}
} else if props.Proto.Type != info.Type && props.Proto.Type != nil {
ctx.ModuleErrorf("Cannot handle arch-variant types for protos at this time.")
}
}
}
if p, ok := m.module.(PkgPathInterface); ok && p.PkgPath(ctx) != nil {
// python_library with pkg_path
// proto_library for this module should have the pkg_path as the import_prefix
attrs.Import_prefix = p.PkgPath(ctx)
attrs.Strip_import_prefix = proptools.StringPtr("")
}
tags := ApexAvailableTagsWithoutTestApexes(ctx, ctx.Module())
moduleDir := ctx.ModuleDir()
if !canonicalPathFromRoot {
// Since we are creating the proto_library in a subpackage, set the import_prefix relative to the current package
if rel, err := filepath.Rel(moduleDir, pkg); err != nil {
ctx.ModuleErrorf("Could not get relative path for %v %v", pkg, err)
} else if rel != "." {
attrs.Import_prefix = &rel
}
}
// TODO - b/246997908: Handle potential orphaned proto_library targets
// To create proto_library targets in the same package, we split the .proto files
// This means that if a proto_library in a subpackage imports another proto_library from the parent package
// (or a different subpackage), it will not find it.
// The CcProtoGen action itself runs fine because we construct the correct ProtoInfo,
// but the FileDescriptorSet of each proto_library might not be compile-able
//
// Add manual tag if either
// 1. .proto files are in more than one package
// 2. proto.include_dirs is not empty
if len(SortedStringKeys(pkgToSrcs)) > 1 || len(protoIncludeDirs) > 0 {
tags.Append(bazel.MakeStringListAttribute([]string{"manual"}))
}
ctx.CreateBazelTargetModule(
bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
CommonAttributes{Name: name, Dir: proptools.StringPtr(pkg), Tags: tags},
&attrs,
)
l := ""
if pkg == moduleDir { // same package that the original module lives in
l = ":" + name
} else {
l = "//" + pkg + ":" + name
}
protoLibraries.Add(&bazel.Label{
Label: l,
})
}
// Partitioning by packages can create dupes of protoIncludeDirs, so dedupe it first.
protoLibrariesInIncludeDir := createProtoLibraryTargetsForIncludeDirs(ctx, SortedUniqueStrings(protoIncludeDirs))
transitiveProtoLibraries.Append(protoLibrariesInIncludeDir)
}
info.Proto_libs = protoLibraries
info.Transitive_proto_libs = transitiveProtoLibraries
return info, true
}
// PkgPathInterface is used as a type assertion in bp2build to get pkg_path property of python_library_host
type PkgPathInterface interface {
PkgPath(ctx BazelConversionContext) *string
}
var (
protoIncludeDirGeneratedSuffix = ".include_dir_bp2build_generated_proto"
protoIncludeDirsBp2buildKey = NewOnceKey("protoIncludeDirsBp2build")
)
func getProtoIncludeDirsBp2build(config Config) *sync.Map {
return config.Once(protoIncludeDirsBp2buildKey, func() interface{} {
return &sync.Map{}
}).(*sync.Map)
}
// key for dynamically creating proto_library per proto.include_dirs
type protoIncludeDirKey struct {
dir string
subpackgeInDir string
}
// createProtoLibraryTargetsForIncludeDirs creates additional proto_library targets for .proto files in includeDirs
// Since Bazel imposes a constratint that the proto_library must be in the same package as the .proto file, this function
// might create the targets in a subdirectory of `includeDir`
// Returns the labels of the proto_library targets
func createProtoLibraryTargetsForIncludeDirs(ctx Bp2buildMutatorContext, includeDirs []string) bazel.LabelList {
var ret bazel.LabelList
for _, dir := range includeDirs {
if exists, _, _ := ctx.Config().fs.Exists(filepath.Join(dir, "Android.bp")); !exists {
ctx.ModuleErrorf("TODO: Add support for proto.include_dir: %v. This directory does not contain an Android.bp file", dir)
}
dirMap := getProtoIncludeDirsBp2build(ctx.Config())
// Find all proto file targets in this dir
protoLabelsInDir := BazelLabelForSrcPatternExcludes(ctx, dir, "**/*.proto", []string{})
// Partition the labels by package and subpackage(s)
protoLabelelsPartitionedByPkg := partitionSrcsByPackage(dir, protoLabelsInDir)
for _, pkg := range SortedStringKeys(protoLabelelsPartitionedByPkg) {
label := strings.ReplaceAll(dir, "/", ".") + protoIncludeDirGeneratedSuffix
ret.Add(&bazel.Label{
Label: "//" + pkg + ":" + label,
})
key := protoIncludeDirKey{dir: dir, subpackgeInDir: pkg}
if _, exists := dirMap.LoadOrStore(key, true); exists {
// A proto_library has already been created for this package relative to this include dir
continue
}
srcs := protoLabelelsPartitionedByPkg[pkg]
rel, err := filepath.Rel(dir, pkg)
if err != nil {
ctx.ModuleErrorf("Could not create a proto_library in pkg %v due to %v\n", pkg, err)
}
// Create proto_library
attrs := ProtoAttrs{
Srcs: bazel.MakeLabelListAttribute(srcs),
Strip_import_prefix: proptools.StringPtr(""),
}
if rel != "." {
attrs.Import_prefix = proptools.StringPtr(rel)
}
// If a specific directory is listed in proto.include_dirs of two separate modules (one host-specific and another device-specific),
// we do not want to create the proto_library with target_compatible_with of the first visited of these two modules
// As a workarounds, delete `target_compatible_with`
alwaysEnabled := bazel.BoolAttribute{}
alwaysEnabled.Value = proptools.BoolPtr(true)
// Add android and linux explicitly so that fillcommonbp2buildmoduleattrs can override these configs
// When we extend b support for other os'es (darwin/windows), we should add those configs here as well
alwaysEnabled.SetSelectValue(bazel.OsConfigurationAxis, bazel.OsAndroid, proptools.BoolPtr(true))
alwaysEnabled.SetSelectValue(bazel.OsConfigurationAxis, bazel.OsLinux, proptools.BoolPtr(true))
ctx.CreateBazelTargetModuleWithRestrictions(
bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
CommonAttributes{
Name: label,
Dir: proptools.StringPtr(pkg),
// This proto_library is used to construct a ProtoInfo
// But it might not be buildable on its own
Tags: bazel.MakeStringListAttribute([]string{"manual"}),
},
&attrs,
alwaysEnabled,
)
}
}
return ret
}

View File

@@ -187,7 +187,6 @@ func (*soongConfigModuleTypeImport) GenerateAndroidBuildActions(ModuleContext) {
type soongConfigModuleTypeModule struct {
ModuleBase
BazelModuleBase
properties soongconfig.ModuleTypeProperties
}
@@ -395,10 +394,6 @@ func loadSoongConfigModuleTypeDefinition(ctx LoadHookContext, from string) map[s
return (map[string]blueprint.ModuleFactory)(nil)
}
if ctx.Config().BuildMode == Bp2build {
ctx.Config().Bp2buildSoongConfigDefinitions.AddVars(mtDef)
}
globalModuleTypes := ctx.moduleFactories()
factories := make(map[string]blueprint.ModuleFactory)
@@ -406,7 +401,7 @@ func loadSoongConfigModuleTypeDefinition(ctx LoadHookContext, from string) map[s
for name, moduleType := range mtDef.ModuleTypes {
factory := globalModuleTypes[moduleType.BaseModuleType]
if factory != nil {
factories[name] = configModuleFactory(factory, moduleType, ctx.Config().BuildMode == Bp2build)
factories[name] = configModuleFactory(factory, moduleType)
} else {
reportErrors(ctx, from,
fmt.Errorf("missing global module type factory for %q", moduleType.BaseModuleType))
@@ -474,7 +469,7 @@ var _ soongconfig.SoongConfig = (*tracingConfig)(nil)
// configModuleFactory takes an existing soongConfigModuleFactory and a
// ModuleType to create a new ModuleFactory that uses a custom loadhook.
func configModuleFactory(factory blueprint.ModuleFactory, moduleType *soongconfig.ModuleType, bp2build bool) blueprint.ModuleFactory {
func configModuleFactory(factory blueprint.ModuleFactory, moduleType *soongconfig.ModuleType) blueprint.ModuleFactory {
// Defer creation of conditional properties struct until the first call from the factory
// method. That avoids having to make a special call to the factory to create the properties
// structs from which the conditional properties struct is created. This is needed in order to
@@ -515,40 +510,22 @@ func configModuleFactory(factory blueprint.ModuleFactory, moduleType *soongconfi
conditionalProps := proptools.CloneEmptyProperties(conditionalFactoryProps)
props = append(props, conditionalProps.Interface())
if bp2build {
// The loadhook is different for bp2build, since we don't want to set a specific
// set of property values based on a vendor var -- we want __all of them__ to
// generate select statements, so we put the entire soong_config_variables
// struct, together with the namespace representing those variables, while
// creating the custom module with the factory.
AddLoadHook(module, func(ctx LoadHookContext) {
if m, ok := module.(Bazelable); ok {
m.SetBaseModuleType(moduleType.BaseModuleType)
// Instead of applying all properties, keep the entire conditionalProps struct as
// part of the custom module so dependent modules can create the selects accordingly
m.setNamespacedVariableProps(namespacedVariableProperties{
moduleType.ConfigNamespace: []interface{}{conditionalProps.Interface()},
})
}
})
} else {
// Regular Soong operation wraps the existing module factory with a
// conditional on Soong config variables by reading the product
// config variables from Make.
AddLoadHook(module, func(ctx LoadHookContext) {
tracingConfig := newTracingConfig(ctx.Config().VendorConfig(moduleType.ConfigNamespace))
newProps, err := soongconfig.PropertiesToApply(moduleType, conditionalProps, tracingConfig)
if err != nil {
ctx.ModuleErrorf("%s", err)
return
}
for _, ps := range newProps {
ctx.AppendProperties(ps)
}
// Regular Soong operation wraps the existing module factory with a
// conditional on Soong config variables by reading the product
// config variables from Make.
AddLoadHook(module, func(ctx LoadHookContext) {
tracingConfig := newTracingConfig(ctx.Config().VendorConfig(moduleType.ConfigNamespace))
newProps, err := soongconfig.PropertiesToApply(moduleType, conditionalProps, tracingConfig)
if err != nil {
ctx.ModuleErrorf("%s", err)
return
}
for _, ps := range newProps {
ctx.AppendProperties(ps)
}
module.(Module).base().commonProperties.SoongConfigTrace = tracingConfig.getTrace()
})
}
module.(Module).base().commonProperties.SoongConfigTrace = tracingConfig.getTrace()
})
return module, props
}
}