Merge "Rename exported make variables for system headers"
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..df092e7
--- /dev/null
+++ b/README.md
@@ -0,0 +1,156 @@
+# Soong
+
+Soong is the replacement for the old Android make-based build system.  It
+replaces Android.mk files with Android.bp files, which are JSON-like simple
+declarative descriptions of modules to build.
+
+## Android.bp file format
+
+By design, Android.bp files are very simple.  There are no conditionals or
+control flow statements - any complexity is handled in build logic written in
+Go.
+
+### Modules
+
+A module in an Android.bp file starts with a module type, followed by a set of
+properties in `name: value,` format:
+
+```
+cc_binary {
+    name: "gzip",
+    srcs: ["src/test/minigzip.c"],
+    shared_libs: ["libz"],
+    stl: "none",
+}
+```
+
+Every module must have a `name` property, and the value must be unique across
+all Android.bp files.
+
+For a list of valid module types and their properties see
+[$OUT_DIR/soong/.bootstrap/docs/soong_build.html](https://android-build.googleplex.com/builds/latest/branches/aosp-build-tools/targets/linux/soong_build.html).
+
+### Variables
+
+An Android.bp file may contain top-level variable assignments:
+```
+gzip_srcs = ["src/test/minigzip.c"],
+
+cc_binary {
+    name: "gzip",
+    srcs: gzip_srcs,
+    shared_libs: ["libz"],
+    stl: "none",
+}
+```
+
+Variables are scoped to the remainder of the file they are declared in, as well
+as any child blueprint files.  Variables are immutable with one exception - they
+can be appended to with a += assignment, but only before they have been
+referenced.
+
+### Comments
+Android.bp files can contain C-style multiline `/* */` and C++ style single-line
+`//` comments.
+
+### Types
+
+Variables and properties are strongly typed, variables dynamically based on the
+first assignment, and properties statically by the module type.  The supported
+types are:
+* Bool (`true` or `false`)
+* Strings (`"string"`)
+* Lists of strings (`["string1", "string2"]`)
+* Maps (`{key1: "value1", key2: ["value2"]}`)
+
+Maps may values of any type, including nested maps.  Lists and maps may have
+trailing commas after the last value.
+
+### Operators
+
+Strings, lists of strings, and maps can be appended using the `+` operator.
+Appending a map produces the union of keys in both maps, appending the values
+of any keys that are present in both maps.
+
+### Defaults modules
+
+A defaults module can be used to repeat the same properties in multiple modules.
+For example:
+
+```
+cc_defaults {
+    name: "gzip_defaults",
+    shared_libs: ["libz"],
+    stl: "none",
+}
+
+cc_binary {
+    name: "gzip",
+    defaults: ["gzip_defaults"],
+    srcs: ["src/test/minigzip.c"],
+}
+```
+
+### Formatter
+
+Soong includes a canonical formatter for blueprint files, similar to
+[gofmt](https://golang.org/cmd/gofmt/).  To recursively reformat all Android.bp files
+in the current directory:
+```
+bpfmt -w .
+```
+
+The canonical format includes 4 space indents, newlines after every element of a
+multi-element list, and always includes a trailing comma in lists and maps.
+
+### Convert Android.mk files
+
+Soong includes a tool perform a first pass at converting Android.mk files
+to Android.bp files:
+
+```
+androidmk Android.mk > Android.bp
+```
+
+The tool converts variables, modules, comments, and some conditionals, but any
+custom Makefile rules or complex conditionals must be converted by hand.
+
+## Build logic
+
+The build logic is written in Go using the
+[blueprint](http://godoc.org/github.com/google/blueprint) framework.  Build
+logic receives module definitions parsed into Go structures using reflection
+and produces build rules.  The build rules are collected by blueprint and
+written to a [ninja](http://ninja-build.org) build file.
+
+## FAQ
+
+### How do I write conditionals?
+
+Soong deliberately does not support conditionals in Android.bp files.
+Instead, complexity in build rules that would require conditionals are handled
+in Go, where high level language features can be used and implicit dependencies
+introduced by conditionals can be tracked.  Most conditionals are converted
+to a map property, where one of the values in the map will be selected and
+appended to the top level properties.
+
+For example, to support architecture specific files:
+```
+cc_library {
+    ...
+    srcs: ["generic.cpp"],
+    arch: {
+        arm: {
+            srcs: ["arm.cpp"],
+        },
+        x86: {
+            srcs: ["x86.cpp"],
+        },
+    },
+}
+```
+
+## Contact
+
+Email android-building@googlegroups.com (external) for any questions, or see
+[go/soong](http://go/soong) (internal).
diff --git a/android/androidmk.go b/android/androidmk.go
index 8d2951d..7d7707f 100644
--- a/android/androidmk.go
+++ b/android/androidmk.go
@@ -146,7 +146,7 @@
 	}
 
 	if data.SubName != "" {
-		name += "_" + data.SubName
+		name += data.SubName
 	}
 
 	if data.Custom != nil {
diff --git a/android/arch.go b/android/arch.go
index 08b7b1f..13fcb29 100644
--- a/android/arch.go
+++ b/android/arch.go
@@ -814,7 +814,7 @@
 		{"mips", "mips32-fp", "", []string{"mips"}},
 		{"mips", "mips32r2-fp", "", []string{"mips"}},
 		{"mips", "mips32r2-fp-xburst", "", []string{"mips"}},
-		{"mips", "mips32r6", "", []string{"mips"}},
+		//{"mips", "mips32r6", "", []string{"mips"}},
 		// mips32r2dsp[r2]-fp fails in the assembler for divdf3.c in compiler-rt:
 		// (same errors in make and soong)
 		//   Error: invalid operands `mtlo $ac0,$11'
diff --git a/android/config.go b/android/config.go
index 0f65410..ae04756 100644
--- a/android/config.go
+++ b/android/config.go
@@ -20,6 +20,7 @@
 	"os"
 	"path/filepath"
 	"runtime"
+	"strconv"
 	"strings"
 	"sync"
 
@@ -278,7 +279,7 @@
 }
 
 func (c *config) PlatformSdkVersion() string {
-	return "22"
+	return strconv.Itoa(*c.ProductVariables.Platform_sdk_version)
 }
 
 func (c *config) BuildNumber() string {
diff --git a/android/defs.go b/android/defs.go
index be28e8b..9c6527d 100644
--- a/android/defs.go
+++ b/android/defs.go
@@ -52,6 +52,13 @@
 		},
 		"cpFlags")
 
+	// A timestamp touch rule.
+	Touch = pctx.StaticRule("Touch",
+		blueprint.RuleParams{
+			Command:     "touch $out",
+			Description: "touch $out",
+		})
+
 	// A symlink rule.
 	Symlink = pctx.StaticRule("Symlink",
 		blueprint.RuleParams{
diff --git a/android/variable.go b/android/variable.go
index a2313d5..b0639ae 100644
--- a/android/variable.go
+++ b/android/variable.go
@@ -115,7 +115,7 @@
 
 func (v *productVariables) SetDefaultConfig() {
 	*v = productVariables{
-		Platform_sdk_version:       intPtr(22),
+		Platform_sdk_version:       intPtr(23),
 		HostArch:                   stringPtr("x86_64"),
 		HostSecondaryArch:          stringPtr("x86"),
 		DeviceName:                 stringPtr("flounder"),
diff --git a/cc/androidmk.go b/cc/androidmk.go
index 7f18155..70e1f47 100644
--- a/cc/androidmk.go
+++ b/cc/androidmk.go
@@ -134,7 +134,7 @@
 func (test *testBinaryLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
 	test.binaryLinker.AndroidMk(ctx, ret)
 	if Bool(test.testLinker.Properties.Test_per_src) {
-		ret.SubName = test.binaryLinker.Properties.Stem
+		ret.SubName = "_" + test.binaryLinker.Properties.Stem
 	}
 }
 
diff --git a/cc/arm64_device.go b/cc/arm64_device.go
index 8e7c57b..b951c1a 100644
--- a/cc/arm64_device.go
+++ b/cc/arm64_device.go
@@ -103,6 +103,7 @@
 		"-isystem ${LibcRoot}/arch-arm64/include",
 		"-isystem ${LibcRoot}/include",
 		"-isystem ${LibcRoot}/kernel/uapi",
+		"-isystem ${LibcRoot}/kernel/android/uapi",
 		"-isystem ${LibcRoot}/kernel/common",
 		"-isystem ${LibcRoot}/kernel/uapi/asm-arm64",
 	}, " "))
diff --git a/cc/arm_device.go b/cc/arm_device.go
index 1624cfc..e985a38 100644
--- a/cc/arm_device.go
+++ b/cc/arm_device.go
@@ -172,6 +172,7 @@
 		"-isystem ${LibcRoot}/arch-arm/include",
 		"-isystem ${LibcRoot}/include",
 		"-isystem ${LibcRoot}/kernel/uapi",
+		"-isystem ${LibcRoot}/kernel/android/uapi",
 		"-isystem ${LibcRoot}/kernel/common",
 		"-isystem ${LibcRoot}/kernel/uapi/asm-arm",
 	}, " "))
diff --git a/cc/builder.go b/cc/builder.go
index 4efcc27..49f8871 100644
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -369,11 +369,11 @@
 
 	libFlagsList = append(libFlagsList, staticLibs.Strings()...)
 
-	if groupLate && len(lateStaticLibs) > 0 {
+	if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
 		libFlagsList = append(libFlagsList, "-Wl,--start-group")
 	}
 	libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
-	if groupLate && len(lateStaticLibs) > 0 {
+	if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
 		libFlagsList = append(libFlagsList, "-Wl,--end-group")
 	}
 
diff --git a/cc/cc.go b/cc/cc.go
index f8723bb..b5545fc 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -193,7 +193,7 @@
 		if override := config.(android.Config).Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
 			return override, nil
 		}
-		return "clang-2812033", nil
+		return "clang-3016494", nil
 	})
 	pctx.StaticVariable("clangPath", "${clangBase}/${HostPrebuiltTag}/${clangVersion}")
 	pctx.StaticVariable("clangBin", "${clangPath}/bin")
@@ -1337,7 +1337,7 @@
 	deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
 
 	if !ctx.sdk() && ctx.ModuleName() != "libcompiler_rt-extras" {
-		deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
+		deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt-extras")
 	}
 
 	if ctx.Device() {
diff --git a/cc/clang.go b/cc/clang.go
index e9dca32..be2bfe1 100644
--- a/cc/clang.go
+++ b/cc/clang.go
@@ -88,6 +88,9 @@
 		// Force clang to always output color diagnostics. Ninja will strip the ANSI
 		// color codes if it is not running in a terminal.
 		"-fcolor-diagnostics",
+
+		// http://b/29823425 Disable -Wexpansion-to-defined for Clang update to r271374
+		"-Wno-expansion-to-defined",
 	}, " "))
 
 	pctx.StaticVariable("clangExtraConlyflags", strings.Join([]string{
@@ -98,6 +101,10 @@
 		// Disable -Winconsistent-missing-override until we can clean up the existing
 		// codebase for it.
 		"-Wno-inconsistent-missing-override",
+
+		// Bug: http://b/29823425 Disable -Wnull-dereference until the
+		// new instances detected by this warning are fixed.
+		"-Wno-null-dereference",
 	}, " "))
 
 	pctx.StaticVariable("clangExtraTargetCflags", strings.Join([]string{
@@ -106,7 +113,10 @@
 
 	pctx.StaticVariable("clangExtraNoOverrideCflags", strings.Join([]string{
 		"-Werror=address-of-temporary",
-		"-Werror=null-dereference",
+		// Bug: http://b/29823425 Disable -Wnull-dereference until the
+		// new cases detected by this warning in Clang r271374 are
+		// fixed.
+		//"-Werror=null-dereference",
 		"-Werror=return-type",
 	}, " "))
 }
diff --git a/cc/mips64_device.go b/cc/mips64_device.go
index 474f284..19f9caf 100644
--- a/cc/mips64_device.go
+++ b/cc/mips64_device.go
@@ -104,6 +104,7 @@
 		"-isystem ${LibcRoot}/arch-mips64/include",
 		"-isystem ${LibcRoot}/include",
 		"-isystem ${LibcRoot}/kernel/uapi",
+		"-isystem ${LibcRoot}/kernel/android/uapi",
 		"-isystem ${LibcRoot}/kernel/common",
 		"-isystem ${LibcRoot}/kernel/uapi/asm-mips",
 	}, " "))
diff --git a/cc/mips_device.go b/cc/mips_device.go
index 7204e36..ac6f2b1 100644
--- a/cc/mips_device.go
+++ b/cc/mips_device.go
@@ -141,6 +141,7 @@
 		"-isystem ${LibcRoot}/arch-mips/include",
 		"-isystem ${LibcRoot}/include",
 		"-isystem ${LibcRoot}/kernel/uapi",
+		"-isystem ${LibcRoot}/kernel/android/uapi",
 		"-isystem ${LibcRoot}/kernel/common",
 		"-isystem ${LibcRoot}/kernel/uapi/asm-mips",
 	}, " "))
diff --git a/cc/sanitize.go b/cc/sanitize.go
index 172aae7..08ffff4 100644
--- a/cc/sanitize.go
+++ b/cc/sanitize.go
@@ -141,9 +141,10 @@
 			ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
 		}
 	}
-	if Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) ||
-		Bool(s.Thread) || Bool(s.Coverage) || Bool(s.Safestack) {
-		sanitize.Properties.SanitizerEnabled = true
+
+	if ctx.staticBinary() {
+		s.Address = nil
+		s.Thread = nil
 	}
 
 	if Bool(s.All_undefined) {
@@ -157,6 +158,11 @@
 		// TODO(ccross): error for compile_multilib = "32"?
 	}
 
+	if Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) ||
+		Bool(s.Thread) || Bool(s.Coverage) || Bool(s.Safestack) {
+		sanitize.Properties.SanitizerEnabled = true
+	}
+
 	if Bool(s.Coverage) {
 		if !Bool(s.Address) {
 			ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
diff --git a/cc/x86_64_device.go b/cc/x86_64_device.go
index 986dc86..a795ba7 100644
--- a/cc/x86_64_device.go
+++ b/cc/x86_64_device.go
@@ -152,6 +152,7 @@
 		"-isystem ${LibcRoot}/arch-x86_64/include",
 		"-isystem ${LibcRoot}/include",
 		"-isystem ${LibcRoot}/kernel/uapi",
+		"-isystem ${LibcRoot}/kernel/android/uapi",
 		"-isystem ${LibcRoot}/kernel/common",
 		"-isystem ${LibcRoot}/kernel/uapi/asm-x86",
 	}, " "))
diff --git a/cc/x86_darwin_host.go b/cc/x86_darwin_host.go
index 2894f7e..d514c14 100644
--- a/cc/x86_darwin_host.go
+++ b/cc/x86_darwin_host.go
@@ -81,6 +81,7 @@
 		"c",
 		"dl",
 		"m",
+		"ncurses",
 		"pthread",
 		"z",
 	}, "-l")
diff --git a/cc/x86_device.go b/cc/x86_device.go
index f16e68b..72689fb 100644
--- a/cc/x86_device.go
+++ b/cc/x86_device.go
@@ -171,6 +171,7 @@
 		"-isystem ${LibcRoot}/arch-x86/include",
 		"-isystem ${LibcRoot}/include",
 		"-isystem ${LibcRoot}/kernel/uapi",
+		"-isystem ${LibcRoot}/kernel/android/uapi",
 		"-isystem ${LibcRoot}/kernel/common",
 		"-isystem ${LibcRoot}/kernel/uapi/asm-x86",
 	}, " "))
diff --git a/cc/x86_windows_host.go b/cc/x86_windows_host.go
index cc0ef66..3a55dbf 100644
--- a/cc/x86_windows_host.go
+++ b/cc/x86_windows_host.go
@@ -76,6 +76,16 @@
 		"-m64",
 		"-L${windowsGccRoot}/${windowsGccTriple}/lib64",
 	}
+
+	windowsAvailableLibraries = addPrefix([]string{
+		"gdi32",
+		"imagehlp",
+		"ole32",
+		"psapi",
+		"userenv",
+		"uuid",
+		"ws2_32",
+	}, "-l")
 )
 
 const (
@@ -187,6 +197,10 @@
 	return ".exe"
 }
 
+func (t *toolchainWindows) AvailableLibraries() []string {
+	return windowsAvailableLibraries
+}
+
 var toolchainWindowsX86Singleton Toolchain = &toolchainWindowsX86{}
 var toolchainWindowsX8664Singleton Toolchain = &toolchainWindowsX8664{}