Merge "Add two flags for the latest ckati"
diff --git a/core/Makefile b/core/Makefile
index 73d6e4b..89d0431 100644
--- a/core/Makefile
+++ b/core/Makefile
@@ -114,7 +114,7 @@
   ifneq ($(filter eng.%,$(BUILD_NUMBER)),)
     # Trim down BUILD_FINGERPRINT: the default BUILD_NUMBER makes it easily exceed
     # the Android system property length limit (PROPERTY_VALUE_MAX=92).
-    BF_BUILD_NUMBER := $(USER)$(shell date +%m%d%H%M)
+    BF_BUILD_NUMBER := $(USER)$(shell $(DATE) +%m%d%H%M)
   else
     BF_BUILD_NUMBER := $(BUILD_NUMBER)
   endif
@@ -197,6 +197,7 @@
 			PRIVATE_BUILD_DESC="$(PRIVATE_BUILD_DESC)" \
 			BUILD_ID="$(BUILD_ID)" \
 			BUILD_DISPLAY_ID="$(BUILD_DISPLAY_ID)" \
+			DATE="$(DATE_FROM_FILE)" \
 			BUILD_NUMBER="$(BUILD_NUMBER_FROM_FILE)" \
 			BOARD_BUILD_SYSTEM_ROOT_IMAGE="$(BOARD_BUILD_SYSTEM_ROOT_IMAGE)" \
 			PLATFORM_VERSION="$(PLATFORM_VERSION)" \
@@ -246,8 +247,8 @@
 	@echo Target vendor buildinfo: $@
 	@mkdir -p $(dir $@)
 	$(hide) echo > $@
-	$(hide) echo ro.vendor.build.date=`date`>>$@
-	$(hide) echo ro.vendor.build.date.utc=`date +%s`>>$@
+	$(hide) echo ro.vendor.build.date=`$(DATE_FROM_FILE)`>>$@
+	$(hide) echo ro.vendor.build.date.utc=`$(DATE_FROM_FILE) +%s`>>$@
 	$(hide) echo ro.vendor.build.fingerprint="$(BUILD_FINGERPRINT_FROM_FILE)">>$@
 endif
 
@@ -262,8 +263,8 @@
 	@echo Target bootimage buildinfo: $@
 	@mkdir -p $(dir $@)
 	$(hide) echo > $@
-	$(hide) echo ro.bootimage.build.date=`date`>>$@
-	$(hide) echo ro.bootimage.build.date.utc=`date +%s`>>$@
+	$(hide) echo ro.bootimage.build.date=`$(DATE_FROM_FILE)`>>$@
+	$(hide) echo ro.bootimage.build.date.utc=`$(DATE_FROM_FILE) +%s`>>$@
 	$(hide) echo ro.bootimage.build.fingerprint="$(BUILD_FINGERPRINT_FROM_FILE)">>$@
 
 # -----------------------------------------------------------------
@@ -1526,8 +1527,10 @@
 	$(hide) echo "full_recovery_image=true" >> $(zip_root)/META/misc_info.txt
 endif
 	$(call generate-userimage-prop-dictionary, $(zip_root)/META/misc_info.txt)
+ifneq ($(INSTALLED_RECOVERYIMAGE_TARGET),)
 	$(hide) PATH=$(foreach p,$(INTERNAL_USERIMAGES_BINARY_PATHS),$(p):)$$PATH MKBOOTIMG=$(MKBOOTIMG) \
 	    ./build/tools/releasetools/make_recovery_patch $(zip_root) $(zip_root)
+endif
 	@# Zip everything up, preserving symlinks
 	$(hide) (cd $(zip_root) && zip -qry ../$(notdir $@) .)
 	@# Run fs_config on all the system, vendor, boot ramdisk,
diff --git a/core/binary.mk b/core/binary.mk
index 35ecaa3..61cdcfe 100644
--- a/core/binary.mk
+++ b/core/binary.mk
@@ -53,6 +53,7 @@
 my_generated_sources := $(LOCAL_GENERATED_SOURCES)
 my_native_coverage := $(LOCAL_NATIVE_COVERAGE)
 my_additional_dependencies := $(LOCAL_MODULE_MAKEFILE) $(LOCAL_ADDITIONAL_DEPENDENCIES)
+my_export_c_include_dirs := $(LOCAL_EXPORT_C_INCLUDE_DIRS)
 
 ifdef LOCAL_IS_HOST_MODULE
 my_allow_undefined_symbols := true
@@ -206,8 +207,6 @@
 my_static_libraries := $(LOCAL_STATIC_LIBRARIES_$($(my_prefix)$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)) $(LOCAL_STATIC_LIBRARIES_$(my_32_64_bit_suffix)) $(my_static_libraries)
 my_whole_static_libraries := $(LOCAL_WHOLE_STATIC_LIBRARIES_$($(my_prefix)$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)) $(LOCAL_WHOLE_STATIC_LIBRARIES_$(my_32_64_bit_suffix)) $(my_whole_static_libraries)
 
-my_cflags := $(filter-out $($(LOCAL_2ND_ARCH_VAR_PREFIX)$(my_prefix)GLOBAL_UNSUPPORTED_CFLAGS),$(my_cflags))
-
 include $(BUILD_SYSTEM)/cxx_stl_setup.mk
 
 # Add static HAL libraries
@@ -415,7 +414,7 @@
 ## Define arm-vs-thumb-mode flags.
 ###########################################################
 LOCAL_ARM_MODE := $(strip $(LOCAL_ARM_MODE))
-ifeq ($(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH),arm)
+ifeq ($($(my_prefix)$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH),arm)
 arm_objects_mode := $(if $(LOCAL_ARM_MODE),$(LOCAL_ARM_MODE),arm)
 normal_objects_mode := $(if $(LOCAL_ARM_MODE),$(LOCAL_ARM_MODE),thumb)
 
@@ -543,34 +542,44 @@
 ALL_GENERATED_SOURCES += $(my_generated_sources)
 
 ###########################################################
-## Compile the .proto files to .cc and then to .o
+## Compile the .proto files to .cc (or .c) and then to .o
 ###########################################################
 proto_sources := $(filter %.proto,$(my_src_files))
 proto_generated_objects :=
 proto_generated_headers :=
 ifneq ($(proto_sources),)
-proto_sources_fullpath := $(addprefix $(LOCAL_PATH)/, $(proto_sources))
-proto_generated_cc_sources_dir := $(generated_sources_dir)/proto
-proto_generated_cc_sources := $(addprefix $(proto_generated_cc_sources_dir)/, \
-    $(patsubst %.proto,%.pb.cc,$(proto_sources_fullpath)))
-proto_generated_headers := $(patsubst %.pb.cc,%.pb.h, $(proto_generated_cc_sources))
+proto_generated_sources_dir := $(generated_sources_dir)/proto
 proto_generated_obj_dir := $(intermediates)/proto
+
+ifneq (,$(filter nanopb-c nanopb-c-enable_malloc, $(LOCAL_PROTOC_OPTIMIZE_TYPE)))
+my_proto_source_suffix := .c
+my_proto_c_includes := external/nanopb-c
+my_protoc_flags := --nanopb_out=$(proto_generated_sources_dir) \
+    --plugin=external/nanopb-c/generator/protoc-gen-nanopb
+else
+my_proto_source_suffix := .cc
+my_proto_c_includes := external/protobuf/src
+my_cflags += -DGOOGLE_PROTOBUF_NO_RTTI
+my_protoc_flags := --cpp_out=$(proto_generated_sources_dir)
+endif
+my_proto_c_includes += $(proto_generated_sources_dir)
+
+proto_sources_fullpath := $(addprefix $(LOCAL_PATH)/, $(proto_sources))
+proto_generated_sources := $(addprefix $(proto_generated_sources_dir)/, \
+    $(patsubst %.proto,%.pb$(my_proto_source_suffix),$(proto_sources_fullpath)))
+proto_generated_headers := $(patsubst %.pb$(my_proto_source_suffix),%.pb.h, $(proto_generated_sources))
 proto_generated_objects := $(addprefix $(proto_generated_obj_dir)/, \
     $(patsubst %.proto,%.pb.o,$(proto_sources_fullpath)))
 
-# Auto-export the generated proto source dir.
-LOCAL_EXPORT_C_INCLUDE_DIRS += $(proto_generated_cc_sources_dir)
-
 # Ensure the transform-proto-to-cc rule is only defined once in multilib build.
 ifndef $(my_prefix)_$(LOCAL_MODULE_CLASS)_$(LOCAL_MODULE)_proto_defined
-$(proto_generated_cc_sources): PRIVATE_PROTO_INCLUDES := $(TOP)
-$(proto_generated_cc_sources): PRIVATE_PROTO_CC_OUTPUT_DIR := $(proto_generated_cc_sources_dir)
-$(proto_generated_cc_sources): PRIVATE_PROTOC_FLAGS := $(LOCAL_PROTOC_FLAGS)
-$(proto_generated_cc_sources): $(proto_generated_cc_sources_dir)/%.pb.cc: %.proto $(PROTOC)
+$(proto_generated_sources): PRIVATE_PROTO_INCLUDES := $(TOP)
+$(proto_generated_sources): PRIVATE_PROTOC_FLAGS := $(LOCAL_PROTOC_FLAGS) $(my_protoc_flags)
+$(proto_generated_sources): $(proto_generated_sources_dir)/%.pb$(my_proto_source_suffix): %.proto $(PROTOC)
 	$(transform-proto-to-cc)
 
 # This is just a dummy rule to make sure gmake doesn't skip updating the dependents.
-$(proto_generated_headers): $(proto_generated_cc_sources_dir)/%.pb.h: $(proto_generated_cc_sources_dir)/%.pb.cc
+$(proto_generated_headers): $(proto_generated_sources_dir)/%.pb.h: $(proto_generated_sources_dir)/%.pb$(my_proto_source_suffix)
 	@echo "Updated header file $@."
 
 $(my_prefix)_$(LOCAL_MODULE_CLASS)_$(LOCAL_MODULE)_proto_defined := true
@@ -578,13 +587,23 @@
 
 $(proto_generated_objects): PRIVATE_ARM_MODE := $(normal_objects_mode)
 $(proto_generated_objects): PRIVATE_ARM_CFLAGS := $(normal_objects_cflags)
-$(proto_generated_objects): $(proto_generated_obj_dir)/%.o: $(proto_generated_cc_sources_dir)/%.cc $(proto_generated_headers)
+$(proto_generated_objects): $(proto_generated_obj_dir)/%.o: $(proto_generated_sources_dir)/%$(my_proto_source_suffix) $(proto_generated_headers)
+ifeq ($(my_proto_source_suffix),.c)
+	$(transform-$(PRIVATE_HOST)c-to-o)
+else
 	$(transform-$(PRIVATE_HOST)cpp-to-o)
+endif
 -include $(proto_generated_objects:%.o=%.P)
 
-my_c_includes += external/protobuf/src $(proto_generated_cc_sources_dir)
-my_cflags += -DGOOGLE_PROTOBUF_NO_RTTI
-ifeq ($(LOCAL_PROTOC_OPTIMIZE_TYPE),full)
+my_c_includes += $(my_proto_c_includes)
+# Auto-export the generated proto source dir.
+my_export_c_include_dirs += $(my_proto_c_includes)
+
+ifeq ($(LOCAL_PROTOC_OPTIMIZE_TYPE),nanopb-c-enable_malloc)
+    my_static_libraries += libprotobuf-c-nano-enable_malloc
+else ifeq ($(LOCAL_PROTOC_OPTIMIZE_TYPE),nanopb-c)
+    my_static_libraries += libprotobuf-c-nano
+else ifeq ($(LOCAL_PROTOC_OPTIMIZE_TYPE),full)
     ifdef LOCAL_SDK_VERSION
         my_static_libraries += libprotobuf-cpp-full
     else
@@ -599,50 +618,54 @@
 endif
 endif  # $(proto_sources) non-empty
 
-
 ###########################################################
 ## Compile the .dbus.xml files to c++ headers
 ###########################################################
-dbus_if_sources := $(filter %.dbus.xml,$(my_src_files))
-ifneq ($(dbus_if_sources),)
+dbus_definitions := $(filter %.dbus.xml,$(my_src_files))
+dbus_generated_headers :=
+ifneq ($(dbus_definitions),)
 
-dbus_if_sources_full_path := $(addprefix $(LOCAL_PATH)/,$(dbus_if_sources))
+dbus_definition_paths := $(addprefix $(LOCAL_PATH)/,$(dbus_definitions))
 dbus_service_config := $(filter %dbus-service-config.json,$(my_src_files))
-dbus_service_config_full_path := $(addprefix $(LOCAL_PATH)/,$(dbus_service_config))
+dbus_service_config_path := $(addprefix $(LOCAL_PATH)/,$(dbus_service_config))
 
-dbus_header_prefix := $(generated_sources_dir)/dbus_bindings
-dbus_adaptors_header := $(dbus_header_prefix)/adaptors.h
-dbus_proxies_header := $(dbus_header_prefix)/proxies.h
-dbus_method_names_header := $(dbus_header_prefix)/method_names.h
+dbus_gen_dir := $(generated_sources_dir)/dbus_bindings
 
-dbus_generated_source_dependencies := \
-    $(dbus_if_sources_full_path) \
-    $(dbus_service_config_full_path) \
-    $(DBUS_GENERATOR)
+ifdef LOCAL_DBUS_PROXY_PREFIX
+dbus_header_dir := $(dbus_gen_dir)/include/$(LOCAL_DBUS_PROXY_PREFIX)
+dbus_headers := dbus-proxies.h
+else
+dbus_header_dir := $(dbus_gen_dir)
+dbus_headers := $(patsubst %.dbus.xml,%.h,$(dbus_definitions))
+endif
+dbus_generated_headers := $(addprefix $(dbus_header_dir)/,$(dbus_headers))
 
 # Ensure that we only define build rules once in multilib builds.
 ifndef $(my_prefix)_$(LOCAL_MODULE_CLASS)_$(LOCAL_MODULE)_dbus_bindings_defined
 $(my_prefix)_$(LOCAL_MODULE_CLASS)_$(LOCAL_MODULE)_dbus_bindings_defined := true
-$(dbus_adaptors_header) : PRIVATE_DBUS_MODULE := $(LOCAL_MODULE)
-$(dbus_adaptors_header) : PRIVATE_DBUS_HEADER_DIRECTORY := $(dbus_header_prefix)
-$(dbus_adaptors_header) : PRIVATE_DBUS_SERVICE_CONFIG := $(dbus_service_config_full_path)
-$(dbus_adaptors_header) : PRIVATE_DBUS_ADAPTORS_HEADER := $(dbus_adaptors_header)
-$(dbus_adaptors_header) : PRIVATE_DBUS_PROXIES_HEADER := $(dbus_proxies_header)
-$(dbus_adaptors_header) : PRIVATE_DBUS_METHOD_NAMES_HEADER := $(dbus_method_names_header)
-$(dbus_adaptors_header) : PRIVATE_DBUS_INTERFACE_DEFINITIONS := $(dbus_if_sources_full_path)
-$(dbus_adaptors_header) : $(dbus_generated_source_dependencies)
-	$(generate-dbus-bindings)
-# These are actually generated by the above recipe, but we'd like to make the
-# dependency chain complete.
-$(dbus_proxies_header) : $(dbus_adaptors_header)
-$(dbus_method_names_header) : $(dbus_adaptors_header)
+
+$(dbus_generated_headers): PRIVATE_MODULE := $(LOCAL_MODULE)
+$(dbus_generated_headers): PRIVATE_DBUS_SERVICE_CONFIG := $(dbus_service_config_path)
+$(dbus_generated_headers) : $(dbus_service_config_path) $(DBUS_GENERATOR)
+ifdef LOCAL_DBUS_PROXY_PREFIX
+$(dbus_generated_headers) : $(dbus_definition_paths)
+	$(generate-dbus-proxies)
+
+# Auto-export the generated dbus proxy directory.
+my_export_c_include_dirs += $(dbus_gen_dir)/include
+my_c_includes += $(dbus_gen_dir)/include
+else
+$(dbus_generated_headers) : $(dbus_header_dir)/%.h : $(LOCAL_PATH)/%.dbus.xml
+	$(generate-dbus-adaptors)
+
+my_export_c_include_dirs += $(dbus_header_dir)
+my_c_includes += $(dbus_header_dir)
+endif  # $(LOCAL_DBUS_PROXY_PREFIX)
 endif  # $(my_prefix)_$(LOCAL_MODULE_CLASS)_$(LOCAL_MODULE)_dbus_bindings_defined
 
-my_generated_sources += \
-    $(dbus_adaptors_header) \
-    $(dbus_proxies_header) \
-    $(dbus_method_names_header)
-endif  # $(dbus_if_sources) non-empty
+my_generated_sources += $(dbus_generated_headers)
+
+endif  # $(dbus_definitions) non-empty
 
 
 ###########################################################
@@ -1188,12 +1211,12 @@
 # Export includes
 ###########################################################
 export_includes := $(intermediates)/export_includes
-$(export_includes): PRIVATE_EXPORT_C_INCLUDE_DIRS := $(LOCAL_EXPORT_C_INCLUDE_DIRS)
+$(export_includes): PRIVATE_EXPORT_C_INCLUDE_DIRS := $(my_export_c_include_dirs)
 # Make sure .pb.h are already generated before any dependent source files get compiled.
-$(export_includes) : $(LOCAL_MODULE_MAKEFILE) $(proto_generated_headers)
+$(export_includes) : $(LOCAL_MODULE_MAKEFILE) $(proto_generated_headers) $(dbus_generated_headers)
 	@echo Export includes file: $< -- $@
 	$(hide) mkdir -p $(dir $@) && rm -f $@
-ifdef LOCAL_EXPORT_C_INCLUDE_DIRS
+ifdef my_export_c_include_dirs
 	$(hide) for d in $(PRIVATE_EXPORT_C_INCLUDE_DIRS); do \
 	        echo "-I $$d" >> $@; \
 	        done
diff --git a/core/clang/TARGET_x86.mk b/core/clang/TARGET_x86.mk
index 4e9e8be..6922f97 100644
--- a/core/clang/TARGET_x86.mk
+++ b/core/clang/TARGET_x86.mk
@@ -19,11 +19,8 @@
   $(CLANG_CONFIG_TARGET_EXTRA_CFLAGS) \
   $(CLANG_CONFIG_x86_EXTRA_CFLAGS) \
   $(CLANG_CONFIG_x86_TARGET_EXTRA_ASFLAGS) \
-  -fno-optimize-sibling-calls \
   -mstackrealign
 
-# http://llvm.org/bugs/show_bug.cgi?id=15086,
-# llvm tail call optimization is wrong for x86.
 # -mstackrealign is needed to realign stack in native code
 # that could be called from JNI, so that movaps instruction
 # will work on assumed stack aligned local variables.
diff --git a/core/clear_vars.mk b/core/clear_vars.mk
index e5d299c..431c688 100644
--- a/core/clear_vars.mk
+++ b/core/clear_vars.mk
@@ -180,6 +180,7 @@
 LOCAL_DPI_FILE_STEM:=
 LOCAL_SANITIZE:=
 LOCAL_SANITIZE_RECOVER:=
+LOCAL_DBUS_PROXY_PREFIX:=
 
 # arch specific variables
 LOCAL_SRC_FILES_$(TARGET_ARCH):=
diff --git a/core/combo/HOST_windows-x86.mk b/core/combo/HOST_windows-x86.mk
index 5839e73..eb1c49c 100644
--- a/core/combo/HOST_windows-x86.mk
+++ b/core/combo/HOST_windows-x86.mk
@@ -14,19 +14,10 @@
 # limitations under the License.
 #
 
-# Configuration for Linux on x86.
+# Settings to use MinGW as a cross-compiler under Linux
 # Included by combo/select.make
 
-# right now we get these from the environment, but we should
-# pick them from the tree somewhere
-TOOLS_PREFIX := #prebuilt/windows/host/bin/
-TOOLS_EXE_SUFFIX := .exe
-
-# Settings to use MinGW has a cross-compiler under Linux
-ifneq ($(findstring Linux,$(UNAME)),)
-ifdef USE_MINGW
 HOST_ACP_UNAVAILABLE := true
-TOOLS_EXE_SUFFIX :=
 $(combo_2nd_arch_prefix)HOST_GLOBAL_CFLAGS += -DUSE_MINGW -DWIN32_LEAN_AND_MEAN
 $(combo_2nd_arch_prefix)HOST_GLOBAL_CFLAGS += -Wno-unused-parameter
 $(combo_2nd_arch_prefix)HOST_GLOBAL_CFLAGS += --sysroot=prebuilts/gcc/linux-x86/host/x86_64-w64-mingw32-4.8/x86_64-w64-mingw32
@@ -36,8 +27,6 @@
 $(combo_2nd_arch_prefix)HOST_C_INCLUDES += prebuilts/gcc/linux-x86/host/x86_64-w64-mingw32-4.8/x86_64-w64-mingw32/include
 $(combo_2nd_arch_prefix)HOST_C_INCLUDES += prebuilts/gcc/linux-x86/host/x86_64-w64-mingw32-4.8/lib/gcc/x86_64-w64-mingw32/4.8.3/include
 $(combo_2nd_arch_prefix)HOST_GLOBAL_LD_DIRS += -Lprebuilts/gcc/linux-x86/host/x86_64-w64-mingw32-4.8/x86_64-w64-mingw32/lib32
-endif # USE_MINGW
-endif # Linux
 
 # Workaround differences in inttypes.h between host and target.
 # See bug 12708004.
@@ -59,22 +48,6 @@
 $(combo_2nd_arch_prefix)HOST_GLOBAL_LDFLAGS += -static
 endif # BUILD_HOST_static
 
-# when building under Cygwin, ensure that we use Mingw compilation by default.
-# you can disable this (i.e. to generate Cygwin executables) by defining the
-# USE_CYGWIN variable in your environment, e.g.:
-#
-#   export USE_CYGWIN=1
-#
-# note that the -mno-cygwin flags are not needed when cross-compiling the
-# Windows host tools on Linux
-#
-ifneq ($(findstring CYGWIN,$(UNAME)),)
-ifeq ($(strip $(USE_CYGWIN)),)
-$(combo_2nd_arch_prefix)HOST_GLOBAL_CFLAGS += -mno-cygwin
-$(combo_2nd_arch_prefix)HOST_GLOBAL_LDFLAGS += -mno-cygwin -mconsole
-endif
-endif
-
 ############################################################
 ## Macros after this line are shared by the 64-bit config.
 
@@ -82,7 +55,6 @@
 HOST_EXECUTABLE_SUFFIX := .exe
 
 # $(1): The file to check
-# TODO: find out what format cygwin's stat(1) uses
 define get-file-size
-999999999
+stat --format "%s" "$(1)" | tr -d '\n'
 endef
diff --git a/core/combo/HOST_windows-x86_64.mk b/core/combo/HOST_windows-x86_64.mk
index c526f20..b38c74c 100644
--- a/core/combo/HOST_windows-x86_64.mk
+++ b/core/combo/HOST_windows-x86_64.mk
@@ -14,25 +14,17 @@
 # limitations under the License.
 #
 
-# Configuration for Windows on x86_64.
+# Settings to use MinGW as a cross-compiler under Linux
 # Included by combo/select.make
 
 # right now we get these from the environment, but we should
 # pick them from the tree somewhere
-TOOLS_PREFIX := #prebuilt/windows/host/bin/
-TOOLS_EXE_SUFFIX := .exe
-
-# Settings to use MinGW has a cross-compiler under Linux
-ifneq ($(findstring Linux,$(UNAME)),)
-ifdef USE_MINGW
-HOST_ACP_UNAVAILABLE := true
-TOOLS_EXE_SUFFIX :=
-HOST_GLOBAL_CFLAGS += -DUSE_MINGW
 TOOLS_PREFIX := /usr/bin/amd64-mingw32msvc-
+
+HOST_ACP_UNAVAILABLE := true
+HOST_GLOBAL_CFLAGS += -DUSE_MINGW
 HOST_C_INCLUDES += /usr/lib/gcc/amd64-mingw32msvc/4.4.2/include
 HOST_GLOBAL_LD_DIRS += -L/usr/amd64-mingw32msvc/lib
-endif # USE_MINGW
-endif # Linux
 
 # Workaround differences in inttypes.h between host and target.
 # See bug 12708004.
@@ -50,19 +42,3 @@
 # Statically-linked binaries are desirable for sandboxed environment
 HOST_GLOBAL_LDFLAGS += -static
 endif # BUILD_HOST_static
-
-# when building under Cygwin, ensure that we use Mingw compilation by default.
-# you can disable this (i.e. to generate Cygwin executables) by defining the
-# USE_CYGWIN variable in your environment, e.g.:
-#
-#   export USE_CYGWIN=1
-#
-# note that the -mno-cygwin flags are not needed when cross-compiling the
-# Windows host tools on Linux
-#
-ifneq ($(findstring CYGWIN,$(UNAME)),)
-ifeq ($(strip $(USE_CYGWIN)),)
-HOST_GLOBAL_CFLAGS += -mno-cygwin
-HOST_GLOBAL_LDFLAGS += -mno-cygwin -mconsole
-endif
-endif
diff --git a/core/combo/include/arch/windows/AndroidConfig.h b/core/combo/include/arch/windows/AndroidConfig.h
index 60268c8..9ed61e9 100644
--- a/core/combo/include/arch/windows/AndroidConfig.h
+++ b/core/combo/include/arch/windows/AndroidConfig.h
@@ -34,11 +34,6 @@
 #endif
 
 /*
- * Define this if you build against MSVCRT.DLL
- */
-#define HAVE_MS_C_RUNTIME
-
-/*
  * We need to choose between 32-bit and 64-bit off_t.  All of our code should
  * agree on the same size.  For desktop systems, use 64-bit values,
  * because some of our libraries (e.g. wxWidgets) expect to be built that way.
diff --git a/core/combo/javac.mk b/core/combo/javac.mk
index fb43ea4..e3838a6 100644
--- a/core/combo/javac.mk
+++ b/core/combo/javac.mk
@@ -26,12 +26,7 @@
 endif
 
 # Whatever compiler is on this system.
-ifeq ($(BUILD_OS), windows)
-    COMMON_JAVAC := development/host/windows/prebuilt/javawrap.exe -J-Xmx256m \
-        $(common_jdk_flags)
-else
-    COMMON_JAVAC := $(JAVACC) -J-Xmx1024M $(common_jdk_flags)
-endif
+COMMON_JAVAC := $(JAVACC) -J-Xmx1024M $(common_jdk_flags)
 
 # Eclipse.
 ifeq ($(CUSTOM_JAVA_COMPILER), eclipse)
diff --git a/core/config.mk b/core/config.mk
index 498c793..78a43ec 100644
--- a/core/config.mk
+++ b/core/config.mk
@@ -85,6 +85,8 @@
 BUILD_NATIVE_TEST := $(BUILD_SYSTEM)/native_test.mk
 BUILD_NATIVE_BENCHMARK := $(BUILD_SYSTEM)/native_benchmark.mk
 BUILD_HOST_NATIVE_TEST := $(BUILD_SYSTEM)/host_native_test.mk
+BUILD_FUZZ_TEST := $(BUILD_SYSTEM)/fuzz_test.mk
+BUILD_HOST_FUZZ_TEST := $(BUILD_SYSTEM)/host_fuzz_test.mk
 
 BUILD_SHARED_TEST_LIBRARY := $(BUILD_SYSTEM)/shared_test_lib.mk
 BUILD_HOST_SHARED_TEST_LIBRARY := $(BUILD_SYSTEM)/host_shared_test_lib.mk
@@ -161,6 +163,23 @@
 # are specific to the user's build configuration.
 include $(BUILD_SYSTEM)/envsetup.mk
 
+# ---------------------------------------------------------------
+# Allow the C/C++ macros __DATE__ and __TIME__ to be set to the
+# build date and time, so that a build may be repeated.
+# Write the date and time to a file so that the command line
+# doesn't change every time, which would cause ninja to rebuild
+# the files.
+$(shell mkdir -p $(OUT_DIR) && \
+    $(DATE) "+%b %_d %Y" > $(OUT_DIR)/build_c_date.txt && \
+    $(DATE) +%T > $(OUT_DIR)/build_c_time.txt)
+BUILD_DATETIME_C_DATE := $$(cat $(OUT_DIR)/build_c_date.txt)
+BUILD_DATETIME_C_TIME := $$(cat $(OUT_DIR)/build_c_time.txt)
+
+ifeq ($(OVERRIDE_C_DATE_TIME),true)
+COMMON_GLOBAL_CFLAGS += -Wno-builtin-macro-redefined -D__DATE__="\"$(BUILD_DATETIME_C_DATE)\"" -D__TIME__=\"$(BUILD_DATETIME_C_TIME)\"
+COMMON_GLOBAL_CPPFLAGS += -Wno-builtin-macro-redefined -D__DATE__="\"$(BUILD_DATETIME_C_DATE)\"" -D__TIME__=\"$(BUILD_DATETIME_C_TIME)\"
+endif
+
 # The build system exposes several variables for where to find the kernel
 # headers:
 #   TARGET_DEVICE_KERNEL_HEADERS is automatically created for the current
@@ -493,12 +512,7 @@
 
 YACC_HEADER_SUFFIX:= .hpp
 
-# Don't use column under Windows, cygwin or not
-ifeq ($(HOST_OS),windows)
-COLUMN:= cat
-else
 COLUMN:= column
-endif
 
 HOST_JDK_TOOLS_JAR:= $(shell $(BUILD_SYSTEM)/find-jdk-tools-jar.sh)
 
diff --git a/core/config_sanitizers.mk b/core/config_sanitizers.mk
index 78cedad..1efc932 100644
--- a/core/config_sanitizers.mk
+++ b/core/config_sanitizers.mk
@@ -4,33 +4,33 @@
 
 my_sanitize := $(strip $(LOCAL_SANITIZE))
 
+# SANITIZE_HOST is only in effect if the module is already using clang (host
+# modules that haven't set `LOCAL_CLANG := false` and device modules that
+# have set `LOCAL_CLANG := true`.
+my_global_sanitize :=
+ifeq ($(my_clang),true)
+  ifdef LOCAL_IS_HOST_MODULE
+    my_global_sanitize := $(strip $(SANITIZE_HOST))
+
+    # SANITIZE_HOST=true is a deprecated way to say SANITIZE_HOST=address.
+    my_global_sanitize := $(subst true,address,$(my_global_sanitize))
+  else
+    my_global_sanitize := $(strip $(SANITIZE_TARGET))
+  endif
+endif
+
+# The sanitizer specified by the environment wins over the module.
+ifneq ($(my_global_sanitize),)
+  my_sanitize := $(my_global_sanitize)
+endif
+
 # Don't apply sanitizers to NDK code.
 ifdef LOCAL_SDK_VERSION
-  my_sanitize := never
+  my_sanitize :=
 endif
 
-# Configure SANITIZE_HOST / SANITIZE_TARGET.
-ifeq ($(my_sanitize),)
-  ifdef LOCAL_IS_HOST_MODULE
-    my_sanitize := $(strip $(SANITIZE_HOST))
-  else
-    my_sanitize := $(strip $(SANITIZE_TARGET))
-  endif
-
-  # SANITIZE_HOST=true is a deprecated way to say SANITIZE_HOST=address.
-  ifeq ($(my_sanitize),true)
-    my_sanitize := address
-  endif
-
-  # SANITIZE_HOST is only in effect if the module is already using clang (host
-  # modules that haven't set `LOCAL_CLANG := false` and device modules that
-  # have set `LOCAL_CLANG := true`.
-  ifneq ($(my_clang),true)
-    my_sanitize :=
-  endif
-endif
-
-ifeq ($(my_sanitize),never)
+# Never always wins.
+ifeq ($(LOCAL_SANITIZE),never)
   my_sanitize :=
 endif
 
@@ -108,7 +108,10 @@
                            $(ADDRESS_SANITIZER_CONFIG_EXTRA_SHARED_LIBRARIES)
     my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)
     my_ldflags += -Wl,-rpath,$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RPATH)
+
     my_linker := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
+    # Make sure linker_asan get installed.
+    $(LOCAL_INSTALLED_MODULE) : | $(PRODUCT_OUT)$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
   endif
 endif
 
diff --git a/core/definitions.mk b/core/definitions.mk
index 35e3a79..36bb0d4 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -957,7 +957,7 @@
 endef
 
 ######################################################################
-## Commands for running protoc to compile .proto into .pb.cc and .pb.h
+## Commands for running protoc to compile .proto into .pb.cc (or.pb.c) and .pb.h
 ######################################################################
 define transform-proto-to-cc
 @echo "Protoc: $@ <= $<"
@@ -965,22 +965,32 @@
 $(hide) $(PROTOC) \
 	$(addprefix --proto_path=, $(PRIVATE_PROTO_INCLUDES)) \
 	$(PRIVATE_PROTOC_FLAGS) \
-	--cpp_out=$(PRIVATE_PROTO_CC_OUTPUT_DIR) $<
+	$<
 endef
 
 
 ######################################################################
-## Commands for running protoc to compile .proto into .pb.cc and .pb.h
+## Commands for generating DBus adaptors from .dbus.xml files.
 ######################################################################
-define generate-dbus-bindings
-@echo "Generating DBus bindings for $(PRIVATE_DBUS_MODULE)"
-@mkdir -p $(PRIVATE_DBUS_HEADER_DIRECTORY)
+define generate-dbus-adaptors
+@echo "Generating DBus adaptors for $(PRIVATE_MODULE)"
+@mkdir -p $(dir $@)
 $(hide) $(DBUS_GENERATOR) \
 	--service-config=$(PRIVATE_DBUS_SERVICE_CONFIG) \
-	--adaptor=$(PRIVATE_DBUS_ADAPTORS_HEADER) \
-	--proxy=$(PRIVATE_DBUS_PROXIES_HEADER) \
-	--method-names=$(PRIVATE_DBUS_METHOD_NAMES_HEADER) \
-	$(PRIVATE_DBUS_INTERFACE_DEFINITIONS)
+	--adaptor=$@ \
+	$<
+endef
+
+######################################################################
+## Commands for generating DBus proxies from .dbus.xml files.
+######################################################################
+define generate-dbus-proxies
+@echo "Generating DBus proxies for $(PRIVATE_MODULE)"
+@mkdir -p $(dir $@)
+$(hide) $(DBUS_GENERATOR) \
+	--service-config=$(PRIVATE_DBUS_SERVICE_CONFIG) \
+	--proxy=$@ \
+	$(filter %.dbus.xml,$^)
 endef
 
 
@@ -1645,11 +1655,7 @@
     $(addprefix --rename-instrumentation-target-package , $(PRIVATE_MANIFEST_INSTRUMENTATION_FOR))
 endef
 
-ifeq ($(HOST_OS),windows)
-xlint_unchecked :=
-else
 xlint_unchecked := -Xlint:unchecked
-endif
 
 ifeq (true, $(ENABLE_INCREMENTALJAVAC))
 incremental_dex := --incremental
@@ -1988,13 +1994,12 @@
 
 #TODO: use a smaller -Xmx value for most libraries;
 #      only core.jar and framework.jar need a heap this big.
-# Avoid the memory arguments on Windows, dx fails to load for some reason with them.
 define transform-classes.jar-to-dex
 @echo "target Dex: $(PRIVATE_MODULE)"
 @mkdir -p $(dir $@)
 $(hide) rm -f $(dir $@)classes*.dex
 $(hide) $(DX) \
-    $(if $(findstring windows,$(HOST_OS)),,-JXms16M -JXmx2048M) \
+    -JXms16M -JXmx2048M \
     --dex --output=$(dir $@) \
     $(incremental_dex) \
     $(if $(NO_OPTIMIZE_DX), \
diff --git a/core/droiddoc.mk b/core/droiddoc.mk
index 417d4fd..aae2aba 100644
--- a/core/droiddoc.mk
+++ b/core/droiddoc.mk
@@ -132,7 +132,7 @@
 
 $(full_target): PRIVATE_DOCLETPATH := $(HOST_OUT_JAVA_LIBRARIES)/jsilver$(COMMON_JAVA_PACKAGE_SUFFIX):$(HOST_OUT_JAVA_LIBRARIES)/doclava$(COMMON_JAVA_PACKAGE_SUFFIX)
 $(full_target): PRIVATE_CURRENT_BUILD := -hdf page.build $(BUILD_ID)-$(BUILD_NUMBER_FROM_FILE)
-$(full_target): PRIVATE_CURRENT_TIME :=  -hdf page.now "$$(date "+%d %b %Y %k:%M")"
+$(full_target): PRIVATE_CURRENT_TIME :=  -hdf page.now "$$($(DATE_FROM_FILE) "+%d %b %Y %k:%M")"
 $(full_target): PRIVATE_CUSTOM_TEMPLATE_DIR := $(LOCAL_DROIDDOC_CUSTOM_TEMPLATE_DIR)
 $(full_target): PRIVATE_IN_CUSTOM_ASSET_DIR := $(LOCAL_DROIDDOC_CUSTOM_TEMPLATE_DIR)/$(LOCAL_DROIDDOC_CUSTOM_ASSET_DIR)
 $(full_target): PRIVATE_OUT_ASSET_DIR := $(out_dir)/$(LOCAL_DROIDDOC_ASSET_DIR)
diff --git a/core/envsetup.mk b/core/envsetup.mk
index 0b68152..c76e9a6 100644
--- a/core/envsetup.mk
+++ b/core/envsetup.mk
@@ -48,9 +48,6 @@
 ifneq (,$(findstring Macintosh,$(UNAME)))
   HOST_OS := darwin
 endif
-ifneq (,$(findstring CYGWIN,$(UNAME)))
-  HOST_OS := windows
-endif
 
 # BUILD_OS is the real host doing the build.
 BUILD_OS := $(HOST_OS)
diff --git a/core/fuzz_test.mk b/core/fuzz_test.mk
new file mode 100644
index 0000000..065cc03
--- /dev/null
+++ b/core/fuzz_test.mk
@@ -0,0 +1,34 @@
+###########################################
+## A thin wrapper around BUILD_EXECUTABLE
+## Common flags for fuzz tests are added.
+###########################################
+
+ifdef LOCAL_SDK_VERSION
+    $(error $(LOCAL_PATH): $(LOCAL_MODULE): NDK fuzz tests are not supported.)
+endif
+
+LOCAL_CFLAGS += -fsanitize-coverage=edge,indirect-calls,8bit-counters
+LOCAL_STATIC_LIBRARIES += libLLVMFuzzer
+
+ifdef LOCAL_MODULE_PATH
+$(error $(LOCAL_PATH): Do not set LOCAL_MODULE_PATH when building test $(LOCAL_MODULE))
+endif
+
+ifdef LOCAL_MODULE_PATH_32
+$(error $(LOCAL_PATH): Do not set LOCAL_MODULE_PATH_32 when building test $(LOCAL_MODULE))
+endif
+
+ifdef LOCAL_MODULE_PATH_64
+$(error $(LOCAL_PATH): Do not set LOCAL_MODULE_PATH_64 when building test $(LOCAL_MODULE))
+endif
+
+LOCAL_MODULE_PATH_64 := $(TARGET_OUT_DATA_NATIVE_TESTS)/$(LOCAL_MODULE)
+LOCAL_MODULE_PATH_32 := $($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_DATA_NATIVE_TESTS)/$(LOCAL_MODULE)
+
+ifndef LOCAL_MULTILIB
+ifndef LOCAL_32_BIT_ONLY
+LOCAL_MULTILIB := both
+endif
+endif
+
+include $(BUILD_EXECUTABLE)
diff --git a/core/host_fuzz_test.mk b/core/host_fuzz_test.mk
new file mode 100644
index 0000000..e917959
--- /dev/null
+++ b/core/host_fuzz_test.mk
@@ -0,0 +1,9 @@
+################################################
+## A thin wrapper around BUILD_HOST_EXECUTABLE
+## Common flags for host fuzz tests are added.
+################################################
+
+LOCAL_CFLAGS += -fsanitize-coverage=edge,indirect-calls,8bit-counters
+LOCAL_STATIC_LIBRARIES += libLLVMFuzzer
+
+include $(BUILD_HOST_EXECUTABLE)
diff --git a/core/main.mk b/core/main.mk
index b290ce1..3c5a554 100644
--- a/core/main.mk
+++ b/core/main.mk
@@ -38,8 +38,6 @@
 #endif
 
 # Check for broken versions of make.
-# (Allow any version under Cygwin since we don't actually build the platform there.)
-ifeq (,$(findstring CYGWIN,$(shell uname -sm)))
 ifneq (1,$(strip $(shell expr $(MAKE_VERSION) \>= 3.81)))
 $(warning ********************************************************************************)
 $(warning *  You are using version $(MAKE_VERSION) of make.)
@@ -48,7 +46,6 @@
 $(warning ********************************************************************************)
 $(error stopping)
 endif
-endif
 
 # Absolute path of the present working direcotry.
 # This overrides the shell variable $PWD, which does not necessarily points to
@@ -102,8 +99,16 @@
 # Write the build number to a file so it can be read back in
 # without changing the command line every time.  Avoids rebuilds
 # when using ninja.
-$(shell mkdir -p $(OUT_DIR) && echo -n $(BUILD_NUMBER) > $(OUT_DIR)/build_number.txt)
+$(shell mkdir -p $(OUT_DIR) && \
+    echo -n $(BUILD_NUMBER) > $(OUT_DIR)/build_number.txt && \
+    echo -n $(BUILD_DATETIME) > $(OUT_DIR)/build_date.txt)
 BUILD_NUMBER_FROM_FILE := $$(cat $(OUT_DIR)/build_number.txt)
+BUILD_DATETIME_FROM_FILE := $$(cat $(OUT_DIR)/build_date.txt)
+ifeq ($(HOST_OS),darwin)
+DATE_FROM_FILE := date -r $(BUILD_DATETIME_FROM_FILE)
+else
+DATE_FROM_FILE := date -d @$(BUILD_DATETIME_FROM_FILE)
+endif
 
 # CTS-specific config.
 -include cts/build/config.mk
@@ -123,7 +128,6 @@
 
 $(info Checking build tools versions...)
 
-ifneq ($(HOST_OS),windows)
 # check for a case sensitive file system
 ifneq (a,$(shell mkdir -p $(OUT_DIR) ; \
                 echo a > $(OUT_DIR)/casecheck.txt; \
@@ -135,7 +139,6 @@
 $(warning ************************************************************)
 $(error Case-insensitive filesystems not supported)
 endif
-endif
 
 # Make sure that there are no spaces in the absolute path; the
 # build system can't deal with them.
@@ -471,11 +474,6 @@
 include $(TOPDIR)sdk/build/windows_sdk_whitelist.mk
 include $(TOPDIR)development/build/windows_sdk_whitelist.mk
 
-# Exclude tools/acp when cross-compiling windows under linux
-ifeq ($(findstring Linux,$(UNAME)),)
-subdirs += build/tools/acp
-endif
-
 else	# !SDK_ONLY
 #
 # Typical build; include any Android.mk files we can find.
diff --git a/core/version_defaults.mk b/core/version_defaults.mk
index f3b2297..170e20b 100644
--- a/core/version_defaults.mk
+++ b/core/version_defaults.mk
@@ -24,6 +24,7 @@
 #     DEFAULT_APP_TARGET_SDK
 #     BUILD_ID
 #     BUILD_NUMBER
+#     BUILD_DATETIME
 #
 
 # Look for an optional file containing overrides of the defaults,
@@ -89,6 +90,18 @@
   BUILD_ID := UNKNOWN
 endif
 
+ifeq "" "$(BUILD_DATETIME)"
+  # Used to reproduce builds by setting the same time. Must be the number
+  # of seconds since the Epoch.
+  BUILD_DATETIME := $(shell date +%s)
+endif
+
+ifneq (,$(findstring Darwin,$(shell uname -sm)))
+DATE := date -r $(BUILD_DATETIME)
+else
+DATE := date -d @$(BUILD_DATETIME)
+endif
+
 ifeq "" "$(BUILD_NUMBER)"
   # BUILD_NUMBER should be set to the source control value that
   # represents the current state of the source code.  E.g., a
@@ -99,5 +112,5 @@
   # If no BUILD_NUMBER is set, create a useful "I am an engineering build
   # from this date/time" value.  Make it start with a non-digit so that
   # anyone trying to parse it as an integer will probably get "0".
-  BUILD_NUMBER := eng.$(USER).$(shell date +%Y%m%d.%H%M%S)
+  BUILD_NUMBER := eng.$(USER).$(shell $(DATE) +%Y%m%d.%H%M%S)
 endif
diff --git a/libs/host/Android.mk b/libs/host/Android.mk
index d1d2e55..821f9b8 100644
--- a/libs/host/Android.mk
+++ b/libs/host/Android.mk
@@ -5,9 +5,6 @@
 LOCAL_SRC_FILES:= \
     CopyFile.c
 
-ifeq ($(HOST_OS),cygwin)
-LOCAL_CFLAGS += -DWIN32_EXE
-endif
 ifeq ($(HOST_OS),darwin)
 LOCAL_CFLAGS += -DMACOSX_RSRC
 endif
diff --git a/libs/host/CopyFile.c b/libs/host/CopyFile.c
index 855a565..b2a3e7f 100644
--- a/libs/host/CopyFile.c
+++ b/libs/host/CopyFile.c
@@ -24,11 +24,8 @@
 #include <errno.h>
 #include <assert.h>
 
-#ifdef HAVE_MS_C_RUNTIME
-#  define  mkdir(path,mode)   _mkdir(path)
-#endif
-
 #if defined(_WIN32)
+#  define mkdir(path,mode)   _mkdir(path)
 #  define S_ISLNK(s) 0
 #  define lstat stat
 #  ifndef EACCESS   /* seems to be missing from the Mingw headers */
@@ -183,7 +180,7 @@
             DBUG(("---   unable to set perms on '%s' to 0%o: %s\n",
                 dst, pSrcStat->st_mode & ~(S_IFMT), strerror(errno)));
         }
-#ifndef HAVE_MS_C_RUNTIME
+#ifndef _WIN32
         /*
          * Set the owner.
          */
@@ -261,7 +258,7 @@
         /* if "force" is set, try removing the destination file and retry */
         if (options & COPY_FORCE) {
             if (unlink(dst) != 0) {
-#ifdef HAVE_MS_C_RUNTIME
+#ifdef _WIN32
 				/* MSVCRT.DLL unlink will fail with EACCESS if the file is set read-only */
 				/* so try to change its mode, and unlink again                           */
 				if (errno == EACCESS) {
@@ -274,7 +271,7 @@
                 (void) close(srcFd);
                 return -1;
             }
-#ifdef HAVE_MS_C_RUNTIME
+#ifdef _WIN32
         Open_File:
 #endif			
             dstFd = open(dst, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644);
@@ -546,57 +543,6 @@
         statResult = stat(src, &srcStat);
     statErrno = errno;        /* preserve across .exe attempt */
 
-#ifdef WIN32_EXE
-    /*
-     * Here's the interesting part.  Under Cygwin, if you have a file
-     * called "foo.exe", stat("foo", ...) will succeed, but open("foo", ...)
-     * will fail.  We need to figure out what its name is supposed to be
-     * so we can create the correct destination file.
-     *
-     * If we don't have the "-e" flag set, we want "acp foo bar" to fail,
-     * not automatically find "foo.exe".  That way, if we really were
-     * trying to copy "foo", it doesn't grab something we don't want.
-     */
-    if (isCmdLine && statResult == 0) {
-        int tmpFd;
-        tmpFd = open(src, O_RDONLY | O_BINARY, 0);
-        if (tmpFd < 0) {
-            statResult = -1;
-            statErrno = ENOENT;
-        } else {
-            (void) close(tmpFd);
-        }
-    }
-
-    /*
-     * If we didn't find the file, try it again with ".exe".
-     */
-    if (isCmdLine && statResult < 0 && statErrno == ENOENT && (options & COPY_TRY_EXE)) {
-        srcExe = malloc(strlen(src) + 4 +1);
-        strcpy(srcExe, src);
-        strcat(srcExe, ".exe");
-
-        if (options & COPY_NO_DEREFERENCE)
-            statResult = lstat(srcExe, &srcStat);
-        else
-            statResult = stat(srcExe, &srcStat);
-
-        if (statResult == 0 && !S_ISREG(srcStat.st_mode))
-            statResult = -1;        /* fail, use original statErrno below */
-
-        if (statResult == 0) {
-            /* found a .exe, copy that instead */
-            dstExe = malloc(strlen(dst) + 4 +1);
-            strcpy(dstExe, dst);
-            strcat(dstExe, ".exe");
-
-            src = srcExe;
-            dst = dstExe;
-        } else {
-            DBUG(("---  couldn't find '%s' either\n", srcExe));
-        }
-    }
-#endif
     if (statResult < 0) {
         if (statErrno == ENOENT)
             fprintf(stderr, "acp: file '%s' does not exist\n", src);
diff --git a/tools/acp/Android.mk b/tools/acp/Android.mk
index fbdb797..e31ad3a 100644
--- a/tools/acp/Android.mk
+++ b/tools/acp/Android.mk
@@ -9,9 +9,6 @@
 LOCAL_SRC_FILES := \
     acp.c
 
-ifeq ($(HOST_OS),cygwin)
-LOCAL_CFLAGS += -DWIN32_EXE
-endif
 ifeq ($(HOST_OS),darwin)
 LOCAL_CFLAGS += -DMACOSX_RSRC
 endif
diff --git a/tools/buildinfo.sh b/tools/buildinfo.sh
index 1fb203a..910894d 100755
--- a/tools/buildinfo.sh
+++ b/tools/buildinfo.sh
@@ -10,8 +10,8 @@
 echo "ro.build.version.codename=$PLATFORM_VERSION_CODENAME"
 echo "ro.build.version.all_codenames=$PLATFORM_VERSION_ALL_CODENAMES"
 echo "ro.build.version.release=$PLATFORM_VERSION"
-echo "ro.build.date=`date`"
-echo "ro.build.date.utc=`date +%s`"
+echo "ro.build.date=`$DATE`"
+echo "ro.build.date.utc=`$DATE +%s`"
 echo "ro.build.type=$TARGET_BUILD_TYPE"
 echo "ro.build.user=$USER"
 echo "ro.build.host=`hostname`"
diff --git a/tools/releasetools/blockimgdiff.py b/tools/releasetools/blockimgdiff.py
index e042e03..199783d 100644
--- a/tools/releasetools/blockimgdiff.py
+++ b/tools/releasetools/blockimgdiff.py
@@ -174,6 +174,12 @@
     return (sum(sr.size() for (_, sr) in self.stash_before) -
             sum(sr.size() for (_, sr) in self.use_stash))
 
+  def ConvertToNew(self):
+    assert self.style != "new"
+    self.use_stash = []
+    self.style = "new"
+    self.src_ranges = RangeSet()
+
   def __str__(self):
     return (str(self.id) + ": <" + str(self.src_ranges) + " " + self.style +
             " to " + str(self.tgt_ranges) + ">")
@@ -268,6 +274,10 @@
       self.ReverseBackwardEdges()
       self.ImproveVertexSequence()
 
+    # Ensure the runtime stash size is under the limit.
+    if self.version >= 2 and common.OPTIONS.cache_size is not None:
+      self.ReviseStashSize()
+
     # Double-check our work.
     self.AssertSequenceGood()
 
@@ -519,6 +529,73 @@
         print("max stashed blocks: %d  (%d bytes), limit: <unknown>\n" % (
               max_stashed_blocks, max_stashed_size))
 
+  def ReviseStashSize(self):
+    print("Revising stash size...")
+    stashes = {}
+
+    # Create the map between a stash and its def/use points. For example, for a
+    # given stash of (idx, sr), stashes[idx] = (sr, def_cmd, use_cmd).
+    for xf in self.transfers:
+      # Command xf defines (stores) all the stashes in stash_before.
+      for idx, sr in xf.stash_before:
+        stashes[idx] = (sr, xf)
+
+      # Record all the stashes command xf uses.
+      for idx, _ in xf.use_stash:
+        stashes[idx] += (xf,)
+
+    # Compute the maximum blocks available for stash based on /cache size and
+    # the threshold.
+    cache_size = common.OPTIONS.cache_size
+    stash_threshold = common.OPTIONS.stash_threshold
+    max_allowed = cache_size * stash_threshold / self.tgt.blocksize
+
+    stashed_blocks = 0
+
+    # Now go through all the commands. Compute the required stash size on the
+    # fly. If a command requires excess stash than available, it deletes the
+    # stash by replacing the command that uses the stash with a "new" command
+    # instead.
+    for xf in self.transfers:
+      replaced_cmds = []
+
+      # xf.stash_before generates explicit stash commands.
+      for idx, sr in xf.stash_before:
+        if stashed_blocks + sr.size() > max_allowed:
+          # We cannot stash this one for a later command. Find out the command
+          # that will use this stash and replace the command with "new".
+          use_cmd = stashes[idx][2]
+          replaced_cmds.append(use_cmd)
+          print("  %s replaced due to an explicit stash of %d blocks." % (
+              use_cmd, sr.size()))
+        else:
+          stashed_blocks += sr.size()
+
+      # xf.use_stash generates free commands.
+      for _, sr in xf.use_stash:
+        stashed_blocks -= sr.size()
+
+      # "move" and "diff" may introduce implicit stashes in BBOTA v3. Prior to
+      # ComputePatches(), they both have the style of "diff".
+      if xf.style == "diff" and self.version >= 3:
+        assert xf.tgt_ranges and xf.src_ranges
+        if xf.src_ranges.overlaps(xf.tgt_ranges):
+          if stashed_blocks + xf.src_ranges.size() > max_allowed:
+            replaced_cmds.append(xf)
+            print("  %s replaced due to an implicit stash of %d blocks." % (
+                xf, xf.src_ranges.size()))
+
+      # Replace the commands in replaced_cmds with "new"s.
+      for cmd in replaced_cmds:
+        # It no longer uses any commands in "use_stash". Remove the def points
+        # for all those stashes.
+        for idx, sr in cmd.use_stash:
+          def_cmd = stashes[idx][1]
+          assert (idx, sr) in def_cmd.stash_before
+          def_cmd.stash_before.remove((idx, sr))
+
+        cmd.ConvertToNew()
+
   def ComputePatches(self, prefix):
     print("Reticulating splines...")
     diff_q = []
diff --git a/tools/releasetools/ota_from_target_files.py b/tools/releasetools/ota_from_target_files.py
index 9ab8cb3..c074f10 100755
--- a/tools/releasetools/ota_from_target_files.py
+++ b/tools/releasetools/ota_from_target_files.py
@@ -42,6 +42,10 @@
       radio image. This option is only meaningful when -i is specified,
       because a full radio is always included in a full OTA if applicable.
 
+  --full_bootloader
+      Similar to --full_radio. When generating an incremental OTA, always
+      include a full copy of bootloader image.
+
   -v  (--verify)
       Remount and verify the checksums of the files written to the
       system and vendor (if used) partitions.  Incremental builds only.
@@ -125,6 +129,7 @@
 OPTIONS.oem_source = None
 OPTIONS.fallback_to_full = True
 OPTIONS.full_radio = False
+OPTIONS.full_bootloader = False
 
 
 def MostPopularKey(d, default):
@@ -1479,6 +1484,8 @@
       OPTIONS.incremental_source = a
     elif o == "--full_radio":
       OPTIONS.full_radio = True
+    elif o == "--full_bootloader":
+      OPTIONS.full_bootloader = True
     elif o in ("-w", "--wipe_user_data"):
       OPTIONS.wipe_user_data = True
     elif o in ("-n", "--no_prereq"):
@@ -1527,6 +1534,7 @@
                                  "package_key=",
                                  "incremental_from=",
                                  "full_radio",
+                                 "full_bootloader",
                                  "wipe_user_data",
                                  "no_prereq",
                                  "extra_script=",