Gen a header library when genrules export includes

Embedding multiple includes from a genrule may be difficult to read,
instead we generate a header library that contains the headers and all
include dirs that Soong generates.

Test: go test bp2build tests
Change-Id: I590c74c133f015f27cccf5a2fd916153ad9c125e
This commit is contained in:
Liz Kammer
2023-07-18 11:39:30 -04:00
parent 5f5dbaad65
commit 0db0e34c68
4 changed files with 175 additions and 3 deletions

View File

@@ -994,6 +994,7 @@ func (m *Module) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
tags := android.ApexAvailableTagsWithoutTestApexes(ctx, m)
bazelName := m.Name()
if ctx.ModuleType() == "gensrcs" {
props := bazel.BazelTargetModuleProperties{
Rule_class: "gensrcs",
@@ -1021,7 +1022,6 @@ func (m *Module) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
break
}
}
bazelName := m.Name()
for _, out := range outs {
if out == bazelName {
// This is a workaround to circumvent a Bazel warning where a genrule's
@@ -1046,6 +1046,54 @@ func (m *Module) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
Tags: tags,
}, attrs)
}
if m.needsCcLibraryHeadersBp2build() {
includeDirs := make([]string, len(m.properties.Export_include_dirs)*2)
for i, dir := range m.properties.Export_include_dirs {
includeDirs[i*2] = dir
includeDirs[i*2+1] = filepath.Clean(filepath.Join(ctx.ModuleDir(), dir))
}
attrs := &ccHeaderLibraryAttrs{
Hdrs: []string{":" + bazelName},
Export_includes: includeDirs,
}
props := bazel.BazelTargetModuleProperties{
Rule_class: "cc_library_headers",
Bzl_load_location: "//build/bazel/rules/cc:cc_library_headers.bzl",
}
ctx.CreateBazelTargetModule(props, android.CommonAttributes{
Name: m.Name() + genruleHeaderLibrarySuffix,
Tags: tags,
}, attrs)
}
}
const genruleHeaderLibrarySuffix = "__header_library"
func (m *Module) needsCcLibraryHeadersBp2build() bool {
return len(m.properties.Export_include_dirs) > 0
}
// GenruleCcHeaderMapper is a bazel.LabelMapper function to map genrules to a cc_library_headers
// target when they export multiple include directories.
func GenruleCcHeaderLabelMapper(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) {
mod, exists := ctx.ModuleFromName(label.OriginalModuleName)
if !exists {
return label.Label, false
}
if m, ok := mod.(*Module); ok {
if m.needsCcLibraryHeadersBp2build() {
return label.Label + genruleHeaderLibrarySuffix, true
}
}
return label.Label, false
}
type ccHeaderLibraryAttrs struct {
Hdrs []string
Export_includes []string
}
var Bool = proptools.Bool
@@ -1099,6 +1147,7 @@ func getSandboxingAllowlistSets(ctx android.PathContext) *sandboxingAllowlistSet
}
}).(*sandboxingAllowlistSets)
}
func getSandboxedRuleBuilder(ctx android.ModuleContext, r *android.RuleBuilder) *android.RuleBuilder {
if !ctx.DeviceConfig().GenruleSandboxing() {
return r.SandboxTools()