From 01bbc0cc57d0cde40a23b0586018bc6136c85177 Mon Sep 17 00:00:00 2001
From: Maxim Cournoyer <maxim.cournoyer@savoirfairelinux.com>
Date: Mon, 15 Feb 2021 00:01:00 -0500
Subject: [PATCH] build: Add support to use cached tarballs for packaging
 targets.

Previously, the only way to ensure a clean release tarball was to
start from scratch, fetching all the contrib sources from the
network (> 1 GiB).  This change leverages the new 'list-tarballs'
targets of the contrib build system to allow reusing the relevant
tarballs from the cache when available, which translates to faster
builds.

It also constructs the pristine source release archive via
git-archive, which guards against including unwanted files.  Since
only the required tarballs are included in the source archive, its
size is reduced from 1.4 GiB to 72 MiB.

GitLab: https://git.jami.net/savoirfairelinux/jami-packaging/-/issues/55
Change-Id: I4993b269f3b97f6a4b6b8592aa8b5f4eb5448943
---
 .gitignore |  1 +
 Makefile   | 48 ++++++++++++++++++++++++++++++++++++------------
 2 files changed, 37 insertions(+), 12 deletions(-)

diff --git a/.gitignore b/.gitignore
index a2b26ad7..ee1732cf 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,6 +8,7 @@ packages
 jami_*.tar.gz
 Makefile.packaging.distro_targets
 repositories
+tarballs.manifest
 manual-download
 .docker-image-*
 
diff --git a/Makefile b/Makefile
index 78e6a8bd..bd31c370 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 # -*- mode: makefile; -*-
-# Copyright (C) 2016-2019 Savoir-faire Linux Inc.
+# Copyright (C) 2016-2021 Savoir-faire Linux Inc.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -54,20 +54,44 @@ CURRENT_GID:=$(shell id -g)
 .PHONY: release-tarball
 release-tarball: $(RELEASE_TARBALL_FILENAME)
 
-$(RELEASE_TARBALL_FILENAME):
-	# Fetch tarballs
-	mkdir -p daemon/contrib/native
+# Fetch the required contrib sources and copy them to
+# daemon/contrib/tarballs.  To use a custom tarballs cache directory,
+# export the TARBALLS environment variable.
+tarballs.manifest:
+	rm -rf daemon/contrib/native
+	mkdir -p daemon/contrib/native && \
 	cd daemon/contrib/native && \
-	    ../bootstrap && make list && \
-	    make fetch-all -j || make fetch-all || make fetch-all
+	../bootstrap && \
+        $(MAKE) list && \
+        $(MAKE) fetch -j && \
+	$(MAKE) --silent list-tarballs > $(CURDIR)/$@
 	rm -rf daemon/contrib/native
 
-	cd $(TMPDIR) && \
-	    tar -C $(CURDIR)/.. \
-	        --exclude-vcs \
-	        -zcf $(RELEASE_TARBALL_FILENAME) \
-	        $(shell basename $(CURDIR)) && \
-	    mv $(RELEASE_TARBALL_FILENAME) $(CURDIR)
+# Generate the release tarball.  Note: to avoid building 1+ GiB
+# tarball containing all the bundled libraries, only the required
+# tarballs are included.  This means the resulting release tarball
+# content depends on what libraries the host has installed.  To build
+# a single release tarball that can be used for any GNU/Linux machine,
+# it should be built in a minimal container.)
+$(RELEASE_TARBALL_FILENAME): tarballs.manifest
+# Prepare the sources of the top repository and relevant submodules.
+	rm -f "$@"
+	mkdir $(TMPDIR)/ring-project
+	git archive HEAD | tar xf - -C $(TMPDIR)/ring-project
+	for m in daemon lrc client-gnome; do \
+		(cd "$$m" && git archive --prefix "$$m/" HEAD \
+			| tar xf - -C $(TMPDIR)/ring-project); \
+	done
+# Create the base archive.
+	tar --create --file $(TMPDIR)/ring-project.tar $(TMPDIR)/ring-project \
+		--transform 's,.*/ring-project,ring-project,'
+# Append the cached tarballs listed in the manifest.
+	tar --append --file $(TMPDIR)/ring-project.tar \
+		--files-from $< \
+		--transform 's,^.*/,ring-project/daemon/contrib/tarballs/,'
+	gzip $(TMPDIR)/ring-project.tar
+	mv $(TMPDIR)/ring-project.tar.gz "$@"
+	rm -rf $(TMPDIR)
 
 #######################
 ## Packaging targets ##
-- 
GitLab