add_img_to_target_files uses build_super_image

Use the new script to build super_empty and super split images. No more
transformation to lpmake_args.

Test: build target_files_package for retrofit device
Change-Id: Id5f6bd607654ca869bcdf58d86b7ae300e3927eb
diff --git a/core/Makefile b/core/Makefile
index fde13a1..c0cb8c7 100644
--- a/core/Makefile
+++ b/core/Makefile
@@ -2921,7 +2921,12 @@
 endif # BOARD_AVB_ENABLE
 
 # -----------------------------------------------------------------
-# super partition image
+# Check image sizes <= size of super partition
+
+ifeq (,$(TARGET_BUILD_APPS))
+# Do not check for apps-only build
+
+ifeq (true,$(PRODUCT_BUILD_SUPER_PARTITION))
 
 # (1): list of items like "system", "vendor", "product", "product_services"
 # return: map each item into a command ( wrapped in $$() ) that reads the size
@@ -2929,46 +2934,10 @@
 $(foreach p,$(1),$(call read-image-prop-dictionary,$($(p)image_intermediates)/generated_$(p)_image_info.txt,$(p)_size))
 endef
 
-ifeq (true,$(PRODUCT_BUILD_SUPER_PARTITION))
-
-# BOARD_SUPER_PARTITION_SIZE must be defined to build super image.
-ifneq ($(BOARD_SUPER_PARTITION_SIZE),)
-
 define super-slot-suffix
 $(if $(filter true,$(AB_OTA_UPDATER)),$(if $(filter true,$(PRODUCT_RETROFIT_DYNAMIC_PARTITIONS)),,_a))
 endef
 
-# $(1): slot A suffix (_a or empty)
-# $(2): include images or not (true or empty)
-define build-superimage-target-args
-  $(if $(2), --sparse) \
-  --metadata-size 65536 \
-  --metadata-slots $(if $(filter true,$(AB_OTA_UPDATER)),2,1) \
-  --super-name $(BOARD_SUPER_PARTITION_METADATA_DEVICE) \
-  $(if $(filter true,$(PRODUCT_RETROFIT_DYNAMIC_PARTITIONS)), $(if $(filter true,$(AB_OTA_UPDATER)), --auto-slot-suffixing)) \
-  $(foreach device,$(BOARD_SUPER_PARTITION_BLOCK_DEVICES), \
-    --device $(device):$(BOARD_SUPER_PARTITION_$(call to-upper,$(device))_DEVICE_SIZE)) \
-  $(foreach group,$(BOARD_SUPER_PARTITION_GROUPS), \
-    --group $(group)$(1):$(BOARD_$(call to-upper,$(group))_SIZE) \
-    $(if $(1), --group $(group)_b:$(BOARD_$(call to-upper,$(group))_SIZE)) \
-    $(foreach name,$(BOARD_$(call to-upper,$(group))_PARTITION_LIST), \
-      --partition $(name)$(1):readonly:$(if $(2),$(call read-size-of-partitions,$(name)),0):$(group)$(1) \
-      $(if $(2), --image $(name)$(1)=$(call images-for-partitions,$(name))) \
-      $(if $(1), --partition $(name)_b:readonly:0:$(group)_b) \
-  ))
-endef
-
-endif # BOARD_SUPER_PARTITION_SIZE
-endif # PRODUCT_BUILD_SUPER_PARTITION
-
-# -----------------------------------------------------------------
-# Check image sizes <= size of super partition
-
-ifeq (,$(TARGET_BUILD_APPS))
-# Do not check for apps-only build
-
-ifeq (true,$(PRODUCT_BUILD_SUPER_PARTITION))
-
 droid_targets: check-all-partition-sizes
 
 .PHONY: check-all-partition-sizes check-all-partition-sizes-nodeps
@@ -3306,10 +3275,9 @@
     echo "use_dynamic_partitions=true" >> $(1))
   $(if $(filter true,$(PRODUCT_RETROFIT_DYNAMIC_PARTITIONS)), \
     echo "dynamic_partition_retrofit=true" >> $(1))
-  $(if $(BOARD_SUPER_PARTITION_SIZE), \
-    echo "lpmake=$(notdir $(LPMAKE))" >> $(1); \
-    echo -n "lpmake_args=" >> $(1); \
-    echo $(call build-superimage-target-args,$(call super-slot-suffix)) >> $(1))
+  echo "lpmake=$(notdir $(LPMAKE))" >> $(1)
+  $(if $(filter true,$(PRODUCT_BUILD_SUPER_PARTITION)), $(if $(BOARD_SUPER_PARTITION_SIZE), \
+    echo "build_super_partition=true" >> $(1)))
   echo "super_metadata_device=$(BOARD_SUPER_PARTITION_METADATA_DEVICE)" >> $(1)
   $(if $(BOARD_SUPER_PARTITION_BLOCK_DEVICES), \
     echo "super_block_devices=$(BOARD_SUPER_PARTITION_BLOCK_DEVICES)" >> $(1))
diff --git a/tools/releasetools/add_img_to_target_files.py b/tools/releasetools/add_img_to_target_files.py
index 139d257..669d87b 100755
--- a/tools/releasetools/add_img_to_target_files.py
+++ b/tools/releasetools/add_img_to_target_files.py
@@ -55,6 +55,7 @@
 import zipfile
 
 import build_image
+import build_super_image
 import common
 import rangelib
 import sparse_img
@@ -648,64 +649,15 @@
   """Create a super_empty.img and store it in output_zip."""
 
   img = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "super_empty.img")
-  cmd = [OPTIONS.info_dict['lpmake']]
-  cmd += shlex.split(OPTIONS.info_dict['lpmake_args'].strip())
-  cmd += ['--output', img.name]
-  common.RunAndCheckOutput(cmd)
-
+  build_super_image.BuildSuperImage(OPTIONS.info_dict, img.name)
   img.Write()
 
 
 def AddSuperSplit(output_zip):
   """Create split super_*.img and store it in output_zip."""
 
-  def GetPartitionSizeFromImage(img):
-    try:
-      simg = sparse_img.SparseImage(img)
-      return simg.blocksize * simg.total_blocks
-    except ValueError:
-      return os.path.getsize(img)
-
-  def TransformPartitionArg(arg):
-    lst = arg.split(':')
-    # Because --auto-slot-suffixing for A/B, there is no need to remove suffix.
-    name = lst[0]
-    if name + '_size' in OPTIONS.info_dict:
-      size = str(OPTIONS.info_dict[name + '_size'])
-      logger.info("Using %s_size = %s", name, size)
-    else:
-      size = str(GetPartitionSizeFromImage(
-          os.path.join(OPTIONS.input_tmp, "IMAGES", '{}.img'.format(name))))
-      logger.info("Using size of prebuilt %s = %s", name, size)
-    lst[2] = size
-    return ':'.join(lst)
-
-  def GetLpmakeArgsWithSizes():
-    lpmake_args = shlex.split(OPTIONS.info_dict['lpmake_args'].strip())
-
-    for i, arg in enumerate(lpmake_args):
-      if arg == '--partition':
-        assert i + 1 < len(lpmake_args), \
-          'lpmake_args has --partition without value'
-        lpmake_args[i + 1] = TransformPartitionArg(lpmake_args[i + 1])
-
-    return lpmake_args
-
-  outdir = OutputFile(output_zip, OPTIONS.input_tmp, "OTA", "")
-  cmd = [OPTIONS.info_dict['lpmake']]
-  cmd += GetLpmakeArgsWithSizes()
-
-  source = OPTIONS.info_dict.get('dynamic_partition_list', '').strip()
-  if source:
-    cmd.append('--sparse')
-    for name in shlex.split(source):
-      img = os.path.join(OPTIONS.input_tmp, "IMAGES", '{}.img'.format(name))
-      # Because --auto-slot-suffixing for A/B, there is no need to add suffix.
-      cmd += ['--image', '{}={}'.format(name, img)]
-
-  cmd += ['--output', outdir.name]
-
-  common.RunAndCheckOutput(cmd)
+  outdir = os.path.join(OPTIONS.input_tmp, "OTA")
+  build_super_image.BuildSuperImage(OPTIONS.input_tmp, outdir)
 
   for dev in OPTIONS.info_dict['super_block_devices'].strip().split():
     img = OutputFile(output_zip, OPTIONS.input_tmp, "OTA",
@@ -906,14 +858,13 @@
     banner("vbmeta")
     AddVBMeta(output_zip, partitions, "vbmeta", vbmeta_partitions)
 
-  if OPTIONS.info_dict.get("lpmake_args"):
+  if OPTIONS.info_dict.get("build_super_partition"):
     banner("super_empty")
     AddSuperEmpty(output_zip)
 
     if OPTIONS.info_dict.get("dynamic_partition_retrofit") == "true":
       banner("super split images")
       AddSuperSplit(output_zip)
-    # TODO(b/119322123): Add super.img to target_files for non-retrofit
 
   banner("radio")
   ab_partitions_txt = os.path.join(OPTIONS.input_tmp, "META",