From 5d8a42d596b60c4b6126e225fc7f716b7b7c298e Mon Sep 17 00:00:00 2001
From: Ray Donnelly <mingw.android@gmail.com>
Date: Tue, 2 Jan 2018 15:51:41 +0000
Subject: [PATCH 08/17] Linux: Do not modify LD_LIBRARY_PATH

---
 Makeconf.in     |  3 ++-
 configure.ac    | 15 ++++++++++++---
 etc/Makeconf.in |  3 ++-
 etc/ldpaths.in  | 14 ++++++++++++++
 4 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/Makeconf.in b/Makeconf.in
index 0e1b82437b..a3e93bdda7 100644
--- a/Makeconf.in
+++ b/Makeconf.in
@@ -64,7 +64,8 @@ MAIN_CFLAGS = @MAIN_CFLAGS@
 MAIN_FFLAGS = @MAIN_FFLAGS@
 MAIN_LD = @MAIN_LD@@BUILD_LTO_TRUE@ $(CFLAGS)
 MAIN_LDFLAGS = @MAIN_LDFLAGS@ @WANT_R_SHLIB_FALSE@ @USE_EXPORTFILES_TRUE@ -Wl,-bE:$(top_builddir)/etc/R.exp
-MAIN_LINK = $(MAIN_LD) $(MAIN_LDFLAGS) $(LIBR0) $(LDFLAGS)
+RPATH_LDFLAGS = -Wl,-rpath,$(abs_top_builddir)/lib @RPATH_LDFLAGS@
+MAIN_LINK = $(MAIN_LD) $(MAIN_LDFLAGS) $(LIBR0) $(LDFLAGS) $(RPATH_LDFLAGS)
 ## need this for bootstrapping
 MKINSTALLDIRS = @R_SHELL@ $(top_srcdir)/src/scripts/mkinstalldirs.in
 NOTANGLE = @NOTANGLE@
diff --git a/configure.ac b/configure.ac
index 1e70ea2dfb..5e31a157b2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -692,7 +692,7 @@ AM_CONDITIONAL(CROSS_COMPILING, [test "${cross_compiling}" = yes])
 
 ### * Checks for libraries.
 
-## Set up LD_LIBRARY_PATH or equivalent.
+## Add -Wl,-rpath, entries to RPATH_LDFLAGS.
 ## <FIXME>
 ## What is this doing *HERE*?
 ## Should be needed for tests using AC_RUN_IFELSE()?
@@ -719,6 +719,7 @@ case "${host_os}" in
         -L*)
 	  lib=`echo ${arg} | sed "s/^-L//"`
 	  R_SH_VAR_ADD(R_LD_LIBRARY_PATH, [${lib}], [${PATH_SEPARATOR}])
+          R_SH_VAR_ADD(RPATH_LDFLAGS, [-Wl,-rpath,${lib}])
 	  ;;
       esac
     done
@@ -738,13 +739,20 @@ case "${host_os}" in
     Rshlibpath_var=${shlibpath_var}
 esac
 AC_SUBST(shlibpath_var)
-## Export LD_LIBRARY_PATH or equivalent.
+## Export LD_LIBRARY_PATH or equivalent (except we do not want to do that
+## instead we want to use -Wl,-rpath .. only doing this for Linux for now).
 if eval "test -z \"\${${Rshlibpath_var}}\""; then
   eval "${Rshlibpath_var}=\"${R_LD_LIBRARY_PATH}\""
 else
   eval "${Rshlibpath_var}=\"${R_LD_LIBRARY_PATH}${PATH_SEPARATOR}\${${Rshlibpath_var}}\""
 fi
-eval "export ${Rshlibpath_var}"
+case "${host_os}" in
+  linux*)
+    ;;
+  *)
+    eval "export ${Rshlibpath_var}"
+    ;;
+esac
 AC_SUBST(Rshlibpath_var)
 
 ## record how to strip shared/dynamic libraries.
@@ -1692,6 +1700,7 @@ AM_CONDITIONAL(DYLIB_UNDEFINED_ALLOWED, [test "x${dylib_undefined_allowed}" = xy
 
 AC_SUBST(MAIN_LD)
 AC_SUBST(MAIN_LDFLAGS)
+AC_SUBST(RPATH_LDFLAGS)
 AC_SUBST(CPICFLAGS)
 AC_SUBST(CXXPICFLAGS)
 AC_SUBST(DYLIB_LD)
diff --git a/etc/Makeconf.in b/etc/Makeconf.in
index cb1b259ba5..1e19bd2448 100644
--- a/etc/Makeconf.in
+++ b/etc/Makeconf.in
@@ -83,7 +83,8 @@ LTO = @LTOALL@
 ## needed to build applications linking to static libR
 MAIN_LD = @MAIN_LD@
 MAIN_LDFLAGS = @MAIN_LDFLAGS@
-MAIN_LINK = $(MAIN_LD) $(MAIN_LDFLAGS) $(LDFLAGS)
+RPATH_LDFLAGS = -Wl,-rpath,$(abs_top_builddir)/lib @RPATH_LDFLAGS@
+MAIN_LINK = $(MAIN_LD) $(MAIN_LDFLAGS) $(LDFLAGS) $(RPATH_LDFLAGS)
 MKINSTALLDIRS = "$(R_HOME)/bin/mkinstalldirs"
 OBJC = @OBJC@
 OBJCFLAGS = @OBJCFLAGS@ $(LTO)
diff --git a/etc/ldpaths.in b/etc/ldpaths.in
index 314d364f5c..3fb5331f9c 100644
--- a/etc/ldpaths.in
+++ b/etc/ldpaths.in
@@ -1,3 +1,17 @@
+# https://github.com/conda/conda/issues/1679:
+#  Internally R_system() calls system() which
+# uses /bin/sh to launch various programs. If
+# /bin/sh is called with LD_LIBRARY_PATH that
+# loads condas shared libraries things break.
+#  It may be that not setting LD_LIBRARY_PATH
+# causes other things to break, in which case
+# R_system() will need to be modified so that
+# it calls execve() with an environment which
+# has these modifications to LD_LIBRARY_PATH
+# removed which may be tricky to orchestrate
+if [ "$(uname -s)" = "Linux" ]; then
+  return 0
+fi
 : ${JAVA_HOME=@JAVA_HOME@}
 : ${R_JAVA_LD_LIBRARY_PATH=@R_JAVA_LD_LIBRARY_PATH@}
 if test -n "@R_LD_LIBRARY_PATH@"; then
-- 
2.18.0

