debian: support multiarch for libhandle

For nearly a decade now, Debian and derivatives have supported the
"multiarch" layout, where shared libraries are installed to
/lib/<gcc triple>/ instead of /lib.  This enables a single rootfs to
support binaries from multiple architectures (e.g. i386 inside an amd64
system).  We should follow this, since libhandle is useful.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
diff --git a/configure.ac b/configure.ac
index 348af14..e24fb0c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -60,6 +60,11 @@
 	enable_lib64=yes)
 AC_SUBST(enable_lib64)
 
+AC_ARG_WITH([multiarch],
+[  --with-multiarch=ARCH   Specify the multiarch triplet],
+	multiarch=$withval)
+AC_SUBST(multiarch)
+
 AC_ARG_ENABLE(librt,
 [  --enable-librt=[yes/no]   Enable librt support [default=yes]],,
 	enable_librt=yes)
@@ -110,6 +115,11 @@
 esac
 
 #
+# If the user specified a multiarch path then disable lib64
+#
+test -n "$multiarch" && enable_lib64=no
+
+#
 # Some important tools should be installed into the root partitions.
 #
 # Check whether exec_prefix=/usr: and install them to /sbin in that
@@ -143,6 +153,7 @@
 AC_PACKAGE_GLOBALS(xfsprogs)
 AC_PACKAGE_UTILITIES(xfsprogs)
 AC_MULTILIB($enable_lib64)
+AC_MULTIARCH($multiarch)
 AC_RT($enable_librt)
 
 AC_PACKAGE_NEED_INI_H
diff --git a/debian/rules b/debian/rules
index df023c6..95df483 100755
--- a/debian/rules
+++ b/debian/rules
@@ -13,6 +13,7 @@
 
 DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
 DEB_HOST_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
+DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
 
 version = $(shell dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2 | cut -d '-' -f 1)
 target ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
@@ -28,6 +29,7 @@
 
 configure_options = \
 	--build=$(DEB_BUILD_GNU_TYPE) \
+	--with-multiarch=$(DEB_HOST_MULTIARCH) \
 	--host=$(DEB_HOST_GNU_TYPE) \
 	--disable-ubsan \
 	--disable-addrsan \
@@ -97,8 +99,8 @@
 	$(pkgme)  $(MAKE) dist
 	install -D -m 0755 debian/local/initramfs.hook debian/xfsprogs/usr/share/initramfs-tools/hooks/xfs
 	rmdir debian/xfslibs-dev/usr/share/doc/xfsprogs
-	rm -f debian/xfslibs-dev/lib/libhandle.la
-	rm -f debian/xfslibs-dev/lib/libhandle.a
+	rm -f debian/xfslibs-dev/lib/$(DEB_HOST_MULTIARCH)/libhandle.la
+	rm -f debian/xfslibs-dev/lib/$(DEB_HOST_MULTIARCH)/libhandle.a
 	rm -fr debian/xfslibs-dev/usr/lib
 	dh_installdocs -XCHANGES
 	dh_installchangelogs
diff --git a/include/builddefs.in b/include/builddefs.in
index 0bb3643..626db21 100644
--- a/include/builddefs.in
+++ b/include/builddefs.in
@@ -51,6 +51,7 @@
 PKG_ROOT_SBIN_DIR = @root_sbindir@
 PKG_ROOT_LIB_DIR= @root_libdir@@libdirsuffix@
 PKG_LIB_DIR	= @libdir@@libdirsuffix@
+PKG_LIB_SCRIPT_DIR	= @libdir@
 PKG_INC_DIR	= @includedir@/xfs
 DK_INC_DIR	= @includedir@/disk
 PKG_MAN_DIR	= @mandir@
diff --git a/m4/multilib.m4 b/m4/multilib.m4
index 8d991d8..862fd8a 100644
--- a/m4/multilib.m4
+++ b/m4/multilib.m4
@@ -41,3 +41,15 @@
   fi
   AC_SUBST(libdirsuffix)
 ])
+
+dnl AC_MULTIARCH creates a variable libdirsuffix containing the suffix of the
+dnl libdir to follow the Debian multiarch spec.  (i.e. "/$multiarch")
+AC_DEFUN([AC_MULTIARCH],
+[
+  if test -n "$1"; then
+    libdirsuffix="/$1"
+    AC_MSG_CHECKING([multiarch triplet])
+    AC_MSG_RESULT([$multiarch])
+  fi
+  AC_SUBST(libdirsuffix)
+])
diff --git a/scrub/Makefile b/scrub/Makefile
index 335e1e8..74492fb 100644
--- a/scrub/Makefile
+++ b/scrub/Makefile
@@ -23,7 +23,7 @@
 CRONTABS = xfs_scrub_all.cron
 OPTIONAL_TARGETS += $(CRONTABS)
 # Don't enable the crontab by default for now
-CROND_DIR = $(PKG_LIB_DIR)/$(PKG_NAME)
+CROND_DIR = $(PKG_LIB_SCRIPT_DIR)/$(PKG_NAME)
 endif
 
 endif	# scrub_prereqs
@@ -119,8 +119,9 @@
 	@echo "    [SED]    $@"
 	$(Q)$(SED) -e "s|@sbindir@|$(PKG_SBIN_DIR)|g" \
 		   -e "s|@scrub_args@|$(XFS_SCRUB_ARGS)|g" \
-		   -e "s|@pkg_lib_dir@|$(PKG_LIB_DIR)|g" \
-		   -e "s|@pkg_name@|$(PKG_NAME)|g" < $< > $@
+		   -e "s|@pkg_lib_dir@|$(PKG_LIB_SCRIPT_DIR)|g" \
+		   -e "s|@pkg_name@|$(PKG_NAME)|g" \
+		   < $< > $@
 
 %.cron: %.cron.in $(builddefs)
 	@echo "    [SED]    $@"
@@ -129,8 +130,8 @@
 install-systemd: default $(SYSTEMD_SERVICES)
 	$(INSTALL) -m 755 -d $(SYSTEMD_SYSTEM_UNIT_DIR)
 	$(INSTALL) -m 644 $(SYSTEMD_SERVICES) $(SYSTEMD_SYSTEM_UNIT_DIR)
-	$(INSTALL) -m 755 -d $(PKG_LIB_DIR)/$(PKG_NAME)
-	$(INSTALL) -m 755 xfs_scrub_fail $(PKG_LIB_DIR)/$(PKG_NAME)
+	$(INSTALL) -m 755 -d $(PKG_LIB_SCRIPT_DIR)/$(PKG_NAME)
+	$(INSTALL) -m 755 xfs_scrub_fail $(PKG_LIB_SCRIPT_DIR)/$(PKG_NAME)
 
 install-crond: default $(CRONTABS)
 	$(INSTALL) -m 755 -d $(CROND_DIR)