From e4f50b7935a3948ca8e02b99acfe2e225bf4706c Mon Sep 17 00:00:00 2001
From: Giovanni Bussi <giovanni.bussi@gmail.com>
Date: Mon, 11 Jun 2018 15:32:18 +0200
Subject: [PATCH] Allow relative paths in OSX libs

I added a configure flag --disable-absolute-install-name
to allow OSX shared libraries to behave such as Linux libraries

This will need DYLD_LIBRARY_PATH to be set, which is not a good idea.

I have to add some documentation.
---
 configure             | 59 ++++++++++++++++++++++++++++++++++++++-----
 configure.ac          | 32 ++++++++++++++++++-----
 src/lib/Makefile      |  5 ++--
 src/lib/modulefile.in | 13 +++++++---
 4 files changed, 90 insertions(+), 19 deletions(-)

diff --git a/configure b/configure
index b5f7b6d89..be6223f2c 100755
--- a/configure
+++ b/configure
@@ -625,8 +625,8 @@ program_name
 pkgconfig_bin
 make_static_archive
 AR_CR
-use_absolute_soname
 LD_RO
+use_absolute_soname
 program_can_run_mpi
 program_can_run
 xxd
@@ -715,6 +715,7 @@ enable_shared
 enable_dependency_tracking
 enable_rpath
 enable_absolute_soname
+enable_absolute_install_name
 enable_ld_r
 enable_ar_cr
 enable_static_archive
@@ -1390,6 +1391,9 @@ Optional Features:
   --enable-absolute-soname
                           enable store absolute soname (Linux only - this is
                           the default behavior on OSX), default: no
+  --enable-absolute-install-name
+                          enable store absolute relative (OSX only - disable
+                          to have a behavior similar to Linux), default: yes
   --enable-ld-r           enable group object files, default: yes
   --enable-ar-cr          enable use ar to build libplumedWrapper.a, default:
                           yes
@@ -2706,6 +2710,24 @@ fi
 
 
 
+absolute_install_name=
+# Check whether --enable-absolute-install-name was given.
+if test "${enable_absolute_install_name+set}" = set; then :
+  enableval=$enable_absolute_install_name; case "${enableval}" in
+             (yes) absolute_install_name=true ;;
+             (no)  absolute_install_name=false ;;
+             (*)   as_fn_error $? "wrong argument to --enable-absolute-install-name" "$LINENO" 5 ;;
+  esac
+else
+  case "yes" in
+             (yes) absolute_install_name=true ;;
+             (no)  absolute_install_name=false ;;
+  esac
+
+fi
+
+
+
 ld_r=
 # Check whether --enable-ld-r was given.
 if test "${enable_ld_r+set}" = set; then :
@@ -8886,6 +8908,35 @@ $as_echo "$as_me: Regtest suite will use env var PLUMED_MPIRUN to run MPI tests
   fi
 fi
 
+if test "$SOEXT" == "dylib" ; then
+  use_absolute_soname=yes
+else
+  use_absolute_soname=no
+fi
+
+
+if test "$absolute_soname" == true ; then
+  if test "$SOEXT" == "dylib" ; then
+    { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: --enable-absolute-soname has no effect on OSX" >&5
+$as_echo "$as_me: WARNING: --enable-absolute-soname has no effect on OSX" >&2;}
+  else
+    { $as_echo "$as_me:${as_lineno-$LINENO}: enabling absolute soname. Full path will be hardcoded in plumed library soname" >&5
+$as_echo "$as_me: enabling absolute soname. Full path will be hardcoded in plumed library soname" >&6;}
+    use_absolute_soname=yes
+  fi
+fi
+
+if test "$absolute_install_name" == false ; then
+  if test "$SOEXT" == "so" ; then
+    { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: --disable-absolute-install-name has no effect on Linux" >&5
+$as_echo "$as_me: WARNING: --disable-absolute-install-name has no effect on Linux" >&2;}
+  else
+    { $as_echo "$as_me:${as_lineno-$LINENO}: enabling relative install_name. You will have to set DYLD_LIBRARY_PATH yor plumed libraries to be found at runtime" >&5
+$as_echo "$as_me: enabling relative install_name. You will have to set DYLD_LIBRARY_PATH yor plumed libraries to be found at runtime" >&6;}
+    use_absolute_soname=no
+  fi
+fi
+
 #### This further tests are required to allow linking with non c++ compiler
 { $as_echo "$as_me:${as_lineno-$LINENO}: PLUMED seems to be configured properly!" >&5
 $as_echo "$as_me: PLUMED seems to be configured properly!" >&6;}
@@ -8935,12 +8986,6 @@ done
 
 fi
 
-use_absolute_soname=no
-
-if test "$absolute_soname" == true ; then
-  use_absolute_soname=yes
-fi
-
 
 AR_CR=
 if test "$ar_cr" == true ; then
diff --git a/configure.ac b/configure.ac
index 039727a49..66caf480f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -238,6 +238,7 @@ PLUMED_CONFIG_ENABLE([shared],[shared libs],[yes])
 PLUMED_CONFIG_ENABLE([dependency-tracking],[dependency tracking],[yes])
 PLUMED_CONFIG_ENABLE([rpath],[store rpath],[no])
 PLUMED_CONFIG_ENABLE([absolute-soname],[store absolute soname (Linux only - this is the default behavior on OSX)],[no])
+PLUMED_CONFIG_ENABLE([absolute-install-name],[store absolute relative (OSX only - disable to have a behavior similar to Linux)],[yes])
 PLUMED_CONFIG_ENABLE([ld-r],[group object files],[yes])
 PLUMED_CONFIG_ENABLE([ar-cr],[use ar to build libplumedWrapper.a],[yes])
 PLUMED_CONFIG_ENABLE([static-archive],[try to build libplumed.a for static linking],[yes])
@@ -929,6 +930,31 @@ if test $mpi_found == ok ; then
   fi
 fi
 
+if test "$SOEXT" == "dylib" ; then
+  use_absolute_soname=yes
+else
+  use_absolute_soname=no
+fi
+
+AC_SUBST(use_absolute_soname)
+if test "$absolute_soname" == true ; then
+  if test "$SOEXT" == "dylib" ; then
+    AC_MSG_WARN([--enable-absolute-soname has no effect on OSX])
+  else
+    AC_MSG_NOTICE([enabling absolute soname. Full path will be hardcoded in plumed library soname])
+    use_absolute_soname=yes
+  fi
+fi
+
+if test "$absolute_install_name" == false ; then
+  if test "$SOEXT" == "so" ; then
+    AC_MSG_WARN([--disable-absolute-install-name has no effect on Linux])
+  else
+    AC_MSG_NOTICE([enabling relative install_name. You will have to set DYLD_LIBRARY_PATH yor plumed libraries to be found at runtime])
+    use_absolute_soname=no
+  fi
+fi
+
 #### This further tests are required to allow linking with non c++ compiler
 AC_MSG_NOTICE([PLUMED seems to be configured properly!])
 AC_MSG_NOTICE([**************************])
@@ -973,12 +999,6 @@ done
 
 fi
 
-use_absolute_soname=no
-AC_SUBST(use_absolute_soname)
-if test "$absolute_soname" == true ; then
-  use_absolute_soname=yes
-fi
-
 AC_SUBST(AR_CR)
 AR_CR=
 if test "$ar_cr" == true ; then
diff --git a/src/lib/Makefile b/src/lib/Makefile
index a1a34b4b2..6533c7f50 100644
--- a/src/lib/Makefile
+++ b/src/lib/Makefile
@@ -20,8 +20,6 @@ ifeq ($(SOEXT),dylib)
 # Extra flags required to properly compile the python interface
   PYTHON_EXTRA_LDFLAGS:=-undefined dynamic_lookup
   SONAME_OPTION:=-Wl,-install_name
-# on OSX we always hardcode paths
-  use_absolute_soname:=yes
 else
   PYTHON_EXTRA_LDFLAGS:=
   SONAME_OPTION:=-Wl,-soname
@@ -146,7 +144,8 @@ endif
            sed "s|@_program_name_@|$(program_name)|" | \
            sed "s|@_libdir_@|$(libdir)|" | \
            sed "s|@_bindir_@|$(bindir)|" | \
-           sed "s|@_includedir_@|$(includedir)|" \
+           sed "s|@_includedir_@|$(includedir)|" | \
+           sed "s|@_use_absolute_soname_@|$(use_absolute_soname)|" \
 	   > install/modulefile
 	@cat pkgconfig.in | \
            sed "s|@_SOEXT_@|$(SOEXT)|" | \
diff --git a/src/lib/modulefile.in b/src/lib/modulefile.in
index f5dafc90f..4e650fa9d 100644
--- a/src/lib/modulefile.in
+++ b/src/lib/modulefile.in
@@ -14,6 +14,9 @@ set soext  "@_SOEXT_@"
 # this is the name of the plumed program
 # it is usually plumed
 set progname "@_program_name_@"
+# if yes, soname is absolute - does not require setting (DY)LD_LIBRARY_PATH
+# otherwise, soname is relative - requires require setting (DY)LD_LIBRARY_PATH (common in Linux)
+set use_absolute_soname "@_use_absolute_soname_@"
 
 # everything past this line should not need any editing
 
@@ -48,9 +51,13 @@ if { [module-info mode load] && [ info exists ::env(PLUMED_KERNEL) ] } {
     puts stderr " (typically means that you are loading two plumed modules, both defining dynamic libraries)"
 }
 
-# ld_library_path is only set on Linux
-if { $soext == "so"  } {
-  prepend-path  LD_LIBRARY_PATH    $libdir
+# if we use a relative soname (as it is common in Linux) then the (DY)LD_LIBRARY_PATH should be set.
+if { $use_absolute_soname != "yes" } {
+  if { $soext == "dylib"  } {
+    prepend-path  DYLD_LIBRARY_PATH    $libdir
+  } else {
+    prepend-path  LD_LIBRARY_PATH    $libdir
+  }
 }
 
 # python path
-- 
GitLab