Merge "Enable R8 by default (third attempt)"
diff --git a/core/Makefile b/core/Makefile
index 20d14d2..85ae96e 100644
--- a/core/Makefile
+++ b/core/Makefile
@@ -841,7 +841,8 @@
 tools_notice_file_txt := $(HOST_OUT_INTERMEDIATES)/NOTICE.txt
 tools_notice_file_html := $(HOST_OUT_INTERMEDIATES)/NOTICE.html
 
-ifeq ($(PRODUCT_FULL_TREBLE),true)
+# TODO(b/69865032): Make PRODUCT_NOTICE_SPLIT the default behavior.
+ifeq ($(PRODUCT_NOTICE_SPLIT),true)
 target_notice_file_html_or_xml := $(TARGET_OUT_INTERMEDIATES)/NOTICE.xml
 target_notice_file_html_or_xml_gz := $(TARGET_OUT_INTERMEDIATES)/NOTICE.xml.gz
 installed_notice_html_or_xml_gz := $(TARGET_OUT)/etc/NOTICE.xml.gz
@@ -1043,7 +1044,6 @@
 $(if $(BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE),$(hide) echo "system_fs_type=$(BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE)" >> $(1))
 $(if $(BOARD_SYSTEMIMAGE_EXTFS_INODE_COUNT),$(hide) echo "system_extfs_inode_count=$(BOARD_SYSTEMIMAGE_EXTFS_INODE_COUNT)" >> $(1))
 $(if $(BOARD_SYSTEMIMAGE_JOURNAL_SIZE),$(hide) echo "system_journal_size=$(BOARD_SYSTEMIMAGE_JOURNAL_SIZE)" >> $(1))
-$(if $(BOARD_HAS_EXT4_RESERVED_BLOCKS),$(hide) echo "has_ext4_reserved_blocks=$(BOARD_HAS_EXT4_RESERVED_BLOCKS)" >> $(1))
 $(if $(BOARD_SYSTEMIMAGE_SQUASHFS_COMPRESSOR),$(hide) echo "system_squashfs_compressor=$(BOARD_SYSTEMIMAGE_SQUASHFS_COMPRESSOR)" >> $(1))
 $(if $(BOARD_SYSTEMIMAGE_SQUASHFS_COMPRESSOR_OPT),$(hide) echo "system_squashfs_compressor_opt=$(BOARD_SYSTEMIMAGE_SQUASHFS_COMPRESSOR_OPT)" >> $(1))
 $(if $(BOARD_SYSTEMIMAGE_SQUASHFS_BLOCK_SIZE),$(hide) echo "system_squashfs_block_size=$(BOARD_SYSTEMIMAGE_SQUASHFS_BLOCK_SIZE)" >> $(1))
@@ -1456,9 +1456,6 @@
            du -sm $(TARGET_OUT) 1>&2;\
            if [ "$(INTERNAL_USERIMAGES_EXT_VARIANT)" == "ext4" ]; then \
                maxsize=$(BOARD_SYSTEMIMAGE_PARTITION_SIZE); \
-               if [ "$(BOARD_HAS_EXT4_RESERVED_BLOCKS)" == "true" ]; then \
-                   maxsize=$$((maxsize - 4096 * 4096)); \
-               fi; \
                echo "The max is $$(( maxsize / 1048576 )) MB." 1>&2 ;\
            else \
                echo "The max is $$(( $(BOARD_SYSTEMIMAGE_PARTITION_SIZE) / 1048576 )) MB." 1>&2 ;\
@@ -2174,6 +2171,7 @@
   $(HOST_OUT_EXECUTABLES)/simg2img \
   $(HOST_OUT_EXECUTABLES)/e2fsck \
   $(HOST_OUT_EXECUTABLES)/build_verity_tree \
+  $(HOST_OUT_EXECUTABLES)/generate_verity_key \
   $(HOST_OUT_EXECUTABLES)/verity_signer \
   $(HOST_OUT_EXECUTABLES)/verity_verifier \
   $(HOST_OUT_EXECUTABLES)/append2simg \
@@ -2199,6 +2197,7 @@
   $(HOST_LIBRARY_PATH)/liblog$(HOST_SHLIB_SUFFIX) \
   $(HOST_LIBRARY_PATH)/libcutils$(HOST_SHLIB_SUFFIX) \
   $(HOST_LIBRARY_PATH)/libselinux$(HOST_SHLIB_SUFFIX) \
+  $(HOST_LIBRARY_PATH)/libcrypto_utils$(HOST_SHLIB_SUFFIX) \
   $(HOST_LIBRARY_PATH)/libcrypto-host$(HOST_SHLIB_SUFFIX) \
   $(HOST_LIBRARY_PATH)/libext2fs-host$(HOST_SHLIB_SUFFIX) \
   $(HOST_LIBRARY_PATH)/libext2_blkid-host$(HOST_SHLIB_SUFFIX) \
@@ -2480,9 +2479,6 @@
 ifdef BOARD_RECOVERYIMAGE_PARTITION_SIZE
 	$(hide) echo "recovery_size=$(BOARD_RECOVERYIMAGE_PARTITION_SIZE)" >> $(zip_root)/META/misc_info.txt
 endif
-ifdef BOARD_HAS_EXT4_RESERVED_BLOCKS
-	$(hide) echo "has_ext4_reserved_blocks=$(BOARD_HAS_EXT4_RESERVED_BLOCKS)" >> $(zip_root)/META/misc_info.txt
-endif
 ifdef TARGET_RECOVERY_FSTYPE_MOUNT_OPTIONS
 	@# TARGET_RECOVERY_FSTYPE_MOUNT_OPTIONS can be empty to indicate that nothing but defaults should be used.
 	$(hide) echo "recovery_mount_options=$(TARGET_RECOVERY_FSTYPE_MOUNT_OPTIONS)" >> $(zip_root)/META/misc_info.txt
diff --git a/core/config.mk b/core/config.mk
index c54d119..15336ad 100644
--- a/core/config.mk
+++ b/core/config.mk
@@ -748,10 +748,17 @@
   PRODUCT_FULL_TREBLE := true
 endif
 
+# TODO(b/69865032): Make PRODUCT_NOTICE_SPLIT the default behavior and remove
+#    references to it here and below.
+ifdef PRODUCT_NOTICE_SPLIT_OVERRIDE
+   $(error PRODUCT_NOTICE_SPLIT_OVERRIDE cannot be set.)
+endif
+
 requirements := \
     PRODUCT_TREBLE_LINKER_NAMESPACES \
     PRODUCT_SEPOLICY_SPLIT \
     PRODUCT_ENFORCE_VINTF_MANIFEST \
+    PRODUCT_NOTICE_SPLIT
 
 # If it is overriden, then the requirement override is taken, otherwise it's
 # PRODUCT_FULL_TREBLE
diff --git a/core/main.mk b/core/main.mk
index 621d309..fe178da 100644
--- a/core/main.mk
+++ b/core/main.mk
@@ -126,6 +126,8 @@
 $(error ADDITIONAL_BUILD_PROPERTIES must not be set before here: $(ADDITIONAL_BUILD_PROPERTIES))
 endif
 
+ADDITIONAL_BUILD_PROPERTIES :=
+
 #
 # -----------------------------------------------------------------
 # Add the product-defined properties to the build properties.
@@ -198,6 +200,11 @@
 
 ADDITIONAL_BUILD_PROPERTIES += ro.treble.enabled=${PRODUCT_FULL_TREBLE}
 
+$(KATI_obsolete_var PRODUCT_FULL_TREBLE,\
+	Code should be written to work regardless of a device being Treble or \
+	variables like PRODUCT_SEPOLICY_SPLIT should be used until that is \
+	possible.)
+
 # -----------------------------------------------------------------
 ###
 ### In this section we set up the things that are different
diff --git a/tools/releasetools/build_image.py b/tools/releasetools/build_image.py
index e4e4138..2a92d86 100755
--- a/tools/releasetools/build_image.py
+++ b/tools/releasetools/build_image.py
@@ -376,6 +376,40 @@
     return None
   return base_fs_file
 
+
+def CheckHeadroom(ext4fs_output, prop_dict):
+  """Checks if there's enough headroom space available.
+
+  Headroom is the reserved space on system image (via PRODUCT_SYSTEM_HEADROOM),
+  which is useful for devices with low disk space that have system image
+  variation between builds. The 'partition_headroom' in prop_dict is the size
+  in bytes, while the numbers in 'ext4fs_output' are for 4K-blocks.
+
+  Args:
+    ext4fs_output: The output string from mke2fs command.
+    prop_dict: The property dict.
+
+  Returns:
+    The check result.
+  """
+  ext4fs_stats = re.compile(
+      r'Created filesystem with .* (?P<used_blocks>[0-9]+)/'
+      r'(?P<total_blocks>[0-9]+) blocks')
+  m = ext4fs_stats.match(ext4fs_output.strip().split('\n')[-1])
+  used_blocks = int(m.groupdict().get('used_blocks'))
+  total_blocks = int(m.groupdict().get('total_blocks'))
+  headroom_blocks = int(prop_dict.get('partition_headroom')) / BLOCK_SIZE
+  adjusted_blocks = total_blocks - headroom_blocks
+  if used_blocks > adjusted_blocks:
+    mount_point = prop_dict.get("mount_point")
+    print("Error: Not enough room on %s (total: %d blocks, used: %d blocks, "
+          "headroom: %d blocks, available: %d blocks)" % (
+              mount_point, total_blocks, used_blocks, headroom_blocks,
+              adjusted_blocks))
+    return False
+  return True
+
+
 def BuildImage(in_dir, prop_dict, out_file, target_out=None):
   """Build an image to out_file from in_dir with property prop_dict.
 
@@ -542,9 +576,7 @@
     shutil.rmtree(staging_system, ignore_errors=True)
     shutil.copytree(origin_in, staging_system, symlinks=True)
 
-  has_reserved_blocks = prop_dict.get("has_ext4_reserved_blocks") == "true"
   ext4fs_output = None
-
   try:
     if fs_type.startswith("ext4"):
       (ext4fs_output, exit_code) = RunCommand(build_command)
@@ -562,37 +594,10 @@
     print("Error: '%s' failed with exit code %d" % (build_command, exit_code))
     return False
 
-  # Bug: 21522719, 22023465
-  # There are some reserved blocks on ext4 FS (lesser of 4096 blocks and 2%).
-  # We need to deduct those blocks from the available space, since they are
-  # not writable even with root privilege. It only affects devices using
-  # file-based OTA and a kernel version of 3.10 or greater (currently just
-  # sprout).
-  # Separately, check if there's enough headroom space available. This is useful for
-  # devices with low disk space that have system image variation between builds.
-  if (has_reserved_blocks or "partition_headroom" in prop_dict) and fs_type.startswith("ext4"):
+  # Check if there's enough headroom space available for ext4 image.
+  if "partition_headroom" in prop_dict and fs_type.startswith("ext4"):
     assert ext4fs_output is not None
-    ext4fs_stats = re.compile(
-        r'Created filesystem with .* (?P<used_blocks>[0-9]+)/'
-        r'(?P<total_blocks>[0-9]+) blocks')
-    m = ext4fs_stats.match(ext4fs_output.strip().split('\n')[-1])
-    used_blocks = int(m.groupdict().get('used_blocks'))
-    total_blocks = int(m.groupdict().get('total_blocks'))
-    reserved_blocks = 0
-    headroom_blocks = 0
-    adjusted_blocks = total_blocks
-    if has_reserved_blocks:
-      reserved_blocks = min(4096, int(total_blocks * 0.02))
-      adjusted_blocks -= reserved_blocks
-    if "partition_headroom" in prop_dict:
-      headroom_blocks = int(prop_dict.get('partition_headroom')) / BLOCK_SIZE
-      adjusted_blocks -= headroom_blocks
-    if used_blocks > adjusted_blocks:
-      mount_point = prop_dict.get("mount_point")
-      print("Error: Not enough room on %s (total: %d blocks, used: %d blocks, "
-            "reserved: %d blocks, headroom: %d blocks, available: %d blocks)" % (
-                mount_point, total_blocks, used_blocks, reserved_blocks,
-                headroom_blocks, adjusted_blocks))
+    if not CheckHeadroom(ext4fs_output, prop_dict):
       return False
 
   if not fs_spans_partition:
@@ -698,7 +703,6 @@
     copy_prop("system_root_image", "system_root_image")
     copy_prop("ramdisk_dir", "ramdisk_dir")
     copy_prop("ramdisk_fs_config", "ramdisk_fs_config")
-    copy_prop("has_ext4_reserved_blocks", "has_ext4_reserved_blocks")
     copy_prop("system_squashfs_compressor", "squashfs_compressor")
     copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt")
     copy_prop("system_squashfs_block_size", "squashfs_block_size")
@@ -718,7 +722,6 @@
     copy_prop("system_size", "partition_size")
     copy_prop("system_journal_size", "journal_size")
     copy_prop("system_verity_block_device", "verity_block_device")
-    copy_prop("has_ext4_reserved_blocks", "has_ext4_reserved_blocks")
     copy_prop("system_squashfs_compressor", "squashfs_compressor")
     copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt")
     copy_prop("system_squashfs_block_size", "squashfs_block_size")
@@ -744,7 +747,6 @@
     copy_prop("vendor_size", "partition_size")
     copy_prop("vendor_journal_size", "journal_size")
     copy_prop("vendor_verity_block_device", "verity_block_device")
-    copy_prop("has_ext4_reserved_blocks", "has_ext4_reserved_blocks")
     copy_prop("vendor_squashfs_compressor", "squashfs_compressor")
     copy_prop("vendor_squashfs_compressor_opt", "squashfs_compressor_opt")
     copy_prop("vendor_squashfs_block_size", "squashfs_block_size")
@@ -755,7 +757,6 @@
     copy_prop("fs_type", "fs_type")
     copy_prop("oem_size", "partition_size")
     copy_prop("oem_journal_size", "journal_size")
-    copy_prop("has_ext4_reserved_blocks", "has_ext4_reserved_blocks")
     copy_prop("oem_extfs_inode_count", "extfs_inode_count")
   d["partition_name"] = mount_point
   return d
diff --git a/tools/releasetools/sign_target_files_apks.py b/tools/releasetools/sign_target_files_apks.py
index f559b29..7bfc04b 100755
--- a/tools/releasetools/sign_target_files_apks.py
+++ b/tools/releasetools/sign_target_files_apks.py
@@ -278,7 +278,7 @@
       if stat.S_ISLNK(info.external_attr >> 16):
         new_data = data
       else:
-        new_data = RewriteProps(data, misc_info)
+        new_data = RewriteProps(data)
       common.ZipWriteStr(output_tf_zip, out_info, new_data)
 
     elif info.filename.endswith("mac_permissions.xml"):
@@ -385,8 +385,14 @@
 
 
 def EditTags(tags):
-  """Given a string containing comma-separated tags, apply the edits
-  specified in OPTIONS.tag_changes and return the updated string."""
+  """Applies the edits to the tag string as specified in OPTIONS.tag_changes.
+
+  Args:
+    tags: The input string that contains comma-separated tags.
+
+  Returns:
+    The updated tags (comma-separated and sorted).
+  """
   tags = set(tags.split(","))
   for ch in OPTIONS.tag_changes:
     if ch[0] == "-":
@@ -396,20 +402,27 @@
   return ",".join(sorted(tags))
 
 
-def RewriteProps(data, misc_info):
+def RewriteProps(data):
+  """Rewrites the system properties in the given string.
+
+  Each property is expected in 'key=value' format. The properties that contain
+  build tags (i.e. test-keys, dev-keys) will be updated accordingly by calling
+  EditTags().
+
+  Args:
+    data: Input string, separated by newlines.
+
+  Returns:
+    The string with modified properties.
+  """
   output = []
   for line in data.split("\n"):
     line = line.strip()
     original_line = line
     if line and line[0] != '#' and "=" in line:
       key, value = line.split("=", 1)
-      if (key in ("ro.build.fingerprint", "ro.vendor.build.fingerprint")
-          and misc_info.get("oem_fingerprint_properties") is None):
-        pieces = value.split("/")
-        pieces[-1] = EditTags(pieces[-1])
-        value = "/".join(pieces)
-      elif (key in ("ro.build.thumbprint", "ro.vendor.build.thumbprint")
-            and misc_info.get("oem_fingerprint_properties") is not None):
+      if key in ("ro.build.fingerprint", "ro.build.thumbprint",
+                 "ro.vendor.build.fingerprint", "ro.vendor.build.thumbprint"):
         pieces = value.split("/")
         pieces[-1] = EditTags(pieces[-1])
         value = "/".join(pieces)
diff --git a/tools/releasetools/test_build_image.py b/tools/releasetools/test_build_image.py
new file mode 100644
index 0000000..6566a5a
--- /dev/null
+++ b/tools/releasetools/test_build_image.py
@@ -0,0 +1,65 @@
+#
+# Copyright (C) 2017 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import shutil
+import tempfile
+import unittest
+
+from build_image import CheckHeadroom, RunCommand
+
+
+class BuildImageTest(unittest.TestCase):
+
+  def test_CheckHeadroom_SizeUnderLimit(self):
+    ext4fs_output = ("Created filesystem with 2777/129024 inodes and "
+                     "508140/516099 blocks")
+    prop_dict = {
+        'partition_headroom' : '4194304',
+        'mount_point' : 'system',
+    }
+    self.assertTrue(CheckHeadroom(ext4fs_output, prop_dict))
+
+  def test_CheckHeadroom_InsufficientHeadroom(self):
+    ext4fs_output = ("Created filesystem with 2777/129024 inodes and "
+                     "515099/516099 blocks")
+    prop_dict = {
+        'partition_headroom' : '4100096',
+        'mount_point' : 'system',
+    }
+    self.assertFalse(CheckHeadroom(ext4fs_output, prop_dict))
+
+  def test_CheckHeadroom_WithMke2fsOutput(self):
+    """Tests the result parsing from actual call to mke2fs."""
+    input_dir = tempfile.mkdtemp()
+    output_image = tempfile.NamedTemporaryFile(suffix='.img')
+    command = ['mkuserimg_mke2fs.sh', input_dir, output_image.name, 'ext4',
+               '/system', '409600', '-j', '0']
+    ext4fs_output, exit_code = RunCommand(command)
+    self.assertEqual(0, exit_code)
+
+    prop_dict = {
+        'partition_headroom' : '40960',
+        'mount_point' : 'system',
+    }
+    self.assertTrue(CheckHeadroom(ext4fs_output, prop_dict))
+
+    prop_dict = {
+        'partition_headroom' : '413696',
+        'mount_point' : 'system',
+    }
+    self.assertFalse(CheckHeadroom(ext4fs_output, prop_dict))
+
+    shutil.rmtree(input_dir)
diff --git a/tools/releasetools/test_sign_target_files_apks.py b/tools/releasetools/test_sign_target_files_apks.py
new file mode 100644
index 0000000..90afdc7
--- /dev/null
+++ b/tools/releasetools/test_sign_target_files_apks.py
@@ -0,0 +1,67 @@
+#
+# Copyright (C) 2017 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from __future__ import print_function
+
+import unittest
+
+from sign_target_files_apks import EditTags, RewriteProps
+
+
+class SignTargetFilesApksTest(unittest.TestCase):
+
+  def test_EditTags(self):
+    self.assertEqual(EditTags('dev-keys'), ('release-keys'))
+    self.assertEqual(EditTags('test-keys'), ('release-keys'))
+
+    # Multiple tags.
+    self.assertEqual(EditTags('abc,dev-keys,xyz'), ('abc,release-keys,xyz'))
+
+    # Tags are sorted.
+    self.assertEqual(EditTags('xyz,abc,dev-keys,xyz'), ('abc,release-keys,xyz'))
+
+  def test_RewriteProps(self):
+    props = (
+        ('', '\n'),
+        ('ro.build.fingerprint=foo/bar/dev-keys',
+         'ro.build.fingerprint=foo/bar/release-keys\n'),
+        ('ro.build.thumbprint=foo/bar/dev-keys',
+         'ro.build.thumbprint=foo/bar/release-keys\n'),
+        ('ro.vendor.build.fingerprint=foo/bar/dev-keys',
+         'ro.vendor.build.fingerprint=foo/bar/release-keys\n'),
+        ('ro.vendor.build.thumbprint=foo/bar/dev-keys',
+         'ro.vendor.build.thumbprint=foo/bar/release-keys\n'),
+        ('# comment line 1', '# comment line 1\n'),
+        ('ro.bootimage.build.fingerprint=foo/bar/dev-keys',
+         'ro.bootimage.build.fingerprint=foo/bar/release-keys\n'),
+        ('ro.build.description='
+         'sailfish-user 8.0.0 OPR6.170623.012 4283428 dev-keys',
+         'ro.build.description='
+         'sailfish-user 8.0.0 OPR6.170623.012 4283428 release-keys\n'),
+        ('ro.build.tags=dev-keys', 'ro.build.tags=release-keys\n'),
+        ('# comment line 2', '# comment line 2\n'),
+        ('ro.build.display.id=OPR6.170623.012 dev-keys',
+         'ro.build.display.id=OPR6.170623.012\n'),
+        ('# comment line 3', '# comment line 3\n'),
+    )
+
+    # Assert the case for each individual line.
+    for input, output in props:
+      self.assertEqual(RewriteProps(input), output)
+
+    # Concatenate all the input lines.
+    self.assertEqual(RewriteProps('\n'.join([prop[0] for prop in props])),
+                     ''.join([prop[1] for prop in props]))