Merge "More helpful diagnostics when selecting a Mac SDK."
diff --git a/core/binary.mk b/core/binary.mk
index 445b9d5..ebb40c8 100644
--- a/core/binary.mk
+++ b/core/binary.mk
@@ -217,11 +217,13 @@
         my_clang := true
     endif
     endif
-# Add option to make clang the default for device build
-else ifeq ($(USE_CLANG_PLATFORM_BUILD),true)
+# Add option to make gcc the default for device build
+else ifeq ($(USE_CLANG_PLATFORM_BUILD),false)
     ifeq ($(my_clang),)
-        my_clang := true
+        my_clang := false
     endif
+else ifeq ($(my_clang),)
+    my_clang := true
 endif
 
 my_cpp_std_version := -std=gnu++14
diff --git a/core/config.mk b/core/config.mk
index 1a65894..26c354d 100644
--- a/core/config.mk
+++ b/core/config.mk
@@ -236,7 +236,7 @@
 # Commands to generate .toc file common to ELF .so files.
 define _gen_toc_command_for_elf
 $(hide) ($($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)READELF) -d $(1) | grep SONAME || echo "No SONAME for $1") > $(2)
-$(hide) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)NM) -gD -f p $(1) | cut -f1-2 -d" " >> $(2)
+$(hide) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)READELF) --dyn-syms $(1) | awk '{$$2=""; $$3=""; print}' >> $(2)
 endef
 
 # Commands to generate .toc file from Darwin dynamic library.
diff --git a/core/soong.mk b/core/soong.mk
index c464ba8..aab26de 100644
--- a/core/soong.mk
+++ b/core/soong.mk
@@ -23,9 +23,8 @@
 	$(hide) mkdir -p $(dir $@)
 	$(hide) (\
 	echo '{'; \
-	echo '    "Device_uses_jemalloc": $(if $(filter dlmalloc,$(MALLOC_IMPL)),false,true),'; \
-	echo '    "Device_uses_dlmalloc": $(if $(filter dlmalloc,$(MALLOC_IMPL)),true,false),'; \
-	echo '    $(if $(BOARD_MALLOC_ALIGNMENT),"Dlmalloc_alignment": $(BOARD_MALLOC_ALIGNMENT)$(comma),)'; \
+	echo '    "Device_uses_jemalloc": $(if $(filter true,$(MALLOC_SVELTE)),false,true),'; \
+	echo '    "Device_uses_dlmalloc": $(if $(filter true,$(MALLOC_SVELTE)),true,false),'; \
 	echo '    "Platform_sdk_version": $(PLATFORM_SDK_VERSION),'; \
 	echo '    "Unbundled_build": $(if $(TARGET_BUILD_APPS),true,false),'; \
 	echo '    "Brillo": $(if $(BRILLO),true,false),'; \
diff --git a/core/version_defaults.mk b/core/version_defaults.mk
index 0596fc8..6ffb752 100644
--- a/core/version_defaults.mk
+++ b/core/version_defaults.mk
@@ -82,7 +82,7 @@
     # assuming the device can only support APIs as of the previous official
     # public release.
     # This value will always be 0 for release builds.
-    PLATFORM_PREVIEW_SDK_VERSION := 0
+    PLATFORM_PREVIEW_SDK_VERSION := 1
   endif
 endif
 
diff --git a/libs/host/Android.mk b/libs/host/Android.mk
index 8a28457..bc25e4b 100644
--- a/libs/host/Android.mk
+++ b/libs/host/Android.mk
@@ -5,11 +5,11 @@
     CopyFile.c
 
 LOCAL_CFLAGS := -Werror -Wall
-LOCAL_CFLAGS_darwin += -DMACOSX_RSRC
 
 LOCAL_MODULE:= libhost
 LOCAL_MODULE_HOST_OS := darwin linux windows
 LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
 LOCAL_CXX_STL := none
 
 # acp uses libhost, so we can't use
diff --git a/libs/host/CopyFile.c b/libs/host/CopyFile.c
index d785dca..5be012c 100644
--- a/libs/host/CopyFile.c
+++ b/libs/host/CopyFile.c
@@ -67,9 +67,9 @@
  */
 static bool isHiresMtime(const struct stat* pSrcStat)
 {
-#if defined(__CYGWIN__) || defined(__MINGW32__)
-  return 0;
-#elif defined(MACOSX_RSRC)
+#if defined(_WIN32)
+    return 0;
+#elif defined(__APPLE__)
     return pSrcStat->st_mtimespec.tv_nsec > 0;
 #else
     return pSrcStat->st_mtim.tv_nsec > 0;
@@ -83,8 +83,7 @@
  */
 static bool isSameFile(const struct stat* pSrcStat, const struct stat* pDstStat)
 {
-#ifndef HAVE_VALID_STAT_ST_INO
-  /* TODO: suspicious, we hit this case even when compiling for linux: b/26355387 */
+#if defined(_WIN32)
   (void)pSrcStat;
   (void)pDstStat;
     /* with MSVCRT.DLL, stat always sets st_ino to 0, and there is no simple way to */
@@ -296,7 +295,8 @@
     if (copyResult != 0)
         return -1;
 
-#ifdef MACOSX_RSRC
+#if defined(__APPLE__)
+    // Copy Mac OS X resource forks too.
     {
         char* srcRsrcName = NULL;
         char* dstRsrcName = NULL;
diff --git a/target/board/generic_x86/BoardConfig.mk b/target/board/generic_x86/BoardConfig.mk
index 419c3f6..8506724 100644
--- a/target/board/generic_x86/BoardConfig.mk
+++ b/target/board/generic_x86/BoardConfig.mk
@@ -18,9 +18,6 @@
 # no hardware camera
 USE_CAMERA_STUB := true
 
-# customize the malloced address to be 16-byte aligned
-BOARD_MALLOC_ALIGNMENT := 16
-
 # Enable dex-preoptimization to speed up the first boot sequence
 # of an SDK AVD. Note that this operation only works on Linux for now
 ifeq ($(HOST_OS),linux)
diff --git a/target/board/generic_x86_64/BoardConfig.mk b/target/board/generic_x86_64/BoardConfig.mk
index de2f33d..fd06e70 100755
--- a/target/board/generic_x86_64/BoardConfig.mk
+++ b/target/board/generic_x86_64/BoardConfig.mk
@@ -24,9 +24,6 @@
 # no hardware camera
 USE_CAMERA_STUB := true
 
-# customize the malloced address to be 16-byte aligned
-BOARD_MALLOC_ALIGNMENT := 16
-
 # Enable dex-preoptimization to speed up the first boot sequence
 # of an SDK AVD. Note that this operation only works on Linux for now
 ifeq ($(HOST_OS),linux)
diff --git a/tools/acp/Android.mk b/tools/acp/Android.mk
index e31ad3a..eec9c9d 100644
--- a/tools/acp/Android.mk
+++ b/tools/acp/Android.mk
@@ -3,20 +3,13 @@
 # Custom version of cp.
 
 LOCAL_PATH:= $(call my-dir)
+
 include $(CLEAR_VARS)
-LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 
 LOCAL_SRC_FILES := \
     acp.c
 
-ifeq ($(HOST_OS),darwin)
-LOCAL_CFLAGS += -DMACOSX_RSRC
-endif
-ifeq ($(HOST_OS),linux)
-endif
-
 LOCAL_STATIC_LIBRARIES := libhost
-LOCAL_C_INCLUDES := build/libs/host/include
 LOCAL_MODULE := acp
 LOCAL_ACP_UNAVAILABLE := true
 LOCAL_CXX_STL := none
diff --git a/tools/atree/Android.mk b/tools/atree/Android.mk
index d895810..f598db5 100644
--- a/tools/atree/Android.mk
+++ b/tools/atree/Android.mk
@@ -12,7 +12,6 @@
 
 LOCAL_STATIC_LIBRARIES := \
 	libhost
-LOCAL_C_INCLUDES := build/libs/host/include
 
 LOCAL_MODULE := atree
 
diff --git a/tools/droiddoc/templates-sac/assets/js/docs.js b/tools/droiddoc/templates-sac/assets/js/docs.js
index 195c9e5..d0c12a8 100644
--- a/tools/droiddoc/templates-sac/assets/js/docs.js
+++ b/tools/droiddoc/templates-sac/assets/js/docs.js
@@ -2015,10 +2015,7 @@
 });
 
 function updateResultTitle(query) {
-  $("#searchTitle").html("Results for <em>" + (query) + "</em>");
-// For some reason, the escapeHTML function wasn't working for me.  TODO fix
-// this by copying in a comparable library function.
-//  $("#searchTitle").html("Results for <em>" + escapeHTML(query) + "</em>");
+  $("#searchTitle").html("Results for <em>" + encodeURIComponent(query) + "</em>");
 }
 
 // forcefully regain key-up event control (previously jacked by search api)
@@ -2055,13 +2052,6 @@
   return queryParts[1];
 }
 
-/* returns the given string with all HTML brackets converted to entities
-    TODO: move this to the site's JS library */
-function escapeHTML(string) {
-  return string.replace(/</g,"&lt;")
-                .replace(/>/g,"&gt;");
-}
-
 
 /* ######################################################## */
 /* #################  JAVADOC REFERENCE ################### */
diff --git a/tools/releasetools/common.py b/tools/releasetools/common.py
index aaf51e9..95aeb62 100644
--- a/tools/releasetools/common.py
+++ b/tools/releasetools/common.py
@@ -1241,9 +1241,6 @@
     self.partition = partition
     self.check_first_block = check_first_block
 
-    # Due to http://b/20939131, check_first_block is disabled temporarily.
-    assert not self.check_first_block
-
     if version is None:
       version = 1
       if OPTIONS.info_dict:
@@ -1307,14 +1304,8 @@
         script.AppendExtra(('if (range_sha1("%s", "%s") == "%s" || '
                             'block_image_verify("%s", '
                             'package_extract_file("%s.transfer.list"), '
-                            '"%s.new.dat", "%s.patch.dat") || '
-                            '(block_image_recover("%s", "%s") && '
-                            'block_image_verify("%s", '
-                            'package_extract_file("%s.transfer.list"), '
-                            '"%s.new.dat", "%s.patch.dat"))) then') % (
+                            '"%s.new.dat", "%s.patch.dat")) then') % (
                             self.device, ranges_str, self.src.TotalSha1(),
-                            self.device, partition, partition, partition,
-                            self.device, ranges_str,
                             self.device, partition, partition, partition))
       elif self.version == 3:
         script.AppendExtra(('if (range_sha1("%s", "%s") == "%s" || '
@@ -1329,22 +1320,36 @@
       script.Print('Verified %s image...' % (partition,))
       script.AppendExtra('else')
 
-      # When generating incrementals for the system and vendor partitions,
-      # explicitly check the first block (which contains the superblock) of
-      # the partition to see if it's what we expect. If this check fails,
-      # give an explicit log message about the partition having been
-      # remounted R/W (the most likely explanation) and the need to flash to
-      # get OTAs working again.
-      if self.check_first_block:
-        self._CheckFirstBlock(script)
+      if self.version >= 4:
+
+        # Bug: 21124327
+        # When generating incrementals for the system and vendor partitions in
+        # version 4 or newer, explicitly check the first block (which contains
+        # the superblock) of the partition to see if it's what we expect. If
+        # this check fails, give an explicit log message about the partition
+        # having been remounted R/W (the most likely explanation).
+        if self.check_first_block:
+          script.AppendExtra('check_first_block("%s");' % (self.device,))
+
+        # If version >= 4, try block recovery before abort update
+        script.AppendExtra((
+            'ifelse (block_image_recover("{device}", "{ranges}") && '
+            'block_image_verify("{device}", '
+            'package_extract_file("{partition}.transfer.list"), '
+            '"{partition}.new.dat", "{partition}.patch.dat"), '
+            'ui_print("{partition} recovered successfully."), '
+            'abort("{partition} partition fails to recover"));\n'
+            'endif;').format(device=self.device, ranges=ranges_str,
+                             partition=partition))
 
       # Abort the OTA update. Note that the incremental OTA cannot be applied
       # even if it may match the checksum of the target partition.
       # a) If version < 3, operations like move and erase will make changes
       #    unconditionally and damage the partition.
       # b) If version >= 3, it won't even reach here.
-      script.AppendExtra(('abort("%s partition has unexpected contents");\n'
-                          'endif;') % (partition,))
+      else:
+        script.AppendExtra(('abort("%s partition has unexpected contents");\n'
+                            'endif;') % (partition,))
 
   def _WritePostInstallVerifyScript(self, script):
     partition = self.partition
@@ -1413,22 +1418,9 @@
 
     return ctx.hexdigest()
 
-  # TODO(tbao): Due to http://b/20939131, block 0 may be changed without
-  # remounting R/W. Will change the checking to a finer-grained way to
-  # mask off those bits.
-  def _CheckFirstBlock(self, script):
-    r = rangelib.RangeSet((0, 1))
-    srchash = self._HashBlocks(self.src, r)
-
-    script.AppendExtra(('(range_sha1("%s", "%s") == "%s") || '
-                        'abort("%s has been remounted R/W; '
-                        'reflash device to reenable OTA updates");')
-                       % (self.device, r.to_string_raw(), srchash,
-                          self.device))
 
 DataImage = blockimgdiff.DataImage
 
-
 # map recovery.fstab's fs_types to mount/format "partition types"
 PARTITION_TYPES = {
     "yaffs2": "MTD",
diff --git a/tools/releasetools/ota_from_target_files.py b/tools/releasetools/ota_from_target_files.py
index a76eaa9..d0636b6 100755
--- a/tools/releasetools/ota_from_target_files.py
+++ b/tools/releasetools/ota_from_target_files.py
@@ -811,7 +811,12 @@
         int(i) for i in
         OPTIONS.info_dict.get("blockimgdiff_versions", "1").split(","))
 
+  # Check first block of system partition for remount R/W only if
+  # disk type is ext4
+  system_partition = OPTIONS.source_info_dict["fstab"]["/system"]
+  check_first_block = system_partition.fs_type=="ext4"
   system_diff = common.BlockDifference("system", system_tgt, system_src,
+                                       check_first_block,
                                        version=blockimgdiff_version)
 
   if HasVendorPartition(target_zip):
@@ -821,7 +826,13 @@
                           OPTIONS.source_info_dict)
     vendor_tgt = GetImage("vendor", OPTIONS.target_tmp,
                           OPTIONS.target_info_dict)
+
+    # Check first block of vendor partition for remount R/W only if
+    # disk type is ext4
+    vendor_partition = OPTIONS.source_info_dict["fstab"]["/vendor"]
+    check_first_block = vendor_partition.fs_type=="ext4"
     vendor_diff = common.BlockDifference("vendor", vendor_tgt, vendor_src,
+                                         check_first_block,
                                          version=blockimgdiff_version)
   else:
     vendor_diff = None