Move auto installclean to soong_ui

This way kati won't need to be run as often (either initially, or when
switching products with the same device).

Bug: 35970961
Test: m clean; m -j blueprint_tools; m -j blueprint_tools; m -j blueprint_tools
Test: lunch aosp_arm-eng; m -j blueprint_tools; lunch full-eng; m -j blueprint_tools; <repeat>
Change-Id: Ie9fca3c8f1dd412459ea47c7090c7c5fdb0bcf6e
diff --git a/ui/build/build.go b/ui/build/build.go
index b84dd7d..598e342 100644
--- a/ui/build/build.go
+++ b/ui/build/build.go
@@ -18,6 +18,7 @@
 	"io/ioutil"
 	"os"
 	"path/filepath"
+	"strings"
 	"text/template"
 )
 
@@ -91,6 +92,62 @@
 	}
 }
 
+// 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()
+}
+
 // Build the tree. The 'what' argument can be used to chose which components of
 // the build to run.
 func Build(ctx Context, config Config, what int) {
@@ -145,6 +202,8 @@
 	}
 
 	if what&BuildNinja != 0 {
+		installcleanIfNecessary(ctx, config)
+
 		// Write combined ninja file
 		createCombinedBuildNinjaFile(ctx, config)