Build cache.img on demand

Bug: 5153694
To build cache.img, set BOARD_CACHEIMAGE_FILE_SYSTEM_TYPE (required,
ext4 only for now), BOARD_CACHEIMAGE_PARTITION_SIZE (optional) in
BoardConfig.mk.

Change-Id: I1d8b91646aa1dba88285e008ad3335768bcbddd2
diff --git a/tools/releasetools/build_image.py b/tools/releasetools/build_image.py
index b5140a9..15acddc 100755
--- a/tools/releasetools/build_image.py
+++ b/tools/releasetools/build_image.py
@@ -67,22 +67,28 @@
     mount_point: such as "system", "data" etc.
   """
   d = {}
+
+  def copy_prop(src_p, dest_p):
+    if src_p in glob_dict:
+      d[dest_p] = str(glob_dict[src_p])
+
   common_props = (
-      "fs_type",
       "extfs_sparse_flag",
       "mkyaffs2_extra_flags",
       )
   for p in common_props:
-    if p in glob_dict:
-      d[p] = glob_dict[p]
+    copy_prop(p, p)
 
   d["mount_point"] = mount_point
   if mount_point == "system":
-    if "system_size" in glob_dict:
-      d["partition_size"] = str(glob_dict["system_size"])
+    copy_prop("fs_type", "fs_type")
+    copy_prop("system_size", "partition_size")
   elif mount_point == "data":
-    if "userdata_size" in glob_dict:
-      d["partition_size"] = str(glob_dict["userdata_size"])
+    copy_prop("fs_type", "fs_type")
+    copy_prop("userdata_size", "partition_size")
+  elif mount_point == "cache":
+    copy_prop("cache_fs_type", "fs_type")
+    copy_prop("cache_size", "partition_size")
 
   return d
 
@@ -117,6 +123,11 @@
     mount_point = "system"
   elif image_filename == "userdata.img":
     mount_point = "data"
+  elif image_filename == "cache.img":
+    mount_point = "cache"
+  else:
+    print >> sys.stderr, "error: unknown image file name ", image_filename
+    exit(1)
 
   image_properties = ImagePropFromGlobalDict(glob_dict, mount_point)
   if not BuildImage(in_dir, image_properties, out_file):
diff --git a/tools/releasetools/common.py b/tools/releasetools/common.py
index 4957354..1049591 100644
--- a/tools/releasetools/common.py
+++ b/tools/releasetools/common.py
@@ -138,6 +138,7 @@
   makeint("blocksize")
   makeint("system_size")
   makeint("userdata_size")
+  makeint("cache_size")
   makeint("recovery_size")
   makeint("boot_size")
 
diff --git a/tools/releasetools/img_from_target_files b/tools/releasetools/img_from_target_files
index 4388f69..002e6e6 100755
--- a/tools/releasetools/img_from_target_files
+++ b/tools/releasetools/img_from_target_files
@@ -80,6 +80,38 @@
   os.rmdir(temp_dir)
 
 
+def AddCache(output_zip):
+  """Create an empty cache image and store it in output_zip."""
+
+  image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
+                                                    "cache")
+  # The build system has to explicitly request for cache.img.
+  if "fs_type" not in image_props:
+    return
+
+  print "creating cache.img..."
+
+  # The name of the directory it is making an image out of matters to
+  # mkyaffs2image.  So we create a temp dir, and within it we create an
+  # empty dir named "cache", and build the image from that.
+  temp_dir = tempfile.mkdtemp()
+  user_dir = os.path.join(temp_dir, "cache")
+  os.mkdir(user_dir)
+  img = tempfile.NamedTemporaryFile()
+
+  fstab = OPTIONS.info_dict["fstab"]
+  if fstab:
+    image_props["fs_type" ] = fstab["/cache"].fs_type
+  succ = build_image.BuildImage(user_dir, image_props, img.name)
+  assert succ, "build cache.img image failed"
+
+  common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
+  output_zip.write(img.name, "cache.img")
+  img.close()
+  os.rmdir(user_dir)
+  os.rmdir(temp_dir)
+
+
 def AddSystem(output_zip):
   """Turn the contents of SYSTEM into a system image and store it in
   output_zip."""
@@ -163,6 +195,7 @@
   if not bootable_only:
     AddSystem(output_zip)
     AddUserdata(output_zip)
+    AddCache(output_zip)
     CopyInfo(output_zip)
 
   print "cleaning up..."