From f8529c9038658ac5fba8b3025d28d8c0548f044f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=81ukasz=20=27sil2100=27=20Zemczak?=
 <lukasz.zemczak@canonical.com>
Date: Tue, 30 Oct 2018 14:13:02 +0100
Subject: [PATCH 1/4] Add support for building classic images using
 makefile-based gadget trees.

---
 debian/control         |  4 ++-
 live-build/auto/build  | 26 +++++++++++++----
 live-build/auto/config | 64 ++++++++++++++++++++++++++++--------------
 3 files changed, 66 insertions(+), 28 deletions(-)

diff --git a/debian/control b/debian/control
index 9d47c949..16cc8c4f 100644
--- a/debian/control
+++ b/debian/control
@@ -23,6 +23,7 @@ Depends: ${misc:Depends},
          live-build (>= 3.0~a57-1ubuntu31~),
          lsb-release,
          lzma,
+         make,
          parted,
          procps,
          python-minimal | python,
@@ -32,7 +33,8 @@ Depends: ${misc:Depends},
          rsync,
          snapd,
          squashfs-tools (>= 1:3.3-1),
-         sudo,
+         sudo
+         u-boot-tools [armhf arm64],
          ubuntu-image,
          vmdk-stream-converter [amd64 i386],
          xz-utils,
diff --git a/live-build/auto/build b/live-build/auto/build
index 35b2219d..2e5adc94 100755
--- a/live-build/auto/build
+++ b/live-build/auto/build
@@ -23,12 +23,26 @@ PREFIX="livecd.$PROJECT${SUBARCH:+-$SUBARCH}"
 if [ "${IMAGEFORMAT:-}" = "ubuntu-image" ]; then
 	# Use ubuntu-image instead of live-build
 
-	CHANNEL="${CHANNEL:-edge}"
-	env SNAPPY_STORE_NO_CDN=1 \
-	ubuntu-image -c "$CHANNEL" $UBUNTU_IMAGE_ARGS \
-		-o "$PREFIX".img "$PREFIX".model-assertion
-	xz -0 -T4 "$PREFIX".img
-	mv seed.manifest "$PREFIX".manifest
+	if [ "$PROJECT" = "ubuntu-core"]; then
+		CHANNEL="${CHANNEL:-edge}"
+		env SNAPPY_STORE_NO_CDN=1 \
+		ubuntu-image snap -c "$CHANNEL" $UBUNTU_IMAGE_ARGS \
+			-o "$PREFIX".img "$PREFIX".model-assertion
+		xz -0 -T4 "$PREFIX".img
+		mv seed.manifest "$PREFIX".manifest
+	else
+		# First we need to build the gadget tree
+		(
+			cd config/$PREFIX-gadget
+			make &&	make install
+		)
+		ubuntu-image classic $UBUNTU_IMAGE_ARGS \
+			-p $PROJECT -a $ARCH --subarch $SUBARCH \
+			-o "$PREFIX".img config/$PREFIX-gadget/install
+		xz -0 -T4 "$PREFIX".img
+		mv filesystem.manifest "$PREFIX".manifest
+	fi
+
 	exit 0
 fi
 
diff --git a/live-build/auto/config b/live-build/auto/config
index 46a7a5cf..d04c86e3 100755
--- a/live-build/auto/config
+++ b/live-build/auto/config
@@ -179,28 +179,50 @@ case $IMAGEFORMAT in
 				exit 1
 				;;
 		esac
-		case $MODEL in
-			pc-amd64|pc-i386)
-				UBUNTU_IMAGE_ARGS="--image-size 3700M" ;;
-			*)
-				UBUNTU_IMAGE_ARGS="" ;;
-		esac
-		case $SUITE in
-			xenial)
-				# Ubuntu Core 16
-				;;
-			*)
-				# Ubuntu Core 18
-				MODEL="ubuntu-core-18-${MODEL#pc-}" ;;
-		esac
 
-		echo "IMAGEFORMAT=$IMAGEFORMAT" >> config/common
-		echo "UBUNTU_IMAGE_ARGS=\"$UBUNTU_IMAGE_ARGS\"" >> config/common
-		# Store model assertion in top dir to get it picked up later as a build artifact
-		env SNAPPY_STORE_NO_CDN=1 snap known --remote model series=16 model="$MODEL" brand-id=canonical > "$PREFIX".model-assertion
-		echo "Configured ubuntu-image for the following model assertion:"
-		cat "$PREFIX".model-assertion
-		echo "----------------------------------------------------------"
+		if [ $PROJECT = "ubuntu-core" ]; then
+			# snap-based core images
+
+			case $SUITE in
+				xenial)
+					# Ubuntu Core 16
+					;;
+				*)
+					# Ubuntu Core 18
+					MODEL="ubuntu-core-18-${MODEL#pc-}" ;;
+			esac
+			case $MODEL in
+				pc-amd64|pc-i386)
+					UBUNTU_IMAGE_ARGS="--image-size 3700M" ;;
+				*)
+					UBUNTU_IMAGE_ARGS="" ;;
+			esac
+
+			echo "IMAGEFORMAT=$IMAGEFORMAT" >> config/common
+			echo "UBUNTU_IMAGE_ARGS=\"$UBUNTU_IMAGE_ARGS\"" >> config/common
+			# Store model assertion in top dir to get it picked up later as a build artifact
+			env SNAPPY_STORE_NO_CDN=1 snap known --remote model series=16 model="$MODEL" brand-id=canonical > "$PREFIX".model-assertion
+			echo "Configured ubuntu-image for the following model assertion:"
+			cat "$PREFIX".model-assertion
+			echo "----------------------------------------------------------"
+		else
+			# classic images
+
+			UBUNTU_IMAGE_ARGS=""
+			if [ "$PROPOSED" ]; then
+				UBUNTU_IMAGE_ARGS="$UBUNTU_IMAGE_ARGS --with-proposed"
+			fi
+			if [ "$EXTRA_PPAS" ]; then
+				UBUNTU_IMAGE_ARGS="$UBUNTU_IMAGE_ARGS --extra-ppas \"$EXTRA_PPAS\""
+			fi
+
+			git clone lp:~canonical-foundations/snap-$MODEL/+git/github-mirror -b classic config/$PREFIX-gadget
+
+			echo "IMAGEFORMAT=$IMAGEFORMAT" >> config/common
+			echo "UBUNTU_IMAGE_ARGS=\"$UBUNTU_IMAGE_ARGS\"" >> config/common
+
+			echo "Configured ubuntu-image for the following gadget model: $MODEL"
+		fi
 		# Fake finished configuration for lb build
 		mkdir -p .build
 		touch .build/config

From 2947cdbe605b61c2c5c88a17890f54eefcc6a425 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=81ukasz=20=27sil2100=27=20Zemczak?=
 <lukasz.zemczak@canonical.com>
Date: Wed, 21 Nov 2018 20:22:38 +0100
Subject: [PATCH 2/4] First batch of obvious changes - use better shellism,
 only call make (without make install).

---
 live-build/auto/build  | 5 +----
 live-build/auto/config | 8 ++------
 2 files changed, 3 insertions(+), 10 deletions(-)

diff --git a/live-build/auto/build b/live-build/auto/build
index 01bf5708..8bc034cb 100755
--- a/live-build/auto/build
+++ b/live-build/auto/build
@@ -32,10 +32,7 @@ if [ "${IMAGEFORMAT:-}" = "ubuntu-image" ]; then
 		mv seed.manifest "$PREFIX".manifest
 	else
 		# First we need to build the gadget tree
-		(
-			cd config/$PREFIX-gadget
-			make &&	make install
-		)
+		make -C "config/$PREFIX-gadget"
 		ubuntu-image classic $UBUNTU_IMAGE_ARGS \
 			-p $PROJECT -a $ARCH --subarch $SUBARCH \
 			-o "$PREFIX".img config/$PREFIX-gadget/install
diff --git a/live-build/auto/config b/live-build/auto/config
index 0523b394..fa386264 100755
--- a/live-build/auto/config
+++ b/live-build/auto/config
@@ -218,12 +218,8 @@ case $IMAGEFORMAT in
 			# classic images
 
 			UBUNTU_IMAGE_ARGS=""
-			if [ "$PROPOSED" ]; then
-				UBUNTU_IMAGE_ARGS="$UBUNTU_IMAGE_ARGS --with-proposed"
-			fi
-			if [ "$EXTRA_PPAS" ]; then
-				UBUNTU_IMAGE_ARGS="$UBUNTU_IMAGE_ARGS --extra-ppas \"$EXTRA_PPAS\""
-			fi
+			UBUNTU_IMAGE_ARGS="$UBUNTU_IMAGE_ARGS${PROPOSED:+ --with-proposed}"
+			UBUNTU_IMAGE_ARGS="$UBUNTU_IMAGE_ARGS${EXTRA_PPAS:+ --extra-ppas \"$EXTRA_PPAS\"}"
 
 			git clone lp:~canonical-foundations/snap-$MODEL/+git/github-mirror -b classic config/$PREFIX-gadget
 

From 8a33b23dd0e888bf30cba8214bb4cce0c4313f8e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=81ukasz=20=27sil2100=27=20Zemczak?=
 <lukasz.zemczak@canonical.com>
Date: Wed, 21 Nov 2018 21:05:21 +0100
Subject: [PATCH 3/4] Use -O instead of -o for ubuntu-image classic builds.

---
 live-build/auto/build | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/live-build/auto/build b/live-build/auto/build
index 8bc034cb..03bb0f0f 100755
--- a/live-build/auto/build
+++ b/live-build/auto/build
@@ -35,9 +35,13 @@ if [ "${IMAGEFORMAT:-}" = "ubuntu-image" ]; then
 		make -C "config/$PREFIX-gadget"
 		ubuntu-image classic $UBUNTU_IMAGE_ARGS \
 			-p $PROJECT -a $ARCH --subarch $SUBARCH \
-			-o "$PREFIX".img config/$PREFIX-gadget/install
+			-O output config/$PREFIX-gadget/install
+		# XXX: currently we only have one image generated, but really
+		#  we should be supporting more than one for models that
+		#  define those.
+		mv output/*.img "$PREFIX".img
 		xz -0 -T4 "$PREFIX".img
-		mv filesystem.manifest "$PREFIX".manifest
+		mv output/filesystem.manifest "$PREFIX".manifest
 	fi
 
 	exit 0

From 05a8be839fcc9ea3e21958029d18b7e584043b6c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=81ukasz=20=27sil2100=27=20Zemczak?=
 <lukasz.zemczak@canonical.com>
Date: Wed, 21 Nov 2018 21:07:16 +0100
Subject: [PATCH 4/4] Do the same for ubuntu-image snap builds.

---
 live-build/auto/build | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/live-build/auto/build b/live-build/auto/build
index 03bb0f0f..410e3def 100755
--- a/live-build/auto/build
+++ b/live-build/auto/build
@@ -27,9 +27,13 @@ if [ "${IMAGEFORMAT:-}" = "ubuntu-image" ]; then
 		CHANNEL="${CHANNEL:-edge}"
 		env SNAPPY_STORE_NO_CDN=1 \
 		ubuntu-image snap -c "$CHANNEL" $UBUNTU_IMAGE_ARGS \
-			-o "$PREFIX".img "$PREFIX".model-assertion
+			-O output "$PREFIX".model-assertion
+		# XXX: currently we only have one image generated, but really
+		#  we should be supporting more than one for models that
+		#  define those.
+		mv output/*.img "$PREFIX".img
 		xz -0 -T4 "$PREFIX".img
-		mv seed.manifest "$PREFIX".manifest
+		mv output/seed.manifest "$PREFIX".manifest
 	else
 		# First we need to build the gadget tree
 		make -C "config/$PREFIX-gadget"