Makefile: Build crosvm image from pre-built tar.gz

Build the crosvm image from the cvd-host_package.tar.gz archive produced
by the Android build system. This allows Make to track the dependency
chain between files (instead of relying on directories), which can then
be expressed from end-to-end:

    cvd-host_package.tar.gz -> crosvm.ext4 -> test-crosvm-*

so that tests requiring the image are now able to automatically re-build
it when the underlying archive has changed.

Support the existing update-prebuilts PHONY target by providing a
corresponding target, used to download the latest version of the
pre-built archive from the Android build servers.

Bug: 198568262
Test: make update-prebuilts-crosvm
Test: make test-crosvm-helloworld
Change-Id: I9325b21cb49a3df9667686b1e0404c49f40c62b1
diff --git a/Makefile b/Makefile
index 717a143..b81ffcb 100644
--- a/Makefile
+++ b/Makefile
@@ -308,15 +308,18 @@
 ## Generating/Updating prebuilts
 ##
 
-PREBUILTS_CROSVM_EXT4 := prebuilts/linux-aarch64/images/crosvm.ext4
+PREBUILTS_CROSVM_TAR_GZ := prebuilts/linux-aarch64/cvd-host_package.tar.gz
 PREBUILTS_ROOTFS_EXT4 := prebuilts/linux-aarch64/images/rootfs.ext4
 PREBUILTS_QEMU_ROM := prebuilts/linux-aarch64/images/efi-virtio.rom
 PREBUILTS_QEMU_BIN := prebuilts/linux-x86/qemu
 
+CROSVM_TAR_GZ := $(PREBUILTS_CROSVM_TAR_GZ)
+CROSVM_EXT4 := $(OUT_DIR)/images/crosvm.ext4
+
 .PHONY:update-prebuilts
 update-prebuilts: \
 	$(PREBUILTS_ROOTFS_EXT4) \
-	$(PREBUILTS_CROSVM_EXT4) \
+	update-prebuilts-crosvm \
 	$(PREBUILTS_QEMU_ROM) \
 	$(PREBUILTS_QEMU_BIN)
 
@@ -329,12 +332,22 @@
 	cp $(BR_OUT)/per-package/host-qemu/host/share/qemu/efi-virtio.rom $@
 
 CF_DOWNLOAD_AOSP_SH := $(ROOT_DIR)/build/cuttlefish/download-aosp.sh
+
+# A PHONY target is necessary for allowing the user to update the archive
+# because, as we are downloading it from a remote server, make dependencies are
+# unable to express the local-->remote dependency:
+.PHONY: update-prebuilts-crosvm
+update-prebuilts-crosvm $(PREBUILTS_CROSVM_TAR_GZ):
+	@rm -f $(PREBUILTS_CROSVM_TAR_GZ)  # else, wget will create "$@.1"
+	cd $(dir $(PREBUILTS_CROSVM_TAR_GZ)) && $(CF_DOWNLOAD_AOSP_SH) -XCa arm64
+	@touch -c $(PREBUILTS_CROSVM_TAR_GZ)  # wget doesn't set timestamp to now
+
 CROSVM_IMG_SIZE_MB := 1024
 
-$(PREBUILTS_CROSVM_EXT4): TMP_DIR := $(shell mktemp -d)
-$(PREBUILTS_CROSVM_EXT4): $(CF_DOWNLOAD_AOSP_SH)
-	mkdir -p $(shell dirname $@)
-	cd $(TMP_DIR) && $(CF_DOWNLOAD_AOSP_SH) -a arm64 -C
+$(CROSVM_EXT4): TMP_DIR := $(shell mktemp -d)
+$(CROSVM_EXT4): $(CROSVM_TAR_GZ)
+	mkdir -p $(@D)
+	cd $(TMP_DIR) && tar xzvf $(abspath $<)
 	dd if=/dev/zero of=$@.tmp bs=1M count=$(CROSVM_IMG_SIZE_MB)
 	mkfs.ext4 -d $(TMP_DIR) $@.tmp
 	rm -rf $(TMP_DIR)
@@ -444,6 +457,7 @@
 test-list-crosvm-helloworld: LISTED_TESTS = $(CROSVM_HELLOWORLD_TESTS)
 
 TESTS += $(CROSVM_HELLOWORLD_TESTS)
+$(CROSVM_HELLOWORLD_TESTS): $(CROSVM_EXT4)
 $(CROSVM_HELLOWORLD_TESTS): RUN_TEST_LOG_DIR = $(DIST_DIR)/logs/crosvm/helloworld
 $(CROSVM_HELLOWORLD_TESTS): RUN_TEST_HOST_SH = $(SCRIPTS_DIR)/crosvm/helloworld/host.sh
 $(CROSVM_HELLOWORLD_TESTS): RUN_TEST_QEMU_ARGS = \
@@ -451,7 +465,7 @@
 		-R $(SCRIPTS_DIR)/crosvm/helloworld/guest.sh \
 		-R $(VM_IMAGE) \
 		-R $(PREBUILTS_ROOTFS_EXT4) \
-		-R $(PREBUILTS_CROSVM_EXT4)
+		-R $(CROSVM_EXT4)
 $(CROSVM_HELLOWORLD_TESTS): RUN_TEST_TIMEOUT := 300s
 
 .PHONY test: $(TESTS)