Add preprocessed_ndk_headers.

Prototypes in the bionic headers are annotated with `__INTRODUCED_IN`
tags that are processed by bionic/tools/versioner into their usable
form (tags replaced with `#if __ANDROID_API__ >= foo` guards). We've
previously done this as a periodic manual step with the results
checked in to prebuilts/ndk, but that's been a huge hassle for me.
Make the tool a part of the build instead.

Test: make checkbuild
Bug: http://b/35673791
Change-Id: I2f5c1aeae239ac4ab6616eb5c71360e3055f86d5
diff --git a/cc/ndk_headers.go b/cc/ndk_headers.go
index 9cc3417..a40fc72 100644
--- a/cc/ndk_headers.go
+++ b/cc/ndk_headers.go
@@ -15,6 +15,8 @@
 package cc
 
 import (
+	"fmt"
+	"os"
 	"path/filepath"
 
 	"github.com/google/blueprint"
@@ -22,6 +24,20 @@
 	"android/soong/android"
 )
 
+var (
+	preprocessBionicHeaders = pctx.AndroidStaticRule("preprocessBionicHeaders",
+		blueprint.RuleParams{
+			Command:     "$versionerCmd $srcDir $depsPath -o $out",
+			CommandDeps: []string{"$versionerCmd"},
+			Description: "versioner preprocess $in",
+		},
+		"depsPath", "srcDir")
+)
+
+func init() {
+	pctx.HostBinToolVariable("versionerCmd", "versioner")
+}
+
 // Returns the NDK base include path for use with sdk_version current. Usable with -I.
 func getCurrentIncludePath(ctx android.ModuleContext) android.OutputPath {
 	return getNdkSysrootBase(ctx).Join(ctx, "usr/include")
@@ -63,6 +79,38 @@
 func (m *headerModule) DepsMutator(ctx android.BottomUpMutatorContext) {
 }
 
+func getHeaderInstallDir(ctx android.ModuleContext, header android.Path, from string,
+	to string) android.OutputPath {
+	// Output path is the sysroot base + "usr/include" + to directory + directory component
+	// of the file without the leading from directory stripped.
+	//
+	// Given:
+	// sysroot base = "ndk/sysroot"
+	// from = "include/foo"
+	// to = "bar"
+	// header = "include/foo/woodly/doodly.h"
+	// output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
+
+	// full/platform/path/to/include/foo
+	fullFromPath := android.PathForModuleSrc(ctx, from)
+
+	// full/platform/path/to/include/foo/woodly
+	headerDir := filepath.Dir(header.String())
+
+	// woodly
+	strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir)
+	if err != nil {
+		ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir,
+			fullFromPath.String(), err)
+	}
+
+	// full/platform/path/to/sysroot/usr/include/bar/woodly
+	installDir := getCurrentIncludePath(ctx).Join(ctx, to, strippedHeaderDir)
+
+	// full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h
+	return installDir
+}
+
 func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
 	if m.properties.License == "" {
 		ctx.PropertyErrorf("license", "field is required")
@@ -72,34 +120,14 @@
 
 	srcFiles := ctx.ExpandSources(m.properties.Srcs, nil)
 	for _, header := range srcFiles {
-		// Output path is the sysroot base + "usr/include" + to directory + directory component
-		// of the file without the leading from directory stripped.
-		//
-		// Given:
-		// sysroot base = "ndk/sysroot"
-		// from = "include/foo"
-		// to = "bar"
-		// header = "include/foo/woodly/doodly.h"
-		// output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
-
-		// full/platform/path/to/include/foo
-		fullFromPath := android.PathForModuleSrc(ctx, m.properties.From)
-
-		// full/platform/path/to/include/foo/woodly
-		headerDir := filepath.Dir(header.String())
-
-		// woodly
-		strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir)
-		if err != nil {
-			ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir,
-				fullFromPath.String(), err)
+		installDir := getHeaderInstallDir(ctx, header, m.properties.From, m.properties.To)
+		installedPath := ctx.InstallFile(installDir, header)
+		installPath := installDir.Join(ctx, header.Base())
+		if installPath != installedPath {
+			panic(fmt.Sprintf(
+				"expected header install path (%q) not equal to actual install path %q",
+				installPath, installedPath))
 		}
-
-		// full/platform/path/to/sysroot/usr/include/bar/woodly
-		installDir := getCurrentIncludePath(ctx).Join(ctx, m.properties.To, strippedHeaderDir)
-
-		// full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h
-		installPath := ctx.InstallFile(installDir, header)
 		m.installPaths = append(m.installPaths, installPath.String())
 	}
 
@@ -117,3 +145,113 @@
 	return android.InitAndroidArchModule(module, android.HostSupportedNoCross,
 		android.MultilibFirst, &module.properties)
 }
+
+type preprocessedHeaderProperies struct {
+	// Base directory of the headers being installed. As an example:
+	//
+	// preprocessed_ndk_headers {
+	//     name: "foo",
+	//     from: "include",
+	//     to: "",
+	// }
+	//
+	// Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
+	// "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
+	From string
+
+	// Install path within the sysroot. This is relative to usr/include.
+	To string
+
+	// Path to the NOTICE file associated with the headers.
+	License string
+}
+
+// Like ndk_headers, but preprocesses the headers with the bionic versioner:
+// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
+//
+// Unlike ndk_headers, we don't operate on a list of sources but rather a whole directory, the
+// module does not have the srcs property, and operates on a full directory (the `from` property).
+//
+// Note that this is really only built to handle bionic/libc/include.
+type preprocessedHeaderModule struct {
+	android.ModuleBase
+
+	properties preprocessedHeaderProperies
+
+	installPaths []string
+	licensePath  android.ModuleSrcPath
+}
+
+func (m *preprocessedHeaderModule) DepsMutator(ctx android.BottomUpMutatorContext) {
+}
+
+func (m *preprocessedHeaderModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+	if m.properties.License == "" {
+		ctx.PropertyErrorf("license", "field is required")
+	}
+
+	m.licensePath = android.PathForModuleSrc(ctx, m.properties.License)
+
+	fromSrcPath := android.PathForModuleSrc(ctx, m.properties.From)
+	toOutputPath := getCurrentIncludePath(ctx).Join(ctx, m.properties.To)
+	srcFiles := ctx.Glob(filepath.Join(fromSrcPath.String(), "**/*.h"), nil)
+	var installPaths []android.WritablePath
+	for _, header := range srcFiles {
+		installDir := getHeaderInstallDir(ctx, header, m.properties.From, m.properties.To)
+		installPath := installDir.Join(ctx, header.Base())
+		installPaths = append(installPaths, installPath)
+		m.installPaths = append(m.installPaths, installPath.String())
+	}
+
+	if len(m.installPaths) == 0 {
+		ctx.ModuleErrorf("glob %q matched zero files", m.properties.From)
+	}
+
+	// The versioner depends on a dependencies directory to simplify determining include paths
+	// when parsing headers. This directory contains architecture specific directories as well
+	// as a common directory, each of which contains symlinks to the actually directories to
+	// be included.
+	//
+	// ctx.Glob doesn't follow symlinks, so we need to do this ourselves so we correctly
+	// depend on these headers.
+	// TODO(http://b/35673191): Update the versioner to use a --sysroot.
+	depsPath := android.PathForSource(ctx, "bionic/libc/versioner-dependencies")
+	depsGlob := ctx.Glob(filepath.Join(depsPath.String(), "**/*"), nil)
+	for i, path := range depsGlob {
+		fileInfo, err := os.Lstat(path.String())
+		if err != nil {
+			ctx.ModuleErrorf("os.Lstat(%q) failed: %s", path.String, err)
+		}
+		if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
+			dest, err := os.Readlink(path.String())
+			if err != nil {
+				ctx.ModuleErrorf("os.Readlink(%q) failed: %s",
+					path.String, err)
+			}
+			// Additional .. to account for the symlink itself.
+			depsGlob[i] = android.PathForSource(
+				ctx, filepath.Clean(filepath.Join(path.String(), "..", dest)))
+		}
+	}
+
+	ctx.ModuleBuild(pctx, android.ModuleBuildParams{
+		Rule:            preprocessBionicHeaders,
+		Output:          toOutputPath,
+		Implicits:       append(srcFiles, depsGlob...),
+		ImplicitOutputs: installPaths,
+		Args: map[string]string{
+			"depsPath": depsPath.String(),
+			"srcDir":   fromSrcPath.String(),
+		},
+	})
+}
+
+func preprocessedNdkHeadersFactory() (blueprint.Module, []interface{}) {
+	module := &preprocessedHeaderModule{}
+	// Host module rather than device module because device module install steps
+	// do not get run when embedded in make. We're not any of the existing
+	// module types that can be exposed via the Android.mk exporter, so just use
+	// a host module.
+	return android.InitAndroidArchModule(module, android.HostSupportedNoCross,
+		android.MultilibFirst, &module.properties)
+}
diff --git a/cc/ndk_sysroot.go b/cc/ndk_sysroot.go
index e82f12b..47dda27 100644
--- a/cc/ndk_sysroot.go
+++ b/cc/ndk_sysroot.go
@@ -61,6 +61,7 @@
 func init() {
 	android.RegisterModuleType("ndk_headers", ndkHeadersFactory)
 	android.RegisterModuleType("ndk_library", ndkLibraryFactory)
+	android.RegisterModuleType("preprocessed_ndk_headers", preprocessedNdkHeadersFactory)
 	android.RegisterSingletonType("ndk", NdkSingleton)
 
 	pctx.Import("android/soong/common")
@@ -93,9 +94,12 @@
 			installPaths = append(installPaths, m.installPaths...)
 			licensePaths = append(licensePaths, m.licensePath.String())
 		}
-	})
 
-	ctx.VisitAllModules(func(module blueprint.Module) {
+		if m, ok := module.(*preprocessedHeaderModule); ok {
+			installPaths = append(installPaths, m.installPaths...)
+			licensePaths = append(licensePaths, m.licensePath.String())
+		}
+
 		if m, ok := module.(*Module); ok {
 			if installer, ok := m.installer.(*stubDecorator); ok {
 				installPaths = append(installPaths, installer.installPath)