From 6fc5ee1f4295b4789d0c78f6fedff02768408dce Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Wed, 25 Apr 2018 17:41:43 +0100
Subject: [PATCH] tools: Add a build option to add a -rpath

We have to do this in two stages, because passing ${ORIGIN} through
Autoconf, Automake and Make is a massive pain: first write in a
dummy rpath that starts with /_ORIGIN_ (implemented here), then
rewrite it to start with ${ORIGIN} with chrpath (to be done
externally).

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 Makefile.am  |  9 +++++++++
 configure.ac | 13 +++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/Makefile.am b/Makefile.am
index 4f560e930..efee56afe 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -34,6 +34,11 @@ pkgconfig_DATA          = data/libcapsule.pc data/libcapsule-tools.pc
 nobase_include_HEADERS  = capsule/capsule.h
 dist_doc_DATA           = $(foreach doc,$(static_docs),doc/$(doc).txt)
 
+if ENABLE_TOOLS_RPATH
+# Use -Wl to hide it from libtool
+tools_ldflags           = -Wl,-rpath,$(enable_tools_rpath)
+endif
+
 if ENABLE_HOST_PREFIX
 bin_SCRIPTS             = data/$(HOST_PREFIX)capsule-mkstublib
 
@@ -83,16 +88,20 @@ capsule_elf_dump_LDADD          = utils/libutils.la -ldl
 # This looks redundant, but it forces Automake to build separate
 # instantiations of dump.o for elf-dump and libcapsule
 capsule_elf_dump_CFLAGS         = $(AM_CFLAGS)
+capsule_elf_dump_LDFLAGS        = $(AM_LDFLAGS) $(tools_ldflags)
 
 capsule_capture_libs_SOURCES = utils/capture-libs.c
 capsule_capture_libs_LDADD   = utils/libld.la
+capsule_capture_libs_LDFLAGS = $(AM_LDFLAGS) $(tools_ldflags)
 
 # now the rules for building our installables:
 capsule_symbols_SOURCES = utils/print-libstubs.c
 capsule_symbols_LDADD   = utils/libld.la
+capsule_symbols_LDFLAGS = $(AM_LDFLAGS) $(tools_ldflags)
 
 capsule_version_SOURCES = utils/print-version.c
 capsule_version_LDADD   = utils/libld.la
+capsule_version_LDFLAGS = $(AM_LDFLAGS) $(tools_ldflags)
 
 libcapsule_la_SOURCES   = capsule/capsule-dlmopen.c  \
                           capsule/capsule-relocate.c \
diff --git a/configure.ac b/configure.ac
index 8b9defb2c..0b15fdf43 100644
--- a/configure.ac
+++ b/configure.ac
@@ -82,6 +82,19 @@ AS_IF([test "x$enable_host_prefix" = xno],
   [enable_host_prefix=""])
 AC_SUBST([HOST_PREFIX], [$enable_host_prefix])
 
+AC_ARG_ENABLE([tools-rpath],
+  [AS_HELP_STRING([--enable-tools-rpath=RPATH/yes/no],
+    [install link to tools with a RPATH ready to override with chrpath])],
+  [],
+  [enable_tools_rpath=no])
+
+AS_IF([test "x$enable_tools_rpath" = xyes],
+  [enable_tools_rpath="/_ORIGIN_/__/lib/$host"])
+AS_IF([test "x$enable_tools_rpath" = xno],
+  [enable_tools_rpath=""])
+AC_SUBST([enable_tools_rpath])
+AM_CONDITIONAL([ENABLE_TOOLS_RPATH], [test "x$enable_tools_rpath" != x])
+
 AC_SEARCH_LIBS([dlmopen], [dl])
 AC_PATH_PROG([XSLTPROC], [xsltproc])
 GTK_DOC_CHECK
-- 
GitLab