Merge "Add bp2build support for cc_library_static."

This commit is contained in:
Rupert Shuttleworth
2021-03-25 21:01:29 +00:00
committed by Gerrit Code Review
6 changed files with 453 additions and 27 deletions

View File

@@ -10,6 +10,7 @@ bootstrap_go_package {
"blueprint-pathtools",
"soong",
"soong-android",
"soong-bazel",
"soong-cc-config",
"soong-etc",
"soong-genrule",

View File

@@ -27,6 +27,7 @@ import (
"github.com/google/blueprint/pathtools"
"android/soong/android"
"android/soong/bazel"
"android/soong/cc/config"
)
@@ -200,6 +201,8 @@ type FlagExporterProperties struct {
func init() {
RegisterLibraryBuildComponents(android.InitRegistrationContext)
android.RegisterBp2BuildMutator("cc_library_static", CcLibraryStaticBp2Build)
}
func RegisterLibraryBuildComponents(ctx android.RegistrationContext) {
@@ -2024,3 +2027,137 @@ func maybeInjectBoringSSLHash(ctx android.ModuleContext, outputFile android.Modu
return outputFile
}
func Bp2BuildParseHeaderLibs(ctx android.TopDownMutatorContext, module *Module) bazel.LabelList {
var headerLibs []string
for _, linkerProps := range module.linker.linkerProps() {
if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
headerLibs = baseLinkerProps.Header_libs
// FIXME: re-export include dirs from baseLinkerProps.Export_header_lib_headers?
break
}
}
headerLibsLabels := android.BazelLabelForModuleDeps(ctx, headerLibs)
return headerLibsLabels
}
func Bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) (bazel.LabelList, bazel.LabelList) {
libraryDecorator := module.linker.(*libraryDecorator)
includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs
includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...)
includeDirsLabels := android.BazelLabelForModuleSrc(ctx, includeDirs)
var includeDirGlobs []string
for _, includeDir := range includeDirs {
includeDirGlobs = append(includeDirGlobs, includeDir+"/**/*.h")
includeDirGlobs = append(includeDirGlobs, includeDir+"/**/*.inc")
includeDirGlobs = append(includeDirGlobs, includeDir+"/**/*.hpp")
}
headersLabels := android.BazelLabelForModuleSrc(ctx, includeDirGlobs)
return includeDirsLabels, headersLabels
}
type bazelCcLibraryStaticAttributes struct {
Copts []string
Srcs bazel.LabelList
Deps bazel.LabelList
Linkstatic bool
Includes bazel.LabelList
Hdrs bazel.LabelList
}
type bazelCcLibraryStatic struct {
android.BazelTargetModuleBase
bazelCcLibraryStaticAttributes
}
func BazelCcLibraryStaticFactory() android.Module {
module := &bazelCcLibraryStatic{}
module.AddProperties(&module.bazelCcLibraryStaticAttributes)
android.InitBazelTargetModule(module)
return module
}
func CcLibraryStaticBp2Build(ctx android.TopDownMutatorContext) {
module, ok := ctx.Module().(*Module)
if !ok {
// Not a cc module
return
}
if !module.ConvertWithBp2build(ctx) {
return
}
if ctx.ModuleType() != "cc_library_static" {
return
}
var copts []string
var srcs []string
var includeDirs []string
var localIncludeDirs []string
for _, props := range module.compiler.compilerProps() {
if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
copts = baseCompilerProps.Cflags
srcs = baseCompilerProps.Srcs
includeDirs = baseCompilerProps.Include_dirs
localIncludeDirs = baseCompilerProps.Local_include_dirs
break
}
}
srcsLabels := android.BazelLabelForModuleSrc(ctx, srcs)
var staticLibs []string
var wholeStaticLibs []string
for _, props := range module.linker.linkerProps() {
if baseLinkerProperties, ok := props.(*BaseLinkerProperties); ok {
staticLibs = baseLinkerProperties.Static_libs
wholeStaticLibs = baseLinkerProperties.Whole_static_libs
break
}
}
// FIXME: Treat Static_libs and Whole_static_libs differently?
allDeps := staticLibs
allDeps = append(allDeps, wholeStaticLibs...)
depsLabels := android.BazelLabelForModuleDeps(ctx, allDeps)
// FIXME: Unify absolute vs relative paths
// FIXME: Use -I copts instead of setting includes= ?
allIncludes := includeDirs
allIncludes = append(allIncludes, localIncludeDirs...)
includesLabels := android.BazelLabelForModuleSrc(ctx, allIncludes)
exportedIncludesLabels, exportedIncludesHeadersLabels := Bp2BuildParseExportedIncludes(ctx, module)
includesLabels.Append(exportedIncludesLabels)
headerLibsLabels := Bp2BuildParseHeaderLibs(ctx, module)
depsLabels.Append(headerLibsLabels)
attrs := &bazelCcLibraryStaticAttributes{
Copts: copts,
Srcs: bazel.UniqueBazelLabelList(srcsLabels),
Deps: bazel.UniqueBazelLabelList(depsLabels),
Linkstatic: true,
Includes: bazel.UniqueBazelLabelList(includesLabels),
Hdrs: bazel.UniqueBazelLabelList(exportedIncludesHeadersLabels),
}
props := bazel.BazelTargetModuleProperties{
Rule_class: "cc_library_static",
Bzl_load_location: "//build/bazel/rules:cc_library_static.bzl",
}
ctx.CreateBazelTargetModule(BazelCcLibraryStaticFactory, module.Name(), props, attrs)
}
func (m *bazelCcLibraryStatic) Name() string {
return m.BaseModuleName()
}
func (m *bazelCcLibraryStatic) GenerateAndroidBuildActions(ctx android.ModuleContext) {}

View File

@@ -94,35 +94,14 @@ func CcLibraryHeadersBp2Build(ctx android.TopDownMutatorContext) {
return
}
lib, _ := module.linker.(*libraryDecorator)
exportedIncludesLabels, exportedIncludesHeadersLabels := Bp2BuildParseExportedIncludes(ctx, module)
// list of directories that will be added to the include path (using -I) for this
// module and any module that links against this module.
includeDirs := lib.flagExporter.Properties.Export_system_include_dirs
includeDirs = append(includeDirs, lib.flagExporter.Properties.Export_include_dirs...)
includeDirLabels := android.BazelLabelForModuleSrc(ctx, includeDirs)
var includeDirGlobs []string
for _, includeDir := range includeDirs {
includeDirGlobs = append(includeDirGlobs, includeDir+"/**/*.h")
}
headerLabels := android.BazelLabelForModuleSrc(ctx, includeDirGlobs)
// list of modules that should only provide headers for this module.
var headerLibs []string
for _, linkerProps := range lib.linkerProps() {
if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
headerLibs = baseLinkerProps.Export_header_lib_headers
break
}
}
headerLibLabels := android.BazelLabelForModuleDeps(ctx, headerLibs)
headerLibsLabels := Bp2BuildParseHeaderLibs(ctx, module)
attrs := &bazelCcLibraryHeadersAttributes{
Includes: includeDirLabels,
Hdrs: headerLabels,
Deps: headerLibLabels,
Includes: exportedIncludesLabels,
Hdrs: exportedIncludesHeadersLabels,
Deps: headerLibsLabels,
}
props := bazel.BazelTargetModuleProperties{