Merge "Switch to clang-3859424."
diff --git a/android/androidmk.go b/android/androidmk.go
index d55cdca..f606522 100644
--- a/android/androidmk.go
+++ b/android/androidmk.go
@@ -175,19 +175,21 @@
 
 	if data.Custom != nil {
 		prefix := ""
-		switch amod.Os().Class {
-		case Host:
-			prefix = "HOST_"
-		case HostCross:
-			prefix = "HOST_CROSS_"
-		case Device:
-			prefix = "TARGET_"
+		if amod.ArchSpecific() {
+			switch amod.Os().Class {
+			case Host:
+				prefix = "HOST_"
+			case HostCross:
+				prefix = "HOST_CROSS_"
+			case Device:
+				prefix = "TARGET_"
 
-		}
+			}
 
-		config := ctx.Config().(Config)
-		if amod.Arch().ArchType != config.Targets[amod.Os().Class][0].Arch.ArchType {
-			prefix = "2ND_" + prefix
+			config := ctx.Config().(Config)
+			if amod.Arch().ArchType != config.Targets[amod.Os().Class][0].Arch.ArchType {
+				prefix = "2ND_" + prefix
+			}
 		}
 
 		return data.Custom(w, name, prefix, filepath.Dir(ctx.BlueprintFile(mod)))
diff --git a/cc/builder.go b/cc/builder.go
index cdfea92..0694cb7 100644
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -201,6 +201,8 @@
 	tidy        bool
 	coverage    bool
 
+	systemIncludeFlags string
+
 	groupStaticLibs bool
 
 	stripKeepSymbols       bool
@@ -244,9 +246,25 @@
 		coverageFiles = make(android.Paths, 0, len(srcFiles))
 	}
 
-	cflags := flags.globalFlags + " " + flags.cFlags + " " + flags.conlyFlags
-	cppflags := flags.globalFlags + " " + flags.cFlags + " " + flags.cppFlags
-	asflags := flags.globalFlags + " " + flags.asFlags
+	cflags := strings.Join([]string{
+		flags.globalFlags,
+		flags.systemIncludeFlags,
+		flags.cFlags,
+		flags.conlyFlags,
+	}, " ")
+
+	cppflags := strings.Join([]string{
+		flags.globalFlags,
+		flags.systemIncludeFlags,
+		flags.cFlags,
+		flags.cppFlags,
+	}, " ")
+
+	asflags := strings.Join([]string{
+		flags.globalFlags,
+		flags.systemIncludeFlags,
+		flags.asFlags,
+	}, " ")
 
 	if flags.clang {
 		cflags += " ${config.NoOverrideClangGlobalCflags}"
diff --git a/cc/cc.go b/cc/cc.go
index 84afa73..b107d01 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -109,6 +109,10 @@
 	TidyFlags   []string // Flags that apply to clang-tidy
 	YasmFlags   []string // Flags that apply to yasm assembly source files
 
+	// Global include flags that apply to C, C++, and assembly source files
+	// These must be after any module include flags, which will be in GlobalFlags.
+	SystemIncludeFlags []string
+
 	Toolchain config.Toolchain
 	Clang     bool
 	Tidy      bool
diff --git a/cc/cmakelists.go b/cc/cmakelists.go
index 83b6d69..4df9ece 100644
--- a/cc/cmakelists.go
+++ b/cc/cmakelists.go
@@ -5,11 +5,12 @@
 
 	"android/soong/android"
 	"android/soong/cc/config"
-	"github.com/google/blueprint"
 	"os"
 	"path"
 	"path/filepath"
 	"strings"
+
+	"github.com/google/blueprint"
 )
 
 // This singleton generates CMakeLists.txt files. It does so for each blueprint Android.bp resulting in a cc.Module
@@ -162,6 +163,10 @@
 	cppParameters := parseCompilerParameters(ccModule.flags.CppFlags, ctx, f)
 	translateToCMake(cppParameters, f, false, true)
 
+	f.WriteString("\n# SYSTEM INCLUDE FLAGS:\n")
+	includeParameters := parseCompilerParameters(ccModule.flags.SystemIncludeFlags, ctx, f)
+	translateToCMake(includeParameters, f, true, true)
+
 	// Add project executable.
 	f.WriteString(fmt.Sprintf("\nadd_executable(%s ${SOURCE_FILES})\n",
 		cleanExecutableName(ccModule.ModuleBase.Name())))
@@ -171,10 +176,9 @@
 	return strings.Replace(s, "@", "-", -1)
 }
 
-
 func translateToCMake(c compilerParameters, f *os.File, cflags bool, cppflags bool) {
-	writeAllSystemDirectories(c.systemHeaderSearchPath, f)
-	writeAllIncludeDirectories(c.headerSearchPath, f)
+	writeAllIncludeDirectories(c.systemHeaderSearchPath, f, true)
+	writeAllIncludeDirectories(c.headerSearchPath, f, false)
 	if cflags {
 		writeAllFlags(c.flags, f, "CMAKE_C_FLAGS")
 	}
@@ -195,26 +199,30 @@
 	return fmt.Sprintf("${ANDROID_ROOT}/%s", p)
 }
 
-func writeAllIncludeDirectories(includes []string, f *os.File) {
+func writeAllIncludeDirectories(includes []string, f *os.File, isSystem bool) {
 	if len(includes) == 0 {
 		return
 	}
-	f.WriteString("include_directories(\n")
-	for _, include := range includes {
-		f.WriteString(fmt.Sprintf("                    \"%s\"\n", buildCMakePath(include)))
-	}
-	f.WriteString(")\n")
-}
 
-func writeAllSystemDirectories(includes []string, f *os.File) {
-	if len(includes) == 0 {
-		return
+	system := ""
+        if isSystem {
+		system = "SYSTEM"
 	}
-	f.WriteString("include_directories(SYSTEM \n")
+
+	f.WriteString(fmt.Sprintf("include_directories(%s \n", system))
+
 	for _, include := range includes {
-		f.WriteString(fmt.Sprintf("                           \"%s\"\n", buildCMakePath(include)))
+		f.WriteString(fmt.Sprintf("    \"%s\"\n", buildCMakePath(include)))
+	}
+	f.WriteString(")\n\n")
+
+	// Also add all headers to source files.
+	f.WriteString("file (GLOB_RECURSE TMP_HEADERS\n");
+	for _, include := range includes {
+		f.WriteString(fmt.Sprintf("    \"%s/**/*.h\"\n", buildCMakePath(include)))
 	}
 	f.WriteString(")\n")
+	f.WriteString("list (APPEND SOURCE_FILES ${TMP_HEADERS})\n\n");
 }
 
 func writeAllFlags(flags []string, f *os.File, tag string) {
diff --git a/cc/compiler.go b/cc/compiler.go
index 84ee652..91eda38 100644
--- a/cc/compiler.go
+++ b/cc/compiler.go
@@ -194,15 +194,15 @@
 	}
 
 	if !ctx.noDefaultCompilerFlags() {
+		flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.PathForModuleSrc(ctx).String())
+
 		if !(ctx.sdk() || ctx.vndk()) || ctx.Host() {
-			flags.GlobalFlags = append(flags.GlobalFlags,
+			flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
 				"${config.CommonGlobalIncludes}",
 				"${config.CommonGlobalSystemIncludes}",
 				tc.IncludeFlags(),
 				"${config.CommonNativehelperInclude}")
 		}
-
-		flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.PathForModuleSrc(ctx).String())
 	}
 
 	if ctx.sdk() || ctx.vndk() {
@@ -210,7 +210,7 @@
 		// typical Soong approach would be to only make the headers for the
 		// library you're using available, we're trying to emulate the NDK
 		// behavior here, and the NDK always has all the NDK headers available.
-		flags.GlobalFlags = append(flags.GlobalFlags,
+		flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
 			"-isystem "+getCurrentIncludePath(ctx).String(),
 			"-isystem "+getCurrentIncludePath(ctx).Join(ctx, tc.ClangTriple()).String())
 
@@ -230,7 +230,7 @@
 		legacyIncludes := fmt.Sprintf(
 			"prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/include",
 			ctx.sdkVersion(), ctx.Arch().ArchType.String())
-		flags.GlobalFlags = append(flags.GlobalFlags, "-isystem "+legacyIncludes)
+		flags.SystemIncludeFlags = append(flags.SystemIncludeFlags, "-isystem "+legacyIncludes)
 	}
 
 	instructionSet := compiler.Properties.Instruction_set
diff --git a/cc/ndk_headers.go b/cc/ndk_headers.go
index e4a790f..6a083ae 100644
--- a/cc/ndk_headers.go
+++ b/cc/ndk_headers.go
@@ -27,11 +27,13 @@
 var (
 	preprocessBionicHeaders = pctx.AndroidStaticRule("preprocessBionicHeaders",
 		blueprint.RuleParams{
-			Command:     "$versionerCmd $srcDir $depsPath -o $out",
+			// The `&& touch $out` isn't really necessary, but Blueprint won't
+			// let us have only implicit outputs.
+			Command:     "$versionerCmd -o $outDir $srcDir $depsPath && touch $out",
 			CommandDeps: []string{"$versionerCmd"},
 			Description: "versioner preprocess $in",
 		},
-		"depsPath", "srcDir")
+		"depsPath", "srcDir", "outDir")
 )
 
 func init() {
@@ -229,14 +231,16 @@
 		}
 	}
 
+	timestampFile := android.PathForModuleOut(ctx, "versioner.timestamp")
 	ctx.ModuleBuild(pctx, android.ModuleBuildParams{
 		Rule:            preprocessBionicHeaders,
-		Output:          toOutputPath,
+		Output:          timestampFile,
 		Implicits:       append(srcFiles, depsGlob...),
 		ImplicitOutputs: installPaths,
 		Args: map[string]string{
 			"depsPath": depsPath.String(),
 			"srcDir":   fromSrcPath.String(),
+			"outDir":   toOutputPath.String(),
 		},
 	})
 }
diff --git a/cc/util.go b/cc/util.go
index 919e14c..36d8dd2 100644
--- a/cc/util.go
+++ b/cc/util.go
@@ -105,6 +105,8 @@
 		coverage:    in.Coverage,
 		tidy:        in.Tidy,
 
+		systemIncludeFlags: strings.Join(in.SystemIncludeFlags, " "),
+
 		groupStaticLibs: in.GroupStaticLibs,
 	}
 }
diff --git a/cmd/fileslist/fileslist.go b/cmd/fileslist/fileslist.go
index 1cf948f..56ea66d 100755
--- a/cmd/fileslist/fileslist.go
+++ b/cmd/fileslist/fileslist.go
@@ -64,18 +64,26 @@
 	n.Size = n.stat.Size()
 
 	// Calculate SHA256.
-	f, err := os.Open(n.path)
-	if err != nil {
-		// If the file can't be read, it's probably a symlink to an absolute path...
-		// Returns the following to mimic the behavior of fileslist.py.
-		n.SHA256 = "----------------------------------------------------------------"
-		return true
-	}
-	defer f.Close()
-
 	h := sha256.New()
-	if _, err := io.Copy(h, f); err != nil {
-		panic(err)
+	if n.stat.Mode()&os.ModeSymlink == 0 {
+		f, err := os.Open(n.path)
+		if err != nil {
+			panic(err)
+		}
+		defer f.Close()
+
+		if _, err := io.Copy(h, f); err != nil {
+			panic(err)
+		}
+	} else {
+		// Hash the content of symlink, not the file it points to.
+		s, err := os.Readlink(n.path)
+		if err != nil {
+			panic(err)
+		}
+		if _, err := io.WriteString(h, s); err != nil {
+			panic(err)
+		}
 	}
 	n.SHA256 = fmt.Sprintf("%x", h.Sum(nil))
 	return true