From 6fe39babda9dbb853de5eab3d3e2efba6c0e77f3 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 06/13] 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 59b8d7c..0ad81ad 100644
--- a/Makeconf.in
+++ b/Makeconf.in
@@ -67,7 +67,8 @@ MAIN_CFLAGS = @MAIN_CFLAGS@
 MAIN_FFLAGS = @MAIN_FFLAGS@
 MAIN_LD = @MAIN_LD@@BUILD_LTO_TRUE@ $(CFLAGS) $(CPICFLAGS) @LTO_LD@
 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 e033c19..075f3dd 100644
--- a/configure.ac
+++ b/configure.ac
@@ -877,7 +877,7 @@ AC_SUBST(LIBTOOL_DEPS)
 
 ### * 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()?
@@ -904,6 +904,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
@@ -926,13 +927,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.
@@ -1875,6 +1883,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 d84a912..ec29989 100644
--- a/etc/Makeconf.in
+++ b/etc/Makeconf.in
@@ -108,7 +108,8 @@ LTO_FC_OPT = @LTO_FC@
 ## 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"
 NM = @NM@
 OBJC = @OBJC@
diff --git a/etc/ldpaths.in b/etc/ldpaths.in
index 314d364..3fb5331 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.39.3 (Apple Git-146)

