Generate NDK sysroots from the platform build.

The list of migrated libraries is currently empty. Libraries will be
migrated as follow up patches.

Test: Migrated libc to this system and everything still builds.
      build.ninja shows libraries being built and used and headers are
      collected for the sysroot.
Bug: http://b/27533932
Change-Id: Iaba00543c1390f432befe0eed768ed3fbb8a9b96
diff --git a/cc/ndk_headers.go b/cc/ndk_headers.go
new file mode 100644
index 0000000..5d70b89
--- /dev/null
+++ b/cc/ndk_headers.go
@@ -0,0 +1,102 @@
+// Copyright 2016 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package cc
+
+import (
+	"path/filepath"
+
+	"github.com/google/blueprint"
+
+	"android/soong/android"
+)
+
+// 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")
+}
+
+type headerProperies struct {
+	// Base directory of the headers being installed. As an example:
+	//
+	// ndk_headers {
+	//     name: "foo",
+	//     from: "include",
+	//     to: "",
+	//     srcs: ["include/foo/bar/baz.h"],
+	// }
+	//
+	// 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
+
+	// List of headers to install. Glob compatible. Common case is "include/**/*.h".
+	Srcs []string
+}
+
+type headerModule struct {
+	android.ModuleBase
+
+	properties headerProperies
+
+	installPaths []string
+}
+
+func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+	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)
+		}
+
+		// 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())
+	}
+
+	if len(m.installPaths) == 0 {
+		ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
+	}
+}
+
+func ndkHeadersFactory() (blueprint.Module, []interface{}) {
+	module := &headerModule{}
+	return android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst,
+		&module.properties)
+}