Stop using DIST_DIR in Soong

We're only using it to distribute files in case of failure, which isn't
well supported currently, but can be handled for now by using the
DIST_DIR environment variable during the command execution.

This was at least one cause that we'd be re-running Soong during every
build server build, as the DIST_DIR values are unique.

Test: m dist
Change-Id: Ibd5e6b6c46695350de80b745bfb6a6aa685033a0
diff --git a/ui/build/config.go b/ui/build/config.go
index 1008b2e..840f505 100644
--- a/ui/build/config.go
+++ b/ui/build/config.go
@@ -34,6 +34,7 @@
 	arguments []string
 	goma      bool
 	environ   *Environment
+	distDir   string
 
 	// From the arguments
 	parallel   int
@@ -86,8 +87,11 @@
 		ret.environ.Set("OUT_DIR", outDir)
 	}
 
-	// Make sure DIST_DIR is set appropriately
-	ret.environ.Set("DIST_DIR", ret.DistDir())
+	if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
+		ret.distDir = filepath.Clean(distDir)
+	} else {
+		ret.distDir = filepath.Join(ret.OutDir(), "dist")
+	}
 
 	ret.environ.Unset(
 		// We're already using it
@@ -110,6 +114,9 @@
 		// We handle this above
 		"OUT_DIR_COMMON_BASE",
 
+		// This is handled above too, and set for individual commands later
+		"DIST_DIR",
+
 		// Variables that have caused problems in the past
 		"CDPATH",
 		"DISPLAY",
@@ -251,10 +258,10 @@
 			}
 		} else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
 			c.environ.Set(k, v)
+		} else if arg == "dist" {
+			c.dist = true
 		} else {
-			if arg == "dist" {
-				c.dist = true
-			} else if arg == "checkbuild" {
+			if arg == "checkbuild" {
 				c.checkbuild = true
 			}
 			c.arguments = append(c.arguments, arg)
@@ -378,10 +385,7 @@
 }
 
 func (c *configImpl) DistDir() string {
-	if distDir, ok := c.environ.Get("DIST_DIR"); ok {
-		return filepath.Clean(distDir)
-	}
-	return filepath.Join(c.OutDir(), "dist")
+	return c.distDir
 }
 
 func (c *configImpl) NinjaArgs() []string {
diff --git a/ui/build/kati.go b/ui/build/kati.go
index b3e820e..56e9a88 100644
--- a/ui/build/kati.go
+++ b/ui/build/kati.go
@@ -129,9 +129,7 @@
 		"TARGET_DEVICE_DIR="+config.TargetDeviceDir(),
 		"KATI_PACKAGE_MK_DIR="+config.KatiPackageMkDir())
 
-	runKati(ctx, config, katiBuildSuffix, args, func(env *Environment) {
-		env.Unset("DIST_DIR")
-	})
+	runKati(ctx, config, katiBuildSuffix, args, func(env *Environment) {})
 }
 
 func runKatiPackage(ctx Context, config Config) {
@@ -170,6 +168,7 @@
 
 		if config.Dist() {
 			env.Set("DIST", "true")
+			env.Set("DIST_DIR", config.DistDir())
 		}
 	})
 }
@@ -184,7 +183,5 @@
 		"-f", "build/make/core/cleanbuild.mk",
 		"SOONG_MAKEVARS_MK=" + config.SoongMakeVarsMk(),
 		"TARGET_DEVICE_DIR=" + config.TargetDeviceDir(),
-	}, func(env *Environment) {
-		env.Unset("DIST_DIR")
-	})
+	}, func(env *Environment) {})
 }
diff --git a/ui/build/ninja.go b/ui/build/ninja.go
index 91cb475..c8f19d1 100644
--- a/ui/build/ninja.go
+++ b/ui/build/ninja.go
@@ -60,6 +60,8 @@
 		cmd.Environment.AppendFromKati(config.KatiEnvFile())
 	}
 
+	cmd.Environment.Set("DIST_DIR", config.DistDir())
+
 	// Allow both NINJA_ARGS and NINJA_EXTRA_ARGS, since both have been
 	// used in the past to specify extra ninja arguments.
 	if extra, ok := cmd.Environment.Get("NINJA_ARGS"); ok {