Converters for contributions to systemapi and vendorapi
The module types in scope of this conversion are
1. cc_library and cc_library_shared (non-null llndk or stubs prop)
2. cc_library_headers (all)
For (2), we need some postprocessing on the results of the parser
bp2BuildParseBaseProps. This is necessary because arch and os specific
API exports need to be flattened and added to the generateed API headers
target along NoConfigAxis
e.g.
```
The api equivalent of
cc_library_headers (
name = "lifoo",
deps = select({
"//build/bazel/platforms/arch:arm": ["arm_deps"],
"//build/bazel/platforms/arch:arm64": ["arm64_deps"],
}),
)
should be
cc_api_library_headers (
name = "lifoo",
deps = ["arm_deps", "arm64_deps"],
)
```
For (1), we also need to generate 1:many header api targets so that
arch-specific deps can propagate arch metadata to the top-level
api_domain rule
Test: go test ./bp2build
Test: go test ./cc
Change-Id: Ie40cba1ac8e89f290b3d926c190d5e93abd52859
diff --git a/cc/cc.go b/cc/cc.go
index d4eaa53..d173cc1 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -3751,7 +3751,18 @@
var _ android.ApiProvider = (*Module)(nil)
func (c *Module) ConvertWithApiBp2build(ctx android.TopDownMutatorContext) {
+ if c.IsPrebuilt() {
+ return
+ }
switch c.typ() {
+ case fullLibrary:
+ apiContributionBp2Build(ctx, c)
+ case sharedLibrary:
+ apiContributionBp2Build(ctx, c)
+ case headerLibrary:
+ // Aggressively generate api targets for all header modules
+ // This is necessary since the header module does not know if it is a dep of API surface stub library
+ apiLibraryHeadersBp2Build(ctx, c)
case ndkLibrary:
ndkLibraryBp2build(ctx, c)
}
diff --git a/cc/library.go b/cc/library.go
index 13a7a3e..4c09403 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -30,6 +30,7 @@
"github.com/google/blueprint"
"github.com/google/blueprint/pathtools"
+ "github.com/google/blueprint/proptools"
)
// LibraryProperties is a collection of properties shared by cc library rules/cc.
@@ -466,6 +467,147 @@
}
}
+func apiContributionBp2Build(ctx android.TopDownMutatorContext, module *Module) {
+ apiSurfaces := make([]string, 0)
+ apiHeaders := make([]string, 0)
+ // systemapi (non-null `stubs` property)
+ if module.HasStubsVariants() {
+ apiSurfaces = append(apiSurfaces, android.SystemApi.String())
+ apiIncludes := getSystemApiIncludes(ctx, module)
+ if !apiIncludes.isEmpty() {
+ createApiHeaderTarget(ctx, apiIncludes)
+ apiHeaders = append(apiHeaders, apiIncludes.name)
+ }
+ }
+ // vendorapi (non-null `llndk` property)
+ if module.HasLlndkStubs() {
+ apiSurfaces = append(apiSurfaces, android.VendorApi.String())
+ apiIncludes := getVendorApiIncludes(ctx, module)
+ if !apiIncludes.isEmpty() {
+ createApiHeaderTarget(ctx, apiIncludes)
+ apiHeaders = append(apiHeaders, apiIncludes.name)
+ }
+ }
+ // create a target only if this module contributes to an api surface
+ // TODO: Currently this does not distinguish systemapi-only headers and vendrorapi-only headers
+ // TODO: Update so that systemapi-only headers do not get exported to vendorapi (and vice-versa)
+ if len(apiSurfaces) > 0 {
+ props := bazel.BazelTargetModuleProperties{
+ Rule_class: "cc_api_contribution",
+ Bzl_load_location: "//build/bazel/rules/apis:cc_api_contribution.bzl",
+ }
+ attrs := &bazelCcApiContributionAttributes{
+ Library_name: module.Name(),
+ Api_surfaces: bazel.MakeStringListAttribute(apiSurfaces),
+ Api: apiLabelAttribute(ctx, module),
+ Hdrs: bazel.MakeLabelListAttribute(
+ bazel.MakeLabelListFromTargetNames(apiHeaders),
+ ),
+ }
+ ctx.CreateBazelTargetModule(
+ props,
+ android.CommonAttributes{
+ Name: android.ApiContributionTargetName(module.Name()),
+ SkipData: proptools.BoolPtr(true),
+ },
+ attrs,
+ )
+ }
+}
+
+// Native apis are versioned in a single .map.txt for all api surfaces
+// Pick any one of the .map.txt files
+func apiLabelAttribute(ctx android.TopDownMutatorContext, module *Module) bazel.LabelAttribute {
+ var apiFile *string
+ linker := module.linker.(*libraryDecorator)
+ if llndkApi := linker.Properties.Llndk.Symbol_file; llndkApi != nil {
+ apiFile = llndkApi
+ } else if systemApi := linker.Properties.Stubs.Symbol_file; systemApi != nil {
+ apiFile = systemApi
+ } else {
+ ctx.ModuleErrorf("API surface library does not have any API file")
+ }
+ apiLabel := android.BazelLabelForModuleSrcSingle(ctx, proptools.String(apiFile)).Label
+ return *bazel.MakeLabelAttribute(apiLabel)
+}
+
+// wrapper struct to flatten the arch and os specific export_include_dirs
+// flattening is necessary since we want to export apis of all arches even when we build for x86 (e.g.)
+type bazelCcApiLibraryHeadersAttributes struct {
+ bazelCcLibraryHeadersAttributes
+
+ Arch *string
+}
+
+func (a *bazelCcApiLibraryHeadersAttributes) isEmpty() bool {
+ return a.Export_includes.IsEmpty() &&
+ a.Export_system_includes.IsEmpty() &&
+ a.Deps.IsEmpty()
+}
+
+type apiIncludes struct {
+ name string // name of the Bazel target in the generated bp2build workspace
+ attrs bazelCcApiLibraryHeadersAttributes
+}
+
+func (includes *apiIncludes) isEmpty() bool {
+ return includes.attrs.isEmpty()
+}
+
+func (includes *apiIncludes) addDep(name string) {
+ l := bazel.Label{Label: ":" + name}
+ ll := bazel.MakeLabelList([]bazel.Label{l})
+ lla := bazel.MakeLabelListAttribute(ll)
+ includes.attrs.Deps.Append(lla)
+}
+
+func getSystemApiIncludes(ctx android.TopDownMutatorContext, c *Module) apiIncludes {
+ flagProps := c.library.(*libraryDecorator).flagExporter.Properties
+ linkProps := c.library.(*libraryDecorator).baseLinker.Properties
+ includes := android.FirstUniqueStrings(flagProps.Export_include_dirs)
+ systemIncludes := android.FirstUniqueStrings(flagProps.Export_system_include_dirs)
+ headerLibs := android.FirstUniqueStrings(linkProps.Export_header_lib_headers)
+ attrs := bazelCcLibraryHeadersAttributes{
+ Export_includes: bazel.MakeStringListAttribute(includes),
+ Export_system_includes: bazel.MakeStringListAttribute(systemIncludes),
+ Deps: bazel.MakeLabelListAttribute(apiHeaderLabels(ctx, headerLibs)),
+ }
+
+ return apiIncludes{
+ name: c.Name() + ".systemapi.headers",
+ attrs: bazelCcApiLibraryHeadersAttributes{
+ bazelCcLibraryHeadersAttributes: attrs,
+ },
+ }
+}
+
+func getVendorApiIncludes(ctx android.TopDownMutatorContext, c *Module) apiIncludes {
+ baseProps := c.library.(*libraryDecorator).flagExporter.Properties
+ llndkProps := c.library.(*libraryDecorator).Properties.Llndk
+ includes := baseProps.Export_include_dirs
+ systemIncludes := baseProps.Export_system_include_dirs
+ // LLNDK can override the base includes
+ if llndkIncludes := llndkProps.Override_export_include_dirs; llndkIncludes != nil {
+ includes = llndkIncludes
+ }
+ if proptools.Bool(llndkProps.Export_headers_as_system) {
+ systemIncludes = append(systemIncludes, includes...)
+ includes = nil
+ }
+
+ attrs := bazelCcLibraryHeadersAttributes{
+ Export_includes: bazel.MakeStringListAttribute(includes),
+ Export_system_includes: bazel.MakeStringListAttribute(systemIncludes),
+ Deps: bazel.MakeLabelListAttribute(apiHeaderLabels(ctx, llndkProps.Export_llndk_headers)),
+ }
+ return apiIncludes{
+ name: c.Name() + ".vendorapi.headers",
+ attrs: bazelCcApiLibraryHeadersAttributes{
+ bazelCcLibraryHeadersAttributes: attrs,
+ },
+ }
+}
+
// cc_library creates both static and/or shared libraries for a device and/or
// host. By default, a cc_library has a single variant that targets the device.
// Specifying `host_supported: true` also creates a library that targets the
diff --git a/cc/library_headers.go b/cc/library_headers.go
index a683f58..1c4f354 100644
--- a/cc/library_headers.go
+++ b/cc/library_headers.go
@@ -15,6 +15,8 @@
package cc
import (
+ "github.com/google/blueprint/proptools"
+
"android/soong/android"
"android/soong/bazel"
"android/soong/bazel/cquery"
@@ -145,3 +147,118 @@
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, attrs)
}
+
+// Append .contribution suffix to input labels
+func apiBazelTargets(ll bazel.LabelList) bazel.LabelList {
+ labels := make([]bazel.Label, 0)
+ for _, l := range ll.Includes {
+ labels = append(labels, bazel.Label{
+ Label: android.ApiContributionTargetName(l.Label),
+ })
+ }
+ return bazel.MakeLabelList(labels)
+}
+
+func apiLibraryHeadersBp2Build(ctx android.TopDownMutatorContext, module *Module) {
+ // cc_api_library_headers have a 1:1 mapping to arch/no-arch
+ // For API export, create a top-level arch-agnostic target and list the arch-specific targets as its deps
+
+ // arch-agnostic includes
+ apiIncludes := getSystemApiIncludes(ctx, module)
+ // arch and os specific includes
+ archApiIncludes, androidOsIncludes := archOsSpecificApiIncludes(ctx, module)
+ for _, arch := range allArches { // sorted iteration
+ archApiInclude := archApiIncludes[arch]
+ if !archApiInclude.isEmpty() {
+ createApiHeaderTarget(ctx, archApiInclude)
+ apiIncludes.addDep(archApiInclude.name)
+ }
+ }
+ // os==android includes
+ if !androidOsIncludes.isEmpty() {
+ createApiHeaderTarget(ctx, androidOsIncludes)
+ apiIncludes.addDep(androidOsIncludes.name)
+ }
+
+ if !apiIncludes.isEmpty() {
+ // override the name from <mod>.systemapi.headers --> <mod>.contribution
+ apiIncludes.name = android.ApiContributionTargetName(module.Name())
+ createApiHeaderTarget(ctx, apiIncludes)
+ }
+}
+
+func createApiHeaderTarget(ctx android.TopDownMutatorContext, includes apiIncludes) {
+ props := bazel.BazelTargetModuleProperties{
+ Rule_class: "cc_api_library_headers",
+ Bzl_load_location: "//build/bazel/rules/apis:cc_api_contribution.bzl",
+ }
+ ctx.CreateBazelTargetModule(
+ props,
+ android.CommonAttributes{
+ Name: includes.name,
+ SkipData: proptools.BoolPtr(true),
+ },
+ &includes.attrs,
+ )
+}
+
+var (
+ allArches = []string{"arm", "arm64", "x86", "x86_64"}
+)
+
+type archApiIncludes map[string]apiIncludes
+
+func archOsSpecificApiIncludes(ctx android.TopDownMutatorContext, module *Module) (archApiIncludes, apiIncludes) {
+ baseProps := bp2BuildParseBaseProps(ctx, module)
+ i := bp2BuildParseExportedIncludes(ctx, module, &baseProps.includes)
+ archRet := archApiIncludes{}
+ for _, arch := range allArches {
+ includes := i.Includes.SelectValue(
+ bazel.ArchConfigurationAxis,
+ arch)
+ systemIncludes := i.SystemIncludes.SelectValue(
+ bazel.ArchConfigurationAxis,
+ arch)
+ deps := baseProps.deps.SelectValue(
+ bazel.ArchConfigurationAxis,
+ arch)
+ attrs := bazelCcLibraryHeadersAttributes{
+ Export_includes: bazel.MakeStringListAttribute(includes),
+ Export_system_includes: bazel.MakeStringListAttribute(systemIncludes),
+ }
+ apiDeps := apiBazelTargets(deps)
+ if !apiDeps.IsEmpty() {
+ attrs.Deps = bazel.MakeLabelListAttribute(apiDeps)
+ }
+ apiIncludes := apiIncludes{
+ name: android.ApiContributionTargetName(module.Name()) + "." + arch,
+ attrs: bazelCcApiLibraryHeadersAttributes{
+ bazelCcLibraryHeadersAttributes: attrs,
+ Arch: proptools.StringPtr(arch),
+ },
+ }
+ archRet[arch] = apiIncludes
+ }
+
+ // apiIncludes for os == Android
+ androidOsDeps := baseProps.deps.SelectValue(bazel.OsConfigurationAxis, bazel.OsAndroid)
+ androidOsAttrs := bazelCcLibraryHeadersAttributes{
+ Export_includes: bazel.MakeStringListAttribute(
+ i.Includes.SelectValue(bazel.OsConfigurationAxis, bazel.OsAndroid),
+ ),
+ Export_system_includes: bazel.MakeStringListAttribute(
+ i.SystemIncludes.SelectValue(bazel.OsConfigurationAxis, bazel.OsAndroid),
+ ),
+ }
+ androidOsApiDeps := apiBazelTargets(androidOsDeps)
+ if !androidOsApiDeps.IsEmpty() {
+ androidOsAttrs.Deps = bazel.MakeLabelListAttribute(androidOsApiDeps)
+ }
+ osRet := apiIncludes{
+ name: android.ApiContributionTargetName(module.Name()) + ".androidos",
+ attrs: bazelCcApiLibraryHeadersAttributes{
+ bazelCcLibraryHeadersAttributes: androidOsAttrs,
+ },
+ }
+ return archRet, osRet
+}
diff --git a/cc/ndk_library.go b/cc/ndk_library.go
index 06ded3f..49a919e 100644
--- a/cc/ndk_library.go
+++ b/cc/ndk_library.go
@@ -581,12 +581,12 @@
}
// Names of the cc_api_header targets in the bp2build workspace
-func (s *stubDecorator) apiHeaderLabels(ctx android.TopDownMutatorContext) bazel.LabelList {
+func apiHeaderLabels(ctx android.TopDownMutatorContext, hdrLibs []string) bazel.LabelList {
addSuffix := func(ctx android.BazelConversionPathContext, module blueprint.Module) string {
label := android.BazelModuleLabel(ctx, module)
return android.ApiContributionTargetName(label)
}
- return android.BazelLabelForModuleDepsWithFn(ctx, s.properties.Export_header_libs, addSuffix)
+ return android.BazelLabelForModuleDepsWithFn(ctx, hdrLibs, addSuffix)
}
func ndkLibraryBp2build(ctx android.TopDownMutatorContext, m *Module) {
@@ -604,7 +604,7 @@
apiLabel := android.BazelLabelForModuleSrcSingle(ctx, proptools.String(symbolFile)).Label
attrs.Api = *bazel.MakeLabelAttribute(apiLabel)
}
- apiHeaders := stubLibrary.apiHeaderLabels(ctx)
+ apiHeaders := apiHeaderLabels(ctx, stubLibrary.properties.Export_header_libs)
attrs.Hdrs = bazel.MakeLabelListAttribute(apiHeaders)
apiContributionTargetName := android.ApiContributionTargetName(ctx.ModuleName())
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: apiContributionTargetName}, attrs)