overlay: Minor refactoring

Use IMGDEPLOYDIR rather than DEPLOY_DIR_IMAGE to leverage sstate.
Add dependency on kernel deploy.
Add dependency on rootfs do_image_<> task.
Adopt standard artifact naming conventions for tars, flash image.
Remove unnecessary checks for missing artifacts provided by dependencies.
Don't deploy intermediate files.
Split tar generation into its own task.

Change-Id: I63385924e38287db20426c6e74dcdac84a4abc33
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/classes/image-overlay.bbclass b/classes/image-overlay.bbclass
index 64a8356..ff97605 100644
--- a/classes/image-overlay.bbclass
+++ b/classes/image-overlay.bbclass
@@ -1,3 +1,7 @@
+# Constructs a bootable, fixed-offset mtd image with u-boot
+# bootloader, kernel fitimage, read only root filesystem,
+# and writeable overlay filesystem.
+
 IMAGE_BASETYPE ?= "squashfs-xz"
 OVERLAY_BASETYPE ?= "jffs2"
 
@@ -6,10 +10,6 @@
 IMAGE_TYPEDEP_overlay = "${IMAGE_BASETYPE}"
 IMAGE_TYPES_MASKED += "overlay"
 
-FLASH_IMAGE_NAME ?= "flash-${MACHINE}-${DATETIME}"
-FLASH_IMAGE_NAME[vardepsexclude] = "DATETIME"
-FLASH_IMAGE_LINK ?= "flash-${MACHINE}"
-
 FLASH_KERNEL_IMAGE ?= "fitImage-${INITRAMFS_IMAGE}-${MACHINE}.bin"
 
 FLASH_UBOOT_OFFSET ?= "0"
@@ -36,51 +36,77 @@
 }
 
 do_generate_flash() {
-	ddir="${DEPLOY_DIR_IMAGE}"
+	ddir="${IMGDEPLOYDIR}"
 	kernel="${FLASH_KERNEL_IMAGE}"
 	uboot="u-boot.${UBOOT_SUFFIX}"
 	rootfs="${IMAGE_LINK_NAME}.${IMAGE_BASETYPE}"
 	rwfs="rwfs.${OVERLAY_BASETYPE}"
 
-	if [ ! -f $ddir/$kernel ]; then
-		bbfatal "Kernel file $ddir/$kernel does not exist"
-	fi
-	if [ ! -f $ddir/$uboot ]; then
-		bbfatal "U-boot file $ddir/$uboot does not exist"
-	fi
-	if [ ! -f $ddir/$rootfs ]; then
-		bbfatal "Rootfs file $ddir/$rootfs does not exist"
-	fi
+	flash="${IMAGE_NAME}.overlay"
 
-	mk_nor_image $ddir/$rwfs ${RWFS_SIZE}
+	mk_nor_image ${S}/$rwfs ${RWFS_SIZE}
 	if [ "${OVERLAY_BASETYPE}" != jffs2 ]; then
-		mkfs.${OVERLAY_BASETYPE} ${OVERLAY_MKFS_OPTS} $ddir/$rwfs || \
+		mkfs.${OVERLAY_BASETYPE} ${OVERLAY_MKFS_OPTS} ${S}/$rwfs || \
 			bbfatal "mkfs rwfs"
 	fi
 
-	dst="$ddir/${FLASH_IMAGE_NAME}"
-	rm -rf $dst
+	# Assemble the flash image
+	dst="$ddir/$flash"
 	mk_nor_image $dst ${FLASH_SIZE}
-	dd if=$ddir/$uboot of=$dst bs=1k conv=notrunc seek=${FLASH_UBOOT_OFFSET}
-	dd if=$ddir/$kernel of=$dst bs=1k conv=notrunc seek=${FLASH_KERNEL_OFFSET}
+	dd if=${DEPLOY_DIR_IMAGE}/$uboot of=$dst bs=1k conv=notrunc seek=${FLASH_UBOOT_OFFSET}
+	dd if=${DEPLOY_DIR_IMAGE}/$kernel of=$dst bs=1k conv=notrunc seek=${FLASH_KERNEL_OFFSET}
 	dd if=$ddir/$rootfs of=$dst bs=1k conv=notrunc seek=${FLASH_ROFS_OFFSET}
-	dd if=$ddir/$rwfs of=$dst bs=1k conv=notrunc seek=${FLASH_RWFS_OFFSET}
-	dstlink="$ddir/${FLASH_IMAGE_LINK}"
-	rm -rf $dstlink
-	ln -sf ${FLASH_IMAGE_NAME} $dstlink
+	dd if=${S}/$rwfs of=$dst bs=1k conv=notrunc seek=${FLASH_RWFS_OFFSET}
 
-	ln -sf ${FLASH_IMAGE_NAME} $ddir/image-bmc
-	ln -sf $uboot $ddir/image-u-boot
-	ln -sf $kernel $ddir/image-kernel
-	ln -sf $rootfs $ddir/image-rofs
-	ln -sf $rwfs $ddir/image-rwfs
+	cd ${IMGDEPLOYDIR}
+	ln -sf $flash ${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.overlay
 
-	tar -h -cvf $ddir/${MACHINE}-${DATETIME}.all.tar -C $ddir image-bmc
-	tar -h -cvf $ddir/${MACHINE}-${DATETIME}.tar -C $ddir image-u-boot image-kernel image-rofs image-rwfs
+	# Maintain a number of non-standard name legacy links.
+	ln -sf $flash ${IMGDEPLOYDIR}/flash-${MACHINE}
 }
-do_generate_flash[vardepsexclude] = "DATETIME"
 
-do_generate_flash[depends] += "${PN}:do_image_complete"
-do_generate_flash[depends] += "u-boot:do_populate_sysroot"
+do_generate_tars() {
+	ddir="${IMGDEPLOYDIR}"
+	kernel="${FLASH_KERNEL_IMAGE}"
+	uboot="u-boot.${UBOOT_SUFFIX}"
+	rootfs="${IMAGE_LINK_NAME}.${IMAGE_BASETYPE}"
+	rwfs="rwfs.${OVERLAY_BASETYPE}"
 
-addtask generate_flash before do_build
+	flash="${IMAGE_NAME}.overlay"
+	alltar="${IMAGE_NAME}.all.tar"
+	tar="${IMAGE_NAME}.tar"
+
+	# Create some links to help make the tar archives
+	ln -sf $ddir/${IMAGE_LINK_NAME}.overlay ${S}/image-bmc
+	ln -sf ${DEPLOY_DIR_IMAGE}/$uboot ${S}/image-u-boot
+	ln -sf ${DEPLOY_DIR_IMAGE}/$kernel ${S}/image-kernel
+	ln -sf $ddir/$rootfs ${S}/image-rofs
+	ln -sf $rwfs ${S}/image-rwfs
+
+	# Create the tar archives
+	tar -h -cvf $ddir/$alltar -C ${S} image-bmc
+	tar -h -cvf $ddir/$tar -C ${S} image-u-boot image-kernel image-rofs image-rwfs
+
+	cd ${IMGDEPLOYDIR}
+	ln -sf $alltar ${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.all.tar
+	ln -sf $tar ${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.tar
+
+	# Maintain a number of non-standard name legacy links.
+	ln -sf $tar ${IMGDEPLOYDIR}/${MACHINE}-${DATETIME}.tar
+	ln -sf $alltar ${IMGDEPLOYDIR}/${MACHINE}-${DATETIME}.all.tar
+}
+
+do_generate_tars[vardepsexclude] = "DATETIME"
+
+do_generate_flash[depends] += " \
+        ${PN}:do_image_${@d.getVar('IMAGE_BASETYPE', True).replace('-', '_')} \
+        virtual/kernel:do_deploy \
+        u-boot:do_populate_sysroot \
+        "
+
+do_generate_tars[depends] += " \
+        ${PN}:do_generate_flash  \
+        "
+
+addtask generate_flash before do_image_complete
+addtask generate_tars before do_image_complete