Add --write_if_changed argument to soong_zip

Simplify making restat rules by adding a --write_if_changed argument
to soong_zip that generates the output file into memory and then
compares it to the version on disk before writing it out.

Test: builds
Change-Id: I5059a1e3a11e79b0ca538b3b829bc7479c126ce6
diff --git a/zip/cmd/main.go b/zip/cmd/main.go
index 348728c..c0418f7 100644
--- a/zip/cmd/main.go
+++ b/zip/cmd/main.go
@@ -120,14 +120,15 @@
 }
 
 var (
-	out          = flag.String("o", "", "file to write zip file to")
-	manifest     = flag.String("m", "", "input jar manifest file name")
-	directories  = flag.Bool("d", false, "include directories in zip")
-	rootPrefix   = flag.String("P", "", "path prefix within the zip at which to place files")
-	relativeRoot = flag.String("C", "", "path to use as relative root of files in following -f, -l, or -D arguments")
-	parallelJobs = flag.Int("j", runtime.NumCPU(), "number of parallel threads to use")
-	compLevel    = flag.Int("L", 5, "deflate compression level (0-9)")
-	emulateJar   = flag.Bool("jar", false, "modify the resultant .zip to emulate the output of 'jar'")
+	out            = flag.String("o", "", "file to write zip file to")
+	manifest       = flag.String("m", "", "input jar manifest file name")
+	directories    = flag.Bool("d", false, "include directories in zip")
+	rootPrefix     = flag.String("P", "", "path prefix within the zip at which to place files")
+	relativeRoot   = flag.String("C", "", "path to use as relative root of files in following -f, -l, or -D arguments")
+	parallelJobs   = flag.Int("j", runtime.NumCPU(), "number of parallel threads to use")
+	compLevel      = flag.Int("L", 5, "deflate compression level (0-9)")
+	emulateJar     = flag.Bool("jar", false, "modify the resultant .zip to emulate the output of 'jar'")
+	writeIfChanged = flag.Bool("write_if_changed", false, "only update resultant .zip if it has changed")
 
 	fArgs            zip.FileArgs
 	nonDeflatedFiles = make(uniqueSet)
@@ -163,6 +164,7 @@
 		ManifestSourcePath:       *manifest,
 		NumParallelJobs:          *parallelJobs,
 		NonDeflatedFiles:         nonDeflatedFiles,
+		WriteIfChanged:           *writeIfChanged,
 	})
 	if err != nil {
 		fmt.Fprintln(os.Stderr, err.Error())