Merge "Refactor code for partitions c srcs"

This commit is contained in:
Liz Kammer
2021-09-24 17:35:16 +00:00
committed by Gerrit Code Review
6 changed files with 496 additions and 128 deletions

View File

@@ -14,7 +14,6 @@
package cc
import (
"fmt"
"path/filepath"
"strings"
@@ -44,18 +43,6 @@ type staticOrSharedAttributes struct {
}
func groupSrcsByExtension(ctx android.TopDownMutatorContext, srcs bazel.LabelListAttribute) (cppSrcs, cSrcs, asSrcs bazel.LabelListAttribute) {
// Branch srcs into three language-specific groups.
// C++ is the "catch-all" group, and comprises generated sources because we don't
// know the language of these sources until the genrule is executed.
// TODO(b/190006308): Handle language detection of sources in a Bazel rule.
isCSrcOrFilegroup := func(s string) bool {
return strings.HasSuffix(s, ".c") || strings.HasSuffix(s, "_c_srcs")
}
isAsmSrcOrFilegroup := func(s string) bool {
return strings.HasSuffix(s, ".S") || strings.HasSuffix(s, ".s") || strings.HasSuffix(s, "_as_srcs")
}
// Check that a module is a filegroup type named <label>.
isFilegroupNamed := func(m android.Module, fullLabel string) bool {
if ctx.OtherModuleType(m) != "filegroup" {
@@ -64,54 +51,39 @@ func groupSrcsByExtension(ctx android.TopDownMutatorContext, srcs bazel.LabelLis
labelParts := strings.Split(fullLabel, ":")
if len(labelParts) > 2 {
// There should not be more than one colon in a label.
panic(fmt.Errorf("%s is not a valid Bazel label for a filegroup", fullLabel))
} else {
return m.Name() == labelParts[len(labelParts)-1]
ctx.ModuleErrorf("%s is not a valid Bazel label for a filegroup", fullLabel)
}
return m.Name() == labelParts[len(labelParts)-1]
}
// Convert the filegroup dependencies into the extension-specific filegroups
// filtered in the filegroup.bzl macro.
cppFilegroup := func(label string) string {
m, exists := ctx.ModuleFromName(label)
if exists {
aModule, _ := m.(android.Module)
if isFilegroupNamed(aModule, label) {
label = label + "_cpp_srcs"
// Convert filegroup dependencies into extension-specific filegroups filtered in the filegroup.bzl
// macro.
addSuffixForFilegroup := func(suffix string) bazel.LabelMapper {
return func(ctx bazel.OtherModuleContext, label string) (string, bool) {
m, exists := ctx.ModuleFromName(label)
if !exists {
return label, false
}
}
return label
}
cFilegroup := func(label string) string {
m, exists := ctx.ModuleFromName(label)
if exists {
aModule, _ := m.(android.Module)
if isFilegroupNamed(aModule, label) {
label = label + "_c_srcs"
if !isFilegroupNamed(aModule, label) {
return label, false
}
return label + suffix, true
}
return label
}
asFilegroup := func(label string) string {
m, exists := ctx.ModuleFromName(label)
if exists {
aModule, _ := m.(android.Module)
if isFilegroupNamed(aModule, label) {
label = label + "_as_srcs"
}
}
return label
}
cSrcs = bazel.MapLabelListAttribute(srcs, cFilegroup)
cSrcs = bazel.FilterLabelListAttribute(cSrcs, isCSrcOrFilegroup)
// TODO(b/190006308): Handle language detection of sources in a Bazel rule.
partitioned := bazel.PartitionLabelListAttribute(ctx, &srcs, bazel.LabelPartitions{
"c": bazel.LabelPartition{Extensions: []string{".c"}, LabelMapper: addSuffixForFilegroup("_c_srcs")},
"as": bazel.LabelPartition{Extensions: []string{".s", ".S"}, LabelMapper: addSuffixForFilegroup("_as_srcs")},
// C++ is the "catch-all" group, and comprises generated sources because we don't
// know the language of these sources until the genrule is executed.
"cpp": bazel.LabelPartition{Extensions: []string{".cpp", ".cc", ".cxx", ".mm"}, LabelMapper: addSuffixForFilegroup("_cpp_srcs"), Keep_remainder: true},
})
asSrcs = bazel.MapLabelListAttribute(srcs, asFilegroup)
asSrcs = bazel.FilterLabelListAttribute(asSrcs, isAsmSrcOrFilegroup)
cppSrcs = bazel.MapLabelListAttribute(srcs, cppFilegroup)
cppSrcs = bazel.SubtractBazelLabelListAttribute(cppSrcs, cSrcs)
cppSrcs = bazel.SubtractBazelLabelListAttribute(cppSrcs, asSrcs)
cSrcs = partitioned["c"]
asSrcs = partitioned["as"]
cppSrcs = partitioned["cpp"]
return
}