From d6dde7ee16d089d3d4f78108d79a11f0490ae1e9 Mon Sep 17 00:00:00 2001
From: Sandra Tobajas <sandra.tobajas@savoirfairelinux.com>
Date: Thu, 8 Oct 2020 11:18:56 +0000
Subject: [PATCH] packaging: debian: use cross compilation for armhf platforms

Using qemu in user mode to build jami package for raspbian platforms
adds a lot of overhead.

Remove the virtualization using the Debian 10 cross-compiling toolchain.
The target platform (i.e. the platform on which the package will be
installed) has to be set and some compilation options has to be added in
the debian/rules file.

Also add the armhf architecture in the Docker container in order to
install the package dependencies for this platform.

Change-Id: Ic2282ad9377d52482bca4a4f0ef017fbcc1f8eb7
---
 docker/Dockerfile_raspbian_10_armhf           | 37 +++++++++++++------
 .../rules/debian-one-click-install/rules      | 14 ++++++-
 packaging/rules/debian/rules                  | 14 ++++++-
 scripts/build-package-debian.sh               | 18 +++++++--
 4 files changed, 64 insertions(+), 19 deletions(-)

diff --git a/docker/Dockerfile_raspbian_10_armhf b/docker/Dockerfile_raspbian_10_armhf
index c3a48529..8072df25 100644
--- a/docker/Dockerfile_raspbian_10_armhf
+++ b/docker/Dockerfile_raspbian_10_armhf
@@ -1,5 +1,4 @@
-FROM arm32v7/debian:buster
-# debian because no raspbian imgs
+FROM debian:buster
 
 # FIXME: dirty qemu-static hack required because our Jenkins node runs Docker 17
 # Remove next line as soon as we get Docker 18 +
@@ -7,14 +6,27 @@ COPY qemu-static/qemu-arm-static /usr/bin/qemu-arm-static
 
 ENV DEBIAN_FRONTEND noninteractive
 
-RUN apt-get clean
-RUN apt-get update && \
-    apt-get install -y vim devscripts
+# Add armhf dpkg architecture and add cross-compiling toolchain and
+# tools
+RUN dpkg --add-architecture armhf
+
+RUN apt-get clean && \
+    apt update && \
+    apt install -y \
+        devscripts \
+        gcc-8-arm-linux-gnueabihf-base \
+        gcc-arm-linux-gnueabihf \
+        binutils-arm-linux-gnueabihf \
+        g++-arm-linux-gnueabihf \
+        cpp-arm-linux-gnueabihf \
+        binutils-arm-linux-gnueabihf-dbg \
+        crossbuild-essential-armhf \
+        build-essential && \
+    rm -rf /var/lib/apt/lists/*
 
 # Speed up mk-build-deps
-RUN apt-get clean
-RUN apt-get update && \
-    apt-get install -y -o Acquire::Retires=10 \
+RUN apt update && \
+    apt install -y -o Acquire::Retries=3 \
         git \
         autoconf \
         automake \
@@ -25,9 +37,9 @@ RUN apt-get update && \
         libdbus-1-dev \
         libdbus-c++-dev \
         libargon2-0-dev \
-        libcanberra-gtk3-dev \
-        libclutter-gtk-1.0-dev \
-        libclutter-1.0-dev \
+        libcanberra-gtk3-dev:armhf \
+        libclutter-gtk-1.0-dev:armhf \
+        libclutter-1.0-dev:armhf \
         libglib2.0-dev \
         libgtk-3-dev \
         libnotify-dev \
@@ -66,7 +78,8 @@ RUN apt-get update && \
         libcrypto++-dev \
         libva-dev \
         libvdpau-dev \
-        libssl-dev
+        libssl-dev:armhf && \
+    rm -rf /var/lib/apt/lists/*
 
 ADD scripts/build-package-debian.sh /opt/build-package-debian.sh
 
diff --git a/packaging/rules/debian-one-click-install/rules b/packaging/rules/debian-one-click-install/rules
index 377d568c..32134cb2 100755
--- a/packaging/rules/debian-one-click-install/rules
+++ b/packaging/rules/debian-one-click-install/rules
@@ -17,9 +17,15 @@ RING_ALL_IN_ONE_PKG_NAME="jami-all"
 
 # Bundled packages from contrib
 BUNDLED_PKGS = .ffmpeg
-ifeq ($(DISTRIBUTION),raspbian_10_armhf)
+ifeq (raspbian_10_armhf,$(findstring raspbian_10_armhf, $(DISTRIBUTION)))
 # Raspbian's yaml-cpp lib does not work properly
 BUNDLED_PKGS += .yaml-cpp
+# Add host environment variables
+CMAKE_OPTIONS=-DCHOST=${HOST_ARCH} \
+		-DCMAKE_C_COMPILER=${HOST_ARCH}-gcc \
+		-DCMAKE_CXX_COMPILER=${HOST_ARCH}-g++ \
+		-DCMAKE_FIND_ROOT_PATH=/usr/${HOST_ARCH} \
+		-DPKG_CONFIG_EXECUTABLE=/usr/bin/${HOST_ARCH}-pkg-config
 endif
 
 %:
@@ -44,6 +50,7 @@ override_dh_auto_configure:
 	mkdir -p daemon/contrib/native
 	cd daemon/contrib/native && \
         ../bootstrap \
+				--host=${HOST_ARCH} \
 				--disable-downloads \
 				--no-checksums \
 				--disable-ogg \
@@ -63,7 +70,8 @@ override_dh_auto_configure:
 		./autogen.sh && \
 		./configure \
 				--prefix=/usr \
-				--disable-shared
+				--disable-shared \
+				--host=${HOST_ARCH}
 
 	#############################
 	## libringclient configure ##
@@ -75,6 +83,7 @@ override_dh_auto_configure:
 			-DRING_BUILD_DIR=$(CURDIR)/daemon/src \
 			-DCMAKE_INSTALL_PREFIX=/usr \
 			-DCMAKE_BUILD_TYPE=Debug \
+			$(CMAKE_OPTIONS) \
 			..
 
 	############################
@@ -87,6 +96,7 @@ override_dh_auto_configure:
 			-DCMAKE_INSTALL_PREFIX=/usr \
 			-DLibRingClient_PROJECT_DIR=/$(CURDIR)/lrc \
 			-DGSETTINGS_LOCALCOMPILE=OFF \
+			$(CMAKE_OPTIONS) \
 			..
 
 	dh_auto_configure
diff --git a/packaging/rules/debian/rules b/packaging/rules/debian/rules
index ba6d33a8..eeb1965e 100755
--- a/packaging/rules/debian/rules
+++ b/packaging/rules/debian/rules
@@ -19,9 +19,15 @@ RING_DAEMON_PKG_NAME="jami-daemon"
 
 # Bundled packages from contrib
 BUNDLED_PKGS = .ffmpeg
-ifeq ($(DISTRIBUTION),raspbian_10_armhf)
+ifeq (raspbian_10_armhf,$(findstring raspbian_10_armhf, $(DISTRIBUTION)))
 # Raspbian's yaml-cpp lib does not work properly
 BUNDLED_PKGS += .yaml-cpp
+# Add host environment variables
+CMAKE_OPTIONS=-DCHOST=${HOST_ARCH} \
+		-DCMAKE_C_COMPILER=${HOST_ARCH}-gcc \
+		-DCMAKE_CXX_COMPILER=${HOST_ARCH}-g++ \
+		-DCMAKE_FIND_ROOT_PATH=/usr/${HOST_ARCH} \
+		-DPKG_CONFIG_EXECUTABLE=/usr/bin/${HOST_ARCH}-pkg-config
 endif
 
 %:
@@ -46,6 +52,7 @@ override_dh_auto_configure:
 	mkdir -p daemon/contrib/native
 	cd daemon/contrib/native && \
         ../bootstrap \
+				--host=${HOST_ARCH} \
 				--disable-downloads \
 				--no-checksums \
 				--disable-ogg \
@@ -65,7 +72,8 @@ override_dh_auto_configure:
 		./autogen.sh && \
 		./configure \
 				--prefix=/usr \
-				--disable-shared
+				--disable-shared \
+				--host=${HOST_ARCH}
 
 	#############################
 	## libringclient configure ##
@@ -77,6 +85,7 @@ override_dh_auto_configure:
 			-DRING_BUILD_DIR=$(CURDIR)/daemon/src \
 			-DCMAKE_INSTALL_PREFIX=/usr \
 			-DCMAKE_BUILD_TYPE=Debug \
+			$(CMAKE_OPTIONS) \
 			..
 
 	############################
@@ -89,6 +98,7 @@ override_dh_auto_configure:
 			-DCMAKE_INSTALL_PREFIX=/usr \
 			-DLibRingClient_PROJECT_DIR=/$(CURDIR)/lrc \
 			-DGSETTINGS_LOCALCOMPILE=OFF \
+			$(CMAKE_OPTIONS) \
 			..
 
 	dh_auto_configure
diff --git a/scripts/build-package-debian.sh b/scripts/build-package-debian.sh
index 43733b4b..85795a47 100755
--- a/scripts/build-package-debian.sh
+++ b/scripts/build-package-debian.sh
@@ -41,11 +41,23 @@ else
     cp -r ${DEBIAN_PACKAGING_OVERRIDE}/* debian/
 fi
 
+DPKG_BUILD_OPTIONS=""
+MKBUILD_OPTIONS=""
+# Set the host architecture as armhf and add some specific architecture
+# options to the package builder.
+if grep -q "raspbian_10_armhf" <<< "${DISTRIBUTION}"; then
+    echo "Adding armhf as the host architecture."
+    export HOST_ARCH=arm-linux-gnueabihf
+    dpkg --add-architecture armhf
+    DPKG_BUILD_OPTIONS="${DPKG_BUILD_OPTIONS} -a armhf"
+    MKBUILD_OPTIONS="${MKBUILD_OPTIONS} --host-arch armhf"
+fi
+
 # install build deps
 apt-get clean
 apt-get update
-apt-get upgrade -o Acquire::Retires=10 -y
-mk-build-deps --remove --install debian/control -t "apt-get -y --no-install-recommends"
+apt-get upgrade -o Acquire::Retries=10 -y
+mk-build-deps ${MKBUILD_OPTIONS} --remove --install debian/control -t "apt-get -y --no-install-recommends"
 
 # create changelog file
 DEBEMAIL="The Jami project <jami@gnu.org>" dch --create --package jami --newversion ${DEBIAN_VERSION} "Automatic nightly release"
@@ -74,7 +86,7 @@ cd ring-project
 cp --verbose -r /opt/ring-project/debian .
 
 # create the package
-dpkg-buildpackage -uc -us
+dpkg-buildpackage -uc -us ${DPKG_BUILD_OPTIONS}
 
 # move the artifacts to output
 cd ..
-- 
GitLab