Merge "Drop build support for LEGACY_USE_JAVA7."
diff --git a/cc/androidmk.go b/cc/androidmk.go
index cba8782..3ce01d2 100644
--- a/cc/androidmk.go
+++ b/cc/androidmk.go
@@ -203,6 +203,7 @@
 
 func (benchmark *benchmarkDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
 	ctx.subAndroidMk(ret, benchmark.binaryDecorator)
+	ret.Class = "NATIVE_TESTS"
 	ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
 		if len(benchmark.Properties.Test_suites) > 0 {
 			fmt.Fprintln(w, "LOCAL_COMPATIBILITY_SUITE :=",
diff --git a/cc/builder.go b/cc/builder.go
index a4fda5b..f8b3e02 100644
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -180,10 +180,10 @@
 	// Abidiff check turned on in advice-only mode. Builds will not fail on abi incompatibilties / extensions.
 	sAbiDiff = pctx.AndroidStaticRule("sAbiDiff",
 		blueprint.RuleParams{
-			Command:     "$sAbiDiffer -advice-only -o ${out} -new $in -old $referenceDump",
+			Command:     "$sAbiDiffer -lib $libName -arch $arch -advice-only -o ${out} -new $in -old $referenceDump",
 			CommandDeps: []string{"$sAbiDiffer"},
 		},
-		"referenceDump")
+		"referenceDump", "libName", "arch")
 )
 
 func init() {
@@ -642,6 +642,8 @@
 		Implicit:    referenceDump,
 		Args: map[string]string{
 			"referenceDump": referenceDump.String(),
+			"libName":       baseName,
+			"arch":          ctx.Arch().ArchType.Name,
 		},
 	})
 	return android.OptionalPathForPath(outputFile)
diff --git a/cc/config/mips_device.go b/cc/config/mips_device.go
index 9e27f37..ec8f133 100644
--- a/cc/config/mips_device.go
+++ b/cc/config/mips_device.go
@@ -92,7 +92,6 @@
 			"-mfp32",
 			"-modd-spreg",
 			"-mno-fused-madd",
-			"-Wa,-mmxu",
 			"-mno-synci",
 		},
 		"mips32r2dsp-fp": []string{
diff --git a/cmd/multiproduct_kati/main.go b/cmd/multiproduct_kati/main.go
index b12628e..fb1c890 100644
--- a/cmd/multiproduct_kati/main.go
+++ b/cmd/multiproduct_kati/main.go
@@ -19,6 +19,7 @@
 	"context"
 	"flag"
 	"fmt"
+	"io/ioutil"
 	"os"
 	"path/filepath"
 	"runtime"
@@ -47,15 +48,20 @@
 var keep = flag.Bool("keep", false, "keep successful output files")
 
 var outDir = flag.String("out", "", "path to store output directories (defaults to tmpdir under $OUT when empty)")
+var alternateResultDir = flag.Bool("dist", false, "write select results to $DIST_DIR (or <out>/dist when empty)")
 
 var onlyConfig = flag.Bool("only-config", false, "Only run product config (not Soong or Kati)")
 var onlySoong = flag.Bool("only-soong", false, "Only run product config and Soong (not Kati)")
 
 var buildVariant = flag.String("variant", "eng", "build variant to use")
 
+const errorLeadingLines = 20
+const errorTrailingLines = 20
+
 type Product struct {
-	ctx    build.Context
-	config build.Config
+	ctx     build.Context
+	config  build.Config
+	logFile string
 }
 
 type Status struct {
@@ -82,7 +88,7 @@
 	s.total = total
 }
 
-func (s *Status) Fail(product string, err error) {
+func (s *Status) Fail(product string, err error, logFile string) {
 	s.Finish(product)
 
 	s.lock.Lock()
@@ -96,7 +102,26 @@
 	s.failed++
 	fmt.Fprintln(s.ctx.Stderr(), "FAILED:", product)
 	s.ctx.Verboseln("FAILED:", product)
-	s.ctx.Println(err)
+
+	if logFile != "" {
+		data, err := ioutil.ReadFile(logFile)
+		if err == nil {
+			lines := strings.Split(strings.TrimSpace(string(data)), "\n")
+			if len(lines) > errorLeadingLines+errorTrailingLines+1 {
+				lines[errorLeadingLines] = fmt.Sprintf("... skipping %d lines ...",
+					len(lines)-errorLeadingLines-errorTrailingLines)
+
+				lines = append(lines[:errorLeadingLines+1],
+					lines[len(lines)-errorTrailingLines:]...)
+			}
+			for _, line := range lines {
+				fmt.Fprintln(s.ctx.Stderr(), "> ", line)
+				s.ctx.Verboseln(line)
+			}
+		}
+	}
+
+	s.ctx.Print(err)
 }
 
 func (s *Status) Finish(product string) {
@@ -163,6 +188,13 @@
 
 		*outDir = filepath.Join(config.OutDir(), name)
 
+		// Ensure the empty files exist in the output directory
+		// containing our output directory too. This is mostly for
+		// safety, but also triggers the ninja_build file so that our
+		// build servers know that they can parse the output as if it
+		// was ninja output.
+		build.SetupOutDir(buildCtx, config)
+
 		if err := os.MkdirAll(*outDir, 0777); err != nil {
 			log.Fatalf("Failed to create tempdir: %v", err)
 		}
@@ -179,8 +211,15 @@
 	log.Println("Output directory:", *outDir)
 
 	build.SetupOutDir(buildCtx, config)
-	log.SetOutput(filepath.Join(config.OutDir(), "soong.log"))
-	trace.SetOutput(filepath.Join(config.OutDir(), "build.trace"))
+	if *alternateResultDir {
+		logsDir := filepath.Join(config.DistDir(), "logs")
+		os.MkdirAll(logsDir, 0777)
+		log.SetOutput(filepath.Join(logsDir, "soong.log"))
+		trace.SetOutput(filepath.Join(logsDir, "build.trace"))
+	} else {
+		log.SetOutput(filepath.Join(config.OutDir(), "soong.log"))
+		trace.SetOutput(filepath.Join(config.OutDir(), "build.trace"))
+	}
 
 	vars, err := build.DumpMakeVars(buildCtx, config, nil, nil, []string{"all_named_products"})
 	if err != nil {
@@ -198,24 +237,34 @@
 	for _, product := range products {
 		wg.Add(1)
 		go func(product string) {
+			var stdLog string
+
 			defer wg.Done()
 			defer logger.Recover(func(err error) {
-				status.Fail(product, err)
+				status.Fail(product, err, stdLog)
 			})
 
 			productOutDir := filepath.Join(config.OutDir(), product)
+			productLogDir := productOutDir
+			if *alternateResultDir {
+				productLogDir = filepath.Join(config.DistDir(), product)
+				if err := os.MkdirAll(productLogDir, 0777); err != nil {
+					log.Fatalf("Error creating log directory: %v", err)
+				}
+			}
 
 			if err := os.MkdirAll(productOutDir, 0777); err != nil {
 				log.Fatalf("Error creating out directory: %v", err)
 			}
 
-			f, err := os.Create(filepath.Join(productOutDir, "std.log"))
+			stdLog = filepath.Join(productLogDir, "std.log")
+			f, err := os.Create(stdLog)
 			if err != nil {
 				log.Fatalf("Error creating std.log: %v", err)
 			}
 
 			productLog := logger.New(&bytes.Buffer{})
-			productLog.SetOutput(filepath.Join(productOutDir, "soong.log"))
+			productLog.SetOutput(filepath.Join(productLogDir, "soong.log"))
 
 			productCtx := build.Context{&build.ContextImpl{
 				Context:        ctx,
@@ -230,7 +279,7 @@
 			productConfig.Lunch(productCtx, product, *buildVariant)
 
 			build.Build(productCtx, productConfig, build.BuildProductConfig)
-			productConfigs <- Product{productCtx, productConfig}
+			productConfigs <- Product{productCtx, productConfig, stdLog}
 		}(product)
 	}
 	go func() {
@@ -247,7 +296,7 @@
 			for product := range productConfigs {
 				func() {
 					defer logger.Recover(func(err error) {
-						status.Fail(product.config.TargetProduct(), err)
+						status.Fail(product.config.TargetProduct(), err, product.logFile)
 					})
 
 					buildWhat := 0
diff --git a/root.bp b/root.bp
index ee7c239..08f2ff8 100644
--- a/root.bp
+++ b/root.bp
@@ -25,7 +25,7 @@
     "hardware/*",
     "libcore",
     "libnativehelper",
-    "packages/apps/HTMLViewer",
+    "packages/apps/*",
     "prebuilts/clang/host/linux-x86",
     "prebuilts/ndk",
     "prebuilts/sdk",
diff --git a/ui/build/Android.bp b/ui/build/Android.bp
index 7a83684..8f5250b 100644
--- a/ui/build/Android.bp
+++ b/ui/build/Android.bp
@@ -21,6 +21,7 @@
     ],
     srcs: [
         "build.go",
+        "cleanbuild.go",
         "config.go",
         "context.go",
         "environment.go",
diff --git a/ui/build/build.go b/ui/build/build.go
index 598e342..51dce05 100644
--- a/ui/build/build.go
+++ b/ui/build/build.go
@@ -18,7 +18,6 @@
 	"io/ioutil"
 	"os"
 	"path/filepath"
-	"strings"
 	"text/template"
 )
 
@@ -92,60 +91,13 @@
 	}
 }
 
-// Since products and build variants (unfortunately) shared the same
-// PRODUCT_OUT staging directory, things can get out of sync if different
-// build configurations are built in the same tree. This function will
-// notice when the configuration has changed and call installclean to
-// remove the files necessary to keep things consistent.
-func installcleanIfNecessary(ctx Context, config Config) {
-	if inList("installclean", config.Arguments()) {
-		return
-	}
-
-	configFile := config.DevicePreviousProductConfig()
-	prefix := "PREVIOUS_BUILD_CONFIG := "
-	suffix := "\n"
-	currentProduct := prefix + config.TargetProduct() + "-" + config.TargetBuildVariant() + suffix
-
-	writeConfig := func() {
-		err := ioutil.WriteFile(configFile, []byte(currentProduct), 0777)
-		if err != nil {
-			ctx.Fatalln("Failed to write product config:", err)
-		}
-	}
-
-	prev, err := ioutil.ReadFile(configFile)
-	if err != nil {
-		if os.IsNotExist(err) {
-			writeConfig()
-			return
-		} else {
-			ctx.Fatalln("Failed to read previous product config:", err)
-		}
-	} else if string(prev) == currentProduct {
-		return
-	}
-
-	if disable, _ := config.Environment().Get("DISABLE_AUTO_INSTALLCLEAN"); disable == "true" {
-		ctx.Println("DISABLE_AUTO_INSTALLCLEAN is set; skipping auto-clean. Your tree may be in an inconsistent state.")
-		return
-	}
-
-	ctx.BeginTrace("installclean")
-	defer ctx.EndTrace()
-
-	prevConfig := strings.TrimPrefix(strings.TrimSuffix(string(prev), suffix), prefix)
-	currentConfig := strings.TrimPrefix(strings.TrimSuffix(currentProduct, suffix), prefix)
-
-	ctx.Printf("Build configuration changed: %q -> %q, forcing installclean\n", prevConfig, currentConfig)
-
-	cleanConfig := CopyConfig(ctx, config, "installclean")
-	cleanConfig.SetKatiArgs([]string{"installclean"})
-	cleanConfig.SetNinjaArgs([]string{"installclean"})
-
-	Build(ctx, cleanConfig, BuildKati|BuildNinja)
-
-	writeConfig()
+func help(ctx Context, config Config, what int) {
+	cmd := Command(ctx, config, "make",
+		"make", "-f", "build/core/help.mk")
+	cmd.Sandbox = makeSandbox
+	cmd.Stdout = ctx.Stdout()
+	cmd.Stderr = ctx.Stderr()
+	cmd.RunOrFatal()
 }
 
 // Build the tree. The 'what' argument can be used to chose which components of
@@ -155,23 +107,10 @@
 	ctx.Verboseln("Environment:", config.Environment().Environ())
 
 	if inList("help", config.Arguments()) {
-		cmd := Command(ctx, config, "make",
-			"make", "-f", "build/core/help.mk")
-		cmd.Sandbox = makeSandbox
-		cmd.Stdout = ctx.Stdout()
-		cmd.Stderr = ctx.Stderr()
-		cmd.RunOrFatal()
+		help(ctx, config, what)
 		return
 	} else if inList("clean", config.Arguments()) || inList("clobber", config.Arguments()) {
-		// We can't use os.RemoveAll, since we don't want to remove the
-		// output directory itself, in case it's a symlink. So just do
-		// exactly what make used to do.
-		cmd := Command(ctx, config, "rm -rf $OUT_DIR/*",
-			"/bin/bash", "-c", "rm -rf "+config.OutDir()+"/*")
-		cmd.Stdout = ctx.Stdout()
-		cmd.Stderr = ctx.Stderr()
-		cmd.RunOrFatal()
-		ctx.Println("Entire build directory removed.")
+		clean(ctx, config, what)
 		return
 	}
 
@@ -187,6 +126,16 @@
 		runMakeProductConfig(ctx, config)
 	}
 
+	if inList("installclean", config.Arguments()) {
+		installClean(ctx, config, what)
+		ctx.Println("Deleted images and staging directories.")
+		return
+	} else if inList("dataclean", config.Arguments()) {
+		dataClean(ctx, config, what)
+		ctx.Println("Deleted data files.")
+		return
+	}
+
 	if what&BuildSoong != 0 {
 		// Run Soong
 		runSoongBootstrap(ctx, config)
@@ -202,7 +151,7 @@
 	}
 
 	if what&BuildNinja != 0 {
-		installcleanIfNecessary(ctx, config)
+		installCleanIfNecessary(ctx, config)
 
 		// Write combined ninja file
 		createCombinedBuildNinjaFile(ctx, config)
diff --git a/ui/build/cleanbuild.go b/ui/build/cleanbuild.go
new file mode 100644
index 0000000..27b6d14
--- /dev/null
+++ b/ui/build/cleanbuild.go
@@ -0,0 +1,169 @@
+// Copyright 2017 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 build
+
+import (
+	"fmt"
+	"io/ioutil"
+	"os"
+	"path/filepath"
+	"strings"
+)
+
+func removeGlobs(ctx Context, globs ...string) {
+	for _, glob := range globs {
+		files, err := filepath.Glob(glob)
+		if err != nil {
+			// Only possible error is ErrBadPattern
+			panic(fmt.Errorf("%q: %s", glob, err))
+		}
+
+		for _, file := range files {
+			err = os.RemoveAll(file)
+			if err != nil {
+				ctx.Fatalf("Failed to remove file %q: %v", file, err)
+			}
+		}
+	}
+}
+
+// Remove everything under the out directory. Don't remove the out directory
+// itself in case it's a symlink.
+func clean(ctx Context, config Config, what int) {
+	removeGlobs(ctx, filepath.Join(config.OutDir(), "*"))
+	ctx.Println("Entire build directory removed.")
+}
+
+func dataClean(ctx Context, config Config, what int) {
+	removeGlobs(ctx, filepath.Join(config.ProductOut(), "data", "*"))
+}
+
+// installClean deletes all of the installed files -- the intent is to remove
+// files that may no longer be installed, either because the user previously
+// installed them, or they were previously installed by default but no longer
+// are.
+//
+// This is faster than a full clean, since we're not deleting the
+// intermediates.  Instead of recompiling, we can just copy the results.
+func installClean(ctx Context, config Config, what int) {
+	dataClean(ctx, config, what)
+
+	if hostCrossOutPath := config.hostCrossOut(); hostCrossOutPath != "" {
+		hostCrossOut := func(path string) string {
+			return filepath.Join(hostCrossOutPath, path)
+		}
+		removeGlobs(ctx,
+			hostCrossOut("bin"),
+			hostCrossOut("coverage"),
+			hostCrossOut("lib*"),
+			hostCrossOut("nativetest*"))
+	}
+
+	hostOutPath := config.HostOut()
+	hostOut := func(path string) string {
+		return filepath.Join(hostOutPath, path)
+	}
+
+	productOutPath := config.ProductOut()
+	productOut := func(path string) string {
+		return filepath.Join(productOutPath, path)
+	}
+
+	// Host bin, frameworks, and lib* are intentionally omitted, since
+	// otherwise we'd have to rebuild any generated files created with
+	// those tools.
+	removeGlobs(ctx,
+		hostOut("obj/NOTICE_FILES"),
+		hostOut("obj/PACKAGING"),
+		hostOut("coverage"),
+		hostOut("cts"),
+		hostOut("nativetest*"),
+		hostOut("sdk"),
+		hostOut("sdk_addon"),
+		hostOut("testcases"),
+		hostOut("vts"),
+		productOut("*.img"),
+		productOut("*.ini"),
+		productOut("*.txt"),
+		productOut("*.xlb"),
+		productOut("*.zip"),
+		productOut("kernel"),
+		productOut("data"),
+		productOut("skin"),
+		productOut("obj/NOTICE_FILES"),
+		productOut("obj/PACKAGING"),
+		productOut("recovery"),
+		productOut("root"),
+		productOut("system"),
+		productOut("system_other"),
+		productOut("vendor"),
+		productOut("oem"),
+		productOut("obj/FAKE"),
+		productOut("breakpad"),
+		productOut("cache"),
+		productOut("coverage"),
+		productOut("installer"),
+		productOut("odm"),
+		productOut("sysloader"),
+		productOut("testcases"))
+}
+
+// Since products and build variants (unfortunately) shared the same
+// PRODUCT_OUT staging directory, things can get out of sync if different
+// build configurations are built in the same tree. This function will
+// notice when the configuration has changed and call installclean to
+// remove the files necessary to keep things consistent.
+func installCleanIfNecessary(ctx Context, config Config) {
+	configFile := config.DevicePreviousProductConfig()
+	prefix := "PREVIOUS_BUILD_CONFIG := "
+	suffix := "\n"
+	currentProduct := prefix + config.TargetProduct() + "-" + config.TargetBuildVariant() + suffix
+
+	writeConfig := func() {
+		err := ioutil.WriteFile(configFile, []byte(currentProduct), 0666)
+		if err != nil {
+			ctx.Fatalln("Failed to write product config:", err)
+		}
+	}
+
+	prev, err := ioutil.ReadFile(configFile)
+	if err != nil {
+		if os.IsNotExist(err) {
+			writeConfig()
+			return
+		} else {
+			ctx.Fatalln("Failed to read previous product config:", err)
+		}
+	} else if string(prev) == currentProduct {
+		return
+	}
+
+	if disable, _ := config.Environment().Get("DISABLE_AUTO_INSTALLCLEAN"); disable == "true" {
+		ctx.Println("DISABLE_AUTO_INSTALLCLEAN is set; skipping auto-clean. Your tree may be in an inconsistent state.")
+		return
+	}
+
+	ctx.BeginTrace("installclean")
+	defer ctx.EndTrace()
+
+	prevConfig := strings.TrimPrefix(strings.TrimSuffix(string(prev), suffix), prefix)
+	currentConfig := strings.TrimPrefix(strings.TrimSuffix(currentProduct, suffix), prefix)
+
+	ctx.Printf("Build configuration changed: %q -> %q, forcing installclean\n", prevConfig, currentConfig)
+
+	installClean(ctx, config, 0)
+
+	writeConfig()
+}
diff --git a/ui/build/config.go b/ui/build/config.go
index 51cff50..7e8091b 100644
--- a/ui/build/config.go
+++ b/ui/build/config.go
@@ -167,22 +167,6 @@
 	return Config{ret}
 }
 
-// CopyConfig copies the configuration from an existing configuration, but replaces
-// the Arguments() list with a new set. Useful if you need to run a different build
-// with the same state as an existing build config.
-func CopyConfig(ctx Context, config Config, args ...string) Config {
-	return Config{&configImpl{
-		arguments: args,
-		goma:      config.goma,
-		environ:   config.environ.Copy(),
-
-		parallel:  config.parallel,
-		keepGoing: config.keepGoing,
-		verbose:   config.verbose,
-		dist:      config.dist,
-	}}
-}
-
 // Lunch configures the environment for a specific product similarly to the
 // `lunch` bash function.
 func (c *configImpl) Lunch(ctx Context, product, variant string) {
@@ -369,8 +353,37 @@
 	return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
 }
 
+func (c *configImpl) ProductOut() string {
+	if buildType, ok := c.environ.Get("TARGET_BUILD_TYPE"); ok && buildType == "debug" {
+		return filepath.Join(c.OutDir(), "debug", "target", "product", c.TargetDevice())
+	} else {
+		return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
+	}
+}
+
 func (c *configImpl) DevicePreviousProductConfig() string {
-	return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice(), "previous_build_config.mk")
+	return filepath.Join(c.ProductOut(), "previous_build_config.mk")
+}
+
+func (c *configImpl) hostOutRoot() string {
+	if buildType, ok := c.environ.Get("HOST_BUILD_TYPE"); ok && buildType == "debug" {
+		return filepath.Join(c.OutDir(), "debug", "host")
+	} else {
+		return filepath.Join(c.OutDir(), "host")
+	}
+}
+
+func (c *configImpl) HostOut() string {
+	return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
+}
+
+// This probably needs to be multi-valued, so not exporting it for now
+func (c *configImpl) hostCrossOut() string {
+	if runtime.GOOS == "linux" {
+		return filepath.Join(c.hostOutRoot(), "windows-x86")
+	} else {
+		return ""
+	}
 }
 
 func (c *configImpl) HostPrebuiltTag() string {