packages: Give us two different ways to build

We can now trigger a headless build by setting the environment variable
HEADLESS_BUILD.

Change-Id: Idf7733825e7b4936b1292d796447800bd0698eee
diff --git a/docker.mk b/docker.mk
index 122e9a7..662bcdf 100644
--- a/docker.mk
+++ b/docker.mk
@@ -59,6 +59,7 @@
 		-e "PACKAGES_FETCH_ROOT_DIRECTORY=/packages" \
 		-e "FETCH_PACKAGES=$(FETCH_PACKAGES)" \
 		-e "PACKAGES_REVISION=$(PACKAGES_REVISION)" \
+		-e "HEADLESS_BUILD=$(HEADLESS_BUILD)" \
 		aiy-board-builder \
 		/bin/bash -c \
 			'groupadd --gid $(shell id -g) $(shell id -g -n); \
diff --git a/kokoro/headless.cfg b/kokoro/headless.cfg
new file mode 100644
index 0000000..c81eb77
--- /dev/null
+++ b/kokoro/headless.cfg
@@ -0,0 +1,31 @@
+build_file: "continuous-build/build/kokoro/headless.sh"
+gfile_resources: "/x20/teams/spacepark/enterprise/kokoro/prod/spacepark/enterprise/rootfs/latest/rootfs.raw.img"
+gfile_resources: "/x20/teams/spacepark/enterprise/kokoro/prod/spacepark/enterprise/rootfs/latest/rootfs.raw.img.sha256sum"
+gfile_resources: "/x20/teams/spacepark/enterprise/kokoro/prod/spacepark/enterprise/docker/aiy-board-builder.tar"
+gfile_resources: "/x20/teams/spacepark/enterprise/kokoro/prod/spacepark/enterprise/pbuilder/base.tgz"
+action {
+  define_artifacts {
+    regex: "boot.img"
+    regex: "flash.sh"
+    regex: "manifest.xml"
+    regex: "sdcard.img"
+    regex: "partition-table-*.img"
+    regex: "rootfs.img"
+    regex: "u-boot.imx"
+    regex: "packages.tgz"
+  }
+}
+
+before_action {
+  invoke_blaze {
+    name: "libedgetpu"
+    command: BUILD
+    targets: "//knowledge/cerebra/spacepark/darwinn:libedgetpu"
+    flags: "--compilation_mode=opt"
+    flags: "--config=loonix"
+    flags: "--define='darwinn_chip=beagle'"
+    flags: "--define='darwinn_io=PCI'"
+    flags: "--cpu=arm"
+    flags: "--compiler=llvm-libcxx"
+  }
+}
diff --git a/kokoro/headless.sh b/kokoro/headless.sh
new file mode 100644
index 0000000..9c82987
--- /dev/null
+++ b/kokoro/headless.sh
@@ -0,0 +1,48 @@
+#!/bin/bash
+
+set -e
+set -x
+
+# Symlink the Makefile, like it would be if repo checked this out.
+# Otherwise, sourcing setup.sh doesn't work as expected.
+ln -sfr git/continuous-build/build/Makefile git/continuous-build/Makefile
+
+# Sourcing this only works in the directory above build...
+pushd git/continuous-build
+source build/setup.sh
+popd
+
+export IS_GLINUX=true
+export TARBALL_FETCH_ROOT_DIRECTORY=${KOKORO_GFILE_DIR}
+export PREBUILT_MODULES_ROOT=${KOKORO_GFILE_DIR}
+export PREBUILT_DOCKER_ROOT=${KOKORO_GFILE_DIR}
+export FETCH_PBUILDER_DIRECTORY=${KOKORO_GFILE_DIR}
+export PACKAGES_FETCH_ROOT_DIRECTORY=${KOKORO_GFILE_DIR}
+export PACKAGES_REVISION=.
+export ROOTFS_REVISION=.
+export FETCH_PACKAGES=false
+export HEADLESS_BUILD=true
+
+# Inject libedgetpu deb build from Blaze into the package directory.
+touch ${PACKAGES_FETCH_ROOT_DIRECTORY}/${PACKAGES_REVISION}/packages.tgz
+m out-dirs
+find ${KOKORO_BLAZE_DIR} -name 'libedgetpu*.deb' -exec cp {} ${PRODUCT_OUT}/packages \;
+touch ${PRODUCT_OUT}/.libedgetpu-pbuilder
+
+m docker-all
+m docker-sdcard
+
+pushd ${ROOTDIR}
+python3 ${ROOTDIR}/build/create_release_manifest.py \
+  -i ${ROOTDIR}/manifest/default.xml \
+  -o ${KOKORO_ARTIFACTS_DIR}/manifest.xml
+popd
+
+cp ${ROOTDIR}/board/flash.sh ${KOKORO_ARTIFACTS_DIR}
+chmod -x ${KOKORO_ARTIFACTS_DIR}/flash.sh
+cp ${PRODUCT_OUT}/u-boot.imx ${KOKORO_ARTIFACTS_DIR}
+cp ${PRODUCT_OUT}/boot.img ${KOKORO_ARTIFACTS_DIR}
+cp ${PRODUCT_OUT}/partition-table-*.img ${KOKORO_ARTIFACTS_DIR}
+cp ${PRODUCT_OUT}/rootfs.img ${KOKORO_ARTIFACTS_DIR}
+cp ${PRODUCT_OUT}/sdcard.img ${KOKORO_ARTIFACTS_DIR}
+cp ${ROOTDIR}/cache/packages.tgz $KOKORO_ARTIFACTS_DIR
diff --git a/rootfs.mk b/rootfs.mk
index a7bf78b..6dcaaee 100644
--- a/rootfs.mk
+++ b/rootfs.mk
@@ -11,7 +11,7 @@
 ROOTFS_FETCH_TARBALL ?= $(IS_GLINUX)
 ROOTFS_REVISION ?= latest
 
-PRE_INSTALL_PACKAGES := \
+BASE_PACKAGES := \
 	aiy-board-audio \
 	aiy-board-gadget \
 	aiy-board-keyring \
@@ -22,29 +22,38 @@
 	bluetooth \
 	bluez \
 	edgetpu-api \
+	libbluetooth3 \
+	libedgetpu \
+	uboot-imx
+
+GUI_PACKAGES := \
 	gstreamer1.0-alsa \
 	gstreamer1.0-plugins-bad \
 	gstreamer1.0-plugins-base \
 	gstreamer1.0-plugins-base-apps \
 	gstreamer1.0-plugins-good \
 	gstreamer1.0-tools \
-	imx-atf \
-	imx-firmware \
 	imx-gpu-viv \
 	imx-gst1.0-plugin \
-	imx-mkimage \
 	imx-vpu-hantro \
 	imx-vpuwrap \
-	libbluetooth3 \
 	libdrm2 \
 	libdrm-vivante \
-	libedgetpu \
 	libgstreamer1.0-0 \
 	libgstreamer-plugins-bad1.0-0 \
 	libgstreamer-plugins-base1.0-0 \
-	uboot-imx \
 	wayland-protocols \
-	weston-imx \
+	weston-imx
+
+ifeq ($(HEADLESS_BUILD),)
+    $(info )
+    $(info *** GUI build selected -- set HEADLESS_BUILD=true if this is not what you intend.)
+	PRE_INSTALL_PACKAGES := $(BASE_PACKAGES) $(GUI_PACKAGES)
+else
+    $(info )
+    $(info *** Headless build selected -- unset HEADLESS_BUILD if this is not what you intend.)
+	PRE_INSTALL_PACKAGES := $(BASE_PACKAGES)
+endif
 
 rootfs: $(PRODUCT_OUT)/rootfs.img
 rootfs_raw: $(ROOTFS_RAW_IMG)
@@ -108,6 +117,7 @@
 	sudo chroot $(ROOTFS_DIR) bash -c 'apt-get update'
 	sudo chroot $(ROOTFS_DIR) bash -c 'apt-get install aiy-board-keyring'
 	sudo chroot $(ROOTFS_DIR) bash -c 'apt-get update'
+
 	sudo chroot $(ROOTFS_DIR) bash -c 'apt-get install --allow-downgrades --no-install-recommends -y $(PRE_INSTALL_PACKAGES)'
 
 	sudo mount -t tmpfs none $(ROOTFS_DIR)/tmp