Incorporate cc_library_headers into mixed builds

Test: go soong tests
Test: bp2build generate & sync; mixed build libc; mixed build su (su is
      an Android.mk target that relies on converted a cc_library_headers)
Bug: 181552740
Change-Id: I9efd587970551fd41f642a208f0aa0a80e8694e0
diff --git a/cc/androidmk.go b/cc/androidmk.go
index 536efa4..8f3a652 100644
--- a/cc/androidmk.go
+++ b/cc/androidmk.go
@@ -191,17 +191,31 @@
 }
 
 func (library *libraryDecorator) androidMkWriteExportedFlags(entries *android.AndroidMkEntries) {
-	exportedFlags := library.flagExporter.flags
-	for _, dir := range library.flagExporter.dirs {
+	var exportedFlags []string
+	var includeDirs android.Paths
+	var systemIncludeDirs android.Paths
+	var exportedDeps android.Paths
+
+	if library.flagExporterInfo != nil {
+		exportedFlags = library.flagExporterInfo.Flags
+		includeDirs = library.flagExporterInfo.IncludeDirs
+		systemIncludeDirs = library.flagExporterInfo.SystemIncludeDirs
+		exportedDeps = library.flagExporterInfo.Deps
+	} else {
+		exportedFlags = library.flagExporter.flags
+		includeDirs = library.flagExporter.dirs
+		systemIncludeDirs = library.flagExporter.systemDirs
+		exportedDeps = library.flagExporter.deps
+	}
+	for _, dir := range includeDirs {
 		exportedFlags = append(exportedFlags, "-I"+dir.String())
 	}
-	for _, dir := range library.flagExporter.systemDirs {
+	for _, dir := range systemIncludeDirs {
 		exportedFlags = append(exportedFlags, "-isystem "+dir.String())
 	}
 	if len(exportedFlags) > 0 {
 		entries.AddStrings("LOCAL_EXPORT_CFLAGS", exportedFlags...)
 	}
-	exportedDeps := library.flagExporter.deps
 	if len(exportedDeps) > 0 {
 		entries.AddStrings("LOCAL_EXPORT_C_INCLUDE_DEPS", exportedDeps.Strings()...)
 	}
diff --git a/cc/library.go b/cc/library.go
index 9a2b02e..0b21a08 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -426,7 +426,8 @@
 	tocFile android.OptionalPath
 
 	flagExporter
-	stripper Stripper
+	flagExporterInfo *FlagExporterInfo
+	stripper         Stripper
 
 	// For whole_static_libs
 	objects Objects
diff --git a/cc/library_headers.go b/cc/library_headers.go
index 078242f..f2cb52b 100644
--- a/cc/library_headers.go
+++ b/cc/library_headers.go
@@ -43,6 +43,52 @@
 	ctx.RegisterModuleType("cc_prebuilt_library_headers", prebuiltLibraryHeaderFactory)
 }
 
+type libraryHeaderBazelHander struct {
+	bazelHandler
+
+	module  *Module
+	library *libraryDecorator
+}
+
+func (h *libraryHeaderBazelHander) generateBazelBuildActions(ctx android.ModuleContext, label string) bool {
+	bazelCtx := ctx.Config().BazelContext
+	ccInfo, ok, err := bazelCtx.GetCcInfo(label, ctx.Arch().ArchType)
+	if err != nil {
+		ctx.ModuleErrorf("Error getting Bazel CcInfo: %s", err)
+		return false
+	}
+	if !ok {
+		return false
+	}
+
+	outputPaths := ccInfo.OutputFiles
+	if len(outputPaths) != 1 {
+		ctx.ModuleErrorf("expected exactly one output file for %q, but got %q", label, outputPaths)
+		return false
+	}
+
+	outputPath := android.PathForBazelOut(ctx, outputPaths[0])
+	h.module.outputFile = android.OptionalPathForPath(outputPath)
+
+	// HeaderLibraryInfo is an empty struct to indicate to dependencies that this is a header library
+	ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{})
+
+	flagExporterInfo := flagExporterInfoFromCcInfo(ctx, ccInfo)
+	// Store flag info to be passed along to androimk
+	// TODO(b/184387147): Androidmk should be done in Bazel, not Soong.
+	h.library.flagExporterInfo = &flagExporterInfo
+	// flag exporters consolidates properties like includes, flags, dependencies that should be
+	// exported from this module to other modules
+	ctx.SetProvider(FlagExporterInfoProvider, flagExporterInfo)
+
+	// Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
+	// validation will fail. For now, set this to an empty list.
+	// TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
+	h.library.collectedSnapshotHeaders = android.Paths{}
+
+	return true
+}
+
 // cc_library_headers contains a set of c/c++ headers which are imported by
 // other soong cc modules using the header_libs property. For best practices,
 // use export_include_dirs property or LOCAL_EXPORT_C_INCLUDE_DIRS for
@@ -51,6 +97,7 @@
 	module, library := NewLibrary(android.HostAndDeviceSupported)
 	library.HeaderOnly()
 	module.sdkMemberTypes = []android.SdkMemberType{headersLibrarySdkMemberType}
+	module.bazelHandler = &libraryHeaderBazelHander{module: module, library: library}
 	return module.Init()
 }
 
diff --git a/cc/library_sdk_member.go b/cc/library_sdk_member.go
index d5f2adf..9010a1a 100644
--- a/cc/library_sdk_member.go
+++ b/cc/library_sdk_member.go
@@ -162,9 +162,16 @@
 	return &nativeLibInfoProperties{memberType: mt}
 }
 
+func isBazelOutDirectory(p android.Path) bool {
+	_, bazel := p.(android.BazelOutPath)
+	return bazel
+}
+
 func isGeneratedHeaderDirectory(p android.Path) bool {
 	_, gen := p.(android.WritablePath)
-	return gen
+	// TODO(b/183213331): Here we assume that bazel-based headers are not generated; we need
+	// to support generated headers in mixed builds.
+	return gen && !isBazelOutDirectory(p)
 }
 
 type includeDirsProperty struct {
diff --git a/cc/linkable.go b/cc/linkable.go
index 0fb9c09..2fa12f6 100644
--- a/cc/linkable.go
+++ b/cc/linkable.go
@@ -2,6 +2,7 @@
 
 import (
 	"android/soong/android"
+	"android/soong/bazel/cquery"
 
 	"github.com/google/blueprint"
 )
@@ -274,3 +275,15 @@
 }
 
 var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
+
+// flagExporterInfoFromCcInfo populates FlagExporterInfo provider with information from Bazel.
+func flagExporterInfoFromCcInfo(ctx android.ModuleContext, ccInfo cquery.CcInfo) FlagExporterInfo {
+
+	includes := android.PathsForBazelOut(ctx, ccInfo.Includes)
+	systemIncludes := android.PathsForBazelOut(ctx, ccInfo.SystemIncludes)
+
+	return FlagExporterInfo{
+		IncludeDirs:       includes,
+		SystemIncludeDirs: systemIncludes,
+	}
+}