Only update previous product config when changed

I'm working on removing the --ignore_dirty=$(OUT_DIR)/% argument from
Kati. If we're always writing to a file that we're also reading, then
with that flag removed, we'll always reparse all the makefiles.

So instead, use the method from the generated java file cleanup where we
read from a "previous" file, write to a "current" file, then use a
cmp || mv to overwrite the "previous" file if necessary. When the flag
is removed, this will cause Kati to need to run twice any time this
changes, but it's better than forever.

Bug: 30947985
Test: Switch between aosp_arm-eng and full-eng
Change-Id: Id2d1445809b60ce26700bb2aca765df3b5c2b360
diff --git a/core/cleanbuild.mk b/core/cleanbuild.mk
index e3e039e..6a29585 100644
--- a/core/cleanbuild.mk
+++ b/core/cleanbuild.mk
@@ -151,6 +151,7 @@
 # necessary to keep things consistent.
 
 previous_build_config_file := $(PRODUCT_OUT)/previous_build_config.mk
+current_build_config_file := $(PRODUCT_OUT)/current_build_config.mk
 
 current_build_config := \
     $(TARGET_PRODUCT)-$(TARGET_BUILD_VARIANT)
@@ -176,15 +177,17 @@
 
 # Write the new state to the file.
 #
-ifneq ($(PREVIOUS_BUILD_CONFIG)-$(PREVIOUS_SANITIZE_TARGET),$(current_build_config)-$(current_sanitize_target))
 $(shell \
-  mkdir -p $(dir $(previous_build_config_file)) && \
+  mkdir -p $(dir $(current_build_config_file)) && \
   echo "PREVIOUS_BUILD_CONFIG := $(current_build_config)" > \
-      $(previous_build_config_file) \
+      $(current_build_config_file) \
  )
-endif
+$(shell cmp $(current_build_config_file) $(previous_build_config_file) > /dev/null 2>&1 || \
+  mv -f $(current_build_config_file) $(previous_build_config_file))
+
 PREVIOUS_BUILD_CONFIG :=
 previous_build_config_file :=
+current_build_config_file :=
 current_build_config :=
 
 #