Merge "Add some tests for singlethreaded usage of the Finder."
diff --git a/ui/build/kati.go b/ui/build/kati.go
index 48c38d4..cc02c76 100644
--- a/ui/build/kati.go
+++ b/ui/build/kati.go
@@ -59,11 +59,13 @@
 }
 
 func runKati(ctx Context, config Config) {
+	genKatiSuffix(ctx, config)
+
+	runKatiCleanSpec(ctx, config)
+
 	ctx.BeginTrace("kati")
 	defer ctx.EndTrace()
 
-	genKatiSuffix(ctx, config)
-
 	executable := config.PrebuiltBuildTool("ckati")
 	args := []string{
 		"--ninja",
@@ -101,9 +103,6 @@
 	}
 	cmd.Stderr = cmd.Stdout
 
-	// Kati leaks memory, so ensure leak detection is turned off
-	cmd.Environment.Set("ASAN_OPTIONS", "detect_leaks=0")
-
 	cmd.StartOrFatal()
 	katiRewriteOutput(ctx, pipe)
 	cmd.WaitOrFatal()
@@ -162,3 +161,33 @@
 		fmt.Fprintln(ctx.Stdout())
 	}
 }
+
+func runKatiCleanSpec(ctx Context, config Config) {
+	ctx.BeginTrace("kati cleanspec")
+	defer ctx.EndTrace()
+
+	executable := config.PrebuiltBuildTool("ckati")
+	args := []string{
+		"--ninja",
+		"--ninja_dir=" + config.OutDir(),
+		"--ninja_suffix=" + config.KatiSuffix() + "-cleanspec",
+		"--regen",
+		"--detect_android_echo",
+		"--color_warnings",
+		"--gen_all_targets",
+		"--werror_find_emulator",
+		"--use_find_emulator",
+		"-f", "build/make/core/cleanbuild.mk",
+		"BUILDING_WITH_NINJA=true",
+		"SOONG_MAKEVARS_MK=" + config.SoongMakeVarsMk(),
+	}
+
+	cmd := Command(ctx, config, "ckati", executable, args...)
+	cmd.Sandbox = katiCleanSpecSandbox
+	cmd.Stdout = ctx.Stdout()
+	cmd.Stderr = ctx.Stderr()
+
+	// Kati leaks memory, so ensure leak detection is turned off
+	cmd.Environment.Set("ASAN_OPTIONS", "detect_leaks=0")
+	cmd.RunOrFatal()
+}
diff --git a/ui/build/sandbox_darwin.go b/ui/build/sandbox_darwin.go
index 54c145c..60407d4 100644
--- a/ui/build/sandbox_darwin.go
+++ b/ui/build/sandbox_darwin.go
@@ -22,11 +22,12 @@
 type Sandbox string
 
 const (
-	noSandbox     = ""
-	globalSandbox = "build/soong/ui/build/sandbox/darwin/global.sb"
-	makeSandbox   = globalSandbox
-	soongSandbox  = globalSandbox
-	katiSandbox   = globalSandbox
+	noSandbox            = ""
+	globalSandbox        = "build/soong/ui/build/sandbox/darwin/global.sb"
+	makeSandbox          = globalSandbox
+	soongSandbox         = globalSandbox
+	katiSandbox          = globalSandbox
+	katiCleanSpecSandbox = globalSandbox
 )
 
 var sandboxExecPath string
diff --git a/ui/build/sandbox_linux.go b/ui/build/sandbox_linux.go
index fb48b9c..6615d37 100644
--- a/ui/build/sandbox_linux.go
+++ b/ui/build/sandbox_linux.go
@@ -17,11 +17,12 @@
 type Sandbox bool
 
 const (
-	noSandbox     = false
-	globalSandbox = false
-	makeSandbox   = false
-	soongSandbox  = false
-	katiSandbox   = false
+	noSandbox            = false
+	globalSandbox        = false
+	makeSandbox          = false
+	soongSandbox         = false
+	katiSandbox          = false
+	katiCleanSpecSandbox = false
 )
 
 func (c *Cmd) sandboxSupported() bool {