Merge "Replace Device_uses_{dl,je}malloc with Malloc_not_svelte"
diff --git a/cc/builder.go b/cc/builder.go
index 98f66d9..bcfbb6e 100644
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -149,6 +149,14 @@
 	cppflags := flags.globalFlags + " " + flags.cFlags + " " + flags.cppFlags
 	asflags := flags.globalFlags + " " + flags.asFlags
 
+	if flags.clang {
+		cflags += " ${noOverrideClangGlobalCflags}"
+		cppflags += " ${noOverrideClangGlobalCflags}"
+	} else {
+		cflags += " ${noOverrideGlobalCflags}"
+		cppflags += " ${noOverrideGlobalCflags}"
+	}
+
 	for i, srcFile := range srcFiles {
 		objFile := common.ObjPathWithExt(ctx, srcFile, subdir, "o")
 
diff --git a/cc/cc.go b/cc/cc.go
index 6cff27f..d79cdbe 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -94,6 +94,7 @@
 		"-Werror=non-virtual-dtor",
 		"-Werror=address",
 		"-Werror=sequence-point",
+		"-Werror=date-time",
 	}
 
 	hostGlobalCflags = []string{}
@@ -102,6 +103,11 @@
 		"-Wsign-promo",
 	}
 
+	noOverrideGlobalCflags = []string{
+		"-Werror=int-to-pointer-cast",
+		"-Werror=pointer-to-int-cast",
+	}
+
 	illegalFlags = []string{
 		"-w",
 	}
@@ -111,6 +117,7 @@
 	pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
 	pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
 	pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
+	pctx.StaticVariable("noOverrideGlobalCflags", strings.Join(noOverrideGlobalCflags, " "))
 
 	pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
 
@@ -120,6 +127,9 @@
 		strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
 	pctx.StaticVariable("hostClangGlobalCflags",
 		strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
+	pctx.StaticVariable("noOverrideClangGlobalCflags",
+		strings.Join(append(clangFilterUnknownCflags(noOverrideGlobalCflags), "${clangExtraNoOverrideCflags}"), " "))
+
 	pctx.StaticVariable("commonClangGlobalCppflags",
 		strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
 
@@ -128,6 +138,7 @@
 	pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
 		[]string{
 			"system/core/include",
+			"system/media/audio/include",
 			"hardware/libhardware/include",
 			"hardware/libhardware_legacy/include",
 			"hardware/ril/include",
@@ -142,7 +153,7 @@
 	pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
 		[]string{"libnativehelper/include/nativehelper"})
 
-	pctx.SourcePathVariable("clangPath", "prebuilts/clang/host/${HostPrebuiltTag}/3.8/bin")
+	pctx.SourcePathVariable("clangPath", "prebuilts/clang/host/${HostPrebuiltTag}/clang-2629532/bin")
 }
 
 type CCModuleContext common.AndroidBaseContext
@@ -632,6 +643,22 @@
 	flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
 	flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
 
+	// We can enforce some rules more strictly in the code we own. strict
+	// indicates if this is code that we can be stricter with. If we have
+	// rules that we want to apply to *our* code (but maybe can't for
+	// vendor/device specific things), we could extend this to be a ternary
+	// value.
+	strict := true
+	if strings.HasPrefix(common.PathForModuleSrc(ctx).String(), "external/") {
+		strict = false
+	}
+
+	// Can be used to make some annotations stricter for code we can fix
+	// (such as when we mark functions as deprecated).
+	if strict {
+		flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
+	}
+
 	// Optimization to reduce size of build.ninja
 	// Replace the long list of flags for each file with a module-local variable
 	ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
@@ -911,7 +938,7 @@
 		if ctx.Host() {
 			flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
 			flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
-			flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
+			flags.LdFlags = append(flags.LdFlags, "-lpthread", "-lm")
 			if c.staticBinary() {
 				flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
 			} else {
@@ -1221,7 +1248,10 @@
 			sharedFlag = "-shared"
 		}
 		if ctx.Device() {
-			flags.LdFlags = append(flags.LdFlags, "-nostdlib")
+			flags.LdFlags = append(flags.LdFlags,
+				"-nostdlib",
+				"-Wl,--gc-sections",
+			)
 		}
 
 		if ctx.Darwin() {
@@ -1233,7 +1263,6 @@
 			)
 		} else {
 			flags.LdFlags = append(flags.LdFlags,
-				"-Wl,--gc-sections",
 				sharedFlag,
 				"-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
 			)
diff --git a/cc/clang.go b/cc/clang.go
index 5e0302a..f4c29f0 100644
--- a/cc/clang.go
+++ b/cc/clang.go
@@ -31,7 +31,6 @@
 	"-Wunused-but-set-parameter",
 	"-Wunused-but-set-variable",
 	"-fdiagnostics-color",
-	"-fdebug-prefix-map=/proc/self/cwd=",
 
 	// arm + arm64 + mips + mips64
 	"-fgcse-after-reload",
@@ -102,6 +101,12 @@
 	pctx.StaticVariable("clangExtraTargetCflags", strings.Join([]string{
 		"-nostdlibinc",
 	}, " "))
+
+	pctx.StaticVariable("clangExtraNoOverrideCflags", strings.Join([]string{
+		"-Werror=address-of-temporary",
+		"-Werror=null-dereference",
+		"-Werror=return-type",
+	}, " "))
 }
 
 func clangFilterUnknownCflags(cflags []string) []string {
diff --git a/cc/gen.go b/cc/gen.go
index 035f40e..94e2304 100644
--- a/cc/gen.go
+++ b/cc/gen.go
@@ -33,12 +33,11 @@
 var (
 	yacc = pctx.StaticRule("yacc",
 		blueprint.RuleParams{
-			Command: "BISON_PKGDATADIR=$yaccDataDir $yaccCmd -d $yaccFlags -o $cppFile $in && " +
-				"cp -f $hppFile $hFile",
+			Command:     "BISON_PKGDATADIR=$yaccDataDir $yaccCmd -d $yaccFlags --defines=$hFile -o $cFile $in",
 			CommandDeps: []string{"$yaccCmd"},
 			Description: "yacc $out",
 		},
-		"yaccFlags", "cppFile", "hppFile", "hFile")
+		"yaccFlags", "cFile", "hFile")
 
 	lex = pctx.StaticRule("lex",
 		blueprint.RuleParams{
@@ -48,36 +47,29 @@
 		})
 )
 
-func genYacc(ctx common.AndroidModuleContext, yaccFile common.Path, yaccFlags string) (cppFile, headerFile common.ModuleGenPath) {
-	cppFile = common.GenPathWithExt(ctx, yaccFile, "cpp")
-	hppFile := common.GenPathWithExt(ctx, yaccFile, "hpp")
+func genYacc(ctx common.AndroidModuleContext, yaccFile common.Path, outFile common.ModuleGenPath, yaccFlags string) (headerFile common.ModuleGenPath) {
 	headerFile = common.GenPathWithExt(ctx, yaccFile, "h")
 
 	ctx.ModuleBuild(pctx, common.ModuleBuildParams{
 		Rule:    yacc,
-		Outputs: common.WritablePaths{cppFile, headerFile},
+		Outputs: common.WritablePaths{outFile, headerFile},
 		Input:   yaccFile,
 		Args: map[string]string{
 			"yaccFlags": yaccFlags,
-			"cppFile":   cppFile.String(),
-			"hppFile":   hppFile.String(),
+			"cFile":     outFile.String(),
 			"hFile":     headerFile.String(),
 		},
 	})
 
-	return cppFile, headerFile
+	return headerFile
 }
 
-func genLex(ctx common.AndroidModuleContext, lexFile common.Path) (cppFile common.ModuleGenPath) {
-	cppFile = common.GenPathWithExt(ctx, lexFile, "cpp")
-
+func genLex(ctx common.AndroidModuleContext, lexFile common.Path, outFile common.ModuleGenPath) {
 	ctx.ModuleBuild(pctx, common.ModuleBuildParams{
 		Rule:   lex,
-		Output: cppFile,
+		Output: outFile,
 		Input:  lexFile,
 	})
-
-	return cppFile
 }
 
 func genSources(ctx common.AndroidModuleContext, srcFiles common.Paths,
@@ -87,13 +79,22 @@
 
 	for i, srcFile := range srcFiles {
 		switch srcFile.Ext() {
-		case ".y", ".yy":
-			cppFile, headerFile := genYacc(ctx, srcFile, buildFlags.yaccFlags)
+		case ".y":
+			cFile := common.GenPathWithExt(ctx, srcFile, "c")
+			srcFiles[i] = cFile
+			deps = append(deps, genYacc(ctx, srcFile, cFile, buildFlags.yaccFlags))
+		case ".yy":
+			cppFile := common.GenPathWithExt(ctx, srcFile, "cpp")
 			srcFiles[i] = cppFile
-			deps = append(deps, headerFile)
-		case ".l", ".ll":
-			cppFile := genLex(ctx, srcFile)
+			deps = append(deps, genYacc(ctx, srcFile, cppFile, buildFlags.yaccFlags))
+		case ".l":
+			cFile := common.GenPathWithExt(ctx, srcFile, "c")
+			srcFiles[i] = cFile
+			genLex(ctx, srcFile, cFile)
+		case ".ll":
+			cppFile := common.GenPathWithExt(ctx, srcFile, "cpp")
 			srcFiles[i] = cppFile
+			genLex(ctx, srcFile, cppFile)
 		}
 	}
 
diff --git a/cc/x86_darwin_host.go b/cc/x86_darwin_host.go
index 1bd3dd0..7f4c489 100644
--- a/cc/x86_darwin_host.go
+++ b/cc/x86_darwin_host.go
@@ -63,10 +63,10 @@
 		"-Wl,-rpath,@loader_path/lib64",
 	}
 
-	darwinClangCflags = append([]string{
+	darwinClangCflags = append(clangFilterUnknownCflags(darwinCflags), []string{
 		"-integrated-as",
 		"-fstack-protector-strong",
-	}, clangFilterUnknownCflags(darwinCflags)...)
+	}...)
 
 	darwinClangLdflags = clangFilterUnknownCflags(darwinLdflags)
 
diff --git a/cc/x86_linux_host.go b/cc/x86_linux_host.go
index d1a92f1..528d2a7 100644
--- a/cc/x86_linux_host.go
+++ b/cc/x86_linux_host.go
@@ -66,28 +66,28 @@
 		`-Wl,-rpath,\$$ORIGIN/lib64`,
 	}
 
-	linuxClangCflags = append([]string{
+	linuxClangCflags = append(clangFilterUnknownCflags(linuxCflags), []string{
 		"--gcc-toolchain=${linuxGccRoot}",
 		"--sysroot=${linuxGccRoot}/sysroot",
 		"-fstack-protector-strong",
-	}, clangFilterUnknownCflags(linuxCflags)...)
+	}...)
 
-	linuxClangLdflags = append([]string{
+	linuxClangLdflags = append(clangFilterUnknownCflags(linuxLdflags), []string{
 		"--gcc-toolchain=${linuxGccRoot}",
 		"--sysroot=${linuxGccRoot}/sysroot",
-	}, clangFilterUnknownCflags(linuxLdflags)...)
+	}...)
 
-	linuxX86ClangLdflags = append([]string{
+	linuxX86ClangLdflags = append(clangFilterUnknownCflags(linuxX86Ldflags), []string{
 		"-B${linuxGccRoot}/lib/gcc/${linuxGccTriple}/${linuxGccVersion}/32",
 		"-L${linuxGccRoot}/lib/gcc/${linuxGccTriple}/${linuxGccVersion}/32",
 		"-L${linuxGccRoot}/${linuxGccTriple}/lib32",
-	}, clangFilterUnknownCflags(linuxX86Ldflags)...)
+	}...)
 
-	linuxX8664ClangLdflags = append([]string{
+	linuxX8664ClangLdflags = append(clangFilterUnknownCflags(linuxX8664Ldflags), []string{
 		"-B${linuxGccRoot}/lib/gcc/${linuxGccTriple}/${linuxGccVersion}",
 		"-L${linuxGccRoot}/lib/gcc/${linuxGccTriple}/${linuxGccVersion}",
 		"-L${linuxGccRoot}/${linuxGccTriple}/lib64",
-	}, clangFilterUnknownCflags(linuxX8664Ldflags)...)
+	}...)
 
 	linuxClangCppflags = []string{
 		"-isystem ${linuxGccRoot}/${linuxGccTriple}/include/c++/${linuxGccVersion}",
diff --git a/common/androidmk.go b/common/androidmk.go
index f041b59..9628a10 100644
--- a/common/androidmk.go
+++ b/common/androidmk.go
@@ -182,9 +182,6 @@
 			fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
 		} else {
 			fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
-
-			// TODO: this isn't true for every module, only dependencies of ACP
-			fmt.Fprintln(w, "LOCAL_ACP_UNAVAILABLE := true")
 		}
 		fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", amod.HostType().String())
 		fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
diff --git a/common/config.go b/common/config.go
index 6fd6dfc..ef59cf7 100644
--- a/common/config.go
+++ b/common/config.go
@@ -261,7 +261,7 @@
 	if c.ProductVariables.DeviceUsesClang != nil {
 		return *c.ProductVariables.DeviceUsesClang
 	}
-	return false
+	return true
 }
 
 func (c *config) ResourceOverlays() []SourcePath {
diff --git a/soong.bash b/soong.bash
index d88ebbf..f695e62 100755
--- a/soong.bash
+++ b/soong.bash
@@ -40,4 +40,4 @@
     fi
 fi
 
-"prebuilts/ninja/${PREBUILTOS}/ninja" -f "${BUILDDIR}/build.ninja" "$@"
+"prebuilts/ninja/${PREBUILTOS}/ninja" -f "${BUILDDIR}/build.ninja" -w dupbuild=err "$@"