Merge "Add a "license" property to ndk_headers."
diff --git a/android/arch.go b/android/arch.go
index 61564d8..6c99684 100644
--- a/android/arch.go
+++ b/android/arch.go
@@ -799,13 +799,15 @@
 	return targets, nil
 }
 
-func decodeMegaDevice() ([]Target, error) {
-	archSettings := []struct {
-		arch        string
-		archVariant string
-		cpuVariant  string
-		abi         []string
-	}{
+type archConfig struct {
+	arch        string
+	archVariant string
+	cpuVariant  string
+	abi         []string
+}
+
+func getMegaDeviceConfig() []archConfig {
+	return []archConfig{
 		// armv5 is only used for unbundled apps
 		//{"arm", "armv5te", "", []string{"armeabi"}},
 		{"arm", "armv7-a", "generic", []string{"armeabi-v7a"}},
@@ -846,10 +848,23 @@
 		{"x86_64", "sandybridge", "", []string{"x86_64"}},
 		{"x86_64", "silvermont", "", []string{"x86_64"}},
 	}
+}
 
+func getNdkAbisConfig() []archConfig {
+	return []archConfig{
+		{"arm", "armv5te", "", []string{"armeabi"}},
+		{"arm64", "armv8-a", "", []string{"arm64-v8a"}},
+		{"mips", "mips32-fp", "", []string{"mips"}},
+		{"mips64", "mips64r6", "", []string{"mips64"}},
+		{"x86", "", "", []string{"x86"}},
+		{"x86_64", "", "", []string{"x86_64"}},
+	}
+}
+
+func decodeArchSettings(archConfigs []archConfig) ([]Target, error) {
 	var ret []Target
 
-	for _, config := range archSettings {
+	for _, config := range archConfigs {
 		arch, err := decodeArch(config.arch, &config.archVariant,
 			&config.cpuVariant, &config.abi)
 		if err != nil {
diff --git a/android/config.go b/android/config.go
index 483ec91..4d7e8df 100644
--- a/android/config.go
+++ b/android/config.go
@@ -37,6 +37,7 @@
 // config file. These will be included in the config struct.
 type FileConfigurableOptions struct {
 	Mega_device *bool `json:",omitempty"`
+	Ndk_abis    *bool `json:",omitempty"`
 }
 
 func (f *FileConfigurableOptions) SetDefaultConfig() {
@@ -211,8 +212,15 @@
 		return Config{}, err
 	}
 
+	var archConfig []archConfig
 	if Bool(config.Mega_device) {
-		deviceTargets, err := decodeMegaDevice()
+		archConfig = getMegaDeviceConfig()
+	} else if Bool(config.Ndk_abis) {
+		archConfig = getNdkAbisConfig()
+	}
+
+	if archConfig != nil {
+		deviceTargets, err := decodeArchSettings(archConfig)
 		if err != nil {
 			return Config{}, err
 		}
diff --git a/cc/compiler.go b/cc/compiler.go
index 198b792..a0068ad 100644
--- a/cc/compiler.go
+++ b/cc/compiler.go
@@ -84,6 +84,9 @@
 	// pass -frtti instead of -fno-rtti
 	Rtti *bool
 
+	// if set to false, use -std=c++* instead of -std=gnu++*
+	Gnu_extensions *bool
+
 	Debug, Release struct {
 		// list of module-specific flags that will be used for C and C++ compiles in debug or
 		// release builds
@@ -278,13 +281,27 @@
 	}
 
 	if !ctx.sdk() {
-		if ctx.Host() && !flags.Clang {
+		cStd := config.CStdVersion
+		cppStd := config.CppStdVersion
+
+		if !flags.Clang {
+			// GCC uses an invalid C++14 ABI (emits calls to
+			// __cxa_throw_bad_array_length, which is not a valid C++ RT ABI).
+			// http://b/25022512
+			cppStd = config.GccCppStdVersion
+		} else if ctx.Host() && !flags.Clang {
 			// The host GCC doesn't support C++14 (and is deprecated, so likely
 			// never will). Build these modules with C++11.
-			flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
-		} else {
-			flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
+			cppStd = config.GccCppStdVersion
 		}
+
+		if compiler.Properties.Gnu_extensions != nil && *compiler.Properties.Gnu_extensions == false {
+			cStd = gnuToCReplacer.Replace(cStd)
+			cppStd = gnuToCReplacer.Replace(cppStd)
+		}
+
+		flags.ConlyFlags = append([]string{"-std=" + cStd}, flags.ConlyFlags...)
+		flags.CppFlags = append([]string{"-std=" + cppStd}, flags.CppFlags...)
 	}
 
 	// We can enforce some rules more strictly in the code we own. strict
@@ -306,6 +323,8 @@
 	return flags
 }
 
+var gnuToCReplacer = strings.NewReplacer("gnu", "c")
+
 func ndkPathDeps(ctx ModuleContext) android.Paths {
 	if ctx.sdk() {
 		// The NDK sysroot timestamp file depends on all the NDK sysroot files
diff --git a/cc/config/global.go b/cc/config/global.go
index 9b77662..348c586 100644
--- a/cc/config/global.go
+++ b/cc/config/global.go
@@ -37,9 +37,7 @@
 		"-UDEBUG",
 	}
 
-	commonGlobalConlyflags = []string{
-		"-std=gnu99",
-	}
+	commonGlobalConlyflags = []string{}
 
 	deviceGlobalCflags = []string{
 		"-fdiagnostics-color",
@@ -66,6 +64,10 @@
 	IllegalFlags = []string{
 		"-w",
 	}
+
+	CStdVersion      = "gnu99"
+	CppStdVersion    = "gnu++14"
+	GccCppStdVersion = "gnu++11"
 )
 
 var pctx = android.NewPackageContext("android/soong/cc/config")
diff --git a/cc/library.go b/cc/library.go
index feeb03c..a61e6a1 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -229,9 +229,13 @@
 			f = append(f,
 				"-dynamiclib",
 				"-single_module",
-				"-read_only_relocs suppress",
 				"-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
 			)
+			if ctx.Arch().ArchType == android.X86 {
+				f = append(f,
+					"-read_only_relocs suppress",
+				)
+			}
 		} else {
 			f = append(f,
 				sharedFlag,
diff --git a/cc/makevars.go b/cc/makevars.go
index 098ec02..770e1d0 100644
--- a/cc/makevars.go
+++ b/cc/makevars.go
@@ -48,6 +48,10 @@
 	ctx.Strict("ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS", asanLdflags)
 	ctx.Strict("ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES", asanLibs)
 
+	ctx.Strict("DEFAULT_C_STD_VERSION", config.CStdVersion)
+	ctx.Strict("DEFAULT_CPP_STD_VERSION", config.CppStdVersion)
+	ctx.Strict("DEFAULT_GCC_CPP_STD_VERSION", config.GccCppStdVersion)
+
 	includeFlags, err := ctx.Eval("${config.CommonGlobalIncludes} ${config.CommonGlobalSystemIncludes}")
 	if err != nil {
 		panic(err)
diff --git a/scripts/build-ndk-prebuilts.sh b/scripts/build-ndk-prebuilts.sh
new file mode 100755
index 0000000..cc418d4
--- /dev/null
+++ b/scripts/build-ndk-prebuilts.sh
@@ -0,0 +1,25 @@
+#!/bin/bash -ex
+
+if [ -z "${OUT_DIR}" ]; then
+    echo Must set OUT_DIR
+    exit 1
+fi
+
+TOP=$(pwd)
+
+SOONG_OUT=${OUT_DIR}/soong
+SOONG_NDK_OUT=${OUT_DIR}/soong/ndk
+rm -rf ${SOONG_OUT}
+mkdir -p ${SOONG_OUT}
+cat > ${SOONG_OUT}/soong.config << EOF
+{
+    "Ndk_abis": true
+}
+EOF
+BUILDDIR=${SOONG_OUT} ./bootstrap.bash
+${SOONG_OUT}/soong ${SOONG_OUT}/ndk.timestamp
+
+if [ -n "${DIST_DIR}" ]; then
+    mkdir -p ${DIST_DIR} || true
+    tar cjf ${DIST_DIR}/ndk_platform.tar.bz2 -C ${SOONG_OUT} ndk
+fi